carlos / platform Public

release: per-box channel watermark, and the account binding required on read (hunt H3-M5, H3-M8)

H3-M5. A box verified a channel pointer's signature, account, app, channel
and shape, never that its promoted_at had advanced; the digest ratchet only
refused digestless pointers. A bucket writer could put a saved, correctly
signed older pointer back and move every box from v2 to v1. The digest
ratchet now also keeps a channel watermark, the promoted_at and version of
the newest pointer this box admitted, beside the channel's digest mark
(<acct>/<app>/<channel>.watermark in the ratchet's own directory, read from
both), the app-pointer twin of the system pointer's seen.json floor. An
older pointer is refused as a replay (ErrPointerReplayed). signPointer
stamps whole seconds, so at an equal stamp the pointer must be the one
recorded or list it in its History, which a successor does and a saved
predecessor cannot. A rollback re-signs with a fresh stamp and goes through;
a floor that cannot be read or written refuses, as a mark does. The held
stage treats a replay as a verdict, like a digestless pointer.

H3-M8. A pointer signed before the Account field existed names no account
and verified under any account's key with that app and channel, so a copy
of one account's pre-binding pointer into another's prefix resolved there,
to whatever sat under the version. verifyPointer now refuses an unbound
pointer (ErrPointerUnbound) with the repair in the message. The write verbs
that replace a pointer (Promote, the ladder's repeat and retry reads,
Rollback, fast-track, Resign, RebindPointer) read it through
verifyPredecessor, which accepts an unbound one, since what they write is
bound: that is how a channel still on a pre-binding pointer gets its
account, and the ladder's repeat onto such a pointer re-signs it instead of
being a no-op. channels digests already lists an unverifiable pointer with
its error, so it names every channel left to repair.

Tests: the two hunt files as written; watermark unit tests and a Pass-level
test that a real rollback is followed while the replayed pointer is refused;
the golden fixtures keep their bytes and assert the signature still verifies
and only the binding refuses; hand-signed fixtures name their account; the
held-stage test blocks one channel's mark rather than the whole directory.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude Fable (hunt fix) pushed by claude agent, operated by paul@keymail.dev 84d2d259b927e88a2d707cfdb3702330bfbbcf55 parent 0fcf776
19 files changed, +761 −87
  • internal/console/ladder_test.go +1 −1
  • internal/host/claimcensus.go +3 −2
  • internal/release/accountbind_test.go +41 −12
  • internal/release/adopt.go +2 −1
  • internal/release/adopt_test.go +1 −1
  • internal/release/golden_test.go +18 −5
  • internal/release/heldauth_test.go +14 −8
  • internal/release/hunt_H3-M5_test.go +61 −0
  • internal/release/hunt_H3-M8_test.go +90 −0
  • internal/release/materializeheld.go +4 −2
  • internal/release/ratchet.go +38 −4
  • internal/release/rebind.go +7 −5
  • internal/release/release.go +66 −27
  • internal/release/release_test.go +29 −10
  • internal/release/resign.go +6 −5
  • internal/release/resign_test.go +9 −2
  • internal/release/scopedverify_test.go +12 −2
  • internal/release/watermark.go +162 −0
  • internal/release/watermark_test.go +197 −0
diff --git a/internal/console/ladder_test.go b/internal/console/ladder_test.go
index de333e509..88c28c783 100644
--- a/internal/console/ladder_test.go
+++ b/internal/console/ladder_test.go
@@ -578,7 +578,7 @@ func TestCanaryExpiryOmitsUnexpiredChannels(t *testing.T) {
// exported).
func seedExpiredCanary(t *testing.T, fx *consoleFixture, app, channel, version string) {
t.Helper()
- p := &release.ChannelPointer{Schema: 1, App: app, Channel: channel,
+ p := &release.ChannelPointer{Schema: 1, Account: release.OpsAccount, App: app, Channel: channel,
Version: version, PromotedAt: time.Now().Add(-(release.CanaryTTL + 30*24*time.Hour))}
payload, err := json.Marshal(p)
if err != nil {
diff --git a/internal/host/claimcensus.go b/internal/host/claimcensus.go
index 71bb2cebb..6a10c1c15 100644
--- a/internal/host/claimcensus.go
+++ b/internal/host/claimcensus.go
@@ -86,8 +86,9 @@ func claimBoxDataCensus(dataDir string) []string {
// key (edge.go's -acme-cache default; bootstrap makes the directory).
hostbox.CensusTree(rel(filepath.Join(dataDir, "acme"))),
// The agent's and edge's pointer-digest ratchet (#253): one mark per
- // channel, <acct>/<app>/<channel>.digests. Box anti-rollback state,
- // never cleared and never an installation artifact (#324).
+ // channel, <acct>/<app>/<channel>.digests, and beside it the channel
+ // watermark, <channel>.watermark (hunt H3-M5). Box anti-rollback
+ // state, never cleared and never an installation artifact (#324).
hostbox.CensusTree(rel(release.EdgeRatchetDir(dataDir))),
// The agent's system update state: its request (requested.json, also
// a file below) and its cache of the router build it fetched,
diff --git a/internal/release/accountbind_test.go b/internal/release/accountbind_test.go
index 3043a857c..d94d4b3ff 100644
--- a/internal/release/accountbind_test.go
+++ b/internal/release/accountbind_test.go
@@ -5,8 +5,10 @@ import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
+ "errors"
"strings"
"testing"
+ "time"
)
// A pointer signed for one account's app must not verify at another
@@ -64,17 +66,27 @@ func TestFreshPointerPayloadNamesItsAccount(t *testing.T) {
}
}
-// A pointer written before the binding existed has no account and must keep
-// verifying wherever it sits: that is most of every production bucket. This
-// is the residual the binding cannot close by itself — see verifyPointer.
-func TestAccountlessPointerStillVerifiesAnywhere(t *testing.T) {
+// A pointer written before the binding existed has no account. It used to
+// verify wherever it sat, which let a copy of one account's pre-binding
+// pointer resolve under another account's key (hunt H3-M8). The sunset the
+// binding promised is in force: it is refused under every account, its own
+// included, with ErrPointerUnbound, and the refusal says how to repair the
+// channel.
+//
+// Mutation-mindedness: drop the unbound check from verifyPointerBinding and
+// both reads below verify again.
+func TestAccountlessPointerIsRefusedEverywhere(t *testing.T) {
pub, err := ParsePublicKey(goldenRootPub, "golden")
if err != nil {
t.Fatal(err)
}
for _, acct := range []string{"bab", "zzz"} {
- if _, err := verifyPointer(pub, []byte(goldenRootSignedWrapper), acct, "kass", "canary/platform-adoption"); err != nil {
- t.Fatalf("golden account-less pointer under %s: %v", acct, err)
+ _, err := verifyPointer(pub, []byte(goldenRootSignedWrapper), acct, "kass", "canary/platform-adoption")
+ if !errors.Is(err, ErrPointerUnbound) {
+ t.Fatalf("golden account-less pointer under %s: %v, want ErrPointerUnbound", acct, err)
+ }
+ if !strings.Contains(err.Error(), "resign") || !strings.Contains(err.Error(), acct) {
+ t.Fatalf("refusal should name the account it was read as and say how to repair the channel: %v", err)
}
}
}
@@ -89,8 +101,9 @@ func TestSignPointerRequiresAccount(t *testing.T) {
}
// Moving an app between accounts: a bound pointer is re-signed for the new
-// account (deterministically, so a re-run compares equal), an unbound one
-// travels as bytes, and the result verifies at the destination only.
+// account (deterministically, so a re-run compares equal), an unbound one is
+// re-signed bound to the destination, and the result verifies at the
+// destination only.
func TestRebindPointerMovesTheBinding(t *testing.T) {
s := NewDirStore(t.TempDir())
ctx := context.Background()
@@ -125,12 +138,28 @@ func TestRebindPointerMovesTheBinding(t *testing.T) {
if _, err := verifyPointer(pub, moved, "aaa", "blog", "edge"); err == nil {
t.Fatal("rebound pointer still verifies at the source account")
}
- // A pointer from before the binding travels unchanged.
+ // A pointer from before the binding verifies nowhere as it is (hunt
+ // H3-M8), so the rebind binds it to the destination, with everything
+ // else kept, and is deterministic as for a bound pointer.
golden := []byte(goldenRootSignedWrapper)
goldPriv := ed25519.NewKeyFromSeed(goldenSeed())
- same, err := RebindPointer(goldPriv, golden, "aaa", "bbb", "kass", "canary/platform-adoption")
- if err != nil || string(same) != string(golden) {
- t.Fatalf("account-less pointer was rewritten (%v)", err)
+ goldPub := goldPriv.Public().(ed25519.PublicKey)
+ bound, err := RebindPointer(goldPriv, golden, "aaa", "bbb", "kass", "canary/platform-adoption")
+ if err != nil {
+ t.Fatalf("rebind of an account-less pointer: %v", err)
+ }
+ if again, err := RebindPointer(goldPriv, golden, "aaa", "bbb", "kass", "canary/platform-adoption"); err != nil || string(again) != string(bound) {
+ t.Fatalf("rebind of an account-less pointer is not deterministic (%v)", err)
+ }
+ gp, err := verifyPointer(goldPub, bound, "bbb", "kass", "canary/platform-adoption")
+ if err != nil {
+ t.Fatalf("rebound account-less pointer does not verify at the destination: %v", err)
+ }
+ if gp.Account != "bbb" || gp.Version != "9f3c1ad" || !gp.PromotedAt.Equal(time.Date(2026, 7, 30, 11, 4, 17, 0, time.UTC)) || len(gp.History) != 2 {
+ t.Fatalf("rebind changed more than the account: %+v", gp)
+ }
+ if _, err := verifyPointer(goldPub, bound, "aaa", "kass", "canary/platform-adoption"); err == nil {
+ t.Fatal("rebound account-less pointer verifies at the source account")
}
// Rebinding needs the root key the pointer verifies against.
wrong := ed25519.NewKeyFromSeed(make([]byte, ed25519.SeedSize))
diff --git a/internal/release/adopt.go b/internal/release/adopt.go
index 6d8acaa7b..b02429973 100644
--- a/internal/release/adopt.go
+++ b/internal/release/adopt.go
@@ -793,7 +793,8 @@ func PassFilter(ctx context.Context, reg *registry.Registry, s Store, pub ed2551
continue
}
// Any error from Admit refuses the pointer, ErrRatchetUnpersisted
- // included, not only ErrPointerWithoutDigests.
+ // and the watermark's ErrPointerReplayed included, not only
+ // ErrPointerWithoutDigests.
if rerr := cfg.ratchet.Admit(k.acct, ptr); rerr != nil {
if read != nil {
read(nil, rerr)
diff --git a/internal/release/adopt_test.go b/internal/release/adopt_test.go
index ad622f27c..9fbe70793 100644
--- a/internal/release/adopt_test.go
+++ b/internal/release/adopt_test.go
@@ -651,7 +651,7 @@ func TestPassFilterPointerGate(t *testing.T) {
// is not something production code should ever be able to do.
func writeBackdatedPointer(t *testing.T, s Store, priv ed25519.PrivateKey, acct, app, channel, version string, promotedAt time.Time) {
t.Helper()
- p := &ChannelPointer{Schema: 1, App: app, Channel: channel, Version: version, PromotedAt: promotedAt}
+ p := &ChannelPointer{Schema: 1, Account: acct, App: app, Channel: channel, Version: version, PromotedAt: promotedAt}
payload, err := json.MarshalIndent(p, "", " ")
if err != nil {
t.Fatalf("marshal backdated pointer: %v", err)
diff --git a/internal/release/golden_test.go b/internal/release/golden_test.go
index 9dee0fc14..8a5540f41 100644
--- a/internal/release/golden_test.go
+++ b/internal/release/golden_test.go
@@ -6,6 +6,7 @@ import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
+ "errors"
"reflect"
"sort"
"strings"
@@ -31,18 +32,30 @@ const goldenRootSignedWrapper = `{
// goldenRootPub is the public half of the fixture's signer.
const goldenRootPub = "ebVWLo/mVPlAeLES6KmLp5AfhTrmlb7X4OORC60ElmQ="
-// A pointer written by an older binary — no grant field at all — verifies
-// against the root public key and decodes to exactly what it said.
+// A pointer written by an older binary — no grant field at all — still has
+// its signature verified against the root public key, and its payload still
+// decodes to exactly what it said. The fixture predates the account binding
+// too, so since hunt H3-M8 the read ends in ErrPointerUnbound: that refusal
+// comes after the signature check and the decode, so reaching it is the
+// proof that both still work on this wire shape.
//
// Mutation-mindedness: if grant handling were ever made mandatory (require a
-// grant, or reject a wrapper without one), this test goes red immediately,
-// because the entire live fleet is on wrappers shaped like this one.
+// grant, or reject a wrapper without one), the refusal below changes and this
+// test goes red immediately, because the entire live fleet is on wrappers
+// shaped like this one.
func TestGoldenRootSignedWrapperStillVerifies(t *testing.T) {
pub, err := ParsePublicKey(goldenRootPub, "golden")
if err != nil {
t.Fatalf("ParsePublicKey: %v", err)
}
- p, err := verifyPointer(pub, []byte(goldenRootSignedWrapper), "bab", "kass", "canary/platform-adoption")
+ _, err = verifyPointer(pub, []byte(goldenRootSignedWrapper), "bab", "kass", "canary/platform-adoption")
+ if !errors.Is(err, ErrPointerUnbound) {
+ t.Fatalf("golden root-signed pointer: %v, want the signature to verify and only the account binding to refuse it", err)
+ }
+ // The write verbs' read (verifyPredecessor) accepts the unbound payload
+ // in order to re-sign it bound; what it sees is what the older binary
+ // wrote.
+ p, err := verifyPredecessor(pub, []byte(goldenRootSignedWrapper), "bab", "kass", "canary/platform-adoption")
if err != nil {
t.Fatalf("golden root-signed pointer no longer verifies: %v", err)
}
diff --git a/internal/release/heldauth_test.go b/internal/release/heldauth_test.go
index 663eceea6..7b8cf10a3 100644
--- a/internal/release/heldauth_test.go
+++ b/internal/release/heldauth_test.go
@@ -336,14 +336,20 @@ func TestHeldAuthorizedAdmitsOnlyNamingPointers(t *testing.T) {
if _, err := os.Stat(markPath(dir, "bab", "km", "a")); err != nil {
t.Fatalf("channel a's verified digests left no mark: %v", err)
}
- if os.Geteuid() != 0 {
- ro := filepath.Join(t.TempDir(), "ro")
- if err := os.MkdirAll(ro, 0o555); err != nil {
- t.Fatal(err)
- }
- if want, err := HeldAuthorized(ctx, s, pub, NewDigestRatchet(ro, nil, false), "bab", "km", "v1"); err != nil || want != "" {
- t.Fatalf("HeldAuthorized with channel a's mark unwritable = %q, %v; want channel b's pointer trust", want, err)
- }
+ // Channel a's mark alone unwritable (a directory where the mark belongs):
+ // channel b's pointer, which names the build, still persists its channel
+ // watermark beside it, so b's trust stands. A directory nothing can be
+ // written to would refuse b as well, since its watermark must persist
+ // (TestWatermarkThatCannotBePersistedRefuses, as for an unpersisted mark).
+ blocked := t.TempDir()
+ if err := os.MkdirAll(markPath(blocked, "bab", "km", "a"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if want, err := HeldAuthorized(ctx, s, pub, NewDigestRatchet(blocked, nil, false), "bab", "km", "v1"); err != nil || want != "" {
+ t.Fatalf("HeldAuthorized with channel a's mark unwritable = %q, %v; want channel b's pointer trust", want, err)
+ }
+ if _, err := os.Stat(watermarkPath(blocked, "bab", "km", "b")); err != nil {
+ t.Fatalf("channel b's watermark was not written: %v", err)
}
}
diff --git a/internal/release/hunt_H3-M5_test.go b/internal/release/hunt_H3-M5_test.go
new file mode 100644
index 000000000..da0cc2f2a
--- /dev/null
+++ b/internal/release/hunt_H3-M5_test.go
@@ -0,0 +1,61 @@
+package release
+
+import (
+ "bytes"
+ "context"
+ "testing"
+)
+
+// hunt H3-M5 (Opus verifier). The account has digests on, so both pointers
+// carry digests and the box's ratchet (ratchet.go:103) marks the channel. The
+// box adopts v2. A bucket writer then puts back the stable pointer the root
+// signed for v1. verifyPointer (release.go:465) checks signature, account,
+// app, channel and shape, never that promoted_at advanced; the ratchet only
+// refuses DIGESTLESS pointers; the bake clock already saw v1 (bake.go:271),
+// so it is eligible at once. SECURE: the box stays on v2.
+func TestHunt_H3_M5_ReplayedOldPointerRollsTheBoxBack(t *testing.T) {
+ s := NewDirStore(t.TempDir())
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ markerOn(t, s, priv, "bab")
+ shipRelease(t, s, "bab", "shop", "v1", adoptPlatform, []byte("v1: has the bug"))
+ shipRelease(t, s, "bab", "shop", "v2", adoptPlatform, []byte("v2: the security fix"))
+
+ opt := t.TempDir()
+ bake := t.TempDir()
+ ratchet := NewDigestRatchet(t.TempDir(), nil, false)
+ reg := testRegistry(t)
+ addRoute(t, reg, "shop.example.com", "shop", "", false)
+ pass := func() string {
+ var out bytes.Buffer
+ withCapturedStdout(t, &out, func() {
+ if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil },
+ BakeDir(bake), WithDigestRatchet(ratchet)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ })
+ return out.String()
+ }
+
+ if err := Promote(ctx, s, priv, "bab", "shop", "stable", "v1"); err != nil {
+ t.Fatal(err)
+ }
+ saved, _, _ := s.Get(ctx, ChannelKey("bab", "shop", "stable"))
+ pass()
+ if err := Promote(ctx, s, priv, "bab", "shop", "stable", "v2"); err != nil {
+ t.Fatal(err)
+ }
+ pass()
+ if live, _ := LiveVersion(opt, "shop.example.com"); live != "v2" {
+ t.Fatalf("setup: live %q, want v2", live)
+ }
+ if p, _ := Channel(ctx, s, pub, "bab", "shop", "stable"); p == nil || p.Digest == "" {
+ t.Fatal("setup: the v2 pointer should carry a digest")
+ }
+
+ opusOverwrite(t, s, ChannelKey("bab", "shop", "stable"), saved)
+ log := pass()
+ if live, _ := LiveVersion(opt, "shop.example.com"); live != "v2" {
+ t.Fatalf("H3-M5: a replayed stable pointer (v1, signed before v2) moved the box from v2 to %q; pass log:\n%s", live, log)
+ }
+}
diff --git a/internal/release/hunt_H3-M8_test.go b/internal/release/hunt_H3-M8_test.go
new file mode 100644
index 000000000..ece09f53f
--- /dev/null
+++ b/internal/release/hunt_H3-M8_test.go
@@ -0,0 +1,90 @@
+package release
+
+import (
+ "bytes"
+ "context"
+ "crypto/ed25519"
+ "encoding/base64"
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/registry"
+)
+
+// hunt H3-M8 (Opus verifier). A pointer the root signed before the Account
+// field existed (bab/blog edge -> 2.0.0) is copied by a bucket writer to
+// dee/blog edge. validatePointer checks the account only when present
+// (release.go:526). dee has shipped 2.0.0 of its own blog. SECURE: dee's
+// boxes refuse a pointer that names no account.
+func TestHunt_H3_M8_AccountlessPointerVerifiesUnderAnotherAccount(t *testing.T) {
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ s := NewDirStore(t.TempDir())
+ shipRelease(t, s, "dee", "blog", "1.0.0", adoptPlatform, []byte("dee 1.0.0"))
+ shipRelease(t, s, "dee", "blog", "2.0.0", adoptPlatform, []byte("dee 2.0.0"))
+ if err := Promote(ctx, s, priv, "dee", "blog", "edge", "1.0.0"); err != nil {
+ t.Fatal(err)
+ }
+
+ legacy := ChannelPointer{Schema: 1, App: "blog", Channel: "edge", Version: "2.0.0",
+ PromotedAt: time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)}
+ payload, _ := json.MarshalIndent(legacy, "", " ")
+ b64 := base64.StdEncoding.EncodeToString(payload)
+ wire, _ := json.MarshalIndent(signedWrapper{Payload: b64,
+ Sig: base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(b64)))}, "", " ")
+ opusOverwrite(t, s, ChannelKey("dee", "blog", "edge"), wire)
+
+ opt := t.TempDir()
+ reg := testRegistry(t)
+ if err := reg.Upsert(registry.Route{Host: "blog.example.com", Kind: "instance", App: "blog", Channel: "edge", Account: "dee"}); err != nil {
+ t.Fatal(err)
+ }
+ var out bytes.Buffer
+ withCapturedStdout(t, &out, func() {
+ if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil }); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ })
+ p, err := Channel(ctx, s, pub, "dee", "blog", "edge")
+ live, _ := LiveVersion(opt, "blog.example.com")
+ if err == nil || live == "2.0.0" {
+ t.Fatalf("H3-M8: an account-less pointer verified at dee's key (%+v, err=%v) and the box went live on %q; log:\n%s",
+ p, err, live, out.String())
+ }
+}
+
+// The stronger variant: dee never shipped 2.0.0. The same bucket writer
+// plants releases/2.0.0 under dee's prefix (Ship's create-only rule binds
+// only Ship, release.go:691) and copies the account-less pointer from bab.
+// A fresh box of dee's stages bytes no publisher shipped. SECURE: refused.
+func TestHunt_H3_M8_AccountlessPointerPlusPlantedRelease(t *testing.T) {
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ s := NewDirStore(t.TempDir())
+ shipRelease(t, s, "dee", "blog", "1.0.0", adoptPlatform, []byte("dee 1.0.0"))
+ if err := Promote(ctx, s, priv, "dee", "blog", "edge", "1.0.0"); err != nil {
+ t.Fatal(err)
+ }
+ // bab's pre-field pointer, as it sits in bab's prefix.
+ legacy := ChannelPointer{Schema: 1, App: "blog", Channel: "edge", Version: "2.0.0",
+ PromotedAt: time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)}
+ payload, _ := json.MarshalIndent(legacy, "", " ")
+ b64 := base64.StdEncoding.EncodeToString(payload)
+ wire, _ := json.MarshalIndent(signedWrapper{Payload: b64,
+ Sig: base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(b64)))}, "", " ")
+
+ evil := []byte("opus: planted, never shipped by dee")
+ shipRelease(t, s, "dee", "blog", "2.0.0", adoptPlatform, evil) // the plant: two PutObjects
+ opusOverwrite(t, s, ChannelKey("dee", "blog", "edge"), wire)
+
+ opt := t.TempDir()
+ reg := testRegistry(t)
+ if err := reg.Upsert(registry.Route{Host: "blog.example.com", Kind: "instance", App: "blog", Channel: "edge", Account: "dee"}); err != nil {
+ t.Fatal(err)
+ }
+ log := opusAgentPass(t, reg, s, pub, opt)
+ if live, _ := LiveVersion(opt, "blog.example.com"); live == "2.0.0" {
+ t.Fatalf("H3-M8: dee's box went live on a planted 2.0.0 behind bab's account-less pointer; log:\n%s", log)
+ }
+}
diff --git a/internal/release/materializeheld.go b/internal/release/materializeheld.go
index db06623a5..360569531 100644
--- a/internal/release/materializeheld.go
+++ b/internal/release/materializeheld.go
@@ -112,12 +112,14 @@ func HeldAuthorized(ctx context.Context, s Store, pub ed25519.PublicKey, r *Dige
}
// The box's ratchet and CARLOS_REQUIRE_POINTER_DIGEST apply here as
// they do to adoption (§4.6): a pointer the ratchet refuses is not
- // evidence. A nil ratchet would admit everything, so it is refused.
+ // evidence, and neither is one older than the channel's watermark
+ // (ErrPointerReplayed). A nil ratchet would admit everything, so it
+ // is refused.
if r == nil {
return "", fmt.Errorf("%w: no pointer digest ratchet", ErrHeldNotAuthorized)
}
if rerr != nil {
- if errors.Is(rerr, ErrPointerWithoutDigests) && !errors.Is(rerr, errRatchetMarkUnreadable) {
+ if (errors.Is(rerr, ErrPointerWithoutDigests) || errors.Is(rerr, ErrPointerReplayed)) && !errors.Is(rerr, errRatchetMarkUnreadable) {
continue // the ratchet's verdict: this pointer is refused
}
return "", fmt.Errorf("release: held stage of %s/%s at %s: channel %s: %w", acct, app, version, name, rerr)
diff --git a/internal/release/ratchet.go b/internal/release/ratchet.go
index f88c01a7d..15da10d37 100644
--- a/internal/release/ratchet.go
+++ b/internal/release/ratchet.go
@@ -30,6 +30,17 @@ var ErrPointerWithoutDigests = errors.New("release: pointer carries no digests")
// durable, because a restart would otherwise forget the channel (Astra I1 #4).
var ErrRatchetUnpersisted = errors.New("release: the pointer digest mark could not be persisted")
+// ErrPointerReplayed is a verified pointer that is older than the one this
+// box already followed for the channel (hunt H3-M5): a saved signed pointer
+// put back into the bucket. Its signature is good; it is simply not news.
+var ErrPointerReplayed = errors.New("release: pointer is older than the one this box already followed")
+
+// ErrWatermarkUnpersisted is a newer pointer whose channel watermark could
+// not be written. Like ErrRatchetUnpersisted, it refuses the pointer until
+// the floor is durable: a restart would otherwise forget how far the channel
+// had come, and the replay the watermark exists to stop would be admitted.
+var ErrWatermarkUnpersisted = errors.New("release: the channel watermark could not be persisted")
+
// errRatchetMarkUnreadable marks Admit's refusal of a pointer without
// digests because the channel's mark could not be read: unlike the flag or
// a mark that is there, it is not a verdict, so a caller that can retry
@@ -56,7 +67,8 @@ func RequirePointerDigest(getenv func(string) string) bool {
// EdgeRatchetDir the sandboxed agent's and edge's, which cannot write /opt
// (/data/pointer-digests). Each process writes its own and reads both. A mark
// is one flat file per channel, <dir>/<acct>/<app>/<channel>.digests, with
-// every "/" in the channel written as "%2F" (see markPath).
+// every "/" in the channel written as "%2F" (see markPath); the channel's
+// watermark sits beside it as <channel>.watermark (watermarkPath).
func RootRatchetDir(optDir string) string { return filepath.Join(optDir, "pointer-digests") }
func EdgeRatchetDir(dataDir string) string { return filepath.Join(dataDir, "pointer-digests") }
@@ -66,6 +78,14 @@ func EdgeRatchetDir(dataDir string) string { return filepath.Join(dataDir, "poin
// cannot be replayed to it. Marks are files, so they outlive the process;
// they are never cleared, and removing the emission marker does not clear
// them.
+//
+// The same object keeps the channel watermark (hunt H3-M5): the promoted_at
+// of the newest pointer this box admitted for each channel, so a pointer
+// signed before it, digests or not, is refused as a replay. The system
+// pointer has the same floor in internal/host/systemapply.go (seen.json);
+// this one lives beside the digest marks because they are the same kind of
+// state, per box and per channel, written by root and the agent to their
+// own directory and read from both.
type DigestRatchet struct {
own string
also []string
@@ -73,13 +93,14 @@ type DigestRatchet struct {
// another goroutine's Admit reads it (Astra I3 #7).
require atomic.Bool
- mu sync.Mutex
- seen map[string]bool // channels found marked, in any dir, by this process
+ mu sync.Mutex
+ seen map[string]bool // channels found marked, in any dir, by this process
+ floor map[string]watermark // the newest pointer admitted per channel, by this process
}
// NewDigestRatchet reads marks from own and also, and writes them to own.
func NewDigestRatchet(own string, also []string, require bool) *DigestRatchet {
- r := &DigestRatchet{own: own, also: also, seen: map[string]bool{}}
+ r := &DigestRatchet{own: own, also: also, seen: map[string]bool{}, floor: map[string]watermark{}}
r.require.Store(require)
return r
}
@@ -102,10 +123,23 @@ func markPath(dir, acct, app, channel string) string {
// the box unprotected after a restart. A mark that cannot be read (anything
// but a confirmed absence) refuses a pointer without digests: only a box that
// knows it has never seen digests for the channel admits one (Astra r1 #1).
+//
+// A pointer the digest verdict lets through is then held to the channel's
+// watermark (admitWatermark): one older than the newest pointer this box has
+// followed is a replay and is refused (ErrPointerReplayed), whatever it
+// carries.
func (r *DigestRatchet) Admit(acct string, p *ChannelPointer) error {
if r == nil || p == nil {
return nil
}
+ if err := r.admitDigests(acct, p); err != nil {
+ return err
+ }
+ return r.admitWatermark(acct, p)
+}
+
+// admitDigests is the #253 verdict: the digest mark, and the require flag.
+func (r *DigestRatchet) admitDigests(acct string, p *ChannelPointer) error {
if p.CarriesDigests() {
return r.note(acct, p.App, p.Channel)
}
diff --git a/internal/release/rebind.go b/internal/release/rebind.go
index 844c598bd..e85e0a9fe 100644
--- a/internal/release/rebind.go
+++ b/internal/release/rebind.go
@@ -30,7 +30,7 @@ func PointerAccount(wrapper []byte) (string, error) {
// RebindPointer is how a channel pointer moves between accounts. A pointer
// names the account it was signed for, so a byte copy into another account's
-// prefix no longer verifies there — which is the point of the binding, and
+// prefix does not verify there — which is the point of the binding, and
// what `accounts migrate` would otherwise trip over. The pointer is verified
// under fromAcct with root's public half, then re-signed by root for toAcct
// with every other field kept, PromotedAt included.
@@ -41,17 +41,19 @@ func PointerAccount(wrapper []byte) (string, error) {
// a pointer has already been copied, and a fresh timestamp would make every
// re-run look like a disagreement.
//
-// A pointer that names no account (written before the binding) or already
-// names toAcct is returned as is: it verifies at the destination unchanged.
+// A pointer that already names toAcct is returned as is. One that names no
+// account (written before the binding) no longer verifies anywhere (hunt
+// H3-M8), so it is re-signed bound to toAcct like any other: this is the
+// one reader that accepts an unbound pointer, and only to bind it.
func RebindPointer(root ed25519.PrivateKey, wrapper []byte, fromAcct, toAcct, app, channel string) ([]byte, error) {
if !ValidAccount(toAcct) {
return nil, fmt.Errorf("rebind: invalid account %q", toAcct)
}
- p, err := verifyPointer(root.Public().(ed25519.PublicKey), wrapper, fromAcct, app, channel)
+ p, err := verifyPredecessor(root.Public().(ed25519.PublicKey), wrapper, fromAcct, app, channel)
if err != nil {
return nil, fmt.Errorf("rebind %s/%s: %w", app, channel, err)
}
- if p.Account == "" || p.Account == toAcct {
+ if p.Account == toAcct {
return wrapper, nil
}
p.Account = toAcct
diff --git a/internal/release/release.go b/internal/release/release.go
index d77987961..1453586d2 100644
--- a/internal/release/release.go
+++ b/internal/release/release.go
@@ -124,12 +124,12 @@ type ChannelPointer struct {
// "blog/edge" in every account at once, and a byte copy of one account's
// pointer verified at another's key (tenant-isolation audit, discussion
// #220). Signed into every pointer written since, and checked against the
- // key's account whenever present.
+ // key's account on every read.
//
- // ADDITIVE, and deliberately optional on read: every pointer written
- // before the field existed has no "account" key and keeps verifying
- // wherever it sits — see verifyPointer for what that leaves open. Old
- // binaries ignore the key and verify exactly as before.
+ // ADDITIVE on the wire, and since hunt H3-M8 required on read: a pointer
+ // written before the field existed has no "account" key and no longer
+ // verifies anywhere (ErrPointerUnbound) — see verifyPointer. Old binaries
+ // ignore the key and verify exactly as before.
Account string `json:"account,omitempty"`
App string `json:"app"`
Channel string `json:"channel"`
@@ -436,18 +436,21 @@ func signPointer(sg Signer, p *ChannelPointer) ([]byte, error) {
// cross-channel replay) is rejected, and so is one copied from another
// account's key when it names its account.
//
-// What the account check cannot close: a pointer signed BEFORE the field
-// existed names no account, and verifies under any account's key with that
-// app and channel name — refusing it would take every channel not promoted
-// since dark. The field is inside the signature, so it cannot be stripped
-// from a newer pointer without breaking it; the exposure is the fixed set of
-// account-less pointers already signed, which (having no expiry) stay
-// replayable until verification stops accepting them. Every promote,
-// rollback or fast-track re-signs a channel with its account, so that set
-// only shrinks. Sunset: once no live channel pointer lacks an account, make
-// the account required here; until then the copy still needs write access
-// to the victim's prefix, and resolves only to a version shipped there,
-// since the manifest is read under the key's own account.
+// A pointer signed BEFORE the field existed names no account. It used to
+// verify under any account's key with that app and channel name, so a copy
+// of one account's pre-binding pointer into another account's prefix
+// resolved there, to whatever that account had shipped (or a writer had
+// planted) under the version (hunt H3-M8). The field is inside the
+// signature, so it cannot be stripped from a bound pointer without breaking
+// it; the exposure was the fixed set of account-less pointers already
+// signed, which (having no expiry) stayed replayable for as long as
+// verification accepted them. That sunset is now in force: a pointer that
+// names no account is refused everywhere (ErrPointerUnbound). Every promote,
+// rollback or resign re-signs a channel bound to its account, so a channel
+// still on a pre-binding pointer is repaired by any of them (they read the
+// pointer they replace through verifyPredecessor, which accepts an unbound
+// one); `carlos channels digests` lists such channels with this refusal
+// against them.
//
// pub is the deployment's ROOT public key, and it is the only key a box is
// ever configured with. A wrapper with no grant was signed by that key
@@ -463,6 +466,29 @@ func signPointer(sg Signer, p *ChannelPointer) ([]byte, error) {
// way: every consumer (Channel, adoption, VersionRung, the console, the edge)
// inherits grant handling by funnelling through here.
func verifyPointer(pub ed25519.PublicKey, wrapper []byte, wantAcct, wantApp, wantChannel string) (*ChannelPointer, error) {
+ return verifyPointerBinding(pub, wrapper, wantAcct, wantApp, wantChannel, false)
+}
+
+// ErrPointerUnbound is a channel pointer that names no account: signed
+// before the account binding existed, and refused on read since hunt H3-M8.
+// The channel is repaired by anything that re-signs its pointer: a promote,
+// a rollback or `carlos channels resign`.
+var ErrPointerUnbound = errors.New("release: pointer names no account")
+
+// verifyPredecessor reads the pointer a write verb is about to replace. It
+// is verifyPointer with the account binding optional, because the verbs that
+// re-sign a channel (Promote, Rollback, fast-track, Resign, RebindPointer)
+// are how a pre-binding pointer gets its account: what they write is bound,
+// since signPointer requires one, so the pointer they read need not be.
+// Every reader that ACTS on a pointer (Channel, adoption, the held stage,
+// the console, the edge) goes through verifyPointer, where it is required.
+func verifyPredecessor(pub ed25519.PublicKey, wrapper []byte, wantAcct, wantApp, wantChannel string) (*ChannelPointer, error) {
+ return verifyPointerBinding(pub, wrapper, wantAcct, wantApp, wantChannel, true)
+}
+
+// verifyPointerBinding is the one parse site behind verifyPointer and
+// verifyPredecessor; acceptUnbound is the only difference between them.
+func verifyPointerBinding(pub ed25519.PublicKey, wrapper []byte, wantAcct, wantApp, wantChannel string, acceptUnbound bool) (*ChannelPointer, error) {
var sw signedWrapper
if err := json.Unmarshal(wrapper, &sw); err != nil {
return nil, fmt.Errorf("pointer wrapper: %v", err)
@@ -499,6 +525,10 @@ func verifyPointer(pub ed25519.PublicKey, wrapper []byte, wantAcct, wantApp, wan
if err := json.Unmarshal(payload, &p); err != nil {
return nil, fmt.Errorf("pointer payload: %v", err)
}
+ if p.Account == "" && !acceptUnbound {
+ return nil, fmt.Errorf("%w: %s/%s was signed before the account binding and cannot be read as %s's; promote, roll back or resign the channel to bind it",
+ ErrPointerUnbound, p.App, p.Channel, wantAcct)
+ }
if err := validatePointer(&p, wantAcct, wantApp, wantChannel); err != nil {
return nil, err
}
@@ -521,8 +551,10 @@ func validatePointer(p *ChannelPointer, wantAcct, wantApp, wantChannel string) e
if p.Schema != 1 {
return fmt.Errorf("unsupported pointer schema %d", p.Schema)
}
- // Absent means a pointer from before the binding (see verifyPointer);
- // present means it must be this key's account.
+ // Present means it must be this key's account. Absent is a pointer from
+ // before the binding: verifyPointer refuses it before it gets here, and
+ // signPointer requires an account, so only verifyPredecessor's read
+ // reaches this line without one.
if p.Account != "" && p.Account != wantAcct {
return fmt.Errorf("pointer %s/%s was signed for account %s, expected %s", p.App, p.Channel, p.Account, wantAcct)
}
@@ -811,7 +843,7 @@ func (sg Signer) claimRawPromote(ctx context.Context, s Store, acct, app, channe
var old *ChannelPointer
switch {
case err == nil:
- old, err = verifyPointer(pub, cur, acct, app, channel)
+ old, err = verifyPredecessor(pub, cur, acct, app, channel)
if err != nil {
return fmt.Errorf("promote: existing pointer at %s: %w", key, err)
}
@@ -1080,7 +1112,7 @@ func (sg Signer) claimRawPromoteOnLadder(ctx context.Context, s Store, acct, app
// A repeat is a no-op, checked before anything else: re-running a command
// must not fail, whichever rung the build has since climbed to.
if cur, _, err := s.Get(ctx, ChannelKey(acct, app, channel)); err == nil {
- if p, verr := verifyPointer(pub, cur, acct, app, channel); verr == nil && p.Version == version {
+ if p, verr := verifyPredecessor(pub, cur, acct, app, channel); verr == nil && p.Version == version {
if opt.Authorize != nil {
pipe, perr := GetPipeline(ctx, s, pub, acct, app)
if perr != nil {
@@ -1090,12 +1122,19 @@ func (sg Signer) claimRawPromoteOnLadder(ctx context.Context, s Store, acct, app
return res, &AuthorizationError{Err: aerr}
}
}
- if opt.Retry {
+ // A repeat onto a pointer from before the account binding is not
+ // a no-op: nothing that acts on the pointer reads it any more
+ // (hunt H3-M8), the ladder included, so this re-signs it bound,
+ // as a retry renews it. It is the repair such a channel gets.
+ if opt.Retry || p.Account == "" {
res.From = res.To
- rec := Promotion{From: res.To, To: res.To, Actor: opt.Actor, Retry: true}
+ var rec []Promotion
+ if opt.Retry {
+ rec = append(rec, Promotion{From: res.To, To: res.To, Actor: opt.Actor, Retry: true})
+ }
// The version guard and the fresh-stamp wait both live in
// sg.promote, on the pointer its write is conditional on.
- if err := sg.promote(ctx, s, acct, app, channel, version, opt.Authorize, version, rec); err != nil {
+ if err := sg.promote(ctx, s, acct, app, channel, version, opt.Authorize, version, rec...); err != nil {
return res, err
}
return res, nil
@@ -1120,7 +1159,7 @@ func (sg Signer) claimRawPromoteOnLadder(ctx context.Context, s Store, acct, app
if opt.Retry {
cur := "nothing"
if c, _, err := s.Get(ctx, ChannelKey(acct, app, channel)); err == nil {
- if p, verr := verifyPointer(pub, c, acct, app, channel); verr == nil {
+ if p, verr := verifyPredecessor(pub, c, acct, app, channel); verr == nil {
cur = p.Version
}
}
@@ -1281,7 +1320,7 @@ func (sg Signer) claimRawRollbackAuthorized(ctx context.Context, s Store, acct,
if err != nil {
return fmt.Errorf("rollback: %w", err)
}
- old, err := verifyPointer(pub, cur, acct, app, channel)
+ old, err := verifyPredecessor(pub, cur, acct, app, channel)
if err != nil {
return fmt.Errorf("rollback: existing pointer at %s: %w", key, err)
}
@@ -1356,7 +1395,7 @@ func claimRawFastTrack(ctx context.Context, s Store, priv ed25519.PrivateKey, ac
if err != nil {
return "", fmt.Errorf("fast-track: %w", err)
}
- old, err := verifyPointer(sg.verifyPub(), cur, acct, app, channel)
+ old, err := verifyPredecessor(sg.verifyPub(), cur, acct, app, channel)
if err != nil {
return "", fmt.Errorf("fast-track: existing pointer at %s: %w", key, err)
}
diff --git a/internal/release/release_test.go b/internal/release/release_test.go
index 3a208eada..d5e6a5995 100644
--- a/internal/release/release_test.go
+++ b/internal/release/release_test.go
@@ -481,10 +481,13 @@ func TestPromotionRecordsAnAuditEntry(t *testing.T) {
}
}
-// The audit trail is additive: pointers written before it existed — and there
-// are live ones in production buckets — must still verify and parse, with no
-// audit data and no error. This is the exact payload the pre-audit signPointer
-// produced, signed the same way.
+// The audit trail is additive: pointers written before it existed must still
+// verify and parse, with no audit data and no error. This is the exact payload
+// the pre-audit signPointer produced, signed the same way. It also predates
+// the account binding, so since hunt H3-M8 a reader that acts on it (Channel)
+// refuses it for exactly that and nothing else, while the write verbs that
+// re-sign it (verifyPredecessor) still parse it whole: that is how such a
+// channel gets its account.
func TestPointerWrittenBeforeAuditStillParses(t *testing.T) {
s := NewDirStore(t.TempDir())
ctx := context.Background()
@@ -513,9 +516,12 @@ func TestPointerWrittenBeforeAuditStillParses(t *testing.T) {
t.Fatalf("plant pre-audit pointer: %v", err)
}
- p, err := Channel(ctx, s, pub, "bab", "myapp", "beta")
+ if _, err := Channel(ctx, s, pub, "bab", "myapp", "beta"); !errors.Is(err, ErrPointerUnbound) {
+ t.Fatalf("Channel on a pre-audit (and pre-binding) pointer: %v, want ErrPointerUnbound and nothing about the audit trail", err)
+ }
+ p, err := verifyPredecessor(pub, wrapper, "bab", "myapp", "beta")
if err != nil {
- t.Fatalf("Channel on a pre-audit pointer: %v", err)
+ t.Fatalf("verifyPredecessor on a pre-audit pointer: %v", err)
}
if p.Version != "v1" || len(p.History) != 1 || p.History[0] != "v0" {
t.Fatalf("pre-audit pointer = %+v", p)
@@ -523,10 +529,23 @@ func TestPointerWrittenBeforeAuditStillParses(t *testing.T) {
if p.Audit != nil {
t.Fatalf("pre-audit pointer has audit data: %+v", p.Audit)
}
- // It still answers "which rung is this build on", and still promotes —
- // the trail simply starts at the next promotion.
+ // "Which rung is this build on" acts on the pointer, so an unbound one
+ // does not place the build (hunt H3-M8); it still promotes, through the
+ // predecessor read, and the trail starts at that promotion.
+ if rung, err := VersionRung(ctx, s, pub, "bab", "myapp", "v1"); err != nil || rung != "" {
+ t.Fatalf("VersionRung on a pre-binding pointer = %q, %v; want no rung", rung, err)
+ }
+ // The repair: promoting the same build onto the same channel is a repeat
+ // the ladder would treat as a no-op, except that an unbound pointer is
+ // re-signed bound. The build is then on its rung again.
+ if r, err := PromoteOnLadder(ctx, s, priv, "bab", "myapp", "beta", "v1", PromoteOptions{Actor: "paul@keymail.dev"}); err != nil || r.NoOp {
+ t.Fatalf("re-promote onto a pre-binding pointer: %+v, %v; want a re-signed pointer, not a no-op", r, err)
+ }
+ if bp, err := Channel(ctx, s, pub, "bab", "myapp", "beta"); err != nil || bp.Account != "bab" || bp.Version != "v1" || len(bp.Audit) != 0 {
+ t.Fatalf("repaired beta pointer = %+v, %v; want it bound to bab at v1 with no audit entry", bp, err)
+ }
if rung, err := VersionRung(ctx, s, pub, "bab", "myapp", "v1"); err != nil || rung != RungBeta {
- t.Fatalf("VersionRung on a pre-audit pointer = %q, %v", rung, err)
+ t.Fatalf("VersionRung after the repair = %q, %v", rung, err)
}
if _, err := PromoteOnLadder(ctx, s, priv, "bab", "myapp", "stable", "v1",
PromoteOptions{Actor: "paul@keymail.dev"}); err != nil {
@@ -719,7 +738,7 @@ func TestFastTrackIsRootOnly(t *testing.T) {
}
// Verify side: build the wrapper by hand, exactly as a patched-out mint
// check would, and watch verification refuse it.
- payload, _ := json.Marshal(ChannelPointer{Schema: 1, App: "kass", Channel: "edge",
+ payload, _ := json.Marshal(ChannelPointer{Schema: 1, Account: "bab", App: "kass", Channel: "edge",
Version: "v1", FastTrack: true, PromotedAt: timeNowTestHelper()})
b64 := base64.StdEncoding.EncodeToString(payload)
sg, err := parseGrantBlob(grantB64, "test")
diff --git a/internal/release/resign.go b/internal/release/resign.go
index 28c72f92d..986a86bdb 100644
--- a/internal/release/resign.go
+++ b/internal/release/resign.go
@@ -44,7 +44,7 @@ func (sg Signer) PlanResign(ctx context.Context, s Store, acct, app, channel str
if err != nil {
return nil, fmt.Errorf("resign: %w", err)
}
- old, err := verifyPointer(sg.verifyPub(), cur, acct, app, channel)
+ old, err := verifyPredecessor(sg.verifyPub(), cur, acct, app, channel)
if err != nil {
return nil, fmt.Errorf("resign: existing pointer at %s: %w", key, err)
}
@@ -100,7 +100,7 @@ func (sg Signer) claimRawResign(ctx context.Context, s Store, plan *ResignPlan,
if etag != plan.etag {
return fmt.Errorf("%w: %s", ErrResignChanged, fmt.Sprintf(TextResignChanged, plan.Channel))
}
- old, err := verifyPointer(sg.verifyPub(), cur, plan.Account, plan.App, plan.Channel)
+ old, err := verifyPredecessor(sg.verifyPub(), cur, plan.Account, plan.App, plan.Channel)
if err != nil {
return fmt.Errorf("resign: existing pointer at %s: %w", key, err)
}
@@ -114,9 +114,10 @@ func (sg Signer) claimRawResign(ctx context.Context, s Store, plan *ResignPlan,
}
}
next := *old
- // A pointer from before the account binding carries no account, which the
- // verifier accepts and the signer refuses. A resigned pointer is a new
- // signed statement, so it is bound to the account it was verified under.
+ // A pointer from before the account binding carries no account, which
+ // verifyPredecessor accepts and the signer refuses. A resigned pointer is
+ // a new signed statement, so it is bound to the account it was verified
+ // under.
next.Account = plan.Account
next.Digest, next.HistoryDigests = plan.Digest, plan.HistoryDigests
if len(next.HistoryDigests) == 0 {
diff --git a/internal/release/resign_test.go b/internal/release/resign_test.go
index 2d33da1b3..2af48c932 100644
--- a/internal/release/resign_test.go
+++ b/internal/release/resign_test.go
@@ -205,8 +205,15 @@ func TestResignBindsTheAccountOnAPreBindingPointer(t *testing.T) {
}
}
unbindPointer(t, s, priv, "bab", "km", "stable")
- if p := readPtr(t, s, pub, "stable"); p.Account != "" {
- t.Fatalf("test setup: pointer still bound to %q", p.Account)
+ // An unbound pointer no longer reads through Channel (hunt H3-M8); the
+ // unverified account read is the one way to see what the setup wrote.
+ if raw, _, err := s.Get(ctx, ChannelKey("bab", "km", "stable")); err != nil {
+ t.Fatal(err)
+ } else if acct, err := PointerAccount(raw); err != nil || acct != "" {
+ t.Fatalf("test setup: pointer still bound to %q (%v)", acct, err)
+ }
+ if _, err := Channel(ctx, s, pub, "bab", "km", "stable"); !errors.Is(err, ErrPointerUnbound) {
+ t.Fatalf("Channel on the unbound pointer: %v, want ErrPointerUnbound", err)
}
markerOn(t, s, priv, "bab")
sg := RootSigner(priv)
diff --git a/internal/release/scopedverify_test.go b/internal/release/scopedverify_test.go
index 4c7c50d26..e6cb64d0d 100644
--- a/internal/release/scopedverify_test.go
+++ b/internal/release/scopedverify_test.go
@@ -4,6 +4,7 @@ import (
"crypto/ed25519"
"encoding/base64"
"encoding/json"
+ "errors"
"reflect"
"sort"
"strings"
@@ -98,7 +99,16 @@ func TestGoldenGrantSignedWrapperShapeAndVerification(t *testing.T) {
t.Fatalf("grant sub-object keys = %v, want %v", got, want)
}
- p, err := verifyPointer(rootPubKey(t), []byte(goldenGrantSignedWrapper), "bab", "kass", "edge")
+ // The fixture predates the account binding, so a reader that acts on it
+ // ends in ErrPointerUnbound (hunt H3-M8): that refusal comes after the
+ // root -> grant -> pointer chain is checked, so reaching it is the proof
+ // the chain still verifies on this wire shape. verifyPredecessor, the
+ // write verbs' read, decodes it whole.
+ _, err := verifyPointer(rootPubKey(t), []byte(goldenGrantSignedWrapper), "bab", "kass", "edge")
+ if !errors.Is(err, ErrPointerUnbound) {
+ t.Fatalf("golden grant-signed pointer: %v, want the chain to verify and only the account binding to refuse it", err)
+ }
+ p, err := verifyPredecessor(rootPubKey(t), []byte(goldenGrantSignedWrapper), "bab", "kass", "edge")
if err != nil {
t.Fatalf("golden grant-signed pointer does not verify: %v", err)
}
@@ -169,7 +179,7 @@ func TestVerifyEnforcesGrantScope(t *testing.T) {
// permission before signing.
func forgePointerAs(t *testing.T, sg Signer, app, channel, version string, at time.Time) []byte {
t.Helper()
- p := &ChannelPointer{Schema: 1, App: app, Channel: channel, Version: version, PromotedAt: at}
+ p := &ChannelPointer{Schema: 1, Account: "bab", App: app, Channel: channel, Version: version, PromotedAt: at}
payload, err := json.MarshalIndent(p, "", " ")
if err != nil {
t.Fatal(err)
diff --git a/internal/release/watermark.go b/internal/release/watermark.go
new file mode 100644
index 000000000..3a2ec1538
--- /dev/null
+++ b/internal/release/watermark.go
@@ -0,0 +1,162 @@
+package release
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "io/fs"
+ "log"
+ "os"
+ "path/filepath"
+ "slices"
+ "strings"
+ "syscall"
+ "time"
+
+ "github.com/carlosframework/platform/internal/safefile"
+)
+
+// watermark is one channel's floor on this box: the promoted_at and version
+// of the newest pointer it admitted (hunt H3-M5). It is the app-pointer twin
+// of the system pointer's seen.json floor (internal/host/systemapply.go): a
+// verified pointer older than the floor is a saved pointer put back, and is
+// refused however good its signature.
+//
+// durable says the floor on disk is at least this one. A floor remembered
+// after a write failed is not; it still refuses what it must, and the write
+// is tried again on the next admission.
+type watermark struct {
+ At time.Time `json:"promoted_at"`
+ Version string `json:"version"`
+ durable bool
+}
+
+// watermarkPath is a channel's floor: <dir>/<acct>/<app>/<channel>.watermark,
+// beside the channel's digest mark, with "/" escaped as markPath does.
+func watermarkPath(dir, acct, app, channel string) string {
+ return filepath.Join(dir, acct, app, strings.ReplaceAll(channel, "/", "%2F")+".watermark")
+}
+
+// admits reports whether p may follow this floor. A later promoted_at is
+// news. The same promoted_at needs a closer look: signPointer stamps whole
+// seconds, so a promote and its rollback, or two promotes, can share one.
+// Promote and Rollback both put the head they replace at History[0], so a
+// successor signed in the floor's second lists the floor's version, and a
+// pointer saved before the floor's head cannot. The pointer already admitted
+// is the other same-second case. Anything older is a replay.
+func (wm watermark) admits(p *ChannelPointer) bool {
+ switch {
+ case wm.At.IsZero() || p.PromotedAt.After(wm.At):
+ return true
+ case p.PromotedAt.Equal(wm.At):
+ return p.Version == wm.Version || slices.Contains(p.History, wm.Version)
+ }
+ return false
+}
+
+// later is the higher of two floors; at the same instant a (this process's
+// memory, which saw the latest admission) wins.
+func later(a, b watermark) watermark {
+ if b.At.After(a.At) {
+ return b
+ }
+ return a
+}
+
+// admitWatermark holds a pointer the digest verdict let through to the
+// channel's floor, and raises the floor to it when it is newer. The floor is
+// read from every directory the ratchet reads marks from and written to its
+// own, like a mark. A floor that cannot be read refuses the pointer, the way
+// an unreadable mark does, and marks the refusal retryable; one that cannot
+// be written refuses it too (ErrWatermarkUnpersisted), since a restart would
+// otherwise forget it.
+//
+// A pointer with no promoted_at is left alone: verifyPointer refuses one
+// before it gets here, so only a hand-built pointer in a test of the digest
+// verdict ever arrives without a stamp.
+func (r *DigestRatchet) admitWatermark(acct string, p *ChannelPointer) error {
+ if p.PromotedAt.IsZero() {
+ return nil
+ }
+ k := acct + "|" + p.App + "|" + p.Channel
+ r.mu.Lock()
+ defer r.mu.Unlock()
+ mem, known := r.floor[k]
+ if known {
+ // Memory alone settles the two common cases: the pointer this process
+ // already admitted, and a replay. Anything else (a newer pointer, a
+ // same-second successor, a floor not yet on disk) goes to the disk.
+ if mem.durable && p.PromotedAt.Equal(mem.At) && p.Version == mem.Version {
+ return nil
+ }
+ if p.PromotedAt.Before(mem.At) {
+ return r.replayed(acct, p, mem)
+ }
+ }
+ disk, err := r.readWatermark(acct, p.App, p.Channel)
+ if err != nil {
+ return markUnreadable{fmt.Errorf("release: %s/%s/%s: this box cannot read its channel watermark: %w", acct, p.App, p.Channel, err)}
+ }
+ floor := disk
+ if known {
+ floor = later(mem, disk)
+ }
+ if !floor.admits(p) {
+ floor.durable = floor.At.Equal(disk.At) && floor.Version == disk.Version
+ r.floor[k] = floor
+ return r.replayed(acct, p, floor)
+ }
+ next := watermark{At: p.PromotedAt, Version: p.Version, durable: true}
+ if r.own == "" || (disk.At.Equal(p.PromotedAt) && disk.Version == p.Version) {
+ r.floor[k] = next // nothing to persist, or the disk already says so
+ return nil
+ }
+ path := watermarkPath(r.own, acct, p.App, p.Channel)
+ body, _ := json.Marshal(next)
+ werr := safefile.MkdirAll(filepath.Dir(path), 0o755)
+ if werr == nil {
+ werr = WriteFileAtomic(path, append(body, '\n'), 0o644)
+ }
+ if werr != nil {
+ next.durable = false
+ r.floor[k] = next // remembered at once, whatever the disk says
+ log.Printf("release: channel watermark: %v", werr)
+ return fmt.Errorf("%w: %s/%s/%s: %v", ErrWatermarkUnpersisted, acct, p.App, p.Channel, werr)
+ }
+ r.floor[k] = next
+ return nil
+}
+
+func (r *DigestRatchet) replayed(acct string, p *ChannelPointer, wm watermark) error {
+ return fmt.Errorf("%w: %s/%s/%s names %s promoted %s, and this box has already followed %s promoted %s (a rollback re-signs the channel with a fresh promoted_at)",
+ ErrPointerReplayed, acct, p.App, p.Channel, p.Version, p.PromotedAt.UTC().Format(time.RFC3339),
+ wm.Version, wm.At.UTC().Format(time.RFC3339))
+}
+
+// readWatermark is the highest floor in any of the ratchet's directories.
+// Only a confirmed absence (ENOENT, or ENOTDIR on a path component) counts
+// as no floor; any other answer, a file that does not parse included, is an
+// error the caller treats as fail-closed, as marked does for a mark.
+func (r *DigestRatchet) readWatermark(acct, app, channel string) (watermark, error) {
+ var out watermark
+ for _, dir := range append([]string{r.own}, r.also...) {
+ if dir == "" {
+ continue
+ }
+ path := watermarkPath(dir, acct, app, channel)
+ raw, err := os.ReadFile(path)
+ if errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) {
+ continue
+ }
+ if err != nil {
+ return watermark{}, err
+ }
+ var wm watermark
+ if err := json.Unmarshal(raw, &wm); err != nil || wm.At.IsZero() {
+ return watermark{}, fmt.Errorf("channel watermark %s is malformed", path)
+ }
+ out = later(out, wm)
+ }
+ out.durable = true
+ return out, nil
+}
diff --git a/internal/release/watermark_test.go b/internal/release/watermark_test.go
new file mode 100644
index 000000000..3d93776c9
--- /dev/null
+++ b/internal/release/watermark_test.go
@@ -0,0 +1,197 @@
+package release
+
+import (
+ "bytes"
+ "context"
+ "errors"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// stamped is a pointer with a promoted_at, the shape every verified pointer
+// has by the time the ratchet sees it.
+func stamped(ch, version string, at time.Time, history ...string) *ChannelPointer {
+ return &ChannelPointer{App: "km", Channel: ch, Version: version, PromotedAt: at, History: history}
+}
+
+// The watermark (hunt H3-M5): once a box has followed a pointer, one signed
+// before it is a replay and is refused, whether or not it carries digests.
+// The pointer already admitted keeps being admitted, every pass re-reads it.
+func TestWatermarkRefusesAnOlderPointer(t *testing.T) {
+ r := NewDigestRatchet(t.TempDir(), nil, false)
+ t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ if err := r.Admit("bab", stamped("stable", "v1", t0)); err != nil {
+ t.Fatal(err)
+ }
+ if err := r.Admit("bab", stamped("stable", "v2", t0.Add(time.Minute), "v1")); err != nil {
+ t.Fatal(err)
+ }
+ if err := r.Admit("bab", stamped("stable", "v2", t0.Add(time.Minute), "v1")); err != nil {
+ t.Fatalf("the pointer already followed: %v", err)
+ }
+ err := r.Admit("bab", stamped("stable", "v1", t0))
+ if !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("a pointer signed before the one followed: %v, want ErrPointerReplayed", err)
+ }
+ // Digests do not buy a replay anything: a saved pointer with digests is
+ // still a saved pointer.
+ if err := r.Admit("bab", &ChannelPointer{App: "km", Channel: "stable", Version: "v1", PromotedAt: t0, Digest: dA}); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("a replayed pointer with digests: %v", err)
+ }
+ if err := r.Admit("bab", stamped("beta", "v1", t0)); err != nil {
+ t.Fatalf("another channel has its own floor: %v", err)
+ }
+ if err := r.Admit("cdf", stamped("stable", "v1", t0)); err != nil {
+ t.Fatalf("another account has its own floor: %v", err)
+ }
+}
+
+// signPointer stamps whole seconds, so two pointers of one channel can share
+// a promoted_at. The successor lists the head it replaced at History[0] (both
+// Promote and Rollback do); the replayed predecessor cannot list its
+// successor. That is how the floor tells them apart at the same second.
+func TestWatermarkAtTheSameSecondAdmitsOnlyASuccessor(t *testing.T) {
+ r := NewDigestRatchet(t.TempDir(), nil, false)
+ at := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ if err := r.Admit("bab", stamped("stable", "v1", at)); err != nil {
+ t.Fatal(err)
+ }
+ if err := r.Admit("bab", stamped("stable", "v2", at, "v1")); err != nil {
+ t.Fatalf("a successor promoted in the same second: %v", err)
+ }
+ if err := r.Admit("bab", stamped("stable", "v1", at)); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("the predecessor put back in the same second: %v, want ErrPointerReplayed", err)
+ }
+ // A rollback in the same second lists v2 and goes through.
+ if err := r.Admit("bab", stamped("stable", "v1", at, "v2")); err != nil {
+ t.Fatalf("a same-second rollback: %v", err)
+ }
+ // A pointer that neither is the head nor descends from it is refused.
+ if err := r.Admit("bab", stamped("stable", "v0", at)); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("an unrelated pointer at the same second: %v", err)
+ }
+}
+
+// The floor is a file, and it outlives the process, in whichever of the
+// ratchet's directories wrote it.
+func TestWatermarkSurvivesARestartAndIsReadFromEveryDir(t *testing.T) {
+ root, agent := t.TempDir(), t.TempDir()
+ t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ if err := NewDigestRatchet(root, []string{agent}, false).Admit("bab", stamped("stable", "v2", t0.Add(time.Minute), "v1")); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := os.Stat(watermarkPath(root, "bab", "km", "stable")); err != nil {
+ t.Fatalf("the floor was not written beside the marks: %v", err)
+ }
+ if err := NewDigestRatchet(root, []string{agent}, false).Admit("bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("a new process forgot the floor: %v", err)
+ }
+ // The agent reads root's floor as its own.
+ if err := NewDigestRatchet(agent, []string{root}, false).Admit("bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("the other process's floor was not read: %v", err)
+ }
+ if err := NewDigestRatchet(agent, []string{root}, false).Admit("bab", stamped("stable", "v2", t0.Add(time.Minute), "v1")); err != nil {
+ t.Fatalf("the pointer at the floor, read from the other dir: %v", err)
+ }
+}
+
+// A floor that cannot be written refuses the pointer, as an unpersisted mark
+// does: admitting it would leave the box replayable after a restart. The
+// refusal is remembered in memory, so the replay is refused in-process too.
+func TestWatermarkThatCannotBePersistedRefuses(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("root can write a 0555 directory")
+ }
+ ro := filepath.Join(t.TempDir(), "ro")
+ if err := os.MkdirAll(ro, 0o555); err != nil {
+ t.Fatal(err)
+ }
+ r := NewDigestRatchet(ro, nil, false)
+ t0 := time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)
+ if err := r.Admit("bab", stamped("stable", "v2", t0.Add(time.Minute), "v1")); !errors.Is(err, ErrWatermarkUnpersisted) {
+ t.Fatalf("Admit = %v, want ErrWatermarkUnpersisted", err)
+ }
+ if err := r.Admit("bab", stamped("stable", "v1", t0)); !errors.Is(err, ErrPointerReplayed) {
+ t.Fatalf("the replay after a failed write: %v, want ErrPointerReplayed", err)
+ }
+}
+
+// A floor that cannot be read refuses, and the refusal is the retryable kind
+// the held stage recognises, exactly as for an unreadable mark.
+func TestWatermarkThatCannotBeReadRefusesRetryably(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.MkdirAll(watermarkPath(dir, "bab", "km", "stable"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ err := NewDigestRatchet(dir, nil, false).Admit("bab", stamped("stable", "v1", time.Date(2026, 10, 1, 12, 0, 0, 0, time.UTC)))
+ if err == nil || !errors.Is(err, errRatchetMarkUnreadable) {
+ t.Fatalf("Admit = %v, want an unreadable (retryable) refusal", err)
+ }
+}
+
+// The property the brief insists on: a real rollback still works. Rollback
+// re-signs the channel with a fresh promoted_at, so the pointer that puts v1
+// back is newer than the floor v2 raised (or, within the same second, lists
+// v2 as the head it replaced) and the box follows it, while the saved v1
+// pointer a bucket writer puts back is refused. The two look alike to every
+// check but this one.
+func TestWatermarkLetsARollbackThroughAndRefusesTheReplay(t *testing.T) {
+ s := NewDirStore(t.TempDir())
+ ctx := context.Background()
+ priv, pub := testKeys(t)
+ shipRelease(t, s, "bab", "shop", "v1", adoptPlatform, []byte("v1"))
+ shipRelease(t, s, "bab", "shop", "v2", adoptPlatform, []byte("v2"))
+
+ opt := t.TempDir()
+ ratchet := NewDigestRatchet(t.TempDir(), nil, false)
+ reg := testRegistry(t)
+ addRoute(t, reg, "shop.example.com", "shop", "", false)
+ pass := func() string {
+ var out bytes.Buffer
+ withCapturedStdout(t, &out, func() {
+ if err := Pass(ctx, reg, s, pub, opt, func(string) error { return nil },
+ BakeDir(t.TempDir()), WithDigestRatchet(ratchet)); err != nil {
+ t.Fatalf("Pass: %v", err)
+ }
+ })
+ return out.String()
+ }
+ live := func() string {
+ v, _ := LiveVersion(opt, "shop.example.com")
+ return v
+ }
+
+ if err := Promote(ctx, s, priv, "bab", "shop", "stable", "v1"); err != nil {
+ t.Fatal(err)
+ }
+ saved, _, _ := s.Get(ctx, ChannelKey("bab", "shop", "stable"))
+ pass()
+ if err := Promote(ctx, s, priv, "bab", "shop", "stable", "v2"); err != nil {
+ t.Fatal(err)
+ }
+ if log := pass(); live() != "v2" {
+ t.Fatalf("setup: live %q, want v2; log:\n%s", live(), log)
+ }
+
+ // The saved v1 pointer, put back: refused, the box stays on v2.
+ opusOverwrite(t, s, ChannelKey("bab", "shop", "stable"), saved)
+ if log := pass(); live() != "v2" {
+ t.Fatalf("replayed v1 pointer moved the box to %q; log:\n%s", live(), log)
+ }
+ // The operator's rollback, re-signed now: followed, the box goes to v1.
+ if err := Promote(ctx, s, priv, "bab", "shop", "stable", "v2"); err != nil {
+ t.Fatal(err) // restore the head the writer overwrote, as the console would show it
+ }
+ if err := Rollback(ctx, s, priv, "bab", "shop", "stable"); err != nil {
+ t.Fatalf("Rollback: %v", err)
+ }
+ if log := pass(); live() != "v1" {
+ t.Fatalf("a real rollback to v1 was refused; live %q; log:\n%s", live(), log)
+ }
+ // And the rollback's own pointer keeps being admitted on the next pass.
+ if log := pass(); live() != "v1" {
+ t.Fatalf("the rollback pointer was refused on re-read; live %q; log:\n%s", live(), log)
+ }
+}