Skip to content

Commit

Permalink
only rewrite redirect urls when it matches target
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matthauck committed Mar 9, 2015
1 parent 14415a5 commit 26029ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/http-proxy/passes/web-outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ var redirectRegex = /^30(1|2|7|8)$/;
if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
&& proxyRes.headers['location']
&& redirectRegex.test(proxyRes.statusCode)) {
var target = url.parse(options.target);
var u = url.parse(proxyRes.headers['location']);

// make sure the redirected host matches the target host before rewriting
if (target.host != u.host) {
return;
}

if (options.hostRewrite) {
u.host = options.hostRewrite;
} else if (options.autoRewrite) {
Expand Down
28 changes: 28 additions & 0 deletions test/lib-http-proxy-passes-web-outgoing-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://ext-manual.com/');
});

it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://some-other/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});

it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://backend.com:8080/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});

context('rewrites location host with autoRewrite', function() {
Expand All @@ -74,6 +88,20 @@ describe('lib/http-proxy/passes/web-outgoing.js', function () {
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com/');
});

it('not when the redirected location does not match target host', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://some-other/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://some-other/');
});

it('not when the redirected location does not match target port', function() {
this.proxyRes.statusCode = 302;
this.proxyRes.headers.location = "http://backend.com:8080/";
httpProxy.setRedirectHostRewrite(this.req, {}, this.proxyRes, this.options);
expect(this.proxyRes.headers.location).to.eql('http://backend.com:8080/');
});
});

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

1 comment on commit 26029ba

@kvetis
Copy link

@kvetis kvetis commented on 26029ba Oct 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too restrictive and should be configurable. I'm just resolving a redirect from a naked domain to www. Target is example.com/api/whatever and location www.example.com/api/whatever. I need this redirection to be rewritten as well.

Please sign in to comment.