api: support regex CORS origins in SecurityPolicy #9966
<span aria-hidden="true">✅</span> Deploy Preview for cerulean-figolla-1f9435 ready!
| Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | ed68383ca6d6d8b44e5d5a1b4714fdde26ff6847 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/6aa8a2f505244500084143e2 |
|<span aria-hidden="true">😎</span> Deploy Preview | https://deploy-preview-9966--cerulean-figolla-1f9435.netlify.app |
|<span aria-hidden="true">📱</span> Preview on mobile | <details><summary> Toggle QR Code... </summary><br /><br />!QR Code<br /><br />_Use your smartphone camera to open QR code link._</details> |
---
<!-- cerulean-figolla-1f9435 Preview -->
_To edit notification comments on pull requests, go to your Netlify project configuration._
<!-- codex-pull-request-review-summary -->
Codex Review Summary
This comment shows the latest Codex review activity on this pull request.
| Review | Status | Commit | Review trigger |
| --- | --- | --- | --- |
| 📝 Code Review | ✅ Completed <relative-time datetime="2026-09-09T14:17:32.033338Z">2026-09-09T14:17:32.033338Z</relative-time> | 55319c4 | Draft marked ready |
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
</details>
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cf13812269
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
</details>
*<sub><sub>!P2 Badge</sub></sub> Preserve wildcard semantics for exact origins*
When allowOriginMatches contains an Exact entry with value "", this branch emits an Envoy exact string matcher, which only matches a literal Origin: rather than allowing every origin. This contradicts the new API and task documentation, which explicitly promise that "" allows any origin; translate this special value to the same wildcard matcher used by allowOrigins.
Useful? React with 👍 / 👎.
Not the case. Envoy's CORS filter tries match("") on every matcher first (https://github.com/envoyproxy/envoy/blob/3afc515c9e0319d33304cddde7f95f50c3e4d83d/source/extensions/filters/http/cors/cors_filter.cc#L210), so an exact already allows any origin.
how is the valid RE2 regular expression works in this case? I think this might cause issue to allow-all origin matcher (interpreted by Envoy CORS filter to match *) , May be add explicit e2e tests for this ? and also how can users avoid this ?
https://github.com/envoyproxy/envoy/blob/v1.39.1/source/extensions/filters/http/cors/cors_filter.cc#L207-L215
allowOriginMatches:
- type: RegularExpression
value: '[^/]+'
I would recommend adding following (suggestions though, but highly recommend getting some maintainers opinions on this) @envoyproxy/gateway-maintainers thoughts on this please ?
Allow-all should be expressed explicitly using Exact match:
allowOriginMatches:
- type: Exact
value: "*"
Regex based match should reject if evaluates to allow-all and only be used for constraint based dynamic origins.
for example:
if err := regex.Validate(match.Value); err != nil {
return nil, err
}
if fullMatch(match.Value, "*") {
return nil, fmt.Errorf(
"CORS origin regular expression must not match %q; use an Exact matcher with value %q to allow all origins",
"*", "*",
)
}
Thanks for catching this. I've added the check 🙏
Since allowOrigins already covers exact origins and , the Exact type in allowOriginMatches seems to add a second spelling for the same thing. is not an exact match, so labelling it Exact may be confusing.
Thinking out loud here, but one option would be to drop Exact and make the new field regex-only, e.g. allowOriginRegexes with a CORSOriginRegex type, and keep allow-all in allowOrigins. The *-rejection check would still apply.
@codex review
<sub><sub>!P2 Badge</sub></sub> Accept the complete documented RE2 syntax
When an origin pattern uses RE2's supported \C byte escape (for example, https://\C+\.example\.com), regexp.Compile rejects it because Go documents its dialect as RE2 syntax except for \C. The API explicitly promises RE2 syntax and Envoy's matcher uses RE2, so this causes a valid documented pattern to mark the SecurityPolicy invalid and replace affected routes with 500 responses; either validate with an Envoy-compatible RE2 parser or explicitly narrow the API contract.
Useful? React with 👍 / 👎.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1dbfdc0685
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
</details>
Codecov Report
:x: Patch coverage is
94.11765% with 1 line in your changes missing coverage. Please review.:white_check_mark: Project coverage is 81.32%. Comparing base (
27abd5a) to head (ed68383).:warning: Report is 2 commits behind head on main.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| internal/gatewayapi/securitypolicy.go | 94.11% | 1 Missing :warning: |
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## main #9966 +/- ##
==========================================
- Coverage 81.34% 81.32% -0.02%
==========================================
Files 264 264
Lines 40973 40987 +14
==========================================
+ Hits 33328 33333 +5
- Misses 7644 7653 +9
Partials 1 1
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Updated the API docs to specify Go's regexp syntax, without \C.
@zhaohuabing Makes sense, done. Dropped Exact, renamed the field to allowOriginRegexes (type CORSOriginRegex), and left allow-all to allowOrigins. The * rejection check is still there.
FWIW I had allowOriginRegexes in mind at first too, but went with the type: Exact | RegularExpression shape since that's what StringMatch fields like removeOnMatch use, and wasn't sure which way to go. Glad to have a call on it.
PTAL when you get a chance.
@codex review
Codex Review: Didn't find any major issues. :tada:
Reviewed commit: 55319c4ba7
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
</details>
LGTM. Thanks!
Thanks for your review 🙏
Approved these changes.
LGTM, thanks!
/retest
Thanks for your review 😆
zhaohuabing
kahirokunn
Fixes #9965
What this PR does / why we need it:
Add
cors.allowOriginRegexestoSecurityPolicyfor origins such ashttps://preview-123.example.com, where allowing every subdomain with a wildcard is too broad.The new field is a list of
CORSOriginRegexstrings. UseallowOriginsfor exact origins, hostname wildcards, and allow-all. An origin is allowed if it matches either list.Regexes use RE2 syntax, the same contract as the other regular expression fields in the API, and are validated with the existing
regex.Validatehelper. They match the full Origin header, including the scheme and port when present. Invalid patterns follow the existing SecurityPolicy error handling:Accepted=Falseand a 500 response on affected routes. Regular expressions that match the literal string""are rejected because Envoy's CORS filter treats any matcher that matches""as allow-all; useallowOrigins: ["*"]to allow any origin explicitly.Which issue(s) this PR fixes:
Fixes #9965
---
PR Checklist
allowOriginRegexesand theCORSOriginRegextype follow @zhaohuabing's suggestion in this PR; awaiting maintainer sign-off.9965-cors-origin-regexes.md.make gen-checkpasses with a clean working tree.allowOriginsbehavior is preserved.