Skip to content

Commit fc9eed9

Browse files
committed
fix(core): Via header is not RFC7230 compliant
Signed-off-by: Zexuan Luo <[email protected]>
1 parent 51b6eb7 commit fc9eed9

File tree

8 files changed

+22
-14
lines changed

8 files changed

+22
-14
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
message: >
2+
Fixed an issue where Via header was not RFC7230 compliant. Previously, the Via header was likely `<protocol> kong/<version>`. Now, it is set to `<protocol> kong (kong/<version>)`.
3+
type: bugfix
4+
scope: Core

kong/plugins/aws-lambda/handler.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ function AWSLambdaHandler:access(conf)
234234
headers = kong.table.merge(headers) -- create a copy of headers
235235

236236
if kong.configuration.enabled_headers[VIA_HEADER] then
237-
local outbound_via = (ngx_var.http2 and "2 " or "1.1 ") .. server_tokens
237+
local outbound_via = (ngx_var.http2 and "2 " or "1.1 ") .. fmt("kong (%s)", server_tokens)
238238
headers[VIA_HEADER] = headers[VIA_HEADER] and headers[VIA_HEADER] .. ", " .. outbound_via
239239
or outbound_via
240240
end

kong/plugins/azure-functions/handler.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function azure:access(conf)
7979
end
8080

8181
if kong.configuration.enabled_headers[VIA_HEADER] then
82-
local outbound_via = (var.http2 and "2 " or "1.1 ") .. server_tokens
82+
local outbound_via = (var.http2 and "2 " or "1.1 ") .. fmt("kong (%s)", server_tokens)
8383
response_headers[VIA_HEADER] = response_headers[VIA_HEADER] and response_headers[VIA_HEADER] .. ", " .. outbound_via
8484
or outbound_via
8585
end

kong/runloop/handler.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,8 +1382,9 @@ return {
13821382

13831383
do
13841384
local req_via = get_header(constants.HEADERS.VIA, ctx)
1385-
local kong_inbound_via = protocol_version and protocol_version .. " " .. SERVER_HEADER
1386-
or SERVER_HEADER
1385+
local received_by_and_comment = fmt("kong (%s)", SERVER_HEADER)
1386+
local kong_inbound_via = protocol_version and protocol_version .. " " .. received_by_and_comment
1387+
or received_by_and_comment
13871388
var.upstream_via = req_via and req_via .. ", " .. kong_inbound_via
13881389
or kong_inbound_via
13891390
end
@@ -1593,7 +1594,7 @@ return {
15931594
DEFAULT_PROXY_HTTP_VERSION
15941595
end
15951596

1596-
local kong_outbound_via = proxy_http_version .. " " .. SERVER_HEADER
1597+
local kong_outbound_via = proxy_http_version .. " " .. fmt("kong (%s)", SERVER_HEADER)
15971598
local resp_via = var["upstream_http_" .. headers.VIA]
15981599
header[headers.VIA] = resp_via and resp_via .. ", " .. kong_outbound_via
15991600
or kong_outbound_via

spec/02-integration/05-proxy/14-server_tokens_spec.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ local uuid = require("kong.tools.uuid").uuid
66

77

88
local default_server_header = meta._SERVER_TOKENS
9-
local default_via_value = "1.1 " .. default_server_header
9+
local default_via_value = "1.1 " .. string.format("kong (%s)", default_server_header)
1010

1111
for _, strategy in helpers.each_strategy() do
1212
describe("headers [#" .. strategy .. "]", function()

spec/02-integration/05-proxy/35-via_spec.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local re_match = ngx.re.match
88
local str_fmt = string.format
99

1010
local SERVER_TOKENS = meta._SERVER_TOKENS
11+
local RECEIVED_BY_AND_COMMENT = str_fmt("kong (%s)", meta._SERVER_TOKENS)
1112

1213
for _, strategy in helpers.all_strategies() do
1314
describe("append Kong Gateway info to the 'Via' header [#" .. strategy .. "]", function()
@@ -146,8 +147,8 @@ for _, strategy in helpers.all_strategies() do
146147

147148
local body = assert.res_status(200, res)
148149
local json_body = cjson.decode(body)
149-
assert.are_same({ via = "1.1 dev, 1.1 " .. SERVER_TOKENS }, json_body)
150-
assert.are_same("2 nginx, HTTP/1.1 http_mock, 1.1 " .. SERVER_TOKENS, res.headers["Via"])
150+
assert.are_same({ via = "1.1 dev, 1.1 " .. RECEIVED_BY_AND_COMMENT }, json_body)
151+
assert.are_same("2 nginx, HTTP/1.1 http_mock, 1.1 " .. RECEIVED_BY_AND_COMMENT, res.headers["Via"])
151152
assert.are_same("http-mock", res.headers["Server"])
152153

153154
if proxy_client then
@@ -170,8 +171,8 @@ for _, strategy in helpers.all_strategies() do
170171

171172
assert.are_equal(200, tonumber(headers:get(":status")))
172173
local json_body = cjson.decode(body)
173-
assert.are_same({ via = "1.1 dev, 2 " .. SERVER_TOKENS }, json_body)
174-
assert.are_same("2 nginx, HTTP/1.1 http_mock, 1.1 " .. SERVER_TOKENS, headers:get("Via"))
174+
assert.are_same({ via = "1.1 dev, 2 " .. RECEIVED_BY_AND_COMMENT }, json_body)
175+
assert.are_same("2 nginx, HTTP/1.1 http_mock, 1.1 " .. RECEIVED_BY_AND_COMMENT, headers:get("Via"))
175176
assert.are_same("http-mock", headers:get("Server"))
176177
end)
177178

@@ -193,7 +194,7 @@ for _, strategy in helpers.all_strategies() do
193194
local server = re_match(resp, [=[Response headers received\:[\s\S]*\nserver\:\s(.*?)\n]=], "jo")
194195
assert.are_equal(SERVER_TOKENS, server[1])
195196
local via = re_match(resp, [=[Response headers received\:[\s\S]*\nvia\:\s(.*?)\n]=], "jo")
196-
assert.are_equal("2 " .. SERVER_TOKENS, via[1])
197+
assert.are_equal("2 " .. RECEIVED_BY_AND_COMMENT, via[1])
197198
local body = re_match(resp, [=[Response contents\:([\s\S]+?)\nResponse trailers received]=], "jo")
198199
local json_body = cjson.decode(body[1])
199200
assert.are_equal("hello world!", json_body.reply)
@@ -217,7 +218,7 @@ for _, strategy in helpers.all_strategies() do
217218
local server = re_match(resp, [=[Response headers received\:[\s\S]*\nserver\:\s(.*?)\n]=], "jo")
218219
assert.are_equal(SERVER_TOKENS, server[1])
219220
local via = re_match(resp, [=[Response headers received\:[\s\S]*\nvia\:\s(.*?)\n]=], "jo")
220-
assert.are_equal("2 " .. SERVER_TOKENS, via[1])
221+
assert.are_equal("2 " .. RECEIVED_BY_AND_COMMENT, via[1])
221222
local body = re_match(resp, [=[Response contents\:([\s\S]+?)\nResponse trailers received]=], "jo")
222223
local json_body = cjson.decode(body[1])
223224
assert.are_equal("hello world!", json_body.reply)

spec/03-plugins/27-aws-lambda/99-access_spec.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local http_mock = require "spec.helpers.http_mock"
77

88
local TEST_CONF = helpers.test_conf
99
local server_tokens = meta._SERVER_TOKENS
10+
local via_header_suffix = string.format("kong (%s)", server_tokens)
1011
local null = ngx.null
1112
local fmt = string.format
1213

@@ -971,7 +972,7 @@ for _, strategy in helpers.each_strategy() do
971972
})
972973

973974
if server_tokens then
974-
assert.equal("2 " .. server_tokens, res.headers["Via"])
975+
assert.equal("2 " .. via_header_suffix, res.headers["Via"])
975976
end
976977
end)
977978

spec/03-plugins/35-azure-functions/01-access_spec.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local meta = require "kong.meta"
44
local http_mock = require "spec.helpers.http_mock"
55

66
local server_tokens = meta._SERVER_TOKENS
7+
local via_header_suffix = string.format("kong (%s)", server_tokens)
78

89

910
for _, strategy in helpers.each_strategy() do
@@ -259,7 +260,7 @@ for _, strategy in helpers.each_strategy() do
259260
}
260261
})
261262

262-
assert.equal("2 " .. server_tokens, res.headers["Via"])
263+
assert.equal("2 " .. via_header_suffix, res.headers["Via"])
263264
end)
264265

265266
it("returns Content-Length header", function()

0 commit comments

Comments
 (0)