Skip to content
This repository has been archived by the owner on Sep 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #21 from yahoo/expose-filePath-for-secure-handlebars
Browse files Browse the repository at this point in the history
Expose filepath for secure-handlebars
  • Loading branch information
neraliu committed Jul 12, 2015
2 parents 3a6a9e3 + 915b2ce commit 4982bb6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-secure-handlebars",
"version": "2.0.3",
"version": "2.0.4",
"licenses": [
{
"type": "BSD",
Expand Down Expand Up @@ -38,8 +38,8 @@
"dependencies": {
"express-handlebars": "^2.0.1",
"handlebars": "^3.0.3",
"secure-handlebars": "^1.1.0",
"xss-filters": "^1.2.2"
"secure-handlebars": "^1.1.1",
"xss-filters": "^1.2.4"
},
"devDependencies": {
"expect.js": "^0.3.1",
Expand Down
9 changes: 9 additions & 0 deletions src/express-secure-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function ExpressSecureHandlebars(config) {
/* inheriting the express-handlebars */
util.inherits(ExpressSecureHandlebars, expressHandlebars);

/* override ExpressHandlebars.render() to expose filePath as compilerOptions */
ExpressSecureHandlebars.prototype.render = function (filePath, context, options) {
// expose filePath as processingFile in compilerOptions for secure-handlebars
this.compilerOptions || (this.compilerOptions = {});
this.compilerOptions.processingFile = filePath;

return expressHandlebars.prototype.render.call(this, filePath, context, options);
};

/* exporting the same signature of express-handlebars */
exports = module.exports = exphbs;
exports.create = create;
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/run-express-secure-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Authors: Nera Liu <neraliu@yahoo-inc.com>

require("mocha");
var expect = require('expect.js'),
path = require('path'),
expressHandlebars = require('express-handlebars'),
expressSecureHandlebars = require('../../src/express-secure-handlebars.js'),
handlebars = require('handlebars');
Expand Down Expand Up @@ -71,13 +72,22 @@ Authors: Nera Liu <neraliu@yahoo-inc.com>
expect(t1(data)).to.be.equal(t2(data));
});

it("handlebars compile test", function() {
it("handlebars compile test", function() {
var template = '<a href="{{url}}">closed</a>';
var t1 = expressSecureHandlebars.create().handlebars.compile(template);
var t2 = expressHandlebars.create().handlebars.compile(template);

expect(t1(data)).not.to.be.equal(t2(data));
});

it("handlebars getTemplate test", function() {
var templateFile = path.resolve("views/yd.hbs");
var expSecureHbs = expressSecureHandlebars.create();
expSecureHbs.render(templateFile);
expect(expSecureHbs.compilerOptions).to.be.ok();
expect(expSecureHbs.compilerOptions.processingFile).to.be.ok();
expect(expSecureHbs.compilerOptions.processingFile).to.be.match(/yd\.hbs/);
});
});

}());

0 comments on commit 4982bb6

Please sign in to comment.