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

Add option to remove the 'secure' attribute from cookies #1166

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ proxyServer.listen(8015);
"*": ""
}
```
* **cookieRemoveSecure**: true/false, Default: false - removes the `secure` attribute from cookies so they can be used by non-HTTPS origins
* **headers**: object with extra headers to be added to target requests.
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **timeout**: timeout (in millis) for incoming requests
Expand Down
18 changes: 18 additions & 0 deletions lib/http-proxy/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ common.rewriteCookieProperty = function rewriteCookieProperty(header, config, pr
});
};

/**
* Removes the specified attribute from a cookie header.
*
* @param {String|Array} Header
* @param {String} Property Name of attribute to remove
*
* @api private
*/
common.removeCookieProperty = function removeCookieProperty(header, property) {
if (Array.isArray(header)) {
return header.map(function (headerElement) {
return removeCookieProperty(headerElement, property);
});
}
// Intentionally not checking for "=" to catch directives with no value (eg "; secure").
return header.replace(new RegExp(';\\s*' + property + '[^;]*', 'i'), '');
};

/**
* Check the host and see if it potentially has a port in it (keep it simple)
*
Expand Down
3 changes: 3 additions & 0 deletions lib/http-proxy/passes/web-outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ module.exports = { // <--
if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
header = common.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
}
if (options.cookieRemoveSecure && key.toLowerCase() === 'set-cookie') {
header = common.removeCookieProperty(header, 'secure');
}
res.setHeader(String(key).trim(), header);
};

Expand Down
22 changes: 21 additions & 1 deletion test/lib-http-proxy-passes-web-outgoing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
how: 'are you?',
'set-cookie': [
'hello; domain=my.domain; path=/',
'there; domain=my.domain; path=/'
'there; domain=my.domain; path=/; secure'
]
}
};
Expand Down Expand Up @@ -404,6 +404,26 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
expect(this.res.headers['set-cookie'])
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
});

it('does not remove `secure` attribute by default', function() {
var options = {};

httpProxy.writeHeaders({}, this.res, this.proxyRes, options);

expect(this.res.headers['set-cookie'])
.to.contain('there; domain=my.domain; path=/; secure');
});

it('removes `secure` attribute when cookieRemoveSecure true', function() {
var options = {
cookieRemoveSecure: true
};

httpProxy.writeHeaders({}, this.res, this.proxyRes, options);

expect(this.res.headers['set-cookie'])
.to.contain('there; domain=my.domain; path=/');
});
});


Expand Down