adopt: zero every live capture; malformed success is inconclusive
Review round 1 on the live checks. The recorder keeps every captured request and answer, and close zeroes them all, not only the two kept for replay. member-allowed and owner-allowed pass only when the client read and decoded the answer; a malformed or oversized 2xx is inconclusive. RunLive's refusal strings move to words.go, pending copy review.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+176
−24
adopt/live.go+44 −22adopt/live_test.go+122 −0adopt/words.go+10 −2
diff --git a/adopt/live.go b/adopt/live.go| index 21dbec0..5c239b5 100644 |
| --- a/adopt/live.go |
| +++ b/adopt/live.go |
| @@ -131,12 +131,23 @@ type exchange struct { |
| body []byte |
| } |
| -// recorder sits under a client's HTTP and keeps the last operations |
| -// request it sent and the answer, so the request can be replayed byte for |
| -// byte. Nothing else is kept. |
| +// recorder sits under a client's HTTP and keeps every operations request |
| +// it sent and the answer, so a request can be replayed byte for byte and |
| +// every captured bearer and body zeroed at the end. last is the newest. |
| type recorder struct { |
| base http.RoundTripper |
| last *exchange |
| + all []*exchange |
| +} |
| + |
| +// zero overwrites every captured request and answer body. |
| +func (r *recorder) zero() { |
| + for _, ex := range r.all { |
| + ex.req.zero() |
| + clear(ex.body) |
| + ex.body, ex.header = nil, nil |
| + } |
| + r.all, r.last = nil, nil |
| } |
| func (r *recorder) RoundTrip(req *http.Request) (*http.Response, error) { |
| @@ -161,6 +172,7 @@ func (r *recorder) RoundTrip(req *http.Request) (*http.Response, error) { |
| } |
| ex.req.bearer = []byte(bearer) |
| r.last = ex |
| + r.all = append(r.all, ex) |
| resp, err := r.base.RoundTrip(req) |
| if err != nil { |
| return nil, err |
| @@ -201,17 +213,29 @@ func newLiveConn() *liveConn { |
| } |
| // call makes one operation call and returns the exchange, nil when no |
| -// request was sent. The bearer's expiry is taken from the store as the |
| -// call left it. |
| -func (l *liveConn) call(ctx context.Context, op string, input []byte) *exchange { |
| +// request was sent, and the client's own error: a success it could not |
| +// read or decode (oversized, invalid JSON) is an error there. The |
| +// bearer's expiry is taken from the store as the call left it. |
| +func (l *liveConn) call(ctx context.Context, op string, input []byte) (*exchange, error) { |
| l.rec.last = nil |
| - _, _ = l.c.Call(ctx, op, input) |
| + _, err := l.c.Call(ctx, op, input) |
| ex := l.rec.last |
| l.rec.last = nil |
| if ex != nil { |
| ex.req.expiry = l.store.expiry() |
| } |
| - return ex |
| + return ex, err |
| +} |
| + |
| +// succeeded is the answer to a call that must succeed: a 2xx counts only |
| +// when the client read and decoded it; a malformed or oversized success |
| +// proves nothing and is ansOther. |
| +func succeeded(ex *exchange, callErr error) string { |
| + answer := answerOfExchange(ex) |
| + if answer == ansAllowed && callErr != nil { |
| + return ansOther |
| + } |
| + return answer |
| } |
| // The answers a check can see, as fixed codes. |
| @@ -316,13 +340,13 @@ func RunLive(ctx context.Context, server, sqid string, p Prompter, s *Store, ope |
| u, perr := url.Parse(server) |
| switch { |
| case perr != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || u.Path != "" && u.Path != "/": |
| - return errors.New("adopt: the live checks need connect's https origin") |
| + return errors.New("adopt: " + LiveNeedOrigin) |
| case sqid == "": |
| - return errors.New("adopt: the live checks need the team") |
| + return errors.New("adopt: " + LiveNeedTeam) |
| } |
| event, perr := strconv.ParseInt(foreignEventID, 10, 64) |
| if perr != nil || event < 1 || strconv.FormatInt(event, 10) != foreignEventID { |
| - return errors.New("adopt: the foreign event is not an event id") |
| + return errors.New("adopt: " + LiveNeedEvent) |
| } |
| if !apply { |
| for _, line := range LiveSteps { |
| @@ -331,7 +355,7 @@ func RunLive(ctx context.Context, server, sqid string, p Prompter, s *Store, ope |
| return nil |
| } |
| if s == nil || p == nil || open == nil { |
| - return errors.New("adopt: the live checks need the journal, a prompter and a browser") |
| + return errors.New("adopt: " + LiveNeedRunners) |
| } |
| r := &liveRun{server: u.Scheme + "://" + u.Host, sqid: sqid, event: event, p: p, s: s, open: open} |
| defer func() { err = errors.Join(err, r.close()) }() |
| @@ -456,7 +480,8 @@ func (r *liveRun) run(ctx context.Context) error { |
| if in, ok := r.teamed(ctx, a); !ok || in { |
| return r.record(ctx, "non-member-refused", liveInconclusive) |
| } |
| - ex := a.call(ctx, "calendar_free_busy", r.busyInput()) |
| + // A success, however malformed, is a non-member let through. |
| + ex, _ := a.call(ctx, "calendar_free_busy", r.busyInput()) |
| if err := r.record(ctx, "non-member-refused", mustRefuse(answerOfExchange(ex), ansUnauthenticated, ansForbidden, ansNotFound)); err != nil { |
| return err |
| } |
| @@ -479,15 +504,15 @@ func (r *liveRun) run(ctx context.Context) error { |
| } |
| // 3. member-allowed: the member's call succeeds, and is kept. |
| - ex = b.call(ctx, "calendar_free_busy", r.busyInput()) |
| - if err := r.record(ctx, "member-allowed", mustSucceed(answerOfExchange(ex))); err != nil { |
| + ex, callErr := b.call(ctx, "calendar_free_busy", r.busyInput()) |
| + if err := r.record(ctx, "member-allowed", mustSucceed(succeeded(ex, callErr))); err != nil { |
| return err |
| } |
| c := r.keep(ex) |
| // 4. member-no-local-permission-refused: membership grants no local |
| // permission, so Paul's event stays out of reach. |
| - ex = b.call(ctx, "calendar_get_event", r.eventInput()) |
| + ex, _ = b.call(ctx, "calendar_get_event", r.eventInput()) |
| if err := r.record(ctx, "member-no-local-permission-refused", mustRefuse(answerOfExchange(ex), ansForbidden, ansNotFound)); err != nil { |
| return err |
| } |
| @@ -547,8 +572,8 @@ func (r *liveRun) owner(ctx context.Context) (string, error) { |
| if in, ok := r.teamed(ctx, o); !ok || !in { |
| return "", r.record(ctx, "owner-allowed", liveInconclusive) |
| } |
| - ex := o.call(ctx, "calendar_free_busy", r.busyInput()) |
| - if err := r.record(ctx, "owner-allowed", mustSucceed(answerOfExchange(ex))); err != nil { |
| + ex, callErr := o.call(ctx, "calendar_free_busy", r.busyInput()) |
| + if err := r.record(ctx, "owner-allowed", mustSucceed(succeeded(ex, callErr))); err != nil { |
| return "", err |
| } |
| c := r.keep(ex) |
| @@ -588,10 +613,7 @@ func (r *liveRun) close() error { |
| c.zero() |
| } |
| for _, l := range r.conns { |
| - if l.rec.last != nil { |
| - l.rec.last.req.zero() |
| - l.rec.last = nil |
| - } |
| + l.rec.zero() |
| l.store.Delete() |
| } |
| r.conns, r.saved = nil, nil |
diff --git a/adopt/live_test.go b/adopt/live_test.go| index 7244ce6..5d56eda 100644 |
| --- a/adopt/live_test.go |
| +++ b/adopt/live_test.go |
| @@ -182,6 +182,12 @@ func (f *fakeConnect) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| case status >= 300 && status < 400: |
| w.Header().Set("Location", f.srv.URL+"/followed") |
| w.WriteHeader(status) |
| + case status == 200 && code == "malformed": |
| + w.Header().Set("Content-Type", "application/json") |
| + io.WriteString(w, `{"tz":`) |
| + case status == 200 && code == "oversized": |
| + w.Header().Set("Content-Type", "application/json") |
| + io.WriteString(w, `"`+strings.Repeat("a", client.MaxResponse)+`"`) |
| case code == "plain": |
| // An app's own refusal: no challenge, no agent body. |
| w.WriteHeader(status) |
| @@ -808,6 +814,122 @@ func TestLiveCloseZeroes(t *testing.T) { |
| } |
| } |
| +// Every request a run captured is zeroed by close, not only the two kept |
| +// for replay: the non-member's, the foreign event's, and a refused or |
| +// malformed call's too. |
| +func TestLiveCloseZeroesEveryCapturedRequest(t *testing.T) { |
| + for name, brk := range map[string]func(*liveFixture){ |
| + "pass": func(*liveFixture) {}, |
| + "foreign": func(x *liveFixture) { x.f.foreignReadable = true }, |
| + "member-refused": func(x *liveFixture) { x.f.memberRefused = true }, |
| + "owner-malformed": func(x *liveFixture) { |
| + x.f.answer = func(op liveOp) (int, string) { |
| + if op.token == 2 { |
| + return 200, "malformed" |
| + } |
| + return 0, "" |
| + } |
| + }, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + x := newLive(t) |
| + brk(x) |
| + ev, _ := strconv.ParseInt(liveEvent, 10, 64) |
| + r := &liveRun{server: x.f.srv.URL, sqid: liveSQID, event: ev, p: x.p, s: x.s, open: x.f.open} |
| + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| + defer cancel() |
| + _ = r.run(ctx) |
| + var held [][]byte |
| + bearers := 0 |
| + for _, l := range r.conns { |
| + for _, ex := range l.rec.all { |
| + if len(ex.req.bearer) > 0 { |
| + bearers++ |
| + } |
| + held = append(held, ex.req.bearer, ex.req.body, ex.body) |
| + } |
| + } |
| + for _, c := range r.saved { |
| + bearers++ |
| + held = append(held, c.bearer, c.body) |
| + } |
| + // Every call the clients made, the replays (sent again from |
| + // the saved bytes) aside. |
| + ops, _, _, _, _ := x.f.state() |
| + calls := map[string]bool{} |
| + for _, op := range ops { |
| + calls[op.name+":"+strconv.Itoa(op.token)] = true |
| + } |
| + if bearers != len(calls) { |
| + t.Fatalf("%d bearers held for %d calls made", bearers, len(calls)) |
| + } |
| + for _, b := range held { |
| + if len(b) > 0 && bytes.Equal(b, make([]byte, len(b))) { |
| + t.Fatal("zeroed before close") |
| + } |
| + } |
| + if err := r.close(); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, b := range held { |
| + if !bytes.Equal(b, make([]byte, len(b))) { |
| + t.Fatal("a captured request or answer outlived the run") |
| + } |
| + } |
| + allRevoked(t, x.f) |
| + }) |
| + } |
| +} |
| + |
| +// A success the client could not read or decode proves nothing for a |
| +// call that must succeed; for a call that must be refused it is still |
| +// access granted. |
| +func TestLiveMalformedSuccess(t *testing.T) { |
| + for _, code := range []string{"malformed", "oversized"} { |
| + t.Run("member "+code, func(t *testing.T) { |
| + x := newLive(t) |
| + x.f.answer = func(op liveOp) (int, string) { |
| + if op.token == 1 && op.name == freeBusy && x.f.members[subjTest] { |
| + return 200, code |
| + } |
| + return 0, "" |
| + } |
| + if err := x.run(t); err == nil { |
| + t.Fatal("a malformed success went on") |
| + } |
| + wantChecks(t, x.s, "non-member-refused=passed", "member-allowed=inconclusive") |
| + allRevoked(t, x.f) |
| + }) |
| + t.Run("owner "+code, func(t *testing.T) { |
| + x := newLive(t) |
| + x.f.answer = func(op liveOp) (int, string) { |
| + if op.token == 2 && op.name == freeBusy { |
| + return 200, code |
| + } |
| + return 0, "" |
| + } |
| + if err := x.run(t); err == nil { |
| + t.Fatal("a malformed success went on") |
| + } |
| + wantChecks(t, x.s, append(everyCheckPassed[:5:5], "owner-allowed=inconclusive")...) |
| + allRevoked(t, x.f) |
| + }) |
| + t.Run("non-member "+code, func(t *testing.T) { |
| + x := newLive(t) |
| + x.f.answer = func(op liveOp) (int, string) { |
| + if op.token == 0 { |
| + return 200, code |
| + } |
| + return 0, "" |
| + } |
| + if err := x.run(t); err == nil { |
| + t.Fatal("a non-member's success went on") |
| + } |
| + wantChecks(t, x.s, "non-member-refused=failed:allowed") |
| + }) |
| + } |
| +} |
| + |
| var outcomeShape = regexp.MustCompile(`^(passed|inconclusive|failed:[a-z_-]+)$`) |
| func TestLiveRecordsCodesOnly(t *testing.T) { |
diff --git a/adopt/words.go b/adopt/words.go| index 8398e1c..612f58e 100644 |
| --- a/adopt/words.go |
| +++ b/adopt/words.go |
| @@ -3,8 +3,8 @@ |
| // a change here is a change of copy. |
| // |
| // PENDING COPY REVIEW: WitnessSentence, WriteSummary and the live |
| -// checks' words (LiveSteps, the LiveSay lines and the four sentences) |
| -// are drafts, not yet reviewed with Paul. They are reviewed in Task 14 |
| +// checks' words (LiveSteps, the LiveSay lines, the four sentences and |
| +// the LiveNeed refusals) are drafts, not yet reviewed with Paul. They are reviewed in Task 14 |
| // step 5 with the runner's other strings. |
| package adopt |
| @@ -53,3 +53,11 @@ var LiveSteps = []string{ |
| "8. You confirm you still use calendar directly (owner-local-access-kept).", |
| "Every connection made is disconnected at the end.", |
| } |
| + |
| +// What RunLive says when it refuses to start, after "adopt: ". |
| +const ( |
| + LiveNeedOrigin = "the live checks need connect's https origin" |
| + LiveNeedTeam = "the live checks need the team" |
| + LiveNeedEvent = "the foreign event is not an event id" |
| + LiveNeedRunners = "the live checks need the journal, a prompter and a browser" |
| +) |