Skip to content

Commit

Permalink
Fix req.host when using "trust proxy" hops count
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed Mar 1, 2015
1 parent 20aa126 commit 2e0f5e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
18 changes: 18 additions & 0 deletions test/req.host.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down

0 comments on commit 2e0f5e7

Please sign in to comment.