Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: strf-9432 Replace eval with vm.runInContext #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const vm = require('vm');
const HandlebarsV3 = require('handlebars');
const HandlebarsV4 = require('@bigcommerce/handlebars-v4');
const helpers = require('./helpers');
Expand Down Expand Up @@ -185,7 +186,8 @@ class HandlebarsRenderer {
*/
addTemplates(templates) {
const paths = Object.keys(templates);

const context = { template: {} };
vm.createContext(context);
for (let i = 0; i < paths.length; i++) {
const path = paths[i];

Expand All @@ -195,7 +197,7 @@ class HandlebarsRenderer {

try {
// Check if it is a precompiled template
const template = this._tryRestoringPrecompiled(templates[path]);
const template = this._tryRestoringPrecompiled(context, templates[path]);

// Register it with handlebars
this.handlebars.registerPartial(path, template);
Expand All @@ -205,7 +207,7 @@ class HandlebarsRenderer {
}
};

_tryRestoringPrecompiled(precompiled) {
_tryRestoringPrecompiled(context, precompiled) {
// Let's analyze the string to make sure it at least looks
// something like a handlebars precompiled template. It should
// be a string representation of an object containing a `main`
Expand All @@ -220,12 +222,11 @@ class HandlebarsRenderer {

// We need to take the string representation and turn it into a
// valid JavaScript object. eval is evil, but necessary in this case.
let template;
eval(`template = ${precompiled}`);
vm.runInContext(`template = ${precompiled}`, context);

// Take the precompiled object and get the actual function out of it,
// after first testing for runtime version compatibility.
return this.handlebars.template(template);
return this.handlebars.template(context.template);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bigcommerce/stencil-paper-handlebars",
"version": "5.9.4",
"version": "5.9.5-beta",
"description": "A paper plugin to render pages using Handlebars.js",
"main": "index.js",
"author": "Bigcommerce",
Expand Down