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): check empty route field #9451

Merged
merged 12 commits into from
Sep 26, 2022
12 changes: 9 additions & 3 deletions kong/router/atc_compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local server_name = require("ngx.ssl").server_name
local tb_new = require("table.new")
local tb_clear = require("table.clear")
local tb_nkeys = require("table.nkeys")
local isempty = require("table.isempty")
chronolaw marked this conversation as resolved.
Show resolved Hide resolved
local yield = require("kong.tools.utils").yield


Expand Down Expand Up @@ -68,6 +69,11 @@ local function is_regex_magic(path)
end


local function is_empty_field(f)
return f == nil or f == null or isempty(f)
end


-- resort `paths` to move regex routes to the front of the array
local function paths_resort(paths)
if not paths then
Expand Down Expand Up @@ -139,7 +145,7 @@ end


local function gen_for_field(name, op, vals, val_transform)
if not vals or vals == null then
if is_empty_field(vals) then
return nil
end

Expand Down Expand Up @@ -186,7 +192,7 @@ local function get_atc(route)
tb_insert(out, gen)
end

if route.hosts and route.hosts ~= null then
if not is_empty_field(route.hosts) then
tb_clear(atc_hosts_t)
local hosts = atc_hosts_t

Expand Down Expand Up @@ -241,7 +247,7 @@ local function get_atc(route)
tb_insert(out, gen)
end

if route.headers and route.headers ~= null then
if not is_empty_field(route.headers) then
tb_clear(atc_headers_t)
local headers = atc_headers_t

Expand Down
78 changes: 78 additions & 0 deletions spec/01-unit/08-router_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,84 @@ for _, flavor in ipairs({ "traditional", "traditional_compatible", "expressions"
end)
end

describe("check empty route fields", function()

it("empty methods", function()
local use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
methods = {},
paths = { "/foo", },
updated_at = 100,
},
},
}
assert(new_router(use_case))
dndx marked this conversation as resolved.
Show resolved Hide resolved
end)

it("empty hosts", function()
local use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
hosts = {},
paths = { "/foo", },
updated_at = 100,
},
},
}
assert(new_router(use_case))
end)

it("empty headers", function()
local use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
headers = {},
paths = { "/foo", },
updated_at = 100,
},
},
}
assert(new_router(use_case))
end)

it("empty paths", function()
local use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
methods = { "GET" },
paths = {},
updated_at = 100,
},
},
}
assert(new_router(use_case))
end)

it("empty snis", function()
local use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
snis = {},
paths = { "/foo", },
updated_at = 100,
},
},
}
assert(new_router(use_case))
end)
end)

describe("normalization stopgap measurements", function()
local use_case, router

Expand Down