Skip to content

Regex Rewrites - #4695

Merged
kubernetes-prow[bot] merged 2 commits into
kubernetes-sigs:mainfrom
Stevenjin8:gateway-api
Sep 18, 2026
Merged

kubernetes-prow[bot] merged 2 commits into
kubernetes-sigs:mainfrom
Stevenjin8:gateway-api

Conversation

@Stevenjin8

Copy link
Copy Markdown
Contributor

What type of PR is this?
/kind feature

Takeover of #4361

What this PR does / why we need it:

Does this PR introduce a user-facing change?:
No

@k8s-ci-robot

Copy link
Copy Markdown
Contributor

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Mar 19, 2026
@k8s-ci-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Stevenjin8
Once this PR has been reviewed and has the lgtm label, please assign robscott for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/gep PRs related to Gateway Enhancement Proposal(GEP) size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 19, 2026
Comment thread geps/gep-4359/index.md Outdated
`pattern` will be a regular expression.
**ALL** instances of `pattern` in the url path MUST be replaced with `substitution`.
The url path includes the leading slash.
If the resulting path is invalid (does not have a leading slash, etc), the proxy MUST return a 500 Internal Server Error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No proxy does this right now. Envoy and traefik both just forward the request with an invalid path. Don't remeber what nginx does but it was super weird.

Comment thread geps/gep-4359/index.md Outdated
( Required for Experimental status and above)

* At least one Feature Name must be listed.
* The `Conformance Details` must be filled out, with conformance test scenarios listed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if we don't have specific APIs, how will we be able to fill conformance tests?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the idea is to have a high level plan of what will happen, then have an specific API with conformance tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah might be a bit too early

@muxanasov

Copy link
Copy Markdown
Contributor

Hey Steven! Thanks for this proposal. I think moving this GAP to provisional totally makes sense. I also agree that supporting syntax/dialect etc should be vendor specific similar to path matchers. In conformance tests we could use some common regex syntax - not to test regex support correctness, but rather to tests API consistency and substitution correctness.

@snorwin

snorwin commented Apr 1, 2026

Copy link
Copy Markdown
Member

/cc @snorwin

@k8s-ci-robot
k8s-ci-robot requested a review from snorwin April 1, 2026 15:28
@Stevenjin8

Copy link
Copy Markdown
Contributor Author

Some unresolved comments:

#4361 (comment)

Hi @muxanasov and thanks for the proposal!

I agree this is a very important feature to get people moving to Gateway API, I would just like to put my "ingress-nginx former maintainer" hat a bit: having regexes as part of the API can be very very complex (and somehow dangerous):

    the Regex parser used by Envoy (re2) is different from NGINX (PCRE) and can even be different from other implementations that may be pure Go or pure Rust, or even on different technologies. So it would be a bit "hard" to define conformance, if each implementation behaves on a different way.
    Because the subset of regex characters that can be used on an API field is big, and because eventually these characters may end up on a proxy configuration file (eg.: NGINX or HAProxy), we need to be really careful with what characters we would allow. We won't be able to do any validation on the API side, because we will be able to only limit the characters for non-characters control but not really define if that is a valid regex. So it is a wide open arbitrary API field
    On some cases, IIRC a regex can be used for internal redirects, that may lead to users pointing to internal non-exposed endpoints. This is a bit risky

That said, I think we do need some feature like this, but if possible I would like to try to scope it to:

    Can we define a minimum regex set that is acceptable by all the parsers, with a minimum set of characters? (eg.: can we define what is the acceptable set of characters that we could validate?)
    Should this be "Implementation Specific", given each implementation may have its own way of parsing a regex?
    Is there any "user story" of regexes that could actually be covered by a lack of some other API that would be more strict?

Thanks again for the proposal!

@youngnick

Copy link
Copy Markdown
Contributor

The biggest problem I see is determining conflicts.

What happens if a regex replacement ends up the same as a Path match? Does that that not count because you've "entered" the regex path match first? (To be honest, that's kind of what I would expect, that matches happen in an order like: Exact, Prefix except for /, Regex, then Prefix of exactly / as a fallthrough.)

What happens when people want to use the captured groups from the path match in other places? Headers? Query parameters?Will we ever allow that? (My feeling would be no, at a minimum that should be a very specific Match type, but we should be specific about what the match groups can and cannot be used for).

It's important to remember that, if we have any conformance tests at all, then the we are effectively blessing some subset of regular expression syntax as part of Gateway API, even if it's behind an extended feature. And if we don't have conformance tests, then we should leave this as a custom matcher, and have it be implementation specific. We can't define a behavior and then not require tests for it for something as widely used as this.

Comment thread geps/gep-4359/index.md Outdated
Comment on lines +41 to +45
| Implementation | Support |
|----------------|------------|
| Envoy | [config.route.v3.RouteAction.regex_rewrite](https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-field-config-route-v3-routeaction-regex-rewrite) |
| HAProxy | [http-request replace-path](https://cbonte.github.io/haproxy-dconv/2.5/configuration.html#4.2-http-request%20replace-path) |
| NGINX | [ngx_http_rewrite_module.html#rewrite](https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite) |

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This section also needs to include information about which regex engine is used by each implementation. Should probably include the kgateway/agentgateway proxy as well. We'll need that info to be able to determine if we can do conformance testing at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I dont think agent gateway supports regex substitutions

Comment thread geps/gep-4359/index.md Outdated
Comment on lines +51 to +55
This is a provisional GEP, so no specific API details, but at a high level there will be two fields: `pattern` and `substitution`.
`pattern` will be a regular expression.
**ALL** instances of `pattern` in the url path MUST be replaced with `substitution`.
The url path includes the leading slash.
We do not define what happens if the path is invalid after the substitution (e.g. missing leading slash)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a pretty big departure from how the rest of path matching works - it only allows matching on full path components - the text between / and /, not on substrings inside path components. That's not a deal breaker, but it's probably worth calling out and then explaining.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can see the callout, but this would be completely seperate from path matching. That is you can have an prefix or even exact path and have a regex rewrite configured. Also, fairly sure that normal RegularExpression path matching does not give path components special treatment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So really emphasizing the fact that the pattern here, is not the pattern in the pathMatch and this GEP is completely orthogonal to any type of path matching (unlike NGINX, where you can reference capture groups in the match pattern).

Comment thread geps/gep-4359/index.md Outdated
The url path includes the leading slash.
We do not define what happens if the path is invalid after the substitution (e.g. missing leading slash)

To be consistent with [HTTP path match](https://gateway-api.sigs.k8s.io/reference/spec/#pathmatchtype), we let implementation define what flavor of regex they will support, and what features they might support.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again, I don't think this is viable without adding Extended features indicating which flavor of regex the implementation supports or something, or defining a smaller subset of regex functionality that MUST be supported, with clear warnings that if you go outside that, you lose portability guarantees.

It's critical that the same HTTPRoute MUST work on another implementation, given the same set of Extended features available across the two implementations. So, we either need to ensure that any two implementations can support the same regex, or we add Extended features to cover the various regex engine options.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My bad, I mean to delete this, but later I talk about using POSIX ERE as a baseline regex language. I couldn't find any trustworthy sources talking about the explicitly compatibility between POSIX ERE and RE2/PCRE, but from some reading, POSIX ERE seems like a fairly good baseline, especially because we don't need to think about unicode, newlines, control characters, etc.

@Stevenjin8

Copy link
Copy Markdown
Contributor Author

@youngnick This is not relevant to this PR, but I'll add that the priority of RegularExpression matches is undefined by the gateway api spec. For example, Istio and Envoy Gateway give them different priorities (istio/istio#58641), which is a bit annoying, especially when trying to define catch-all routes, but its probably too late to change now.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. and removed cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Apr 10, 2026
@Stevenjin8
Stevenjin8 force-pushed the gateway-api branch 2 times, most recently from eb8e8ed to 42673be Compare April 10, 2026 17:36
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 10, 2026
Comment thread geps/gep-4359/index.md Outdated
We do not define what happens if the path is invalid after the substitution (e.g. missing leading slash)

If the implementation deems `pattern` or `substitution` to be invalid (contains illegal characters, unmatched parenthesis, unsupported features),
it MUST not accept the HTTPRoute with a `Reason` of `invalid` and a descriptive `Message`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should the entire route be rejected? Can the invalid rule alone be rejected and have the appropriate error message (similar to an invalid backendref on one rule) ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, I agree that this behavior should be defined, and more, I think that rejecting the rule is probably right.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@youngnick +1, we can just call the route "invalid", and let the implementation decide what to do subject to contrains

https://github.com/kubernetes-sigs/gateway-api/blob/main/apis/v1/shared_types.go#L435-L470

@Stevenjin8

Copy link
Copy Markdown
Contributor Author

/retest

@Stevenjin8

Copy link
Copy Markdown
Contributor Author

@youngnick the GWAPI recording has not been uploaded, so most of this is from word-of-mouth. It seems like we want some very strong validation on the regex expression at the API-level. I'm happy to restrict the character set to something like a, .., z, A, ..., Z, 0, ..., 0, -, _, %, [, ], {, }, \, ., \, but I do not think its theoretically possible to write a cel or regex expression that validates a regex is valid (or even having balanced parentheses, etc)

@youngnick

Copy link
Copy Markdown
Contributor

@Stevenjin8, yeah, we want to make sure we limit this to a limited subset of regex characters as far as possible.

Sadly, I think we'll need * and + as well, I think. Basically, all the special characters you mention in the section about ERE will need to be in there.

We'll need negative conformance tests for all the cases you call out in that section as well, maybe? Like, not supporting classes etc, so no :alpha: etc.

@k8s-triage-robot

Copy link
Copy Markdown

The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.

This bot triages PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the PR is closed

You can:

  • Mark this PR as fresh with /remove-lifecycle stale
  • Close this PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@kubernetes-prow kubernetes-prow Bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 15, 2026
@Stevenjin8

Copy link
Copy Markdown
Contributor Author

@youngnick @rikatz @robscott now that #4764 merged, would love to get another review.

@robscott robscott left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @Stevenjin8!

Comment thread geps/gep-4360/index.yaml Resolved
@kubernetes-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: robscott, Stevenjin8

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 28, 2026
@robscott

Copy link
Copy Markdown
Member

/release-note-none
/hold cancel

@kubernetes-prow kubernetes-prow Bot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Aug 28, 2026
@kubernetes-prow kubernetes-prow Bot added do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 28, 2026
@Stevenjin8

Copy link
Copy Markdown
Contributor Author

/release-note-none
/hold cancel

@kubernetes-prow kubernetes-prow Bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Aug 28, 2026
Comment thread geps/gep-4360/index.md Resolved
@rikatz

rikatz commented Sep 18, 2026

Copy link
Copy Markdown
Member

/lifecycle active

@kubernetes-prow kubernetes-prow Bot added lifecycle/active Indicates that an issue or PR is actively being worked on by a contributor. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Sep 18, 2026
@rikatz

rikatz commented Sep 18, 2026 •

Copy link
Copy Markdown
Member

/lgtm

This looks good for provisional, we can iterate better into the how during Implementable and Experimental (I do expect more deep discussion happening on it)

thanks @Stevenjin8

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 18, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 8f24c96 into kubernetes-sigs:main Sep 18, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/feature Categorizes issue or PR as related to a new feature. kind/gep PRs related to Gateway Enhancement Proposal(GEP) lgtm "Looks good to me", indicates that a PR is ready to be merged. lifecycle/active Indicates that an issue or PR is actively being worked on by a contributor. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants