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.
@@ -46,11 +46,7 @@ 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 ;
@@ -68,12 +64,12 @@ 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.
67+ * @param trustProxyHeaders - A set of allowed proxy headers.
7268 * @returns A `Headers` object containing the converted headers.
7369 */
7470function createRequestHeaders (
7571 nodeHeaders : IncomingHttpHeaders ,
76- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
72+ trustProxyHeaders : ReadonlySet < string > ,
7773) : Headers {
7874 const headers = new Headers ( ) ;
7975
@@ -84,7 +80,7 @@ function createRequestHeaders(
8480
8581 if (
8682 name . toLowerCase ( ) . startsWith ( 'x-forwarded-' ) &&
87- ! isProxyHeaderAllowed ( name . toLowerCase ( ) , trustProxyHeaders )
83+ ! isProxyHeaderAllowed ( name , trustProxyHeaders )
8884 ) {
8985 continue ;
9086 }
@@ -105,7 +101,7 @@ function createRequestHeaders(
105101 * Creates a `URL` object from a Node.js `IncomingMessage`, taking into account the protocol, host, and port.
106102 *
107103 * @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.
104+ * @param trustProxyHeaders - A set of allowed proxy headers.
109105 *
110106 * @remarks
111107 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -116,7 +112,7 @@ function createRequestHeaders(
116112 */
117113export function createRequestUrl (
118114 nodeRequest : IncomingMessage | Http2ServerRequest ,
119- trustProxyHeaders ?: boolean | ReadonlySet < string > ,
115+ trustProxyHeaders : ReadonlySet < string > ,
120116) : URL {
121117 const {
122118 headers,
@@ -154,13 +150,13 @@ export function createRequestUrl(
154150 *
155151 * @param headers - The Node.js incoming HTTP headers.
156152 * @param headerName - The name of the proxy header to retrieve.
157- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
153+ * @param trustProxyHeaders - A set of allowed proxy headers.
158154 * @returns The value of the allowed proxy header, or `undefined` if not allowed or not present.
159155 */
160156function getAllowedProxyHeaderValue (
161157 headers : IncomingHttpHeaders ,
162158 headerName : string ,
163- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
159+ trustProxyHeaders : ReadonlySet < string > ,
164160) : string | undefined {
165161 return isProxyHeaderAllowed ( headerName , trustProxyHeaders )
166162 ? getFirstHeaderValue ( headers [ headerName ] )
@@ -171,26 +167,9 @@ function getAllowedProxyHeaderValue(
171167 * Checks if a specific proxy header is allowed.
172168 *
173169 * @param headerName - The name of the proxy header to check.
174- * @param allowedProxyHeaders - A boolean or a set of allowed proxy headers.
170+ * @param allowedProxyHeaders - A set of allowed proxy headers.
175171 * @returns `true` if the header is allowed, `false` otherwise.
176172 */
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-
173+ function isProxyHeaderAllowed ( headerName : string , trustProxyHeaders : ReadonlySet < string > ) : boolean {
195174 return trustProxyHeaders . has ( headerName . toLowerCase ( ) ) ;
196175}
0 commit comments