Skip to content

Commit f373f17

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 42e8e1e commit f373f17

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ proxyServer.listen(8015);
372372
"*": ""
373373
}
374374
```
375+
* **cookieRemoveSecure**: true/false, Default: false - removes the `secure` attribute from cookies so they can be used by non-HTTPS origins
375376
* **headers**: object with extra headers to be added to target requests.
376377
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
377378
* **timeout**: timeout (in millis) for incoming requests

lib/http-proxy/common.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,24 @@ common.rewriteCookieProperty = function rewriteCookieProperty(header, config, pr
236236
});
237237
};
238238

239+
/**
240+
* Removes the specified attribute from a cookie header.
241+
*
242+
* @param {String|Array} Header
243+
* @param {String} Property Name of attribute to remove
244+
*
245+
* @api private
246+
*/
247+
common.removeCookieProperty = function removeCookieProperty(header, property) {
248+
if (Array.isArray(header)) {
249+
return header.map(function (headerElement) {
250+
return removeCookieProperty(headerElement, property);
251+
});
252+
}
253+
// Intentionally not checking for "=" to catch directives with no value (eg "; secure").
254+
return header.replace(new RegExp(';\\s*' + property + '[^;]*', 'i'), '');
255+
};
256+
239257
/**
240258
* Check the host and see if it potentially has a port in it (keep it simple)
241259
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ module.exports = { // <--
9595
if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
9696
header = common.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
9797
}
98+
if (options.cookieRemoveSecure && key.toLowerCase() === 'set-cookie') {
99+
header = common.removeCookieProperty(header, 'secure');
100+
}
98101
res.setHeader(String(key).trim(), header);
99102
};
100103

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
};
@@ -404,6 +404,26 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
404404
expect(this.res.headers['set-cookie'])
405405
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
406406
});
407+
408+
it('does not remove `secure` attribute by default', function() {
409+
var options = {};
410+
411+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
412+
413+
expect(this.res.headers['set-cookie'])
414+
.to.contain('there; domain=my.domain; path=/; secure');
415+
});
416+
417+
it('removes `secure` attribute when cookieRemoveSecure true', function() {
418+
var options = {
419+
cookieRemoveSecure: true
420+
};
421+
422+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
423+
424+
expect(this.res.headers['set-cookie'])
425+
.to.contain('there; domain=my.domain; path=/');
426+
});
407427
});
408428

409429

0 commit comments

Comments
 (0)