Skip to content

Let a patch fix a package's file permissions in agent mode - #2433

Merged
Mikola Lysenko (mikolalysenko) merged 9 commits into
mainfrom
fix/agent-apply-tighten-modes
Dec 2, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 9 commits into
mainfrom
fix/agent-apply-tighten-modes

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

The problem

A release can be vulnerable through its file permissions alone. octopoller 0.2.0 (GHSA-26qj-cr27-r5c4) shipped its files world-writable (0666), so any local user can edit lib/octopoller.rb and have the next app that loads it run their code. The fix, 0.3.0, is the same code repacked as 0644.

That fix could not be delivered in agent mode:

  • An in-place apply writes a file's content and keeps its mode bit for bit.
  • A patch record had no way to say that a file's permissions are part of the fix, and a fix that changes no content had no record at all.

A fresh hosted or vendored install is not affected: the patched archive already gives every file 0644 or 0755, and the vendored extraction does the same. (An install made before the patch was pinned is; see "Not in this PR".)

The change

A file entry of a patch record may carry a mode ("0644", "0755"). It may do so when its content is unchanged (beforeHash equal to afterHash), which is how a permissions-only fix is recorded.

  • What the mode means. It is an upper bound on write bits, never a mode to set. A file is patched when its content hashes to afterHash and it has no group-write or other-write bit the mode lacks. Read, execute, owner, setuid, setgid and sticky bits are never read or changed.
  • apply clears exactly those bits, through the same stage and rename as a content change, so a file hard-linked into a store is replaced and its other links are untouched. A file that already holds the patched bytes is reported with appliedVia: "permissions".
  • apply --check and vex judge a copy patched in place by the same rule: patched bytes under an excess write bit are not applied yet.
  • Hosted and vendored copies are judged by their content alone, exactly as before, whatever mode their entries have. Their files come from the patched archive, and the bits an installer or a git checkout leaves on them follow the umask. The same holds for a local Go patch copy under .socket/go-patches/ and for a copy conda built: both are written and judged without the mode.
  • A native gem is not rebuilt for an entry that fixes a file's permissions alone, and a gem copy that does not hold that entry's file takes the record without it.
  • rollback and remove restore content only. They never give back a write bit a patch cleared.
  • Off Unix a file has no such bits and a mode changes nothing.

On a real install of octopoller 0.2.0 (old system Bundler, files at 0666/0777), with a record whose 22 entries have unchanged content and a mode:

$ socket-patch apply --check
Error: Patches are OUT OF SYNC:
  pkg:gem/octopoller@0.2.0: patch not applied (an installed copy is still unpatched)
$ socket-patch apply
Patched packages:
  pkg:gem/octopoller@0.2.0 (via permissions)
$ socket-patch apply --check
Patches are in sync (1 patch checked).

Afterwards the files are 0644/0755 and none is group- or world-writable. vex attests the advisory only then.

Backward compatibility

Records without a mode behave exactly as before. That includes an entry whose content is unchanged and has no mode, which stays "already patched" as it is today.

I ran the same commands with a build of main and a build of this branch on two identical projects whose manifests have no mode:

  • an npm project with one content patch: apply --check, apply, apply again, list, vex, a remove of another record (which saves the manifest) and rollback;
  • the octopoller install above with a record of 22 unchanged-content entries and no mode, its files at 0666: apply --check, apply, vex and rollback.

Every stdout (the --json envelopes), stderr and exit code is the same, and so are the manifest bytes after the apply, after the save and after the rollback, and the modes of the installed files: 44 files compared, 43 identical. The one difference is the random @id and the timestamps of a VEX document. The second project reads as "already patched" and keeps 0666 under both builds, as it does today.

A mode this release cannot read fails only its own patch. Any spelling other than 0 and three octal digits, or any other JSON value, still loads: the manifest and the API response parse, every other patch works, and the value is written back exactly as it was read. The patch it belongs to is never reported as applied. apply fails it (errorCode: apply_failed, cause: unreadable_file_mode) before writing any of its files, under --force too. apply --check and vex report unreadable_file_mode for it. rollback, remove, and hosted and vendored use do not read the mode and work as usual.

One request parameter is new. Every request to a JSON patch endpoint carries capabilities=file-modes. A service that does not know the parameter ignores it. I checked that against the live service with this build: by-package, view, batch and package all answered as before, on the public proxy and on the authenticated org API, and an agent-mode and a vendored scan completed. No URL with the parameter is written to a lockfile or a ledger or compared with a stored URL, and artifact URLs (what a patch mirror is asked for) do not carry it.

Rust API. PatchFileInfo gains the public field mode and VerifyStatus the variant UnreadableMode: a source break for code that builds a PatchFileInfo by hand or matches VerifyStatus exhaustively. The JSON forms only gain an optional key.

Older releases and a shared manifest. A release that predates mode ignores it and reads an entry with unchanged content as already patched. The parameter above lets the patch service keep such entries from those releases. It cannot cover a manifest shared with one: any of its commands that saves the manifest writes every entry back without its mode, and the record then reads as applied to every release, this one included, until it is removed and downloaded again. CLI_CONTRACT.md says so.

Not in this PR

  • Needs a matching change in Socket's backend before any patch carries a mode. Until then nothing sends one, and this change is inert apart from the request parameter. The service must answer by the parameter everywhere, cached views included: a view cached for a release that named the capability must not be served to one that did not.
  • A warm hosted install of a permissions-only patch. Hosted and vendored copies are judged by content, so an install made from the upstream release before such a patch was pinned (same content, old modes) is not seen as stale and is not reinstalled. It keeps its modes until it is installed again from the patched archive: for a gem, by removing its specifications entry and cached .gem and running bundle install, as the stale-install guard describes (bundle pristine reinstalls from the cached upstream archive). Reading the mode in the hosted gem check would close this; an earlier revision of this branch did, and the review below found it reported copies as stale that no reinstall changes, so it is left for a change that can be tested against a real hosted install.
  • A guard for the shared-manifest case above (treating a record made only of unchanged entries without a mode as unverifiable). It would change how existing records read, so I left it out.

Review

/code-review high reported ten findings, none about records without a mode. Fixed:

  • A tolerant mismatch policy (--force, and the default for content an earlier Socket patch wrote) could write a file of a record whose mode cannot be read, and report it as applied. Such a record is now refused before any file is verified, in the file engine, the jar swap and the gem apply.
  • A permissions-only entry under a gem's ext/ put the patch on the native rebuild path.
  • Hosted and vendored paths read the mode (a vendored package's installed copies, the hosted stale-install check and attestation, the unwinding of a hosted gem, a compiled conda build), which reported copies as stale or out of sync under a umask no reinstall changes. They are content-only again.
  • Inside a grouped (vendored or hosted) run, a change that had to clear bits renamed the file while it was still being staged. It is refused there instead; no in-place apply runs inside one.

A second /code-review high, of those fixes alone, reported six more, again all about records with a mode. Fixed:

  • vex and apply --check refuse a record with an unreadable mode as a whole, as apply does, and a Maven record with one is not split across Gradle hash directories, so no part of it is written.
  • A package wired to a hosted patch is judged by content wherever its copies are found (Composer and NuGet installs were read through the in-place rule), and so are a copy conda built and a local Go patch copy.
  • A permissions-only entry of a file a gem copy does not hold failed the record as a missing file.

Bugbot's one finding (a compiled conda build could be written to for a mode) is the conda item above; its thread has the fix and a test.

Not changed:

  • Offline and blobs. A permissions-only write reads no blob, but an --offline run still stops when the record's blobs are missing, like for any record. The contract now says so. Skipping the preflight for such entries would also skip it for an entry whose file holds other content and does need the blob.
  • Cached views on the public proxy are the backend item above.

Testing

  • cargo test --workspace --locked --no-fail-fast on the fixes, before the last merge of main: 15,522 passed, 531 ignored, 2 failed. The two failures are e2e_vendor_cargo_build's old-toolchain tests, which cannot start on this machine (launching the old cargo fails with Bad CPU type in executable) and do not touch this change. After merging the one commit main gained since, the core library tests (8,849) and the CLI's unit, apply, get, scan and in-process gem suites pass.
  • cargo clippy --locked --workspace --all-features -- -D warnings and cargo fmt --all -- --check are clean. The npm schema tests pass (11 of 11).
  • Windows: a pull request's CI runs no Windows job, so I dispatched the Windows-only run for this branch (ci.yml, os=windows). All 24 of its jobs passed, both Windows test shards included.
  • CI on the first push: 114 of 116 checks passed. The failure was a stack overflow in in_process_gem_multi_platform under the coverage build (and the aggregate check). The mode check had deepened the async call chain of every command that verifies a file. File verification is now main's content check plus a separate mode step, both behind boxed futures; the deepest tests of that suite pass with a 1.4 to 1.5 MiB thread stack here, where main needs 1.9 MiB.
  • Two checks that are red on this PR are not caused by it: pnpm hosted compatibility fails on main as well (pnpm 11.0.0; Keep a vendored pnpm patch when pnpm 11.0.0 to 11.0.2 rewrite its lock entry without the tarball #2434 fixes it), and the Windows nokogiri job timed out downloading libiconv during its native build.
  • New tests: patch/apply_file_mode_tests.rs (20 tests: bits cleared and nothing else, hard-linked copies, dry run, rollback, content plus mode in one write, an unreadable mode under every mismatch policy), the manifest and API round-trips of a mode in any spelling, vex in place against content-only and hosted-wired copies, the vendored artifact check, and the native-gem classification and record narrowing.
  • Command level, with this build: the real octopoller install above; a two-patch project where one record has "mode": "644" and another "mode": 420 (the other patch applies, the affected files are untouched, exit 1, also under --force; apply --check and vex name the unreadable mode; a manifest save writes both values back as they were; rollback works); the before/after comparison against main; and the live-service requests.
  • Not exercised: the suites that are ignored locally (live service, Docker and toolchain suites), a hosted install end to end with a record that carries a mode, and a conda build or a local Go patch copy with one (those two take the record without its modes, which has a unit test).

🤖 Generated with Claude Code

A release can ship its files world-writable and be fixed by a repack with
the same content (octopoller 0.2.0, GHSA-26qj-cr27-r5c4). Hosted and
vendored installs already get the patched archive's modes. An in-place
apply kept the installer's mode bit for bit, and a fix with no content
change could not be recorded at all.

A file entry of a patch record may now carry `mode` ("0644", "0755"), also
when its content is unchanged (`beforeHash` equal to `afterHash`):

- The mode is an upper bound on write bits. A file is patched when its
  content hashes to `afterHash` and it has no group-write or other-write
  bit the mode lacks. Nothing else of the file's mode is read or changed.
- `apply` clears exactly those bits through the usual stage and rename,
  and reports the file with `appliedVia: "permissions"`. It needs no blob
  for a file that already holds the patched bytes.
- `apply --check`, `vex` and the hosted stale-install check judge an
  installed copy by the same rule. A committed vendored artifact is judged
  by its content alone, as before.
- `rollback` and `remove` restore content only and keep the tightened mode.
- A `mode` in a spelling this release cannot read fails only its own patch
  (`unreadable_file_mode`): the manifest still loads, every other patch
  works, and the value is written back as it was read.

Records without a `mode` behave exactly as before, and their manifests
serialize byte for byte as before.

Patch requests now carry `capabilities=file-modes`, so the patch service
can keep such entries from releases that would read unchanged content as
"already patched". A service that does not know the parameter ignores it.
Needs a matching change in Socket's backend before any patch carries a
mode.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…en-modes

# Conflicts:
#	crates/socket-patch-cli/src/commands/agent_download.rs
…s record

The wheel matcher added on main tells a record of another artifact from the
installed copy's by content. A file whose entry has a mode this release
cannot read holds content the entry describes, so it does not tell the two
apart; applying the record reports the mode.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…oes under one

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ne before any policy

Review fixes:

- A record with a file mode this release cannot read is refused before any
  file is verified, in the file engine, the jar swap and the gem apply. A
  tolerant mismatch policy could otherwise write a file that holds neither
  side of its entry and report the patch as applied.
- Hosted and vendored copies are judged by their content alone again, as on
  main: a vendored artifact and its installed copies, hosted installs, the
  stale-install checks, the unwinding of a hosted gem and a compiled conda
  build. Their bits follow the installer's umask or a git checkout, so
  reading the mode there reported copies as stale that no reinstall changes.
  `vex` and `apply --check` read the mode for a copy patched in place.
- An entry that fixes a file's permissions alone is no native build input
  of a gem.
- Inside a grouped run a change that must clear bits is refused instead of
  renaming the file while it is being staged.

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

cursor Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes agent-mode apply/verify/vex semantics and API negotiation; mistakes could mis-report patch state or mishandle permissions on Unix installs, though content-only and legacy records stay unchanged.

Overview
Patch records can now include an optional mode on file entries ("0644", "0755", etc.), so agent-mode fixes can tighten write permission bits without changing content (beforeHash == afterHash). Apply treats mode as a ceiling on group/other write bits: patched content with excess write bits is still “unpatched” until apply clears those bits via the same staged rename path; telemetry uses appliedVia: "permissions" when only bits change.

Core adds FileMode, VerifyStatus::UnreadableMode, split between verify_file_patch (content + modes) and verify_file_content (content only for vendored/hosted checks). Unreadable mode values fail only that patch with unreadable_file_mode. All patch API requests now send capabilities=file-modes; CLI_CONTRACT.md and JSON envelopes document the behavior. Tests and fixtures wire mode: None and query parsing tolerates extra query params.

Reviewed by Cursor Bugbot for commit 4be5b2a. Configure here.

…ristine

The cached archive of such an install is the upstream one, so
`bundle pristine` brings the same files back.

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 4be5b2a. Configure here.

…by content

CI: the coverage build overflowed a 2 MiB test thread in the in-process
gem suite. The mode check had added an async layer and two call sites to
the verify and apply paths, which sit under the deepest commands. File
verification is now main's content check plus a separate mode step, both
handed out as boxed futures, and the permissions-only write is one boxed
call. The suite's deepest tests need about 1.4 MiB here, against 1.9 MiB
on main.

Review of the previous fixes:

- A record with an unreadable mode is refused as a whole by `vex` and
  `apply --check` too, and a Maven record is no longer split across Gradle
  hash dirs when it has one, so no part of it is written.
- A package wired to a hosted patch is judged by content wherever its
  copies are found (Composer and NuGet have no consumed-copy list), and so
  are a copy conda built and a local Go patch copy, which are also written
  without the mode.
- A gem copy that lacks the file of a permissions-only entry takes the
  record without it, instead of failing on a missing build input.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A compiled conda build that holds a permissions-only record's content is
reported as patched and left as it is, loose bits included; a record that
changes content is still refused.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) added this pull request to the merge queue Dec 2, 2026
Merged via the queue into main with commit 762cec2 Dec 2, 2026
121 of 122 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the fix/agent-apply-tighten-modes branch December 2, 2026 04:26
Mikola Lysenko (mikolalysenko) pushed a commit that referenced this pull request Dec 2, 2026
Resolve the conflict with main's file-mode support (#2433) and its boxed
engine futures (#2432):
- keep one `boxed` helper, main's (both sides added the same function);
- thread main's new `mode` argument of `stage_write` through this branch's
  shared `stage`; the optional RECORD write passes `None`, so it clears no
  permission bits;
- compute the sidecar fixup files once, before the RECORD plan, as this
  branch does, reading main's `(file, AppliedVia)` entries of `written`;
- give this branch's tests the new `mode` field and argument.

Assisted-by: Claude Code:claude-opus-5-5
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