Unverified Commit 16590f76 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Make sure routeKeys are PCRE compliant (#14987)

This adds additional checks against the routeKeys used to build the named regexes for dynamic routes to ensure they follow PCRE rules for named capture groups

x-ref: https://github.com/vercel/vercel/pull/4813
parent 731cfa46
...@@ -17,6 +17,7 @@ import { fileExists } from '../lib/file-exists' ...@@ -17,6 +17,7 @@ import { fileExists } from '../lib/file-exists'
import { findPagesDir } from '../lib/find-pages-dir' import { findPagesDir } from '../lib/find-pages-dir'
import loadCustomRoutes, { import loadCustomRoutes, {
getRedirectStatus, getRedirectStatus,
normalizeRouteRegex,
Redirect, Redirect,
RouteType, RouteType,
} from '../lib/load-custom-routes' } from '../lib/load-custom-routes'
...@@ -273,7 +274,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -273,7 +274,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
permanent: undefined, permanent: undefined,
} }
: {}), : {}),
regex: routeRegex.source, regex: normalizeRouteRegex(routeRegex.source),
} }
} }
...@@ -291,7 +292,7 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -291,7 +292,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
const routeRegex = getRouteRegex(page) const routeRegex = getRouteRegex(page)
return { return {
page, page,
regex: routeRegex.re.source, regex: normalizeRouteRegex(routeRegex.re.source),
routeKeys: routeRegex.routeKeys, routeKeys: routeRegex.routeKeys,
namedRegex: routeRegex.namedRegex, namedRegex: routeRegex.namedRegex,
} }
...@@ -611,9 +612,8 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -611,9 +612,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (isDynamicRoute(page)) { if (isDynamicRoute(page)) {
const routeRegex = getRouteRegex(dataRoute.replace(/\.json$/, '')) const routeRegex = getRouteRegex(dataRoute.replace(/\.json$/, ''))
dataRouteRegex = routeRegex.re.source.replace( dataRouteRegex = normalizeRouteRegex(
/\(\?:\\\/\)\?\$$/, routeRegex.re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.json$')
'\\.json$'
) )
namedDataRouteRegex = routeRegex.namedRegex!.replace( namedDataRouteRegex = routeRegex.namedRegex!.replace(
/\(\?:\/\)\?\$$/, /\(\?:\/\)\?\$$/,
...@@ -621,13 +621,15 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -621,13 +621,15 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
routeKeys = routeRegex.routeKeys routeKeys = routeRegex.routeKeys
} else { } else {
dataRouteRegex = new RegExp( dataRouteRegex = normalizeRouteRegex(
`^${path.posix.join( new RegExp(
'/_next/data', `^${path.posix.join(
escapeStringRegexp(buildId), '/_next/data',
`${pagePath}.json` escapeStringRegexp(buildId),
)}$` `${pagePath}.json`
).source )}$`
).source
)
} }
return { return {
...@@ -887,14 +889,17 @@ export default async function build(dir: string, conf = null): Promise<void> { ...@@ -887,14 +889,17 @@ export default async function build(dir: string, conf = null): Promise<void> {
) )
finalDynamicRoutes[tbdRoute] = { finalDynamicRoutes[tbdRoute] = {
routeRegex: getRouteRegex(tbdRoute).re.source, routeRegex: normalizeRouteRegex(getRouteRegex(tbdRoute).re.source),
dataRoute, dataRoute,
fallback: ssgFallbackPages.has(tbdRoute) fallback: ssgFallbackPages.has(tbdRoute)
? `${normalizedRoute}.html` ? `${normalizedRoute}.html`
: false, : false,
dataRouteRegex: getRouteRegex( dataRouteRegex: normalizeRouteRegex(
dataRoute.replace(/\.json$/, '') getRouteRegex(dataRoute.replace(/\.json$/, '')).re.source.replace(
).re.source.replace(/\(\?:\\\/\)\?\$$/, '\\.json$'), /\(\?:\\\/\)\?\$$/,
'\\.json$'
)
),
} }
}) })
const prerenderManifest: PrerenderManifest = { const prerenderManifest: PrerenderManifest = {
......
...@@ -29,6 +29,11 @@ export function getRedirectStatus(route: Redirect): number { ...@@ -29,6 +29,11 @@ export function getRedirectStatus(route: Redirect): number {
) )
} }
export function normalizeRouteRegex(regex: string) {
// clean up un-necessary escaping from regex.source which turns / into \\/
return regex.replace(/\\\//g, '/')
}
function checkRedirect( function checkRedirect(
route: Redirect route: Redirect
): { invalidParts: string[]; hadInvalidStatus: boolean } { ): { invalidParts: string[]; hadInvalidStatus: boolean } {
......
...@@ -51,6 +51,25 @@ export function getRouteRegex( ...@@ -51,6 +51,25 @@ export function getRouteRegex(
// dead code eliminate for browser since it's only needed // dead code eliminate for browser since it's only needed
// while generating routes-manifest // while generating routes-manifest
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
let routeKeyCharCode = 97
let routeKeyCharLength = 1
// builds a minimal routeKey using only a-z and minimal number of characters
const getSafeRouteKey = () => {
let routeKey = ''
for (let i = 0; i < routeKeyCharLength; i++) {
routeKey += String.fromCharCode(routeKeyCharCode)
routeKeyCharCode++
if (routeKeyCharCode > 122) {
routeKeyCharLength++
routeKeyCharCode = 97
}
}
return routeKey
}
const routeKeys: { [named: string]: string } = {} const routeKeys: { [named: string]: string } = {}
let namedParameterizedRoute = segments let namedParameterizedRoute = segments
...@@ -59,7 +78,22 @@ export function getRouteRegex( ...@@ -59,7 +78,22 @@ export function getRouteRegex(
const { key, optional, repeat } = parseParameter(segment.slice(1, -1)) const { key, optional, repeat } = parseParameter(segment.slice(1, -1))
// replace any non-word characters since they can break // replace any non-word characters since they can break
// the named regex // the named regex
const cleanedKey = key.replace(/\W/g, '') let cleanedKey = key.replace(/\W/g, '')
let invalidKey = false
// check if the key is still invalid and fallback to using a known
// safe key
if (cleanedKey.length === 0 || cleanedKey.length > 30) {
invalidKey = true
}
if (!isNaN(parseInt(cleanedKey.substr(0, 1)))) {
invalidKey = true
}
if (invalidKey) {
cleanedKey = getSafeRouteKey()
}
routeKeys[cleanedKey] = key routeKeys[cleanedKey] = key
return repeat return repeat
? optional ? optional
......
export const getServerSideProps = ({ params }) => {
console.log({ params })
return {
props: {
params,
},
}
}
export default function Page(props) {
return <p id="props">{JSON.stringify(props)}</p>
}
export const getServerSideProps = ({ params }) => {
console.log({ params })
return {
props: {
params,
},
}
}
export default function Page(props) {
return <p id="props">{JSON.stringify(props)}</p>
}
...@@ -552,6 +552,30 @@ function runTests(dev) { ...@@ -552,6 +552,30 @@ function runTests(dev) {
rewrites: [], rewrites: [],
redirects: expect.arrayContaining([]), redirects: expect.arrayContaining([]),
dataRoutes: [ dataRoutes: [
{
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/b\\/([^\\/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/b/(?<a>[^/]+?)\\.json$`,
page: '/b/[123]',
routeKeys: {
a: '123',
},
},
{
dataRouteRegex: `^\\/_next\\/data\\/${escapeRegex(
buildId
)}\\/c\\/([^\\/]+?)\\.json$`,
namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId
)}/c/(?<a>[^/]+?)\\.json$`,
page: '/c/[alongparamnameshouldbeallowedeventhoughweird]',
routeKeys: {
a: 'alongparamnameshouldbeallowedeventhoughweird',
},
},
{ {
namedDataRouteRegex: `^/_next/data/${escapeRegex( namedDataRouteRegex: `^/_next/data/${escapeRegex(
buildId buildId
...@@ -596,6 +620,14 @@ function runTests(dev) { ...@@ -596,6 +620,14 @@ function runTests(dev) {
}, },
], ],
dynamicRoutes: [ dynamicRoutes: [
{
namedRegex: '^/b/(?<a>[^/]+?)(?:/)?$',
page: '/b/[123]',
regex: normalizeRegEx('^\\/b\\/([^\\/]+?)(?:\\/)?$'),
routeKeys: {
a: '123',
},
},
{ {
namedRegex: `^/blog/(?<name>[^/]+?)/comment/(?<id>[^/]+?)(?:/)?$`, namedRegex: `^/blog/(?<name>[^/]+?)/comment/(?<id>[^/]+?)(?:/)?$`,
page: '/blog/[name]/comment/[id]', page: '/blog/[name]/comment/[id]',
...@@ -607,6 +639,14 @@ function runTests(dev) { ...@@ -607,6 +639,14 @@ function runTests(dev) {
id: 'id', id: 'id',
}, },
}, },
{
namedRegex: '^/c/(?<a>[^/]+?)(?:/)?$',
page: '/c/[alongparamnameshouldbeallowedeventhoughweird]',
regex: normalizeRegEx('^\\/c\\/([^\\/]+?)(?:\\/)?$'),
routeKeys: {
a: 'alongparamnameshouldbeallowedeventhoughweird',
},
},
{ {
namedRegex: '^/catchall\\-dash/(?<helloworld>.+?)(?:/)?$', namedRegex: '^/catchall\\-dash/(?<helloworld>.+?)(?:/)?$',
page: '/catchall-dash/[...hello-world]', page: '/catchall-dash/[...hello-world]',
......
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