Skip to content

Add an internal scan --selection-filter for reachability-aware patching - #2062

Merged
Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
feat/scan-selection-filter
Nov 12, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
feat/scan-selection-filter

Conversation

@mikolalysenko

Copy link
Copy Markdown
Collaborator

Adds scan --selection-filter <FILE> (env SOCKET_SELECTION_FILTER): a JSON sidecar that says what one scan need not patch, so the scan applies only the patches that fix a reachable (or unknown) vulnerability. It is the interface for the socket fix remediation pull requests. The flag is hidden from --help, and the file is versioned ("version": 1) instead of being covered by the CLI's semver policy.

{
  "version": 1,
  "skip": {
    "paths": ["services/legacy/"],
    "packages": ["pkg:npm/left-pad"],
    "advisories": ["GHSA-jf85-cpcp-j695", "CVE-2021-23337"]
  },
  "reachability": [
    { "advisory": "GHSA-35jh-r3h4-6jhm", "package": "pkg:npm/lodash@4.17.20", "state": "reachable" },
    { "advisory": "GHSA-p6mc-m468-83gw", "state": "unreachable" },
    { "package": "pkg:npm/minimist@1.2.5", "state": "unreachable" }
  ],
  "unlistedAdvisories": "patch"
}

The format reference is docs/selection-filter.md: fields, value grammars, the rules with a worked table, how to read the result, errors, limits and integration recipes. Its JSON Schema is docs/selection-filter.schema.json.

Behavior

  • Skips. skip.paths and skip.packages leave project roots and packages alone, with the grammar of socket.yml's patches.ignorePaths and of --package.
  • Per advisory. A skipped advisory needs no fix. Otherwise the most specific finding decides (advisory and package, then advisory, then package); among equally specific ones the most cautious wins. unreachable needs no fix; reachable and unknown need one. An advisory no finding covers follows unlistedAdvisories: patch (default) or skip, which makes the findings an allowlist.
  • Per patch. A patch is withheld only when no advisory it fixes needs a fix. A patch that fixes one that does is applied whole, so a merged patch brings its unreachable fixes along. A package goes unpatched only when every advisory its patches fix is unreachable or skipped.
  • Ranking is unchanged. Among the patches not withheld the usual ranking picks one, over everything each patch fixes, and the severity floor judges the whole patch.
  • One preference. When exactly one advisory of a package needs a fix and a patch fixes it alone, that single-advisory patch is taken instead of a merged one.
  • Recorded patches. The filter never removes a patch.
    • A recorded package it skips is retained, and one whose patches are all withheld keeps its patch.
    • A recorded patch that is itself withheld gives way to a wanted one, so the patch for a reachable advisory lands whether or not the package was patched before.
    • A package already on a wanted merged patch follows the usual ranking: it is not traded for the single-advisory patch.
  • Composition. It narrows beside the socket.yml policy. --no-socket-yml drops only the file.
  • Output. policy.filtered[] gains filter_path_skipped, filter_package_skipped and filter_not_needed; a run given a filter echoes its digest and entry counts as policy.selectionFilter. Without a filter no output changes.
  • Errors. A file that cannot be read or honored fails before any request or write: exit 2, error.code: filter_invalid.

Only scan reads the filter, in every mode. The in-memory engine behind the Node addon does not take one yet.

Where the code is

  • crates/socket-patch-core/src/policy/filter.rs (new): the parser, validation and per-package judgement.
  • policy/mod.rs: SelectionPolicy::with_filter, the path and package skips in admits_root / admits_purl, judge_offers, and Offers.withheld.
  • rollout/stage.rs: classify lets a recorded patch the filter withholds give way to the selection.
  • scan/policy.rs: ScanPolicy::select applies the judgement beside the severity floor. scan/socket_yml_args.rs holds the hidden flag.

Testing

  • Unit tests for the parser, the rules, the policy wiring and the selection, and a test that keeps the JSON Schema in step with the parser (keys, enumerations, limits, id and spec patterns, examples).
  • Nine end-to-end tests in e2e_socket_yml_policy.rs run the real binary against a mock patch API: hosted, agent and vendored modes, merged patches, the single-advisory preference, the allowlist, path skips, fail-closed errors, and recorded-patch retention.
  • Locally: both unit suites pass in full (7,730 core, 929 CLI), with the scan, policy, rollout, hosted-memory and discovery integration suites, clippy -D warnings and rustfmt. The Docker and per-ecosystem build suites were not run locally.
  • commands::apply::tests::cargo_patches_refuse_under_a_cargo_config_above_the_repository failed once in a full local unit run and passed on every re-run. This PR does not touch it.

Review

/code-review high ran on the diff; the confirmed findings are fixed. Three were left as they are:

  • The patch-level rules are applied in the CLI's select, not in core's floor_filter. The per-package judgement is in core (SelectionPolicy::judge_offers). The choice against a recorded patch stays in ScanPolicy::select, where the severity floor's lives too. The in-memory engine cannot be handed a filter today, so nothing reaches the path that would ignore it. Wiring it in belongs with the change that gives the Node addon a filter.
  • Package-only findings are matched linearly. They are matched once per package with a patch, not per patch: 10,000 of them over 200 such packages cost about a quarter of a second, measured. Findings that name an advisory are indexed. The docs tell callers with many results to name the advisory.
  • The skip.paths compile-and-report block resembles socket_yml::check_patterns. That helper compiles a list and discards the matcher; the filter keeps it.

🤖 Generated with Claude Code

`scan --selection-filter <FILE>` (or `SOCKET_SELECTION_FILTER`) reads a JSON
sidecar that says what one scan need not patch: project roots, packages and
advisories to skip, and reachability findings. It is the interface for
Socket's remediation tooling (`socket fix` pull requests): hidden from
`--help`, and versioned (`"version": 1`) rather than covered by semver.

What a scan does with it:

- `skip.paths` and `skip.packages` leave roots and packages alone, like
  socket.yml's `patches.ignorePaths` and `patches.ignorePackages`.
- Each advisory a patch fixes is judged: skipped ids and `unreachable`
  findings need no fix, `reachable` and `unknown` ones do. The most specific
  finding decides (advisory and package, then advisory, then package), and
  `unlistedAdvisories: "skip"` turns the findings into an allowlist.
- A patch is withheld only when no advisory it fixes needs a fix. A patch
  that fixes one that does is applied whole, so a merged patch brings its
  unreachable fixes along, and a package goes unpatched only when every
  advisory its patches fix is unreachable or skipped.
- The filter never changes how patches rank; the severity floor judges the
  whole patch. One preference comes first: when exactly one advisory of a
  package needs a fix and a patch fixes it alone, that single-advisory patch
  is taken instead of a merged one.
- It narrows beside the socket.yml policy and never removes a patch. A
  recorded patch the filter withholds gives way to a wanted one; a package
  already on a wanted merged patch follows the usual ranking.

`policy.filtered[]` gains the reasons `filter_path_skipped`,
`filter_package_skipped` and `filter_not_needed`, and a run given a filter
echoes it as `policy.selectionFilter`. A file that cannot be read or honored
fails before any request or write: exit 2, `filter_invalid`.

The format reference is docs/selection-filter.md, with a JSON Schema in
docs/selection-filter.schema.json that a unit test keeps in step with the
parser.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core scan patch-selection and recorded-patch upgrade behavior (fail-closed on bad files), but the interface is internal/hidden and only affects runs that pass a filter file.

Overview
Adds a hidden scan --selection-filter / SOCKET_SELECTION_FILTER path that loads a versioned JSON sidecar and narrows one scan so packages are patched only when at least one advisory their candidate patches fix is still “needed” (not skipped, not unreachable, per reachability findings and unlistedAdvisories).

socket-patch-core gains the parser and rules in policy/filter, wires skip paths/packages into SelectionPolicy, judges offers via judge_offers, and tracks filter-withheld offers on Offers.withheld. rollout/stage::classify can replace a recorded patch when the filter withheld it but a wanted patch exists (unlike the severity floor). scan loads the file fail-closed as filter_invalid, updates patch selection (merged patches stay whole; optional single-advisory preference when one advisory needs a fix), and extends policy output with filter_* reasons and policy.selectionFilter. docs/selection-filter.md and a JSON Schema document the internal contract.

Reviewed by Cursor Bugbot for commit 9601238. Configure here.

Merged via the queue into main with commit de81f1b Nov 12, 2026
50 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the feat/scan-selection-filter branch November 12, 2026 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants