r/javascript • • Nov 27 '18

event-stream vulnerability explained

https://schneid.io/blog/event-stream-vulnerability-explained/
412 Upvotes

70 comments sorted by

View all comments

16

u/samjmckenzie Nov 27 '18

Why are minified libraries published onto npm? Is this standard practice?

11

u/schneidmaster Nov 27 '18

Sort of. It's a common practice from the days before module bundlers, where you would directly use the dependency in the browser via a `<script>` tag (or maybe concatenate it with grunt or whatever) and the size matters. Some folks still do this sort of thing via https://unpkg.com/. I think the closest thing to a "standard practice" is to ship a minified version for browsers and an unminified version for servers/module bundlers.

1

u/0xF013 Nov 28 '18

Also, the unminified version can be any transpilied version of JS or not even JS, so the consumer might not even be able to properly build it.

2

u/[deleted] Nov 28 '18

Many libraries published to npm are using some form of source transform like babel or typescript or flow, etc.

So for people to actually consume that module by source they need their own build process to do those same transforms, and then you'd need to have this be selective per every external module because no one build process is going to work for everything.

So generally you consume something that's been compiled and wrapped in UMD

1

u/samjmckenzie Nov 28 '18

TypeScript doesn't minify, though. It compiles to readable JavaScript.

1

u/[deleted] Nov 28 '18

I agree with this question. If I were to check the source code of every dependency in my node_modules and use a lock file, it would be great if I could feel confidant that this is the code I'm executing.

Having to read minified code to review dependencies is, well, stupid. This feel like a flaw in npm. I would prefer if npm never used minified code, but the source code instead. If that's not possible because of backwards compatibility, adding a flag like --no-minified could probably solve it.

8

u/schneidmaster Nov 28 '18

That would be pretty hard for npm to enforce -- minified javascript is just javascript with short variable names, no extraneous whitespace, etc. Who's npm to say whether e.g. a function with a 1-letter name is there because the code is minified, or just there because that's what someone happened to write?

7

u/[deleted] Nov 28 '18 edited Nov 28 '18

Yes, you're right, that's not possible. What I was thinking about was that there shouldn't be a difference between the code on github and the code published to npm. In that case the minified code should be uploaded to github, so that anyone that reviews the code on github should see the minified code. That's totally fine. The problem is that it's totally fine to upload something else to npm then github (minified code for instance).

That is what I meant with my previous comment.

2

u/NutsEverywhere Nov 28 '18

There's a difference between minification and variable mangling. Generally they are done together, but you can minify javascript without mangling, or mangle without minifying.