Skip to content

Commit 2e0f5e7

Browse files
committedMar 1, 2015
Fix req.host when using "trust proxy" hops count
1 parent 20aa126 commit 2e0f5e7

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed
 

‎History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
3.x
22
===
33

4+
* Fix `req.host` when using "trust proxy" hops count
45
* Fix `req.protocol`/`req.secure` when using "trust proxy" hops count
56

67
3.20.0 / 2015-02-18

‎lib/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ req.__defineGetter__('host', function(){
491491
var trust = this.app.get('trust proxy fn');
492492
var host = this.get('X-Forwarded-Host');
493493

494-
if (!host || !trust(this.connection.remoteAddress)) {
494+
if (!host || !trust(this.connection.remoteAddress, 0)) {
495495
host = this.get('Host');
496496
}
497497

‎test/req.host.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ describe('req', function(){
117117
.set('Host', 'example.com')
118118
.expect('example.com', done);
119119
})
120+
121+
describe('when trusting hop count', function () {
122+
it('should respect X-Forwarded-Host', function (done) {
123+
var app = express();
124+
125+
app.set('trust proxy', 1);
126+
127+
app.use(function (req, res) {
128+
res.end(req.host);
129+
});
130+
131+
request(app)
132+
.get('/')
133+
.set('Host', 'localhost')
134+
.set('X-Forwarded-Host', 'example.com')
135+
.expect('example.com', done);
136+
})
137+
})
120138
})
121139

122140
describe('when "trust proxy" is disabled', function(){

0 commit comments

Comments
 (0)