Skip to content

Commit 489f743

Browse files
authored
Bug fix: Thirdparty cookie leak
1 parent 6e4468a commit 489f743

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

index.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ const once = require('once')
99
const querystring = require('querystring')
1010
const url = require('url')
1111

12-
var flag=false
13-
var original_host;
14-
1512
const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'
1613

1714
function simpleGet (opts, cb) {
@@ -37,13 +34,8 @@ function simpleGet (opts, cb) {
3734
opts.headers['content-type'] = 'application/x-www-form-urlencoded'
3835
}
3936

40-
//getting original host
41-
if (!flag){
42-
original_host=opts.hostname
43-
//console.log(original_host)
44-
flag=true
45-
}
46-
37+
const ohost = opts.hostname
38+
4739
if (body) {
4840
if (!opts.method) opts.method = 'POST'
4941
if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body)
@@ -61,13 +53,13 @@ function simpleGet (opts, cb) {
6153
delete opts.headers.host // Discard `host` header on redirect (see #32)
6254
res.resume() // Discard response
6355

64-
var redirect_host=url.parse(opts.url).hostname //getting redirected hostname
65-
//if redirected host is different than original host then drop cookie header to prevent cookie leak in thirdparty site redirect
66-
if(redirect_host !== null && redirect_host !== original_host){
67-
delete opts.headers.cookie;
68-
delete opts.headers.authorization;
69-
}
70-
56+
const rhost = url.parse(opts.url).hostname // eslint-disable-line node/no-deprecated-api
57+
// if redirected host is different than original host then drop cookie header to prevent cookie leak in thirdparty site redirect
58+
if (rhost !== null && rhost !== ohost) {
59+
delete opts.headers.cookie
60+
delete opts.headers.authorization
61+
}
62+
7163
if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
7264
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
7365
delete opts.headers['content-length']; delete opts.headers['content-type']

0 commit comments

Comments
 (0)