Skip to content

Commit

Permalink
fix: prevent possibility of execution of the code injected via protot…
Browse files Browse the repository at this point in the history
…ype pollution when undefined is passed to compiled template function, closes #291
  • Loading branch information
epoberezkin committed Dec 7, 2019
1 parent 299b4da commit 2cf2226
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function InstallDots(o) {
if (this.__destination[this.__destination.length-1] !== '/') this.__destination += '/';
this.__global = o.global || "window.render";
this.__rendermodule = o.rendermodule || {};
this.__settings = o.templateSettings ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
this.__settings = Object.prototype.hasOwnProperty.call(o,"templateSettings") ? copy(o.templateSettings, copy(doT.templateSettings)) : undefined;
this.__includes = {};
}

Expand Down
31 changes: 31 additions & 0 deletions test/process.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

var assert = require('assert');
var doT = require('..');


describe('doT.process', function() {
describe('polluting object prototype should not affect template compilation', function() {
it('should ignore varname on object prototype', function() {
var currentLog = console.log;
console.log = log;
var logged;

Object.prototype.templateSettings = {varname: 'it=(console.log("executed"),{})'};

try {
const templates = doT.process({path: './test'});
assert.notEqual(logged, 'executed');
// injected code can only be executed if undefined is passed to template function
templates.test();
assert.notEqual(logged, 'executed');
} finally {
console.log = currentLog;
}

function log(str) {
logged = str;
}
})
});
});
1 change: 1 addition & 0 deletions test/test.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{=it && it.test}}

0 comments on commit 2cf2226

Please sign in to comment.