6 ms·
Lave: eval in reverse
- javajosh 11y agoThis is very interesting. It is a serialization format generalized to handle real references, including circular ones, and more complex data-types than JSON can handle. I think it's a very poor tagline, since "eval in reverse" sounds useless, and this lib is anything but. (no affliation btw) The down-side is that a consumer has to run a full-blown eval() (as opposed to the more restricted JSON.parse()). This isn't that much of a downside in a typical webapp since you have full control over the browser process anyway, but it's deadly for cross-domain. The upside is considerable for certain data-structures that are hard to represent in JSON efficiently, e.g. with a lot of denormalization. A key concern for me is runtime efficiency, particularly compared to JSON.stringify.
- personjerry 11y agoYeah, the "eval in reverse" tagline and the cuteness of its name ("lave") made me think it was something for amusement.
- jedschmidt 11y agoMy original HN submission was clearer, but alas didn't go anywhere: https://news.ycombinator.com/item?id=11185365 https://news.ycombinator.com/item?id=11185365
- jedschmidt 11y agoAbsolutely. The downside of running a full-blown eval() exists in JSON too, it's just that the eval() there is taking place in the reviver function, which requires a lot more coordination than a self-contained file. As far as efficiency, I'd think for most uses the issue would be on the JSON.parse end. In this case, lave might be more efficient, since JSON reviving often ends up creating temporary objects that need to GC'ed after reification.
- javajosh 11y agoInteresting. I'd like to whip up some simple benchmarks tomorrow, and I'll share what I find. Also, it would be interesting to support other data-types, such as the ones that come with Immutable.js. That would be slick.
- abecedarius 11y agoIt's not a new idea (not to knock the OP). http://wiki.erights.org/wiki/Safe_Serialization_Under_Mutual_Suspicion/Introducing_Data_E#Unserialization_as_Evaluation http://wiki.erights.org/wiki/Safe_Serialization_Under_Mutual... calls it uneval instead of lave, and attributes it to Jonathan Rees. See http://wiki.erights.org/wiki/Safe_Serialization_Under_Mutual_Suspicion/Abstract_and_Introduction http://wiki.erights.org/wiki/Safe_Serialization_Under_Mutual... for more on the security issues -- this was all for the E language, but I guess most of it could carry over to modern Javascript. One aspect that wouldn't: in E when you resolve a promise it becomes (http://gbracha.blogspot.com/2009/07/miracle-of-become.html http://gbracha.blogspot.com/2009/07/miracle-of-become.html) the resolution. Data-E uses this to express reference cycles.
- bprosnitz 11y agoA few weeks ago, I created something similar so I could reproduce a failure. In case it is useful: https://gist.github.com/bprosnitz/cf1d1a3dd1008eef5a85 https://gist.github.com/bprosnitz/cf1d1a3dd1008eef5a85
- bcook 11y agoWow. You should comment more often. You seem useful.
- csense 11y agolave(';)"dlrow olleh"(trela');
- intronic 11y agoLike print, if you have a lisp.
- junke 11y agoIf you use only structs, lists, numbers and strings, print is perfect. It supports circular structure when PRINT-CIRCLE is true. For custom classes you have to define custom printing methods, because, well, how you print a particular object readably has no general answer: for example, for some data (streams, threads), it makes no sense to serialize them. Java has the "transient" keyword to avoid serializing some fields in classes. You should also set PRINT-READABLY to T so that you have an error if an object cannot be printed readably. Also, Common Lisp has to serialize values into FASL files when compiling them, but it can do it only for its own types. You can use LOAD-TIME-VALUE to have a form evaluated at load time, and MAKE-LOAD-FORM is a generic function that you can specialize to print a form which, when read, allocates and initializes objects at load-time. For your own classes, it is sufficient to use the helper function MAKE-LOAD-FORM-SAVING-SLOTS. All that gives you a lot of control over what, how and when forms are evaluated and stored. The good news is that you have libraries available to do that very easily: http://www.cliki.net/serialization http://www.cliki.net/serialization (see for example http://www.cliki.net/cl-marshal http://www.cliki.net/cl-marshal). See http://clhs.lisp.se/ http://clhs.lisp.se/
- wtbob 11y agoAnd Lisp even offers good ways for most structure (even circular structures!) to read data in without enabling generic code execution.
- njohnson41 11y agoSounds a lot like python's "pickle" module (which is super-useful for prototyping), but with the same achilles' heel: all of your serialized objects can now run arbitrary code when you deserialize them!
- jedschmidt 11y agoNot _quite_ arbitrary; the only code run is that generated by lave. Arbitrary code present in functions is parsed, but not run.
- conradev 11y agoBut if there is a persistence or network layer involved, when compromised, it could function as an injection vector into the application, right?
- jedschmidt 11y agoSure, as it could with any part of your app, including wherever your JSON.parse code lives.
- conradev 11y agoBut could JSON.parse() fed with malicious data fire off an XMLHttpRequest or delete all of your data?
- mwhite 11y agoIt seems like it theoretically shouldn't be too hard to add the ability to validate that the data is a valid lave output if you're concerned about that. That more or less leaves only the issue of anonymous functions in the data being replaced with malicious functions, but frankly the only reason to be serializing functions is if they're user input, otherwise you should instead be serializing function name/key strings or some other well-defined form of function references and/or arguments.
- dclowd9901 11y agoHas everyone creates one of these? https://github.com/dclowd9901/betterObjectToString https://github.com/dclowd9901/betterObjectToString Yours is clearly better.
- kvakvs 11y agoA first planet in Elite back on ZX Spectrum. I think the planet is so popular so it is recreated in new versions of Elite up to this day. http://elite-dangerous.wikia.com/wiki/Lave http://elite-dangerous.wikia.com/wiki/Lave
- pacmanche 11y agoLave means to make in Danish
- rmchugh 11y agomake or do, as in "hvad laver du?" -> "What are you doing?"
- cgriswald 11y agoIn Spanish, it's the first person subjunctive form of "to wash". So you'd use it to express something like "It's good that I am washing myself" or "Maybe I am washing it."
- omaranto 11y agoI don't know what subjunctive means so I don't know if you are right about that, but I am a native Spanish speaker, so I can tell you that those examples you gave using "washing" (a gerund, I think) would use the corresponding tense "lavando" in Spanish: It's good that I am washing myself = Es bueno que me esté lavando. (That's a literal translation: I don't think anyone would use "lavar" for washing yourself, more likely "bañando" [bathing] or "aseando" [cleaning] --at least not in Mexico, but what's idiomatic does vary quite a bit from country to country. Although for "wash your face" you would say "lávate la cara", using the verb "lavar".) I am washing it = Lo estoy lavando. Here are some sentences where I would use "lave" or "lavé": Dile que lo lave mañana= Tell him/her to wash it tomorrow Lavé las cortinas con agua caliente = I washed the curtains in hot water.
- JadeNB 11y ago> In Spanish, it's the first person subjunctive form of "to wash". It also means "to wash" in English. http://www.google.com/search?q=lave http://www.google.com/search?q=lave
- joosters 11y agoIt's Perl's Data::Dumper ! Some of the options present in the perl version could be useful in the JavaScript too, e.g. pretty-printing structures for debugging output.
- laumars 11y agoData::Dumper has proven invaluable during those "what the fsck is this routine doing" moments.
- joosters 11y agoYes, it's a fantastic aid, I don't know what I'd do without it when trying to debug code dealing with nasty mixes of arrays and hashes. What would be fantastic for perl is actually something that javascript already has. In most browsers, if you print a data structure to the console, it is presented as a clickable tree structure, where you can show or hide the bits of the data that you want to check. I'd love some way to recreate this in perl (and in a terminal if possible). The downside of Data::Dumper is that you can produce hundreds of pages of output, when you really want to just examine a tiny part of it.
- perlancarb 11y agohttps://metacpan.org/pod/Data::Dumper::GUI https://metacpan.org/pod/Data::Dumper::GUI AFAIK, no one has written a terminal version yet. There's a TUI framework called Tickit which would make writing one quite easy, I think.
- kazinator 11y agoMore like security hole in disguise! If code-to-be-evaluated represents data, you have to trust it or else very carefully sandbox it. The real fix is to have a printed notation that has a richer type system and handles circularity. (Printed notation is code, effectively; it's just code in a distinct language of hopefully limited power, just for constructing objects from a description.)