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,51 +425,61 @@ export default class Server { ...@@ -425,51 +425,61 @@ export default class Server {
rewrites: combinedRewrites, rewrites: combinedRewrites,
}) })
utils.handleRewrites(req, parsedUrl) try {
utils.handleRewrites(req, parsedUrl)
// interpolate dynamic params and normalize URL if needed
if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {}
Object.assign(parsedUrl.query, query) // interpolate dynamic params and normalize URL if needed
const paramsResult = utils.normalizeDynamicRouteParams(parsedUrl.query) if (pageIsDynamic) {
let params: ParsedUrlQuery | false = {}
if (paramsResult.hasValidParams) { Object.assign(parsedUrl.query, query)
params = paramsResult.params const paramsResult = utils.normalizeDynamicRouteParams(
} else if (req.headers['x-now-route-matches']) { parsedUrl.query
const opts: Record<string, string> = {}
params = utils.getParamsFromRouteMatches(
req,
opts,
(parsedUrl.query.__nextLocale as string | undefined) || ''
) )
if (opts.locale) { if (paramsResult.hasValidParams) {
parsedUrl.query.__nextLocale = opts.locale params = paramsResult.params
} else if (req.headers['x-now-route-matches']) {
const opts: Record<string, string> = {}
params = utils.getParamsFromRouteMatches(
req,
opts,
(parsedUrl.query.__nextLocale as string | undefined) || ''
)
if (opts.locale) {
parsedUrl.query.__nextLocale = opts.locale
}
} else {
params = utils.dynamicRouteMatcher!(matchedPathnameNoExt)
} }
} else {
params = utils.dynamicRouteMatcher!(matchedPathnameNoExt)
}
if (params) { if (params) {
params = utils.normalizeDynamicRouteParams(params).params params = utils.normalizeDynamicRouteParams(params).params
matchedPathname = utils.interpolateDynamicPath( matchedPathname = utils.interpolateDynamicPath(
matchedPathname, matchedPathname,
params params
) )
req.url = utils.interpolateDynamicPath(req.url!, params) req.url = utils.interpolateDynamicPath(req.url!, params)
} }
if (reqUrlIsDataUrl && matchedPathIsDataUrl) { if (reqUrlIsDataUrl && matchedPathIsDataUrl) {
req.url = formatUrl({ req.url = formatUrl({
...parsedPath, ...parsedPath,
pathname: matchedPathname, pathname: matchedPathname,
}) })
} }
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 || ''}${
......
...@@ -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