Unverified Commit 3558208d authored by Justin Ridgewell's avatar Justin Ridgewell Committed by GitHub

Add `x-forwarded-*` headers to turbopack renders (#50012)

This PR forwards the [new
`ServerInfo`](https://github.com/vercel/turbo/pull/5018) struct to
Turbopack's rendering processes, allowing it to initialize the
requesting headers with the `x-forwarded-*` headers. These headers, and
`x-forwarded-proto` in particular, are necessary for [NextAuth to
work](https://github.com/nextauthjs/next-auth/blob/f62c0167/packages/next-auth/src/core/index.ts#L73-L76).

These headers are initialized in the non-turbo server via
[`http-proxy`](https://github.com/http-party/node-http-proxy/blob/9b96cd72/lib/http-proxy/passes/web-incoming.js#L58-L86).

Fixes WEB-1064
parent 702eb176
...@@ -25,7 +25,7 @@ import type { RenderOpts } from 'next/dist/server/app-render/types' ...@@ -25,7 +25,7 @@ import type { RenderOpts } from 'next/dist/server/app-render/types'
import { renderToHTMLOrFlight } from 'next/dist/server/app-render/app-render' import { renderToHTMLOrFlight } from 'next/dist/server/app-render/app-render'
import { RSC_VARY_HEADER } from 'next/dist/client/components/app-router-headers' import { RSC_VARY_HEADER } from 'next/dist/client/components/app-router-headers'
import { headersFromEntries } from '../internal/headers' import { headersFromEntries, initProxiedHeaders } from '../internal/headers'
import { parse, ParsedUrlQuery } from 'node:querystring' import { parse, ParsedUrlQuery } from 'node:querystring'
import { PassThrough } from 'node:stream' import { PassThrough } from 'node:stream'
;('TURBOPACK { transition: next-layout-entry; chunking-type: isolatedParallel }') ;('TURBOPACK { transition: next-layout-entry; chunking-type: isolatedParallel }')
...@@ -247,7 +247,10 @@ async function runOperation(renderData: RenderData) { ...@@ -247,7 +247,10 @@ async function runOperation(renderData: RenderData) {
const req: IncomingMessage = { const req: IncomingMessage = {
url: renderData.originalUrl, url: renderData.originalUrl,
method: renderData.method, method: renderData.method,
headers: headersFromEntries(renderData.rawHeaders), headers: initProxiedHeaders(
headersFromEntries(renderData.rawHeaders),
renderData.data?.serverInfo
),
} as any } as any
const res = createServerResponse(req, renderData.path) const res = createServerResponse(req, renderData.path)
......
import type { Ipc, StructuredError } from '@vercel/turbopack-node/ipc/index' import type { Ipc, StructuredError } from '@vercel/turbopack-node/ipc/index'
import type { IncomingMessage, ServerResponse } from 'node:http' import type { IncomingMessage, ServerResponse } from 'node:http'
import { Buffer } from 'node:buffer' import { Buffer } from 'node:buffer'
import { createServer, makeRequest } from '../internal/server' import { createServer, makeRequest, type ServerInfo } from '../internal/server'
import { toPairs } from '../internal/headers' import { toPairs } from '../internal/headers'
import { import {
makeResolver, makeResolver,
RouteResult, type RouteResult,
ServerAddress,
} from 'next/dist/server/lib/route-resolver' } from 'next/dist/server/lib/route-resolver'
import loadConfig from 'next/dist/server/config' import loadConfig from 'next/dist/server/config'
import { PHASE_DEVELOPMENT_SERVER } from 'next/dist/shared/lib/constants' import { PHASE_DEVELOPMENT_SERVER } from 'next/dist/shared/lib/constants'
...@@ -58,7 +57,7 @@ let resolveRouteMemo: Promise< ...@@ -58,7 +57,7 @@ let resolveRouteMemo: Promise<
async function getResolveRoute( async function getResolveRoute(
dir: string, dir: string,
serverAddr: Partial<ServerAddress> serverInfo: ServerInfo
): ReturnType< ): ReturnType<
typeof import('next/dist/server/lib/route-resolver').makeResolver typeof import('next/dist/server/lib/route-resolver').makeResolver
> { > {
...@@ -74,17 +73,17 @@ async function getResolveRoute( ...@@ -74,17 +73,17 @@ async function getResolveRoute(
matcher: middlewareConfig.matcher, matcher: middlewareConfig.matcher,
} }
return await makeResolver(dir, nextConfig, middlewareCfg, serverAddr) return await makeResolver(dir, nextConfig, middlewareCfg, serverInfo)
} }
export default async function route( export default async function route(
ipc: Ipc<RouterRequest, IpcOutgoingMessage>, ipc: Ipc<RouterRequest, IpcOutgoingMessage>,
routerRequest: RouterRequest, routerRequest: RouterRequest,
dir: string, dir: string,
serverAddr: Partial<ServerAddress> serverInfo: ServerInfo
) { ) {
const [resolveRoute, server] = await Promise.all([ const [resolveRoute, server] = await Promise.all([
(resolveRouteMemo ??= getResolveRoute(dir, serverAddr)), (resolveRouteMemo ??= getResolveRoute(dir, serverInfo)),
createServer(), createServer(),
]) ])
...@@ -99,7 +98,8 @@ export default async function route( ...@@ -99,7 +98,8 @@ export default async function route(
routerRequest.method, routerRequest.method,
routerRequest.pathname, routerRequest.pathname,
routerRequest.rawQuery, routerRequest.rawQuery,
routerRequest.rawHeaders routerRequest.rawHeaders,
serverInfo
) )
const body = Buffer.concat( const body = Buffer.concat(
......
...@@ -121,7 +121,8 @@ export default function startHandler(handler: Handler): void { ...@@ -121,7 +121,8 @@ export default function startHandler(handler: Handler): void {
renderData.method, renderData.method,
renderData.path, renderData.path,
renderData.rawQuery, renderData.rawQuery,
renderData.rawHeaders renderData.rawHeaders,
renderData.data?.serverInfo
) )
return { return {
......
import { ServerInfo } from './server'
export type Headers = Record<string, string | string[]>
/** /**
* Converts an array of raw header entries to a map of header names to values. * Converts an array of raw header entries to a map of header names to values.
*/ */
export function headersFromEntries( export function headersFromEntries(entries: Array<[string, string]>): Headers {
entries: Array<[string, string]>
): Record<string, string | string[]> {
const headers: Record<string, string | string[]> = Object.create(null) const headers: Record<string, string | string[]> = Object.create(null)
for (const [key, value] of entries) { for (const [key, value] of entries) {
if (key in headers) { if (key in headers) {
...@@ -41,3 +42,20 @@ export function toPairs<T>(arr: T[]): Array<[T, T]> { ...@@ -41,3 +42,20 @@ export function toPairs<T>(arr: T[]): Array<[T, T]> {
return pairs return pairs
} }
/**
* These headers are provided by default to match the http-proxy behavior
* https://github.com/http-party/node-http-proxy/blob/9b96cd72/lib/http-proxy/passes/web-incoming.js#L58-L86
*/
export function initProxiedHeaders(
headers: Headers,
proxiedFor: ServerInfo | null | undefined
): Headers {
const hostname = proxiedFor?.hostname || 'localhost'
const port = String(proxiedFor?.port || 3000)
headers['x-forwarded-for'] = proxiedFor?.ip || '::1'
headers['x-forwarded-host'] = `${hostname}:${port}`
headers['x-forwarded-port'] = port
headers['x-forwarded-proto'] = proxiedFor?.protocol || 'http'
return headers
}
...@@ -14,7 +14,7 @@ import { buildStaticPaths } from 'next/dist/build/utils' ...@@ -14,7 +14,7 @@ import { buildStaticPaths } from 'next/dist/build/utils'
import type { BuildManifest } from 'next/dist/server/get-page-files' import type { BuildManifest } from 'next/dist/server/get-page-files'
import type { ReactLoadableManifest } from 'next/dist/server/load-components' import type { ReactLoadableManifest } from 'next/dist/server/load-components'
import { headersFromEntries } from './headers' import { headersFromEntries, initProxiedHeaders } from './headers'
import { createServerResponse } from './http' import { createServerResponse } from './http'
import type { Ipc } from '@vercel/turbopack-node/ipc/index' import type { Ipc } from '@vercel/turbopack-node/ipc/index'
import type { RenderData } from 'types/turbopack' import type { RenderData } from 'types/turbopack'
...@@ -218,7 +218,10 @@ export default function startHandler({ ...@@ -218,7 +218,10 @@ export default function startHandler({
const req: IncomingMessage = { const req: IncomingMessage = {
url: renderData.url, url: renderData.url,
method: 'GET', method: 'GET',
headers: headersFromEntries(renderData.rawHeaders), headers: initProxiedHeaders(
headersFromEntries(renderData.rawHeaders),
renderData.data?.serverInfo
),
} as any } as any
const res: ServerResponse = createServerResponse(req, renderData.path) const res: ServerResponse = createServerResponse(req, renderData.path)
......
import type { ClientRequest, IncomingMessage, Server } from 'node:http' import type { ClientRequest, IncomingMessage, Server } from 'node:http'
import type { AddressInfo } from 'node:net' import type { AddressInfo } from 'node:net'
import http, { ServerResponse } from 'node:http' import http, { ServerResponse } from 'node:http'
import { headersFromEntries } from './headers' import { headersFromEntries, initProxiedHeaders } from './headers'
export type ServerInfo = Partial<{
hostname: string
port: number
ip: string
protocol: string
}>
/** /**
* Creates a server that listens a random port. * Creates a server that listens a random port.
...@@ -24,7 +31,8 @@ export function makeRequest( ...@@ -24,7 +31,8 @@ export function makeRequest(
method: string, method: string,
path: string, path: string,
rawQuery?: string, rawQuery?: string,
rawHeaders?: [string, string][] rawHeaders?: [string, string][],
proxiedFor?: ServerInfo
): Promise<{ ): Promise<{
clientRequest: ClientRequest clientRequest: ClientRequest
clientResponsePromise: Promise<IncomingMessage> clientResponsePromise: Promise<IncomingMessage>
...@@ -101,13 +109,16 @@ export function makeRequest( ...@@ -101,13 +109,16 @@ export function makeRequest(
const address = server.address() as AddressInfo const address = server.address() as AddressInfo
const headers = headersFromEntries(rawHeaders ?? [])
initProxiedHeaders(headers, proxiedFor)
clientRequest = http.request({ clientRequest = http.request({
host: 'localhost', host: 'localhost',
port: address.port, port: address.port,
method, method,
path: path:
rawQuery != null && rawQuery.length > 0 ? `${path}?${rawQuery}` : path, rawQuery != null && rawQuery.length > 0 ? `${path}?${rawQuery}` : path,
headers: rawHeaders != null ? headersFromEntries(rawHeaders) : undefined, headers,
}) })
// Otherwise Node.js waits for the first chunk of data to be written before sending the request. // Otherwise Node.js waits for the first chunk of data to be written before sending the request.
......
import type { ServerInfo } from '@vercel/turbopack-next/internal/server'
import type { RenderOptsPartial } from 'next/dist/server/render' import type { RenderOptsPartial } from 'next/dist/server/render'
export type RenderData = { export type RenderData = {
...@@ -10,5 +11,6 @@ export type RenderData = { ...@@ -10,5 +11,6 @@ export type RenderData = {
rawHeaders: Array<[string, string]> rawHeaders: Array<[string, string]>
data?: { data?: {
nextConfigOutput?: RenderOptsPartial['nextConfigOutput'] nextConfigOutput?: RenderOptsPartial['nextConfigOutput']
serverInfo?: ServerInfo
} }
} }
...@@ -446,7 +446,7 @@ pub async fn create_app_source( ...@@ -446,7 +446,7 @@ pub async fn create_app_source(
client_compile_time_info, client_compile_time_info,
next_config, next_config,
); );
let render_data = render_data(next_config); let render_data = render_data(next_config, server_addr);
let entrypoints = entrypoints.await?; let entrypoints = entrypoints.await?;
let mut sources: Vec<_> = entrypoints let mut sources: Vec<_> = entrypoints
......
...@@ -276,7 +276,7 @@ pub async fn create_page_source( ...@@ -276,7 +276,7 @@ pub async fn create_page_source(
next_config, next_config,
); );
let render_data = render_data(next_config); let render_data = render_data(next_config, server_addr);
let page_extensions = next_config.page_extensions(); let page_extensions = next_config.page_extensions();
let mut sources = vec![]; let mut sources = vec![];
......
...@@ -20,7 +20,7 @@ use turbopack_binding::{ ...@@ -20,7 +20,7 @@ use turbopack_binding::{
changed::any_content_changed, changed::any_content_changed,
chunk::ChunkingContext, chunk::ChunkingContext,
context::{AssetContext, AssetContextVc}, context::{AssetContext, AssetContextVc},
environment::{EnvironmentIntention::Middleware, ServerAddrVc}, environment::{EnvironmentIntention::Middleware, ServerAddrVc, ServerInfo},
ident::AssetIdentVc, ident::AssetIdentVc,
issue::IssueVc, issue::IssueVc,
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType}, reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
...@@ -388,10 +388,7 @@ async fn route_internal( ...@@ -388,10 +388,7 @@ async fn route_internal(
vec![ vec![
JsonValueVc::cell(request), JsonValueVc::cell(request),
JsonValueVc::cell(dir.to_string_lossy().into()), JsonValueVc::cell(dir.to_string_lossy().into()),
JsonValueVc::cell(json!({ JsonValueVc::cell(serde_json::to_value(ServerInfo::try_from(&*server_addr)?)?),
"hostname": server_addr.hostname(),
"port": server_addr.port(),
})),
], ],
CompletionsVc::all(vec![next_config_changed, routes_changed]), CompletionsVc::all(vec![next_config_changed, routes_changed]),
/* debug */ false, /* debug */ false,
......
...@@ -11,6 +11,7 @@ use turbopack_binding::{ ...@@ -11,6 +11,7 @@ use turbopack_binding::{
turbopack::{ turbopack::{
core::{ core::{
asset::{Asset, AssetVc}, asset::{Asset, AssetVc},
environment::{ServerAddrVc, ServerInfo},
ident::AssetIdentVc, ident::AssetIdentVc,
issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc, OptionIssueSourceVc}, issue::{Issue, IssueSeverity, IssueSeverityVc, IssueVc, OptionIssueSourceVc},
reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType}, reference_type::{EcmaScriptModulesReferenceSubType, ReferenceType},
...@@ -356,17 +357,23 @@ pub async fn load_next_json<T: DeserializeOwned>( ...@@ -356,17 +357,23 @@ pub async fn load_next_json<T: DeserializeOwned>(
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn render_data(next_config: NextConfigVc) -> Result<JsonValueVc> { pub async fn render_data(
next_config: NextConfigVc,
server_addr: ServerAddrVc,
) -> Result<JsonValueVc> {
#[derive(Serialize)] #[derive(Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
struct Data { struct Data {
next_config_output: Option<OutputType>, next_config_output: Option<OutputType>,
server_info: Option<ServerInfo>,
} }
let config = next_config.await?; let config = next_config.await?;
let server_info = ServerInfo::try_from(&*server_addr.await?);
let value = serde_json::to_value(Data { let value = serde_json::to_value(Data {
next_config_output: config.output.clone(), next_config_output: config.output.clone(),
server_info: server_info.ok(),
})?; })?;
Ok(JsonValue(value).cell()) Ok(JsonValue(value).cell())
} }
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const headers = request.headers
return NextResponse.json({
host: headers.get('x-forwarded-host'),
ip: headers.get('x-forwarded-ip'),
port: headers.get('x-forwarded-port'),
proto: headers.get('x-forwarded-proto'),
})
}
export const config = {
matcher: '/headers',
}
import { useTestHarness } from '@turbo/pack-test-harness'
export default function Foo() {
useTestHarness(runTests)
return 'index'
}
function runTests() {
it('should stream middleware response from node', async () => {
const res = await fetch('/headers')
const json = await res.json()
const { host, port, proto } = json
expect(host).toBe(`localhost:${port}`)
expect(port).toMatch(/\d+/)
expect(proto).toBe('http')
})
}
...@@ -26,8 +26,8 @@ export type MiddlewareConfig = { ...@@ -26,8 +26,8 @@ export type MiddlewareConfig = {
} }
export type ServerAddress = { export type ServerAddress = {
hostname: string hostname?: string | null
port: number port?: number | null
} }
export type RouteResult = export type RouteResult =
......
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