Skip to content

Commit

Permalink
Merge 51114bc into 0ddc097
Browse files Browse the repository at this point in the history
  • Loading branch information
jmkucerak committed Apr 19, 2020
2 parents 0ddc097 + 51114bc commit 883c75b
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 33 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Expand Up @@ -21,3 +21,4 @@ rules:
no-return-assign: 2
no-use-before-define: [ 2, nofunc ]
no-path-concat: 2
no-useless-escape: 0
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -32,3 +32,4 @@ node_modules

# Minified bundle
doT.min.js
package-lock.json
9 changes: 4 additions & 5 deletions .travis.yml
@@ -1,9 +1,8 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "6"
- "7"
- "8"
- "10"
- "12"
- "13"
after_script:
- coveralls < coverage/lcov.info
45 changes: 36 additions & 9 deletions README.md
Expand Up @@ -9,6 +9,17 @@ doT.js is fast, small and has no dependencies.
[![Coverage Status](http://coveralls.io/repos/github/olado/doT/badge.svg?branch=master)](https://coveralls.io/github/olado/doT?branch=master)


## Note from the maintainer

doT is a really solid piece of software engineering (I didn’t create it) that is rarely updated exactly for this reason.

It took me years to grasp how it works even though it’s only 140 lines of code - it looks like magic.

I used it in my other projects (e.g. [ajv](https://github.com/epoberezkin/ajv)) as the smallest, the fastest and the most functional (all three!) templating engine ever made, that is particularly useful in all code generation scenarios where manipulating AST is an overkill.

It’s a race car of templating engines - doT lacks bells and whistles that other templating engines have, but it allows to achive more than any other, if you use it right (YMMV).


## Features
custom delimiters
runtime evaluation
Expand All @@ -28,17 +39,21 @@ http://olado.github.com/doT (todo: update docs with new features added in versio

## New in version 1.0.0

####Added parameters support in partials
#### Added parameters support in partials

{{##def.macro:param:
<div>{{=param.foo}}</div>
#}}
```html
{{##def.macro:param:
<div>{{=param.foo}}</div>
#}}

{{#def.macro:myvariable}}
{{#def.macro:myvariable}}
```

####Node module now supports auto-compilation of dot templates from specified path
#### Node module now supports auto-compilation of dot templates from specified path

var dots = require("dot").process({ path: "./views"});
```js
var dots = require("dot").process({ path: "./views"});
```

This will compile .def, .dot, .jst files found under the specified path.
Details
Expand All @@ -53,7 +68,7 @@ Details
compiled into separate functions and are available via _render.filename.definename

Basic usage:
```
```js
var dots = require("dot").process({path: "./views"});
dots.mytemplate({foo:"hello world"});
```
Expand All @@ -64,7 +79,7 @@ Details
* Return an object with functions compiled from .dot templates as its properties
* Render mytemplate template

####CLI tool to compile dot templates into js files
#### CLI tool to compile dot templates into js files

./bin/dot-packer -s examples/views -d out/views

Expand All @@ -77,6 +92,18 @@ Details
doU.js is here only so that legacy external tests do not break. Use doT.js.
doT.js with doT.templateSettings.append=false provides the same performance as doU.js.

## Security considerations

doT allows arbitrary JavaScript code in templates, making it one of the most flexible and powerful templating engines. It means that doT security model assumes that you only use trusted templates and you don't use any user input as any part of the template, as otherwise it can lead to code injection.

It is strongly recommended to compile all templates to JS code as early as possible. Possible options:

- using doT as dev-dependency only and compiling templates to JS files, for example, as described above or using a custom script, during the build. This is the most performant and secure approach and it is strongly recommended.
- if the above approach is not possible for some reason (e.g. templates are dynamically generated using some run-time data), it is recommended to compile templates to in-memory functions during application start phase, before any external input is processed.
- compiling templates lazily, on demand, is less safe. Even though the possibility of the code injection via prototype pollution was patched (#291), there may be some other unknown vulnerabilities that could lead to code injection.

Please report any found vulnerabilities to npm, not via issue tracker.

## Author
Laura Doktorova [@olado](http://twitter.com/olado)

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/jslitmus.js
Expand Up @@ -466,7 +466,7 @@

var tests = [], // test store (all tests added w/ jslitmus.test())
queue = [], // test queue (to be run)
currentTest; // currently runnning test
currentTest; // currently running test

// jslitmus gets EventEmitter API
EventEmitter.call(jslitmus);
Expand Down
18 changes: 9 additions & 9 deletions doT.js
Expand Up @@ -9,15 +9,15 @@
name: "doT",
version: "1.1.1",
templateSettings: {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
evaluate: /\{\{-?([\s\S]+?(\}?)+)-?\}\}/g,
interpolate: /\{\{-?=([\s\S]+?)-?\}\}/g,
encode: /\{\{-?!([\s\S]+?)-?\}\}/g,
use: /\{\{-?#([\s\S]+?)-?\}\}/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
define: /\{\{-?##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#-?\}\}/g,
defineParams:/^\s*([\w$]+):([\s\S]+)/,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
conditional: /\{\{-?\?(\?)?\s*([\s\S]*?)\s*-?\}\}/g,
iterate: /\{\{-?~\s*(?:-?\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*-?\}\})/g,
varname: "it",
strip: true,
append: true,
Expand Down Expand Up @@ -95,7 +95,8 @@
str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;

str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g," ")
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""): str)
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g,""): str.replace(/([\s]+)({{-)/g, "$2")
.replace(/(-}})([\s])/g, "$1"))
.replace(/'|\\/g, "\\$&")
.replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
Expand All @@ -121,7 +122,6 @@
+ "';return out;")
.replace(/\n/g, "\\n").replace(/\t/g, '\\t').replace(/\r/g, "\\r")
.replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, "");
//.replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');

if (needhtmlencode) {
if (!c.selfcontained && _globals && !_globals._encodeHTML) _globals._encodeHTML = doT.encodeHTMLSource(c.doNotSkipEncoded);
Expand Down
8 changes: 7 additions & 1 deletion index.js
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 All @@ -58,6 +58,12 @@ InstallDots.prototype.compileToFile = function(path, template, def) {
, fn;

for (var property in defs) {
// It looks like the code block inside "if" below can never be executed,
// because InstallDots constructor is private, compileToFile is only called from compileAll method
// and def parameter is never passed to it, so the condition in if will always fail.
// This code will be removed from the next major version.
// For now it is only excluded from coverage report
/* istanbul ignore if */
if (defs[property] !== def[property] && defs[property] !== this.__includes[property]) {
fn = undefined;
if (typeof defs[property] === 'string') {
Expand Down
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -21,21 +21,22 @@
"license": "MIT",
"scripts": {
"eslint": "if-node-version '>=4' eslint *.js --ignore-pattern *.min.js",
"test-cov": "nyc mocha test/*.test.js",
"test-spec": "mocha test/*.test.js",
"test-cov": "nyc npm run test-spec",
"test": "npm run eslint && npm run test-cov",
"bundle": "uglifyjs doT.js -o doT.min.js -c -m --preamble '/* Laura Doktorova https://github.com/olado/doT */'",
"prepublish": "npm run bundle"
},
"dependencies": {},
"devDependencies": {
"commander": "*",
"coveralls": "^2.11.14",
"eslint": "^3.9.1",
"if-node-version": "^1.1.0",
"coveralls": "^3.0.9",
"eslint": "^6.7.2",
"if-node-version": "^1.1.1",
"jshint": "*",
"mkdirp": "*",
"mocha": "*",
"nyc": "^8.3.2",
"nyc": "^14.1.1",
"pre-commit": "^1.1.3",
"uglify-js": "*"
},
Expand Down
3 changes: 1 addition & 2 deletions test/defines.test.js
@@ -1,7 +1,6 @@
'use strict';

var test = require('./util').test;
var doT = require('../doT');
var doT = require('..');
var assert = require('assert');

describe('defines', function() {
Expand Down
2 changes: 1 addition & 1 deletion test/dot.test.js
Expand Up @@ -2,7 +2,7 @@

var test = require('./util').test;
var assert = require("assert")
var doT = require("../doT");
var doT = require("..");


describe('doT', function(){
Expand Down
59 changes: 59 additions & 0 deletions test/process.test.js
@@ -0,0 +1,59 @@
'use strict';

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


describe('doT.process', function() {
beforeEach(function() {
removeCompiledTemplateFiles();
});

afterEach(function() {
removeCompiledTemplateFiles();
});

function removeCompiledTemplateFiles() {
try { fs.unlinkSync('./test/templates/test.js'); } catch(e) {}
}

it('should compile all templates in folder', function() {
const templates = doT.process({path: './test/templates'});
var str = templates.test({data: 2});
assert.equal(str, '21');

var js = fs.statSync('./test/templates/test.js');
assert.ok(js.isFile());

// code below passes if the test is run without coverage using `npm run test-spec`
// because source code of doT.encodeHTMLSource is used inside compiled templates

// var fn = require('./templates/test.js');
// var str = fn({data: 2});
// assert.equal(str, '21');
});


it('should ignore varname with polluted 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/templates'});
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/templates/test.def
@@ -0,0 +1 @@
1
2 changes: 2 additions & 0 deletions test/templates/test.dot
@@ -0,0 +1,2 @@
{{=it && it.data}}
{{#def.test}}
2 changes: 2 additions & 0 deletions test/templates/test.jst
@@ -0,0 +1,2 @@
{{=it && it.data}}
{{#def.test}}

0 comments on commit 883c75b

Please sign in to comment.