Add decodeURLPath function for URL decoding and validation#3915
Add decodeURLPath function for URL decoding and validation#3915
Conversation
|
|
The latest updates on your projects. Learn more about Argos notifications ↗︎
|
25d727d to
b69186d
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces canonical URL-path decoding in the GitBook Next.js middleware to reduce duplicate URL variants (important for caching) while validating/limiting decoding to mitigate malformed input and potential DoS patterns.
Changes:
- Added
decodeURLPath(URL)helper to decode percent-encoded path segments with a 2-pass limit and character validation. - Updated middleware to redirect requests to the decoded/canonical URL when applicable.
- Added unit tests covering decoding behavior, nested encoding limits, invalid characters, and path-length bounds.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| packages/gitbook/src/middleware.ts | Redirects to a decoded/canonical pathname early in the request pipeline. |
| packages/gitbook/src/lib/data/urls.ts | Implements decodeURLPath plus invalid-character detection and decoding-pass limits. |
| packages/gitbook/src/lib/data/urls.test.ts | Adds test coverage for the new decoding/validation behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Implement a new function to decode URL path components, ensuring validation against invalid characters and limiting decoding iterations.
This will now error on path with too deeply nested percent encoding, which should reduces 404 for voluntarily bad URL.
It can also handle percent encoded character properly in the URL.