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

  • The Pull Request has tests
  • A changelog file has been created under changelog/unreleased/kong or skip-changelog label added on PR if changelog is unnecessary. README.md
  • [N/A] There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

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
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>
@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
4 participants