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

Add additional check for segments in destination but not source (#12067)

* Add initial test for invalid segments usage

* Add additional check for segments in destination but not source

* bump
parent 9f1e7132
import * as pathToRegexp from 'next/dist/compiled/path-to-regexp' import * as pathToRegexp from 'next/dist/compiled/path-to-regexp'
import { parse as parseUrl } from 'url'
import { import {
PERMANENT_REDIRECT_STATUS, PERMANENT_REDIRECT_STATUS,
TEMPORARY_REDIRECT_STATUS, TEMPORARY_REDIRECT_STATUS,
...@@ -72,6 +73,50 @@ function checkHeader(route: Header) { ...@@ -72,6 +73,50 @@ function checkHeader(route: Header) {
return invalidParts return invalidParts
} }
type ParseAttemptResult = {
error?: boolean
tokens?: pathToRegexp.Token[]
}
function tryParsePath(route: string, handleUrl?: boolean): ParseAttemptResult {
const result: ParseAttemptResult = {}
let routePath = route
try {
if (handleUrl) {
const parsedDestination = parseUrl(route, true)
routePath = `${parsedDestination.pathname!}${parsedDestination.hash ||
''}`
}
// Make sure we can parse the source properly
result.tokens = pathToRegexp.parse(routePath)
pathToRegexp.tokensToRegexp(result.tokens)
} catch (err) {
// If there is an error show our err.sh but still show original error or a formatted one if we can
const errMatches = err.message.match(/at (\d{0,})/)
if (errMatches) {
const position = parseInt(errMatches[1], 10)
console.error(
`\nError parsing \`${route}\` ` +
`https://err.sh/zeit/next.js/invalid-route-source\n` +
`Reason: ${err.message}\n\n` +
` ${routePath}\n` +
` ${new Array(position).fill(' ').join('')}^\n`
)
} else {
console.error(
`\nError parsing ${route} https://err.sh/zeit/next.js/invalid-route-source`,
err
)
}
result.error = true
}
return result
}
export type RouteType = 'rewrite' | 'redirect' | 'header' export type RouteType = 'rewrite' | 'redirect' | 'header'
export default function checkCustomRoutes( export default function checkCustomRoutes(
...@@ -155,31 +200,12 @@ export default function checkCustomRoutes( ...@@ -155,31 +200,12 @@ export default function checkCustomRoutes(
if (typeof route.source === 'string' && route.source.startsWith('/')) { if (typeof route.source === 'string' && route.source.startsWith('/')) {
// only show parse error if we didn't already show error // only show parse error if we didn't already show error
// for not being a string // for not being a string
try { const { tokens, error } = tryParsePath(route.source)
// Make sure we can parse the source properly
sourceTokens = pathToRegexp.parse(route.source)
pathToRegexp.tokensToRegexp(sourceTokens)
} catch (err) {
// If there is an error show our err.sh but still show original error or a formatted one if we can
const errMatches = err.message.match(/at (\d{0,})/)
if (errMatches) { if (error) {
const position = parseInt(errMatches[1], 10)
console.error(
`\nError parsing \`${route.source}\` ` +
`https://err.sh/zeit/next.js/invalid-route-source\n` +
`Reason: ${err.message}\n\n` +
` ${route.source}\n` +
` ${new Array(position).fill(' ').join('')}^\n`
)
} else {
console.error(
`\nError parsing ${route.source} https://err.sh/zeit/next.js/invalid-route-source`,
err
)
}
invalidParts.push('`source` parse failed') invalidParts.push('`source` parse failed')
} }
sourceTokens = tokens
} }
// make sure no unnamed patterns are attempted to be used in the // make sure no unnamed patterns are attempted to be used in the
...@@ -193,9 +219,9 @@ export default function checkCustomRoutes( ...@@ -193,9 +219,9 @@ export default function checkCustomRoutes(
for (const token of sourceTokens) { for (const token of sourceTokens) {
if (typeof token === 'object' && typeof token.name === 'number') { if (typeof token === 'object' && typeof token.name === 'number') {
const unnamedIndex = `:${token.name}` const unnamedIndex = new RegExp(`:${token.name}(?!\\d)`)
if ((route as Rewrite).destination.includes(unnamedIndex)) { if ((route as Rewrite).destination.match(unnamedIndex)) {
unnamedInDest.add(unnamedIndex) unnamedInDest.add(`:${token.name}`)
} }
} }
} }
...@@ -206,6 +232,39 @@ export default function checkCustomRoutes( ...@@ -206,6 +232,39 @@ export default function checkCustomRoutes(
', ' ', '
)}` )}`
) )
} else {
const {
tokens: destTokens,
error: destinationParseFailed,
} = tryParsePath((route as Rewrite).destination, true)
if (destinationParseFailed) {
invalidParts.push('`destination` parse failed')
} else {
const sourceSegments = new Set(
sourceTokens
.map(item => typeof item === 'object' && item.name)
.filter(Boolean)
)
const invalidDestSegments = new Set()
for (const token of destTokens!) {
if (
typeof token === 'object' &&
!sourceSegments.has(token.name)
) {
invalidDestSegments.add(token.name)
}
}
if (invalidDestSegments.size) {
invalidParts.push(
`\`destination\` has segments not in \`source\` (${[
...invalidDestSegments,
].join(', ')})`
)
}
}
} }
} }
} }
......
...@@ -371,6 +371,24 @@ const runTests = () => { ...@@ -371,6 +371,24 @@ const runTests = () => {
expect(stderr).toContain(`headers must return an array, received undefined`) expect(stderr).toContain(`headers must return an array, received undefined`)
}) })
it('should show valid error when segments not in source are used in destination', async () => {
await writeConfig(
[
{
source: '/feedback/:type',
destination: '/feedback/:id',
},
],
'rewrites'
)
const stderr = await getStderr()
expect(stderr).toContain(
`\`destination\` has segments not in \`source\` (id) for route {"source":"/feedback/:type","destination":"/feedback/:id"}`
)
})
} }
describe('Errors on invalid custom routes', () => { describe('Errors on invalid custom routes', () => {
......
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