Skip to content

Commit 8784682

Browse files
ntkoopmanijjk
andauthored
Escape string when converting to regexp (vercel#31791)
fixes vercel#31411 ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` Co-authored-by: JJ Kasper <[email protected]>
1 parent d2cef24 commit 8784682

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/next/shared/lib/router/utils/prepare-destination.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { NextParsedUrlQuery } from '../../../../server/request-meta'
44
import type { Params } from '../../../../server/router'
55
import type { RouteHas } from '../../../../lib/load-custom-routes'
66
import { compile, pathToRegexp } from 'next/dist/compiled/path-to-regexp'
7+
import escapeStringRegexp from 'next/dist/compiled/escape-string-regexp'
78
import { parseUrl } from './parse-url'
89

910
export function matchHas(
@@ -240,7 +241,7 @@ function getSafeParamName(paramName: string) {
240241

241242
function escapeSegment(str: string, segmentName: string) {
242243
return str.replace(
243-
new RegExp(`:${segmentName}`, 'g'),
244+
new RegExp(`:${escapeStringRegexp(segmentName)}`, 'g'),
244245
`__ESC_COLON_${segmentName}`
245246
)
246247
}

test/integration/route-index/test/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ const runTests = () => {
3232
expect(res.status).toBe(404)
3333
expect(await res.text()).toContain('page could not be found')
3434
})
35+
36+
it('should handle /index/?bar%60%3C%25%22%27%7B%24%2A%25%5C correctly', async () => {
37+
const res = await fetchViaHTTP(
38+
appPort,
39+
'/index/?bar%60%3C%25%22%27%7B%24%2A%25%5C'
40+
)
41+
expect(res.status).toBe(200)
42+
})
43+
44+
it('should handle /index?file%3A%5C correctly', async () => {
45+
const res = await fetchViaHTTP(appPort, '/index?file%3A%5C')
46+
expect(res.status).toBe(200)
47+
})
3548
}
3649

3750
describe('Route index handling', () => {

0 commit comments

Comments
 (0)