Dev.to Security 🔐 Cybersecurity 👁 2 📖 2 min read

--ignore-scripts didn't stop this one: git dependencies on npm 10

You cloned a take-home repo from a recruiter. You know install scripts are the classic trap, so you run: npm install --ignore-scripts On npm 10, that still runs code from a stranger, if one of the dependencies c

You cloned a take-home repo from a recruiter. You know install scripts are the classic trap, so you run:

npm install --ignore-scripts

On npm 10, that still runs code from a stranger, if one of the dependencies comes from git.

What npm does with a git dependency

A dependency can point at a git repository instead of the registry:

{
  "dependencies": {
    "utils": "github:someone/utils"
  }
}

npm can't install a git repo as it is, it has to pack it first. The npm docs say that if a package installed through git has a prepare script, its dependencies and devDependencies are installed and prepare is run before the package is packed and installed.

So prepare in that other repository runs on your machine during your npm install. Nobody typed npm run prepare.

The test

We built the smallest possible case, with a harmless marker instead of a payload:

  • a local git repo dep-demo whose prepare script writes a marker file
  • an app whose only dependency is "dep-demo": "git+file:///…/dep"
  • npm install with and without --ignore-scripts, in a clean folder each time
npm version plain npm install npm install --ignore-scripts ignore-scripts=true in project .npmrc
10.9.9 (bundled with Node 22) prepare ran prepare ran prepare ran
11.21.3 prepare ran did not run not tested

On npm 10.9.9 the flag made no difference for the git dependency. On npm 11.21.3 it stopped it.

We tested on Linux with a local git+file: URL. We did not test every 10.x release, and we only measured the local case; a github: URL should behave the same way according to the docs.

Why this matters for take-home repos

--ignore-scripts is the advice most of us give, including us. It still helps: it stops the install hooks of registry packages and the project's own preinstall and postinstall. But on npm 10 a git dependency is a hole in it, and git dependencies are rare enough in normal projects that one in a take-home repo is worth a closer look on its own.

What to do

  1. Check your version: npm --version. If it starts with 10, assume the flag does not cover git dependencies.
  2. Before any install, read dependencies, devDependencies and optionalDependencies for anything that is not a version range: github:, git+, git://, tarball URLs, file:.
  3. Look at overrides, resolutions and pnpm.overrides too. A git URL can hide there while dependencies looks normal.
  4. Open untrusted repos in a throwaway VM or container, not on the laptop with your keys and tokens.

There is also an open npm bug report on a related problem: allowScripts not stopping a git dependency's prepare, with a fix proposed in npm/cli PR #10342.

If you want the step 2 and 3 checks done for you, npm-install-audit flags non-registry dependencies, including ones in overrides, without running anything from the repo: https://github.com/cryptostrapon/npm-install-audit

Have you ever seen a git dependency in a take-home project? What was in it?

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.