Skip to content

Commit 2cf2226

Browse files
committed
fix: prevent possibility of execution of the code injected via prototype pollution when undefined is passed to compiled template function, closes #291
1 parent 299b4da commit 2cf2226

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function InstallDots(o) {
4242
if (this.__destination[this.__destination.length-1] !== '/') this.__destination += '/';
4343
this.__global = o.global || "window.render";
4444
this.__rendermodule = o.rendermodule || {};
45-
this.__settings = o.templateSettings ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
45+
this.__settings = Object.prototype.hasOwnProperty.call(o,"templateSettings") ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
4646
this.__includes = {};
4747
}
4848

test/process.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var doT = require('..');
5+
6+
7+
describe('doT.process', function() {
8+
describe('polluting object prototype should not affect template compilation', function() {
9+
it('should ignore varname on object prototype', function() {
10+
var currentLog = console.log;
11+
console.log = log;
12+
var logged;
13+
14+
Object.prototype.templateSettings = {varname: 'it=(console.log("executed"),{})'};
15+
16+
try {
17+
const templates = doT.process({path: './test'});
18+
assert.notEqual(logged, 'executed');
19+
// injected code can only be executed if undefined is passed to template function
20+
templates.test();
21+
assert.notEqual(logged, 'executed');
22+
} finally {
23+
console.log = currentLog;
24+
}
25+
26+
function log(str) {
27+
logged = str;
28+
}
29+
})
30+
});
31+
});

test/test.dot

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{=it && it.test}}

0 commit comments

Comments
 (0)