Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: thirdparty site cookie leak #73

Merged
merged 3 commits into from Jan 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions index.js
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