Skip to content

Commit

Permalink
add support for multiple X-Forwarded-Proto values. Closes #1646
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 5, 2013
1 parent 0431d22 commit 8ab4408
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,10 @@ req.is = function(type){

req.__defineGetter__('protocol', function(){
var trustProxy = this.app.get('trust proxy');
return this.connection.encrypted
? 'https'
: trustProxy
? (this.get('X-Forwarded-Proto') || 'http')
: 'http';
if (this.connection.encrypted) return 'https';
if (!trustProxy) return 'http';
var proto = this.get('X-Forwarded-Proto') || 'http';
return proto.split(/\s*,\s*/)[0];
});

/**
Expand Down
30 changes: 30 additions & 0 deletions test/req.secure.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,36 @@ describe('req', function(){
.set('X-Forwarded-Proto', 'https')
.expect('yes', done)
})

it('should return false when initial proxy is http', function(done){
var app = express();

app.enable('trust proxy');

app.get('/', function(req, res){
res.send(req.secure ? 'yes' : 'no');
});

request(app)
.get('/')
.set('X-Forwarded-Proto', 'http, https')
.expect('no', done)
})

it('should return true when initial proxy is https', function(done){
var app = express();

app.enable('trust proxy');

app.get('/', function(req, res){
res.send(req.secure ? 'yes' : 'no');
});

request(app)
.get('/')
.set('X-Forwarded-Proto', 'https, http')
.expect('yes', done)
})
})
})
})

0 comments on commit 8ab4408

Please sign in to comment.