From 3a700cddfa31dabcfe7800e948ebf8998af39b4b Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Mon, 16 Nov 2026 04:08:06 +1100 Subject: [PATCH] fix(connectors,runs): an approved action runs as judged however its input was re-encoded, and one that can't be confirmed is reported as unknown (archie #1985, #1990) --- modules/approvals/approvals.go | 9 +++- modules/connectors/connector.go | 19 +++++++- modules/connectors/digest_test.go | 75 +++++++++++++++++++++++++++++++ modules/runs/investigate.go | 23 ++++++++-- 4 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 modules/connectors/digest_test.go diff --git a/modules/approvals/approvals.go b/modules/approvals/approvals.go index 670ddf901..5aebb9401 100644 --- a/modules/approvals/approvals.go +++ b/modules/approvals/approvals.go @@ -182,6 +182,13 @@ func (m *Module) Answer(ctx context.Context, id uuid.UUID, by, via string, appro return requestOf(row, judgement.Decision{}), nil } +// Settle answers a request on one of the channels that carries it, which is +// what a Telegram tap calls. It reports only whether the answer was taken. +func (m *Module) Settle(ctx context.Context, id uuid.UUID, by, via string, approved bool) error { + _, err := m.Answer(ctx, id, by, via, approved) + return err +} + // Expire settles a request whose deadline passed, so the trail shows why it // never ran. func (m *Module) Expire(ctx context.Context, id uuid.UUID) error { @@ -315,7 +322,7 @@ func answerText(a Answer) string { if a.Approved { verb = "Approved" } - return fmt.Sprintf("%s by %s on the %s.", verb, a.By, a.Via) + return fmt.Sprintf("%s by %s in the %s.", verb, a.By, a.Via) } // Summary is what a person is shown about the action they are asked to allow: diff --git a/modules/connectors/connector.go b/modules/connectors/connector.go index 271001894..0af367344 100644 --- a/modules/connectors/connector.go +++ b/modules/connectors/connector.go @@ -255,6 +255,7 @@ func (c *Connector) Invoke(ctx context.Context, inv Invocation) (string, error) if !a.effect.ReadOnly && !a.idempotent { return "", fmt.Errorf("connector %q: action %q: %w", c.name, inv.Action, ErrNotIdempotent) } + inv.Input = Canonical(inv.Input) digest := Digest(inv.Action, inv.Input) resp, err := c.client.Invoke(ctx, &pb.InvokeRequest{ Action: inv.Action, Input: inv.Input, IdempotencyKey: inv.IdempotencyKey, Digest: digest, @@ -266,7 +267,7 @@ func (c *Connector) Invoke(ctx context.Context, inv Invocation) (string, error) return "", fmt.Errorf("connector %q: action %q: %w", c.name, inv.Action, ErrDigestMismatch) } if resp.GetOutcomeUnknown() { - return "", fmt.Errorf("connector %q: action %q: %w", c.name, inv.Action, ErrOutcomeUnknown) + return "", fmt.Errorf("connector %q: action %q: %w: %s", c.name, inv.Action, ErrOutcomeUnknown, resp.GetOutput()) } return string(resp.GetOutput()), nil } @@ -305,10 +306,24 @@ func Digest(action string, input jsontext.Value) string { h := sha256.New() h.Write([]byte(action)) h.Write([]byte{0}) - h.Write(input) + h.Write(Canonical(input)) return hex.EncodeToString(h.Sum(nil)) } +// Canonical is the form an input is hashed and sent in. Two spellings of the +// same JSON are the same action, so the bytes mustn't depend on spacing or key +// order: an input that has been through a step record or a decision body comes +// back spelled differently, and an action must still run as the one judged. +// RFC 8785's form keeps a number's exact digits, so two large ids stay two +// actions. +func Canonical(input jsontext.Value) jsontext.Value { + out := slices.Clone(input) + if err := out.Canonicalize(jsontext.CanonicalizeRawInts(false)); err != nil { + return input + } + return out +} + func effectFrom(e *pb.Effect) Effect { return Effect{ReadOnly: e.GetReadOnly(), Reversibility: Reversibility(e.GetReversibility()), Summary: e.GetSummary()} } diff --git a/modules/connectors/digest_test.go b/modules/connectors/digest_test.go new file mode 100644 index 000000000..256c2730c --- /dev/null +++ b/modules/connectors/digest_test.go @@ -0,0 +1,75 @@ +package connectors + +import ( + "encoding/json/jsontext" + json "encoding/json/v2" + "testing" +) + +// TestDigestIgnoresHowJSONIsSpelled guards the rule that makes an approval +// mean one action: an input that has been through a step record or a decision +// body comes back re-encoded, and the digest over it must match the digest of +// what the agent wrote. A digest that changed would refuse a judged action as +// altered, so an approved action would never run. +func TestDigestIgnoresHowJSONIsSpelled(t *testing.T) { + tests := []struct { + name string + a jsontext.Value + b jsontext.Value + same bool + }{ + { + name: "spacing doesn't change the action", + a: jsontext.Value(`{"clean_dir": "/tmp", "host": "web-1"}`), + b: jsontext.Value(`{"clean_dir":"/tmp","host":"web-1"}`), + same: true, + }, + { + name: "key order doesn't change the action", + a: jsontext.Value(`{"host":"web-1","clean_dir":"/tmp"}`), + b: jsontext.Value(`{"clean_dir":"/tmp","host":"web-1"}`), + same: true, + }, + { + name: "two large ids are two actions", + a: jsontext.Value(`{"id":9007199254740993}`), + b: jsontext.Value(`{"id":9007199254740992}`), + same: false, + }, + { + name: "a different value is a different action", + a: jsontext.Value(`{"host":"web-1","clean_dir":"/tmp"}`), + b: jsontext.Value(`{"host":"web-1","clean_dir":"/var/tmp"}`), + same: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := Digest("clean", tt.a) == Digest("clean", tt.b) + if got != tt.same { + t.Fatalf("same digest = %v, want %v", got, tt.same) + } + }) + } +} + +// TestDigestSurvivesAStepRecord is the same rule through the encoding a run +// actually puts an input through: a value marshalled into a step and read back. +func TestDigestSurvivesAStepRecord(t *testing.T) { + judged := jsontext.Value(`{"host": "web-1", "older_than_hours": "24"}`) + recorded, err := json.Marshal(struct { + Input jsontext.Value `json:"input"` + }{Input: judged}) + if err != nil { + t.Fatalf("encode: %v", err) + } + var back struct { + Input jsontext.Value `json:"input"` + } + if err := json.Unmarshal(recorded, &back); err != nil { + t.Fatalf("decode: %v", err) + } + if Digest("clean", back.Input) != Digest("clean", judged) { + t.Fatalf("digest changed through a step record: %s vs %s", back.Input, judged) + } +} diff --git a/modules/runs/investigate.go b/modules/runs/investigate.go index ec19f3905..f48fa0a82 100644 --- a/modules/runs/investigate.go +++ b/modules/runs/investigate.go @@ -141,6 +141,9 @@ type approvalOutcome struct { By string `json:"by,omitzero"` Result string `json:"result,omitzero"` Error string `json:"error,omitzero"` + // Unknown is an action that was started but can't be confirmed: it may + // have taken effect, and it is never started again. + Unknown bool `json:"unknown,omitzero"` } // settleProposals puts every action judgement held to a person, one at a time, @@ -198,9 +201,19 @@ func settleOne(rc *Context, judge Judge, ask Approver, d judgement.Decision, dea // has recorded it, so the investigation still reports. run, runErr := runApproved(rc, judge, d, answer) out.State, out.Result, out.Error = approvals.StateApproved, run, errText(runErr) + out.Unknown = errors.Is(runErr, ErrOutcomeUnknown) return out, nil } +// unknownOutcome records a connector's unconfirmed action as a step whose +// outcome is unknown, so a resumed run reports it and never runs it again. +func unknownOutcome(err error) error { + if errors.Is(err, connectors.ErrOutcomeUnknown) { + return fmt.Errorf("%w: %w", ErrOutcomeUnknown, err) + } + return err +} + // errText is a failed action's reason, or nothing when it ran. func errText(err error) string { if err == nil { @@ -248,11 +261,13 @@ func runApproved(rc *Context, judge Judge, d judgement.Decision, answer approval case fresh.Level == judgement.Never: return "", fmt.Errorf("refused after approval: %s", fresh.Reason) case fresh.Level.Runs(): - return judge.Execute(ctx, fresh, d.Action.Input, key) + out, err := judge.Execute(ctx, fresh, d.Action.Input, key) + return out, unknownOutcome(err) case fresh.Level > d.Level: return "", fmt.Errorf("judgement got worse after approval: now %s", fresh.Level) } - return judge.ExecuteApproved(ctx, d, answer.Approval(d), d.Action.Input, key) + out, err := judge.ExecuteApproved(ctx, d, answer.Approval(d), d.Action.Input, key) + return out, unknownOutcome(err) }) } @@ -298,8 +313,10 @@ func outcomeLines(outcomes []approvalOutcome) string { for _, o := range outcomes { line := fmt.Sprintf("- %s on %s: %s", o.Action, o.Target, o.State) switch { + case o.Unknown: + line += ", it was started but its outcome is unknown: " + o.Error case o.Error != "": - line += ", " + o.Error + line += ", it did not run: " + o.Error case o.Result != "": line += ", it ran: " + o.Result case o.State == approvals.StateApproved: