Skip to content

Commit

Permalink
fix(pdk) ignore user set Tranfer-Encoding (#8698)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlightIbuki authored Apr 21, 2022
1 parent 31ca6ea commit fb8aa2d
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@

## Unreleased

### Fixes

#### PDK

- `pdk.response.set_header()`, `pdk.response.set_headers()`, `pdk.response.exit()` now ignore and emit warnings for manually set `Transfer-Encoding` headers.
[#8698](https://github.com/Kong/kong/pull/8698)

### Breaking Changes

#### Admin API
Expand Down
22 changes: 20 additions & 2 deletions kong/pdk/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ local function new(self, major_version)
--
-- Be aware that changing this setting might break any plugins that
-- rely on the automatic underscore conversion.
-- You cannot set Transfer-Encoding header with this function. It will be ignored.
--
-- @function kong.response.set_header
-- @phases rewrite, access, header_filter, response, admin_api
Expand All @@ -408,6 +409,11 @@ local function new(self, major_version)
end

validate_header(name, 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.")
return
end

ngx.header[name] = normalize_header(value)
end
Expand Down Expand Up @@ -487,6 +493,8 @@ local function new(self, major_version)
-- This function overrides any existing header bearing the same name as those
-- specified in the `headers` argument. Other headers remain unchanged.
--
-- You cannot set Transfer-Encoding header with this function. It will be ignored.
--
-- @function kong.response.set_headers
-- @phases rewrite, access, header_filter, response, admin_api
-- @tparam table headers
Expand Down Expand Up @@ -514,7 +522,12 @@ local function new(self, major_version)
validate_headers(headers)

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.")
else
ngx.header[name] = normalize_multi_header(value)
end
end
end

Expand Down Expand Up @@ -645,8 +658,13 @@ local function new(self, major_version)
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.")
else
ngx.header[name] = normalize_multi_header(value)
end
if not has_content_type or not has_content_length then
local lower_name = lower(name)
if lower_name == "content-type"
or lower_name == "content_type"
then
Expand Down
29 changes: 29 additions & 0 deletions t/01-pdk/08-response/05-set_header.t
Original file line number Diff line number Diff line change
Expand Up @@ -248,3 +248,32 @@ type: string
X-Foo: {}
--- no_error_log
[error]
=== TEST 8: response.set_header() does not set transfer-encoding
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
header_filter_by_lua_block {
ngx.header.content_length = nil
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.set_header("Transfer-Encoding", "gzip")
ngx.status = 200
}
body_filter_by_lua_block {
local new_headers = ngx.resp.get_headers()
ngx.arg[1] = "Transfer-Encoding: " .. new_headers["Transfer-Encoding"]
ngx.arg[2] = true
}
}
--- request
GET /t
--- response_body chop
Transfer-Encoding: chunked
--- error_log
manually setting Transfer-Encoding. Ignored.
36 changes: 35 additions & 1 deletion t/01-pdk/08-response/08-set_headers.t
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ X-Foo: {zzz}
local PDK = require "kong.pdk"
local pdk = PDK.new()
local ok, err pdk.response.set_headers({})
local ok, err = pdk.response.set_headers({})
if not ok then
ngx.ctx.err = err
end
Expand Down Expand Up @@ -698,3 +698,37 @@ Content-Type: text/plain
ok
--- no_error_log
[error]
=== TEST 18: response.set_header() does not set transfer-encoding
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
header_filter_by_lua_block {
ngx.header.content_length = nil
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.set_headers {
["Transfer-Encoding"] = "gzip",
["X-test"] = "test",
}
ngx.status = 200
}
body_filter_by_lua_block {
local new_headers = ngx.resp.get_headers()
ngx.arg[1] = "Transfer-Encoding: " .. new_headers["Transfer-Encoding"] .. "\n"
.. "X-test: " .. new_headers["X-test"]
ngx.arg[2] = true
}
}
--- request
GET /t
--- response_body chop
Transfer-Encoding: chunked
X-test: test
--- error_log
manually setting Transfer-Encoding. Ignored.
31 changes: 30 additions & 1 deletion t/01-pdk/08-response/11-exit.t
Original file line number Diff line number Diff line change
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) + 10;
plan tests => repeat_each() * (blocks() * 4) + 11;

run_tests();

Expand Down Expand Up @@ -1125,3 +1125,32 @@ unable to proxy stream connection, status: 400, err: error message
[error]
--- error_log
finalize stream session: 200
=== TEST 18: response.exit() does not set transfer-encoding from headers
--- http_config eval: $t::Util::HttpConfig
--- config
location = /t {
access_by_lua_block {
ngx.header.content_length = nil
local PDK = require "kong.pdk"
local pdk = PDK.new()
pdk.response.exit(200, "test\n", {
["Transfer-Encoding"] = "gzip",
["X-test"] = "test",
})
}
}
--- request
GET /t
--- response_body
test
--- response_headers
Content-Length: 5
X-test: test
--- error_log
manually setting Transfer-Encoding. Ignored.

0 comments on commit fb8aa2d

Please sign in to comment.