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) wildcard host matching not considered with plain host match #4152

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 13 additions & 10 deletions kong/router.lua
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ end
do
local matchers = {
[MATCH_RULES.HOST] = function(route_t, ctx)
local host = route_t.hosts[ctx.hits.host or ctx.req_host]
local host = route_t.hosts[ctx.req_host] or
route_t.hosts[ctx.hits.host]
Copy link
Member

Choose a reason for hiding this comment

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

Here, we are ignoring the matched hint, which is thus rendered useless. In which case we could simply remove it. This change can be detrimental in some scenarios.

Copy link
Member Author

@bungle bungle Jan 9, 2019

Choose a reason for hiding this comment

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

What do you mean? If you remove route_t.hosts[ctx.hits.host] the tests will not pass, and if you remove route_t.hosts[ctx.req_host] the tests won't pass either. So it is not ignored (?).

if host then
ctx.matches.host = host

Expand Down Expand Up @@ -705,7 +706,8 @@ end
do
local reducers = {
[MATCH_RULES.HOST] = function(category, ctx)
return category.routes_by_hosts[ctx.hits.host]
return category.routes_by_hosts[ctx.req_host] or
category.routes_by_hosts[ctx.hits.host]
end,

[MATCH_RULES.URI] = function(category, ctx)
Expand Down Expand Up @@ -986,23 +988,24 @@ function _M.new(routes)

-- host match

if plain_indexes.hosts[ctx.req_host] then
req_category = bor(req_category, MATCH_RULES.HOST)
Copy link
Member

Choose a reason for hiding this comment

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

This change can be highly detrimental in many scenarios.


elseif ctx.req_host then
do
local wildcard_match, _, err
for i = 1, #wildcard_hosts do
local from, _, err = re_find(ctx.req_host, wildcard_hosts[i].regex, "ajo")
wildcard_match, _, err = re_find(ctx.req_host, wildcard_hosts[i].regex, "ajo")
if err then
log(ERR, "could not match wildcard host: ", err)
return
end

if from then
hits.host = wildcard_hosts[i].value
req_category = bor(req_category, MATCH_RULES.HOST)
if wildcard_match then
hits.host = wildcard_hosts[i].value
break
end
end

if wildcard_match or plain_indexes.hosts[ctx.req_host] then
req_category = bor(req_category, MATCH_RULES.HOST)
end
end

-- uri match
Expand Down
57 changes: 57 additions & 0 deletions spec/02-integration/05-proxy/02-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,63 @@ for _, strategy in helpers.each_strategy() do
end)
end)

describe("[paths] + [hosts] (plain and wildcard)", function()
local routes

lazy_setup(function()
routes = insert_routes {
{
strip_path = true,
hosts = { "*.route.com" },
paths = { "/path1" },
},
{
strip_path = true,
hosts = { "plain.route.com" },
paths = { "/path2" },
},
}
end)

lazy_teardown(function()
remove_routes(routes)
end)

it("matches wildcard host even when there is plain host", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/path1",
headers = {
["Host"] = "plain.route.com",
["kong-debug"] = 1,
}
})

assert.res_status(200, res)

assert.equal(routes[1].id, res.headers["kong-route-id"])
assert.equal(routes[1].service.id, res.headers["kong-service-id"])
assert.equal(routes[1].service.name, res.headers["kong-service-name"])
end)

it("matches plain host even when there is wildcard host", function()
local res = assert(proxy_client:send {
method = "GET",
path = "/path2",
headers = {
["Host"] = "plain.route.com",
["kong-debug"] = 1,
}
})

assert.res_status(200, res)

assert.equal(routes[2].id, res.headers["kong-route-id"])
assert.equal(routes[2].service.id, res.headers["kong-service-id"])
assert.equal(routes[2].service.name, res.headers["kong-service-name"])
end)
end)

describe("slash handing", function()
local checks = {
-- upstream url paths request path expected path strip uri
Expand Down