fix(sbom): npm registry policy per project, from nested .npmrc/.yarnrc.yml #534

Merged
sharang merged 1 commits from fix/b-npmrc-nested into main 2026-10-08 21:45:45 +00:00
Owner

What was wrong

registry_license::npm_policy(root) read .npmrc / .yarnrc.yml only at the archive root. A monorepo project in a subfolder with its own .npmrc (@corp:registry=https://npm.corp.example.com/) had its private-scope package names looked up on registry.npmjs.org, and got public tarball URLs as distribution references.

What changed

  • New NpmPolicies (in registry_license.rs): the policy of a project directory comes from the .npmrc / .yarnrc.yml in it and in every parent up to the root. The nearest file names a scope's registry (as npm reads them); a non-public default registry (registry= / npmRegistryServer) anywhere on the way stops every lookup for that project. Each directory's files are read once per scan, each project's policy computed once; a location with .. falls back to the root's policy.
  • How entries map to projects: Syft's CycloneDX output carries syft:location:N:path per component (already used for .NET assemblies). syft.rs now keeps those paths for pkg:npm/ components, and SbomScanner::generate passes a purl -> lockfile paths map to enrich_registry_licenses. No new field is persisted on SbomEntry.
  • A name found in several projects is looked up only if every one of them takes it from the public registry. An entry without a known location uses the root's policy (old behaviour).
  • distribution.rs (fallback registry.npmjs.org/.../-/x.tgz URL when a lockfile names none) uses the same per-project policy, from the locations of Syft's JSON artifacts.
  • Docs: docs/guide/sbom.md, docs/reference/tools.md.

Tests

  • registry_license::tests::a_nested_npmrc_keeps_its_projects_names_inside: web/.npmrc routes @corp; only react and the root project's @corp/shared are asked, not web's @corp/pump-ui.
  • registry_license::tests::the_nearest_npmrc_names_a_scopes_registry: nearest-wins override, a .yarnrc.yml private default registry in a parent, a package in two projects, a .. path.
  • syft::tests::npm_packages_keep_the_lockfiles_they_were_read_from; distribution::tests::a_private_registry_gets_no_public_url now with a nested web/.npmrc.
  • cargo test -p compliance-agent --lib pipeline::sbom (100 passed), cargo fmt --all --check, clippy -D warnings for compliance-agent.
  • Not verified: a real Syft run on a monorepo (the location property format is the one Syft already gives for .NET assemblies, but I did not run Syft here).

Stage re-test

  1. Upload an archive with a root package.json/package-lock.json and a subfolder web/ with its own package.json, package-lock.json and web/.npmrc containing @corp:registry=https://npm.corp.example.com/, with a @corp/... dependency in web/.
  2. Scan it; on the Pakete tab the @corp/... package stays without a licence and without a public download URL, while public packages (e.g. react) get theirs.
  3. In the agent log, no request to registry.npmjs.org/@corp... appears.
  4. Move the .npmrc to the archive root and rescan: same result (root policy still applies to every project).

🤖 Generated with Claude Code

https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p

## What was wrong `registry_license::npm_policy(root)` read `.npmrc` / `.yarnrc.yml` only at the archive root. A monorepo project in a subfolder with its own `.npmrc` (`@corp:registry=https://npm.corp.example.com/`) had its private-scope package names looked up on `registry.npmjs.org`, and got public tarball URLs as distribution references. ## What changed - New `NpmPolicies` (in `registry_license.rs`): the policy of a project directory comes from the `.npmrc` / `.yarnrc.yml` in it and in every parent up to the root. The nearest file names a scope's registry (as npm reads them); a non-public default registry (`registry=` / `npmRegistryServer`) anywhere on the way stops every lookup for that project. Each directory's files are read once per scan, each project's policy computed once; a location with `..` falls back to the root's policy. - How entries map to projects: Syft's CycloneDX output carries `syft:location:N:path` per component (already used for .NET assemblies). `syft.rs` now keeps those paths for `pkg:npm/` components, and `SbomScanner::generate` passes a purl -> lockfile paths map to `enrich_registry_licenses`. No new field is persisted on `SbomEntry`. - A name found in several projects is looked up only if **every** one of them takes it from the public registry. An entry without a known location uses the root's policy (old behaviour). - `distribution.rs` (fallback `registry.npmjs.org/.../-/x.tgz` URL when a lockfile names none) uses the same per-project policy, from the `locations` of Syft's JSON artifacts. - Docs: `docs/guide/sbom.md`, `docs/reference/tools.md`. ## Tests - `registry_license::tests::a_nested_npmrc_keeps_its_projects_names_inside`: `web/.npmrc` routes `@corp`; only `react` and the root project's `@corp/shared` are asked, not `web`'s `@corp/pump-ui`. - `registry_license::tests::the_nearest_npmrc_names_a_scopes_registry`: nearest-wins override, a `.yarnrc.yml` private default registry in a parent, a package in two projects, a `..` path. - `syft::tests::npm_packages_keep_the_lockfiles_they_were_read_from`; `distribution::tests::a_private_registry_gets_no_public_url` now with a nested `web/.npmrc`. - `cargo test -p compliance-agent --lib pipeline::sbom` (100 passed), `cargo fmt --all --check`, clippy `-D warnings` for compliance-agent. - Not verified: a real Syft run on a monorepo (the location property format is the one Syft already gives for .NET assemblies, but I did not run Syft here). ## Stage re-test 1. Upload an archive with a root `package.json`/`package-lock.json` and a subfolder `web/` with its own `package.json`, `package-lock.json` and `web/.npmrc` containing `@corp:registry=https://npm.corp.example.com/`, with a `@corp/...` dependency in `web/`. 2. Scan it; on the **Pakete** tab the `@corp/...` package stays without a licence and without a public download URL, while public packages (e.g. `react`) get theirs. 3. In the agent log, no request to `registry.npmjs.org/@corp...` appears. 4. Move the `.npmrc` to the archive root and rescan: same result (root policy still applies to every project). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang added 1 commit 2026-10-08 21:29:56 +00:00
fix(sbom): npm registry policy per project, from nested .npmrc/.yarnrc.yml
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Check (pull_request) Successful in 11m37s
595de73c4a
The npm private-registry policy read .npmrc and .yarnrc.yml only at the
archive root, so a monorepo project in a subfolder with its own
`@corp:registry=...` had its private-scope names looked up on
registry.npmjs.org. The policy of an npm package now comes from the files
next to the lockfile Syft read it from and every parent directory up to
the root: the nearest file names a scope's registry, and a non-public
default registry anywhere on the way stops every lookup for that project.
A package found in several projects is looked up only if all of them
take it from the public registry. The same policy gates the fallback
tarball URL in the SBOM's distribution references.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p
sharang merged commit 0f22455db9 into main 2026-10-08 21:45:45 +00:00
Sign in to join this conversation.