Skip to content

Commit 52af63a

Browse files
committed
feature: ngx.req.get_post_args(), ngx.req.get_uri_args(), ngx.req.get_headers(), ngx.resp.get_headers(), and ngx.decode_args() now would return an error string, "truncated", when the input exceeds the max_args/max_headers limits.
bugfix: ngx.resp.get_headers(): the max_headers limit did not cover builtin headers.
1 parent 19cc6bf commit 52af63a

12 files changed

+1055
-200
lines changed

README.markdown

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,22 +4173,29 @@ For reading *request* headers, use the [ngx.req.get_headers](#ngxreqget_headers)
41734173

41744174
ngx.resp.get_headers
41754175
--------------------
4176-
**syntax:** *headers = ngx.resp.get_headers(max_headers?, raw?)*
4176+
**syntax:** *headers, err = ngx.resp.get_headers(max_headers?, raw?)*
41774177

41784178
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, balancer_by_lua**
41794179

41804180
Returns a Lua table holding all the current response headers for the current request.
41814181

41824182
```lua
41834183

4184-
local h = ngx.resp.get_headers()
4184+
local h, err = ngx.resp.get_headers()
4185+
4186+
if err == "truncated" then
4187+
-- one can choose to ignore or reject the current response here
4188+
end
4189+
41854190
for k, v in pairs(h) do
41864191
...
41874192
end
41884193
```
41894194

41904195
This function has the same signature as [ngx.req.get_headers](#ngxreqget_headers) except getting response headers instead of request headers.
41914196

4197+
Note that a maximum of 100 response headers are parsed by default (including those with the same name) and that additional response headers are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.
4198+
41924199
This API was first introduced in the `v0.9.5` release.
41934200

41944201
[Back to TOC](#nginx-api-for-lua)
@@ -4461,7 +4468,7 @@ See also [ngx.req.set_uri](#ngxreqset_uri).
44614468

44624469
ngx.req.get_uri_args
44634470
--------------------
4464-
**syntax:** *args = ngx.req.get_uri_args(max_args?)*
4471+
**syntax:** *args, err = ngx.req.get_uri_args(max_args?)*
44654472

44664473
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, balancer_by_lua**
44674474

@@ -4471,7 +4478,12 @@ Returns a Lua table holding all the current request URL query arguments.
44714478
44724479
location = /test {
44734480
content_by_lua_block {
4474-
local args = ngx.req.get_uri_args()
4481+
local args, err = ngx.req.get_uri_args()
4482+
4483+
if err == "truncated" then
4484+
-- one can choose to ignore or reject the current request here
4485+
end
4486+
44754487
for key, val in pairs(args) do
44764488
if type(val) == "table" then
44774489
ngx.say(key, ": ", table.concat(val, ", "))
@@ -4523,7 +4535,7 @@ Updating query arguments via the nginx variable `$args` (or `ngx.var.args` in Lu
45234535
```lua
45244536

45254537
ngx.var.args = "a=3&b=42"
4526-
local args = ngx.req.get_uri_args()
4538+
local args, err = ngx.req.get_uri_args()
45274539
```
45284540

45294541
Here the `args` table will always look like
@@ -4535,20 +4547,23 @@ Here the `args` table will always look like
45354547

45364548
regardless of the actual request query string.
45374549

4538-
Note that a maximum of 100 request arguments are parsed by default (including those with the same name) and that additional request arguments are silently discarded to guard against potential denial of service attacks.
4550+
Note that a maximum of 100 request arguments are parsed by default (including those with the same name) and that additional request arguments are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.
45394551

45404552
However, the optional `max_args` function argument can be used to override this limit:
45414553

45424554
```lua
45434555

4544-
local args = ngx.req.get_uri_args(10)
4556+
local args, err = ngx.req.get_uri_args(10)
4557+
if err == "truncated" then
4558+
-- one can choose to ignore or reject the current request here
4559+
end
45454560
```
45464561

45474562
This argument can be set to zero to remove the limit and to process all request arguments received:
45484563

45494564
```lua
45504565

4551-
local args = ngx.req.get_uri_args(0)
4566+
local args, err = ngx.req.get_uri_args(0)
45524567
```
45534568

45544569
Removing the `max_args` cap is strongly discouraged.
@@ -4569,6 +4584,11 @@ Returns a Lua table holding all the current request POST query arguments (of the
45694584
content_by_lua_block {
45704585
ngx.req.read_body()
45714586
local args, err = ngx.req.get_post_args()
4587+
4588+
if err == "truncated" then
4589+
-- one can choose to ignore or reject the current request here
4590+
end
4591+
45724592
if not args then
45734593
ngx.say("failed to get post args: ", err)
45744594
return
@@ -4637,20 +4657,23 @@ That is, they will take Lua boolean values `true`. However, they are different f
46374657

46384658
Empty key arguments are discarded. `POST /test` with body `=hello&=world` will yield empty outputs for instance.
46394659

4640-
Note that a maximum of 100 request arguments are parsed by default (including those with the same name) and that additional request arguments are silently discarded to guard against potential denial of service attacks.
4660+
Note that a maximum of 100 request arguments are parsed by default (including those with the same name) and that additional request arguments are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.
46414661

46424662
However, the optional `max_args` function argument can be used to override this limit:
46434663

46444664
```lua
46454665

4646-
local args = ngx.req.get_post_args(10)
4666+
local args, err = ngx.req.get_post_args(10)
4667+
if err == "truncated" then
4668+
-- one can choose to ignore or reject the current request here
4669+
end
46474670
```
46484671

46494672
This argument can be set to zero to remove the limit and to process all request arguments received:
46504673

46514674
```lua
46524675

4653-
local args = ngx.req.get_post_args(0)
4676+
local args, err = ngx.req.get_post_args(0)
46544677
```
46554678

46564679
Removing the `max_args` cap is strongly discouraged.
@@ -4659,15 +4682,20 @@ Removing the `max_args` cap is strongly discouraged.
46594682

46604683
ngx.req.get_headers
46614684
-------------------
4662-
**syntax:** *headers = ngx.req.get_headers(max_headers?, raw?)*
4685+
**syntax:** *headers, err = ngx.req.get_headers(max_headers?, raw?)*
46634686

46644687
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua**
46654688

46664689
Returns a Lua table holding all the current request headers.
46674690

46684691
```lua
46694692

4670-
local h = ngx.req.get_headers()
4693+
local h, err = ngx.req.get_headers()
4694+
4695+
if err == "truncated" then
4696+
-- one can choose to ignore or reject the current request here
4697+
end
4698+
46714699
for k, v in pairs(h) do
46724700
...
46734701
end
@@ -4698,20 +4726,24 @@ the value of `ngx.req.get_headers()["Foo"]` will be a Lua (array) table such as:
46984726
{"foo", "bar", "baz"}
46994727
```
47004728

4701-
Note that a maximum of 100 request headers are parsed by default (including those with the same name) and that additional request headers are silently discarded to guard against potential denial of service attacks.
4729+
Note that a maximum of 100 request headers are parsed by default (including those with the same name) and that additional request headers are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.
47024730

47034731
However, the optional `max_headers` function argument can be used to override this limit:
47044732

47054733
```lua
47064734

4707-
local headers = ngx.req.get_headers(10)
4735+
local headers, err = ngx.req.get_headers(10)
4736+
4737+
if err == "truncated" then
4738+
-- one can choose to ignore or reject the current request here
4739+
end
47084740
```
47094741

47104742
This argument can be set to zero to remove the limit and to process all request headers received:
47114743

47124744
```lua
47134745

4714-
local headers = ngx.req.get_headers(0)
4746+
local headers, err = ngx.req.get_headers(0)
47154747
```
47164748

47174749
Removing the `max_headers` cap is strongly discouraged.
@@ -5489,13 +5521,13 @@ This method was first introduced in the `v0.3.1rc27` release.
54895521

54905522
ngx.decode_args
54915523
---------------
5492-
**syntax:** *table = ngx.decode_args(str, max_args?)*
5524+
**syntax:** *table, err = ngx.decode_args(str, max_args?)*
54935525

54945526
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**
54955527

54965528
Decodes a URI encoded query-string into a Lua table. This is the inverse function of [ngx.encode_args](#ngxencode_args).
54975529

5498-
The optional `max_args` argument can be used to specify the maximum number of arguments parsed from the `str` argument. By default, a maximum of 100 request arguments are parsed (including those with the same name) and that additional URI arguments are silently discarded to guard against potential denial of service attacks.
5530+
The optional `max_args` argument can be used to specify the maximum number of arguments parsed from the `str` argument. By default, a maximum of 100 request arguments are parsed (including those with the same name) and that additional URI arguments are silently discarded to guard against potential denial of service attacks. Since `v0.10.13`, when the limit is exceeded, it will return a second value which is the string `"truncated"`.
54995531

55005532
This argument can be set to zero to remove the limit and to process all request arguments received:
55015533

@@ -8149,7 +8181,7 @@ This Lua module does not ship with this ngx_lua module itself rather it is shipp
81498181
the
81508182
[lua-resty-core](https://github.com/openresty/lua-resty-core) library.
81518183

8152-
Please refer to the [documentation](https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/ocsp.md)
8184+
Please refer to the [documentation](https://github.com/openresty/lua-resty-core/blob/ocsp-cert-by-lua-2/lib/ngx/ocsp.md)
81538185
for this `ngx.ocsp` Lua module for more details.
81548186

81558187
This feature requires at least ngx_lua `v0.10.0`.

0 commit comments

Comments
 (0)