Skip to content
Myr

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

5 files+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 */89/**10 * Module dependencies.11 * @api private12 */1314var connect = require('connect')15 , Router = require('./router')
14 unmodified lines
3031var app = exports = module.exports = {};3233/**34 * Variable for trust proxy inheritance back-compat35 * @api private36 */3738var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';3940/**41 * Initialize the server.42 *43 * - setup default configuration44 * - setup default middleware
23 unmodified lines
68 this.set('env', env);69 this.set('subdomain offset', 2);70 this.set('trust proxy', false);7172 // trust proxy inherit back-compat73 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {74 configurable: true,75 value: true76 });7778 debug('booting in %s mode', env);7980 // implicit middleware81 this.use(connect.query());82 this.use(middleware.init(this));8362 // 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 }9192 // 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 lines
299 break;300 case 'trust proxy':301 debug('compile trust proxy %s', val);302 this.set('trust proxy fn', compileTrust(val));303304 // trust proxy inherit back-compat305 Object.defineProperty(this.settings, trustProxyDefaultSymbol, {306 configurable: true,307 value: false308 });309310 break;311 }312313 return this;
278 unmodified lines
modified:test/app.use.js+3 added-2 removed
modified:test/config.js+74 added-29 removed
12var express = require('../')3 , assert = require('assert');2var assert = require('assert');3var express = require('..');45describe('config', function(){6 describe('.set()', function(){7 it('should set a value', function(){5describe('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 })1212 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 })1718 it('should return the app when undefined', function () {19 var app = express();20 assert.equal(app.set('foo', undefined), app);21 })2223 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 })2829 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 })3637 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 })4647 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 })5859 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();6364 app.set('title', 'Express');65 app.use(blog);60 blog.get('title').should.equal('Express');66 assert.equal(blog.get('title'), 'Express');67 })62 6869 it('should given precedence to the child', function(){64 var app = express()65 , blog = express();70 var app = express();71 var blog = express();7273 app.use(blog);74 app.set('title', 'Express');75 blog.set('title', 'Some Blog');7671 blog.get('title').should.equal('Some Blog');77 assert.equal(blog.get('title'), 'Some Blog');78 })7980 it('should inherit "trust proxy" setting', function () {81 var app = express();82 var blog = express();8384 function fn() { return false }8586 app.set('trust proxy', fn);87 assert.equal(app.get('trust proxy'), fn);88 assert.equal(app.get('trust proxy fn'), fn);8990 app.use(blog);9192 assert.equal(blog.get('trust proxy'), fn);93 assert.equal(blog.get('trust proxy fn'), fn);94 })9596 it('should prefer child "trust proxy" setting', function () {97 var app = express();98 var blog = express();99100 function fn1() { return false }101 function fn2() { return true }102103 app.set('trust proxy', fn1);104 assert.equal(app.get('trust proxy'), fn1);105 assert.equal(app.get('trust proxy fn'), fn1);106107 blog.set('trust proxy', fn2);108 assert.equal(blog.get('trust proxy'), fn2);109 assert.equal(blog.get('trust proxy fn'), fn2);110111 app.use(blog);112113 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 })120121 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
modified:History.md+1 added
13.x2===34 * 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.0
1983 unmodified lines