Ignore Object.prototype values in settings through app.set/app.get
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #4802 closes #4803
authored by Evan Hahn · committed by Douglas Christopher Wilson · parent 87279c0 · Browse files · 3 files·+63 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+63 added−1 removed
- tests:+44 added−0 removed
- other:+19 added−1 removed
Provenance
- Commit
c17fe05- Tree
24b6549- Author
Evan Hahn <me@evanhahn.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
87279c0
3 files changed+63 added−1 removed
modified:lib/application.js+18 added-1 removed
27 unmodified lines28 var flatten = require('array-flatten');29 var merge = require('utils-merge');30 var resolve = require('path').resolve;31 var setPrototypeOf = require('setprototypeof')32+33+/**34+ * Module variables.35+ * @private36+ */37+38+var hasOwnProperty = Object.prototype.hasOwnProperty39 var slice = Array.prototype.slice;40 41 /**42 * Application prototype.315 unmodified lines358 359 app.set = function set(setting, val) {360 if (arguments.length === 1) {361 // app.get(setting)355- return this.settings[setting];362+ var settings = this.settings363+364+ while (settings && settings !== Object.prototype) {365+ if (hasOwnProperty.call(settings, setting)) {366+ return settings[setting]367+ }368+369+ settings = Object.getPrototypeOf(settings)370+ }371+372+ return undefined373 }374 375 debug('set "%s" to %o', setting, val);376 285 unmodified linesmodified:test/config.js+44 added
9 unmodified lines10111213141516171819202122232 unmodified lines2627282930313233343536373839404142434422 unmodified lines6768697071727374757677787966 unmodified lines1461471481491501511521531541551561571581591601611621631641651661671681691701711721735 unmodified lines1791801811821831841851861871881891901915 unmodified lines1971981992002012022032042052062079 unmodified lines app.set('foo', 'bar'); assert.equal(app.get('foo'), 'bar'); })
it('should set prototype values', function () { var app = express() app.set('hasOwnProperty', 42) assert.strictEqual(app.get('hasOwnProperty'), 42) })
it('should return the app', function () { var app = express(); assert.equal(app.set('foo', 'bar'), app); })2 unmodified lines var app = express(); assert.equal(app.set('foo', undefined), app); })
it('should return set value', function () { var app = express() app.set('foo', 'bar') assert.strictEqual(app.set('foo'), 'bar') })
it('should return undefined for prototype values', function () { var app = express() assert.strictEqual(app.set('hasOwnProperty'), undefined) })
describe('"etag"', function(){ it('should throw on bad value', function(){ var app = express(); assert.throws(app.set.bind(app, 'etag', 42), /unknown value/);22 unmodified lines var app = express(); assert.strictEqual(app.get('foo'), undefined); })
it('should return undefined for prototype values', function () { var app = express() assert.strictEqual(app.get('hasOwnProperty'), undefined) })
it('should otherwise return the value', function(){ var app = express(); app.set('foo', 'bar'); assert.equal(app.get('foo'), 'bar');66 unmodified lines var app = express(); assert.equal(app.enable('tobi'), app); assert.strictEqual(app.get('tobi'), true); })
it('should set prototype values', function () { var app = express() app.enable('hasOwnProperty') assert.strictEqual(app.get('hasOwnProperty'), true) }) })
describe('.disable()', function(){ it('should set the value to false', function(){ var app = express(); assert.equal(app.disable('tobi'), app); assert.strictEqual(app.get('tobi'), false); })
it('should set prototype values', function () { var app = express() app.disable('hasOwnProperty') assert.strictEqual(app.get('hasOwnProperty'), false) }) })
describe('.enabled()', function(){ it('should default to false', function(){5 unmodified lines var app = express(); app.set('foo', 'bar'); assert.strictEqual(app.enabled('foo'), true); })
it('should default to false for prototype values', function () { var app = express() assert.strictEqual(app.enabled('hasOwnProperty'), false) }) })
describe('.disabled()', function(){ it('should default to true', function(){5 unmodified lines var app = express(); app.set('foo', 'bar'); assert.strictEqual(app.disabled('foo'), false); })
it('should default to true for prototype values', function () { var app = express() assert.strictEqual(app.disabled('hasOwnProperty'), true) }) })})modified:History.md+1 added
1 unreleased2 ==========3 4+ * Ignore `Object.prototype` values in settings through `app.set`/`app.get`5 * Support proper 205 responses using `res.send`6 7 4.17.3 / 2022-02-168 ===================3523 unmodified lines