From 9bdee14dc77e0076960f2b943822fe7507c5f1d2 Mon Sep 17 00:00:00 2001 From: leslie <59061168+leslie-tsang@users.noreply.github.com> Date: Mon, 21 Feb 2022 17:36:04 +0800 Subject: [PATCH] fix(request-validation): should not limit the urlencoded post args number (#6396) --- apisix/plugins/request-validation.lua | 4 +- t/plugin/request-validation.t | 56 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/apisix/plugins/request-validation.lua b/apisix/plugins/request-validation.lua index 0b3b0f3e18f3..20abffc9277a 100644 --- a/apisix/plugins/request-validation.lua +++ b/apisix/plugins/request-validation.lua @@ -88,7 +88,9 @@ function _M.rewrite(conf, ctx) end if headers["content-type"] == "application/x-www-form-urlencoded" then - req_body, err = ngx.decode_args(body) + -- use 0 to avoid truncated result and keep the behavior as the + -- same as other platforms + req_body, err = ngx.decode_args(body, 0) else -- JSON as default req_body, err = core.json.decode(body) end diff --git a/t/plugin/request-validation.t b/t/plugin/request-validation.t index 92f810896a36..ba42136993a4 100644 --- a/t/plugin/request-validation.t +++ b/t/plugin/request-validation.t @@ -1817,3 +1817,59 @@ qr/object matches none of the required/ --- error_code: 400 --- no_error_log [error] + + + +=== TEST 51: add route for urlencoded post data validation +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "request-validation": { + "body_schema": { + "type": "object", + "required": ["required_payload"], + "properties": { + "required_payload": {"type": "string"} + }, + "rejected_msg": "customize reject message" + } + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/echo" + }]]) + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 52: test urlencoded post data +--- more_headers +Content-Type: application/x-www-form-urlencoded +--- request eval +"POST /echo +" . "a=b&" x 101 . "required_payload=101-hello" +--- response_body eval +qr/101-hello/ +--- no_error_log +[error]