151 points · by CapacitorSet · 9y ago · 29 comments
>If, anyway, you want to make math.eval resistant against arbitrary code execution, I think it would be best to have a whitelist of methods and constructs (i.e. you parse the code that is meant to be evaluated and ensure that every construct is allowed). I analyze JS malware in my free time (see [box-js](https://github.com/CapacitorSet/box-js)), and I found that it is virtually impossible to blacklist functions. For instance, if the parser forbids `[].map.constructor`, I could very well use `[].map["constructor"]`; and if you blacklist the word "constructor", I could use `[].map["rotcurtsnoc".split("").reverse().join("")]`, and so on, there's an infinity of methods one can come up with to avoid blacklists.
The examples didn't really work in math.js, but it turns out that there's still [quite a few ways to get around it](https://github.com/josdejong/mathjs/issues/821).
browsers do have isolated contexts these days, they're just cumbersome to use.
1. spawn an iframe with sandbox="allow-script" and srcdoc="...<meta http-equiv="Content-Security-Policy" ...><script>...</script>..."
2. use window.postMessage to communicate between the parent page and the iframe running in a null origin
3. send code to eval into that iframeHowever, the trick is to make Javascript execute Function, through a function that math.js won't mind executing. What I found was simply using Function.apply and Function.call; the author found Function.bind, and someone in this thread found several more.
An unmatched left parenthesis creates an unresolved tension that will stay with you all day.
Math.js is available as a RESTful web service: http://api.mathjs.orgI was hoping vm to offer you an isolated v8 interpreter without bindings that could used as a sandbox, but this wasn't the case.