Skip to content

Commit

Permalink
fix(error_handler): fix the bug that error handler can't recognize st…
Browse files Browse the repository at this point in the history
…atus code 494. (#12114)

* fix(error_handler): fix the bug that error handler can't recognize
status code 494.

There is a dedicated response body for 494 defined in error_handler.
However, based on the current configuration for `error_page` in
nginx-kong.conf, 494 will not be treated correctly wihout reserving it
by the `=response` option in `error_page` directive.
In this PR, a `error_page` configuration is added for 494 separately, so
that it can be recognized in error handler, and it will be replaced
with 400 finally.

FTI-5374
  • Loading branch information
liverpool8056 authored Nov 30, 2023
1 parent 3b09d87 commit cfb56a7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/error_handler_494.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions kong/error_handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
3 changes: 2 additions & 1 deletion kong/templates/nginx_kong.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/02-integration/05-proxy/13-error_handlers_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down

0 comments on commit cfb56a7

Please sign in to comment.