Fix "trust proxy" setting to inherit when app is mounted
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fixes #2550 fixes #2551
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent eaf3318 · Browse files · 5 files·+133 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+133 added−33 removed
- tests:+94 added−31 removed
- other:+39 added−2 removed
Provenance
- Commit
b40e74d- Tree
f8ab34a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
eaf3318
5 files changed+133 added−33 removed
modified:lib/application.js+38 added-2 removed
1+/*!2+ * express3+ * Copyright(c) 2009-2013 TJ Holowaychuk4+ * Copyright(c) 2013 Roman Shtylman5+ * Copyright(c) 2014-2015 Douglas Christopher Wilson6+ * MIT Licensed7+ */8+9 /**10 * Module dependencies.11+ * @api private12 */13 14 var connect = require('connect')15 , Router = require('./router')14 unmodified lines30 31 var app = exports = module.exports = {};32 33 /**34+ * Variable for trust proxy inheritance back-compat35+ * @api private36+ */37+38+var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';39+40+/**41 * Initialize the server.42 *43 * - setup default configuration44 * - setup default middleware23 unmodified lines68 this.set('env', env);69 this.set('subdomain offset', 2);70 this.set('trust proxy', false);71 72+ // trust proxy inherit back-compat73+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {74+ configurable: true,75+ value: true76+ });77+78 debug('booting in %s mode', env);79 80 // implicit middleware81 this.use(connect.query());82 this.use(middleware.init(this));83 62- // inherit protos63- this.on('mount', function(parent){84+ this.on('mount', function onmount(parent) {85+ // inherit trust proxy86+ if (this.settings[trustProxyDefaultSymbol] === true87+ && typeof parent.settings['trust proxy fn'] === 'function') {88+ delete this.settings['trust proxy'];89+ delete this.settings['trust proxy fn'];90+ }91+92+ // inherit protos93 this.request.__proto__ = parent.request;94 this.response.__proto__ = parent.response;95 this.engines.__proto__ = parent.engines;96 this.settings.__proto__ = parent.settings;202 unmodified lines299 break;300 case 'trust proxy':301 debug('compile trust proxy %s', val);302 this.set('trust proxy fn', compileTrust(val));303+304+ // trust proxy inherit back-compat305+ Object.defineProperty(this.settings, trustProxyDefaultSymbol, {306+ configurable: true,307+ value: false308+ });309+310 break;311 }312 313 return this;278 unmodified linesmodified:test/app.use.js+3 added-2 removed
122334567860 unmodified lines
var assert = require('assert');var express = require('../') , request = require('supertest');var express = require('..');var request = require('supertest');
describe('app', function(){ it('should emit "mount" when mounted', function(done){ var blog = express()60 unmodified linesmodified:test/config.js+74 added-29 removed
1 2-var express = require('../')3- , assert = require('assert');2+var assert = require('assert');3+var express = require('..');4 5-describe('config', function(){6- describe('.set()', function(){7- it('should set a value', function(){5+describe('config', function () {6+ describe('.set()', function () {7+ it('should set a value', function () {8 var app = express();9- app.set('foo', 'bar').should.equal(app);9+ app.set('foo', 'bar');10+ assert.equal(app.get('foo'), 'bar');11 })12 12- it('should return the app when undefined', function(){13+ it('should return the app', function () {14 var app = express();14- app.set('foo', undefined).should.equal(app);15+ assert.equal(app.set('foo', 'bar'), app);16 })17 18+ it('should return the app when undefined', function () {19+ var app = express();20+ assert.equal(app.set('foo', undefined), app);21+ })22+23 describe('"etag"', function(){24 it('should throw on bad value', function(){19- var app = express()20- app.set.bind(app, 'etag', 42).should.throw(/unknown value/)25+ var app = express();26+ assert.throws(app.set.bind(app, 'etag', 42), /unknown value/);27 })28 29 it('should set "etag fn"', function(){30 var app = express()31 var fn = function(){}32 app.set('etag', fn)27- app.get('etag fn').should.equal(fn)33+ assert.equal(app.get('etag fn'), fn)34 })35 })36 37 describe('"trust proxy"', function(){38 it('should set "trust proxy fn"', function(){39 var app = express()40 var fn = function(){}41 app.set('trust proxy', fn)36- app.get('trust proxy fn').should.equal(fn)42+ assert.equal(app.get('trust proxy fn'), fn)43 })44 })45 })46 47 describe('.get()', function(){48 it('should return undefined when unset', function(){49 var app = express();44- assert(undefined === app.get('foo'));50+ assert.strictEqual(app.get('foo'), undefined);51 })52 53 it('should otherwise return the value', function(){54 var app = express();55 app.set('foo', 'bar');50- app.get('foo').should.equal('bar');56+ assert.equal(app.get('foo'), 'bar');57 })58 59 describe('when mounted', function(){60 it('should default to the parent app', function(){55- var app = express()56- , blog = express();61+ var app = express();62+ var blog = express();63 64 app.set('title', 'Express');65 app.use(blog);60- blog.get('title').should.equal('Express');66+ assert.equal(blog.get('title'), 'Express');67 })62- 68+69 it('should given precedence to the child', function(){64- var app = express()65- , blog = express();70+ var app = express();71+ var blog = express();72 73 app.use(blog);74 app.set('title', 'Express');75 blog.set('title', 'Some Blog');76 71- blog.get('title').should.equal('Some Blog');77+ assert.equal(blog.get('title'), 'Some Blog');78 })79+80+ it('should inherit "trust proxy" setting', function () {81+ var app = express();82+ var blog = express();83+84+ function fn() { return false }85+86+ app.set('trust proxy', fn);87+ assert.equal(app.get('trust proxy'), fn);88+ assert.equal(app.get('trust proxy fn'), fn);89+90+ app.use(blog);91+92+ assert.equal(blog.get('trust proxy'), fn);93+ assert.equal(blog.get('trust proxy fn'), fn);94+ })95+96+ it('should prefer child "trust proxy" setting', function () {97+ var app = express();98+ var blog = express();99+100+ function fn1() { return false }101+ function fn2() { return true }102+103+ app.set('trust proxy', fn1);104+ assert.equal(app.get('trust proxy'), fn1);105+ assert.equal(app.get('trust proxy fn'), fn1);106+107+ blog.set('trust proxy', fn2);108+ assert.equal(blog.get('trust proxy'), fn2);109+ assert.equal(blog.get('trust proxy fn'), fn2);110+111+ app.use(blog);112+113+ assert.equal(app.get('trust proxy'), fn1);114+ assert.equal(app.get('trust proxy fn'), fn1);115+ assert.equal(blog.get('trust proxy'), fn2);116+ assert.equal(blog.get('trust proxy fn'), fn2);117+ })118 })119 })120 121 describe('.enable()', function(){122 it('should set the value to true', function(){123 var app = express();79- app.enable('tobi').should.equal(app);80- app.get('tobi').should.be.true;124+ assert.equal(app.enable('tobi'), app);125+ assert.strictEqual(app.get('tobi'), true);126 })127 })128 129 describe('.disable()', function(){130 it('should set the value to false', function(){131 var app = express();87- app.disable('tobi').should.equal(app);88- app.get('tobi').should.be.false;132+ assert.equal(app.disable('tobi'), app);133+ assert.strictEqual(app.get('tobi'), false);134 })135 })136 137 describe('.enabled()', function(){138 it('should default to false', function(){139 var app = express();95- app.enabled('foo').should.be.false;140+ assert.strictEqual(app.enabled('foo'), false);141 })142 143 it('should return true when set', function(){144 var app = express();145 app.set('foo', 'bar');101- app.enabled('foo').should.be.true;146+ assert.strictEqual(app.enabled('foo'), true);147 })148 })149 150 describe('.disabled()', function(){151 it('should default to true', function(){152 var app = express();108- app.disabled('foo').should.be.true;153+ assert.strictEqual(app.disabled('foo'), true);154 })155 156 it('should return false when set', function(){157 var app = express();158 app.set('foo', 'bar');114- app.disabled('foo').should.be.false;159+ assert.strictEqual(app.disabled('foo'), false);160 })161 })162 })modified:test/req.ip.js+17 added
33 unmodified lines3435363738394041424344454647484950515253545556575839 unmodified lines33 unmodified lines .get('/') .set('X-Forwarded-For', 'client, p1, p2') .expect('p1', done); })
it('should return the addr after trusted proxy, from sub app', function (done) { var app = express(); var sub = express();
app.set('trust proxy', 2); app.use(sub);
sub.use(function (req, res, next) { res.send(req.ip); });
request(app) .get('/') .set('X-Forwarded-For', 'client, p1, p2') .expect(200, 'p1', done); }) })
describe('when "trust proxy" is disabled', function(){ it('should return the remote address', function(done){39 unmodified linesmodified:History.md+1 added
1 3.x2 ===3 4+ * Fix `"trust proxy"` setting to inherit when app is mounted5 * Generate `ETag`s for all request responses6 - No longer restricted to only responses for `GET` and `HEAD` requests7 * Use `content-type` to parse `Content-Type` headers8 * deps: connect@2.29.01983 unmodified lines