Skip to content

Commit

Permalink
fix(core): Regex header values in traditional router are now validated
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlightIbuki committed Jan 18, 2024
1 parent 0c5fe19 commit f004449
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
message: Regex header values in traditional router are now validated.
type: bugfix
scope: Core
4 changes: 4 additions & 0 deletions kong/db/schema/entities/routes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ else
},
},
},
values = {
type = "array",
elements = typedefs.regex_or_plain_pattern,
}
} },
{ https_redirect_status_code = { type = "integer",
description = "The status code Kong responds with when all properties of a Route match except the protocol",
Expand Down
23 changes: 23 additions & 0 deletions kong/db/schema/typedefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,23 @@ local function validate_path_with_regexes(path)
return true
end

local function validate_regex_or_plain_pattern(pattern)
if pattern:sub(1, 1) ~= "~" then
return true
end

pattern = pattern:sub(2)

local _, _, err = ngx.re.find("", pattern, "aj")
if err then
return nil,
string.format("invalid regex: '%s' (PCRE returned: %s)",
pattern, err)
end

return true
end


typedefs.sources = Schema.define {
type = "set",
Expand Down Expand Up @@ -620,6 +637,12 @@ typedefs.headers = Schema.define {
description = "A map of header names to arrays of header values."
}

typedefs.regex_or_plain_pattern = Schema.define {
type = "string",
custom_validator = validate_regex_or_plain_pattern,
description = "A string representing a regex or plain pattern."
}

typedefs.no_headers = Schema.define(typedefs.headers { eq = null, description = "A null value representing no headers." })

typedefs.semantic_version = Schema.define {
Expand Down
11 changes: 11 additions & 0 deletions spec/01-unit/01-db/01-schema/06-routes_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,17 @@ describe("routes schema (flavor = traditional/traditional_compatible)", function
assert.falsy(ok)
assert.equal("length must be at least 1", err.headers[1])
end)

it("value must be a plain pattern or a valid regex pattern", function()
local route = {
headers = { location = { "~[" } },
protocols = { "http" },
}

local ok, err = Routes:validate(route)
assert.falsy(ok)
assert.match("invalid regex", err.headers[1])
end)
end)

describe("methods attribute", function()
Expand Down

0 comments on commit f004449

Please sign in to comment.