Skip to content

Commit

Permalink
Check for invalid objects in rewrites/redirects/headers (#10238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Alvarez D authored and ijjk committed Jan 23, 2020
1 parent f4a5b61 commit b747f7b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/next/lib/check-custom-routes.ts
Expand Up @@ -95,6 +95,18 @@ export default function checkCustomRoutes(
}

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 invalidKeys = keys.filter(key => !allowedKeys.has(key))
const invalidParts: string[] = []
Expand Down
45 changes: 44 additions & 1 deletion test/integration/invalid-custom-routes/test/index.test.js
Expand Up @@ -59,6 +59,9 @@ const runTests = () => {
destination: '/another',
permanent: 'yes',
},
// invalid objects
null,
'string',
],
'redirects'
)
Expand All @@ -68,6 +71,10 @@ const runTests = () => {
`\`destination\` is missing for route {"source":"/hello","permanent":false}`
)

expect(stderr).toContain(
`\`destination\` is missing for route {"source":"/hello","permanent":false}`
)

expect(stderr).toContain(
`\`source\` is not a string for route {"source":123,"destination":"/another","permanent":false}`
)
Expand All @@ -85,8 +92,21 @@ const runTests = () => {
)

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')
})

Expand Down Expand Up @@ -122,6 +142,9 @@ const runTests = () => {
source: '/feedback/(?!general)',
destination: '/feedback/general',
},
// invalid objects
null,
'string',
],
'rewrites'
)
Expand Down Expand Up @@ -150,6 +173,15 @@ const runTests = () => {
expect(stderr).toContain(
`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(`/feedback/(?!general)`)

Expand Down Expand Up @@ -220,6 +252,9 @@ const runTests = () => {
},
],
},
// invalid objects
null,
'string',
],
'headers'
)
Expand All @@ -245,6 +280,14 @@ const runTests = () => {
'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')
})

Expand Down

0 comments on commit b747f7b

Please sign in to comment.