Skip to content

Commit 62e546a

Browse files
committed
Drop confidential headers across schemes.
1 parent 2ede36d commit 62e546a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,9 @@ RedirectableRequest.prototype._processResponse = function (response) {
392392
var redirectUrlParts = url.parse(redirectUrl);
393393
Object.assign(this._options, redirectUrlParts);
394394

395-
// Drop the confidential headers when redirecting to another domain
396-
if (!(redirectUrlParts.host === currentHost || isSubdomainOf(redirectUrlParts.host, currentHost))) {
395+
// Drop confidential headers when redirecting to another scheme:domain
396+
if (redirectUrlParts.protocol !== currentUrlParts.protocol ||
397+
!isSameOrSubdomain(redirectUrlParts.host, currentHost)) {
397398
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
398399
}
399400

@@ -559,7 +560,10 @@ function abortRequest(request) {
559560
request.abort();
560561
}
561562

562-
function isSubdomainOf(subdomain, domain) {
563+
function isSameOrSubdomain(subdomain, domain) {
564+
if (subdomain === domain) {
565+
return true;
566+
}
563567
const dot = subdomain.length - domain.length - 1;
564568
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
565569
}

test/test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,36 @@ describe("follow-redirects", function () {
15221522
});
15231523
});
15241524
});
1525+
1526+
it("drops the header when redirected to a different scheme", function () {
1527+
app.get("/a", redirectsTo(302, "http://localhost:3601/b"));
1528+
app.get("/b", function (req, res) {
1529+
res.end(JSON.stringify(req.headers));
1530+
});
1531+
1532+
var opts = url.parse("https://localhost:3601/a");
1533+
opts.ca = ca;
1534+
opts.headers = {};
1535+
opts.headers[header] = "the header value";
1536+
1537+
// Intercept the scheme
1538+
opts.beforeRedirect = function (options) {
1539+
assert.equal(options.protocol, "http:");
1540+
options.protocol = "https:";
1541+
};
1542+
1543+
return server.start(httpsOptions(app))
1544+
.then(asPromise(function (resolve, reject) {
1545+
https.get(opts, resolve).on("error", reject);
1546+
}))
1547+
.then(asPromise(function (resolve, reject, res) {
1548+
res.pipe(concat({ encoding: "string" }, resolve)).on("error", reject);
1549+
}))
1550+
.then(function (str) {
1551+
var body = JSON.parse(str);
1552+
assert.equal(body[header.toLowerCase()], undefined);
1553+
});
1554+
});
15251555
});
15261556

15271557
describe("when the followRedirects option is set to false", function () {

0 commit comments

Comments
 (0)