Skip to content

Commit

Permalink
Update status code for normalize error
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Apr 29, 2022
1 parent c838b5f commit 6ceae6c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
17 changes: 11 additions & 6 deletions packages/next/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import type { ParsedUrlQuery } from 'querystring'
import type { RenderOpts, RenderOptsPartial } from './render'
import type { ResponseCacheEntry, ResponseCacheValue } from './response-cache'
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 { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
Expand Down Expand Up @@ -39,7 +44,6 @@ import {
checkIsManualRevalidate,
} from './api-utils'
import * as envConfig from '../shared/lib/runtime-config'
import { DecodeError, normalizeRepeatedSlashes } from '../shared/lib/utils'
import { isTargetLikeServerless } from './utils'
import Router from './router'
import { getPathMatch } from '../shared/lib/router/utils/path-match'
Expand Down Expand Up @@ -565,7 +569,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
])
}
} catch (err) {
if (err instanceof DecodeError) {
if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400
return this.renderError(null, req, res, '/_error', {})
}
Expand Down Expand Up @@ -613,7 +617,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
} catch (err: any) {
if (
(err && typeof err === 'object' && err.code === 'ERR_INVALID_URL') ||
err instanceof DecodeError
err instanceof DecodeError ||
err instanceof NormalizeError
) {
res.statusCode = 400
return this.renderError(null, req, res, '/_error', {})
Expand Down Expand Up @@ -986,7 +991,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
return
}
} catch (err) {
if (err instanceof DecodeError) {
if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400
return this.renderError(null, req, res, '/_error', {})
}
Expand Down Expand Up @@ -1746,7 +1751,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
if (err instanceof NoFallbackError && bubbleNoFallback) {
throw err
}
if (err instanceof DecodeError) {
if (err instanceof DecodeError || err instanceof NormalizeError) {
res.statusCode = 400
return await this.renderErrorToResponse(ctx, err)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/server/normalize-page-path.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { posix } from '../shared/lib/isomorphic/path'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { NormalizeError } from '../shared/lib/utils'

export { normalizePathSep, denormalizePagePath } from './denormalize-page-path'

Expand All @@ -17,7 +18,7 @@ export function normalizePagePath(page: string): string {
// Throw when using ../ etc in the pathname
const resolvedPage = posix.normalize(page)
if (page !== resolvedPage) {
throw new Error(
throw new NormalizeError(
`Requested and resolved page mismatch: ${page} ${resolvedPage}`
)
}
Expand Down
1 change: 1 addition & 0 deletions packages/next/shared/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ export const ST =
typeof performance.measure === 'function'

export class DecodeError extends Error {}
export class NormalizeError extends Error {}

export interface CacheFs {
readFile(f: string): Promise<string>
Expand Down
4 changes: 3 additions & 1 deletion test/integration/production/test/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ module.exports = (context) => {
]

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([400, 404].includes(res.status)).toBeTruthy()
}
})

Expand Down

0 comments on commit 6ceae6c

Please sign in to comment.