Skip to content

fix(serve): refuse rewinds, kind flips and dangling tips in pushed ref updates - #366

Merged
tonibergholm-codento merged 9 commits into
mainfrom
fix/serve-ref-ff
Nov 20, 2026
Merged

tonibergholm-codento merged 9 commits into
mainfrom
fix/serve-ref-ff

Conversation

@tonibergholm-codento

Copy link
Copy Markdown
Collaborator

sc serve trusted the pushing client to enforce fast-forward. UpdateRef only compared the old tip, so anyone with a read-write token, or on an open writable listener, could:

  • rewind or sideways-move any branch on the server,
  • point a branch at an absent object or a blob, which breaks clone, fetch and gc for everyone,
  • move a public branch onto a private-branch manifest.

The server's next sc gc then pruned the abandoned history. A served bucket (sc serve --store) had the same gap.

The server now decides a ref move itself, through the new ref_guard, on both the local and bucket paths:

  • Kind: the new tip must be a snapshot or private-branch manifest of the same kind as the current tip.
  • Ancestry: the current tip must be an ancestor. Snapshot parents must be snapshots, and a manifest's ancestry is its prev chain only.
  • Connectivity: the tip must be connected. Every parent, changed subtree, new leaf, secret, manifest closure entry, base and anchor that the push adds must be held and of the right kind.
    • The walk checks only what differs from an existing branch tip.
    • The local kind check reads one byte of each object.
  • Locking: the walk runs before the repo lock. Under the lock the server confirms the compare-and-swap, that the boundary tips it trusted are still branch tips, and that every object the walk relied on is still on disk. If anything moved, it refuses with "retry the push".
  • Bucket ingest: it now refuses a record whose re-encoding does not hash to its id.
  • Ref temp files: they are dot-prefixed and bounded in length, so branch listings no longer hide names such as release.tmp-fix.

Declined in review, with reasons:

  • Trusting existing tips as connected. History an older server accepted, or a partial clone's existing tips, are the server's own state. An incomplete tip already fails clones, and verifying every tip's closure would walk full history on each push.
  • Stale pack indexes after a repack. These already fail reads in any long-lived session today; a retry works.
  • A crash-leftover temp file from an older binary. It is parsed as a ref, exactly as on main today.
  • A damaged blob already on disk. That is storage corruption, not pushed input.

Still open, recorded in the threat model: a keyless writer can roll a private branch back to an older manifest's content under a new prev, because the index associated data does not bind prev.

Proof: ref_rules_hold runs on both transports and covers rewind, sideways move, kind flips, absent and wrong-kind targets, parents, roots, nested blobs, closure and secret entries. Every case failed with the check disabled. Further tests cover:

  • an object pruned between walk and lock (an_object_pruned_between_the_walk_and_the_lock_refuses_the_move)
  • a boundary branch deleted or rewritten before the lock (a_trusted_tip_that_stops_being_a_tip_refuses_the_move)
  • a non-canonical bucket record
  • dot-prefixed ref temp files
  • unchanged content never being read (unchanged_content_is_not_read)
  • a refused rewind over the wire, and private-branch pushes through grant and revoke

cargo test -p scl-repo -p scl-cli passes (1228).

Reviewed with sol-loop: 5 rounds with gpt-6.2-sol. Sol's last round found one should-fix, the boundary tips under the lock, which the last commit adds. A Mythos security review found the kind-flip and dangling-tip holes in round 1.

Created with Claude Opus 5.5 and Claude Sonnet 5.5 in Claude Code.

UpdateRef checked only compare-and-swap; the fast-forward check lived in the
pushing client, so any writer could rewind or sideways-move a served branch
or point it at an absent object or a blob. LocalTransport and BucketTransport
(so `sc serve --store` too) now refuse, under their own lock, a target that is
not a held snapshot or manifest, and a move whose current tip is not an
ancestor of the new one. A walk that hits an absent object fails closed.
Review of the server-side fast-forward check found it too weak.

- A manifest's `base` counted as a parent, so a writer could move a public
  branch to a private manifest (and a snapshot could name a manifest as a
  parent). The ancestry rule is now per kind: snapshot parents must be
  snapshots, a manifest's ancestors are its `prev` chain only, and the new
  tip must have the same kind as the current one.
- The walk stopped at the first parent equal to the current tip, so a commit
  with an absent second parent, root tree, nested blob or manifest closure
  entry landed and broke clone and fetch for the branch. New `ref_guard`
  module: a connectivity check like receive-pack's, covering only what the
  push added (it stops at existing ref tips and descends only into subtrees
  that differ from a parent's).
- Local: the check runs before the repo-wide lock, from `expected_old`; under
  the lock only `current == expected_old` and the stopped-op guard remain. It
  reads a fresh store view, so an object `sc gc` pruned while a served session
  still cached it is not trusted.
- Bucket: `PutPack` and `PutObject` refuse a record that decodes but is not in
  canonical encoding (the upload re-encodes, which stored it under an id it
  does not hash to). Pending objects keep decoded trees, snapshots and
  manifests so the check can walk what a push just uploaded.
- THREAT-MODEL: drop the overclaims, describe the rules, and record that a
  keyless writer can still roll a private branch back under a new `prev`.
…nd check entry kinds

Second review round on the server-side ref rule.

- Local: the walk uses the session's store again (no per-push pack index
  reload) and records every object the verdict relied on. Under the repo lock,
  which gc also takes, each is confirmed to still be on disk (new
  `Store::contains_on_disk`: a loose-file stat, or an indexed pack that still
  exists; a miss rescans the pack directory once). A gc prune between the
  walk and the lock, or of an object a long-lived session still caches, now
  refuses the move instead of leaving a dangling ref.
- A file entry's id was memoized in the same set as subtrees, so an id named
  as both a file and a directory skipped the subtree walk. Trees and leaves
  are memoized apart, a file entry must name a blob (or sealed object), and a
  tree entry must name a tree.
- `LocalTransport::list_refs` goes through `refs::list_heads`, which skips a
  ref's in-flight temp file and tolerates a ref that vanishes while the
  directory is read.
…iles

Third review round on the server-side ref rule.

- A manifest closure entry or a snapshot secret was only checked for
  presence. A closure entry naming the public base made reachability mark it
  seen without walking it, so clone and gc could omit its tree. Closure
  entries must now be sealed objects and registry entries secrets. Entries a
  parent snapshot or the previous manifest already lists are inside its
  verified closure and are not read again.
- The local walk now asks for an object's kind through `Store::peek_kind`,
  which reads the first encoded byte (loose or packed) instead of streaming
  the whole blob; `Object::kind_name_of_tag` maps the byte. Unchanged file
  entries in a changed directory were already skipped against the parent's
  tree; a counting test now pins that for files, secrets and closure entries.
  The bucket still decodes an on-chain object to learn its kind (its index
  has no kind), only for what the push changed.
- `list_heads` hid any branch whose name contained ".tmp". Ref writes now use
  a dot-prefixed temp file (no branch name can start with a dot) and the
  listing skips only dot-prefixed entries.
…lock

The unlocked walk stops at other branches' tips as already connected. If such
a branch was removed or rewritten before the lock was taken and gc pruned its
closure, the locked check (which only re-confirmed objects the walk read) could
publish a dangling ref.

`check_ref_move` now returns the tips the walk actually stopped at, and
`commit_ref_move` requires each to still be the tip of some local branch (one
`refs::list_heads` read under the lock) or refuses with the usual "retry the
push" error.

Bucket remotes: the manifest CAS the check read from is what the commit lands
against, so those tips are still tips, and compaction never deletes what the
chain names. The check skipped on a CAS retry is now reused only while the tips
it trusted are still in the refreshed view.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 751e81ac-354b-48a1-8ddc-e8328e794739
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T10:45:19.051534Z 7626ef1 New commits
🔒 Security Review ✅ Completed 2026-11-20T10:52:59.525573Z 7626ef1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@tonibergholm tonibergholm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed: sol-loop (5 rounds), Mythos security review of the final version (no blockers; its one finding applied), required checks green, Codex review has no findings.

@tonibergholm-codento
tonibergholm-codento merged commit 64f2241 into main Nov 20, 2026
11 checks passed
@tonibergholm-codento
tonibergholm-codento deleted the fix/serve-ref-ff branch November 20, 2026 10:53
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