20 ms·
Security concerns with minified JavaScript code
- jimminy 11y agoAll of the interesting content is in the link provided in the list[1], and otherwise this is just a post about how debian should handle the security issues presented there. This might be a good candidate for having the link updated. [1]: https://zyan.scripts.mit.edu/blog/backdooring-js/ https://zyan.scripts.mit.edu/blog/backdooring-js/
- boklm 11y agoThis link has already been posted: https://news.ycombinator.com/item?id=10107833 https://news.ycombinator.com/item?id=10107833
- 0942v8653 11y ago(As of right now, 202 points and 34 comments)
- TazeTSchnitzel 11y agoThat article was already on HN. This post is discussing the Debian reaction to it.
- personjerry 11y agoThe moral of the story, it seems, is to beware any tool that modifies or writes your code for you, even something as seemingly harmless as a JS minifier.
- mtgx 11y agoWouldn't that include compilers, too?
- robinhouston 11y agoYes indeed: http://blog.regehr.org/archives/1241 http://blog.regehr.org/archives/1241
- federico3 11y ago...and that's why the Debian community is driving reproducible builds: https://reproducible.debian.net/reproducible.html https://reproducible.debian.net/reproducible.html
- pascal_cuoq 11y agoReproducible builds do not solve the problem of a compiler bug being used to introduce a backdoor during the translation from source code to binary.
- kuschku 11y agoYes, they do – you take a C compiler (written in ASM) that is just good enough to compile TCC, now you prove that the source of TCC and GCC is right, you compile TCC with your simple compiler, then compile GCC with TCC, and you have a compiler which is guaranteed to be free of such backdoors. Now, you compile your code with this compiler, and if your own build reproducibly has the same results as this verified compiler, you're safe.
- AgentME 11y agoThat defends you from a different type of attack. That can't fix bugs in your compiler or minifier. See https://zyan.scripts.mit.edu/blog/backdooring-js/ https://zyan.scripts.mit.edu/blog/backdooring-js/
- kuschku 11y agoYour attack only applies if my minifier is wrong. My solution even protects me from attacks where the source of the minifier is safe, and only the minified minifier contains the bug.
- kpcyrd 11y agoThe quoted link discusses backdooring js by writing legit code that turns malicious through minifier bugs. The only way to be sure is not minifying the source.
- gipsies 11y agoOr by using a trusted minifier on the source code yourself, as mentioned in the link.
- kpcyrd 11y agoHow do you tell your trusted minifier has no exploitable bugs?
- icebraining 11y agoExploitable by what?
- TheLoneWolfling 11y agoAnyone who has ever pushed a patch to the project that you're running through the minifier.
- icebraining 11y agoWhat would be the point of exploiting the minifier? The purpose is to get the code to the Debian users, how does exploiting the minifier help with that? If you can put an exploit is the program's source, you just want it to be minified as-is so that it'll run on the end user machines.
- adrianN 11y agoIt might be a lot harder to spot the exploit during review if it only works in conjunction with a minifier bug.
- cbr 11y agoLooks like Debian is mostly on top of it: > To me the problem suggests that it is important from a security and > accountability perspective to 1) include the human-readable source > code of JavaScript in Debian packages, and 2) to compile the > human-readable source code into a minified code (if required) during > package builds, using a JS-minifier that is included in Debian. > Thoughts? This is anyway mandatory in Debian. https://lists.debian.org/debian-devel/2015/08/msg00478.html https://lists.debian.org/debian-devel/2015/08/msg00478.html
- zamalek 11y ago> Looks like Debian is mostly on top of it I wonder... I'm assuming their concern surrounds stuff that is installed on-disk with the package manager. Is there any benefit at all to JS minification when the file:// protocol is in use? Marginal (if at all) improvements to parse time? Anything else? Or is "everyone doing it?"
- teamhappy 11y agoUglifyJS also evaluates constant expressions and that kind of thing. So you get a couple of basic optimizations.
- jon-wood 11y agoApt can be used to install web applications to be hosted from a Debian based server, so just because you're installing a package on a machine doesn't mean the files it installs won't be accessed remotely.
- nteon 11y agoV8 uses a function's length in characters, _including comments_, to make optimization decisions. By removing comments and shortening var identifiers internal to a function, I can imagine a number of functions dropping under that 600-char threshold. Would be great to measure.
- 0942v8653 11y ago
- caf 11y agoIt's hard to see how shipping minified JavaScript in the source packages passed muster with Debian in the first place. If it's not the human-readable source that's in the preferred form for modifications, it's not really the source at all.
- skarap 11y agoI like how strict Fedora Packaging Policy is regarding this (https://fedoraproject.org/wiki/Packaging:JavaScript https://fedoraproject.org/wiki/Packaging:JavaScript). "If a JavaScript library typically is shipped as minified or compiled code, it must be compiled or minified as part of the RPM build process. Shipping pre-minified or pre-compiled code is unacceptable in Fedora."
- vbernat 11y agoThey say that but this is not always the case. See for example Roundcube. They just shipped minified JS as shipped by upstream. In Debian, pre-minification JS files are shipped and minified during the build process. http://pkgs.fedoraproject.org/cgit/roundcubemail.git/tree/roundcubemail.spec http://pkgs.fedoraproject.org/cgit/roundcubemail.git/tree/ro...
- prolepunk 11y agoOne of the messages in the thread links to this debian wiki article that lays out all the arguments against minification. https://wiki.debian.org/onlyjob/no-minification https://wiki.debian.org/onlyjob/no-minification One argument that really drives it home -- GZIP compression without minification is faster and better, and from my own experience takes exactly two lines of nginx config to set up. Is there really any benefit of using minification besides code obfuscation?
- cwyers 11y agoDefine "faster." GZIP creates a smaller file size, but the benefit of minification is faster parsing by the Javascript engine -- GZIP does nothing to help with this.
- ape4 11y agoMaybe not always. Minifiers optimize for space. So they might turn "false" into "!1". Not sure that executes faster.
- jhgg 11y agoUglifyJS does a lot of dead code elimination. Lots of stuff that only runs on dev environments isn't even included in the production build. Stuff like: if (process.env.NODE_ENV !== 'production') { invariant(x, y); console.info(z); }
- BinaryIdiot 11y agoSo yeah they optimize for space but they also optimize for execution. For example the closure minifier literally puts everything on one line, without semicolons (where possible) and uses commas which forces the JavaScript engine to execute it as a single statement which can be faster. That's just a minor point but it can help with speed. Is it enough of an optimization that it's worth it though? I'm not sure; I haven't seen much in the way of benchmarks in this area.
- wyldfire 11y agoIs the AST structure preserved when minifying? If only identifier names and whitespace changed, then it seems like one could verify that the minified matches the original.
- prezjordan 11y agoNot always, most do tricks like `!0` for `true` and `a && b` instead of `if (a) b`.
- userbinator 11y agoIMHO that's going into optimiser territory, and perhaps the reason why these minifier bugs exist - the minifier writers need to be very sure of the semantics of the language before attempting to rewrite code to have the same behaviour, and JS/ECMAscript semantics are not trivial especially w.r.t the (highly) dynamic type system as the example shows. Even renaming things is not as trivial as it seems because the names can themselves be dynamic, e.g. a.somefield vs a['s' + 'ome' + 'field']. (I'm not a JS developer so I might've missed a few other things, but minifying JS is definitely a much harder problem than doing it with something like C.)
- EGreg 11y agojust one nitpick on your comment - minifiers do not replace property names, only variable names in JS
- comex 11y agoClosure Compiler does rewrite property names in advanced mode.
- Arnavion 11y agoUglifyJS also rewrites property names if you tell it to. It's conservative so it only replaces the names you explicitly tell it to rename. Recently it gained the ability to let you give a regex, so if you have a convention of always prefixing private rename-safe properties with an underscore, you can now have those renamed.
- mapgrep 11y agoJavascript minification seems like a classic case of premature optimization. I'm not a front-end developer so maybe I'm missing something, but wouldn't simple, transparent gzip compression via your webserver — one line in a config file — get you something like ~90% of the benefits of minification, for free? I'm sure there are some additional gains to be made from knowing the syntactical structure of the language you're minifying, since no generic compression scheme can know that. But that seems really marginal — and, as the article in question shows, opens up potential security vulns.
- vcarl 11y agoPart of the benefit of minifying is obfuscating the code, the only way to strip out all meaning is to have a minifier that understands the syntax.
- antris 11y agoI'm a front-end developer and you are absolutely right.
- moreInfo1234 11y agoI've worked on embedded systems with tight resource constraints. Minification was used on those systems because the space savings resulted in performance improvements. Deminification would be less of a security concern than potential issues being introduced by minification.
- jordigh 11y agoThis is why LibreJS is an important project. https://www.gnu.org/software/librejs/ https://www.gnu.org/software/librejs/ It won't help towards minification bugs or backdooring, but it's a step in the right direction.
- dahart 11y agoThe whole point of the back-dooring js blog post was that you can create "deniable" bugs that are hard if not impossible to see by inspecting the unminified source, and can't prove intention. They already have great reasons to require source, so it's ironic this would be the catalyst to start, no?
- fixermark 11y agoConceptually, this is a reasonable line to draw in the sand. Minified Javascript should be thought of as equivalent to assembly language or Java bytecode[1], and if the policy is to not ship compiled binaries without source available, minified JS should fall into that category. [1] https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript https://www.destroyallsoftware.com/talks/the-birth-and-death... is funny, but the honest truth is that it's likely the future of web technologies, as Javascript is so deeply entrenched in the fiber of the web standards and the practical implementation of the browsers.
- eastbayjake 11y agoThis thread on V8 inlining was buried at the bottom of the blog post linked in this thread, but it was super valuable and is changing the way I write functions in Node this morning: https://news.ycombinator.com/item?id=10108672 https://news.ycombinator.com/item?id=10108672
- nickpsecurity 11y agoDid anyone else get a kick out of seeing these two in a row on the homepage? Closure compiler parses JavaScript code, removes dead code, minimizes what's left Security concerns with minified JavaScript code One of those moments where I pause to wonder if I should even read further haha.