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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@
- Fix issue in `header_filter` instrumentation where the span was not
correctly created.
[#9434](https://github.com/Kong/kong/pull/9434)
- Fix issue in router building where some field is an empty table.
[#9451](https://github.com/Kong/kong/pull/9451)
chronolaw marked this conversation as resolved.
Show resolved Hide resolved

## [3.0.0]

Expand Down
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")
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
42 changes: 42 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,48 @@ for _, flavor in ipairs({ "traditional", "traditional_compatible", "expressions"
end)
end

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

before_each(function()
use_case = {
{
service = service,
route = {
id = "e8fb37f1-102d-461e-9c51-6608a6bb8101",
methods = { "GET" },
paths = { "/foo", },
},
},
}
end)

it("empty methods", function()
use_case[1].route.methods = {}
assert(new_router(use_case))
dndx marked this conversation as resolved.
Show resolved Hide resolved
end)

it("empty hosts", function()
use_case[1].route.hosts = {}
assert(new_router(use_case))
end)

it("empty headers", function()
use_case[1].route.headers = {}
assert(new_router(use_case))
end)

it("empty paths", function()
use_case[1].route.paths = {}
assert(new_router(use_case))
end)

it("empty snis", function()
use_case[1].route.snis = {}
assert(new_router(use_case))
end)
end)

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

Expand Down