Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ defineGetter(req, 'host', function host(){
} else if (val.indexOf(',') !== -1) {
// Note: X-Forwarded-Host is normally only ever a
// single value, but this is to be safe.
val = val.substring(0, val.indexOf(',')).trimRight()
val = val.substring(0, val.indexOf(',')).trim()
} else {
val = val.trim()
}

return val || undefined;
Expand Down
16 changes: 16 additions & 0 deletions test/req.host.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ describe('req', function(){
.expect('example.com', done);
})

it('should trim whitespace in X-Forwarded-Host', function(done){
var app = express();

app.enable('trust proxy');

app.use(function(req, res){
res.end(req.host);
});

request(app)
.get('/')
.set('Host', 'localhost')
.set('X-Forwarded-Host', ' example.com , malicious.example.com')
.expect('example.com', done);
})

it('should ignore X-Forwarded-Host if socket addr not trusted', function(done){
var app = express();

Expand Down