Skip to content

fix: match request paths the same way under every reading - #342

Merged
daveshanley merged 1 commit into
mainfrom
claude/path-matching-normalization
Nov 11, 2026
Merged

daveshanley merged 1 commit into
mainfrom
claude/path-matching-normalization

Conversation

@daveshanley

Copy link
Copy Markdown
Member

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%65 matched /users/{id} while a router that decodes segments dispatched it to /users/me. Similarly, /users/5%2Fsecrets is one segment to some routers and three to others.

The router now resolves the path as sent and under each normalized reading:

  • with percent-encoded path characters decoded (%65 → e, %3A → :)
  • with encoded slashes decoded (%2F → /)
  • with empty and dot segments removed

When two readings select different operations, the request fails with ValidationSubType ambiguousPath. When the readings agree, or the others match nothing, the route as sent is used, so encoded parameter values such as /users/a%20b validate as before.

paths.FindPath applies the same check when no router is configured (standalone parameter, request, and response validators).

Behaviour changes

  • router.ErrAmbiguousPath is new. It also matches router.ErrPathNotFound, so existing errors.Is checks keep treating it as a missing path.
  • helpers.ValidationAmbiguousPath and errors.HowToFixAmbiguousPath are new.
  • Server base paths are removed only on a segment boundary. With a server URL ending in /api, /apistatus is no longer matched as /status.
  • radix.Tree keeps empty segments, so /items//5 matches /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//alice still finds /users/{id}.

Testing

  • go test -race ./... passes. Every changed file has 100% statement coverage.
  • New tests cover the readings (internal/pathvariant), base path boundaries, radix empty segments, router and paths.FindPath ambiguity in path-only, strict, radix and regex modes, and end-to-end ValidateHttpRequest.
  • Paths with thousands of /, %2F, or /.. segments validate in under 0.5ms.
  • Benchmarks against main (10 interleaved runs, benchstat):
    • radix lookups 10–28% faster, allocations halved (splitPath no longer copies segments)
    • router allocations down 5–20%
    • requests and parameters show no significant change in time or allocs/op

🤖 Generated with Claude Code

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

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.76%. Comparing base (754a5b7) to head (d69a7f3).

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              
Flag Coverage Δ
unittests 98.76% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@daveshanley
daveshanley merged commit 70dff79 into main Nov 11, 2026
4 checks passed
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