From ca7dabdd0bdf72215f5875beed0093cb3b30e2cb Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Mon, 29 Jul 2024 08:55:57 +0000 Subject: [PATCH 1/4] feat(pdk): add negative value to support unlimited body size It is the cherry-pick of kong/kong-ee#9472. KAG-4698 --- .../kong/feat-pdk-unlimited-body-size.yml | 3 ++ kong/pdk/request.lua | 7 +++-- t/01-pdk/04-request/15-get_raw_body.t | 28 ++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml diff --git a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml new file mode 100644 index 000000000000..c8b7febcc0dd --- /dev/null +++ b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml @@ -0,0 +1,3 @@ +message: Added negative value to support unlimited body size. +type: feature +scope: PDK diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index f588f06c8fb3..24204bf902da 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -695,13 +695,14 @@ local function new(self) -- If the size of the body is greater than the Nginx buffer size (set by -- `client_body_buffer_size`), this function fails and returns an error -- message explaining this limitation, unless `max_allowed_file_size` - -- is set and larger than the body size buffered to disk. + -- is set and less than 0 or larger than the body size buffered to disk. -- Use of `max_allowed_file_size` requires Kong to read data from filesystem -- and has performance implications. -- -- @function kong.request.get_raw_body -- @phases rewrite, access, response, admin_api - -- @max_allowed_file_size[opt] number the max allowed file size to be read from + -- @max_allowed_file_size[opt] number the max allowed file size to be read from, + -- less than zero mean no limit. -- @treturn string|nil The plain request body or nil if it does not fit into -- the NGINX temporary buffer. -- @treturn nil|string An error message. @@ -731,7 +732,7 @@ local function new(self) end local size = file:seek("end") or 0 - if size > max_allowed_file_size then + if max_allowed_file_size >= 0 and size > max_allowed_file_size then return nil, ("request body file too big: %d > %d"):format(size, max_allowed_file_size) end diff --git a/t/01-pdk/04-request/15-get_raw_body.t b/t/01-pdk/04-request/15-get_raw_body.t index 490354ac7408..52e6caaccb4a 100644 --- a/t/01-pdk/04-request/15-get_raw_body.t +++ b/t/01-pdk/04-request/15-get_raw_body.t @@ -170,4 +170,30 @@ body length: 20000 --- response_body body err: request body file too big: 20000 > 19999 --- no_error_log -[error] \ No newline at end of file +[error] + + + +=== TEST 8: request.get_raw_body() returns correctly if max_allowed_file_size is less than 0 +--- http_config eval: $t::Util::HttpConfig +--- config + location = /t { + access_by_lua_block { + local PDK = require "kong.pdk" + local pdk = PDK.new() + + local body, err = pdk.request.get_raw_body(-1) + if body then + ngx.say("body length: ", #body) + + else + ngx.say("body err: ", err) + end + } + } +--- request eval +"GET /t\r\n" . ("a" x 20000) +--- response_body +body length: 20000 +--- no_error_log +[error] From d53d5fc7ae9f2b9bf0de8a0e9ce59735482d53fc Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Tue, 30 Jul 2024 05:56:52 +0000 Subject: [PATCH 2/4] add more detail --- changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml | 2 +- kong/pdk/request.lua | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml index c8b7febcc0dd..48f137dc7a1e 100644 --- a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml +++ b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml @@ -1,3 +1,3 @@ -message: Added negative value to support unlimited body size. +message: Added negative value to support unlimited body size. When parameter `max_allowed_file_size` is negative, `get_raw_body` will return the entire body, but the size of this body will still be limited by Nginx's `client_max_body_size`. type: feature scope: PDK diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index 24204bf902da..86e10cf81a1f 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -702,7 +702,8 @@ local function new(self) -- @function kong.request.get_raw_body -- @phases rewrite, access, response, admin_api -- @max_allowed_file_size[opt] number the max allowed file size to be read from, - -- less than zero mean no limit. + -- less than zero mean no limit, but the size of this body will still be limited + -- by Nginx's client_max_body_size. -- @treturn string|nil The plain request body or nil if it does not fit into -- the NGINX temporary buffer. -- @treturn nil|string An error message. From f489675e54c93ecd1e55e93eb3dc07190e90ec71 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Tue, 30 Jul 2024 09:46:10 +0000 Subject: [PATCH 3/4] fix the grammar error in the comments --- kong/pdk/request.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index 86e10cf81a1f..c22400a5027a 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -702,7 +702,7 @@ local function new(self) -- @function kong.request.get_raw_body -- @phases rewrite, access, response, admin_api -- @max_allowed_file_size[opt] number the max allowed file size to be read from, - -- less than zero mean no limit, but the size of this body will still be limited + -- less than zero means no limit, but the size of this body will still be limited -- by Nginx's client_max_body_size. -- @treturn string|nil The plain request body or nil if it does not fit into -- the NGINX temporary buffer. From 50a4ac352121584038fc6f320efcd0ac031dc9c5 Mon Sep 17 00:00:00 2001 From: Water-Melon Date: Fri, 2 Aug 2024 05:46:21 +0000 Subject: [PATCH 4/4] use 0 to indicate unlimited --- .../unreleased/kong/feat-pdk-unlimited-body-size.yml | 2 +- kong/pdk/request.lua | 8 ++++---- t/01-pdk/04-request/15-get_raw_body.t | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml index 48f137dc7a1e..5cb3f291d15f 100644 --- a/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml +++ b/changelog/unreleased/kong/feat-pdk-unlimited-body-size.yml @@ -1,3 +1,3 @@ -message: Added negative value to support unlimited body size. When parameter `max_allowed_file_size` is negative, `get_raw_body` will return the entire body, but the size of this body will still be limited by Nginx's `client_max_body_size`. +message: Added `0` to support unlimited body size. When parameter `max_allowed_file_size` is `0`, `get_raw_body` will return the entire body, but the size of this body will still be limited by Nginx's `client_max_body_size`. type: feature scope: PDK diff --git a/kong/pdk/request.lua b/kong/pdk/request.lua index c22400a5027a..fbd55a741944 100644 --- a/kong/pdk/request.lua +++ b/kong/pdk/request.lua @@ -695,14 +695,14 @@ local function new(self) -- If the size of the body is greater than the Nginx buffer size (set by -- `client_body_buffer_size`), this function fails and returns an error -- message explaining this limitation, unless `max_allowed_file_size` - -- is set and less than 0 or larger than the body size buffered to disk. + -- is set and equal to 0 or larger than the body size buffered to disk. -- Use of `max_allowed_file_size` requires Kong to read data from filesystem -- and has performance implications. -- -- @function kong.request.get_raw_body -- @phases rewrite, access, response, admin_api -- @max_allowed_file_size[opt] number the max allowed file size to be read from, - -- less than zero means no limit, but the size of this body will still be limited + -- 0 means unlimited, but the size of this body will still be limited -- by Nginx's client_max_body_size. -- @treturn string|nil The plain request body or nil if it does not fit into -- the NGINX temporary buffer. @@ -723,7 +723,7 @@ local function new(self) return "" end - if not max_allowed_file_size then + if not max_allowed_file_size or max_allowed_file_size < 0 then return nil, "request body did not fit into client body buffer, consider raising 'client_body_buffer_size'" end @@ -733,7 +733,7 @@ local function new(self) end local size = file:seek("end") or 0 - if max_allowed_file_size >= 0 and size > max_allowed_file_size then + if max_allowed_file_size > 0 and size > max_allowed_file_size then return nil, ("request body file too big: %d > %d"):format(size, max_allowed_file_size) end diff --git a/t/01-pdk/04-request/15-get_raw_body.t b/t/01-pdk/04-request/15-get_raw_body.t index 52e6caaccb4a..2e47aeb461da 100644 --- a/t/01-pdk/04-request/15-get_raw_body.t +++ b/t/01-pdk/04-request/15-get_raw_body.t @@ -174,7 +174,7 @@ body err: request body file too big: 20000 > 19999 -=== TEST 8: request.get_raw_body() returns correctly if max_allowed_file_size is less than 0 +=== TEST 8: request.get_raw_body() returns correctly if max_allowed_file_size is equal to 0 --- http_config eval: $t::Util::HttpConfig --- config location = /t { @@ -182,7 +182,7 @@ body err: request body file too big: 20000 > 19999 local PDK = require "kong.pdk" local pdk = PDK.new() - local body, err = pdk.request.get_raw_body(-1) + local body, err = pdk.request.get_raw_body(0) if body then ngx.say("body length: ", #body)