@@ -33,7 +33,7 @@ import ResponseOrig from '../src/response.js';
3333import Body , { getTotalBytes , extractContentType } from '../src/body.js' ;
3434import TestServer from './utils/server.js' ;
3535import chaiTimeout from './utils/chai-timeout.js' ;
36- import { isDomainOrSubdomain } from '../src/utils/is.js' ;
36+ import { isDomainOrSubdomain , isSameProtocol } from '../src/utils/is.js' ;
3737
3838const AbortControllerPolyfill = abortControllerPolyfill . AbortController ;
3939const encoder = new TextEncoder ( ) ;
@@ -522,7 +522,7 @@ describe('node-fetch', () => {
522522 expect ( res . url ) . to . equal ( `${ base } inspect` ) ;
523523 expect ( headers . get ( 'other-safe-headers' ) ) . to . equal ( 'stays' ) ;
524524 expect ( headers . get ( 'x-foo' ) ) . to . equal ( 'bar' ) ;
525- // Unsafe headers should not have been sent to httpbin
525+ // Unsafe headers are not removed
526526 expect ( headers . get ( 'cookie' ) ) . to . equal ( 'is=cookie' ) ;
527527 expect ( headers . get ( 'cookie2' ) ) . to . equal ( 'is=cookie2' ) ;
528528 expect ( headers . get ( 'www-authenticate' ) ) . to . equal ( 'is=www-authenticate' ) ;
@@ -542,6 +542,39 @@ describe('node-fetch', () => {
542542 expect ( isDomainOrSubdomain ( 'http://bob.uk.com' , 'http://xyz.uk.com' ) ) . to . be . false ;
543543 } ) ;
544544
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+
545578 it ( 'should treat broken redirect as ordinary response (follow)' , async ( ) => {
546579 const url = `${ base } redirect/no-location` ;
547580 const res = await fetch ( url ) ;
0 commit comments