# pre-commit: block a lockfile with malicious or advisory-hit packages

A git pre-commit hook (the [pre-commit](https://pre-commit.com) framework) that runs when a lockfile changes: `package-lock.json`, `npm-shrinkwrap.json`, `pnpm-lock.yaml`, `yarn.lock` (v1 and Berry), `poetry.lock`, `uv.lock` or `requirements*.txt`. It reads exact pins and checks them with KarmaDue's batch endpoint, one call per 100 packages. Only ecosystem, name and version leave the machine. No account or key is needed.

Use it alongside an install hook (https://karmadue.expo.app/docs/hooks.md). Hooks see `npm install name@1.2.3`, not what a lockfile pulls in. This catches the lockfile when it gets committed.

## Setup (two files)

1. Save the script in the repository:

```sh
mkdir -p .karmadue && curl -fsSL -A karmadue-setup https://karmadue.expo.app/recipes/pre-commit/karmadue-lockfile-check.py -o .karmadue/karmadue-lockfile-check.py
```

2. Add to `.pre-commit-config.yaml` (full file: https://karmadue.expo.app/recipes/pre-commit/pre-commit-config.yaml):

```yaml
repos:
  - repo: local
    hooks:
      - id: karmadue-lockfile-check
        name: KarmaDue lockfile check
        entry: python3 .karmadue/karmadue-lockfile-check.py
        language: system
        files: '(^|/)(package-lock\.json|npm-shrinkwrap\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lock|poetry\.lock|uv\.lock|Cargo\.lock|go\.sum|composer\.lock|Gemfile\.lock|gems\.locked|packages\.lock\.json|gradle\.lockfile|requirements[^/]*\.txt)$'
```

Then `pre-commit install`. Run it on the whole repository once with `pre-commit run karmadue-lockfile-check --all-files`. The script also works as a plain CI step: `python3 .karmadue/karmadue-lockfile-check.py package-lock.json requirements.txt`.

## What it does

- Blocks (exit 1) on any malicious-package report or published advisory for an exact pinned version.
- `--allow-advisories`: advisories become warnings. Each one still gets a single `check`, and a `deny` there still blocks. The batch lists some malicious-code incidents (event-stream's GHSA, the compromised ctx 0.2.6) as plain advisories, so the single check is the answer to obey.
- Lists as NOT CHECKED: ranges (`flask>=2`), `-r`/`-e` option lines, lockfileVersion 1, and anything the batch answers `ok: false` for.
- KarmaDue unreachable: retries once after 3 s (v0.6.1), then prints NOT CHECKED and passes. Add `--fail-closed` to block instead. Use `--fail-closed` in CI, where nobody reads a NOT CHECKED line.
- Python 3.8+, standard library only. Calls carry `x-kd-via: karmadue-precommit`; set `KARMADUE_VIA` to change it.

## Tested (2026-11-15, 18:45-18:55 UTC, live KarmaDue)

Run inside pre-commit 4.7.0 in a scratch git repository, with the same config as above:

| Commit | Result |
|---|---|
| package-lock.json with is-number 7.0.0 | `Passed`, commit made |
| package-lock.json with event-stream 3.3.6, lodash 4.17.20, is-number 7.0.0 | `Failed`, commit refused: `BLOCK npm:event-stream@3.3.6 1 advisory: GHSA-mh6f-8j2x-4483`, `BLOCK npm:lodash@4.17.20 4 advisories` |

Direct runs: the same lockfile plus requirements.txt (`requests==2.32.3`, `ctx==0.2.6`, `flask>=2`, `-r other.txt`) gave 4 blocks and 2 NOT CHECKED. With `--allow-advisories`, event-stream and ctx still blocked ("single check says deny"), and requests and lodash became warnings. Unreachable API: exit 0 by default, exit 1 with `--fail-closed`.

## More lockfile formats (v0.2, 2026-11-15, 19:05 UTC)

Run directly on sample lockfiles written by hand in each format (not generated by pnpm, yarn, poetry or uv here): pnpm-lock.yaml v9 blocked event-stream 3.3.6, lodash 4.17.20 and @babel/core 7.24.0 (1 advisory). yarn.lock v1 blocked event-stream and listed a github: entry as NOT CHECKED. Berry yarn.lock blocked lodash 4.17.20. poetry.lock blocked ctx 0.2.6 and requests 2.32.3 and skipped a directory source. uv.lock blocked jinja2 3.1.2 (10 advisories) and skipped the editable project. With `--allow-advisories`, the pnpm run still blocked event-stream (single check: deny).

## Limits

- Read: npm lockfiles v2/v3, pnpm-lock.yaml (v6-v9 package keys), yarn.lock (v1 and Berry), poetry.lock and uv.lock (needs Python 3.11+ for TOML), bun.lock, Cargo.lock, go.sum, composer.lock, and pip requirements with `==` pins. Git, path, directory and editable entries are listed as NOT CHECKED. The binary bun.lockb is not read.
- No report found is not a clearance. The check covers known reports and advisories. It does not cover runtime behaviour.
- With the default settings, any advisory blocks, including low-severity ones. Use `--allow-advisories` if that is too strict for your repository.

## Fix in v0.2.1 (2026-11-15)

Tested on lockfiles generated by real tools (npm 10, pnpm 10.34.6, yarn 1.22.22, yarn 4.5.3, uv 0.13.7, poetry) for the same small dependency set. v0.2 blocked packages whose listed advisories do not affect the pinned version (for example semver 6.3.2, json5 2.2.3, ms 2.1.3): it counted every advisory KD returned for the package name. v0.2.1 counts only advisories KD marks as affecting the exact version; an advisory without that flag still counts. Malicious-package reports are unchanged. Re-download the script to pick up the fix.

## bun.lock (v0.3, 2026-11-15)

v0.3 reads the text bun.lock that bun 1.2+ writes. The binary bun.lockb is not read. Each entry under "packages" gives an exact npm name and version. Entries from git, a file path, a workspace or a non-npmjs registry are listed as NOT CHECKED. Tested on a bun.lock from bun install --lockfile-only for the same dependencies as the npm, pnpm and yarn tests: it gives the same 41 pinned packages and the same 3 blocks. Update your .pre-commit-config.yaml files pattern to include bun\.lock (the published config already does).

## Cargo.lock, go.sum and composer.lock (v0.4, 2026-11-15)

v0.4 reads three more formats and sends them to the same batch check:

- Cargo.lock: [[package]] entries whose source is the crates.io registry (git or other registries are NOT CHECKED; your own workspace crates have no source and are skipped).
- go.sum: each module and version that has a code hash. Lines ending in /go.mod only record a go.mod file and are skipped. The leading "v" is dropped.
- composer.lock: packages and packages-dev. Branch versions such as dev-main are NOT CHECKED.

Outside npm and PyPI, KarmaDue answers from a live OSV.dev exact-version lookup plus the registry, and takes at most 25 such packages per call, so the script sends them in groups of 25.

Tested on 2026-11-15: a Cargo.lock from cargo generate-lockfile (time =0.1.43, serde =1.0.200; 11 crates) blocked time 0.1.43 (GHSA-wcg3-cvx6-7396, fixed in 0.2.23). The same file with a hand-added rustdecimal 1.23.1 entry blocked it as MALICIOUS (MAL-2022-1, RUSTSEC-2022-0042) even with --allow-advisories, and listed a git crate as NOT CHECKED. A go.sum from go get (golang.org/x/net v0.6.0, github.com/google/uuid v1.6.0) blocked x/net 0.6.0 (20 advisories) and passed uuid. composer.lock was written by hand (Composer was not installed on the test machine): guzzlehttp/guzzle 7.4.4 (11 advisories) and phpunit/phpunit 10.5.20 (1) blocked, monolog/monolog 3.5.0 passed, dev-main NOT CHECKED. Update your .pre-commit-config.yaml files pattern to include Cargo\.lock, go\.sum and composer\.lock (the published config already does).

## Clearer deny lines (v0.4.1, 2026-11-15)

With --allow-advisories, a package that would only warn gets one single check. Before v0.4.1, a deny from that check printed as "N advisories ... - single check says deny", which hid that the reason was a malicious-package report. v0.4.1 prints the check's own answer first, for example:

    BLOCK npm:event-stream@3.3.6 DENY (single check: Do not install event-stream@3.3.6: malicious-package report GHSA-mh6f-8j2x-4483 ...); 1 advisory: GHSA-mh6f-8j2x-4483

What blocks and what passes is unchanged. If the single check can't be reached, the package stays blocked and the line says so.

## Guide links per ecosystem (v0.4.2, 2026-11-15)

When a commit is refused, the last line now links the guide for each ecosystem that blocked: npm packages to <https://karmadue.expo.app/docs/check-npm-packages.md> and PyPI packages to <https://karmadue.expo.app/docs/check-python-packages.md>. Any other ecosystem goes to <https://karmadue.expo.app/docs/check-other-ecosystems.md> (v0.4.3; v0.4.2 linked docs/tools.md). Before this, every refusal linked the npm guide. Block and pass results are unchanged.

## Gemfile.lock and NuGet packages.lock.json (v0.5, 2026-11-16)

v0.5 reads two more formats and sends them to the same batch check:

- Gemfile.lock (also gems.locked): gems in the GEM section whose remote is rubygems.org. Platform suffixes such as -x86_64-linux are dropped. Gems from GIT or PATH sections are listed as NOT CHECKED.
- packages.lock.json (NuGet): the resolved version of every package across all target frameworks. Project references (your own code) are skipped.

Tested on real lockfiles. Rails v6.1.0's committed Gemfile.lock gave 187 gems checked and 33 blocking with advisories (for example rack 2.2.3 and nokogiri 1.10.10). 17 git/path gems were NOT CHECKED, and mimemagic 0.3.5 was NOT CHECKED because OSV had no answer for it. A real NuGet packages.lock.json (17 packages) passed. Full Rails run: about 12 s, in batches of 25 outside npm and PyPI. Details for these ecosystems: <https://karmadue.expo.app/docs/check-other-ecosystems.md>

## gradle.lockfile (v0.6, 2026-11-16)

v0.6 reads Gradle dependency lockfiles: gradle.lockfile, buildscript-gradle.lockfile, and legacy gradle/dependency-locks/*.lockfile files. Each group:artifact:version line is sent as a Maven package. The empty= line (configurations with no dependencies) is skipped, and any other line is listed as NOT CHECKED.

Tested on two real committed lockfiles, with the published script run on 2026-11-16 at 02:32 UTC (also inside pre-commit 4.7.0, which refused the log4j commit):

- embulk/embulk embulk-deps/gradle.lockfile (commit 74a2f15): 37 packages checked, 6 blocking with advisories, for example jackson-databind 2.16.2 (12, fixed in 2.18.11) and netty-common 4.1.109.Final (2, fixed in 4.1.118.Final).
- Netflix/maestro maestro-http/gradle.lockfile (commit 4af9a65): 94 packages checked, 7 blocking, for example httpclient5 5.1.3 (fixed in 5.6.3) and commons-beanutils 1.9.4 (fixed in 1.11.0).
- A hand-written lockfile pinning log4j-core 2.14.1 was refused, with 8 advisories including GHSA-jfh8-c2jp-5v3q (Log4Shell).

Limits: Gradle's lockfile lists resolved versions but not the repository a package came from, so a package from a private or non-Maven Central repository is checked as if it were public. Advisory counts come from OSV at the time of the run and will grow.

## Retry and Maven single check (v0.6.1, 2026-11-16)

- One retry: on 2026-11-16 around 02:30 UTC the batch endpoint answered HTTP 503 on 2 of about 60 paced calls. v0.5 and v0.6 turned each one into NOT CHECKED, so the commit passed unchecked. v0.6.1 waits 3 s and tries once more before giving up. Tested by pointing the script at a failing URL with `--fail-closed`: it retried, then blocked (exit 1) after about 3 s.
- Maven single check: with `--allow-advisories`, the single check for Maven packages from gradle.lockfile was sent with the wrong ecosystem prefix in v0.6. v0.6.1 sends maven:group:artifact@version. Example: maven:org.apache.logging.log4j:log4j-core@2.14.1 answers warn, so it stays a warning in that mode, and still blocks in the default mode.

## Every evidence ID, sorted (v0.6.2, 2026-11-16)

Advisory lines now list every advisory ID KarmaDue returned for that exact version, sorted, instead of the first five in arrival order. An outside reproduction on 2026-11-16 found the order differed between runs. The verdicts and exit codes are unchanged. Treat report IDs as evidence, not as keys. Key your own records on `ecosystem:package@version`, because one incident can carry several IDs (GHSA, MAL, CVE), and the set can grow when a new report is published.
