console: restore proof fails closed on corrupt reads (#658)
Astra task 5 review round 1: - A corrupt record under the key (a body that does not decode, or names another account, app or host) is row 1, R1/503, as an unreadable one is; it no longer falls through to an eligible verdict. - restoreAsk gains RegionSpecified. A request naming the home region on a console outside it normalizes to "" but is now R5; an omitted region still takes the stored one. - A kept record_body that does not decode is R1/503, not R8. An absent retirement or body stays R8.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+114
−20
internal/console/restore_proof.go+28 −17internal/console/restore_proof_test.go+86 −3
diff --git a/internal/console/restore_proof.go b/internal/console/restore_proof.go| index 30c191972..fef71c606 100644 |
| --- a/internal/console/restore_proof.go |
| +++ b/internal/console/restore_proof.go |
| @@ -64,12 +64,15 @@ func msgInstanceExists(host string) string { |
| // Task 1 note §3) and AccountID its numeric id, the key of its apps row. |
| // Lifetime is the app's current apps.lifetime, as the door read it |
| // (AppLife(AccountID, App)). Region is the request's, normalized with |
| -// homeRegionIn, so naming the home region is "". Kind, Role and Addrs are |
| +// homeRegionIn, so naming the home region is "", and RegionSpecified says |
| +// whether the request named one at all: "" with it set is the home region |
| +// named, not a region left out. Kind, Role and Addrs are |
| // the request's; Placement is appPlacement's answer and Pool the manifest's |
| // pool. Shape is nil at the door's early call and the record actually being |
| // written at the deciding call. |
| type restoreAsk struct { |
| Account, App, Lifetime, Host, Region string |
| + RegionSpecified bool |
| AccountID int64 |
| Kind, Role, Placement, Pool string |
| Addrs []string |
| @@ -99,7 +102,8 @@ type restoreVerdict struct { |
| // restoreProof answers ask by the admission table, the first row that |
| // applies, in this order: |
| // |
| -// 1. a source it reads cannot be read: R1 (503) |
| +// 1. a source it reads cannot be read (a corrupt record or kept body |
| +// among them): R1 (503) |
| // 2. the mark or the slot does not name the account and app: S1 |
| // 3. an allocation, retirement or migration of the host is pending: R1 |
| // 4. a valid record of this account and app stands under the key: duplicate |
| @@ -112,7 +116,7 @@ type restoreVerdict struct { |
| // else, or is not a committed, restorable normal retirement: R8 |
| // 10. the stored body, the request or the built record is not plain: R7 |
| // 11. the stored body is not in this console's region, or the request |
| -// names another region: R5 |
| +// names another region (the home region named counts): R5 |
| // 12. any object under moves/<host>/: R6 |
| // 13. otherwise eligible. |
| // |
| @@ -169,7 +173,9 @@ func (s *Server) restoreProof(ctx context.Context, ask restoreAsk) restoreVerdic |
| // Row 4: the owner's own record stands, as a retry after success finds. |
| obs := s.judge.Observe(ctx, s.cfg.Releases, ask.Account, ask.App, host) |
| - if obs.Verdict == provision.LifetimeUnreadable { |
| + // A corrupt body (one that does not decode, or names another key) is |
| + // a record that cannot be read: row 1, never "absent". |
| + if obs.Verdict == provision.LifetimeUnreadable || obs.Verdict == provision.LifetimeCorrupt { |
| return unchecked(true, "read the record", obs.Err) |
| } |
| if validRecord(obs) { |
| @@ -210,7 +216,10 @@ func (s *Server) restoreProof(ctx context.Context, ask restoreAsk) restoreVerdic |
| if err != nil { |
| return unchecked(true, "read the newest retirement", err) |
| } |
| - body, why := retiredBody(e, ask, slot.Incarnation) |
| + body, why, err := retiredBody(e, ask, slot.Incarnation) |
| + if err != nil { |
| + return unchecked(true, "decode the retired body", err) |
| + } |
| if why != "" { |
| return tooOld(why) |
| } |
| @@ -221,10 +230,10 @@ func (s *Server) restoreProof(ctx context.Context, ask restoreAsk) restoreVerdic |
| } |
| // Row 11: this console's own region. |
| - if body.Region != s.cfg.Region || (ask.Region != "" && ask.Region != s.cfg.Region) || |
| + if body.Region != s.cfg.Region || ((ask.RegionSpecified || ask.Region != "") && ask.Region != s.cfg.Region) || |
| (ask.Shape != nil && ask.Shape.Region != body.Region) { |
| return refuse(msgRestoreRegion(host, s.restoreRegionName(body.Region)), |
| - fmt.Sprintf("stored region %q, requested %q, console %q", body.Region, ask.Region, s.cfg.Region)) |
| + fmt.Sprintf("stored region %q, requested %q (named %v), console %q", body.Region, ask.Region, ask.RegionSpecified, s.cfg.Region)) |
| } |
| // Row 12: a move's staged copy. |
| @@ -297,28 +306,30 @@ func lineageGap(rows []Allocation, ask restoreAsk, incarnation uint64) string { |
| // retiredBody is row 9 (spec §4 stage 2 item 4; controller's ruling 1): the |
| // newest retirement is a committed, restorable normal retirement of this |
| // account and app at the slot's incarnation, and the body it kept is this |
| -// host's record of the same. It answers the body, or why not. |
| -func retiredBody(e *RetirementEvidence, ask restoreAsk, incarnation uint64) (provision.Record, string) { |
| +// host's record of the same. It answers the body, or why not (R8). A kept |
| +// body that does not decode is an error instead: a source that cannot be |
| +// read (row 1), not missing evidence. |
| +func retiredBody(e *RetirementEvidence, ask restoreAsk, incarnation uint64) (provision.Record, string, error) { |
| switch { |
| case e == nil: |
| - return provision.Record{}, "no retirement" |
| + return provision.Record{}, "no retirement", nil |
| case e.Kind != retireNormal || e.State != lifetimeCommitted || !e.Restorable: |
| - return provision.Record{}, fmt.Sprintf("the newest retirement %s is %s %s, restorable %v", e.OpID, e.Kind, e.State, e.Restorable) |
| + return provision.Record{}, fmt.Sprintf("the newest retirement %s is %s %s, restorable %v", e.OpID, e.Kind, e.State, e.Restorable), nil |
| case e.Incarnation != incarnation: |
| - return provision.Record{}, fmt.Sprintf("the newest retirement %s is of incarnation %d, the slot's is %d", e.OpID, e.Incarnation, incarnation) |
| + return provision.Record{}, fmt.Sprintf("the newest retirement %s is of incarnation %d, the slot's is %d", e.OpID, e.Incarnation, incarnation), nil |
| case e.Account != ask.Account || e.App != ask.App: |
| - return provision.Record{}, "the newest retirement " + e.OpID + " names another account or app" |
| + return provision.Record{}, "the newest retirement " + e.OpID + " names another account or app", nil |
| case len(e.RecordBody) == 0: |
| - return provision.Record{}, "the newest retirement " + e.OpID + " kept no body" |
| + return provision.Record{}, "the newest retirement " + e.OpID + " kept no body", nil |
| } |
| body, err := provision.DecodeRecord(e.RecordBody) |
| if err != nil { |
| - return provision.Record{}, "the newest retirement " + e.OpID + " kept a body that does not decode" |
| + return provision.Record{}, "", fmt.Errorf("the newest retirement %s: %w", e.OpID, err) |
| } |
| if body.Host != ask.Host || body.Account != ask.Account || body.App != ask.App || body.Retired || body.Incarnation != e.Incarnation { |
| - return provision.Record{}, "the newest retirement " + e.OpID + " kept a body of another record" |
| + return provision.Record{}, "the newest retirement " + e.OpID + " kept a body of another record", nil |
| } |
| - return body, "" |
| + return body, "", nil |
| } |
| // restorePlain reports whether rec is a plain instance (spec §4 stage 2 item |
diff --git a/internal/console/restore_proof_test.go b/internal/console/restore_proof_test.go| index 114e68b66..aaf2ebddb 100644 |
| --- a/internal/console/restore_proof_test.go |
| +++ b/internal/console/restore_proof_test.go |
| @@ -50,6 +50,7 @@ type proofFx struct { |
| ask restoreAsk |
| recBefore []byte |
| slotBefore []byte |
| + stored *provision.Record // the kept body setBody wrote, nil for the deleted record's own |
| } |
| // newProofBase creates ltOne through the door with the history active and |
| @@ -164,6 +165,7 @@ func (p *proofFx) setBody(t *testing.T, mutate func(*provision.Record)) { |
| t.Fatal(err) |
| } |
| p.exec(t, `UPDATE record_retirements SET record_body = ? WHERE host = ?`, b, ltOne) |
| + p.stored = &rec |
| } |
| // failGets fails every read of key through the console's bucket. |
| @@ -196,9 +198,31 @@ func (p *proofFx) commitAllocation(t *testing.T, a Allocation) { |
| // deleted is the record as the delete retired it. |
| func (p *proofFx) deleted(t *testing.T) provision.Record { |
| t.Helper() |
| + if p.stored != nil { |
| + return *p.stored |
| + } |
| return decodeRec(t, p.recBefore) |
| } |
| +// putRecord writes raw under ltOne's record key. |
| +func (p *proofFx) putRecord(t *testing.T, raw []byte) { |
| + t.Helper() |
| + p.overwrite(t, recKey(ltApp, ltOne), raw) |
| +} |
| + |
| +// foreignRecord is the deleted record, named for another key by mutate, |
| +// under ltOne's own key: a body whose identity disagrees with its key. |
| +func (p *proofFx) foreignRecord(t *testing.T, mutate func(*provision.Record)) { |
| + t.Helper() |
| + rec := decodeRec(t, p.recBefore) |
| + mutate(&rec) |
| + b, err := json.Marshal(rec) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + p.putRecord(t, b) |
| +} |
| + |
| // requireVerdict holds v to one answer of the table. class is R1-pre (an |
| // unreadable source before ownership), R1, S1, dup, R2, R4 to R8, or ok; |
| // region is R5's <region>. |
| @@ -313,6 +337,16 @@ func TestRestoreProof(t *testing.T) { |
| // Row 4. |
| {"the owner's valid record and nothing pending", func(t *testing.T, p *proofFx) { p.reviveRecord(t) }, "dup", ""}, |
| {"an unreadable record", func(t *testing.T, p *proofFx) { p.failGets(recKey(ltApp, ltOne)) }, "R1", ""}, |
| + {"a record that does not decode", func(t *testing.T, p *proofFx) { p.putRecord(t, []byte("{")) }, "R1", ""}, |
| + {"a record naming another account", func(t *testing.T, p *proofFx) { |
| + p.foreignRecord(t, func(r *provision.Record) { r.Account = "zzz" }) |
| + }, "R1", ""}, |
| + {"a record naming another app", func(t *testing.T, p *proofFx) { |
| + p.foreignRecord(t, func(r *provision.Record) { r.App = "blog" }) |
| + }, "R1", ""}, |
| + {"a record naming another host", func(t *testing.T, p *proofFx) { |
| + p.foreignRecord(t, func(r *provision.Record) { r.Host = ltFresh }) |
| + }, "R1", ""}, |
| // Row 5. |
| {"the history incomplete", func(t *testing.T, p *proofFx) { |
| forceHostsState(t, p.fx.srv, provision.HostsState{Mode: provision.HostsModeIncomplete}) |
| @@ -381,6 +415,12 @@ func TestRestoreProof(t *testing.T) { |
| {"the newest retirement of another app", func(t *testing.T, p *proofFx) { |
| p.exec(t, `UPDATE record_retirements SET app = 'blog' WHERE host = ?`, ltOne) |
| }, "R8", ""}, |
| + {"a kept body that does not decode", func(t *testing.T, p *proofFx) { |
| + p.exec(t, `UPDATE record_retirements SET record_body = ? WHERE host = ?`, []byte("{"), ltOne) |
| + }, "R1", ""}, |
| + {"no retirement at all", func(t *testing.T, p *proofFx) { |
| + p.exec(t, `DELETE FROM record_retirements WHERE host = ?`, ltOne) |
| + }, "R8", ""}, |
| {"the newest retirement kept no body", func(t *testing.T, p *proofFx) { |
| p.exec(t, `UPDATE record_retirements SET record_body = NULL WHERE host = ?`, ltOne) |
| }, "R8", ""}, |
| @@ -431,12 +471,14 @@ func TestRestoreProof(t *testing.T) { |
| {"the stored body in another region", func(t *testing.T, p *proofFx) { |
| p.setBody(t, func(r *provision.Record) { r.Region = "uk" }) |
| }, "R5", "uk"}, |
| - {"the request naming another region", func(t *testing.T, p *proofFx) { p.ask.Region = "uk" }, "R5", "ie"}, |
| + {"the request naming another region", func(t *testing.T, p *proofFx) { |
| + p.ask.Region, p.ask.RegionSpecified = "uk", true |
| + }, "R5", "ie"}, |
| {"ask.Shape in another region", func(t *testing.T, p *proofFx) { |
| p.ask.Shape = plainShape(p, func(r *provision.Record) { r.Region = "uk" }) |
| }, "R5", "ie"}, |
| {"the request naming the home region", func(t *testing.T, p *proofFx) { |
| - p.ask.Region = p.fx.srv.homeRegionIn("ie") |
| + p.ask.Region, p.ask.RegionSpecified = p.fx.srv.homeRegionIn("ie"), true |
| }, "ok", ""}, |
| // Row 12. |
| {"a move's build-state.json", func(t *testing.T, p *proofFx) { |
| @@ -486,6 +528,45 @@ func TestRestoreProofStrangerReadsNothingMore(t *testing.T) { |
| } |
| } |
| +// TestRestoreProofCorruptRecordPrecedence: a corrupt record under the key |
| +// is row 1 at row 4's read, so it answers R1 over every later row's |
| +// refusal (rows 5 to 12, built by proofRows). |
| +func TestRestoreProofCorruptRecordPrecedence(t *testing.T) { |
| + for _, row := range proofRows()[4:] { |
| + t.Run(fmt.Sprintf("over %d", row.n), func(t *testing.T) { |
| + p := newProofFx(t) |
| + row.apply(t, p) |
| + p.putRecord(t, []byte("{")) |
| + p.requireVerdict(t, p.prove(), "R1", "") |
| + }) |
| + } |
| +} |
| + |
| +// TestRestoreProofNonHomeConsole: on a console in uk whose home is ie, |
| +// with the stored body in uk, a request naming no region or uk is |
| +// eligible, and one naming the home region by name is R5, though its |
| +// normalized region is "" like an omitted one. |
| +func TestRestoreProofNonHomeConsole(t *testing.T) { |
| + for _, c := range []struct { |
| + name string |
| + region string |
| + specified bool |
| + class string |
| + }{ |
| + {"no region named", "", false, "ok"}, |
| + {"its own region", "uk", true, "ok"}, |
| + {"the home region", "ie", true, "R5"}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + p := newProofFx(t) |
| + p.setBody(t, func(r *provision.Record) { r.Region = "uk" }) |
| + p.fx.srv.cfg.Region = "uk" |
| + p.ask.Region, p.ask.RegionSpecified = p.fx.srv.homeRegionIn(c.region), c.specified |
| + p.requireVerdict(t, p.prove(), c.class, "uk") |
| + }) |
| + } |
| +} |
| + |
| // TestRestoreProofPendingRetirement (Review Focus 3): a delete pending |
| // before N1 with the record still valid, after N1, and after N2 is R1 each |
| // time, never the duplicate; once recovery settles it, the restore is |
| @@ -599,7 +680,9 @@ func proofRows() []proofRow { |
| p.exec(t, `UPDATE record_retirements SET restorable = 0 WHERE host = ?`, ltOne) |
| }}, |
| {10, "the request is a seed", "R7", "", func(t *testing.T, p *proofFx) { p.ask.From = true }}, |
| - {11, "the request names another region", "R5", "ie", func(t *testing.T, p *proofFx) { p.ask.Region = "uk" }}, |
| + {11, "the request names another region", "R5", "ie", func(t *testing.T, p *proofFx) { |
| + p.ask.Region, p.ask.RegionSpecified = "uk", true |
| + }}, |
| {12, "a move's staging", "R6", "", func(t *testing.T, p *proofFx) { p.stage(t, "moves/"+ltOne+"/build-state.json") }}, |
| } |
| } |