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(core): validation against header regex match #13290

Closed
wants to merge 1 commit into from

Conversation

StarlightIbuki
Copy link
Contributor

@StarlightIbuki StarlightIbuki commented Jun 24, 2024

Summary

The regex pattern for header should starts with "~*" instead of "~"

Checklist

Issue reference

fixup #12365
fix KAG-4788

@StarlightIbuki StarlightIbuki force-pushed the fix/regex-header branch 2 times, most recently from ef8b4c2 to f5accc1 Compare June 24, 2024 08:58
Copy link
Contributor

@ADD-SP ADD-SP left a comment

Choose a reason for hiding this comment

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

The header.value will be treated as a regex if and only if there is only one value, but the current impl doesn't fit it.

#6079

kong/db/schema/typedefs.lua Outdated Show resolved Hide resolved
kong/db/schema/typedefs.lua Outdated Show resolved Hide resolved
@pull-request-size pull-request-size bot added size/M and removed size/S labels Jun 25, 2024
@StarlightIbuki StarlightIbuki requested a review from ADD-SP June 25, 2024 09:08
@StarlightIbuki StarlightIbuki force-pushed the fix/regex-header branch 2 times, most recently from e62b197 to b00ac60 Compare July 1, 2024 09:35
@ADD-SP ADD-SP requested a review from chronolaw July 1, 2024 09:40
kong/router/utils.lua Outdated Show resolved Hide resolved
kong/db/schema/typedefs.lua Show resolved Hide resolved
kong/db/schema/typedefs.lua Show resolved Hide resolved
-- the value will be interpreted as a regex by the router; but is it a
-- valid one? Let's dry-run it with the same options as our router.
local _, _, err = ngx.re.find("", regex, "aj")
local _, _, err = ngx.re.find("", pattern, "a")
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment for removing the option j.

if not is_regex_pattern(pattern) then
local function validate_header_regex_or_plain_pattern(patterns)
-- when there's only 1 pattern and it starts with "~*", it's a regex pattern
if #patterns ~= 1 or patterns[1]:sub(1, 2) ~= "~*" then
Copy link
Contributor

Choose a reason for hiding this comment

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

The NOT condition is hard to understand, could we rewrite it like

if #patterns == 1 and patterns[1]:sub(1, 2) == "~*" then
  return is_valid_regex_pattern(patterns[1]:sub(3))
end

return true

2 condtions:
1. only 1 header value presents
2. it starts with "~*"

Fixup #12365
Fix KAG-4788

Co-authored-by: Qi <add_sp@outlook.com>
@StarlightIbuki StarlightIbuki marked this pull request as draft July 2, 2024 07:52
@chronolaw
Copy link
Contributor

should we close it?

@StarlightIbuki
Copy link
Contributor Author

Let's close this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants