fix: correct path traversal check to allow '...' in URL path segments#461
Merged
mcollina merged 2 commits intofastify:mainfrom Mar 11, 2026
Merged
Conversation
Next.js catch-all routes like '[...slug]' appear percent-encoded as '%5B...slug%5D' in static chunk URLs. These contain '...' as a substring but are not path traversal and should not be rejected. See: fastify#460
Contributor
Author
|
To start, I am only pushing up the test change. The unit test should fail. Once we've shown that, I will push up the fix. I just need a maintainer to approve the GHA workflow run. |
Member
|
go ahead and keep going with the implementation |
The previous check used includes('..') which incorrectly rejects URLs
containing '...' as part of a filename or route name, such as Next.js
catch-all routes like '[...slug]' (%5B...slug%5D when percent-encoded).
Replace with a targeted check that only flags '..' when it appears as a
complete path segment (surrounded by '/' or at a string boundary).
Fixes: fastify#460
Contributor
Author
|
Pushed up the proposed fix. |
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
Fixes #460
The path traversal check in
buildURLusesincludes('..')which incorrectly rejects URLs containing...(three dots) as part of a filename or route name, for example, Next.js catch-all routes like[...slug], which appear percent-encoded as%5B...slug%5Din static chunk URLs.This PR replaces the check with a more precise one that only flags
..when it appears as a complete path segment (surrounded by/or at a string boundary).The fix is intentionally structured for performance:
===is checked first as the cheapest comparison, followed by twoincludescalls that short-circuit. We considered a regex approach (/(?:^|\/)\.\.(?:\/|$)/) but benchmarking showedincludesruns 2–3x faster for typical URL lengths.All existing path traversal test cases continue to pass.