88
99import type { IncomingHttpHeaders , IncomingMessage } from 'node:http' ;
1010import type { Http2ServerRequest } from 'node:http2' ;
11- import { getFirstHeaderValue } from '../../src/utils/validation' ;
11+ import {
12+ getFirstHeaderValue ,
13+ isProxyHeaderAllowed ,
14+ normalizeTrustProxyHeaders ,
15+ } from '../../src/utils/validation' ;
1216
1317/**
1418 * A set containing all the pseudo-headers defined in the HTTP/2 specification.
@@ -33,7 +37,7 @@ const HTTP2_PSEUDO_HEADERS: ReadonlySet<string> = new Set([
3337 * be used by web platform APIs.
3438 *
3539 * @param nodeRequest - The Node.js request object (`IncomingMessage` or `Http2ServerRequest`) to convert.
36- * @param trustProxyHeaders - A boolean or an array of allowed proxy headers.
40+ * @param trustProxyHeaders - A boolean or an array of proxy headers to trust when constructing the request URL .
3741 *
3842 * @remarks
3943 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -46,18 +50,14 @@ export function createWebRequestFromNodeRequest(
4650 nodeRequest : IncomingMessage | Http2ServerRequest ,
4751 trustProxyHeaders ?: boolean | readonly string [ ] ,
4852) : Request {
49- const trustProxyHeadersNormalized =
50- trustProxyHeaders && typeof trustProxyHeaders !== 'boolean'
51- ? new Set ( trustProxyHeaders . map ( ( h ) => h . toLowerCase ( ) ) )
52- : trustProxyHeaders ;
53-
53+ const trustProxyHeadersNormalized = normalizeTrustProxyHeaders ( trustProxyHeaders ) ;
5454 const { headers, method = 'GET' } = nodeRequest ;
5555 const withBody = method !== 'GET' && method !== 'HEAD' ;
5656 const referrer = headers . referer && URL . canParse ( headers . referer ) ? headers . referer : undefined ;
5757
5858 return new Request ( createRequestUrl ( nodeRequest , trustProxyHeadersNormalized ) , {
5959 method,
60- headers : createRequestHeaders ( headers , trustProxyHeadersNormalized ) ,
60+ headers : createRequestHeaders ( headers ) ,
6161 body : withBody ? nodeRequest : undefined ,
6262 duplex : withBody ? 'half' : undefined ,
6363 referrer,
@@ -68,27 +68,16 @@ export function createWebRequestFromNodeRequest(
6868 * Creates a `Headers` object from Node.js `IncomingHttpHeaders`.
6969 *
7070 * @param nodeHeaders - The Node.js `IncomingHttpHeaders` object to convert.
71- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
7271 * @returns A `Headers` object containing the converted headers.
7372 */
74- function createRequestHeaders (
75- nodeHeaders : IncomingHttpHeaders ,
76- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
77- ) : Headers {
73+ function createRequestHeaders ( nodeHeaders : IncomingHttpHeaders ) : Headers {
7874 const headers = new Headers ( ) ;
7975
8076 for ( const [ name , value ] of Object . entries ( nodeHeaders ) ) {
8177 if ( HTTP2_PSEUDO_HEADERS . has ( name ) ) {
8278 continue ;
8379 }
8480
85- if (
86- name . toLowerCase ( ) . startsWith ( 'x-forwarded-' ) &&
87- ! isProxyHeaderAllowed ( name . toLowerCase ( ) , trustProxyHeaders )
88- ) {
89- continue ;
90- }
91-
9281 if ( typeof value === 'string' ) {
9382 headers . append ( name , value ) ;
9483 } else if ( Array . isArray ( value ) ) {
@@ -105,7 +94,7 @@ function createRequestHeaders(
10594 * Creates a `URL` object from a Node.js `IncomingMessage`, taking into account the protocol, host, and port.
10695 *
10796 * @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.
97+ * @param trustProxyHeaders - A set of allowed proxy headers.
10998 *
11099 * @remarks
111100 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -116,7 +105,7 @@ function createRequestHeaders(
116105 */
117106export function createRequestUrl (
118107 nodeRequest : IncomingMessage | Http2ServerRequest ,
119- trustProxyHeaders ?: boolean | ReadonlySet < string > ,
108+ trustProxyHeaders : ReadonlySet < string > ,
120109) : URL {
121110 const {
122111 headers,
@@ -154,43 +143,15 @@ export function createRequestUrl(
154143 *
155144 * @param headers - The Node.js incoming HTTP headers.
156145 * @param headerName - The name of the proxy header to retrieve.
157- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
146+ * @param trustProxyHeaders - A set of allowed proxy headers.
158147 * @returns The value of the allowed proxy header, or `undefined` if not allowed or not present.
159148 */
160149function getAllowedProxyHeaderValue (
161150 headers : IncomingHttpHeaders ,
162151 headerName : string ,
163- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
152+ trustProxyHeaders : ReadonlySet < string > ,
164153) : string | undefined {
165154 return isProxyHeaderAllowed ( headerName , trustProxyHeaders )
166155 ? getFirstHeaderValue ( headers [ headerName ] )
167156 : undefined ;
168157}
169-
170- /**
171- * Checks if a specific proxy header is allowed.
172- *
173- * @param headerName - The name of the proxy header to check.
174- * @param allowedProxyHeaders - A boolean or a set of allowed proxy headers.
175- * @returns `true` if the header is allowed, `false` otherwise.
176- */
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-
195- return trustProxyHeaders . has ( headerName . toLowerCase ( ) ) ;
196- }
0 commit comments