Skip to content

The "copy may still hold the patch" rule is written twice and drifted: cargo GC keeps a missing-blob modified copy, rollback's other-scope check drops it #1926

Open

Description

[agent] Filed by the scheduled architecture audit (audit-cli-tests). Register: T44.

Kind: refactor (with a latent drift). Register ID: T44.

Problem

Two CLI sites each turn a core verify_file_rollback result into "this installed copy may still hold the patched bytes, so keep the manifest record".

Copy A: ecosystem_dispatch.rs L64-L84 (cargo copies still patched):

let still = match v.status {
    VerifyRollbackStatus::Ready | VerifyRollbackStatus::MissingBlob => true,
    VerifyRollbackStatus::NotFound => !v.is_absent(),
    VerifyRollbackStatus::AlreadyOriginal | VerifyRollbackStatus::HashMismatch => false,
};

Copy B: rollback.rs L324-L345 (rollback's other-scope check):

let unverifiable = v.current_hash.is_none()
    && v.status != VerifyRollbackStatus::AlreadyOriginal
    && !v.is_absent();
if unverifiable || v.status == VerifyRollbackStatus::Ready
    || v.current_hash.as_deref() == Some(info.after_hash.as_str())

The drift. Core verify_file_rollback L229-L320 returns MissingBlob after the AlreadyOriginal check but before the current == after check. A MissingBlob result therefore says nothing about whether the copy is patched, and the two copies read it differently:

  • A file whose hash is neither before nor after, with the before-blob gone:
    • copy A says still patched (keep);
    • copy B says not patched.
  • npm's LF-shebang form of a patched bin (where source_hash == after_hash but current_hash != after_hash), with the blob gone:
    • copy B misses it;
    • copy A never sees npm.

I verified this by reading both copies and the core function. The rule is deterministic, so the two answers follow from the code. I did not run it end to end.

Proposed change

  1. Add VerifyRollbackResult::may_hold_patch(&self, info: &PatchFileInfo) -> bool in core patch/rollback.rs, next to the function whose status order it depends on. Pick one meaning for MissingBlob. Keeping the copy (copy A) is the fail-safe choice, because dropping the record strands patched bytes.
  2. Use it from both sites.
  3. Optionally, carry the npm-bin source_hash match in the result, so the predicate needs no CLI knowledge.

Size

3 files, roughly -25/+20 lines.

Acceptance criteria

  • Tests that guard the change:
    • the cargo_copies_still_patched callers (scan --prune, rollback);
    • rollback's other-scope tests.
  • Add one core unit test of may_hold_patch over every VerifyRollbackStatus, including MissingBlob with a third hash.
  • The PR states which MissingBlob reading it chose.

Activity

  1. added
    arch-audit
    refactor
    on Nov 1, 2026
  2. mikolalysenko commented on Nov 1, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Triaged: priority:p3 (cross-cutting rollback/GC refactor; latent drift in the MissingBlob reading between ecosystem_dispatch.rs and rollback.rs). No duplicate or open fix PR found.


    Generated by Claude Code

  3. added a commit that references this issue on Nov 26, 2026
  4. mikolalysenko commented on Nov 29, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] A third copy of this rule, run end to end on main at cfc870f. It gives a wrong outcome with every blob present.

    The copy. holds_patched_bytes (CLI commands/rollback.rs, lines 4286 to 4313) answers "does this copy hold any of the record's patched bytes" by hashing each file and comparing the result with the afterHash. It does not go through core's verify_file_rollback, so it does not know npm's LF-shebang form of a patched bin. patched_in_other_scope, in the same file, was taught that form by #1757 (lines 377 to 381: "Ready also covers npm's LF-shebang form of a patched bin"). #2119 then put holds_patched_bytes in front of every ecosystem: when a newer patch replaces a recorded one and drops one of its files, rollback_pass (CLI commands/agent_download.rs, line 1018) uses it to choose between "refuse the replacement" and "the old patch is not on this copy".

    What a user sees. A package lists a bin file that was published with a CRLF shebang. npm 10.9.9, pnpm 10.28.0 and Bun 1.4.4 install it with the first \r\n turned into \n (Yarn 1.22.22 keeps it), and apply writes that form for a linked bin under all four. The recorded patch aaaaaaaa…0001 changes that bin (cli.js) and index.js. A developer adds a line to index.js. A newer patch …0002 that lists neither file arrives, and scan runs:

    $ socket-patch scan --mode agent --yes
    Found 1 package (1 npm)
    1 package has a newer patch available.
      [update] pkg:npm/crlfbin@1.0.0 (replacing aaaaaaaa)
    Warning: pkg:npm/crlfbin@1.0.0: the replaced patch aaaaaaaa was not rolled back in ./node_modules/crlfbin: no file there holds its patched bytes (Cannot roll back: package/index.js - File holds neither the patched nor the original content. Cannot safely roll back). A file edited after that patch was applied stays as it is. Reinstall the package to get the original files back.
    Applied 1 patch.
      HIGH  crlfbin@1.0.0  CVE-2026-0001
    $ echo $?
    0
    

    "No file there holds its patched bytes" is false: cli.js holds the first patch. The record is replaced, so nothing lists cli.js any more:

    manifest before:  {'pkg:npm/crlfbin@1.0.0': ('0001', ['cli.js', 'index.js'])}
    manifest after:   {'pkg:npm/crlfbin@1.0.0': ('0002', ['lf.js'])}
    $ ./node_modules/.bin/crlfbin
    crlfbin AFTER                      # the first patch's content
    $ socket-patch rollback --yes      # exit 0, the manifest is now empty
    $ ./node_modules/.bin/crlfbin
    crlfbin AFTER                      # still patched, and no record to roll it back with
    

    Under --json the same run has status: success, the events updated and applied, and the warning replaced_patch_not_on_copy.

    The control is the same package, edit and pair of patches, with the patched bin published with an LF shebang (lf.js). The check sees the patched bytes and the replacement is refused, as #2119 intended:

    $ socket-patch scan --mode agent --yes
      [update] pkg:npm/crlfbin@1.0.0 (replacing aaaaaaaa)
      [fail] pkg:npm/crlfbin@1.0.0 (the recorded patch aaaaaaaa could not be rolled back first: Cannot roll back: package/index.js - File holds neither the patched nor the original content. Cannot safely roll back. That patch stays recorded and applied. Reinstall the package to get the original files back, then run this command again.)
    No changes to ./.socket/manifest.json (1 failed).
    $ echo $?
    1
    

    With no edit to index.js, both shapes are fine: the dry rollback passes, the first patch's two files are restored and the new patch is applied.

    For the fix. The may_hold_patch predicate proposed above should serve this site too, so that the LF-shebang form is answered in one place. copy_carries_record (lines 4330 to 4363 of the same file) makes the same raw comparison. Its one caller asks about release variants of one purl, which npm packages do not have, so I read it and did not run it.

    It takes three things at once (a bin with a CRLF shebang, a local edit to another patched file, and a newer patch that drops the bin), so the harm is rare: one file left at the old patch's content with no record.

    How to run it

    A package crlfbin@1.0.0 with "bin": {"crlfbin": "cli.js", "lfbin": "lf.js"}; cli.js starts #!/usr/bin/env node\r\n, lf.js starts #!/usr/bin/env node\n. Serve its packument and tarball from a directory on loopback and install with npm install --registry http://127.0.0.1:<port>/. A stand-in for the public patch proxy (SOCKET_PROXY_URL) answers the batch, by-package, view and blob routes from a catalog.

    1. Catalog with the first patch alone (package/cli.js and package/index.js): socket-patch scan --mode agent --yes.
    2. echo "console.log('debugging')" >> node_modules/crlfbin/index.js
    3. Catalog with the first patch and a second one for the same purl, a later publishedAt, listing package/lf.js alone: socket-patch scan --mode agent --yes.

    For the control, the first patch lists package/lf.js and package/index.js, and the second package/cli.js.


    Generated by Claude Code

  5. added a commit that references this issue on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedarch-auditpriority:p3refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions