Fix req.host when using "trust proxy" hops count

This commit is contained in:
Douglas Christopher Wilson 2015-02-28 21:32:51 -05:00
parent 20aa12616a
commit 2e0f5e7817
3 changed files with 20 additions and 1 deletions

View File

@ -1,6 +1,7 @@
3.x
===
* Fix `req.host` when using "trust proxy" hops count
* Fix `req.protocol`/`req.secure` when using "trust proxy" hops count
3.20.0 / 2015-02-18

View File

@ -491,7 +491,7 @@ req.__defineGetter__('host', function(){
var trust = this.app.get('trust proxy fn');
var host = this.get('X-Forwarded-Host');
if (!host || !trust(this.connection.remoteAddress)) {
if (!host || !trust(this.connection.remoteAddress, 0)) {
host = this.get('Host');
}

View File

@ -117,6 +117,24 @@ describe('req', function(){
.set('Host', 'example.com')
.expect('example.com', done);
})
describe('when trusting hop count', function () {
it('should respect X-Forwarded-Host', function (done) {
var app = express();
app.set('trust proxy', 1);
app.use(function (req, res) {
res.end(req.host);
});
request(app)
.get('/')
.set('Host', 'localhost')
.set('X-Forwarded-Host', 'example.com')
.expect('example.com', done);
})
})
})
describe('when "trust proxy" is disabled', function(){