Giter Site home page Giter Site logo

Comments (2)

e2jk avatar e2jk commented on June 15, 2024

I just did it again, opening an issue without noticing #43 and #45.
However, reviewing the discussion, I still think a PR (I'll see if I can help with that) would make sense, to be able to use include_moment() just as the library is thought to be used.
I see the past discussion mentions the fact that this specific usage of eval seems unharmful (which I agree), but the problem is that if we want to use include_moment() and CSP, it means using unsafe-eval for all the scripts on the webpage, and thus potentially allowing other libraries/malicious scripts usage of eval()...

from flask-moment.

e2jk avatar e2jk commented on June 15, 2024

Looking a bit more at the code, it seems to me that this is solvable pretty easily.
Right now the code works by generating a <span> tag with a data-timestamp and a data-format attribute.
The data-timestamp is fine (and it's content can later be retrieved and passed to the moment() function without needing eval()), but the issue comes from the data-format attribute which contains both the function and the argument(s) that need to be performed on the Moment object.

It seems to me that by separating the content of what goes now in data-format we can get rid pretty easily of the call to eval(), these would be the changes to apply to the _render() Python function:

  • remove data-format
  • add a new data-function that would simply be the name of the function in string format (like "format", "fromNow" or "calendar")
  • add a new data-argument that would be the argument passed when the function is format, fromNow, or from (no need for arguments when the function is calendar(), valueOf() or unix())
  • one last data-from-timestamp when the function is "from", since the call to from needs a second attribute to indicate the first timestamp

Then in the JavaScript code that gets generated at the bottom of include_moment():

  • call moment($(elem).data('timestamp')) to get the Moment representation of the timestamp
  • and then a Switch statement based on $(elem).data('function') that call the appropriate Moment JavaScript function with the $(elem).data('argument') argument (and the extra second argument $(elem).data('from-timestamp') when the function is "from"

from flask-moment.

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.