Skip to content

Commit 20aa126

Browse files
Paul Serbydougwilson
authored andcommitted
Fix req.protocol/req.secure when using "trust proxy" hops count
fixes #2569 closes #2570
1 parent bb4703e commit 20aa126

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
3.x
2+
===
3+
4+
* Fix `req.protocol`/`req.secure` when using "trust proxy" hops count
5+
16
3.20.0 / 2015-02-18
27
===================
38

lib/request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ req.__defineGetter__('protocol', function(){
358358
: 'http';
359359
var trust = this.app.get('trust proxy fn');
360360

361-
if (!trust(this.connection.remoteAddress)) {
361+
if (!trust(this.connection.remoteAddress, 0)) {
362362
return proto;
363363
}
364364

test/req.protocol.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ describe('req', function(){
7575
.get('/')
7676
.expect('http', done);
7777
})
78+
79+
describe('when trusting hop count', function () {
80+
it('should respect X-Forwarded-Proto', function (done) {
81+
var app = express();
82+
83+
app.set('trust proxy', 1);
84+
85+
app.use(function (req, res) {
86+
res.end(req.protocol);
87+
});
88+
89+
request(app)
90+
.get('/')
91+
.set('X-Forwarded-Proto', 'https')
92+
.expect('https', done);
93+
})
94+
})
7895
})
7996

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

test/req.secure.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,23 @@ describe('req', function(){
7878
.set('X-Forwarded-Proto', 'https, http')
7979
.expect('yes', done)
8080
})
81+
82+
describe('when "trust proxy" trusting hop count', function () {
83+
it('should respect X-Forwarded-Proto', function (done) {
84+
var app = express();
85+
86+
app.set('trust proxy', 1);
87+
88+
app.get('/', function (req, res) {
89+
res.send(req.secure ? 'yes' : 'no');
90+
});
91+
92+
request(app)
93+
.get('/')
94+
.set('X-Forwarded-Proto', 'https')
95+
.expect('yes', done)
96+
})
97+
})
8198
})
8299
})
83100
})

0 commit comments

Comments
 (0)