Skip to content

Commit 26029ba

Browse files
committed
only rewrite redirect urls when it matches target
if functioning as a reverse proxy for host1.foo.com, with a backend target of backend.foo.com:8080, the node proxy should only rewrite the redirect if it is a redirect to somewhere on backend.foo.com:8080
1 parent 14415a5 commit 26029ba

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ var redirectRegex = /^30(1|2|7|8)$/;
5050
if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
5151
&& proxyRes.headers['location']
5252
&& redirectRegex.test(proxyRes.statusCode)) {
53+
var target = url.parse(options.target);
5354
var u = url.parse(proxyRes.headers['location']);
55+
56+
// make sure the redirected host matches the target host before rewriting
57+
if (target.host != u.host) {
58+
return;
59+
}
60+
5461
if (options.hostRewrite) {
5562
u.host = options.hostRewrite;
5663
} else if (options.autoRewrite) {

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

+28
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
4949
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
5050
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
5151
});
52+
53+
it('not when the redirected location does not match target host', function() {
54+
this.proxyRes.statusCode = 302;
55+
this.proxyRes.headers.location = "http://some-other/";
56+
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
57+
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
58+
});
59+
60+
it('not when the redirected location does not match target port', function() {
61+
this.proxyRes.statusCode = 302;
62+
this.proxyRes.headers.location = "http://backend.com:8080/";
63+
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
64+
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
65+
});
5266
});
5367

5468
context('rewrites location host with autoRewrite', function() {
@@ -74,6 +88,20 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
7488
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
7589
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
7690
});
91+
92+
it('not when the redirected location does not match target host', function() {
93+
this.proxyRes.statusCode = 302;
94+
this.proxyRes.headers.location = "http://some-other/";
95+
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
96+
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
97+
});
98+
99+
it('not when the redirected location does not match target port', function() {
100+
this.proxyRes.statusCode = 302;
101+
this.proxyRes.headers.location = "http://backend.com:8080/";
102+
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
103+
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
104+
});
77105
});
78106

79107
context('rewrites location protocol with protocolRewrite', function() {

0 commit comments

Comments
 (0)