Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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