@@ -27,16 +27,13 @@ class RedirectHandler {
2727 throw new InvalidArgumentError ( 'throwOnMaxRedirect must be a boolean' )
2828 }
2929
30- if ( opts . stripHeadersOnRedirect != null && ! Array . isArray ( opts . stripHeadersOnRedirect ) ) {
31- throw new InvalidArgumentError ( 'stripHeadersOnRedirect must be an array' )
32- }
33-
3430 this . dispatch = dispatch
3531 this . location = null
36- const { maxRedirections : _ , stripHeadersOnRedirect, ...cleanOpts } = opts
32+ const { maxRedirections : _ , stripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect , ...cleanOpts } = opts
3733 this . opts = cleanOpts // opts must be a copy, exclude maxRedirections
3834 this . opts . body = util . wrapRequestBody ( this . opts . body )
39- this . stripHeadersOnRedirect = normalizeStripHeadersOnRedirect ( stripHeadersOnRedirect )
35+ this . stripHeadersOnRedirect = normalizeStripHeaders ( stripHeadersOnRedirect , 'stripHeadersOnRedirect' )
36+ this . stripHeadersOnCrossOriginRedirect = normalizeStripHeaders ( stripHeadersOnCrossOriginRedirect , 'stripHeadersOnCrossOriginRedirect' )
4037 this . maxRedirections = maxRedirections
4138 this . handler = handler
4239 this . history = [ ]
@@ -105,7 +102,7 @@ class RedirectHandler {
105102 // Remove headers referring to the original URL.
106103 // By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
107104 // https://tools.ietf.org/html/rfc7231#section-6.4
108- this . opts . headers = cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin , this . stripHeadersOnRedirect )
105+ this . opts . headers = cleanRequestHeaders ( this . opts . headers , statusCode === 303 , this . opts . origin !== origin , this . stripHeadersOnRedirect , this . stripHeadersOnCrossOriginRedirect )
109106 this . opts . path = path
110107 this . opts . origin = origin
111108 this . opts . query = null
@@ -157,53 +154,57 @@ class RedirectHandler {
157154}
158155
159156// https://tools.ietf.org/html/rfc7231#section-6.4.4
160- function shouldRemoveHeader ( header , removeContent , unknownOrigin , stripHeaders ) {
161- if ( header . length === 4 ) {
162- return util . headerNameToString ( header ) === 'host'
157+ function shouldRemoveHeader ( header , removeContent , unknownOrigin , stripHeaders , stripHeadersOnCrossOrigin ) {
158+ const name = util . headerNameToString ( header )
159+ if ( name === 'host' ) {
160+ return true
163161 }
164- if ( stripHeaders ?. has ( util . headerNameToString ( header ) ) ) {
162+ if ( stripHeaders ?. has ( name ) || ( unknownOrigin && stripHeadersOnCrossOrigin ?. has ( name ) ) ) {
165163 return true
166164 }
167- if ( removeContent && util . headerNameToString ( header ) . startsWith ( 'content-' ) ) {
165+ if ( removeContent && name . startsWith ( 'content-' ) ) {
168166 return true
169167 }
170- if ( unknownOrigin && ( header . length === 13 || header . length === 6 || header . length === 19 ) ) {
171- const name = util . headerNameToString ( header )
168+ if ( unknownOrigin ) {
172169 return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
173170 }
174171 return false
175172}
176173
177174// https://tools.ietf.org/html/rfc7231#section-6.4
178- function normalizeStripHeadersOnRedirect ( headers ) {
175+ function normalizeStripHeaders ( headers , optionName ) {
179176 if ( headers == null ) {
180177 return null
181178 }
182179
180+ if ( ! Array . isArray ( headers ) ) {
181+ throw new InvalidArgumentError ( `${ optionName } must be an array` )
182+ }
183+
183184 const normalized = new Set ( )
184185 for ( const header of headers ) {
185186 if ( typeof header !== 'string' ) {
186- throw new InvalidArgumentError ( 'stripHeadersOnRedirect must contain header names' )
187+ throw new InvalidArgumentError ( ` ${ optionName } must contain header names` )
187188 }
188189
189190 normalized . add ( util . headerNameToString ( header ) )
190191 }
191192 return normalized
192193}
193194
194- function cleanRequestHeaders ( headers , removeContent , unknownOrigin , stripHeaders ) {
195+ function cleanRequestHeaders ( headers , removeContent , unknownOrigin , stripHeaders , stripHeadersOnCrossOrigin ) {
195196 const ret = [ ]
196197 if ( Array . isArray ( headers ) ) {
197198 for ( let i = 0 ; i < headers . length ; i += 2 ) {
198- if ( ! shouldRemoveHeader ( headers [ i ] , removeContent , unknownOrigin , stripHeaders ) ) {
199+ if ( ! shouldRemoveHeader ( headers [ i ] , removeContent , unknownOrigin , stripHeaders , stripHeadersOnCrossOrigin ) ) {
199200 ret . push ( headers [ i ] , headers [ i + 1 ] )
200201 }
201202 }
202203 } else if ( headers && typeof headers === 'object' ) {
203204 const entries = util . hasSafeIterator ( headers ) ? headers : Object . entries ( headers )
204205
205206 for ( const [ key , value ] of entries ) {
206- if ( ! shouldRemoveHeader ( key , removeContent , unknownOrigin , stripHeaders ) ) {
207+ if ( ! shouldRemoveHeader ( key , removeContent , unknownOrigin , stripHeaders , stripHeadersOnCrossOrigin ) ) {
207208 ret . push ( key , value )
208209 }
209210 }
0 commit comments