diff --git a/changelog/unreleased/kong/error_handler_494.yml b/changelog/unreleased/kong/error_handler_494.yml new file mode 100644 index 00000000000..dabcfd0cdc8 --- /dev/null +++ b/changelog/unreleased/kong/error_handler_494.yml @@ -0,0 +1,3 @@ +message: Fix a bug that the error_handler can not provide the meaningful response body when the internal error code 494 is triggered. +type: bugfix +scope: Core \ No newline at end of file diff --git a/kong/error_handlers.lua b/kong/error_handlers.lua index e4e8e17d002..8fd83cf55aa 100644 --- a/kong/error_handlers.lua +++ b/kong/error_handlers.lua @@ -59,6 +59,13 @@ return function(ctx) local status = kong.response.get_status() local message = get_body(status) + -- Nginx 494 status code is used internally when the client sends + -- too large or invalid HTTP headers. Kong is obliged to convert + -- it back to `400 Bad Request`. + if status == 494 then + status = 400 + end + local headers if find(accept_header, TYPE_GRPC, nil, true) == 1 then message = { message = message } diff --git a/kong/templates/nginx_kong.lua b/kong/templates/nginx_kong.lua index 3375dcf1457..cc2e8c16729 100644 --- a/kong/templates/nginx_kong.lua +++ b/kong/templates/nginx_kong.lua @@ -81,7 +81,8 @@ server { listen $(entry.listener); > end - error_page 400 404 405 408 411 412 413 414 417 494 /kong_error_handler; + error_page 400 404 405 408 411 412 413 414 417 /kong_error_handler; + error_page 494 =494 /kong_error_handler; error_page 500 502 503 504 /kong_error_handler; # Append the kong request id to the error log diff --git a/spec/02-integration/05-proxy/13-error_handlers_spec.lua b/spec/02-integration/05-proxy/13-error_handlers_spec.lua index 3c864e5d653..a755d515bed 100644 --- a/spec/02-integration/05-proxy/13-error_handlers_spec.lua +++ b/spec/02-integration/05-proxy/13-error_handlers_spec.lua @@ -36,7 +36,7 @@ describe("Proxy error handlers", function() assert.res_status(400, res) local body = res:read_body() assert.matches("kong/", res.headers.server, nil, true) - assert.matches("Bad request\nrequest_id: %x+\n", body) + assert.matches("Request header or cookie too large", body) end) it("Request For Routers With Trace Method Not Allowed", function ()