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
Provenance
- Commit
4d87efc- Tree
e191395- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
e47d368
4 files changed+25 added−16 removed
modified:lib/request.js+10 added-7 removed
281 unmodified lines282 return !! ~ct.indexOf(type);283 };284 285 /**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 */296 295-req.protocol = function(trustProxy){297+req.__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+});305 306 /**307 * Short-hand for `req.connection.encrypted`.308 *55 unmodified linesmodified:lib/response.js+1 added-1 removed
484 unmodified lines485 }486 487 // Absolute488 var host = req.header('Host');489- url = req.protocol(true) + '://' + host + url;489+ url = req.protocol + '://' + host + url;490 }491 492 // Support text/{plain,html} by default493 if (req.accepts('html')) {83 unmodified linesmodified:test/req.protocol.js+11 added-7 removed
1234566789101111121314152 unmodified lines1819202122222324252627282729303132336 unmodified lines404142434445464347484950513 unmodified lines555657585559606162636064656667689 unmodified lines
var express = require('../') , request = require('./support/http');
describe('req', function(){ describe('.protocol(trustProxy)', function(){ describe('.protocol', function(){ it('should return the protocol string', function(done){ var app = express();
app.use(function(req, res){ res.end(req.protocol()); res.end(req.protocol); });
request(app) .get('/')2 unmodified lines done(); }) })
describe('when trustProxy is true', function(){ describe('when "trust proxy" is enabled', function(){ it('should respect X-Forwarded-Proto', function(done){ var app = express();
app.enable('trust proxy');
app.use(function(req, res){ res.end(req.protocol(true)); res.end(req.protocol); });
request(app) .get('/')6 unmodified lines
it('should default to http', function(done){ var app = express();
app.enable('trust proxy');
app.use(function(req, res){ res.end(req.protocol(true)); res.end(req.protocol); });
request(app) .get('/')3 unmodified lines }) }) })
describe('when trustProxy is false', function(){ describe('when "trust proxy" is disabled', function(){ it('should ignore X-Forwarded-Proto', function(done){ var app = express();
app.use(function(req, res){ res.end(req.protocol()); res.end(req.protocol); });
request(app) .get('/')9 unmodified linesmodified:test/res.redirect.js+3 added-1 removed
2 unmodified lines3 , request = require('./support/http');4 5 describe('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();9 10+ app.enable('trust proxy');11+12 app.use(function(req, res){13 res.redirect('/login');14 });15 241 unmodified lines