Skip to content

Commit eb86fa1

Browse files
committed
Add option to remove the 'secure' attribute from cookies
This allows cookies proxied from HTTPS sites to be used by a non-HTTPS localhost development environment. Fixes #1165.
1 parent c979ba9 commit eb86fa1

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ proxyServer.listen(8015);
350350
"*": ""
351351
}
352352
```
353+
* **cookieRemoveSecure**: true/false, Default: false - removes the `secure` attribute from cookies so they can be used by non-HTTPS origins
353354
* **headers**: object with extra headers to be added to target requests.
354355
* **proxyTimeout**: timeout (in millis) when proxy receives no response from target
355356

lib/http-proxy/common.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ var common = exports,
55

66
var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
77
isSSL = /^https|wss/,
8-
cookieDomainRegex = /(;\s*domain=)([^;]+)/i;
8+
cookieDomainRegex = /(;\s*domain=)([^;]+)/i,
9+
cookieSecureRegex = /;\s*secure[^;]*/i;
910

1011
/**
1112
* Simple Regex for testing if protocol is https
@@ -237,6 +238,20 @@ common.rewriteCookieDomain = function rewriteCookieDomain(header, config) {
237238
});
238239
};
239240

241+
/**
242+
* Removes the secure attribute of a cookie header
243+
*
244+
* @param {String|Array} Header
245+
*
246+
* @api private
247+
*/
248+
common.removeCookieSecure = function removeCookieSecure(header) {
249+
if (Array.isArray(header)) {
250+
return header.map(removeCookieSecure);
251+
}
252+
return header.replace(cookieSecureRegex, "");
253+
};
254+
240255
/**
241256
* Check the host and see if it potentially has a port in it (keep it simple)
242257
*

lib/http-proxy/passes/web-outgoing.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ module.exports = { // <--
8484
*/
8585
writeHeaders: function writeHeaders(req, res, proxyRes, options) {
8686
var rewriteCookieDomainConfig = options.cookieDomainRewrite,
87+
cookieRemoveSecure = options.cookieRemoveSecure,
8788
preserveHeaderKeyCase = options.preserveHeaderKeyCase,
8889
rawHeaderKeyMap,
8990
setHeader = function(key, header) {
9091
if (header == undefined) return;
91-
if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
92-
header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig);
92+
if (key.toLowerCase() === 'set-cookie') {
93+
if (rewriteCookieDomainConfig) {
94+
header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig);
95+
}
96+
if (cookieRemoveSecure) {
97+
header = common.removeCookieSecure(header);
98+
}
9399
}
94100
res.setHeader(String(key).trim(), header);
95101
};

test/lib-http-proxy-passes-web-outgoing-test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
235235
how: 'are you?',
236236
'set-cookie': [
237237
'hello; domain=my.domain; path=/',
238-
'there; domain=my.domain; path=/'
238+
'there; domain=my.domain; path=/; secure'
239239
]
240240
}
241241
};
@@ -373,6 +373,26 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
373373
expect(this.res.headers['set-cookie'])
374374
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
375375
});
376+
377+
it('does not remove secure', function() {
378+
var options = {};
379+
380+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
381+
382+
expect(this.res.headers['set-cookie'])
383+
.to.contain('there; domain=my.domain; path=/; secure');
384+
});
385+
386+
it('removes secure', function() {
387+
var options = {
388+
cookieRemoveSecure: true
389+
};
390+
391+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
392+
393+
expect(this.res.headers['set-cookie'])
394+
.to.contain('there; domain=my.domain; path=/');
395+
});
376396
});
377397

378398

0 commit comments

Comments
 (0)