-
Notifications
You must be signed in to change notification settings - Fork 29.8k
fix: strip _NEXTSEP_ from interpolated pathnames #84430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
188 changes: 188 additions & 0 deletions
188
packages/next/src/shared/lib/router/utils/route-match-utils.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import { safeCompile } from './route-match-utils' | ||
| import { | ||
| PARAM_SEPARATOR, | ||
| stripNormalizedSeparators, | ||
| } from '../../../../lib/route-pattern-normalizer' | ||
|
|
||
| describe('safeCompile', () => { | ||
| describe('interception route patterns', () => { | ||
| it('should strip _NEXTSEP_ from compiled output for (.) interception marker', () => { | ||
| // Pattern with interception marker followed by parameter | ||
| const pattern = '/photos/(.):author/:id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| // The interception marker (.) is treated as an unnamed parameter (index 0) | ||
| const result = compile({ '0': '(.)', author: 'next', id: '123' }) | ||
|
|
||
| // Should NOT contain the internal separator | ||
| expect(result).toBe('/photos/(.)next/123') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ from compiled output for (..) interception marker', () => { | ||
| const pattern = '/photos/(..):category/:id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ '0': '(..)', category: 'blog', id: '456' }) | ||
|
|
||
| expect(result).toBe('/photos/(..)blog/456') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ from compiled output for (...) interception marker', () => { | ||
| const pattern = '/photos/(...):path' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ '0': '(...)', path: 'deep/nested/route' }) | ||
|
|
||
| expect(result).toBe('/photos/(...)deep/nested/route') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ from compiled output for (..)(..) interception marker', () => { | ||
| const pattern = '/photos/(.)(..)/:id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| // (..)(..) is treated as two unnamed parameters | ||
| const result = compile({ '0': '(..)', '1': '(..)', id: '789' }) | ||
|
|
||
| expect(result).toBe('/photos/(..)(..)/789') | ||
| }) | ||
|
|
||
| it('should handle multiple interception markers in one pattern', () => { | ||
| const pattern = '/(.):author/photos/(.):id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| // Multiple markers are numbered sequentially | ||
| const result = compile({ | ||
| '0': '(.)', | ||
| author: 'john', | ||
| '1': '(.)', | ||
| id: '999', | ||
| }) | ||
|
|
||
| expect(result).toBe('/(.)john/photos/(.)999') | ||
| }) | ||
|
|
||
| it('should work with the actual failing case from interception routes', () => { | ||
| // This is the exact pattern that was failing | ||
| const pattern = | ||
| '/intercepting-routes-dynamic/photos/(.):nxtPauthor/:nxtPid' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ | ||
| '0': '(.)', | ||
| nxtPauthor: 'next', | ||
| nxtPid: '123', | ||
| }) | ||
|
|
||
| expect(result).toBe('/intercepting-routes-dynamic/photos/(.)next/123') | ||
| }) | ||
| }) | ||
|
|
||
| describe('patterns without normalization needs', () => { | ||
| it('should work normally for patterns without adjacent parameters', () => { | ||
| const pattern = '/photos/:author/:id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ author: 'jane', id: '456' }) | ||
|
|
||
| expect(result).toBe('/photos/jane/456') | ||
| }) | ||
|
|
||
| it('should work with optional parameters', () => { | ||
| const pattern = '/photos/:author?/:id' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ id: '789' }) | ||
|
|
||
| expect(result).toBe('/photos/789') | ||
| }) | ||
|
|
||
| it('should work with catchall parameters', () => { | ||
| const pattern = '/files/:path*' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ path: ['folder', 'subfolder', 'file.txt'] }) | ||
|
|
||
| expect(result).toBe('/files/folder/subfolder/file.txt') | ||
| }) | ||
| }) | ||
|
|
||
| describe('edge cases', () => { | ||
| it('should handle patterns with path separators between parameters', () => { | ||
| // Normal case - parameters separated by path segments | ||
| const pattern = '/:param1/separator/:param2' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| const result = compile({ param1: 'value1', param2: 'value2' }) | ||
|
|
||
| expect(result).toBe('/value1/separator/value2') | ||
| }) | ||
|
|
||
| it('should not strip _NEXTSEP_ from user content outside interception markers', () => { | ||
| // If user content happens to contain _NEXTSEP_, it should be preserved | ||
| // Only separators after interception markers should be stripped | ||
| const pattern = '/:folder/:file' | ||
| const compile = safeCompile(pattern, { validate: false }) | ||
|
|
||
| // User has a file or folder named something_NEXTSEP_something | ||
| const result = compile({ | ||
| folder: 'my_NEXTSEP_folder', | ||
| file: 'my_NEXTSEP_file.txt', | ||
| }) | ||
|
|
||
| // The _NEXTSEP_ in user content should be preserved | ||
| expect(result).toBe('/my_NEXTSEP_folder/my_NEXTSEP_file.txt') | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('stripNormalizedSeparators', () => { | ||
| it('should strip _NEXTSEP_ after single dot interception marker', () => { | ||
| const input = `/photos/(.)${PARAM_SEPARATOR}next/123` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe('/photos/(.)next/123') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ after double dot interception marker', () => { | ||
| const input = `/photos/(..)${PARAM_SEPARATOR}blog/456` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe('/photos/(..)blog/456') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ after triple dot interception marker', () => { | ||
| const input = `/photos/(...)${PARAM_SEPARATOR}deep/nested/route` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe('/photos/(...)deep/nested/route') | ||
| }) | ||
|
|
||
| it('should strip _NEXTSEP_ for adjacent interception markers with parameters', () => { | ||
| // When there are two separate interception paths, each with parameters | ||
| // Pattern: /(.)_NEXTSEP_:param1/(..)_NEXTSEP_:param2 | ||
| // After compilation: /(.)_NEXTSEP_value1/(..)_NEXTSEP_value2 | ||
| const input = `/(.)${PARAM_SEPARATOR}first/(..)${PARAM_SEPARATOR}second` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe('/(.)first/(..)second') | ||
| }) | ||
|
|
||
| it('should handle multiple interception markers in one path', () => { | ||
| const input = `/(.)${PARAM_SEPARATOR}john/photos/(.)${PARAM_SEPARATOR}999` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe('/(.)john/photos/(.)999') | ||
| }) | ||
|
|
||
| it('should NOT strip _NEXTSEP_ from user content', () => { | ||
| // If the separator appears outside the interception marker context, | ||
| // it should be preserved as it's part of user content | ||
| const input = `/folder/my${PARAM_SEPARATOR}file/data${PARAM_SEPARATOR}value` | ||
| const result = stripNormalizedSeparators(input) | ||
| expect(result).toBe( | ||
| `/folder/my${PARAM_SEPARATOR}file/data${PARAM_SEPARATOR}value` | ||
| ) | ||
| }) | ||
|
|
||
| it('should only strip after closing paren, not before', () => { | ||
| const input = `/path${PARAM_SEPARATOR}(.)${PARAM_SEPARATOR}value` | ||
| const result = stripNormalizedSeparators(input) | ||
| // Should only strip the one after ), not the one before | ||
| expect(result).toBe(`/path${PARAM_SEPARATOR}(.)value`) | ||
| }) | ||
| }) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
stripNormalizedSeparatorsfunction will incorrectly modify user parameter values that contain the substring)_NEXTSEP_, not just the normalization markers it's intended to strip.View Details
📝 Patch Details
Analysis
Silent data corruption in stripNormalizedSeparators strips user content containing
)_NEXTSEP_What fails: The
stripNormalizedSeparatorsfunction inpackages/next/src/lib/route-pattern-normalizer.tsline 115 strips ANY occurrence of)_NEXTSEP_from compiled pathnames, not just normalization markers after interception routesHow to reproduce:
Result: User parameter values containing the substring
)_NEXTSEP_are silently corrupted. The 10-character sequence is removed without error, breaking URLs, usernames, file paths, etc.Expected: The function's documentation (lines 108-109) claims it uses a "targeted approach" that "ensures we don't accidentally remove the separator from legitimate user content," but the regex
/\)_NEXTSEP_/gmatches ANY)_NEXTSEP_regardless of context.Fix: Changed regex from
/\)_NEXTSEP_/gto/(\(\.+\))_NEXTSEP_/gto match only interception marker patterns:(.),(..),(...)followed by the separator, not arbitrary user content.