Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .agents/skills/verify-lane-schedule/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ With every fact TRUE in the code, setting each fact FALSE in the model alone fai

## Coverage record

Sync commit: `0f5979dd0589da6224230f74e9bc7d15143ef5d1` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it).
Sync commit: `e1d4a797802da859205fc67494aa719ff23e68a3` (the last commit at which someone confirmed that the model describes this code; only `check.sh --record` moves it).

Every declaration in these files is covered:

Expand Down
4 changes: 3 additions & 1 deletion .agents/skills/verify-lane-schedule/model/fingerprints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ internal/githttp/hook.go:const_reservationDenied f72c8a7d40a9207271da47cf160c52e
internal/githttp/hook.go:const_reservationJIT d051ee28340ba848f8282f31689c8bbdde129b4970df2be75292d3b4486c429c
internal/githttp/hook.go:const_reservationNeedsVerify b8ec7eeca735e84bb9aa7aa596b7961d17778741956392c03d71116285ff5730
internal/githttp/hook.go:denyAuthorityDFConflict 3ad4a9b7cd317dbae7ebf368256b119a33774f58734c26fef36d47545f50ba5b
internal/githttp/hook.go:denyUnregisteredTarget a07930a318a3bd3488190e6869d6ba1d7589354257c8b832d66e43b733ad665c
internal/githttp/hook.go:enforceRefPolicy 2cd2cf6aea243221e8fe27280a6333695d4455a0304e7f536f7e8729dbb17a4d
internal/githttp/hook.go:enforceSyncClaimFence efb7b07e54bbb4008ac0cf53778c2ba690ad936051c103421aaf308b52e1cdf9
internal/githttp/hook.go:evalRefPolicy b232fd0a747ced157cec1bc4588e53bc13c54a39ff829f45108ae62c9601b5c8
Expand Down Expand Up @@ -248,7 +249,7 @@ internal/githttp/hook.go:type_refValuesProber 0389595e3fb341c7e67784d27674d922dd
internal/githttp/hook.go:type_reservationClass 48d346b5a457165c93ed2a9aca43bcd39438f05201427fa2351d8b3a4066ff29
internal/githttp/hook.go:type_sigVerifier afbcc825325e58413e4a3646073bab61862ebda41b2c8c9f6fefae2ad35a4745
internal/githttp/hook.go:updateBarrierEachPath 53afd6c803fba2472970b4a81e706f4e6c975c84753c0c94714bd52c1fdd0fcf
internal/githttp/hook.go:updateHook 2d7d924d17781fd711f5c8395f2b0e3d4511ef5e12e4643ef8c8716a607f670f
internal/githttp/hook.go:updateHook a502272c8ecb19db2bf09a988ed121d8b7be88f4fff859137bf1714f60e0cb6d
internal/githttp/hook.go:var_WrapHookRefStore 2a84a08163beaeaef666d54ff198e62e68b8e70a83fa6f46f9025b30c28ce5c5
internal/githttp/hook.go:var__ 14f3ac5e5fb70c7fe12bcffeaa3f5891a5f61b624d170427d94afa8153dcf5fd
internal/githttp/hook.go:var__#2 d54a83bcc2c7fb6caf4b5040f8c28ca780368fb5c2c578f05c5a48f6edb6dd97
Expand All @@ -268,6 +269,7 @@ internal/githttp/hook.go:var_pushRepoDirLen 9db02c4285217113dfb520f541a4af97e86a
internal/githttp/hook.go:var_refPolicyCache 12635e37a2a1162463fb6f64c3fa934b9e3793394ff7847bc17bf2015a822e50
internal/githttp/hook.go:var_refPolicyParses e1a83d07f4b25c69151a945c0c6abb1110dd5710769f8c3c8296824f19c1d01b
internal/githttp/hook.go:var_refTxFlushAt f6baf6b2e0ccd94a65e622a30218ecf8e574c22c94141b0b99cc4fb2e01e55aa
internal/githttp/hook.go:var_unregisteredTargetCheck c3c0d603df53454230d740514760d74554900d1fb3fbc2d052036fed07e03802
internal/githttp/hook.go:verifySig 51a7f42302b43ac7155d125dd99bbaf3abd305fb30512c5357230bb333414778
internal/githttp/hook.go:verifySigDenied 478714e01ee647624c46ac6f5b5aa04c2c7bac29c61f3cee57bdf7bdc0142a56
internal/githttp/hydrate.go:BaseRepo 20aab83202d8a04a36232730cb5eb3f34f38261ec029f26db7bc44677aeb6a20
Expand Down
9 changes: 9 additions & 0 deletions docs/reference/http-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,15 @@ old behavior, read `default_branch` from `GET /repo` and send it as `base_ref`.
This follows the public OpenAPI/SDK contract, not demonstrated upstream cluster behavior
([contract audit](../explanation/wire-compatibility.md#server-allocated-branch-contract)).

#### Which commit the base and the tag target can name

The commit that `base_ref`, `base_branch` or `preferred_base` of `POST /repos/branches/create`, or
`ref` of `POST /repos/tags`, names must be in a pack that the repository's pack registry lists.
The server checks the registered packs, not the node's cache repository, because every other node
(and this node after it loses its cache) hydrates from the registered packs alone and cannot hold a
ref at any other object. A commit that only a cache holds answers `404`, like a commit that does
not exist, and a `base_ref` that names no usable commit still falls back to `preferred_base`.

#### Guarded branch create and move

`expected_target_sha` on `POST /repos/branches/create` (both path styles) guards the
Expand Down
29 changes: 26 additions & 3 deletions internal/api/handlers_write_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,29 @@ func wrefValidateCreateBranch(req *wrefCreateBranchRequest) (target string, allo
return target, allocate, guard, errs.err()
}

// wrefResolveHeld is gitread.ResolveRef for a write that stores the commit in a
// ref. The commit must also be in a pack that the registry lists for the
// repository: the node's cache can hold objects that no registered pack holds
// (a cache that was copied or edited out of band, or a registry that was rolled
// back), and a ref at such an object wedges every other node and this node
// after its cache is gone, because they hydrate from the registered packs
// alone. A commit that no registered pack holds answers like one that does not
// exist.
func (s *server) wrefResolveHeld(ctx context.Context, repoID, repoPath, spec string, ephemeral bool) (string, error) {
sha, err := gitread.ResolveRef(ctx, repoPath, spec, ephemeral)
if err != nil {
return "", err
}
kinds, err := gitread.RegisteredKinds(ctx, s.refStore, s.blobStore, repoPath, repoID, []string{sha})
if err != nil {
return "", fmt.Errorf("check that a registered pack holds %s: %w", sha, err)
}
if kinds[sha] != "commit" {
return "", gitread.ErrNotFound
}
return sha, nil
}

// handleCreateBranch resolves the base to a commit (branch, tag — peeled —
// or hex sha via gitread.ResolveRef; preferred_base is the fallback when
// the primary base is absent or names no ref, and a base whose commit git
Expand Down Expand Up @@ -436,7 +459,7 @@ func (s *server) handleCreateBranch(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
baseSHA := ""
if base != "" {
sha, err := gitread.ResolveRef(ctx, repoPath, base, req.BaseIsEphemeral)
sha, err := s.wrefResolveHeld(ctx, repo.RepoID, repoPath, base, req.BaseIsEphemeral)
switch {
case errors.Is(err, gitread.ErrNotFound):
// Fall through to preferred_base below (when given). An
Expand All @@ -449,7 +472,7 @@ func (s *server) handleCreateBranch(w http.ResponseWriter, r *http.Request) {
}
}
if baseSHA == "" && req.PreferredBase != "" {
sha, err := gitread.ResolveRef(ctx, repoPath, req.PreferredBase, req.BaseIsEphemeral)
sha, err := s.wrefResolveHeld(ctx, repo.RepoID, repoPath, req.PreferredBase, req.BaseIsEphemeral)
switch {
case errors.Is(err, gitread.ErrNotFound):
// Report below with the primary-base message.
Expand Down Expand Up @@ -752,7 +775,7 @@ func (s *server) handleCreateTag(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
sha, err := gitread.ResolveRef(r.Context(), repoPath, target, false)
sha, err := s.wrefResolveHeld(r.Context(), repo.RepoID, repoPath, target, false)
switch {
case errors.Is(err, gitread.ErrNotFound):
writeProblem(w, http.StatusNotFound, codeRefNotFound, "ref does not resolve to a commit")
Expand Down
72 changes: 72 additions & 0 deletions internal/api/registry_kinds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package api

import (
"context"
"errors"
"os"
"path/filepath"
"testing"

"github.com/coder/openstorage/internal/githttp"
"github.com/coder/openstorage/internal/gitread"
)

// RegisteredKinds answers from the registered packs alone: a commit that only
// the cache holds reads as missing, and a registered pack that the cache lacks
// (a fold replaced the packs since the cache was hydrated) is downloaded first,
// so that a healthy object never reads as missing.
func TestRegisteredKindsReadsTheRegisteredPacksOnly(t *testing.T) {
runBackends(t, func(t *testing.T, e *testEnv) {
fx := wrefSeedFixture(t, e)
ctx := context.Background()
repoPath, err := gitread.Materialize(ctx, e.refStore, e.blobStore, e.cacheDir, fx.repoID, "main")
if err != nil {
t.Fatal(err)
}
planted := plantCacheOnlyCommit(t, repoPath, fx.c2)
kinds, err := gitread.RegisteredKinds(ctx, e.refStore, e.blobStore, repoPath, fx.repoID, []string{fx.c1, fx.c2, fx.tagObj, planted})
if err != nil {
t.Fatal(err)
}
want := map[string]string{fx.c1: "commit", fx.c2: "commit", fx.tagObj: "tag", planted: "missing"}
for id, k := range want {
if kinds[id] != k {
t.Fatalf("kind of %s = %q, want %q (all: %v)", id, kinds[id], k, kinds)
}
}

// The cache loses its pack files: the registry still lists the packs.
packs, err := filepath.Glob(filepath.Join(repoPath, "objects", "pack", "pack-*"))
if err != nil || len(packs) == 0 {
t.Fatalf("pack files in the cache: %v, %v", packs, err)
}
for _, p := range packs {
if err := os.Remove(p); err != nil {
t.Fatal(err)
}
}
kinds, err = gitread.RegisteredKinds(ctx, e.refStore, e.blobStore, repoPath, fx.repoID, []string{fx.c2})
if err != nil || kinds[fx.c2] != "commit" {
t.Fatalf("after the cache lost its packs: %v, %v", kinds, err)
}
})
}

// With no way to fetch a pack that the cache lacks, the view refuses, so that
// a caller never reads "missing" for an object that a registered pack holds.
func TestRegisteredPackViewRefusesWhenAPackIsMissingAndCannotBeFetched(t *testing.T) {
e := testEnvironment(t, "sqlite")
fx := wrefSeedFixture(t, e)
repoPath := filepath.Join(t.TempDir(), "empty.git")
if err := os.MkdirAll(filepath.Join(repoPath, "objects", "pack"), 0o755); err != nil {
t.Fatal(err)
}
if _, err := githttp.NewRegisteredPackView(context.Background(), e.refStore, repoPath, fx.repoID, nil); !errors.Is(err, githttp.ErrPackMissing) {
t.Fatalf("NewRegisteredPackView with no heal = %v, want ErrPackMissing", err)
}
calls := 0
_, err := githttp.NewRegisteredPackView(context.Background(), e.refStore, repoPath, fx.repoID, func(context.Context) error { calls++; return nil })
if !errors.Is(err, githttp.ErrPackMissing) || calls != 1 {
t.Fatalf("NewRegisteredPackView with a heal that fetches nothing = %v after %d heals, want ErrPackMissing after 1", err, calls)
}
}
106 changes: 106 additions & 0 deletions internal/api/registry_target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package api

// A ref write must not store a commit that only the node's cache holds. The
// cache can hold objects that no registered pack holds (a cache that was copied
// or edited out of band, or a registry that was rolled back), and a ref at such
// an object wedges every other node, and this node after its cache is gone,
// because they hydrate from the registered packs alone.

import (
"context"
"net/http"
"os"
"os/exec"
"strings"
"testing"

"github.com/coder/openstorage/internal/gitread"
)

// plantCacheOnlyCommit writes a commit (a child of parent, with a new blob) as
// loose objects into the cache repository at repoPath only: no pack holds it.
func plantCacheOnlyCommit(t *testing.T, repoPath, parent string) string {
t.Helper()
run := func(stdin string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repoPath
for _, kv := range os.Environ() {
if !strings.HasPrefix(kv, "GIT_") {
cmd.Env = append(cmd.Env, kv)
}
}
cmd.Env = append(cmd.Env, "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null",
"GIT_AUTHOR_NAME=p", "GIT_AUTHOR_EMAIL=p@x.invalid", "GIT_COMMITTER_NAME=p", "GIT_COMMITTER_EMAIL=p@x.invalid")
cmd.Stdin = strings.NewReader(stdin)
out, err := cmd.Output()
if err != nil {
t.Fatalf("git %v: %v", args, err)
}
return strings.TrimSpace(string(out))
}
blob := run("cache only\n", "hash-object", "-w", "--stdin")
tree := run("100644 blob "+blob+"\tcacheonly.txt\n", "mktree")
return run("", "commit-tree", "-p", parent, "-m", "cache only", tree)
}

func TestRefWritesRefuseACommitThatOnlyTheCacheHolds(t *testing.T) {
type shape struct {
name, path string
body func(sha string) map[string]any
ref string // the ref that the request would create
}
shapes := []shape{
{"branch base_ref", "/api/v1/repos/branches/create", func(sha string) map[string]any {
return map[string]any{"target_branch": "co", "base_ref": sha}
}, "refs/heads/co"},
{"branch preferred_base", "/api/v1/repos/branches/create", func(sha string) map[string]any {
return map[string]any{"target_branch": "co", "base_ref": "nosuch", "preferred_base": sha}
}, "refs/heads/co"},
{"branch force", "/api/v1/repos/branches/create", func(sha string) map[string]any {
return map[string]any{"target_branch": "feature", "base_ref": sha, "force": true}
}, "refs/heads/feature"},
{"ephemeral branch", "/api/v1/repos/branches/create", func(sha string) map[string]any {
return map[string]any{"target_branch": "co", "base_ref": sha, "base_is_ephemeral": true, "target_is_ephemeral": true}
}, "refs/namespaces/ephemeral/refs/heads/co"},
{"tag", "/api/v1/repos/tags", func(sha string) map[string]any {
return map[string]any{"name": "co", "ref": sha}
}, "refs/tags/co"},
}
runBackends(t, func(t *testing.T, e *testEnv) {
for _, tc := range shapes {
t.Run(tc.name, func(t *testing.T) {
fx := wrefSeedFixture(t, e)
repoPath, err := gitread.Materialize(context.Background(), e.refStore, e.blobStore, e.cacheDir, fx.repoID, "main")
if err != nil {
t.Fatal(err)
}
planted := plantCacheOnlyCommit(t, repoPath, fx.c2)
srv := newWrefServer(t, e)
before := wrefRefs(t, e, fx.repoID)

resp, body := wrefDo(t, srv, http.MethodPost, tc.path, fx.token, tc.body(planted))
if resp.StatusCode != http.StatusNotFound {
t.Fatalf("a commit that only the cache holds: status %d, want 404, body %v", resp.StatusCode, body)
}
after := wrefRefs(t, e, fx.repoID)
if len(after) != len(before) || after[tc.ref] != before[tc.ref] {
t.Fatalf("refs changed after the refused request: before %v, after %v", before, after)
}
// Every other node hydrates from the registered packs alone.
if _, err := gitread.Materialize(context.Background(), e.refStore, e.blobStore, t.TempDir(), fx.repoID, "main"); err != nil {
t.Fatalf("a fresh node cannot hydrate the repository: %v", err)
}

// A commit that a registered pack holds still works.
resp, body = wrefDo(t, srv, http.MethodPost, tc.path, fx.token, tc.body(fx.c1))
if resp.StatusCode != http.StatusOK {
t.Fatalf("a commit that a pack holds: status %d, want 200, body %v", resp.StatusCode, body)
}
if got := wrefRefs(t, e, fx.repoID)[tc.ref]; got != fx.c1 {
t.Fatalf("%s = %q, want %s", tc.ref, got, fx.c1)
}
})
}
})
}
60 changes: 60 additions & 0 deletions internal/githttp/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,21 @@ func updateHook(ctx context.Context, args []string) (retErr error) {
return err
}
}
// The new value must be in a pack that the registry lists. git has already
// checked that the object is readable, but it reads the node's cache, which
// can hold objects that no registered pack holds (a cache that was copied or
// edited out of band, or a registry that was rolled back). A ref at such an
// object wedges every node that hydrates from the registered packs. A push
// that carries its objects is safe: pre-receive registered its pack before
// this hook runs, and git moved the pack into the cache. The case that this
// closes is an update to an object that no pack of this push carries.
// Checked on the raw store, before the CAS, and denied per ref like a policy
// denial. Deletions have no new value.
if new != ref.ZeroSHA {
if err := unregisteredTargetCheck(ctx, st, repo, refName, new); err != nil {
return err
Comment on lines +1853 to +1855

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Reject unregistered targets before atomic update hooks

For a multi-ref --atomic push where an earlier command targets a registered object and a later command targets a cache-only object, the earlier update hook commits its authority CAS before this later per-ref check rejects. Git then aborts the local atomic ref transaction and reports the entire push as failed, while the earlier authority update remains committed. The registered-target check needs a whole-push pre-receive gate for atomic pushes, before any update hook can run.

Useful? React with 👍 / 👎.

}
Comment on lines +1853 to +1856

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛡️ Codex Security Review

P2 Badge Security: Batch registered-target checks across push refs

With a git:write token and default-off rate limiting, an attacker can send one allowed no-pack push containing very many refs at an already registered commit. Git runs update once per ref, and this new call rebuilds a registered-pack view, calls ListPacks, creates two symlinks per pack, and launches git cat-file each time. The repository has no command-count cap, and receive-pack has no work slot or server deadline, so an N-ref push buys N backend reads, temp views, and Git children. Concurrent pushes can exhaust a node or shared store. Batch the SHAs in pre-receive or impose a strict command/work budget.

Useful? React with 👍 / 👎.

}
var inner ref.RefStore = st
if testMode() {
inner = injectCASDrop(inner)
Expand Down Expand Up @@ -2917,6 +2932,51 @@ func objectExists(sha string) bool {
panic(fmt.Sprintf("githttp assertion failed: cat-file -e %s: %v", sha, err))
}

// unregisteredTargetCheck is denyUnregisteredTarget. Tests that run the update
// hook on an object id that they invented, to reach a later step, replace it.
var unregisteredTargetCheck = denyUnregisteredTarget

// denyUnregisteredTarget refuses the update of refName to new when no pack that
// the registry lists for repo holds the object. It reads the cache repository
// through a RegisteredPackView, so only the registered packs answer. A registered
// pack that the cache lacks (a fold replaced the packs after the cache was
// hydrated) is downloaded first. An object that is no commit is left to the
// other checks: only "missing" is refused here.
func denyUnregisteredTarget(ctx context.Context, st ref.RefStore, repo, refName, new string) error {
repoPath, err := os.Getwd() // git runs hooks in the repository
if err != nil {
return fmt.Errorf("update: find the repository: %w", err)
}
view, err := NewRegisteredPackView(ctx, st, repoPath, repo, func(ctx context.Context) error {
bl, err := blob.Open(ctx, mustEnv(envHookBlobURL...))
if err != nil {
return err
}
defer bl.Close()
return HydratePacks(ctx, st, bl, repoPath, repo)
})
if err != nil {
return fmt.Errorf("update: read the registered packs: %w", err)
}
defer view.Close()
var out strings.Builder
cmd := exec.CommandContext(ctx, "git", "cat-file", "--batch-check=%(objecttype)")
cmd.Env = gitlimit.NoReplaceObjects(view.Env(os.Environ()))
cmd.Stdin = strings.NewReader(new + "\n")
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
return fmt.Errorf("update: read the object type of %s in the registered packs: %w", new, err)
}
kinds, err := ParseObjectKinds([]string{new}, out.String())
if err != nil {
return fmt.Errorf("update: %w", err)
}
if kinds[new] == "missing" {
return refusef("update: %s is at %s, which no pack of this repository's registry holds", refName, new)
Comment on lines +2970 to +2975

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Validate the registered object closure, not only the tip

When a crafted incoming pack contains the new commit or tag object but omits a referenced tree, parent, or tag target that exists only in this node's cache, receive-pack's connectivity check succeeds and the pack is registered. Since kinds[new] is then non-missing, this check accepts the ref even though a fresh node hydrating only registered packs cannot traverse it. Validate connectivity using the registered-pack-only view rather than checking only the direct object's type.

Useful? React with 👍 / 👎.

}
return nil
}

// objectInMainStore reports whether sha is resolvable WITHOUT the
// quarantine. git runs pre-receive with GIT_OBJECT_DIRECTORY pointed at the
// quarantine and the main store as an alternate (githooks(5)); stripping
Expand Down
11 changes: 11 additions & 0 deletions internal/githttp/hookcanary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ func canarySQLExec(t *testing.T, dbURL string, stmts ...string) {
}
}

// skipRegisteredTargetCheck lets a test run the update hook on an object id
// that it invented: the check that a registered pack holds the new value would
// refuse it before the step that the test wants to reach.
func skipRegisteredTargetCheck(t *testing.T) {
t.Helper()
prev := unregisteredTargetCheck
unregisteredTargetCheck = func(context.Context, ref.RefStore, string, string, string) error { return nil }
t.Cleanup(func() { unregisteredTargetCheck = prev })
}

// canaryTable is a unique name for a table that does not exist.
func canaryTable() string { return "canary_" + strings.ToLower(rand.Text()[:8]) }

Expand Down Expand Up @@ -118,6 +128,7 @@ func TestUpdateHookHidesStepErrors(t *testing.T) {
t.Run("WrapHookRefStore error", func(t *testing.T) {
canary := canaryTable()
records := hookEnv(t, canarySQLite(t))
skipRegisteredTargetCheck(t)
prev := WrapHookRefStore
t.Cleanup(func() { WrapHookRefStore = prev })
WrapHookRefStore = func(context.Context, ref.RefStore) (ref.RefStore, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/githttp/hookrefusal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestUpdateHookHidesStoreErrors(t *testing.T) {
st.Close()
c := pgCanary()
records := hookEnv(t, dbURL)
skipRegisteredTargetCheck(t)
prev := WrapHookRefStore
t.Cleanup(func() { WrapHookRefStore = prev })
WrapHookRefStore = func(_ context.Context, st ref.RefStore) (ref.RefStore, error) {
Expand Down
Loading
Loading