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

Ensure decode error in minimal mode responds with 400 not 500 (#31037)

This ensures we handle decode failures in minimal mode as a 400 response (bad request) instead of a 500 (server error). 

## Bug

- [ ] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent b75b2f02
...@@ -425,6 +425,7 @@ export default class Server { ...@@ -425,6 +425,7 @@ export default class Server {
rewrites: combinedRewrites, rewrites: combinedRewrites,
}) })
try {
utils.handleRewrites(req, parsedUrl) utils.handleRewrites(req, parsedUrl)
// interpolate dynamic params and normalize URL if needed // interpolate dynamic params and normalize URL if needed
...@@ -432,7 +433,9 @@ export default class Server { ...@@ -432,7 +433,9 @@ export default class Server {
let params: ParsedUrlQuery | false = {} let params: ParsedUrlQuery | false = {}
Object.assign(parsedUrl.query, query) Object.assign(parsedUrl.query, query)
const paramsResult = utils.normalizeDynamicRouteParams(parsedUrl.query) const paramsResult = utils.normalizeDynamicRouteParams(
parsedUrl.query
)
if (paramsResult.hasValidParams) { if (paramsResult.hasValidParams) {
params = paramsResult.params params = paramsResult.params
...@@ -471,6 +474,13 @@ export default class Server { ...@@ -471,6 +474,13 @@ export default class Server {
Object.assign(parsedUrl.query, params) Object.assign(parsedUrl.query, params)
utils.normalizeVercelUrl(req, true) utils.normalizeVercelUrl(req, true)
} }
} catch (err) {
if (err instanceof DecodeError) {
res.statusCode = 400
return this.renderError(null, req, res, '/_error', {})
}
throw err
}
parsedUrl.pathname = `${basePath || ''}${ parsedUrl.pathname = `${basePath || ''}${
matchedPathname === '/' && basePath ? '' : matchedPathname matchedPathname === '/' && basePath ? '' : matchedPathname
......
...@@ -39,6 +39,10 @@ describe('should set-up next', () => { ...@@ -39,6 +39,10 @@ describe('should set-up next', () => {
source: '/some-catch-all/:path*', source: '/some-catch-all/:path*',
destination: '/', destination: '/',
}, },
{
source: '/to-dynamic/:path',
destination: '/dynamic/:path',
},
] ]
}, },
}, },
...@@ -553,6 +557,21 @@ describe('should set-up next', () => { ...@@ -553,6 +557,21 @@ describe('should set-up next', () => {
}) })
}) })
it('should handle bad request correctly with rewrite', async () => {
const res = await fetchViaHTTP(
appPort,
'/to-dynamic/%c0.%c0.',
'?path=%c0.%c0.',
{
headers: {
'x-matched-path': '/dynamic/[slug]',
},
}
)
expect(res.status).toBe(400)
expect(await res.text()).toContain('Bad Request')
})
it('should bubble error correctly for gip page', async () => { it('should bubble error correctly for gip page', async () => {
errors = [] errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' }) const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
......
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