Skip to content

fix: preserve percent-encoding in prefix stripping#255

Merged
mcollina merged 1 commit intofastify:mainfrom
panva:fix/preserve-percent-encoding-in-prefix-stripping
Mar 5, 2026
Merged

fix: preserve percent-encoding in prefix stripping#255
mcollina merged 1 commit intofastify:mainfrom
panva:fix/preserve-percent-encoding-in-prefix-stripping

Conversation

@panva
Copy link
Contributor

@panva panva commented Mar 5, 2026

When a middleware is registered with a path prefix (e.g. app.use('/prefix', fn)), the prefix is stripped from req.url before invoking the middleware. The previous implementation used normalizePathForMatching() which calls sanitizeUrlPath(), decoding percent-encoded characters (%20, %2F, etc.) before building the new req.url. This caused encoded characters to appear decoded in req.url.

Fix

Try the regexp against the original sanitized URL first (preserving percent-encoding). When that matches, slice the original URL and apply only structural normalizations (duplicate slashes, trailing slash). Fall back to the fully normalized URL only when normalization was necessary for matching (e.g. duplicate leading slashes, semicolon delimiters)

Checklist

When a middleware is registered with a path prefix (e.g. app.use('/prefix', fn)),
the prefix is stripped from req.url before invoking the middleware. The previous
implementation used normalizePathForMatching() which calls sanitizeUrlPath(),
decoding percent-encoded characters (%20, %2F, etc.) before building the new
req.url. This caused encoded characters to appear decoded in req.url.

Fix by trying the regexp against the original sanitized URL first (preserving
percent-encoding). When that matches, slice the original URL and apply only
structural normalizations (duplicate slashes, trailing slash). Fall back to
the fully normalized URL only when normalization was necessary for matching
(e.g. duplicate leading slashes, semicolon delimiters).
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit 24aa8d3 into fastify:main Mar 5, 2026
14 checks passed
@panva panva deleted the fix/preserve-percent-encoding-in-prefix-stripping branch March 5, 2026 13:19
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.

3 participants