Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router): host matches should be case insensitive as per RFC 3986 #14198

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: "Fixed an issue where the Host header was case-sensitive on route for matching, which should be case-insensitive as per RFC 3986."
type: breaking_change
scope: Core
2 changes: 2 additions & 0 deletions kong/router/atc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local pairs = pairs
local ipairs = ipairs
local next = next
local max = math.max
local lower = string.lower


local ngx = ngx
Expand Down Expand Up @@ -445,6 +446,7 @@ function _M:exec(ctx)

local req_uri = ctx and ctx.request_uri or var.request_uri
local req_host = get_header("host", ctx)
req_host = req_host and lower(req_host) or nil

req_uri = strip_uri_args(req_uri)

Expand Down
8 changes: 5 additions & 3 deletions kong/router/traditional.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ local function marshall_route(r)
else
-- plain host matching
has_host_plain = true
append(hosts_t, { value = host })
hosts_t[host] = host
append(hosts_t, { value = lower(host) })
hosts_t[lower(host)] = host
end
end

Expand Down Expand Up @@ -1648,7 +1648,7 @@ function _M.new(routes, cache, cache_neg)
elseif match_wildcard_hosts then
for i = 1, wildcard_hosts[0] do
local host = wildcard_hosts[i]
local from, _, err = re_find(host_with_port, host.regex, "ajo")
local from, _, err = re_find(host_with_port, host.regex, "ajoi")
if err then
log(ERR, "could not match wildcard host: ", err)
return
Expand Down Expand Up @@ -1759,6 +1759,8 @@ function _M.new(routes, cache, cache_neg)
local req_scheme = ctx and ctx.scheme or var.scheme
local sni = server_name()

req_host = req_host and lower(req_host) or nil

local headers
if match_headers then
local err
Expand Down
3 changes: 2 additions & 1 deletion kong/router/transform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local ipairs = ipairs
local tb_insert = table.insert
local fmt = string.format
local byte = string.byte
local lower = string.lower
local bor, band, lshift, rshift = bit.bor, bit.band, bit.lshift, bit.rshift


Expand Down Expand Up @@ -401,7 +402,7 @@ local function get_expression(route)
host = host:sub(1, -2)
end

local exp = "http.host ".. op .. [[ r#"]] .. host .. [["#]]
local exp = "http.host ".. op .. [[ r#"]] .. lower(host) .. [["#]]
if port then
exp = "(" .. exp .. LOGICAL_AND ..
"net.dst.port ".. OP_EQUAL .. " " .. port .. ")"
Expand Down
16 changes: 15 additions & 1 deletion spec/01-unit/08-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ for _, flavor in ipairs({ "traditional", "traditional_compatible", "expressions"
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
hosts = {
"domain-1.org",
"domain-2.org"
"domain-2.org",
"Domain-Capitalized.org"
},
},
},
Expand Down Expand Up @@ -385,6 +386,19 @@ for _, flavor in ipairs({ "traditional", "traditional_compatible", "expressions"
assert.same(nil, match_t.matches.uri_captures)
end)

it("[host] matches should be case insensitive", function()
-- host
local match_t = router:select("GET", "/", "domain-capitalized.org")
assert.truthy(match_t)
assert.same(use_case[1].route, match_t.route)
if flavor == "traditional" then
assert.same(use_case[1].route.hosts[3], match_t.matches.host)
end
assert.same(nil, match_t.matches.method)
assert.same(nil, match_t.matches.uri)
assert.same(nil, match_t.matches.uri_captures)
end)

it("[host] ignores default port", function()
-- host
local match_t = router:select("GET", "/", "domain-1.org:80")
Expand Down
Loading