The one thing this article doesn't mention, which isn't as clever as the rest but really shows how well he planned this attack, is that after introducing flatmap-stream, he bumped event-stream to v3.3.6. All well and good. But a week later when he removed flatmap-stream, he bumped event-stream to v4.0.0.
So any investigation of event-stream wouldn't find flatmap-stream as a dependency at all, yet by bumping to 4.0.0, he ensured that any one of the millions of downloads would continue downloading the infected version, unless their package.json showed * or something similar. Since npm install by default would add ^3.3.whatever, it's super unlikely anyone would actually upgrade to 4.0 anytime soon, yet an audit of the package would look clean.
This attack was super clever for a variety of reasons, including this one.
As the article says, use a package-lock.json or yarn.lock, and set your package.json to strict semver.
package-lock.json and yarn.lock are for the developers of the module only. When consumed as a published module, installed via npm/yarn, these lock files are ignored.
Npm does respect a published npm-shrinkwrap.json. Yarn does not have a locking mechanism for transitive dependencies if published modules (they call this a feature).
Yeah, if everyone would use strict semver then this lock file issue would go away, but then we get other issues... like increasing the size of node_modules even more due to having multiple versions of the same modules in a package this risking opening a black hole here on Earth because of it 😆
As the article says, use a package-lock.json or yarn.lock, and set your package.json to strict semver.
This is like buying food from a bunch of random people, and then storing it all in your fridge and assuming that it's all healthy and fine because it's in your fridge and you don't go buy it every day.
This isn't the only thing you should do, it is a thing you should do. To use your analogy, it's the difference between putting it in the fridge and leaving it on the sidewalk.
108
u/blood_bender Nov 27 '18
The one thing this article doesn't mention, which isn't as clever as the rest but really shows how well he planned this attack, is that after introducing
flatmap-stream, he bumpedevent-streamtov3.3.6. All well and good. But a week later when he removedflatmap-stream, he bumpedevent-streamtov4.0.0.So any investigation of
event-streamwouldn't findflatmap-streamas a dependency at all, yet by bumping to4.0.0, he ensured that any one of the millions of downloads would continue downloading the infected version, unless theirpackage.jsonshowed*or something similar. Sincenpm installby default would add^3.3.whatever, it's super unlikely anyone would actually upgrade to 4.0 anytime soon, yet an audit of the package would look clean.This attack was super clever for a variety of reasons, including this one.
As the article says, use a
package-lock.jsonoryarn.lock, and set yourpackage.jsonto strict semver.