Supply chain26 Nov 2018

event-stream npm Backdoor

A stranger offered to maintain a popular npm package, added a hidden dependency, and targeted one Bitcoin wallet app's users.

Severity
Status page turned red
Difficulty
intermediate
Companies
npm, Inc., Copay (BitPay)
Tech
npm, JavaScript, Open-source maintenance, Cryptocurrency wallets
Root cause
poisoned dependency, social engineering

The story

Quick (1 min)

In November 2018, developers discovered that event-stream, a widely used npm package, had quietly started pulling in a malicious dependency.

How did that happen? The original author was no longer maintaining it. A newcomer offered to take over and was given control. In September 2018, the new maintainer added a small, unknown package called flatmap-stream. One version of it contained hidden, encrypted code.

The code only activated inside the build process of one specific app: Copay, a Bitcoin wallet. There, it inserted code to steal private keys from wallets holding large balances (over 100 Bitcoin). Copay confirmed malicious versions 5.0.2 to 5.1.0 were released to users.

The lesson: the people behind your dependencies are part of your security. A friendly handover can be an attack.

Keep reading to the end to earn XP.Read. XP earned.

Engineer (2 min)

The takeover (social engineering). npm: "This attack started out as a social engineering attack. The attacker, posing as a maintainer, took over maintainership of the event-stream module." In the GitHub issue that broke the story, a commenter asked why the new maintainer, right9ctrl, had been given access.

The poisoned dependency. On 9 September 2018, the new maintainer added flatmap-stream as a direct dependency in event-stream 3.3.6. Its version 0.1.1 contained the payload: present in the minified published code but not the readable source, which made casual review useless.

Targeted, encrypted payload (npm's analysis):

  1. Read AES-encrypted data from a file disguised as a test fixture.
  2. Used the npm package description of the importing module (from an automatically set environment variable) as the decryption key. It only decrypted correctly inside Copay's build.
  3. Compiled and ran the decrypted module in memory, which wrote the final malicious code into a dependency bundled by Copay's release build.
  4. In the shipped app (mobile, Cordova or Electron), checked balances and, above 100 BTC or 1,000 BCH, harvested account data and private keys and sent them to a collection server.

Why most users were unaffected. "Running the payload in any other environment has no effect." Only Copay's build environment produced the right key.

Response. npm was notified on 26 November 2018, removed flatmap-stream and event-stream 3.3.6, and took ownership of event-stream. BitPay advised users of Copay 5.0.2–5.1.0 not to open the app.

Interview takeaway: watch for maintainer changes and new dependencies in updates, compare published packages with source, and pin dependencies with lockfiles and integrity hashes.

Keep reading to the end to earn XP.Read. XP earned.

Deep dive (2 min)

Timeline (npm, GitHub issue #116)

  • Before September 2018: event-stream's original author hands maintainership to a new contributor who offered to help.
  • 9 September 2018: the new maintainer adds flatmap-stream as a direct dependency in event-stream 3.3.6.
  • Later: flatmap-stream 0.1.1, with the injected payload, is the version pulled in. A commenter on the GitHub issue notes the package had essentially zero downloads before event-stream used it.
  • Copay releases 5.0.2 through 5.1.0 ship the malicious code (Copay's confirmation via npm).
  • 20–26 November 2018: the GitHub issue "I don't know what to say." gathers analysis; a contributor identifies Copay as the target.
  • 26 November 2018: npm Security is notified, removes flatmap-stream and event-stream@3.3.6, and takes ownership of event-stream.
  • 27 November 2018: npm publishes its incident analysis.

Technical cleverness

  • Hiding in minified code: the readable source on GitHub didn't contain the payload; the published package did.
  • Environment-keyed decryption: using the importing package's description as the AES key meant automated sandboxes running the code elsewhere saw nothing.
  • Fake test fixture: encrypted payload data was stored in a file that looked like test data.
  • Code transformed from invalid to valid JavaScript at runtime, which npm believed was to evade dynamic analysis.

The human side

Open-source maintainers are often unpaid volunteers. Handing a project to an enthusiastic newcomer is common and usually harmless, which is exactly why it works as an attack. The case raised long-running questions about maintainer burnout and how ownership transfers should be vetted.

npm's advice

Run npm audit to check for the vulnerable dependency and update to a later version.

Keep reading to the end to earn XP.Read. XP earned.

Failure replay

Step by step, what broke and what it hit next.

  1. Before Sep 2018
    event-stream maintainerdegraded

    The original author, no longer maintaining event-stream, hands control to a friendly newcomer.

    Affects: event-stream package

  2. Sep 9, 2018
    event-stream packagecompromised

    The new maintainer adds flatmap-stream as a dependency in version 3.3.6.

    Affects: flatmap-stream 0.1.1

  3. Autumn 2018
    flatmap-stream 0.1.1compromised

    Its published minified code hides an AES-encrypted payload in a fake test fixture. The GitHub source looks clean.

    Affects: Copay build pipeline

  4. Autumn 2018
    Copay build pipelinecompromised

    Only in Copay's build does the decryption key match. The payload injects key-stealing code into the app bundle.

    Affects: Copay wallet users

  5. v5.0.2–5.1.0
    Copay wallet userscompromised

    For wallets over 100 BTC or 1,000 BCH, the app sends account data and private keys to the attacker's server.

  6. Nov 20–26, 2018
    Open-source communityrecovering

    Developers investigate in a GitHub issue and identify Copay as the target.

    Affects: event-stream package

  7. Nov 26, 2018
    event-stream packagerecovering

    npm removes the malicious versions and takes ownership of event-stream. BitPay warns Copay users.

Impact

Who was affected
Users of Copay wallet versions 5.0.2–5.1.0 with large balances were targeted; millions of event-stream installs pulled the malicious dependency but the payload only activated in Copay's build.
How long
Malicious dependency added 9 September 2018; removed 26 November 2018 (about 11 weeks).
Cost
not disclosed

Lessons

  • Maintainer changes are a security event; vet new owners of critical packages.
  • Published packages can differ from public source; verify what you install.
  • Targeted, encrypted payloads evade generic scanning; review dependency changes, not just CVEs.
  • Pin dependencies with lockfiles and review new transitive dependencies in updates.

Quiz

  1. How did the attacker gain control of event-stream?

    Check answer

    By offering to maintain it and being given access by the original author. npm called it a social engineering attack in which the attacker posed as a maintainer.

  2. What was the malicious dependency?

    Check answer

    flatmap-stream version 0.1.1. It was added to event-stream 3.3.6 on 9 September 2018.

  3. Why did the payload do nothing on most developers' machines?

    Check answer

    Its decryption key was the package description of Copay, so it only decrypted inside Copay's build. The environment-specific key meant the code only worked in the targeted build.

  4. What did the injected code try to steal?

    Check answer

    Private keys and account data from Copay wallets with large balances. It targeted wallets with more than 100 BTC or 1,000 BCH.

Interview prep

Try answering out loud before you open the model answer.

  1. security

    How did the event-stream attack stay hidden from most developers and automated tools?

    Show model answer

    Several techniques: the payload was in the published minified package but not the GitHub source; it was AES-encrypted and stored in a file disguised as a test fixture; the decryption key was the npm description of the importing package, so it only decrypted inside Copay's build; and it transformed code at runtime to evade dynamic analysis. Everywhere else it did nothing. Defences: diff published artifacts against source, flag new dependencies and maintainer changes, use provenance attestations, and review updates to critical dependencies.

    A strong answer hits:

    • Minified-only payload
    • Encrypted, environment-keyed decryption
    • Fake test fixture
    • Artifact vs source diffing and provenance
  2. system design

    Design a process for safely updating third-party dependencies in a security-sensitive application like a crypto wallet.

    Show model answer

    Pin exact versions with lockfiles and integrity hashes. Use an internal registry proxy. Treat every dependency update as a code change: review changelogs, diffs of published packages, newly added transitive dependencies and maintainer changes. Prefer packages with provenance (signed builds linked to source). Run builds in isolated, hermetic environments with egress controls. For releases, use reproducible builds and compare outputs. Minimise dependency count for sensitive code.

    A strong answer hits:

    • Pinned versions and integrity hashes
    • Review diffs, new deps, maintainer changes
    • Provenance and hermetic builds
    • Minimise dependencies
  3. behavioural

    You maintain a popular open-source project you no longer have time for. A stranger offers to take over. What do you do?

    Show model answer

    Be careful, since this is exactly how event-stream was compromised. Look at the person's history and contributions, start with limited permissions (triage, PR review), and grow trust over time. Consider moving the project to an organisation or foundation with multiple maintainers. Announce the change publicly so users can evaluate it. If no trusted successor exists, mark the project deprecated rather than handing it over blindly.

    A strong answer hits:

    • Vet the person and their history
    • Gradual permission increase
    • Organisations or foundations with multiple maintainers
    • Public announcement or deprecation

Go deeper