socket.yml and the selection filter refuse a scoped npm purl that scan --package accepts

Aperta
#2,294 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
backend, cli

Direzione di ricerca

Start with package_spec_error in crates/socket-patch-core/src/policy/socket_yml.rs and compare its purl parsing with package_spec_matches in crates/socket-patch-core/src/policy/mod.rs; then read spec_error in policy/filter.rs. Update the shared parsing behavior and the named package_specs, schema parity, and e2e_socket_yml_policy.rs tests. Done means both unencoded scoped purls are accepted consistently while empty scoped names remain rejected.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

agent:triaged arch-audit bug priority:p1

[agent] Found by the architecture refactor routine on main at cfc870f. Kind: two implementations of the --package grammar that disagree.

Problem

socket.yml (patches.packages, patches.ignorePackages) and the selection filter (skip.packages, reachability[].package) refuse the purl of a scoped npm package written the way socket-patch prints it: pkg:npm/@babel/core or pkg:npm/@babel/[email protected]. The whole scan fails, in every mode and on a dry run, with socket_yml_invalid (exit 1) or filter_invalid (exit 2). The message is "purl spec needs a type and a name", for a spec that has both. scan --package takes the same spec and selects the package.

CLI_CONTRACT.md says the three share one grammar:

  • socket.yml patch policy: "Package specs are exactly --package's: a name … or a purl with or without a version".
  • Selection filter: "skip.packages and a finding's package use the --package grammar".
  • Target grammar: "the same matcher as scan --package and socket.yml".

The unencoded spelling is the one every command prints (the scan table, packages[].purl, policy.filtered[].purl, the manifest keys). It is also the one the ambiguous_target message tells the user to type: "core" is ambiguous: it names pkg:npm/@angular/core, pkg:npm/@babel/core; use the full name or a purl.

Who it hurts. Anyone who allows or ignores a scoped npm package by purl, which the contract recommends over a bare name ("so prefer purls"). The run fails closed, so nothing is patched against the policy. The cost is a failed scan (a red CI job) and an error that points at the wrong thing. pkg:npm/%40babel/core and the bare name @babel/core work today.

Evidence on main

A project with node_modules/@babel/core at 7.0.0 and four other packages, and a local stand-in for the patch API that offers one patch for each. The binary is built from cfc870f.

The flag takes the spec:

$ socket-patch scan --mode agent --dry-run --package pkg:npm/@babel/[email protected]
…
[dry-run] Would download and apply 1 patch. No changes made.
  MODERATE  @babel/[email protected]  CVE-2026-0005
…
(exit 0)

socket.yml refuses it:

$ cat socket.yml
version: 2
patches:
  ignorePackages:
    - pkg:npm/@babel/[email protected]

$ socket-patch scan --mode agent --dry-run
Error (socket_yml_invalid): socket.yml: patches.ignorePackages[0]: purl spec needs a type and a name (`pkg:npm/lodash`): `pkg:npm/@babel/[email protected]` (fix the file, or pass --no-socket-yml to ignore it)
(exit 1)

So does the selection filter:

$ cat filter.json
{"version": 1, "skip": {"packages": ["pkg:npm/@babel/[email protected]"]}}

$ socket-patch scan --mode agent --dry-run --selection-filter filter.json
Error: selection filter filter.json: skip.packages[0]: purl spec needs a type and a name (`pkg:npm/lodash`): `pkg:npm/@babel/[email protected]`
(exit 2)

The same spec in each place, with and without a version:

Spec scan --package patches.ignorePackages / patches.packages skip.packages / reachability[].package
pkg:npm/@babel/[email protected] selects the package socket_yml_invalid, exit 1 filter_invalid, exit 2
pkg:npm/@babel/core selects the package socket_yml_invalid, exit 1 filter_invalid, exit 2
pkg:npm/%40babel/[email protected] selects the package honored honored
@babel/core selects the package honored honored

Hosted mode gives the same result: on a hosted npm project with 24 patches, four of them for scoped packages, ignorePackages: ["pkg:npm/@aninhe925/[email protected]"] fails the scan with socket_yml_invalid and changes no file, and --package with that spec pins that one package.

The selection filter's JSON Schema (docs/selection-filter.schema.json, packageSpec) accepts both unencoded spellings, so the schema and the parser disagree too.

A unit test that feeds one spec to both functions fails on main:

// crates/socket-patch-core/src/policy/socket_yml.rs, mod tests
#[test]
fn probe_scoped_npm_purl_matcher_vs_validator() {
    for spec in ["pkg:npm/@babel/core", "pkg:npm/@babel/[email protected]"] {
        assert!(
            crate::policy::package_spec_matches(spec, "pkg:npm/@babel/[email protected]"),
            "the matcher takes {spec}"
        );
        assert_eq!(package_spec_error(spec), None, "the validator refuses {spec}");
    }
}
$ cargo test -p socket-patch-core --lib probe_scoped_npm_purl
assertion `left == right` failed: the validator refuses pkg:npm/@babel/core
  left: Some("purl spec needs a type and a name (`pkg:npm/lodash`)")
 right: None
test result: FAILED. 0 passed; 1 failed

Cause

package_spec_error in crates/socket-patch-core/src/policy/socket_yml.rs (line 484) accepts a purl spec only when, at line 495:

!ty.is_empty() && !name.trim_matches('/').is_empty() && !name.starts_with('@')

starts_with('@') is there to refuse pkg:npm/@1.0.0, an empty name with a version (the package_specs test pins that, line 1285). It refuses every npm scope with it.

The matcher, package_spec_matches in crates/socket-patch-core/src/policy/mod.rs (line 841), finds the version at the last @ after the first character, so it reads pkg:npm/@babel/[email protected] as the name @babel/core at 7.0.0. --package goes to the matcher with no validator in front of it. The selection filter's spec_error (policy/filter.rs, line 318) calls package_spec_error. The test that keeps the schema and the parser in step has no unencoded scoped purl in its table.

Planned fix

  • Give the validator the matcher's reading of a purl spec: one function that splits a spec into type, name and version, used by package_spec_matches and package_spec_error, so a spec is valid exactly when the matcher can match it.
  • Keep refusing what can never match: pkg:npm/@1.0.0 (no name), and a scope with no name after it (pkg:npm/@babel).
  • Add both scoped spellings to the package_specs test, to the schema parity table in policy/filter.rs, and one case to e2e_socket_yml_policy.rs.

No JSON shape, flag, default or exit code changes. Two inputs that are errors today become what the contract says they are.

Not run: the in-memory engine, which reads socket.yml through the same parser. get reads the file only for its policy_bypassed warning, so with such a file it drops the warning and still succeeds.


Generated by Claude Code

Lingua principale
Rust
Stelle
9
Fork
2
Merge medio
1g 8h
PR unite (30g)
313

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SocketDev/socket-patch

Tutte le issue di SocketDev/socket-patch

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.