Skip to content

Commit a2348c8

Browse files
committed
Use object with null prototype for various app properties
`app.cache`, `app.engines`, and `app.settings` are now created with `Object.create(null)` instead of `{}`. This also adds a test that `app.locals` is created the same way.
1 parent 6faf26d commit a2348c8

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/application.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ var trustProxyDefaultSymbol = '@@symbol:trust_proxy_default';
5454
app.init = function init() {
5555
var router = null;
5656

57-
this.cache = {};
58-
this.engines = {};
59-
this.settings = {};
57+
this.cache = Object.create(null);
58+
this.engines = Object.create(null);
59+
this.settings = Object.create(null);
6060

6161
this.defaultConfiguration();
6262

test/app.locals.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11

2+
var assert = require('assert');
23
var express = require('../')
34

45
describe('app', function(){
56
describe('.locals(obj)', function(){
67
it('should merge locals', function(){
78
var app = express();
8-
Object.keys(app.locals).should.eql(['settings']);
9+
assert.deepStrictEqual(Object.keys(app.locals), ['settings']);
910
app.locals.user = 'tobi';
1011
app.locals.age = 2;
11-
Object.keys(app.locals).should.eql(['settings', 'user', 'age']);
12-
app.locals.user.should.equal('tobi');
13-
app.locals.age.should.equal(2);
12+
assert.deepStrictEqual(Object.keys(app.locals), ['settings', 'user', 'age']);
13+
assert.strictEqual(app.locals.user, 'tobi');
14+
assert.strictEqual(app.locals.age, 2);
15+
})
16+
17+
it('is an object with no prototype', function(){
18+
var app = express();
19+
assert.strictEqual(Object.getPrototypeOf(app.locals), null);
1420
})
1521
})
1622

@@ -19,8 +25,8 @@ describe('app', function(){
1925
var app = express();
2026
app.set('title', 'House of Manny');
2127
var obj = app.locals.settings;
22-
obj.should.have.property('env', 'test');
23-
obj.should.have.property('title', 'House of Manny');
28+
assert.strictEqual(obj.env, 'test');
29+
assert.strictEqual(obj.title, 'House of Manny');
2430
})
2531
})
2632
})

0 commit comments

Comments
 (0)