question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Precompile jsrender templates to avoid unsafe-eval

See original GitHub issue

For implementing our Content Security Policy I would like to avoid any code that uses eval() or new Function, so that I don’t have to add unsafe-eval to the CSP. However, JsRender uses new Function to compile templates.

Is there a way to avoid this or work around it? Is it on the JsRender roadmap?

Note: I was thinking of solving this by precompiling our templates on the server (something like http://handlebarsjs.com/precompilation.html), so that I only need to render on the client. When I examine the code, it looks like I could make it work by serializing the compiled template (i.e., the result of calling compileTmpl), including all its subtemplates, to a JSON-string on the server, putting that in a <script> tag and then use that to render.

One of the issues I’m running into with this approach, is that the compiled templates also contain a reference to a render function, which is internal to JsRender. I would have to expose it to be able to use it on the client, I think.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
build3dpartscommented, Nov 23, 2022

Hi, I know this issue is closed but we need to have JSRender work with precompiled version of templates in a CSP environment with no eval() or new Function(). Adding this to a future release will help our project a lot.

0reactions
BorisMoorecommented, May 28, 2022

No plan for the moment. (Although we may re-consider this in the future…). See also the discussion here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Safety of disabling unsafe-eval 2 seconds after page load ...
I'm currently working on a project which requires unsafe-eval at startup due to the use of lodash 's _.template function. After startup eval ......
Read more >
JsRender Quickstart
JsRender is used for data-driven rendering of templates to strings, ready for insertion in the DOM. It is also used by the JsViews...
Read more >
107538 - extensions doen't load for some valid CSP schemes.
'unsafe-inline' and 'unsafe-eval' are blocked for script-src. This sucks. Lots of JS libraries compile templates into JS functions using eval and "new ...
Read more >
Template Injection: JsRender/JsViews - AppCheck
In this blog post we will explore Template Injection attacks against the JsRender/JsViews library, a successor to jQuery Templates .
Read more >
Vuejs - Alexander Mikhailian
Vue.js render child component to variable to be used within component ... policy to allow unsafe-eval or pre-compiling your templates into render functions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found