ledger verify: bound the HTTP walk, refuse a HEAD that names no chain
carlos ledger verify used http.DefaultClient, so a status host that
accepted the connection and never answered hung the command for good.
It now uses a client with a 30 s per-request timeout.
A HEAD of {} or null decoded to the zero Head, the walk never started,
and Verify reported success with an empty head digest, which the verb
then sliced for display and panicked on. Verify now checks the HEAD
before walking: there is no empty-ledger HEAD (Append writes HEAD only
after its first entry), so every HEAD must carry a head_key, a 64-digit
lowercase sha256 and a count of one or more. The display slice goes
through short12, which never slices a short string.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+155
−3
apimode_test.go+56 −0cli.go+27 −3internal/ledger/verify.go+34 −0internal/ledger/verify_test.go+38 −0
CI cancelled — superseded by a newer push — run details
Intent
diff --git a/apimode_test.go b/apimode_test.go| index 185a87ed3..e9ab975e2 100644 |
| --- a/apimode_test.go |
| +++ b/apimode_test.go |
| @@ -2661,6 +2661,62 @@ func TestLedgerVerifyVerb(t *testing.T) { |
| } |
| } |
| +// TestLedgerVerifyRefusesEmptyHead: a status host that serves {} or null as |
| +// HEAD.json gets a refusal, not a "verified" line. Before the fix this |
| +// returned success with an empty head digest and the verb panicked slicing |
| +// it for display. |
| +func TestLedgerVerifyRefusesEmptyHead(t *testing.T) { |
| + for _, body := range []string{`{}`, `null`} { |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + w.Write([]byte(body)) |
| + })) |
| + res, err := verifyPublishedLedger(ts.URL+"/", "bab", "platform", "carbon") |
| + ts.Close() |
| + if err == nil { |
| + t.Fatalf("HEAD %s verified: %+v", body, res) |
| + } |
| + if !strings.Contains(err.Error(), "HEAD malformed") { |
| + t.Fatalf("HEAD %s: error = %v, want a malformed-HEAD refusal", body, err) |
| + } |
| + } |
| + if got := short12("abc"); got != "abc" { |
| + t.Fatalf("short12(abc) = %q", got) |
| + } |
| +} |
| + |
| +// TestLedgerVerifyIsBounded: a status host that accepts the request and never |
| +// answers fails the walk within ledgerVerifyTimeout instead of hanging the |
| +// command. The old verb used http.DefaultClient, which has no timeout. |
| +func TestLedgerVerifyIsBounded(t *testing.T) { |
| + hold := make(chan struct{}) |
| + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + select { |
| + case <-hold: |
| + case <-r.Context().Done(): |
| + } |
| + })) |
| + defer ts.Close() |
| + defer close(hold) |
| + |
| + old := ledgerVerifyTimeout |
| + ledgerVerifyTimeout = 200 * time.Millisecond |
| + defer func() { ledgerVerifyTimeout = old }() |
| + |
| + done := make(chan error, 1) |
| + go func() { |
| + _, err := verifyPublishedLedger(ts.URL, "bab", "platform", "carbon") |
| + done <- err |
| + }() |
| + select { |
| + case err := <-done: |
| + if err == nil { |
| + t.Fatal("a status host that never answered verified") |
| + } |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("ledger verify still waiting on a silent status host after 10s") |
| + } |
| +} |
| + |
| // ---- Task 3: config client methods (configGet/configApply/configSync) ---- |
| // TestAPISessionConfigGetFullShape pins configGet's decode of the full wire |
diff --git a/cli.go b/cli.go| index c9a63816c..26f6018cd 100644 |
| --- a/cli.go |
| +++ b/cli.go |
| @@ -3824,12 +3824,36 @@ func runLedgerVerify(args []string) { |
| log.Fatalf("ledger verify: invalid ledger name %q", name) |
| } |
| - g := ledger.HTTPGetter(http.DefaultClient, strings.TrimRight(*urlBase, "/")) |
| - res, err := ledger.Verify(context.Background(), g, *account, *app, name) |
| + res, err := verifyPublishedLedger(*urlBase, *account, *app, name) |
| if err != nil { |
| log.Fatal(err) |
| } |
| - fmt.Fprintf(stdout, "%s %d entries, head %s\n", colorFor(stdout).ok("verified:"), res.Count, res.HeadSHA256[:12]) |
| + fmt.Fprintf(stdout, "%s %d entries, head %s\n", colorFor(stdout).ok("verified:"), res.Count, short12(res.HeadSHA256)) |
| +} |
| + |
| +// ledgerVerifyTimeout bounds each GET of a `carlos ledger verify` walk, the |
| +// header wait and the body together. Every object is a HEAD or one entry of |
| +// at most ledger.MaxBlob, so 30 s is generous; without it a status host that |
| +// accepts the connection and never answers hangs the command for good. |
| +var ledgerVerifyTimeout = 30 * time.Second |
| + |
| +// verifyPublishedLedger walks a published ledger over plain HTTPS with a |
| +// bounded client. The public status host needs no credential, so this is |
| +// a client of its own rather than a console session's. |
| +func verifyPublishedLedger(urlBase, account, app, name string) (ledger.VerifyResult, error) { |
| + client := &http.Client{Timeout: ledgerVerifyTimeout} |
| + g := ledger.HTTPGetter(client, strings.TrimRight(urlBase, "/")) |
| + return ledger.Verify(context.Background(), g, account, app, name) |
| +} |
| + |
| +// short12 is the first 12 characters of a digest for display, or the whole |
| +// string when it is shorter: a display helper must never panic on a value a |
| +// remote server supplied. |
| +func short12(s string) string { |
| + if len(s) > 12 { |
| + return s[:12] |
| + } |
| + return s |
| } |
| // defaultGrantTTL is how long a scoped key lives when the operator does not |
diff --git a/internal/ledger/verify.go b/internal/ledger/verify.go| index 97cd9debb..a99628905 100644 |
| --- a/internal/ledger/verify.go |
| +++ b/internal/ledger/verify.go |
| @@ -38,6 +38,37 @@ func HTTPGetter(client *http.Client, base string) Getter { |
| } |
| } |
| +// checkHead refuses a HEAD that cannot name a chain. There is no empty |
| +// ledger HEAD: Append writes HEAD.json only after the entry it points at, so |
| +// a ledger with no entries has no HEAD object and the GET for it fails. |
| +// Every HEAD therefore names at least one entry: a non-empty head_key, that |
| +// entry's sha256 as 64 lowercase hex digits, and a count of one or more. |
| +// Without this a HEAD of {} or null decodes to the zero value, the walk never |
| +// starts, and the empty chain "verifies" with no head digest at all. |
| +func checkHead(h Head) error { |
| + switch { |
| + case h.Count < 1: |
| + return fmt.Errorf("ledger: HEAD malformed: count %d, a HEAD names at least one entry", h.Count) |
| + case h.HeadKey == "": |
| + return fmt.Errorf("ledger: HEAD malformed: no head_key") |
| + case !isSHA256Hex(h.HeadSHA256): |
| + return fmt.Errorf("ledger: HEAD malformed: head_sha256 %q is not a sha256 digest", h.HeadSHA256) |
| + } |
| + return nil |
| +} |
| + |
| +func isSHA256Hex(s string) bool { |
| + if len(s) != 64 { |
| + return false |
| + } |
| + for _, c := range s { |
| + if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| + return false |
| + } |
| + } |
| + return true |
| +} |
| + |
| type VerifyResult struct { |
| Count int |
| HeadSHA256 string |
| @@ -56,6 +87,9 @@ func Verify(ctx context.Context, get Getter, sqid, app, name string) (VerifyResu |
| if err := json.Unmarshal(hb, &head); err != nil { |
| return VerifyResult{}, fmt.Errorf("ledger: HEAD corrupt: %w", err) |
| } |
| + if err := checkHead(head); err != nil { |
| + return VerifyResult{}, err |
| + } |
| // The walk follows keys the CHAIN supplies (HEAD.json's head_key, then |
| // each entry's prev_key), so those keys are exactly as trustworthy as the |
diff --git a/internal/ledger/verify_test.go b/internal/ledger/verify_test.go| index 4139c2fab..c16eb1289 100644 |
| --- a/internal/ledger/verify_test.go |
| +++ b/internal/ledger/verify_test.go |
| @@ -134,3 +134,41 @@ func TestVerifyOverHTTP(t *testing.T) { |
| t.Fatalf("res=%+v err=%v", res, err) |
| } |
| } |
| + |
| +// TestVerifyRefusesMalformedHead: a HEAD that decodes but names no chain |
| +// ({}, null, a zero count, a missing key, a digest that is not a sha256) is |
| +// refused, never reported as a clean empty ledger. Before checkHead, {} and |
| +// null decoded to the zero Head, the walk never started, and Verify returned |
| +// success with an empty HeadSHA256, which the CLI then sliced and panicked on. |
| +func TestVerifyRefusesMalformedHead(t *testing.T) { |
| + ctx := context.Background() |
| + key := Prefix("bab", "platform", "carbon") + "entries/x.json" |
| + sha := strings.Repeat("a", 64) |
| + for name, head := range map[string]string{ |
| + "empty object": `{}`, |
| + "null": `null`, |
| + "zero count": `{"head_key":"` + key + `","head_sha256":"` + sha + `","count":0}`, |
| + "negative count": `{"count":-1}`, |
| + "no key": `{"head_sha256":"` + sha + `","count":1}`, |
| + "no digest": `{"head_key":"` + key + `","count":1}`, |
| + "short digest": `{"head_key":"` + key + `","head_sha256":"abc","count":1}`, |
| + "upper digest": `{"head_key":"` + key + `","head_sha256":"` + strings.Repeat("A", 64) + `","count":1}`, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + get := func(_ context.Context, k string) ([]byte, error) { |
| + if k == HeadKey("bab", "platform", "carbon") { |
| + return []byte(head), nil |
| + } |
| + t.Fatalf("walked to %s from a malformed HEAD", k) |
| + return nil, nil |
| + } |
| + res, err := Verify(ctx, get, "bab", "platform", "carbon") |
| + if err == nil { |
| + t.Fatalf("HEAD %s verified: %+v", head, res) |
| + } |
| + if !strings.Contains(err.Error(), "HEAD malformed") { |
| + t.Fatalf("error = %v, want it to say the HEAD is malformed", err) |
| + } |
| + }) |
| + } |
| +} |