Skip to content
Myr

Added "trust proxy" setting

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent e47d368 · Browse files · 4 files+25 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+25 added−16 removed

  • tests:+14 added−8 removed
  • other:+11 added−8 removed

4 files+25 added−16 removed

modified:lib/request.js+10 added-7 removed
281 unmodified lines
282 return !! ~ct.indexOf(type);283};284285/**286 * Return the protocol string "http" or "https",287 * while optionally trusting X-Forwarded-Proto288 * when running behind a reverse proxy.286 * Return the protocol string "http" or "https"287 * when requested with TLS. When the "trust proxy" 288 * setting is enabled the "X-Forwarded-Proto" header289 * field will be trusted. If you're running behind290 * a reverse proxy that supplies https for you this291 * may be enabled.292 *290 * @param {Boolean} trustProxy293 * @return {String}294 * @api public295 */296295req.protocol = function(trustProxy){297req.__defineGetter__('protocol', function(trustProxy){298 var trustProxy = this.app.settings['trust proxy'];299 return this.secure300 ? 'https'301 : trustProxy299 ? (this.header('X-Forwarded-Proto') || 'http')302 ? (this.get('X-Forwarded-Proto') || 'http')303 : 'http';301};304});305306/**307 * Short-hand for `req.connection.encrypted`.308 *
55 unmodified lines
modified:lib/response.js+1 added-1 removed
484 unmodified lines
485 }486487 // Absolute488 var host = req.header('Host');489 url = req.protocol(true) + '://' + host + url;489 url = req.protocol + '://' + host + url;490 }491492 // Support text/{plain,html} by default493 if (req.accepts('html')) {
83 unmodified lines
modified:test/req.protocol.js+11 added-7 removed
modified:test/res.redirect.js+3 added-1 removed
2 unmodified lines
3 , request = require('./support/http');45describe('res', function(){6 describe('.redirect(url)', function(){7 it('should respect X-Forwarded-Proto', function(done){7 it('should respect X-Forwarded-Proto when "trust proxy" is enabled', function(done){8 var app = express();910 app.enable('trust proxy');1112 app.use(function(req, res){13 res.redirect('/login');14 });15
241 unmodified lines