Commit 3c5f99d5 authored by JJ Kasper's avatar JJ Kasper

Validate subrequest header further

parent d36a1f3c
...@@ -41,7 +41,7 @@ import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-path ...@@ -41,7 +41,7 @@ import { getNextPathnameInfo } from '../../shared/lib/router/utils/get-next-path
import { getHostname } from '../../shared/lib/get-hostname' import { getHostname } from '../../shared/lib/get-hostname'
import { detectDomainLocale } from '../../shared/lib/i18n/detect-domain-locale' import { detectDomainLocale } from '../../shared/lib/i18n/detect-domain-locale'
import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix' import { normalizedAssetPrefix } from '../../shared/lib/normalized-asset-prefix'
import { filterInternalHeaders } from './server-ipc/utils' import { filterInternalHeaders } from './server-ipc/filter-internal-headers'
const debug = setupDebug('next:router-server:main') const debug = setupDebug('next:router-server:main')
const isNextFont = (pathname: string | null) => const isNextFont = (pathname: string | null) =>
...@@ -149,7 +149,7 @@ export async function initialize(opts: { ...@@ -149,7 +149,7 @@ export async function initialize(opts: {
renderServer.instance = renderServer.instance =
require('./render-server') as typeof import('./render-server') require('./render-server') as typeof import('./render-server')
const randomBytes = new Uint8Array(8) const randomBytes = new Uint8Array(16)
crypto.getRandomValues(randomBytes) crypto.getRandomValues(randomBytes)
const middlewareSubrequestId = Buffer.from(randomBytes).toString('hex') const middlewareSubrequestId = Buffer.from(randomBytes).toString('hex')
;(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')] = ;(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')] =
......
import crypto from 'crypto'
// These are headers that are only used internally and should
// not be honored from the external request
const INTERNAL_HEADERS = [
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-set-cookie',
'x-middleware-skip',
'x-middleware-override-headers',
'x-middleware-next',
'x-now-route-matches',
'x-matched-path',
]
export const filterInternalHeaders = (
headers: Record<string, undefined | string | string[]>
) => {
const subrequestIdHeader = headers['x-middleware-subrequest-id']
for (const header in headers) {
if (INTERNAL_HEADERS.includes(header)) {
delete headers[header]
}
// If this request didn't origin from this session we filter
// out the "x-middleware-subrequest" header so we don't skip
// middleware incorrectly
if (
header === 'x-middleware-subrequest' &&
!crypto.timingSafeEqual(
Buffer.from(
typeof subrequestIdHeader === 'string' ? subrequestIdHeader : ''
),
Buffer.from(
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')] ||
''
)
)
) {
delete headers['x-middleware-subrequest']
}
}
}
...@@ -36,37 +36,3 @@ export const filterReqHeaders = ( ...@@ -36,37 +36,3 @@ export const filterReqHeaders = (
} }
return headers as Record<string, undefined | string | string[]> return headers as Record<string, undefined | string | string[]>
} }
// These are headers that are only used internally and should
// not be honored from the external request
const INTERNAL_HEADERS = [
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-set-cookie',
'x-middleware-skip',
'x-middleware-override-headers',
'x-middleware-next',
'x-now-route-matches',
'x-matched-path',
]
export const filterInternalHeaders = (
headers: Record<string, undefined | string | string[]>
) => {
for (const header in headers) {
if (INTERNAL_HEADERS.includes(header)) {
delete headers[header]
}
// If this request didn't origin from this session we filter
// out the "x-middleware-subrequest" header so we don't skip
// middleware incorrectly
if (
header === 'x-middleware-subrequest' &&
headers['x-middleware-subrequest-id'] !==
(globalThis as any)[Symbol.for('@next/middleware-subrequest-id')]
) {
delete headers['x-middleware-subrequest']
}
}
}
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