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(cors): treat empty Origin header same as missing header #14368

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dingjiayi
Copy link
Contributor

@dingjiayi dingjiayi commented Mar 18, 2025

Summary

This change ensures that an empty Origin header (Origin: "") is treated
the same way as a missing Origin header, fixing inconsistent behavior
between these two cases.

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
  • There is a user-facing docs PR against https://github.com/Kong/docs.konghq.com - PUT DOCS PR HERE

Issue reference

Fix #14352

@dingjiayi dingjiayi closed this Mar 18, 2025
@dingjiayi dingjiayi reopened this Mar 18, 2025
@dingjiayi dingjiayi marked this pull request as draft March 18, 2025 08:34
@dingjiayi dingjiayi marked this pull request as ready for review March 18, 2025 12:16
@team-eng-enablement team-eng-enablement added the author/community PRs from the open-source community (not Kong Inc) label Mar 18, 2025
Copy link
Contributor

@StarlightIbuki StarlightIbuki left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Not a blocker but please take a look

@@ -89,7 +89,7 @@ local function configure_origin(conf, header_filter, req_origin)
end
end

if req_origin then
if req_origin and req_origin ~= "" then
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this way it's simpler?

Suggested change
if req_origin and req_origin ~= "" then
if req_origin == "" then
req_origin = nil
end
if req_origin then

Copy link
Contributor Author

@dingjiayi dingjiayi Apr 9, 2025

Choose a reason for hiding this comment

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

Hi @StarlightIbuki

Thanks a lot for reviewing this PR and for your suggestion!

I agree, your proposed change does make the conditional logic simpler and the code cleaner. That's a good direction.

One minor point I noticed is that this approach would convert an empty string req_origin ("") into nil. While this doesn't have a functional impact on the current plugin's behavior, it does lead to a subtle loss of information by removing the distinction between an explicitly empty origin and a non-existent one (nil).

Inspired by your comment aiming for simplification, what do you think about encapsulating this check into a small helper function instead? This way, we can keep the main logic clean while preserving the original values.

Something like this:

local function is_valid_origin(origin)
  return origin and origin ~= ""
end

We could then use if is_valid_origin(req_origin) then ... in the configure_origin function.

Let me know your thoughts! Thanks again for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author/community PRs from the open-source community (not Kong Inc) size/M
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Plugin(cors):Assertion failure on empty origin header
3 participants