rootfs apps plan 3: Task 3 fix round 1, protected-field aliases, final bytes, write-backs, create etag
runtimeFixed refuses a before or body carrying runtime or hibernate under
any other spelling (ErrRecordSequence): AdvanceRecordSequence re-sorts the
fields, so two spellings could pass the guard in one order and be stored
in the other. WriteRecord also runs the guard on the final bytes it puts,
after sequencing. A write-back that moves no field (record_seq aside)
passes, as the lifetime fence and migration supersede need. A before of
{} with an etag is refused before any Put.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+253
−35
internal/provision/record_sequence.go+9 −1internal/provision/runtime.go+78 −26internal/provision/runtime_test.go+166 −8
CI cancelled — superseded by a newer push — run details
diff --git a/internal/provision/record_sequence.go b/internal/provision/record_sequence.go| index 785045c86..ba0e8173c 100644 |
| --- a/internal/provision/record_sequence.go |
| +++ b/internal/provision/record_sequence.go |
| @@ -118,7 +118,9 @@ type preparedRecord struct { |
| // ErrRecordMigrating unless ctx carries WithMigration of that marker; a |
| // create-only write reads no before and is unaffected. A write that moves |
| // the runtime, or a rootfs record's hibernation, is refused with |
| -// ErrRuntimeProtected (runtimeFixed): this guard is every writer's. |
| +// ErrRuntimeProtected (runtimeFixed, on the body handed in and on the bytes |
| +// Put stores): this guard is every writer's. A before of {} is a create and |
| +// takes no etag. |
| func WriteRecord(ctx context.Context, s release.Store, key string, before, body []byte, etag string) (string, uint64, error) { |
| if !IsRecordKey(key) { |
| return "", 0, ErrRecordSequence |
| @@ -127,6 +129,9 @@ func WriteRecord(ctx context.Context, s release.Store, key string, before, body |
| if err != nil { |
| return "", 0, err |
| } |
| + if len(fields) == 0 && etag != "" { |
| + return "", 0, ErrRecordSequence // a create is create-only; {} never stands in for a read |
| + } |
| if err := checkRecordMarker(ctx, before); err != nil { |
| return "", 0, err |
| } |
| @@ -158,6 +163,9 @@ func WriteRecord(ctx context.Context, s release.Store, key string, before, body |
| } |
| p.body, p.seq, p.sequenced = append([]byte(nil), body...), base+1, true |
| } |
| + if err := runtimeFixed(before, body); err != nil { |
| + return "", 0, err // the bytes Put stores, after any sequencing, pass too |
| + } |
| tag, err := s.Put(ctx, key, body, etag) |
| return tag, p.seq, err |
| } |
diff --git a/internal/provision/runtime.go b/internal/provision/runtime.go| index 3d90c4350..0b974a2c0 100644 |
| --- a/internal/provision/runtime.go |
| +++ b/internal/provision/runtime.go |
| @@ -5,6 +5,7 @@ import ( |
| "encoding/json" |
| "errors" |
| "fmt" |
| + "strings" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -67,28 +68,63 @@ var ErrRuntimeProtected = errors.New("provision: an instance's runtime, and a ro |
| // runtimeFields is all runtimeFixed reads of a record body. Decoding with |
| // encoding/json, as DecodeRecord does, gives the same answer every reader of |
| -// the record gets, case-insensitive key matches included. |
| +// the record gets; protectedSpellings first makes sure there is only one |
| +// key it could have matched. |
| type runtimeFields struct { |
| Runtime string `json:"runtime"` |
| Hibernate *bool `json:"hibernate"` |
| } |
| -// runtimeFixed is WriteRecord's guard: before is the stored body the write |
| -// is If-Match'd on ({} for a create), body the one it writes. A create |
| -// passes, and so does the exact tombstone of before (retirement ends the |
| -// life; the hostname's next life is a new mint). Otherwise the runtime must |
| -// be byte-equal, and a stored rootfs-v1 record's hibernate must be present |
| -// in both and equal. A body that does not decode is ErrRecordSequence, as |
| -// RecordSequence answers it. |
| +// protectedKeys are the fields runtimeFixed protects. |
| +var protectedKeys = []string{"runtime", "hibernate"} |
| + |
| +// protectedSpellings refuses a body that carries a protected field under a |
| +// spelling other than its own ("RUNTIME", "Hibernate"). encoding/json |
| +// matches keys case-insensitively and the last one wins, and |
| +// AdvanceRecordSequence re-marshals the fields sorted, so two spellings |
| +// could pass the guard in one order and be stored in the other. |
| +// RecordSequence already refuses an exact duplicate. |
| +func protectedSpellings(fields map[string]json.RawMessage) error { |
| + for name := range fields { |
| + for _, k := range protectedKeys { |
| + if name != k && strings.EqualFold(name, k) { |
| + return ErrRecordSequence |
| + } |
| + } |
| + } |
| + return nil |
| +} |
| + |
| +// runtimeFixed is WriteRecord's guard, run on the body it is handed and |
| +// again on the final bytes it puts: before is the stored body the write is |
| +// If-Match'd on ({} for a create), body the one it writes. Either carrying a |
| +// protected field under another spelling is ErrRecordSequence. A create |
| +// passes. So does a write-back that moves no field (record_seq aside: a |
| +// fence or a supersede writes the stored bytes again, even a record that |
| +// would not pass below), and the exact tombstone of before (retirement |
| +// ends the life; the hostname's next life is a new mint). Otherwise the |
| +// runtime must be byte-equal, and a stored rootfs-v1 record's hibernate |
| +// must be present in both and equal. A body that does not decode is |
| +// ErrRecordSequence, as RecordSequence answers it. |
| func runtimeFixed(before, body []byte) error { |
| prior, _, err := RecordSequence(before) |
| if err != nil { |
| return err |
| } |
| + next, _, err := RecordSequence(body) |
| + if err != nil { |
| + return err |
| + } |
| + if err := protectedSpellings(prior); err != nil { |
| + return err |
| + } |
| + if err := protectedSpellings(next); err != nil { |
| + return err |
| + } |
| if len(prior) == 0 { |
| return nil |
| } |
| - if isTombstoneOf(before, body) { |
| + if sameFieldsButSeq(prior, next) || isTombstoneOf(before, next) { |
| return nil |
| } |
| var was, now runtimeFields |
| @@ -104,10 +140,11 @@ func runtimeFixed(before, body []byte) error { |
| return nil |
| } |
| -// isTombstoneOf reports whether body is TombstoneBody(before), with |
| -// record_seq left to WriteRecord's own sequencing (a store wrapper's nested |
| -// WriteRecord sees the advanced counter). |
| -func isTombstoneOf(before, body []byte) bool { |
| +// isTombstoneOf reports whether next, a body's fields, is |
| +// TombstoneBody(before) with record_seq left to WriteRecord's own |
| +// sequencing (the final bytes, and a store wrapper's nested WriteRecord, |
| +// carry the advanced counter). |
| +func isTombstoneOf(before []byte, next map[string]json.RawMessage) bool { |
| tomb, err := TombstoneBody(before) |
| if err != nil { |
| return false |
| @@ -116,20 +153,35 @@ func isTombstoneOf(before, body []byte) bool { |
| if err != nil { |
| return false |
| } |
| - got, _, err := RecordSequence(body) |
| - if err != nil { |
| - return false |
| - } |
| - delete(want, "record_seq") |
| - delete(got, "record_seq") |
| - if len(want) != len(got) { |
| - return false |
| - } |
| - for k, v := range want { |
| - g, ok := got[k] |
| - if !ok || !bytes.Equal(g, v) { |
| + return sameFieldsButSeq(want, next) |
| +} |
| + |
| +// sameFieldsButSeq reports whether a and b carry the same fields with the |
| +// same values, compared compacted (a sequenced write re-marshals them), and |
| +// record_seq not compared. |
| +func sameFieldsButSeq(a, b map[string]json.RawMessage) bool { |
| + n := 0 |
| + for k, v := range a { |
| + if k == "record_seq" { |
| + continue |
| + } |
| + n++ |
| + w, ok := b[k] |
| + if !ok || !sameJSON(v, w) { |
| return false |
| } |
| } |
| - return true |
| + _, seq := b["record_seq"] |
| + if seq { |
| + n++ |
| + } |
| + return n == len(b) |
| +} |
| + |
| +func sameJSON(a, b []byte) bool { |
| + var ca, cb bytes.Buffer |
| + if json.Compact(&ca, a) != nil || json.Compact(&cb, b) != nil { |
| + return false |
| + } |
| + return bytes.Equal(ca.Bytes(), cb.Bytes()) |
| } |
diff --git a/internal/provision/runtime_test.go b/internal/provision/runtime_test.go| index 8d6f2ca0d..424b01f8a 100644 |
| --- a/internal/provision/runtime_test.go |
| +++ b/internal/provision/runtime_test.go |
| @@ -209,14 +209,6 @@ func TestRawWriteThatDropsRuntimeIsRefused(t *testing.T) { |
| if _, _, err := WriteRecord(ctx, s, key, stored, without("hibernate"), etag); !errors.Is(err, ErrRuntimeProtected) { |
| t.Errorf("drop hibernate: err = %v, want ErrRuntimeProtected", err) |
| } |
| - // A case alias decodes like the field it names, for every reader. |
| - alias := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":"rootfs-v1","RUNTIME":""`), 1) |
| - if bytes.Equal(alias, stored) { |
| - t.Fatal("test body: no runtime key to alias") |
| - } |
| - if _, _, err := WriteRecord(ctx, s, key, stored, alias, etag); !errors.Is(err, ErrRuntimeProtected) { |
| - t.Errorf("aliased runtime: err = %v, want ErrRuntimeProtected", err) |
| - } |
| // A body whose runtime does not decode answers as RecordSequence does. |
| bad := bytes.Replace(stored, []byte(`"runtime":"rootfs-v1"`), []byte(`"runtime":5`), 1) |
| if _, _, err := WriteRecord(ctx, s, key, stored, bad, etag); !errors.Is(err, ErrRecordSequence) { |
| @@ -271,3 +263,169 @@ func TestBinaryRecordBytesUnchanged(t *testing.T) { |
| t.Fatalf("rootfs record marshals no runtime: %s", body) |
| } |
| } |
| + |
| +// A protected field under a second spelling could pass the guard in one |
| +// order and be stored in the other (AdvanceRecordSequence sorts the |
| +// fields), so any non-canonical spelling refuses, in the body handed in |
| +// and in the stored before, plain or sequenced. |
| +func TestProtectedFieldAliasesRefused(t *testing.T) { |
| + ctx := context.Background() |
| + rec := rootfsTestRecord("a.shop.example.com", false) |
| + key := RecordKey(rec.Account, rec.App, rec.Host) |
| + canonical, err := json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + swap := func(old, repl string) []byte { |
| + out := bytes.Replace(canonical, []byte(old), []byte(repl), 1) |
| + if bytes.Equal(out, canonical) { |
| + t.Fatalf("test body: no %s in %s", old, canonical) |
| + } |
| + return out |
| + } |
| + aliased := map[string][]byte{ |
| + "runtime then RUNTIME": swap(`"runtime":"rootfs-v1"`, `"runtime":"","RUNTIME":"rootfs-v1"`), |
| + "runtime then Runtime": swap(`"runtime":"rootfs-v1"`, `"runtime":"rootfs-v1","Runtime":""`), |
| + "RUNTIME alone": swap(`"runtime":"rootfs-v1"`, `"RUNTIME":"rootfs-v1"`), |
| + "hibernate then HIBERNATE": swap(`"hibernate":false`, `"hibernate":true,"HIBERNATE":false`), |
| + "Hibernate alone": swap(`"hibernate":false`, `"Hibernate":false`), |
| + } |
| + for name, body := range aliased { |
| + for _, seq := range []bool{false, true} { |
| + wctx := ctx |
| + if seq { |
| + wctx = WithRecordSequence(ctx) |
| + } |
| + // Submitted over the canonical stored record. |
| + s := dirStore(t) |
| + etag, err := s.Put(ctx, key, canonical, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := WriteRecord(wctx, s, key, canonical, body, etag); !errors.Is(err, ErrRecordSequence) { |
| + t.Errorf("%s (sequenced %v): err = %v, want ErrRecordSequence", name, seq, err) |
| + } |
| + if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, canonical) { |
| + t.Errorf("%s (sequenced %v): stored body moved: %s (%v)", name, seq, now, err) |
| + } |
| + // Already stored (written around WriteRecord) and written back |
| + // unchanged: a sequenced write-back re-sorts the fields and |
| + // could change what they mean, so it refuses too. |
| + s = dirStore(t) |
| + if etag, err = s.Put(ctx, key, body, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, _, err := WriteRecord(wctx, s, key, body, body, etag); !errors.Is(err, ErrRecordSequence) { |
| + t.Errorf("write-back of stored %s (sequenced %v): err = %v, want ErrRecordSequence", name, seq, err) |
| + } |
| + if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, body) { |
| + t.Errorf("write-back of stored %s (sequenced %v): body moved: %s (%v)", name, seq, now, err) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// A write-back moves nothing, so it passes even for a record the guard |
| +// could not otherwise read or would refuse: the lifetime fence and the |
| +// migration supersede depend on it. Plain and sequenced. |
| +func TestWriteBackUnchangedPasses(t *testing.T) { |
| + ctx := context.Background() |
| + for name, body := range map[string]string{ |
| + "corrupt hibernate": `{"schema":1,"host":"a.shop.example.com","slug":"shop","app":"shop","account":"bab","channel":"stable","created_at":"2026-10-26T09:00:00Z","hibernate":"yes"}`, |
| + "rootfs without hibernate": `{"schema":1,"host":"a.shop.example.com","slug":"shop","app":"shop","account":"bab","channel":"stable","created_at":"2026-10-26T09:00:00Z","runtime":"rootfs-v1"}`, |
| + } { |
| + for _, seq := range []bool{false, true} { |
| + s := dirStore(t) |
| + key := RecordKey("bab", "shop", "a.shop.example.com") |
| + etag, err := s.Put(ctx, key, []byte(body), "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + wctx := ctx |
| + if seq { |
| + wctx = WithRecordSequence(ctx) |
| + } |
| + if _, _, err := WriteRecord(wctx, s, key, []byte(body), []byte(body), etag); err != nil { |
| + t.Errorf("%s (sequenced %v): write-back err = %v, want nil", name, seq, err) |
| + } |
| + now, _, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if seq && !strings.Contains(string(now), `"record_seq":1`) { |
| + t.Errorf("%s: sequenced write-back stored %s, want record_seq 1", name, now) |
| + } |
| + if name == "rootfs without hibernate" { |
| + // Moving it still refuses: the write-back rule is no door. |
| + var m map[string]json.RawMessage |
| + _ = json.Unmarshal(now, &m) |
| + m["hibernate"] = json.RawMessage("false") |
| + moved, _ := json.Marshal(m) |
| + _, etag, _ := s.Get(ctx, key) |
| + if _, _, err := WriteRecord(wctx, s, key, now, moved, etag); !errors.Is(err, ErrRuntimeProtected) { |
| + t.Errorf("%s (sequenced %v): adding hibernate err = %v, want ErrRuntimeProtected", name, seq, err) |
| + } |
| + } |
| + } |
| + } |
| +} |
| + |
| +// A before of {} is a create: create-only, so it takes no etag. With one, |
| +// it would replace the stored record unread. |
| +func TestCreateBeforeTakesNoETag(t *testing.T) { |
| + ctx := context.Background() |
| + s := dirStore(t) |
| + rec := rootfsTestRecord("a.shop.example.com", false) |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := RecordKey(rec.Account, rec.App, rec.Host) |
| + stored, etag, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + rec.Runtime, rec.Hibernate = "", nil |
| + body, err := json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, wctx := range []context.Context{ctx, WithRecordSequence(ctx)} { |
| + if _, _, err := WriteRecord(wctx, s, key, []byte(`{}`), body, etag); !errors.Is(err, ErrRecordSequence) { |
| + t.Errorf("{} with etag: err = %v, want ErrRecordSequence", err) |
| + } |
| + } |
| + if now, _, err := s.Get(ctx, key); err != nil || !bytes.Equal(now, stored) { |
| + t.Fatalf("stored body moved: %s (%v)", now, err) |
| + } |
| +} |
| + |
| +// The tombstone match leaves record_seq to sequencing: a record created |
| +// sequenced carries the counter, and its tombstone is stored with it |
| +// advanced, so the final bytes differ from TombstoneBody(before) in it. |
| +func TestSequencedTombstonePasses(t *testing.T) { |
| + ctx := WithRecordSequence(context.Background()) |
| + s := dirStore(t) |
| + rec := rootfsTestRecord("a.shop.example.com", false) |
| + if err := CreateRecord(ctx, s, rec); err != nil { |
| + t.Fatal(err) |
| + } |
| + key := RecordKey(rec.Account, rec.App, rec.Host) |
| + stored, etag, err := s.Get(ctx, key) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !strings.Contains(string(stored), `"record_seq":1`) { |
| + t.Fatalf("sequenced create stored %s, want record_seq 1", stored) |
| + } |
| + tomb, err := TombstoneBody(stored) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, seq, err := WriteRecord(ctx, s, key, stored, tomb, etag); err != nil || seq != 2 { |
| + t.Fatalf("sequenced tombstone: seq %d, err %v; want 2, nil", seq, err) |
| + } |
| + now, _, err := s.Get(ctx, key) |
| + if err != nil || strings.Contains(string(now), `"runtime"`) || !strings.Contains(string(now), `"record_seq":2`) || !strings.Contains(string(now), `"retired":true`) { |
| + t.Fatalf("after the tombstone: %s (%v)", now, err) |
| + } |
| +} |