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

Update status code for normalize error (#36580)

This updates to show a 400 (bad request) when an invalid path is sent to Next.js similar to our decode failure handling. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Closes: https://github.com/vercel/next.js/issues/36555Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 7a09f88d
...@@ -11,7 +11,12 @@ import type { ParsedUrlQuery } from 'querystring' ...@@ -11,7 +11,12 @@ import type { ParsedUrlQuery } from 'querystring'
import type { RenderOpts, RenderOptsPartial } from './render' import type { RenderOpts, RenderOptsPartial } from './render'
import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache' import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache'
import type { UrlWithParsedQuery } from 'url' import type { UrlWithParsedQuery } from 'url'
import type { CacheFs } from '../shared/lib/utils' import {
CacheFs,
NormalizeError,
DecodeError,
normalizeRepeatedSlashes,
} from '../shared/lib/utils'
import type { PreviewData } from 'next/types' import type { PreviewData } from 'next/types'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin' import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http' import type { BaseNextRequest, BaseNextResponse } from './base-http'
...@@ -39,7 +44,6 @@ import { ...@@ -39,7 +44,6 @@ import {
checkIsManualRevalidate, checkIsManualRevalidate,
} from './api-utils' } from './api-utils'
import * as envConfig from '../shared/lib/runtime-config' import * as envConfig from '../shared/lib/runtime-config'
import { DecodeError, normalizeRepeatedSlashes } from '../shared/lib/utils'
import { isTargetLikeServerless } from './utils' import { isTargetLikeServerless } from './utils'
import Router from './router' import Router from './router'
import { getPathMatch } from '../shared/lib/router/utils/path-match' import { getPathMatch } from '../shared/lib/router/utils/path-match'
...@@ -564,7 +568,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -564,7 +568,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
]) ])
} }
} catch (err) { } catch (err) {
if (err instanceof DecodeError) { if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400 res.statusCode = 400
return this.renderError(null, req, res, '/_error', {}) return this.renderError(null, req, res, '/_error', {})
} }
...@@ -612,7 +616,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -612,7 +616,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} catch (err: any) { } catch (err: any) {
if ( if (
(err && typeof err === 'object' && err.code === 'ERR_INVALID_URL') || (err && typeof err === 'object' && err.code === 'ERR_INVALID_URL') ||
err instanceof DecodeError err instanceof DecodeError ||
err instanceof NormalizeError
) { ) {
res.statusCode = 400 res.statusCode = 400
return this.renderError(null, req, res, '/_error', {}) return this.renderError(null, req, res, '/_error', {})
...@@ -985,7 +990,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -985,7 +990,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return return
} }
} catch (err) { } catch (err) {
if (err instanceof DecodeError) { if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400 res.statusCode = 400
return this.renderError(null, req, res, '/_error', {}) return this.renderError(null, req, res, '/_error', {})
} }
...@@ -1745,7 +1750,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1745,7 +1750,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (err instanceof NoFallbackError && bubbleNoFallback) { if (err instanceof NoFallbackError && bubbleNoFallback) {
throw err throw err
} }
if (err instanceof DecodeError) { if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400 res.statusCode = 400
return await this.renderErrorToResponse(ctx, err) return await this.renderErrorToResponse(ctx, err)
} }
......
import { ensureLeadingSlash } from './ensure-leading-slash' import { ensureLeadingSlash } from './ensure-leading-slash'
import { isDynamicRoute } from '../router/utils' import { isDynamicRoute } from '../router/utils'
import { posix } from '../isomorphic/path' import { posix } from '../isomorphic/path'
import { NormalizeError } from '../utils'
/** /**
* Takes a page and transforms it into its file counterpart ensuring that the * Takes a page and transforms it into its file counterpart ensuring that the
...@@ -22,7 +23,7 @@ export function normalizePagePath(page: string): string { ...@@ -22,7 +23,7 @@ export function normalizePagePath(page: string): string {
const resolvedPage = posix.normalize(normalized) const resolvedPage = posix.normalize(normalized)
if (resolvedPage !== normalized) { if (resolvedPage !== normalized) {
throw new Error( throw new NormalizeError(
`Requested and resolved page mismatch: ${normalized} ${resolvedPage}` `Requested and resolved page mismatch: ${normalized} ${resolvedPage}`
) )
} }
......
...@@ -397,6 +397,7 @@ export const ST = ...@@ -397,6 +397,7 @@ export const ST =
typeof performance.measure === 'function' typeof performance.measure === 'function'
export class DecodeError extends Error {} export class DecodeError extends Error {}
export class NormalizeError extends Error {}
export interface CacheFs { export interface CacheFs {
readFile(f: string): Promise<string> readFile(f: string): Promise<string>
......
...@@ -5,12 +5,7 @@ import { readFileSync } from 'fs' ...@@ -5,12 +5,7 @@ import { readFileSync } from 'fs'
import http from 'http' import http from 'http'
import url from 'url' import url from 'url'
import { join } from 'path' import { join } from 'path'
import { import { getBrowserBodyText, waitFor, fetchViaHTTP } from 'next-test-utils'
renderViaHTTP,
getBrowserBodyText,
waitFor,
fetchViaHTTP,
} from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { homedir } from 'os' import { homedir } from 'os'
...@@ -73,8 +68,10 @@ module.exports = (context) => { ...@@ -73,8 +68,10 @@ module.exports = (context) => {
] ]
for (const path of pathsToCheck) { for (const path of pathsToCheck) {
const data = await renderViaHTTP(context.appPort, path) const res = await fetchViaHTTP(context.appPort, path)
const data = await res.text()
expect(data.includes('cool-version')).toBeFalsy() expect(data.includes('cool-version')).toBeFalsy()
expect([400, 404].includes(res.status)).toBeTruthy()
} }
}) })
......
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