Skip to content

Commit

Permalink
fix(pdk): response send function should ignore transfer-encoding corr…
Browse files Browse the repository at this point in the history
…ectly (#11936)

This PR is a follow-up fix for #8698 to ignore the transfer encoding header set by the user.

The line removed in this PR seems to be conflict with the original fix and makes the original fix meaningless, so removed this line to get the expected behavior.

We have related bug reports that when using the AWS-Lambda plugin in proxy_integration mode if the lamdba function returns an arbitrary transfer-encoding header, the response sent by Kong will both contain content-length and transfer-encoding, which is an unexpected result.

Fix FTI-5028
  • Loading branch information
windmgc committed Nov 8, 2023
1 parent 37bd9c2 commit 444b214
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
@@ -0,0 +1,3 @@
message: Fix an issue that when using kong.response.exit, the Transfer-Encoding header set by user is not removed
type: bugfix
scope: PDK
1 change: 0 additions & 1 deletion kong/pdk/response.lua
Expand Up @@ -660,7 +660,6 @@ local function new(self, major_version)
local has_content_length
if headers ~= nil then
for name, value in pairs(headers) do
ngx.header[name] = normalize_multi_header(value)
local lower_name = lower(name)
if lower_name == "transfer-encoding" or lower_name == "transfer_encoding" then
self.log.warn("manually setting Transfer-Encoding. Ignored.")
Expand Down
37 changes: 37 additions & 0 deletions spec/03-plugins/27-aws-lambda/99-access_spec.lua
Expand Up @@ -150,6 +150,12 @@ for _, strategy in helpers.each_strategy() do
service = null,
}

local route24 = bp.routes:insert {
hosts = { "lambda24.com" },
protocols = { "http", "https" },
service = null,
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route1.id },
Expand Down Expand Up @@ -463,6 +469,19 @@ for _, strategy in helpers.each_strategy() do
}
}

bp.plugins:insert {
name = "aws-lambda",
route = { id = route24.id },
config = {
port = 10001,
aws_key = "mock-key",
aws_secret = "mock-secret",
aws_region = "us-east-1",
function_name = "functionWithTransferEncodingHeader",
is_proxy_integration = true,
}
}

fixtures.dns_mock:A({
name = "custom.lambda.endpoint",
address = "127.0.0.1",
Expand Down Expand Up @@ -1148,6 +1167,24 @@ for _, strategy in helpers.each_strategy() do
assert.equals("https", req.vars.scheme)
end)

it("#test2 works normally by removing transfer encoding header when proxy integration mode", function ()
proxy_client:set_timeout(3000)
assert.eventually(function ()
local res = assert(proxy_client:send({
method = "GET",
path = "/get",
headers = {
["Host"] = "lambda24.com"
}
}))

assert.res_status(200, res)
assert.is_nil(res.headers["Transfer-Encoding"])
assert.is_nil(res.headers["transfer-encoding"])

return true
end).with_timeout(3).is_truthy()
end)
end)

describe("AWS_REGION environment is set", function()
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/aws-lambda.lua
Expand Up @@ -57,6 +57,9 @@ local fixtures = {
elseif string.match(ngx.var.uri, "functionEcho") then
require("spec.fixtures.mock_upstream").send_default_json_response()
elseif string.match(ngx.var.uri, "functionWithTransferEncodingHeader") then
ngx.say("{\"statusCode\": 200, \"headers\": { \"Transfer-Encoding\": \"chunked\", \"transfer-encoding\": \"chunked\"}}")
elseif type(res) == 'string' then
ngx.header["Content-Length"] = #res + 1
ngx.say(res)
Expand Down
5 changes: 3 additions & 2 deletions t/01-pdk/08-response/11-exit.t
Expand Up @@ -4,7 +4,7 @@ use Test::Nginx::Socket::Lua;
use Test::Nginx::Socket::Lua::Stream;
do "./t/Util.pm";

plan tests => repeat_each() * (blocks() * 4) + 11;
plan tests => repeat_each() * (blocks() * 4) + 12;

run_tests();

Expand Down Expand Up @@ -1128,7 +1128,7 @@ finalize stream session: 200
=== TEST 18: response.exit() does not set transfer-encoding from headers
=== TEST 44: response.exit() does not set transfer-encoding from headers
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
Expand All @@ -1148,6 +1148,7 @@ GET /t
--- response_body
test
--- response_headers
! Transfer-Encoding
Content-Length: 5
X-test: test
--- error_log
Expand Down

1 comment on commit 444b214

@khcp-gha-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:444b214c8b9bb1fe954a5516410ef630083c6c69
Artifacts available https://github.com/Kong/kong/actions/runs/6796212066

Please sign in to comment.