Unverified Commit 8784682a authored by Tim Koopman's avatar Tim Koopman Committed by GitHub

Escape string when converting to regexp (#31791)



fixes #31411

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent d2cef24d
...@@ -4,6 +4,7 @@ import type { NextParsedUrlQuery } from '../../../../server/request-meta' ...@@ -4,6 +4,7 @@ import type { NextParsedUrlQuery } from '../../../../server/request-meta'
import type { Params } from '../../../../server/router' import type { Params } from '../../../../server/router'
import type { RouteHas } from '../../../../lib/load-custom-routes' import type { RouteHas } from '../../../../lib/load-custom-routes'
import { compile, pathToRegexp } from 'next/dist/compiled/path-to-regexp' import { compile, pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import escapeStringRegexp from 'next/dist/compiled/escape-string-regexp'
import { parseUrl } from './parse-url' import { parseUrl } from './parse-url'
export function matchHas( export function matchHas(
...@@ -240,7 +241,7 @@ function getSafeParamName(paramName: string) { ...@@ -240,7 +241,7 @@ function getSafeParamName(paramName: string) {
function escapeSegment(str: string, segmentName: string) { function escapeSegment(str: string, segmentName: string) {
return str.replace( return str.replace(
new RegExp(`:${segmentName}`, 'g'), new RegExp(`:${escapeStringRegexp(segmentName)}`, 'g'),
`__ESC_COLON_${segmentName}` `__ESC_COLON_${segmentName}`
) )
} }
......
...@@ -32,6 +32,19 @@ const runTests = () => { ...@@ -32,6 +32,19 @@ const runTests = () => {
expect(res.status).toBe(404) expect(res.status).toBe(404)
expect(await res.text()).toContain('page could not be found') expect(await res.text()).toContain('page could not be found')
}) })
it('should handle /index/?bar%60%3C%25%22%27%7B%24%2A%25%5C correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/index/?bar%60%3C%25%22%27%7B%24%2A%25%5C'
)
expect(res.status).toBe(200)
})
it('should handle /index?file%3A%5C correctly', async () => {
const res = await fetchViaHTTP(appPort, '/index?file%3A%5C')
expect(res.status).toBe(200)
})
} }
describe('Route index handling', () => { describe('Route index handling', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment