Skip to content

Commit 0979c60

Browse files
authored
Prevent Cookie & Authorization Headers from being forwarded when the URL redirects to another domain (information leak) #137
1 parent 5e1a63c commit 0979c60

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ var extend = require('extend');
1111
var request = require('request');
1212
var RetryStrategies = require('./strategies');
1313
var _ = require('lodash');
14+
var url = require('url');
15+
var querystring = require("querystring");
1416

1517
var DEFAULTS = {
1618
maxAttempts: 5, // try 5 times
@@ -24,6 +26,42 @@ function defaultPromiseFactory(resolver) {
2426
return new Promise(resolver);
2527
}
2628

29+
// Prevent Cookie & Authorization Headers from being forwarded
30+
// when the URL redirects to another domain (information leak) #137
31+
function sanitizeHeaders(options) {
32+
33+
const HEADERS_TO_IGNORE = ["cookie", "authorization"];
34+
35+
const urlObject = url.parse(options.url)
36+
const queryObject = querystring.parse(urlObject.query);
37+
38+
const hasExternalLink = Object.keys(queryObject).reduce(function(acc, cur) {
39+
40+
let qUrl = url.parse(queryObject[cur]);
41+
42+
// external link if protocol || host || port is different
43+
if(!!qUrl.host && (qUrl.protocol !== urlObject.protocol || qUrl.host !== urlObject.host || qUrl.port !== urlObject.port) ) {
44+
acc = true;
45+
}
46+
47+
return acc;
48+
49+
}, false);
50+
51+
if (hasExternalLink && options.hasOwnProperty("headers") && typeof(options.headers) === "object") {
52+
53+
// if External Link: remove Cookie and Authorization from Headers
54+
Object.keys(options.headers).filter(function(key) {
55+
return HEADERS_TO_IGNORE.includes(key.toLowerCase())
56+
}).map(function(key) {
57+
return delete options.headers[key]
58+
});
59+
60+
}
61+
62+
return options;
63+
}
64+
2765
function _cloneOptions(options) {
2866
const cloned = {};
2967
for (let key in options) {
@@ -85,7 +123,7 @@ function Request(url, options, f, retryConfig) {
85123
* Option object
86124
* @type {Object}
87125
*/
88-
this.options = options;
126+
this.options = sanitizeHeaders(options);
89127

90128
/**
91129
* Return true if the request should be retried

0 commit comments

Comments
 (0)