From 2c98416ac2bf17bb5f515b9e10ee4485f5913846 Mon Sep 17 00:00:00 2001 From: Sean Willis Date: Tue, 20 Feb 2018 17:50:59 -0800 Subject: [PATCH] Adding ability to set cookie path --- lib/http-proxy/common.js | 29 ++++++++++--------- lib/http-proxy/passes/web-outgoing.js | 10 ++++++- ...lib-http-proxy-passes-web-outgoing-test.js | 11 +++++++ 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/lib/http-proxy/common.js b/lib/http-proxy/common.js index aa97002..c00994c 100644 --- a/lib/http-proxy/common.js +++ b/lib/http-proxy/common.js @@ -5,7 +5,7 @@ var common = exports, var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i, isSSL = /^https|wss/, - cookieDomainRegex = /(;\s*domain=)([^;]+)/i; + cookieProps = ['domain', 'path']; /** * Simple Regex for testing if protocol is https @@ -211,27 +211,30 @@ common.urlJoin = function() { * * @api private */ -common.rewriteCookieDomain = function rewriteCookieDomain(header, config) { +common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) { + if(cookieProps.indexOf(property) === -1) //Property not supported + return header; + if (Array.isArray(header)) { return header.map(function (headerElement) { - return rewriteCookieDomain(headerElement, config); + return rewriteCookieProperty(headerElement, config, property); }); } - return header.replace(cookieDomainRegex, function(match, prefix, previousDomain) { - var newDomain; - if (previousDomain in config) { - newDomain = config[previousDomain]; + return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)"), function(match, prefix, previousValue) { + var newValue; + if (previousValue in config) { + newValue = config[previousValue]; } else if ('*' in config) { - newDomain = config['*']; + newValue = config['*']; } else { - //no match, return previous domain + //no match, return previous value return match; } - if (newDomain) { - //replace domain - return prefix + newDomain; + if (newValue) { + //replace value + return prefix + newValue; } else { - //remove domain + //remove value return ''; } }); diff --git a/lib/http-proxy/passes/web-outgoing.js b/lib/http-proxy/passes/web-outgoing.js index cff86a7..05ded88 100644 --- a/lib/http-proxy/passes/web-outgoing.js +++ b/lib/http-proxy/passes/web-outgoing.js @@ -84,12 +84,16 @@ module.exports = { // <-- */ writeHeaders: function writeHeaders(req, res, proxyRes, options) { var rewriteCookieDomainConfig = options.cookieDomainRewrite, + rewriteCookiePathConfig = options.cookiePathRewrite, preserveHeaderKeyCase = options.preserveHeaderKeyCase, rawHeaderKeyMap, setHeader = function(key, header) { if (header == undefined) return; if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') { - header = common.rewriteCookieDomain(header, rewriteCookieDomainConfig); + header = common.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain'); + } + if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') { + header = common.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path'); } res.setHeader(String(key).trim(), header); }; @@ -98,6 +102,10 @@ module.exports = { // <-- rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig }; } + if (typeof rewriteCookiePathConfig === 'string') { //also test for '' + rewriteCookiePathConfig = { '*': rewriteCookiePathConfig }; + } + // message.rawHeaders is added in: v0.11.6 // https://nodejs.org/api/http.html#http_message_rawheaders if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) { diff --git a/test/lib-http-proxy-passes-web-outgoing-test.js b/test/lib-http-proxy-passes-web-outgoing-test.js index ae86904..aef521c 100644 --- a/test/lib-http-proxy-passes-web-outgoing-test.js +++ b/test/lib-http-proxy-passes-web-outgoing-test.js @@ -298,6 +298,17 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () { .to.contain('hello; domain=my.domain; path=/'); }); + it('rewrites path', function() { + var options = { + cookiePathRewrite: '/dummyPath' + }; + + httpProxy.writeHeaders({}, this.res, this.proxyRes, options); + + expect(this.res.headers['set-cookie']) + .to.contain('hello; domain=my.domain; path=/dummyPath'); + }); + it('rewrites domain', function() { var options = { cookieDomainRewrite: 'my.new.domain'