Unverified Commit cfab3698 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

Set correct origin for internal redirects in custom server (#96011)

https://github.com/vercel/next.js/security/advisories/GHSA-p9j2-gv94-2wf4
parent 39c4783f
......@@ -1442,5 +1442,8 @@
"1441": "DevValidationScheduler requires at least one active validation",
"1442": "The Server Reference ID did not match the expected format. Received %s.\\nRead more: https://nextjs.org/docs/messages/failed-to-find-server-action",
"1443": "Unsupported body type: %s",
"1444": "Invalid target hostname \"%s\" for proxy request, expected \"%s\""
"1444": "Invalid target hostname \"%s\" for proxy request, expected \"%s\"",
"1445": "Invariant: Missing `__NEXT_PRIVATE_ORIGIN` environment variable.",
"1446": "Missing initURL",
"1447": "Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server."
}
......@@ -227,12 +227,24 @@ async function createForwardedActionResponse(
// with the response from the forwarded worker
forwardedHeaders.set('x-action-forwarded', '1')
const proto =
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
// For standalone or the serverful mode, use the internal origin directly
// other than the host headers from the request.
const origin = process.env.__NEXT_PRIVATE_ORIGIN || `${proto}://${host.value}`
// TODO: Remove __NEXT_PRIVATE_ORIGIN
let origin: string | undefined = process.env.__NEXT_PRIVATE_ORIGIN
if (origin === undefined) {
const initUrl = getRequestMeta(req, 'initURL')
if (initUrl !== undefined) {
try {
const parsedUrl = new URL(initUrl)
origin = parsedUrl.origin
} catch (error) {
throw new Error(
'Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.',
{ cause: error }
)
}
} else {
throw new InvariantError('Missing initURL')
}
}
const fetchUrl = new URL(`${origin}${basePath}${workerPathname}`)
......@@ -385,13 +397,25 @@ async function createRedirectRenderResult(
const forwardedHeaders = getForwardedHeaders(req, res)
forwardedHeaders.set(RSC_HEADER, '1')
const proto =
getRequestMeta(req, 'initProtocol')?.replace(/:+$/, '') || 'https'
// For standalone or the serverful mode, use the internal origin directly
// other than the host headers from the request.
const origin =
process.env.__NEXT_PRIVATE_ORIGIN || `${proto}://${originalHost.value}`
// TODO: Remove __NEXT_PRIVATE_ORIGIN
let origin: string | undefined = process.env.__NEXT_PRIVATE_ORIGIN
if (origin === undefined) {
const initUrl = getRequestMeta(req, 'initURL')
if (initUrl !== undefined) {
try {
const parsedUrl = new URL(initUrl)
origin = parsedUrl.origin
} catch (error) {
throw new Error(
'Could not determine origin for forwarded Server Actions request. This can happen if port or hostname are not configured for this server.',
{ cause: error }
)
}
} else {
throw new InvariantError('Missing initURL')
}
}
const fetchUrl = new URL(
`${origin}${appRelativeRedirectUrl.pathname}${appRelativeRedirectUrl.search}`
......
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