Skip to content

Commit

Permalink
Bug fix: thirdparty site cookie leak
Browse files Browse the repository at this point in the history
  • Loading branch information
ranjit-git authored Jan 18, 2022
1 parent 81eaf56 commit 6e4468a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const once = require('once')
const querystring = require('querystring')
const url = require('url')

var flag=false
var original_host;

const isStream = o => o !== null && typeof o === 'object' && typeof o.pipe === 'function'

function simpleGet (opts, cb) {
Expand All @@ -34,6 +37,13 @@ function simpleGet (opts, cb) {
opts.headers['content-type'] = 'application/x-www-form-urlencoded'
}

//getting original host
if (!flag){
original_host=opts.hostname
//console.log(original_host)
flag=true
}

if (body) {
if (!opts.method) opts.method = 'POST'
if (!isStream(body)) opts.headers['content-length'] = Buffer.byteLength(body)
Expand All @@ -51,6 +61,13 @@ function simpleGet (opts, cb) {
delete opts.headers.host // Discard `host` header on redirect (see #32)
res.resume() // Discard response

var redirect_host=url.parse(opts.url).hostname //getting redirected hostname
//if redirected host is different than original host then drop cookie header to prevent cookie leak in thirdparty site redirect
if(redirect_host !== null && redirect_host !== original_host){
delete opts.headers.cookie;
delete opts.headers.authorization;
}

if (opts.method === 'POST' && [301, 302].includes(res.statusCode)) {
opts.method = 'GET' // On 301/302 redirect, change POST to GET (see #35)
delete opts.headers['content-length']; delete opts.headers['content-type']
Expand Down

0 comments on commit 6e4468a

Please sign in to comment.