oficina / equipo Public

wire: refuse metered reports below protocol 12

Security round finding 7: an API-key attempt's report (its provider, its
key and model prompts, its refusal reasons) and a question with a hold
reason are refused below 12, never translated; a test decodes what is
still sent into frozen pre-12 shapes and values.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev e424e80f83811f70340d291178fd2cd20781644e parent c78522e
2 files changed, +171 −9
  • internal/wire/runners.go +19 −8
  • internal/wire/runners_test.go +152 −1
diff --git a/internal/wire/runners.go b/internal/wire/runners.go
index f2bb053c..c7466ff6 100644
--- a/internal/wire/runners.go
+++ b/internal/wire/runners.go
@@ -480,8 +480,8 @@ func Encode(f Frame, protocol int) (Frame, error) {
if c, ok := body.(*ConnectCommand); ok && c.Kind == KindAPIKey {
return Frame{}, fmt.Errorf("%w: an API-key connection at protocol %d", ErrProtocol, protocol)
}
- if a, ok := body.(*AnswerReport); ok && a.State == QuestionQueued {
- return Frame{}, fmt.Errorf("%w: a held question at protocol %d", ErrProtocol, protocol)
+ if meteredReport(body) {
+ return Frame{}, fmt.Errorf("%w: a metered report at protocol %d", ErrProtocol, protocol)
}
changed = downgradeMetered(body)
}
@@ -529,6 +529,23 @@ func Encode(f Frame, protocol int) (Frame, error) {
return f, nil
}
+// meteredReport says a report only a metered connection makes, which an
+// older instance has no words for: an API-key attempt's report (its
+// provider, its key or model card, its refusal reasons) and a question
+// held by the metered guard (queued, or ended, with a hold reason). It
+// is refused below ProtocolMetered, never translated.
+func meteredReport(body any) bool {
+ switch v := body.(type) {
+ case *ConnectionReport:
+ _, apiKey := APIKeyProviders[v.Provider]
+ prompt := v.Prompt != nil && (v.Prompt.Kind == PromptAPIKey || v.Prompt.Kind == PromptModel || v.Prompt.Models != nil)
+ return apiKey || prompt || v.Reason == "key_refused" || v.Reason == "no_priced_model"
+ case *AnswerReport:
+ return v.State == QuestionQueued || v.Reason == HoldBusy || v.Reason == HoldBudget || v.Reason == HoldSpendUnknown
+ }
+ return false
+}
+
// meteredOnly are the frame types below ProtocolMetered never carries.
var meteredOnly = map[string]bool{FrameModel: true, FrameBudget: true, FrameResumeSpend: true}
@@ -558,12 +575,6 @@ func downgradeMetered(body any) bool {
return held(&v.Connections)
case *Sync:
return held(&v.Connections)
- case *ConnectionReport:
- if v.Prompt == nil || v.Prompt.Models == nil {
- return false
- }
- v.Prompt.Models = nil
- return true
case *ConnectCommand:
set := v.Billing != "" || v.Budget != nil
v.Billing, v.Budget = "", nil
diff --git a/internal/wire/runners_test.go b/internal/wire/runners_test.go
index 425760d0..3469fdcc 100644
--- a/internal/wire/runners_test.go
+++ b/internal/wire/runners_test.go
@@ -623,7 +623,6 @@ func TestEncodeBelowProtocol12DropsMetered(t *testing.T) {
{wire.FrameUsage, wire.UsageReport{ProbeID: "pr_1", Rows: []wire.Usage{priced}}, wire.UsageReport{CheckID: "pr_1", Rows: []wire.Usage{row}}, &wire.UsageReport{}},
{wire.FrameStep, wire.StepReport{TaskID: "tk", Findings: []wire.Finding{}, Usage: []wire.Usage{priced}}, wire.StepReport{TaskID: "tk", Findings: []wire.Finding{}, Usage: []wire.Usage{row}}, &wire.StepReport{}},
{wire.FrameAnswer, wire.AnswerReport{QuestionID: "q", Usage: []wire.Usage{priced}}, wire.AnswerReport{QuestionID: "q", Usage: []wire.Usage{row}}, &wire.AnswerReport{}},
- {wire.FrameConnection, wire.ConnectionReport{Connection: "cn", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptModel, Models: []string{"z-ai/glm-5.3"}}}, wire.ConnectionReport{Connection: "cn", State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptModel}}, &wire.ConnectionReport{}},
} {
f, err := wire.NewFrame("f1", c.kind, c.new)
if err != nil {
@@ -675,3 +674,155 @@ func TestEncodeBelowProtocol12DropsMetered(t *testing.T) {
t.Fatalf("current %d", wire.ProtocolCurrent)
}
}
+
+// Frozen pre-12 shapes and values (security round, finding 7): what an
+// instance at protocol 11 decodes strictly and the values it knows,
+// written down here, not taken from today's types.
+type pre12Prompt struct {
+ Kind string `json:"kind"`
+ URL string `json:"url"`
+ Code string `json:"code"`
+ Attempt int64 `json:"attempt,omitempty"`
+ WrongCode bool `json:"wrong_code,omitempty"`
+}
+
+type pre12Report struct {
+ Connection string `json:"connection"`
+ Person string `json:"person"`
+ LocalID string `json:"local_id"`
+ Provider string `json:"provider"`
+ State string `json:"state"`
+ Reason string `json:"reason"`
+ Label string `json:"label"`
+ Plan string `json:"plan"`
+ Headroom json.RawMessage `json:"headroom"`
+ Prompt *pre12Prompt `json:"prompt"`
+}
+
+type pre12Usage struct {
+ Provider string `json:"provider"`
+ Connection string `json:"connection"`
+ Model string `json:"model"`
+ Attempt int64 `json:"attempt"`
+ Outcome string `json:"outcome"`
+ Complete bool `json:"complete"`
+ InputTokens *int64 `json:"input_tokens"`
+ CachedInputTokens *int64 `json:"cached_input_tokens"`
+ CacheWriteTokens *int64 `json:"cache_write_tokens"`
+ OutputTokens *int64 `json:"output_tokens"`
+ ReasoningOutputTokens *int64 `json:"reasoning_output_tokens"`
+ Turns *int64 `json:"turns,omitempty"`
+}
+
+type pre12UsageReport struct {
+ TurnID string `json:"turn_id"`
+ StepID string `json:"step_id"`
+ QuestionID string `json:"question_id"`
+ CheckID string `json:"check_id,omitempty"`
+ Rows []pre12Usage `json:"rows"`
+}
+
+type pre12Held struct {
+ LocalID string `json:"local_id"`
+ Person string `json:"person"`
+ Provider string `json:"provider"`
+ Kind string `json:"kind"`
+ Label string `json:"label"`
+ State string `json:"state"`
+ Plan string `json:"plan"`
+ Headroom json.RawMessage `json:"headroom"`
+ ExpiresAt *time.Time `json:"expires_at,omitempty"`
+ ExpiresEstimated bool `json:"expires_estimated,omitempty"`
+}
+
+var (
+ pre12PromptKinds = map[string]bool{"device_code": true, "authorize_link": true, "token": true}
+ pre12ConnectReasons = map[string]bool{"": true, "timed_out": true, "cancelled": true, "provider_refused": true, "runner_offline": true, "identity_mismatch": true, "already_connected": true}
+ pre12AnswerReasons = map[string]bool{"": true, "cancelled": true, "interrupted": true, "runner_restarted": true, "turn_timeout": true, "provider_failed": true, "provider_rejected": true, "policy_rejected": true, "account_unavailable": true, "sign_in_again": true, "clone_failed": true, "candidate_unavailable": true, "session_gone": true, "superseded": true, "task_ended": true, "limited": true}
+ pre12AnswerStates = map[string]bool{"running": true, "answered": true, "failed": true, "cancelled": true}
+ pre12Providers = map[string]bool{"claude": true, "codex": true}
+ pre12Kinds = map[string]bool{"subscription": true, "token": true}
+)
+
+// TestEncodeBelowProtocol12AgainstFrozenSchemas: every report a metered
+// connection makes is either refused below protocol 12 or decodes, with
+// unknown fields refused, into the frozen pre-12 shape with only values
+// a protocol 11 instance knows.
+func TestEncodeBelowProtocol12AgainstFrozenSchemas(t *testing.T) {
+ cost, n := int64(5), int64(9)
+ row := wire.Usage{Provider: wire.ProviderTensorX, Connection: "tensorx-1", Model: "z-ai/glm-5.3", Attempt: 1, Outcome: wire.OutcomeReplied, Complete: true, InputTokens: &n, CostMicros: &cost, Currency: "USD", PriceVersion: "v"}
+ frames := map[string][]any{
+ wire.FrameConnection: {
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderTensorX, State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptAPIKey}},
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderTensorX, State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptModel, Models: []string{"m"}}},
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderTensorX, State: wire.ConnError, Reason: "key_refused"},
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderClaude, State: wire.ConnError, Reason: "no_priced_model"},
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderTensorX, State: wire.ConnHealthy, LocalID: "tensorx-1"},
+ wire.ConnectionReport{Connection: "cn", Provider: wire.ProviderClaude, State: wire.ConnConnecting, Prompt: &wire.ConnectPrompt{Kind: wire.PromptToken}},
+ },
+ wire.FrameAnswer: {
+ wire.AnswerReport{QuestionID: "q", Attempt: 1, State: wire.QuestionQueued, Reason: wire.HoldBudget},
+ wire.AnswerReport{QuestionID: "q", Attempt: 1, State: wire.QuestionFailed, Reason: wire.HoldSpendUnknown},
+ wire.AnswerReport{QuestionID: "q", Attempt: 1, State: wire.QuestionAnswered, Usage: []wire.Usage{row}},
+ },
+ wire.FrameUsage: {wire.UsageReport{ProbeID: "pr_1", Rows: []wire.Usage{row}}},
+ wire.FrameHello: {wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{{LocalID: "tensorx-1", Provider: wire.ProviderTensorX, Kind: wire.KindAPIKey, State: wire.ConnHealthy, Billing: wire.BillingMetered}, {LocalID: "claude-a", Provider: "claude", Kind: wire.KindSubscription, State: wire.ConnHealthy}}}},
+ }
+ sent := 0
+ for kind, bodies := range frames {
+ for _, b := range bodies {
+ f, _ := wire.NewFrame("f", kind, b)
+ old, err := wire.Encode(f, wire.ProtocolPreviewHost)
+ if errors.Is(err, wire.ErrProtocol) {
+ continue
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ sent++
+ switch kind {
+ case wire.FrameConnection:
+ var v pre12Report
+ if err := wire.Strict(old.Data, &v); err != nil || !pre12Providers[v.Provider] || !pre12ConnectReasons[v.Reason] || v.Prompt != nil && !pre12PromptKinds[v.Prompt.Kind] {
+ t.Fatalf("a pre-12 instance cannot read %s: %v", old.Data, err)
+ }
+ case wire.FrameAnswer:
+ var v struct {
+ TaskID string `json:"task_id"`
+ QuestionID string `json:"question_id"`
+ Attempt int64 `json:"attempt"`
+ State string `json:"state"`
+ Reason string `json:"reason"`
+ Text string `json:"text"`
+ Timing json.RawMessage `json:"timing"`
+ Usage []pre12Usage `json:"usage"`
+ }
+ if err := wire.Strict(old.Data, &v); err != nil || !pre12AnswerStates[v.State] || !pre12AnswerReasons[v.Reason] {
+ t.Fatalf("a pre-12 instance cannot read %s: %v", old.Data, err)
+ }
+ case wire.FrameUsage:
+ var v pre12UsageReport
+ if err := wire.Strict(old.Data, &v); err != nil || v.CheckID != "pr_1" {
+ t.Fatalf("a pre-12 instance cannot read %s: %v", old.Data, err)
+ }
+ case wire.FrameHello:
+ var v struct {
+ Connections []pre12Held `json:"connections"`
+ }
+ var all map[string]json.RawMessage
+ json.Unmarshal(old.Data, &all)
+ if err := wire.Strict(all["connections"], &v.Connections); err != nil {
+ t.Fatalf("a pre-12 instance cannot read %s: %v", old.Data, err)
+ }
+ for _, h := range v.Connections {
+ if !pre12Providers[h.Provider] || !pre12Kinds[h.Kind] {
+ t.Fatalf("a pre-12 hello names %s %s", h.Provider, h.Kind)
+ }
+ }
+ }
+ }
+ }
+ if sent != 4 {
+ t.Fatalf("%d reports sent below 12, want the token prompt, the answer, the usage and the hello", sent)
+ }
+}