Skip to content

Commit 2ec9b0b

Browse files
committed
Keep headers when upgrading from HTTP to HTTPS.
Fixes #192
1 parent 5fc74dd commit 2ec9b0b

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
array-callback-return: "error",
1313
block-scoped-var: "error",
1414
class-methods-use-this: "error",
15-
complexity: "error",
15+
complexity: "off",
1616
consistent-return: "error",
1717
curly: "error",
1818
default-case: "error",

index.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,12 @@ RedirectableRequest.prototype._processResponse = function (response) {
403403
var redirectUrlParts = url.parse(redirectUrl);
404404
Object.assign(this._options, redirectUrlParts);
405405

406-
// Drop confidential headers when redirecting to another scheme:domain
407-
if (redirectUrlParts.protocol !== currentUrlParts.protocol ||
408-
!isSameOrSubdomain(redirectUrlParts.host, currentHost)) {
406+
// Drop confidential headers when redirecting to a less secure protocol
407+
// or to a different domain that is not a superdomain
408+
if (redirectUrlParts.protocol !== currentUrlParts.protocol &&
409+
redirectUrlParts.protocol !== "https:" ||
410+
redirectUrlParts.host !== currentHost &&
411+
!isSubdomain(redirectUrlParts.host, currentHost)) {
409412
removeMatchingHeaders(/^(?:authorization|cookie)$/i, this._options.headers);
410413
}
411414

@@ -561,10 +564,7 @@ function abortRequest(request) {
561564
request.abort();
562565
}
563566

564-
function isSameOrSubdomain(subdomain, domain) {
565-
if (subdomain === domain) {
566-
return true;
567-
}
567+
function isSubdomain(subdomain, domain) {
568568
const dot = subdomain.length - domain.length - 1;
569569
return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
570570
}

test/test.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,36 @@ describe("follow-redirects", function () {
15231523
});
15241524
});
15251525

1526-
it("drops the header when redirected to a different scheme", function () {
1526+
it("keeps the header when redirected from HTTP to HTTPS", function () {
1527+
app.get("/a", redirectsTo(302, "https://localhost:3600/b"));
1528+
app.get("/b", function (req, res) {
1529+
res.end(JSON.stringify(req.headers));
1530+
});
1531+
1532+
var opts = url.parse("http://localhost:3600/a");
1533+
opts.headers = {};
1534+
opts.headers[header] = "the header value";
1535+
1536+
// Intercept the scheme
1537+
opts.beforeRedirect = function (options) {
1538+
assert.equal(options.protocol, "https:");
1539+
options.protocol = "http:";
1540+
};
1541+
1542+
return server.start(app)
1543+
.then(asPromise(function (resolve, reject) {
1544+
http.get(opts, resolve).on("error", reject);
1545+
}))
1546+
.then(asPromise(function (resolve, reject, res) {
1547+
res.pipe(concat({ encoding: "string" }, resolve)).on("error", reject);
1548+
}))
1549+
.then(function (str) {
1550+
var body = JSON.parse(str);
1551+
assert.equal(body[header.toLowerCase()], "the header value");
1552+
});
1553+
});
1554+
1555+
it("drops the header when redirected from HTTPS to HTTP", function () {
15271556
app.get("/a", redirectsTo(302, "http://localhost:3601/b"));
15281557
app.get("/b", function (req, res) {
15291558
res.end(JSON.stringify(req.headers));

0 commit comments

Comments
 (0)