A hosted scan reports a gem as patched, and its --vex attests it, after the Gemfile stops naming the patch source

Offen
#2,303 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
46/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
ruby, rust
Bereich
backend, cli

Rechercherichtung

Start with confirm in core hosted/engine.rs and the nearby a_gem_lock_pin_whose_gemfile_lost_its_source_is_not_confirmed test described in the issue. Read how the rewriter reports refusals in core patch/redirect/mod.rs, then compare the existing ecosystem-specific confirmation fixes cited here. Done means an orphaned Gemfile.lock patch pin is not confirmed, while an intact Gemfile and lock pair still is; verify the related scan summary and in-run VEX behavior.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

agent:triaged arch-audit bug priority:p1

[agent] Found by the architecture refactor routine (ledger discussion #2244, lead L44), on main at cfc870f.

Problem

A hosted gem pin is a pair: the source: option on the gem's Gemfile declaration, and the gem's own GEM section in Gemfile.lock. When the Gemfile stops naming the patch source while the lock keeps its section, Bundler no longer installs the patch: a frozen install fails, and an ordinary bundle install rewrites the lock and installs the upstream gem. docs/ecosystems.md (line 4937, "Keep the Gemfile and the lock together") describes this state and says a hosted scan skips the gem.

The scan does refuse to rewrite the gem. It then reports the gem as patched:

  • human output: 1 package is already on hosted patches; nothing to rewrite. and Patched 1 package, fixing 1 vulnerability (1 high). … Nothing left to patch., exit 0;
  • --json: an applied event, summary.applied: 1, summary.skipped: 0, status: "success";
  • scan --mode hosted --vex, in a checkout with no gems installed: a not_affected statement for the gem, written by the run whose own stderr says "not attested";
  • the in-memory engine that the Socket GitHub app runs: redirected: 1 and a pinned patch.

The standalone vex over the same tree is right: it warns patched_ref_unattributable, writes no statement and exits 2.

The warning the scan prints beside its summary gives the wrong cause and the wrong remedy. It says the lock "pins daaran1 to a source of its own" and tells the user to declare the gem in the Gemfile's default source or to switch modes. The source is socket-patch's own, the gem already is in the default source, and the remedy the docs give is bundle install once without frozen mode, then socket-patch scan again.

Who it hurts

  • A project whose pull request with the hosted pin met a change to the same Gemfile line on the base branch, where the conflict was resolved by taking the base branch's line. The lock merges cleanly and keeps the patch section.
  • A developer who removes the long source: URL from the Gemfile by hand, or runs git checkout on that one file.
  • Their CI: a frozen bundle install fails, while the scan step before it says "Nothing left to patch".
  • Anyone who reads the VEX document: it says not_affected for a gem that no install takes from the patch source.

Three states give the same report: the Gemfile line without its option (redirect_gem_source_option), the line naming another source (redirect_gem_source_option), and the line deleted (redirect_gem_declaration_not_visible).

Evidence on main

The binary is built from cfc870f. The patch API is the bench's (socket-patch-bench serve bundler/hosted --bin target/debug/socket-patch --scale 0.02 --work-dir w prints the environment that socket-patch runs with below). app is a copy of the bench project without vendor/ and .bundle/: a fresh checkout, as CI has before bundle install.

$ grep daaran1 Gemfile
gem "daaran1", "~> 8.10.6"

$ socket-patch scan --mode hosted
Switched 1 package to hosted patches; rewrote 2 files.
  HIGH  [email protected]  CVE-2024-10000
Added `source:` to 1 gem declaration in Gemfile (daaran1).
…                                                           (exit 0)

$ grep daaran1 Gemfile
gem "daaran1", "~> 8.10.6", source: "http://127.0.0.1:39681/patch-registry/gem/48a8d707-…/c68e142c-2b8b-44ec-8008-41231281f868/"
$ grep -n -A3 patch-registry Gemfile.lock; grep -n 'daaran1.*!' Gemfile.lock
2:  remote: http://127.0.0.1:39681/patch-registry/gem/48a8d707-…/c68e142c-2b8b-44ec-8008-41231281f868/
3-  specs:
4-    daaran1 (8.10.6)
5-      urtexehi3 (>= 7.12.9)
45:  daaran1 (~> 8.10.6)!

The Gemfile loses the option, as after a merge that took the other side of that file. Gemfile.lock stays:

$ grep daaran1 Gemfile
gem "daaran1", "~> 8.10.6"

$ socket-patch scan --mode hosted
1 package is already on hosted patches; nothing to rewrite.
  HIGH  [email protected]  CVE-2024-10000

Patched 1 package, fixing 1 vulnerability (1 high).
Saved about 2 hours of engineering time (1 package upgrade at ~2 hours).
Nothing left to patch.
Found 33 packages (33 ruby)
Warning: Gemfile.lock pins daaran1 to a source of its own (`daaran1 (~> 8.10.6)!`), which already selects where bundler fetches the gem from (bundler allows one source per gem); redirect skipped — hosted mode re-points gems that come from the Gemfile's default source, so either declare daaran1 there or patch it with `--mode vendored` or `--mode agent`
                                                            (exit 0)

$ socket-patch scan --mode hosted --json | jq -c '.status, .summary.applied, .summary.skipped, .events, [.warnings[].code], .redirect'
"success"
1
0
[{"action":"applied","purl":"pkg:gem/[email protected]","uuid":"c68e142c-2b8b-44ec-8008-41231281f868","details":{"mode":"hosted"}}]
["redirect_gem_source_option"]
{"mode":"hosted","rewrittenFiles":[]}

Bundler 2.6.2 (the version in the lock's BUNDLED WITH), frozen, over that pair:

$ BUNDLE_FROZEN=true bundle install
The list of sources changed, but the lockfile can't be updated because frozen
mode is set

You have changed in the Gemfile:
* daaran1 from `rubygems repository
http://127.0.0.1:39681/patch-registry/gem/48a8d707-…/c68e142c-2b8b-44ec-8008-41231281f868/`
to `no specified source`
                                                            (exit 16)

The same scan with --vex attests the gem, two lines after it says it does not:

$ socket-patch scan --mode hosted --vex vex.json --vex-product pkg:generic/app@1
1 package is already on hosted patches; nothing to rewrite.
…
Warning: Gemfile.lock: line 4: daaran1 8.10.6 is locked from Socket patch c68e142c-…, but Gemfile declares daaran1 with no source of its own, not with `source: "http://127.0.0.1:39681/patch-registry/gem/…"`. Bundler takes a gem from the source its Gemfile declaration names, so it installs daaran1 from there and rewrites this lock (a frozen install fails); not attested. To patch it again, run `bundle install` once without frozen mode so the lock follows the Gemfile, then re-run `socket-patch scan`
Warning: Gemfile.lock pins daaran1 to a source of its own (…); redirect skipped — …
Wrote OpenVEX document with 1 statement to ./vex.json (hosted patches are attested from their patch records, not hash-verified — …).
                                                            (exit 0)

$ jq -c '.statements[] | {status, justification, impact_statement, products: [.products[]."@id"]}' vex.json
{"status":"not_affected","justification":"inline_mitigations_already_exist","impact_statement":"Patched via Socket patch c68e142c-2b8b-44ec-8008-41231281f868 (redirected)","products":["pkg:generic/app@1","pkg:gem/[email protected]"]}

The standalone command over the same tree:

$ socket-patch vex --product pkg:generic/app@1 -O -
Warning: Gemfile.lock: line 4: daaran1 8.10.6 is locked from Socket patch c68e142c-…, but Gemfile declares daaran1 with no source of its own, … not attested. …
Error: No patches in this project; nothing to attest. Run `socket-patch scan` first.
                                                            (exit 2)

With the unpatched gem installed in vendor/bundle, the in-run document is not written: the gem is omitted as redirect_unwired and the run exits 1. Its summary still says Patched 1 package … Nothing left to patch.

For comparison, the same refusal over a lock that is not wired, and the yarn berry twin of this state (the lock entry kept, the resolutions entry gone), are both reported as what they are:

$ socket-patch scan --mode hosted          # gem "daaran1", "~> 8.10.6", git: "…", lock not wired
Switched 0 packages to hosted patches; rewrote 0 files.

1 vulnerability left to patch: 1 is in packages this run did not patch.
No patches could be switched to hosted:
  pkg:gem/[email protected]: no lockfile entry pinning it could be rewritten (see the warning below)
                                                            (exit 0; a `skipped` event, `redirect_unconfirmed`)

Cause

confirm (core hosted/engine.rs, line 5940) decides which patches a hosted run pinned. Its last arm (line 6155) confirms a patch when its artifact URL or registry index URL occurs anywhere in the project's final files. For a gem that URL is the remote: line of the lock section, so the section alone confirms the gem, whatever the Gemfile says and although the gem rewriter refused it (core patch/redirect/mod.rs, line 8451, the source-pinned DEPENDENCIES entry). The confirmed set is what the events, the summary, the rollout counts and the in-run VEX exemption are built from.

The class was fixed before, one ecosystem at a time, each by letting the rewriter's own report decide in place of the substring:

Ecosystem Fix
Cargo #193 (confirmed_cargo_uuids): "Substring presence must never confirm a cargo dep"
Go #252, "Stop hosted Go redirects claiming unpatched deps"
yarn berry #465, and the test an_orphaned_berry_lock_pin_is_not_confirmed (core hosted/engine.rs, line 7538): "a confirmed dep feeds the in-run VEX not_affected exemption"
yarn classic, npm, Maven reactor roots #839, #1008, #1886

The gem rewriter reports neither the gems it refused nor the pairs it found whole, so gems still fall to the substring arm. I ran the next scan over every proper subset of a hosted scan's writes, for each bench project whose scan writes more than one file (npm, pnpm, yarn berry, uv, Bundler, Cargo, Go, NuGet, Maven, Gradle: 56 states). 51 converge on the full result. The yarn berry lock without its resolutions entry is reported as skipped. Three Maven states keep a wired pom.xml and do not get their .mvn/ Trusted Checksums files back; the suffixed version in pom.xml is the pin there, so applied is true of them. The Bundler lock without its Gemfile option is the one reported as patched while nothing installs the patch.

A failing test

The gem twin of the berry test, beside it in core hosted/engine.rs. It fails on main at the last assertion, with [("pkg:gem/[email protected]", "77777777-7777-4777-8777-777777777777")]:

#[test]
fn a_gem_lock_pin_whose_gemfile_lost_its_source_is_not_confirmed() {
    use crate::patch::redirect::rewrite_registry_redirect;
    let candidate = gem_candidate();
    let gemfile = "source \"https://rubygems.org\"\n\ngem \"rails\", \"7.0.0\"\n".to_string();
    let lock = format!(
        "GEM\n  remote: https://rubygems.org/\n  specs:\n    rails (7.0.0)\n\nPLATFORMS\n  \
         ruby\n\nDEPENDENCIES\n  rails (= 7.0.0)\n\nCHECKSUMS\n  rails (7.0.0) sha256={}\n\n\
         BUNDLED WITH\n   2.6.2\n",
        "2".repeat(64)
    );
    let mut files = BTreeMap::new();
    files.insert("Gemfile".to_string(), gemfile.clone());
    files.insert("Gemfile.lock".to_string(), lock);
    let run = |files: &BTreeMap<String, String>| {
        let rewrite = rewrite_registry_redirect(files, std::slice::from_ref(&candidate.dep));
        let confirmed = confirm(files, &rewrite, std::slice::from_ref(&candidate), false);
        (rewrite, confirmed)
    };
    let (first, confirmed) = run(&files);
    assert_eq!(confirmed.len(), 1, "{:?}", first.warnings);

    // Rescan with the pair intact: still confirmed, nothing written.
    let mut pinned = files.clone();
    pinned.insert("Gemfile".to_string(), first.files["Gemfile"].clone());
    pinned.insert("Gemfile.lock".to_string(), first.files["Gemfile.lock"].clone());
    let (again, confirmed) = run(&pinned);
    assert!(again.files.is_empty(), "{:?}", again.files.keys());
    assert_eq!(confirmed.len(), 1, "{:?}", again.warnings);

    // The Gemfile reverted: the lock still names the patch registry.
    let mut orphan = pinned;
    orphan.insert("Gemfile".to_string(), gemfile);
    let (rewrite, confirmed) = run(&orphan);
    assert!(rewrite.warnings.iter().any(|w| w.code == "redirect_gem_source_option"));
    assert!(rewrite.files.is_empty(), "{:?}", rewrite.files.keys());
    assert!(confirmed.is_empty(), "{confirmed:?}");
}
$ cargo test -p socket-patch-core --lib a_gem_lock_pin_whose_gemfile_lost_its_source_is_not_confirmed
test hosted::engine::tests::a_gem_lock_pin_whose_gemfile_lost_its_source_is_not_confirmed ... FAILED
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 8642 filtered out

Planned fix

  1. The gem rewriter reports the patches whose pair it found or left whole: the check its "never a half-applied pair" rule already makes (the manifest gives the gem the index URL as its one source, and the lock resolves it from a GEM section naming that remote, with a source-pinned DEPENDENCIES entry). confirm decides a gem by that report and never by substring, as it does for Cargo. A refused gem then gets the skipped / redirect_unconfirmed event and the "No patches could be switched to hosted" lines that the berry twin gets, and the in-run VEX no longer exempts it. The exit code stays 0, as the contract says of every hosted skip.
  2. When the source that blocks the gem is socket-patch's own patch registry for this patch, the redirect_gem_source_option detail says so and gives the remedy from docs/ecosystems.md: the Gemfile no longer names the source the lock resolves the gem from; run bundle install once without frozen mode, then scan again.

The skip itself is the documented behavior (CLI_CONTRACT.md, line 2658: "The gem is skipped, nothing written for it"), so the fix changes what the run reports, not what it writes. Whether a scan should instead put the option back, since the lock half is already in place, is a separate question and not part of this.

The vendored twin (a Gemfile that loses its path: while the lock keeps its PATH section) ran clean, over a file-only Bundler project and the repository's vendor-service fixture: vendor --check exits 1 with "wiring missing … re-run socket-patch vendor to rewire it", vex writes no statement, and vendor puts the option back and reports Vendored 1 package, after which the pair is whole and vendor --vex attests it. It goes through the vendor backend and the ledger's wiring check, not through confirm. One line there is off: vendor --dry-run over that state says 1 package is already vendored; nothing to do.


Generated by Claude Code

Vorherrschende Sprache
Rust
Sterne
9
Forks
2
Ø Merge
1 T. 8 Std.
Gemergte PRs (30 T.)
313

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus SocketDev/socket-patch

Alle Issues in SocketDev/socket-patch

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.