Skip to content

Commit b3ab1fb

Browse files
Jeremy JudeauxJeremy Judeaux
Jeremy Judeaux
authored and
Jeremy Judeaux
committed
cookieDomainRewrite option
1 parent 5082acc commit b3ab1fb

File tree

3 files changed

+99
-18
lines changed

3 files changed

+99
-18
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ proxyServer.listen(8015);
337337
* **hostRewrite**: rewrites the location hostname on (301/302/307/308) redirects.
338338
* **autoRewrite**: rewrites the location host/port on (301/302/307/308) redirects based on requested host/port. Default: false.
339339
* **protocolRewrite**: rewrites the location protocol on (301/302/307/308) redirects to 'http' or 'https'. Default: null.
340+
* **cookieDomainRewrite**: rewrites domain of `set-cookie` headers. `""` to remove the domain. Can provide an object to map domains one by one. Default: `false`.
340341
* **headers**: object with extra headers to be added to target requests.
341342

342343
**NOTE:**

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,44 @@ var redirectRegex = /^30(1|2|7|8)$/;
8080
*
8181
* @api private
8282
*/
83-
function writeHeaders(req, res, proxyRes) {
83+
function writeHeaders(req, res, proxyRes, options) {
84+
var rewriteCookieDomainConfig = options ? options.cookieDomainRewrite : undefined;
85+
if(typeof rewriteCookieDomainConfig === "string") { //also test for ""
86+
rewriteCookieDomainConfig = {"*": rewriteCookieDomainConfig};
87+
}
8488
Object.keys(proxyRes.headers).forEach(function(key) {
85-
if(proxyRes.headers[key] != undefined){
86-
res.setHeader(String(key).trim(), proxyRes.headers[key]);
89+
var header = proxyRes.headers[key];
90+
if(header != undefined){
91+
if(rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
92+
header = rewriteCookieDomain(header);
93+
}
94+
res.setHeader(String(key).trim(), header);
8795
}
8896
});
97+
98+
function rewriteCookieDomain(header) {
99+
if(Array.isArray(header)) {
100+
return header.map(rewriteCookieDomain);
101+
}
102+
return header.replace(/(;\s*domain=)([^;]+)/, function(match, prefix, previousDomain) {
103+
var newDomain;
104+
if(previousDomain in rewriteCookieDomainConfig) {
105+
newDomain = rewriteCookieDomainConfig[previousDomain];
106+
} else if ("*" in rewriteCookieDomainConfig) {
107+
newDomain = rewriteCookieDomainConfig["*"];
108+
} else {
109+
//no match, return previous domain
110+
return match;
111+
}
112+
if(newDomain) {
113+
//replace domain
114+
return prefix + newDomain;
115+
} else {
116+
//remove domain
117+
return "";
118+
}
119+
});
120+
}
89121
},
90122

91123
/**

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

+63-15
Original file line numberDiff line numberDiff line change
@@ -206,24 +206,72 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
206206
});
207207

208208
describe('#writeHeaders', function() {
209-
var proxyRes = {
210-
headers: {
211-
hey: 'hello',
212-
how: 'are you?'
213-
}
214-
};
209+
beforeEach(function() {
210+
this.proxyRes = {
211+
headers: {
212+
hey: 'hello',
213+
how: 'are you?',
214+
'set-cookie': 'hello; domain=my.domain; path=/'
215+
}
216+
};
217+
this.res = {
218+
setHeader: function(k, v) {
219+
this.headers[k] = v;
220+
},
221+
headers: {}
222+
};
223+
});
215224

216-
var res = {
217-
setHeader: function(k, v) {
218-
this.headers[k] = v;
219-
},
220-
headers: {}
221-
};
225+
it("writes headers", function() {
226+
httpProxy.writeHeaders({}, this.res, this.proxyRes);
222227

223-
httpProxy.writeHeaders({}, res, proxyRes);
228+
expect(this.res.headers.hey).to.eql('hello');
229+
expect(this.res.headers.how).to.eql('are you?');
230+
});
224231

225-
expect(res.headers.hey).to.eql('hello');
226-
expect(res.headers.how).to.eql('are you?');
232+
it("rewrites domain", function() {
233+
var options = {
234+
cookieDomainRewrite: "my.new.domain"
235+
};
236+
237+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
238+
239+
expect(this.res.headers['set-cookie']).to.eql('hello; domain=my.new.domain; path=/');
240+
});
241+
242+
it("removes domain", function() {
243+
var options = {
244+
cookieDomainRewrite: ""
245+
};
246+
247+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
248+
249+
expect(this.res.headers['set-cookie']).to.eql('hello; path=/');
250+
});
251+
252+
it("rewrites headers with advanced configuration", function() {
253+
var options = {
254+
cookieDomainRewrite: {
255+
'*': '',
256+
'my.old.domain': 'my.new.domain',
257+
'my.special.domain': 'my.special.domain'
258+
}
259+
};
260+
this.proxyRes.headers['set-cookie'] = [
261+
'hello-on-my.domain; domain=my.domain; path=/',
262+
'hello-on-my.old.domain; domain=my.old.domain; path=/',
263+
'hello-on-my.special.domain; domain=my.special.domain; path=/'
264+
];
265+
266+
httpProxy.writeHeaders({}, this.res, this.proxyRes, options);
267+
268+
expect(this.res.headers['set-cookie'])
269+
.to.contain('hello-on-my.domain; path=/');
270+
expect(this.res.headers['set-cookie'])
271+
.to.contain('hello-on-my.old.domain; domain=my.new.domain; path=/');
272+
expect(this.res.headers['set-cookie'])
273+
.to.contain('hello-on-my.special.domain; domain=my.special.domain; path=/');
274+
});
227275
});
228276

229277

0 commit comments

Comments
 (0)