Unverified Commit fc216731 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update header replacing to be more relaxed (#15592)

Instead of trying to parse header values as URLs and then replace path segments in them this switches to escaping characters that can break `path-to-regexp` compiling

Fixes: https://github.com/vercel/next.js/issues/15580
x-ref: https://github.com/vercel/vercel/pull/4942
parent 1e8271eb
...@@ -479,26 +479,41 @@ export default class Server { ...@@ -479,26 +479,41 @@ export default class Server {
if (!value.includes(':')) { if (!value.includes(':')) {
return value return value
} }
const { parsedDestination } = prepareDestination(
value, for (const key of Object.keys(params)) {
params, if (value.includes(`:${key}`)) {
{}, value = value
false, .replace(
'' new RegExp(`:${key}\\*`, 'g'),
`:${key}--ESCAPED_PARAM_ASTERISKS`
)
.replace(
new RegExp(`:${key}\\?`, 'g'),
`:${key}--ESCAPED_PARAM_QUESTION`
)
.replace(
new RegExp(`:${key}\\+`, 'g'),
`:${key}--ESCAPED_PARAM_PLUS`
) )
.replace(
new RegExp(`:${key}(?!\\w)`, 'g'),
`--ESCAPED_PARAM_COLON${key}`
)
}
}
value = value
.replace(/(:|\*|\?|\+|\(|\)|\{|\})/g, '\\$1')
.replace(/--ESCAPED_PARAM_PLUS/g, '+')
.replace(/--ESCAPED_PARAM_COLON/g, ':')
.replace(/--ESCAPED_PARAM_QUESTION/g, '?')
.replace(/--ESCAPED_PARAM_ASTERISKS/g, '*')
if (
!parsedDestination.pathname ||
!parsedDestination.pathname.startsWith('/')
) {
// the value needs to start with a forward-slash to be compiled // the value needs to start with a forward-slash to be compiled
// correctly // correctly
return compilePathToRegex(`/${value}`, { validate: false })( return compilePathToRegex(`/${value}`, { validate: false })(
params params
).substr(1) ).substr(1)
} }
return formatUrl(parsedDestination)
}
// Headers come very first // Headers come very first
const headers = this.customRoutes.headers.map((r) => { const headers = this.customRoutes.headers.map((r) => {
......
...@@ -243,6 +243,47 @@ module.exports = { ...@@ -243,6 +243,47 @@ module.exports = {
key: 'some:path', key: 'some:path',
value: 'hi', value: 'hi',
}, },
{
key: 'x-test',
value: 'some:value*',
},
{
key: 'x-test-2',
value: 'value*',
},
{
key: 'x-test-3',
value: ':value?',
},
{
key: 'x-test-4',
value: ':value+',
},
{
key: 'x-test-5',
value: 'something https:',
},
{
key: 'x-test-6',
value: ':hello(world)',
},
{
key: 'x-test-7',
value: 'hello(world)',
},
{
key: 'x-test-8',
value: 'hello{1,}',
},
{
key: 'x-test-9',
value: ':hello{1,2}',
},
{
key: 'content-security-policy',
value:
"default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com/:path",
},
], ],
}, },
{ {
......
...@@ -46,6 +46,24 @@ const runTests = (isDev = false) => { ...@@ -46,6 +46,24 @@ const runTests = (isDev = false) => {
expect(html).toMatch(/multi-rewrites/) expect(html).toMatch(/multi-rewrites/)
}) })
it('should handle param like headers properly', async () => {
const res = await fetchViaHTTP(appPort, '/my-other-header/my-path')
expect(res.headers.get('x-path')).toBe('my-path')
expect(res.headers.get('somemy-path')).toBe('hi')
expect(res.headers.get('x-test')).toBe('some:value*')
expect(res.headers.get('x-test-2')).toBe('value*')
expect(res.headers.get('x-test-3')).toBe(':value?')
expect(res.headers.get('x-test-4')).toBe(':value+')
expect(res.headers.get('x-test-5')).toBe('something https:')
expect(res.headers.get('x-test-6')).toBe(':hello(world)')
expect(res.headers.get('x-test-7')).toBe('hello(world)')
expect(res.headers.get('x-test-8')).toBe('hello{1,}')
expect(res.headers.get('x-test-9')).toBe(':hello{1,2}')
expect(res.headers.get('content-security-policy')).toBe(
"default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com/my-path"
)
})
it('should not match dynamic route immediately after applying header', async () => { it('should not match dynamic route immediately after applying header', async () => {
const res = await fetchViaHTTP(appPort, '/blog/post-321') const res = await fetchViaHTTP(appPort, '/blog/post-321')
expect(res.headers.get('x-something')).toBe('applied-everywhere') expect(res.headers.get('x-something')).toBe('applied-everywhere')
...@@ -348,7 +366,7 @@ const runTests = (isDev = false) => { ...@@ -348,7 +366,7 @@ const runTests = (isDev = false) => {
it('should apply params header key/values with URL that has port', async () => { it('should apply params header key/values with URL that has port', async () => {
const res = await fetchViaHTTP(appPort, '/with-params/url2/first') const res = await fetchViaHTTP(appPort, '/with-params/url2/first')
expect(res.headers.get('x-url')).toBe( expect(res.headers.get('x-url')).toBe(
'https://example.com:8080/?hello=first' 'https://example.com:8080?hello=first'
) )
}) })
...@@ -667,6 +685,47 @@ const runTests = (isDev = false) => { ...@@ -667,6 +685,47 @@ const runTests = (isDev = false) => {
key: 'some:path', key: 'some:path',
value: 'hi', value: 'hi',
}, },
{
key: 'x-test',
value: 'some:value*',
},
{
key: 'x-test-2',
value: 'value*',
},
{
key: 'x-test-3',
value: ':value?',
},
{
key: 'x-test-4',
value: ':value+',
},
{
key: 'x-test-5',
value: 'something https:',
},
{
key: 'x-test-6',
value: ':hello(world)',
},
{
key: 'x-test-7',
value: 'hello(world)',
},
{
key: 'x-test-8',
value: 'hello{1,}',
},
{
key: 'x-test-9',
value: ':hello{1,2}',
},
{
key: 'content-security-policy',
value:
"default-src 'self'; img-src *; media-src media1.com media2.com; script-src userscripts.example.com/:path",
},
], ],
regex: normalizeRegEx('^\\/my-other-header(?:\\/([^\\/]+?))$'), regex: normalizeRegEx('^\\/my-other-header(?:\\/([^\\/]+?))$'),
source: '/my-other-header/:path', source: '/my-other-header/:path',
......
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