Repository navigation
fix: match request paths the same way under every reading - #342
Merged
Merged
Conversation
Routers read request paths differently: some decode each segment, some
route on the fully decoded path, some on the escaped path, and some
remove empty and dot segments. The validator read a path one way, so
/users/m%65 matched /users/{id} while a router that decodes segments
dispatched it to /users/me.
The router now resolves the path as sent and under each normalized
reading: decoded path characters, decoded slashes, and empty and dot
segments removed. When two readings select different operations, the
request fails with ValidationSubType "ambiguousPath"
(router.ErrAmbiguousPath, which also matches router.ErrPathNotFound).
paths.FindPath does the same when no router is configured.
Server base paths are removed only on a segment boundary, so a base of
/api no longer turns /apistatus into /status.
The radix tree keeps empty segments, so /items//5 matches /items/{a}/{b}
as the regex matcher does. When the path as sent matches nothing,
doubled slashes are collapsed and matched again, as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #342 +/- ##
==========================================
+ Coverage 98.74% 98.76% +0.01%
==========================================
Files 94 95 +1
Lines 12310 12428 +118
==========================================
+ Hits 12156 12274 +118
Misses 129 129
Partials 25 25
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Routers read request paths differently. Some decode each segment, some route on the fully decoded path, some on the escaped path, and some remove empty and dot segments. The validator read a path one way, so
GET /users/m%65matched/users/{id}while a router that decodes segments dispatched it to/users/me. Similarly,/users/5%2Fsecretsis one segment to some routers and three to others.The router now resolves the path as sent and under each normalized reading:
%65→e,%3A→:)%2F→/)When two readings select different operations, the request fails with
ValidationSubTypeambiguousPath. When the readings agree, or the others match nothing, the route as sent is used, so encoded parameter values such as/users/a%20bvalidate as before.paths.FindPathapplies the same check when no router is configured (standalone parameter, request, and response validators).Behaviour changes
router.ErrAmbiguousPathis new. It also matchesrouter.ErrPathNotFound, so existingerrors.Ischecks keep treating it as a missing path.helpers.ValidationAmbiguousPathanderrors.HowToFixAmbiguousPathare new./api,/apistatusis no longer matched as/status.radix.Treekeeps empty segments, so/items//5matches/items/{a}/{b}, as the regex matcher already did. When the path as sent matches nothing, doubled slashes are collapsed and matched again, so/users//alicestill finds/users/{id}.Testing
go test -race ./...passes. Every changed file has 100% statement coverage.internal/pathvariant), base path boundaries, radix empty segments, router andpaths.FindPathambiguity in path-only, strict, radix and regex modes, and end-to-endValidateHttpRequest./,%2F, or/..segments validate in under 0.5ms.main(10 interleaved runs,benchstat):splitPathno longer copies segments)requestsandparametersshow no significant change in time or allocs/op🤖 Generated with Claude Code