Skip to content

fix(http-route): anchor regex matches to the entire value - #4638

Open
danish293 wants to merge 1 commit into
linkerd:mainfrom
danish293:http-route-anchor-regex
Open

danish293 wants to merge 1 commit into
linkerd:mainfrom
danish293:http-route-anchor-regex

Conversation

@danish293

Copy link
Copy Markdown

Regex route matches miss values that the whole pattern matches

MatchPath, MatchHeader and MatchQueryParam check regexes with Regex::find and then require the match to cover the entire value. find uses leftmost-first semantics, so with /admin|/admin/.* it returns /admin for /admin/users and the route is rejected, even though the pattern fully matches the path. Lazy repetitions (/foo/.+?) fail the same way. The policy controller passes the Gateway API regex through unanchored, so the request is then served by whichever less specific route matches. In inbound policy that route can have a looser authorization, for example a catch-all / route next to a restricted admin route.

Repro, with routes [Prefix("/") -> catch-all, Regex("/admin|/admin/.*") -> admin] and GET /admin/users, http::find selects the catch-all route on main and the admin route with this change. Regexes from the policy API are now compiled as ^(?:re)$ after the raw pattern is validated on its own, so a pattern like /foo)|(/bar is still rejected. Regression tests for the path, header and query param conversions fail on main and pass here.

Path, header, and query param regex matches use `Regex::find` and then
require the match to span the whole value. `find` returns the
leftmost-first match, so a pattern like `/admin|/admin/.*` stops at
`/admin` and never matches `/admin/users`, and lazy repetitions stop at
the shortest match. The route is then skipped and the request is
handled by a less specific route, which may carry a different
authorization policy.

Compile regexes received from the policy API as `^(?:re)$` so the
search only considers matches of the entire value. The raw pattern is
parsed first so that wrapping cannot make an invalid pattern valid.

Signed-off-by: danish bhat <danish@medios.digiscrypt.com>
@danish293
danish293 requested a review from a team as a code owner October 16, 2026 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant