Skip to content

Fix for issue #318 Reverse Proxy port rewriting for HTTP3xx Location headers #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 29 additions & 31 deletions lib/node-http-proxy/http-proxy.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
/*
node-http-proxy.js: http proxy for node.js
node-http-proxy.js: http proxy for node.js

Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Marak Squires, Fedor Indutny
Copyright (c) 2010 Charlie Robbins, Mikeal Rogers, Marak Squires, Fedor Indutny

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
*/

var events = require('events'),
http = require('http'),
util = require('util'),
httpProxy = require('../node-http-proxy');
http = require('http'),
util = require('util'),
httpProxy = require('../node-http-proxy'),
ReverseProxyHelper = require('./reverse-proxy-helper.js').ReverseProxyHelper;

//
// ### function HttpProxy (options)
Expand Down Expand Up @@ -66,6 +67,8 @@ var HttpProxy = exports.HttpProxy = function (options) {
this.forward = options.forward;
this.target = options.target;

this.reverseProxyHelper = new ReverseProxyHelper(self.target);

//
// Setup the necessary instances instance variables for
// the `target` and `forward` `host:port` combinations
Expand Down Expand Up @@ -104,6 +107,7 @@ var HttpProxy = exports.HttpProxy = function (options) {
this.source = options.source || { host: 'localhost', port: 8000 };
this.source.https = this.source.https || options.https;
this.changeOrigin = options.changeOrigin || false;

};

// Inherit from events.EventEmitter
Expand Down Expand Up @@ -225,10 +229,11 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// origin of the host header to the target URL! Please
// don't revert this without documenting it!
//
var originalHost = req.headers.host;
if (this.changeOrigin) {
outgoing.headers.host = this.target.host + ':' + this.target.port;
}

//
// Open new HTTP request to internal resource with will act
// as a reverse proxy pass
Expand All @@ -247,14 +252,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
delete response.headers['transfer-encoding'];
}

if ((response.statusCode === 301) || (response.statusCode === 302)) {
if (self.source.https && !self.target.https) {
response.headers.location = response.headers.location.replace(/^http\:/, 'https:');
}
if (self.target.https && !self.source.https) {
response.headers.location = response.headers.location.replace(/^https\:/, 'http:');
}
}
self.reverseProxyHelper.rewriteLocationHeader(req, response, originalHost);

// Set the headers of the client response
res.writeHead(response.statusCode, response.headers);
Expand Down
122 changes: 122 additions & 0 deletions lib/node-http-proxy/reverse-proxy-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
reverse-proxy-helper.js: http reverse proxy helper methods.

Copyright (c) 2012 Jo Voordeckers - @jovoordeckers - [email protected]

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

//
// #### function ReverseProxyHelper(target)
// #### @target {Object} Proxy options, target (proxied) server
// Helper functions for reverse proxy mode.
var ReverseProxyHelper = exports.ReverseProxyHelper = function(target) {
var self = this;
self.target = target;
}

//
// #### function httpRedirect(repsonse)
// #### @response {ServerResponse} Response from the target server
// Check if the response is a server-side HTTP 30x Redirect.
ReverseProxyHelper.prototype.isHttpRedirect = function(response) {
return (response.statusCode === 301 || response.statusCode === 302) && !!response.headers && !!response.headers.location;
}

//
// #### function decomposeUrl(url)
// #### @url {String} absolute URL String
// Return an absolute URL in decomposed form { proto, host, port, path }.
ReverseProxyHelper.prototype.decomposeUrl = function (url) {

if (url) {

var urlMatch = url.match(/(https?)\:\/\/([a-zA-Z0-9\-\.]+)(?:\:(\d{1,5}))?((?:\/|\?).+)?/);

if (urlMatch) {

var decomp = {
proto: urlMatch[1],
host: urlMatch[2],
port: urlMatch[3] ? Number(urlMatch[3]) : (urlMatch[1] === "http" ? 80 : 443),
path: urlMatch[4]
};

return decomp;

}
}

return null;
}

//
// #### function rewriteLocationHeader(request, response, originalHost)
// #### @request {ServerRequest} Incoming HTTP Request intercepted by the proxy
// #### @response {ServerResponse} Outgoing HTTP Request to write proxied data to
// #### @originalHost {String} Original Host header of the incoming request, before manipulation
// If needed rewrite the Location header to be consistent with the source and target configuration.
// This will only rewrite if a Host header is present in the original request and
// the X-Forwarded-Proto in the proxy request header.
ReverseProxyHelper.prototype.rewriteLocationHeader = function (request, response, originalHost) {

var self = this,
decompConn;


function isRedirectToTarget(decomp) { // Check if the redirect URL assumes a redirect to the target server

var sourceProto = request.headers["x-forwarded-proto"];

decompConn = self.decomposeUrl(sourceProto+"://"+originalHost);

if (!decompConn) return false;

var targetProto = (self.target.https ? "https" : "http")
var sameProto = targetProto == decomp.proto;
var samePort = Number(self.target.port) === decomp.port;
var isLocalHost = "127.0.0.1" === decomp.host || "localhost" === decomp.host;

return sameProto && samePort && ( decompConn.host === decomp.host || self.target.host == decomp.host || isLocalHost);
}

if (self.isHttpRedirect(response)) {

var decomp = this.decomposeUrl(response.headers.location);

if (decomp && isRedirectToTarget(decomp)) {

var defaultPort = (decompConn.port === 80 || decompConn.port === 443);

var proto = decompConn.proto + "://",
host = decompConn.host,
port = defaultPort ? "" : ":" + decompConn.port,
path = decomp.path;

response.headers['x-reverse-proxy-location-rewritten-from'] = response.headers.location;

response.headers.location = proto + host + port + path;

response.headers['x-reverse-proxy-location-rewritten-to'] = response.headers.location;

}
}
}
Loading