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.
@@ -48,11 +52,7 @@ export function createWebRequestFromNodeRequest(
4852 nodeRequest : IncomingMessage | Http2ServerRequest ,
4953 trustProxyHeaders ?: boolean | readonly string [ ] ,
5054) : Request {
51- const trustProxyHeadersNormalized =
52- trustProxyHeaders && typeof trustProxyHeaders !== 'boolean'
53- ? new Set ( trustProxyHeaders . map ( ( h ) => h . toLowerCase ( ) ) )
54- : trustProxyHeaders ;
55-
55+ const trustProxyHeadersNormalized = normalizeTrustProxyHeaders ( trustProxyHeaders ) ;
5656 const { headers, method = 'GET' } = nodeRequest ;
5757 const withBody = method !== 'GET' && method !== 'HEAD' ;
5858 const referrer = headers . referer && URL . canParse ( headers . referer ) ? headers . referer : undefined ;
@@ -70,12 +70,12 @@ export function createWebRequestFromNodeRequest(
7070 * Creates a `Headers` object from Node.js `IncomingHttpHeaders`.
7171 *
7272 * @param nodeHeaders - The Node.js `IncomingHttpHeaders` object to convert.
73- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
73+ * @param trustProxyHeaders - A set of allowed proxy headers.
7474 * @returns A `Headers` object containing the converted headers.
7575 */
7676function createRequestHeaders (
7777 nodeHeaders : IncomingHttpHeaders ,
78- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
78+ trustProxyHeaders : ReadonlySet < string > ,
7979) : Headers {
8080 const headers = new Headers ( ) ;
8181
@@ -88,6 +88,8 @@ function createRequestHeaders(
8888 name . toLowerCase ( ) . startsWith ( 'x-forwarded-' ) &&
8989 ! isProxyHeaderAllowed ( name , trustProxyHeaders )
9090 ) {
91+ // eslint-disable-next-line no-console
92+ console . warn ( `Received "${ name } " header but "trustProxyHeaders" was not set up to allow it.` ) ;
9193 continue ;
9294 }
9395
@@ -107,7 +109,7 @@ function createRequestHeaders(
107109 * Creates a `URL` object from a Node.js `IncomingMessage`, taking into account the protocol, host, and port.
108110 *
109111 * @param nodeRequest - The Node.js `IncomingMessage` or `Http2ServerRequest` object to extract URL information from.
110- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
112+ * @param trustProxyHeaders - A set of allowed proxy headers.
111113 *
112114 * @remarks
113115 * When `trustProxyHeaders` is enabled, headers such as `X-Forwarded-Host` and
@@ -118,7 +120,7 @@ function createRequestHeaders(
118120 */
119121export function createRequestUrl (
120122 nodeRequest : IncomingMessage | Http2ServerRequest ,
121- trustProxyHeaders ?: boolean | ReadonlySet < string > ,
123+ trustProxyHeaders : ReadonlySet < string > ,
122124) : URL {
123125 const {
124126 headers,
@@ -156,37 +158,15 @@ export function createRequestUrl(
156158 *
157159 * @param headers - The Node.js incoming HTTP headers.
158160 * @param headerName - The name of the proxy header to retrieve.
159- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
161+ * @param trustProxyHeaders - A set of allowed proxy headers.
160162 * @returns The value of the allowed proxy header, or `undefined` if not allowed or not present.
161163 */
162164function getAllowedProxyHeaderValue (
163165 headers : IncomingHttpHeaders ,
164166 headerName : string ,
165- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
167+ trustProxyHeaders : ReadonlySet < string > ,
166168) : string | undefined {
167169 return isProxyHeaderAllowed ( headerName , trustProxyHeaders )
168170 ? getFirstHeaderValue ( headers [ headerName ] )
169171 : undefined ;
170172}
171-
172- /**
173- * Checks if a specific proxy header is allowed.
174- *
175- * @param headerName - The name of the proxy header to check.
176- * @param trustProxyHeaders - A boolean or a set of allowed proxy headers.
177- * @returns `true` if the header is allowed, `false` otherwise.
178- */
179- function isProxyHeaderAllowed (
180- headerName : string ,
181- trustProxyHeaders : boolean | ReadonlySet < string > | undefined ,
182- ) : boolean {
183- if ( ! trustProxyHeaders ) {
184- return false ;
185- }
186-
187- if ( trustProxyHeaders === true ) {
188- return true ;
189- }
190-
191- return trustProxyHeaders . has ( headerName . toLowerCase ( ) ) ;
192- }
0 commit comments