Skip to content

Commit e87b093

Browse files
authoredJul 18, 2022
fix(Headers): don't forward secure headers on protocol change (#1599)
* fix(Headers): don't forward secure headers on protocol change * fix lint
1 parent bcfb71c commit e87b093

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed
 

‎src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {FetchError} from './errors/fetch-error.js';
2222
import {AbortError} from './errors/abort-error.js';
2323
import {isRedirect} from './utils/is-redirect.js';
2424
import {FormData} from 'formdata-polyfill/esm.min.js';
25-
import {isDomainOrSubdomain} from './utils/is.js';
25+
import {isDomainOrSubdomain, isSameProtocol} from './utils/is.js';
2626
import {parseReferrerPolicyFromHeader} from './utils/referrer.js';
2727
import {
2828
Blob,
@@ -203,7 +203,10 @@ export default async function fetch(url, options_) {
203203
// that is not a subdomain match or exact match of the initial domain.
204204
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
205205
// will forward the sensitive headers, but a redirect to "bar.com" will not.
206-
if (!isDomainOrSubdomain(request.url, locationURL)) {
206+
// headers will also be ignored when following a redirect to a domain using
207+
// a different protocol. For example, a redirect from "https://foo.com" to "http://foo.com"
208+
// will not forward the sensitive headers
209+
if (!isDomainOrSubdomain(request.url, locationURL) || !isSameProtocol(request.url, locationURL)) {
207210
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
208211
requestOptions.headers.delete(name);
209212
}

‎src/utils/is.js

+14
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,17 @@ export const isDomainOrSubdomain = (destination, original) => {
7171

7272
return orig === dest || orig.endsWith(`.${dest}`);
7373
};
74+
75+
/**
76+
* isSameProtocol reports whether the two provided URLs use the same protocol.
77+
*
78+
* Both domains must already be in canonical form.
79+
* @param {string|URL} original
80+
* @param {string|URL} destination
81+
*/
82+
export const isSameProtocol = (destination, original) => {
83+
const orig = new URL(original).protocol;
84+
const dest = new URL(destination).protocol;
85+
86+
return orig === dest;
87+
};

‎test/main.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import ResponseOrig from '../src/response.js';
3333
import Body, {getTotalBytes, extractContentType} from '../src/body.js';
3434
import TestServer from './utils/server.js';
3535
import chaiTimeout from './utils/chai-timeout.js';
36-
import {isDomainOrSubdomain} from '../src/utils/is.js';
36+
import {isDomainOrSubdomain, isSameProtocol} from '../src/utils/is.js';
3737

3838
const AbortControllerPolyfill = abortControllerPolyfill.AbortController;
3939
const 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

Comments
 (0)
Please sign in to comment.