GithubHelp home page GithubHelp logo

Comments (18)

tipiirai avatar tipiirai commented on May 17, 2024

My guess is that this is significantly slower because the template function is not compiled and cached. I maybe wrong. Would be nice to see benchmarks. Thanks!

from riot.

antonheryanto avatar antonheryanto commented on May 17, 2024

sorry if my issue not clear, but i just put the code as reference (as the only code that work i have now). actually i would like to request the riot.render version which not uses eval() or new Function() but has same functionality and acceptable performance. Thanks too for all great works has been done

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

I'm not sure if that's possible. The benefit of using Function construct is that these functions can be pre-compiled and reused on successive calls.

What is the actual concern here? You can use riot.render(template, data, true) to make automatic escaping and XSS prevention so that 3rd parties cannot execute unexpected javascript.

from riot.

antonheryanto avatar antonheryanto commented on May 17, 2024

we created the web application and chrome apps application targeting desktop and mobile. but chrome apps have restriction described here https://developer.chrome.com/extensions/contentSecurityPolicy so riot.render cannot be uses in chrome apps as it contains Function construct. so we need riot.render which not uses Function construct using option or separate implementation that can be uses to replace current riot.render in makefile. so the web version and chrome apps version shared same code based

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

Okay. That's a valid reason indeed. Maybe you can contribute this feature to the repository yourself?

from riot.

antonheryanto avatar antonheryanto commented on May 17, 2024

this is my rough riot.render implementation to handle CSP
it can be improved further as I'm still newbie in javascript world.
it pass render test, but lagging in performance in benchmark

Templating without HTML escaping...

riot x 65,759 ops/sec ±0.41% (100 runs sampled)
mustache x 89,176 ops/sec ±0.45% (98 runs sampled)
tmpl x 228,212 ops/sec ±0.44% (100 runs sampled)
underscore x 135,949 ops/sec ±0.55% (100 runs sampled)
ejs x 165,057 ops/sec ±0.28% (100 runs sampled)

Templating with HTML escaping...

riot x 32,433 ops/sec ±0.45% (100 runs sampled)
mustache x 34,667 ops/sec ±0.28% (100 runs sampled)
ejs x 43,559 ops/sec ±0.44% (101 runs sampled)

The question is, whether you willing to accept pull request for this CSP version ? my though it that we create another lib/render_csp.js which will be combined into riot.js using make csp options. or you have another way?

here the function

var FN = {}, // Precompiled templates (JavaScript functions)
  template_escape = {"\\": "\\\\", "\n": "\\n", "\r": "\\r", "'": "\\'"},
  render_escape = {'&': '&amp;', '"': '&quot;', '<': '&lt;', '>': '&gt;'};

function default_escape_fn(str, key) {
  return str == undefined ? '' : (str+'').replace(/[&\"<>]/g, function(char) {
    return render_escape[char];
  });
}

riot.render = function(tmpl, data, escape_fn) {
  if (escape_fn === true) escape_fn = default_escape_fn;
  tmpl = tmpl || '';

  return tmpl.replace(/{\s*([\w\.]+)\s*}/g, function(k, v) { 
    var p = v.split(".");
    var t = data[p[0]];
    if (t === undefined || t === null) return '';
    for (var i = 1; i < p.length; i++) t = t[p[i]];
    return (escape_fn ? escape_fn(t, v) : t || (t === undefined || t === null ? '': t));
  });
};

from riot.

kaustavdm avatar kaustavdm commented on May 17, 2024

I've been trying to use riot for Firefox OS apps, and hit the same issue. Content Security Policy blocks calls to Function(). I'll see if I can patch it up, or if @antonheryanto's patch works.

from riot.

kaustavdm avatar kaustavdm commented on May 17, 2024

@antonheryanto You just need to replace:

    var t = data[p[0]];

with

    var t = data ? data[p[0]] : null;

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

Hey all,

Nice catch! Can anyone make a pull request about the above change?

from riot.

kaustavdm avatar kaustavdm commented on May 17, 2024

@tipiirai The changes above work, but they slow down riot.render considerably. I'll see if I can patch up something better.

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

I think you can patch that directly without spending too much time on that. Riot 2.0 will be released soon with a new templating engine. More details later. Unfortunately I cannot give you ETA.

from riot.

kaustavdm avatar kaustavdm commented on May 17, 2024

@tipiirai Ah, that's nice. In that case, I should let @antonheryanto do the honour 👍

from riot.

antonheryanto avatar antonheryanto commented on May 17, 2024

thank for review and analysis @kaustavdm, to be save will wait for Riot 2.0 hope with more modular build so we can add and drop extension like CSP template engine, complete route and other. for now i just uses my minimal framework https://github.com/antonheryanto/riotjs-stack based on riotjs-admin which uses riotjs raw lib, add and drop it for production uses.

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

Perhaps you saw already that Riot 2.0 was released yesterday. Please poke around the new docs:

https://muut.com/riotjs/

I'm actually closing this one because Riot 2.0 made this issue obsolete. It's not based on templates anymore but custom tags instead.

I hope you like it.

from riot.

kaustavdm avatar kaustavdm commented on May 17, 2024

👍

from riot.

steelbrain avatar steelbrain commented on May 17, 2024

The new Function() thingy is back in master, and I am unable to use riot in my atom/atom's package because of this.
https://github.com/muut/riotjs/blob/master/riot.js#L237

from riot.

JariInc avatar JariInc commented on May 17, 2024

I'm also affected by this. In a Chrome App you can define a file to be sandboxed to circumvent CSP, but then you can't make any CORS requests which makes it unusable.

Can this be reopened or do I make a new issue?

from riot.

tipiirai avatar tipiirai commented on May 17, 2024

New issue would be clearer. Thanks

from riot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.