88
99import type { IncomingHttpHeaders , IncomingMessage } from 'node:http' ;
1010import type { Http2ServerRequest } from 'node:http2' ;
11- import { getFirstHeaderValue } from '../../src/utils/validation' ;
11+ import { getFirstHeaderValue , normalizeTrustProxyHeaders } from '../../src/utils/validation' ;
1212
1313/**
1414 * A set containing all the pseudo-headers defined in the HTTP/2 specification.
@@ -33,7 +33,7 @@ const HTTP2_PSEUDO_HEADERS: ReadonlySet<string> = new Set([
3333 * be used by web platform APIs.
3434 *
3535 * @param nodeRequest - The Node.js request object (`IncomingMessage` or `Http2ServerRequest`) to convert.
36- * @param trustProxyHeaders - A boolean or an array of allowed proxy headers.
36+ * @param trustProxyHeaders - A boolean or an array of proxy headers to trust when constructing the request URL .
3737 *
3838 * @remarks
3939 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -46,18 +46,14 @@ export function createWebRequestFromNodeRequest(
4646 nodeRequest : IncomingMessage | Http2ServerRequest ,
4747 trustProxyHeaders ?: boolean | readonly string [ ] ,
4848) : Request {
49- const trustProxyHeadersNormalized =
50- trustProxyHeaders && typeof trustProxyHeaders !== 'boolean'
51- ? new Set ( trustProxyHeaders . map ( ( h ) => h . toLowerCase ( ) ) )
52- : trustProxyHeaders ;
53-
49+ const trustProxyHeadersNormalized = normalizeTrustProxyHeaders ( trustProxyHeaders ) ;
5450 const { headers, method = 'GET' } = nodeRequest ;
5551 const withBody = method !== 'GET' && method !== 'HEAD' ;
5652 const referrer = headers . referer && URL . canParse ( headers . referer ) ? headers . referer : undefined ;
5753
5854 return new Request ( createRequestUrl ( nodeRequest , trustProxyHeadersNormalized ) , {
5955 method,
60- headers : createRequestHeaders ( headers , trustProxyHeadersNormalized ) ,
56+ headers : createRequestHeaders ( headers ) ,
6157 body : withBody ? nodeRequest : undefined ,
6258 duplex : withBody ? 'half' : undefined ,
6359 referrer,
@@ -68,27 +64,16 @@ export function createWebRequestFromNodeRequest(
6864 * Creates a `Headers` object from Node.js `IncomingHttpHeaders`.
6965 *
7066 * @param nodeHeaders - The Node.js `IncomingHttpHeaders` object to convert.
71- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
7267 * @returns A `Headers` object containing the converted headers.
7368 */
74- function createRequestHeaders (
75- nodeHeaders : IncomingHttpHeaders ,
76- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
77- ) : Headers {
69+ function createRequestHeaders ( nodeHeaders : IncomingHttpHeaders ) : Headers {
7870 const headers = new Headers ( ) ;
7971
8072 for ( const [ name , value ] of Object . entries ( nodeHeaders ) ) {
8173 if ( HTTP2_PSEUDO_HEADERS . has ( name ) ) {
8274 continue ;
8375 }
8476
85- if (
86- name . toLowerCase ( ) . startsWith ( 'x-forwarded-' ) &&
87- ! isProxyHeaderAllowed ( name . toLowerCase ( ) , trustProxyHeaders )
88- ) {
89- continue ;
90- }
91-
9277 if ( typeof value === 'string' ) {
9378 headers . append ( name , value ) ;
9479 } else if ( Array . isArray ( value ) ) {
@@ -105,7 +90,7 @@ function createRequestHeaders(
10590 * Creates a `URL` object from a Node.js `IncomingMessage`, taking into account the protocol, host, and port.
10691 *
10792 * @param nodeRequest - The Node.js `IncomingMessage` or `Http2ServerRequest` object to extract URL information from.
108- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
93+ * @param trustProxyHeaders - A set of allowed proxy headers.
10994 *
11095 * @remarks
11196 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -116,7 +101,7 @@ function createRequestHeaders(
116101 */
117102export function createRequestUrl (
118103 nodeRequest : IncomingMessage | Http2ServerRequest ,
119- trustProxyHeaders ?: boolean | ReadonlySet < string > ,
104+ trustProxyHeaders : ReadonlySet < string > ,
120105) : URL {
121106 const {
122107 headers,
@@ -154,13 +139,13 @@ export function createRequestUrl(
154139 *
155140 * @param headers - The Node.js incoming HTTP headers.
156141 * @param headerName - The name of the proxy header to retrieve.
157- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
142+ * @param trustProxyHeaders - A set of allowed proxy headers.
158143 * @returns The value of the allowed proxy header, or `undefined` if not allowed or not present.
159144 */
160145function getAllowedProxyHeaderValue (
161146 headers : IncomingHttpHeaders ,
162147 headerName : string ,
163- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
148+ trustProxyHeaders : ReadonlySet < string > ,
164149) : string | undefined {
165150 return isProxyHeaderAllowed ( headerName , trustProxyHeaders )
166151 ? getFirstHeaderValue ( headers [ headerName ] )
@@ -171,26 +156,9 @@ function getAllowedProxyHeaderValue(
171156 * Checks if a specific proxy header is allowed.
172157 *
173158 * @param headerName - The name of the proxy header to check.
174- * @param allowedProxyHeaders - A boolean or a set of allowed proxy headers.
159+ * @param allowedProxyHeaders - A set of allowed proxy headers.
175160 * @returns `true` if the header is allowed, `false` otherwise.
176161 */
177- function isProxyHeaderAllowed (
178- headerName : string ,
179- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
180- ) : boolean {
181- if ( trustProxyHeaders === undefined ) {
182- const lower = headerName . toLowerCase ( ) ;
183-
184- return lower === 'x-forwarded-host' || lower === 'x-forwarded-proto' ;
185- }
186-
187- if ( trustProxyHeaders === false ) {
188- return false ;
189- }
190-
191- if ( trustProxyHeaders === true ) {
192- return true ;
193- }
194-
162+ function isProxyHeaderAllowed ( headerName : string , trustProxyHeaders : ReadonlySet < string > ) : boolean {
195163 return trustProxyHeaders . has ( headerName . toLowerCase ( ) ) ;
196164}
0 commit comments