Open bradib0y opened 4 weeks ago
I fixed the bug and wrote a test case, but I was not able to run it
fix (request-validation.lua)
local body_is_json = true
local FORM_URLENCODED_MEDIA_TYPE="application/x-www-form-urlencoded"
if string.sub(headers["content-type"], 1,
string.len(FORM_URLENCODED_MEDIA_TYPE)) == FORM_URLENCODED_MEDIA_TYPE then
-- use 0 to avoid truncated result and keep the behavior as the
-- same as other platforms
req_body, err = ngx.decode_args(body, 0)
body_is_json = false
else -- JSON as default
req_body, err = core.json.decode(body)
end
test case (request-validation.t)
=== TEST 53: test urlencoded post data with charset parameter
--- more_headers
Content-Type: application/x-www-form-urlencoded; charset=utf-8
--- request eval
"POST /echo
" . "a=b&" x 101 . "required_payload=101-hello"
--- response_body eval
qr/101-hello/
Current Behavior
The Content-Type header value has this required format, according to RFC 7231:
Valid Examples:
Content-Type: application/x-www-form-urlencoded
Content-Type: application/x-www-form-urlencoded; charset=utf-8
The second example is parameterized. In that case, APISIX request-validation plugin will fail with the following error:
parse error: Invalid numeric literal at line 1, column 8
Removing the plugin is the only solution, if you have a parameterized Content-Type header.
Expected Behavior
It should accept the parameterized content type header.
I found the issue in the source code. The body is treated as JSON by default, then the form data type is check with an exact header value match, so if it is extended by params, the body will be treated as JSON, and failing with the validation on the first character, because it is not
{
or[
as expected with JSON.Source from the repo:
apisix\plugins\request-validation.lua
Error Logs
Response
parse error: Invalid numeric literal at line 1, column 8
APISIX logs
Steps to Reproduce
Content-Type: application/x-www-form-urlencoded
Content-Type: application/x-www-form-urlencoded; charset=utf-8
`
Environment
apisix version
): 3.11.0uname -a
): Linux apisix-data-plane-7cd95bcd7-hm9ss 6.6.28-talos #1 SMP Thu Apr 18 16:21:02 UTC 2024 x86_64 GNU/Linuxopenresty -V
ornginx -V
): nginx version: openresty/1.25.3.2curl http://127.0.0.1:9090/v1/server_info
):luarocks --version
):