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,25 +479,40 @@ export default class Server {
if (!value.includes(':')) {
return value
}
const { parsedDestination } = prepareDestination(
value,
params,
{},
false,
''
)
if (
!parsedDestination.pathname ||
!parsedDestination.pathname.startsWith('/')
) {
// the value needs to start with a forward-slash to be compiled
// correctly
return compilePathToRegex(`/${value}`, { validate: false })(
params
).substr(1)
for (const key of Object.keys(params)) {
if (value.includes(`:${key}`)) {
value = value
.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}`
)
}
}
return formatUrl(parsedDestination)
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, '*')
// the value needs to start with a forward-slash to be compiled
// correctly
return compilePathToRegex(`/${value}`, { validate: false })(
params
).substr(1)
}
// Headers come very first
......
......@@ -243,6 +243,47 @@ module.exports = {
key: 'some:path',
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) => {
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 () => {
const res = await fetchViaHTTP(appPort, '/blog/post-321')
expect(res.headers.get('x-something')).toBe('applied-everywhere')
......@@ -348,7 +366,7 @@ const runTests = (isDev = false) => {
it('should apply params header key/values with URL that has port', async () => {
const res = await fetchViaHTTP(appPort, '/with-params/url2/first')
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) => {
key: 'some:path',
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(?:\\/([^\\/]+?))$'),
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