Skip to content

Commit f5d3cf5

Browse files
authored
fix(Headers): don't forward secure headers to 3th party (#1449)
* fix(Headers): don't forward secure headers to 3th party * added more narrow test for isDomainOrSubdomain
1 parent f2c3d56 commit f5d3cf5

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

src/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Request, {getNodeRequestOptions} from './request.js';
2121
import {FetchError} from './errors/fetch-error.js';
2222
import {AbortError} from './errors/abort-error.js';
2323
import {isRedirect} from './utils/is-redirect.js';
24+
import {isDomainOrSubdomain} from './utils/is.js';
2425
import {parseReferrerPolicyFromHeader} from './utils/referrer.js';
2526

2627
export {Headers, Request, Response, FetchError, AbortError, isRedirect};
@@ -188,6 +189,18 @@ export default async function fetch(url, options_) {
188189
referrerPolicy: request.referrerPolicy
189190
};
190191

192+
// when forwarding sensitive headers like "Authorization",
193+
// "WWW-Authenticate", and "Cookie" to untrusted targets,
194+
// headers will be ignored when following a redirect to a domain
195+
// that is not a subdomain match or exact match of the initial domain.
196+
// For example, a redirect from "foo.com" to either "foo.com" or "sub.foo.com"
197+
// will forward the sensitive headers, but a redirect to "bar.com" will not.
198+
if (!isDomainOrSubdomain(request.url, locationURL)) {
199+
for (const name of ['authorization', 'www-authenticate', 'cookie', 'cookie2']) {
200+
requestOptions.headers.delete(name);
201+
}
202+
}
203+
191204
// HTTP-redirect fetch step 9
192205
if (response_.statusCode !== 303 && request.body && options_.body instanceof Stream.Readable) {
193206
reject(new FetchError('Cannot follow redirect with body being a readable stream', 'unsupported-redirect'));

src/utils/is.js

+17
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,20 @@ export const isAbortSignal = object => {
5656
)
5757
);
5858
};
59+
60+
/**
61+
* isDomainOrSubdomain reports whether sub is a subdomain (or exact match) of
62+
* the parent domain.
63+
*
64+
* Both domains must already be in canonical form.
65+
* @param {string|URL} original
66+
* @param {string|URL} destination
67+
*/
68+
export const isDomainOrSubdomain = (destination, original) => {
69+
const orig = new URL(original).hostname;
70+
const dest = new URL(destination).hostname;
71+
72+
return orig === dest || (
73+
orig[orig.length - dest.length - 1] === '.' && orig.endsWith(dest)
74+
);
75+
};

test/main.js

+61
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import ResponseOrig from '../src/response.js';
3535
import Body, {getTotalBytes, extractContentType} from '../src/body.js';
3636
import TestServer from './utils/server.js';
3737
import chaiTimeout from './utils/chai-timeout.js';
38+
import {isDomainOrSubdomain} from '../src/utils/is.js';
3839

3940
const AbortControllerPolyfill = abortControllerPolyfill.AbortController;
4041
const encoder = new TextEncoder();
@@ -496,6 +497,66 @@ describe('node-fetch', () => {
496497
});
497498
});
498499

500+
it('should not forward secure headers to 3th party', async () => {
501+
const res = await fetch(`${base}redirect-to/302/https://httpbin.org/get`, {
502+
headers: new Headers({
503+
cookie: 'gets=removed',
504+
cookie2: 'gets=removed',
505+
authorization: 'gets=removed',
506+
'www-authenticate': 'gets=removed',
507+
'other-safe-headers': 'stays',
508+
'x-foo': 'bar'
509+
})
510+
});
511+
512+
const headers = new Headers((await res.json()).headers);
513+
// Safe headers are not removed
514+
expect(headers.get('other-safe-headers')).to.equal('stays');
515+
expect(headers.get('x-foo')).to.equal('bar');
516+
// Unsafe headers should not have been sent to httpbin
517+
expect(headers.get('cookie')).to.equal(null);
518+
expect(headers.get('cookie2')).to.equal(null);
519+
expect(headers.get('www-authenticate')).to.equal(null);
520+
expect(headers.get('authorization')).to.equal(null);
521+
});
522+
523+
it('should forward secure headers to same host', async () => {
524+
const res = await fetch(`${base}redirect-to/302/${base}inspect`, {
525+
headers: new Headers({
526+
cookie: 'is=cookie',
527+
cookie2: 'is=cookie2',
528+
authorization: 'is=authorization',
529+
'other-safe-headers': 'stays',
530+
'www-authenticate': 'is=www-authenticate',
531+
'x-foo': 'bar'
532+
})
533+
});
534+
535+
const headers = new Headers((await res.json()).headers);
536+
// Safe headers are not removed
537+
expect(res.url).to.equal(`${base}inspect`);
538+
expect(headers.get('other-safe-headers')).to.equal('stays');
539+
expect(headers.get('x-foo')).to.equal('bar');
540+
// Unsafe headers should not have been sent to httpbin
541+
expect(headers.get('cookie')).to.equal('is=cookie');
542+
expect(headers.get('cookie2')).to.equal('is=cookie2');
543+
expect(headers.get('www-authenticate')).to.equal('is=www-authenticate');
544+
expect(headers.get('authorization')).to.equal('is=authorization');
545+
});
546+
547+
it('isDomainOrSubdomain', () => {
548+
// Forwarding headers to same (sub)domain are OK
549+
expect(isDomainOrSubdomain('http://a.com', 'http://a.com')).to.be.true;
550+
expect(isDomainOrSubdomain('http://a.com', 'http://www.a.com')).to.be.true;
551+
expect(isDomainOrSubdomain('http://a.com', 'http://foo.bar.a.com')).to.be.true;
552+
553+
// Forwarding headers to parent domain, another sibling or a totally other domain is not ok
554+
expect(isDomainOrSubdomain('http://b.com', 'http://a.com')).to.be.false;
555+
expect(isDomainOrSubdomain('http://www.a.com', 'http://a.com')).to.be.false;
556+
expect(isDomainOrSubdomain('http://bob.uk.com', 'http://uk.com')).to.be.false;
557+
expect(isDomainOrSubdomain('http://bob.uk.com', 'http://xyz.uk.com')).to.be.false;
558+
});
559+
499560
it('should treat broken redirect as ordinary response (follow)', () => {
500561
const url = `${base}redirect/no-location`;
501562
return fetch(url).then(res => {

test/utils/server.js

+6
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ export default class TestServer {
245245
res.end();
246246
}
247247

248+
if (p.startsWith('/redirect-to/3')) {
249+
res.statusCode = p.slice(13, 16);
250+
res.setHeader('Location', p.slice(17));
251+
res.end();
252+
}
253+
248254
if (p === '/redirect/302') {
249255
res.statusCode = 302;
250256
res.setHeader('Location', '/inspect');

0 commit comments

Comments
 (0)