Skip to content

Commit 2ddae30

Browse files
EvanHahndougwilson
authored andcommitted
Ignore Object.prototype values in settings through app.set/app.get
closes #4802 closes #4803
1 parent 628c524 commit 2ddae30

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
unreleased
22
==========
33

4+
* Ignore `Object.prototype` values in settings through `app.set`/`app.get`
45
* Support proper 205 responses using `res.send`
56

67
4.17.3 / 2022-02-16

lib/application.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ var flatten = require('array-flatten');
2929
var merge = require('utils-merge');
3030
var resolve = require('path').resolve;
3131
var setPrototypeOf = require('setprototypeof')
32+
33+
/**
34+
* Module variables.
35+
* @private
36+
*/
37+
38+
var hasOwnProperty = Object.prototype.hasOwnProperty
3239
var slice = Array.prototype.slice;
3340

3441
/**
@@ -352,7 +359,17 @@ app.param = function param(name, fn) {
352359
app.set = function set(setting, val) {
353360
if (arguments.length === 1) {
354361
// app.get(setting)
355-
return this.settings[setting];
362+
var settings = this.settings
363+
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 undefined
356373
}
357374

358375
debug('set "%s" to %o', setting, val);

test/config.js

+44
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ describe('config', function () {
1111
assert.equal(app.get('foo'), 'bar');
1212
})
1313

14+
it('should set prototype values', function () {
15+
var app = express()
16+
app.set('hasOwnProperty', 42)
17+
assert.strictEqual(app.get('hasOwnProperty'), 42)
18+
})
19+
1420
it('should return the app', function () {
1521
var app = express();
1622
assert.equal(app.set('foo', 'bar'), app);
@@ -21,6 +27,17 @@ describe('config', function () {
2127
assert.equal(app.set('foo', undefined), app);
2228
})
2329

30+
it('should return set value', function () {
31+
var app = express()
32+
app.set('foo', 'bar')
33+
assert.strictEqual(app.set('foo'), 'bar')
34+
})
35+
36+
it('should return undefined for prototype values', function () {
37+
var app = express()
38+
assert.strictEqual(app.set('hasOwnProperty'), undefined)
39+
})
40+
2441
describe('"etag"', function(){
2542
it('should throw on bad value', function(){
2643
var app = express();
@@ -51,6 +68,11 @@ describe('config', function () {
5168
assert.strictEqual(app.get('foo'), undefined);
5269
})
5370

71+
it('should return undefined for prototype values', function () {
72+
var app = express()
73+
assert.strictEqual(app.get('hasOwnProperty'), undefined)
74+
})
75+
5476
it('should otherwise return the value', function(){
5577
var app = express();
5678
app.set('foo', 'bar');
@@ -125,6 +147,12 @@ describe('config', function () {
125147
assert.equal(app.enable('tobi'), app);
126148
assert.strictEqual(app.get('tobi'), true);
127149
})
150+
151+
it('should set prototype values', function () {
152+
var app = express()
153+
app.enable('hasOwnProperty')
154+
assert.strictEqual(app.get('hasOwnProperty'), true)
155+
})
128156
})
129157

130158
describe('.disable()', function(){
@@ -133,6 +161,12 @@ describe('config', function () {
133161
assert.equal(app.disable('tobi'), app);
134162
assert.strictEqual(app.get('tobi'), false);
135163
})
164+
165+
it('should set prototype values', function () {
166+
var app = express()
167+
app.disable('hasOwnProperty')
168+
assert.strictEqual(app.get('hasOwnProperty'), false)
169+
})
136170
})
137171

138172
describe('.enabled()', function(){
@@ -146,6 +180,11 @@ describe('config', function () {
146180
app.set('foo', 'bar');
147181
assert.strictEqual(app.enabled('foo'), true);
148182
})
183+
184+
it('should default to false for prototype values', function () {
185+
var app = express()
186+
assert.strictEqual(app.enabled('hasOwnProperty'), false)
187+
})
149188
})
150189

151190
describe('.disabled()', function(){
@@ -159,5 +198,10 @@ describe('config', function () {
159198
app.set('foo', 'bar');
160199
assert.strictEqual(app.disabled('foo'), false);
161200
})
201+
202+
it('should default to true for prototype values', function () {
203+
var app = express()
204+
assert.strictEqual(app.disabled('hasOwnProperty'), true)
205+
})
162206
})
163207
})

0 commit comments

Comments
 (0)