Commit b747f7b2 authored by Luis Alvarez D's avatar Luis Alvarez D Committed by JJ Kasper

Check for invalid objects in rewrites/redirects/headers (#10238)

parent f4a5b612
...@@ -95,6 +95,18 @@ export default function checkCustomRoutes( ...@@ -95,6 +95,18 @@ export default function checkCustomRoutes(
} }
for (const route of routes) { for (const route of routes) {
if (!route || typeof route !== 'object') {
console.error(
`The route ${JSON.stringify(
route
)} is not a valid object with \`source\` and \`${
type === 'header' ? 'headers' : 'destination'
}\``
)
numInvalidRoutes++
continue
}
const keys = Object.keys(route) const keys = Object.keys(route)
const invalidKeys = keys.filter(key => !allowedKeys.has(key)) const invalidKeys = keys.filter(key => !allowedKeys.has(key))
const invalidParts: string[] = [] const invalidParts: string[] = []
......
...@@ -59,6 +59,9 @@ const runTests = () => { ...@@ -59,6 +59,9 @@ const runTests = () => {
destination: '/another', destination: '/another',
permanent: 'yes', permanent: 'yes',
}, },
// invalid objects
null,
'string',
], ],
'redirects' 'redirects'
) )
...@@ -68,6 +71,10 @@ const runTests = () => { ...@@ -68,6 +71,10 @@ const runTests = () => {
`\`destination\` is missing for route {"source":"/hello","permanent":false}` `\`destination\` is missing for route {"source":"/hello","permanent":false}`
) )
expect(stderr).toContain(
`\`destination\` is missing for route {"source":"/hello","permanent":false}`
)
expect(stderr).toContain( expect(stderr).toContain(
`\`source\` is not a string for route {"source":123,"destination":"/another","permanent":false}` `\`source\` is not a string for route {"source":123,"destination":"/another","permanent":false}`
) )
...@@ -85,8 +92,21 @@ const runTests = () => { ...@@ -85,8 +92,21 @@ const runTests = () => {
) )
expect(stderr).toContain( expect(stderr).toContain(
'Valid redirect statusCode values are 301, 302, 303, 307, 308' `\`permanent\` is not set to \`true\` or \`false\` for route {"source":"/hello","destination":"/another","permanent":"yes"}`
)
expect(stderr).toContain(
`\`permanent\` is not set to \`true\` or \`false\` for route {"source":"/hello","destination":"/another","permanent":"yes"}`
)
expect(stderr).toContain(
`The route null is not a valid object with \`source\` and \`destination\``
)
expect(stderr).toContain(
`The route "string" is not a valid object with \`source\` and \`destination\``
) )
expect(stderr).toContain('Invalid redirects found') expect(stderr).toContain('Invalid redirects found')
}) })
...@@ -122,6 +142,9 @@ const runTests = () => { ...@@ -122,6 +142,9 @@ const runTests = () => {
source: '/feedback/(?!general)', source: '/feedback/(?!general)',
destination: '/feedback/general', destination: '/feedback/general',
}, },
// invalid objects
null,
'string',
], ],
'rewrites' 'rewrites'
) )
...@@ -150,6 +173,15 @@ const runTests = () => { ...@@ -150,6 +173,15 @@ const runTests = () => {
expect(stderr).toContain( expect(stderr).toContain(
`Error parsing \`/feedback/(?!general)\` https://err.sh/zeit/next.js/invalid-route-source` `Error parsing \`/feedback/(?!general)\` https://err.sh/zeit/next.js/invalid-route-source`
) )
expect(stderr).toContain(
`The route null is not a valid object with \`source\` and \`destination\``
)
expect(stderr).toContain(
`The route "string" is not a valid object with \`source\` and \`destination\``
)
expect(stderr).toContain(`Reason: Pattern cannot start with "?" at 11`) expect(stderr).toContain(`Reason: Pattern cannot start with "?" at 11`)
expect(stderr).toContain(`/feedback/(?!general)`) expect(stderr).toContain(`/feedback/(?!general)`)
...@@ -220,6 +252,9 @@ const runTests = () => { ...@@ -220,6 +252,9 @@ const runTests = () => {
}, },
], ],
}, },
// invalid objects
null,
'string',
], ],
'headers' 'headers'
) )
...@@ -245,6 +280,14 @@ const runTests = () => { ...@@ -245,6 +280,14 @@ const runTests = () => {
'invalid field: destination for route {"source":"/again","destination":"/another","headers":[{"key":"x-first","value":"idk"}]}' 'invalid field: destination for route {"source":"/again","destination":"/another","headers":[{"key":"x-first","value":"idk"}]}'
) )
expect(stderr).toContain(
`The route null is not a valid object with \`source\` and \`headers\``
)
expect(stderr).toContain(
`The route "string" is not a valid object with \`source\` and \`headers\``
)
expect(stderr).not.toContain('/valid-header') expect(stderr).not.toContain('/valid-header')
}) })
......
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