@@ -33,7 +33,7 @@ import ResponseOrig from '../src/response.js';
33
33
import Body , { getTotalBytes , extractContentType } from '../src/body.js' ;
34
34
import TestServer from './utils/server.js' ;
35
35
import chaiTimeout from './utils/chai-timeout.js' ;
36
- import { isDomainOrSubdomain } from '../src/utils/is.js' ;
36
+ import { isDomainOrSubdomain , isSameProtocol } from '../src/utils/is.js' ;
37
37
38
38
const AbortControllerPolyfill = abortControllerPolyfill . AbortController ;
39
39
const encoder = new TextEncoder ( ) ;
@@ -522,7 +522,7 @@ describe('node-fetch', () => {
522
522
expect ( res . url ) . to . equal ( `${ base } inspect` ) ;
523
523
expect ( headers . get ( 'other-safe-headers' ) ) . to . equal ( 'stays' ) ;
524
524
expect ( headers . get ( 'x-foo' ) ) . to . equal ( 'bar' ) ;
525
- // Unsafe headers should not have been sent to httpbin
525
+ // Unsafe headers are not removed
526
526
expect ( headers . get ( 'cookie' ) ) . to . equal ( 'is=cookie' ) ;
527
527
expect ( headers . get ( 'cookie2' ) ) . to . equal ( 'is=cookie2' ) ;
528
528
expect ( headers . get ( 'www-authenticate' ) ) . to . equal ( 'is=www-authenticate' ) ;
@@ -542,6 +542,39 @@ describe('node-fetch', () => {
542
542
expect ( isDomainOrSubdomain ( 'http://bob.uk.com' , 'http://xyz.uk.com' ) ) . to . be . false ;
543
543
} ) ;
544
544
545
+ it ( 'should not forward secure headers to changed protocol' , async ( ) => {
546
+ const res = await fetch ( 'https://httpbin.org/redirect-to?url=http%3A%2F%2Fhttpbin.org%2Fget&status_code=302' , {
547
+ headers : new Headers ( {
548
+ cookie : 'gets=removed' ,
549
+ cookie2 : 'gets=removed' ,
550
+ authorization : 'gets=removed' ,
551
+ 'www-authenticate' : 'gets=removed' ,
552
+ 'other-safe-headers' : 'stays' ,
553
+ 'x-foo' : 'bar'
554
+ } )
555
+ } ) ;
556
+
557
+ const headers = new Headers ( ( await res . json ( ) ) . headers ) ;
558
+ // Safe headers are not removed
559
+ expect ( headers . get ( 'other-safe-headers' ) ) . to . equal ( 'stays' ) ;
560
+ expect ( headers . get ( 'x-foo' ) ) . to . equal ( 'bar' ) ;
561
+ // Unsafe headers should not have been sent to downgraded http
562
+ expect ( headers . get ( 'cookie' ) ) . to . equal ( null ) ;
563
+ expect ( headers . get ( 'cookie2' ) ) . to . equal ( null ) ;
564
+ expect ( headers . get ( 'www-authenticate' ) ) . to . equal ( null ) ;
565
+ expect ( headers . get ( 'authorization' ) ) . to . equal ( null ) ;
566
+ } ) ;
567
+
568
+ it ( 'isSameProtocol' , ( ) => {
569
+ // Forwarding headers to same protocol is OK
570
+ expect ( isSameProtocol ( 'http://a.com' , 'http://a.com' ) ) . to . be . true ;
571
+ expect ( isSameProtocol ( 'https://a.com' , 'https://www.a.com' ) ) . to . be . true ;
572
+
573
+ // Forwarding headers to diff protocol is not OK
574
+ expect ( isSameProtocol ( 'http://b.com' , 'https://b.com' ) ) . to . be . false ;
575
+ expect ( isSameProtocol ( 'http://www.a.com' , 'https://a.com' ) ) . to . be . false ;
576
+ } ) ;
577
+
545
578
it ( 'should treat broken redirect as ordinary response (follow)' , async ( ) => {
546
579
const url = `${ base } redirect/no-location` ;
547
580
const res = await fetch ( url ) ;
0 commit comments