security: redact the launch key from everything Pi says
Astra's S2 round, finding 5. A step can read its key (spec §2.1), so it can echo it: the runner reads the launch's key once and takes it, as itself and as JSON text, out of every delta (as a stream, so a key split across deltas is caught), the step log, the tail, activity subjects, provider errors, unknown event names and the arguments Pi gives Equipo's tools (a verdict, a question to the person).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+189
−15
internal/fakepi/fake.go+9 −0internal/runner/provider/pi/redact.go+75 −0internal/runner/provider/pi/redact_test.go+28 −0internal/runner/thin/pistep.go+47 −15internal/runner/thin/pistep_test.go+30 −0
diff --git a/internal/fakepi/fake.go b/internal/fakepi/fake.go| index 34543c58..0c1e30d1 100644 |
| --- a/internal/fakepi/fake.go |
| +++ b/internal/fakepi/fake.go |
| @@ -364,6 +364,15 @@ func (f *fake) run(prompt string) { |
| default: |
| reply = "ok " + strings.Join(notes, " ") |
| } |
| + if strings.Contains(prompt, "echokey") { |
| + // What a step that reads its environment could say: the key, in |
| + // the reply (split across deltas) and in the verdict it records. |
| + key := os.Getenv("TENSORX_API_KEY") |
| + reply = "the key is " + key + " and that is all" |
| + if strings.HasPrefix(prompt, "[Equipo: review]") { |
| + tool("mcp__equipo__submit_review", map[string]any{"verdict": "approve", "headline": "Key " + key, "summary": "I read " + key, "findings": []map[string]any{}}) |
| + } |
| + } |
| if m := floodWord.FindStringSubmatch(prompt); m != nil { |
| n, _ := strconv.Atoi(m[1]) |
| reply = strings.Repeat("f", n) |
diff --git a/internal/runner/provider/pi/redact.go b/internal/runner/provider/pi/redact.go| new file mode 100644 |
| index 00000000..b1949f5e |
| --- /dev/null |
| +++ b/internal/runner/provider/pi/redact.go |
| @@ -0,0 +1,75 @@ |
| +package pi |
| + |
| +import ( |
| + "encoding/json" |
| + "strings" |
| +) |
| + |
| +// Redacted stands where a launch's key was. |
| +const Redacted = "[redacted]" |
| + |
| +// Redactor takes a launch's key out of what the harness says (a step can |
| +// read the key, connectors spec §2.1, so it can echo it): every string |
| +// the runner logs or reports from Pi passes through it. Stream handles |
| +// text that arrives in pieces, holding back an end that could be the |
| +// start of the key. |
| +type Redactor struct { |
| + forms []string |
| + pending string |
| +} |
| + |
| +// NewRedactor redacts key, as itself and as it appears inside JSON. |
| +func NewRedactor(key string) *Redactor { |
| + r := &Redactor{} |
| + if key == "" { |
| + return r |
| + } |
| + r.forms = []string{key} |
| + if raw, err := json.Marshal(key); err == nil { |
| + if quoted := string(raw[1 : len(raw)-1]); quoted != key { |
| + r.forms = append(r.forms, quoted) |
| + } |
| + } |
| + return r |
| +} |
| + |
| +// Redact replaces every whole occurrence. |
| +func (r *Redactor) Redact(s string) string { |
| + for _, f := range r.forms { |
| + s = strings.ReplaceAll(s, f, Redacted) |
| + } |
| + return s |
| +} |
| + |
| +// RedactJSON redacts a JSON document's text (the key inside a string |
| +// value keeps it valid JSON). |
| +func (r *Redactor) RedactJSON(raw json.RawMessage) json.RawMessage { |
| + return json.RawMessage(r.Redact(string(raw))) |
| +} |
| + |
| +// Stream takes the next piece of a text and returns what can be shown |
| +// now: everything but an end that could still become the key. |
| +func (r *Redactor) Stream(piece string) string { |
| + if len(r.forms) == 0 { |
| + return piece |
| + } |
| + s := r.Redact(r.pending + piece) |
| + hold := 0 |
| + for _, f := range r.forms { |
| + for k := min(len(f)-1, len(s)); k > hold; k-- { |
| + if strings.HasSuffix(s, f[:k]) { |
| + hold = k |
| + break |
| + } |
| + } |
| + } |
| + r.pending = s[len(s)-hold:] |
| + return s[:len(s)-hold] |
| +} |
| + |
| +// Flush returns what Stream held back, once the text has ended. |
| +func (r *Redactor) Flush() string { |
| + s := r.pending |
| + r.pending = "" |
| + return s |
| +} |
diff --git a/internal/runner/provider/pi/redact_test.go b/internal/runner/provider/pi/redact_test.go| new file mode 100644 |
| index 00000000..57816ca9 |
| --- /dev/null |
| +++ b/internal/runner/provider/pi/redact_test.go |
| @@ -0,0 +1,28 @@ |
| +package pi |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +// The key is redacted wherever it falls across the pieces of a stream, |
| +// and nothing else is held back for good. |
| +func TestRedactorStream(t *testing.T) { |
| + key := "tx-abcdefghijklmnop" |
| + text := "before " + key + " middle " + key + key + " after tx-a" |
| + for cut := 1; cut < 12; cut++ { |
| + r := NewRedactor(key) |
| + var out strings.Builder |
| + for i := 0; i < len(text); i += cut { |
| + out.WriteString(r.Stream(text[i:min(i+cut, len(text))])) |
| + } |
| + out.WriteString(r.Flush()) |
| + if strings.Contains(out.String(), key) || out.String() != strings.ReplaceAll(text, key, Redacted) { |
| + t.Fatalf("pieces of %d: %q", cut, out.String()) |
| + } |
| + } |
| + quoted := NewRedactor(`tx-"q"`) |
| + if got := quoted.Redact(`{"summary":"tx-\"q\" here"}`); strings.Contains(got, `tx-\"q\"`) { |
| + t.Fatalf("the JSON form: %s", got) |
| + } |
| +} |
diff --git a/internal/runner/thin/pistep.go b/internal/runner/thin/pistep.go| index e91a739e..e5ec70d7 100644 |
| --- a/internal/runner/thin/pistep.go |
| +++ b/internal/runner/thin/pistep.go |
| @@ -142,7 +142,22 @@ func (e *Engineer) piAttempt(ctx context.Context, r stepRow, tree workspace.Task |
| return stepResult{reason: "provider_failed", terminated: true}, notStarted |
| } |
| defer os.RemoveAll(scratch) |
| - tool, err := openToolSet(context.Background(), o.Runner.StateDir, toolSet{list: spec.tools, names: spec.names}, spec.call) |
| + // A step can read the key (spec §2.1) and so echo it: everything Pi |
| + // says is redacted of it before it is logged, reported or handed to |
| + // Equipo's tools. |
| + key, err := provider.ReadToken(b.CredentialHome) |
| + if err != nil { |
| + return stepResult{reason: "account_unavailable", terminated: true}, notStarted |
| + } |
| + red := pi.NewRedactor(string(key)) |
| + clear(key) |
| + call := spec.call |
| + if call != nil { |
| + call = func(ctx context.Context, name, id string, args json.RawMessage) (string, bool) { |
| + return spec.call(ctx, name, id, red.RedactJSON(args)) |
| + } |
| + } |
| + tool, err := openToolSet(context.Background(), o.Runner.StateDir, toolSet{list: spec.tools, names: spec.names}, call) |
| if err != nil { |
| return stepResult{reason: "provider_failed", terminated: true}, notStarted |
| } |
| @@ -214,12 +229,16 @@ func (e *Engineer) piAttempt(ctx context.Context, r stepRow, tree workspace.Task |
| if !spec.noLog { |
| logFile = e.openLog(r) |
| } |
| - res = e.piRun(ctx, launcher, sess, limit, spec, logFile) |
| + res = e.piRun(ctx, launcher, sess, limit, spec, logFile, red) |
| logFile.Close() |
| // Events Equipo does not know were dropped: their type names and |
| // how many, nothing else of them. |
| if unknown := launcher.Unknown(sess); len(unknown) > 0 && e.O.Logger != nil { |
| - e.O.Logger.Info("pi unknown events", "types", unknown) |
| + named := map[string]int{} |
| + for name, n := range unknown { |
| + named[red.Redact(name)] += n |
| + } |
| + e.O.Logger.Info("pi unknown events", "types", named) |
| } |
| } |
| res.session = session |
| @@ -248,7 +267,7 @@ func (e *Engineer) piAttempt(ctx context.Context, r stepRow, tree workspace.Task |
| // piRun sends the prompt and reads Pi's events to the run's end. Every |
| // question Equipo's extension asks is refused: in a step nobody can |
| // answer, and the namespace is the containment (connectors spec §3). |
| -func (e *Engineer) piRun(ctx context.Context, l *pi.Launcher, sess provider.Session, limit time.Duration, spec specialistRun, logFile io.Writer) stepResult { |
| +func (e *Engineer) piRun(ctx context.Context, l *pi.Launcher, sess provider.Session, limit time.Duration, spec specialistRun, logFile io.Writer, red *pi.Redactor) stepResult { |
| tail := &stepTail{} |
| tally := &pi.Tally{} |
| tctx, cancel := context.WithTimeout(ctx, limit) |
| @@ -291,16 +310,33 @@ func (e *Engineer) piRun(ctx context.Context, l *pi.Launcher, sess provider.Sess |
| if !ok { |
| return finish(stepResult{reason: "provider_failed", summary: text.String()}) |
| } |
| - if line, err := json.Marshal(map[string]any{"kind": ev.Kind, "payload": ev.Payload}); err == nil && written+len(line) < stepLogLimit { |
| - n, _ := logFile.Write(append(line, '\n')) |
| + payload := ev.Payload |
| + var d pi.Delta |
| + if ev.Kind == pi.KindDelta { |
| + // A key can fall across deltas: the stream holds back an |
| + // end that could still become it. |
| + _ = json.Unmarshal(ev.Payload, &d) |
| + d.Text = red.Stream(d.Text) |
| + payload, _ = json.Marshal(d) |
| + } |
| + if line, err := json.Marshal(map[string]any{"kind": ev.Kind, "payload": payload}); err == nil && written+len(line) < stepLogLimit { |
| + n, _ := logFile.Write(append([]byte(red.Redact(string(line))), '\n')) |
| written += n |
| } |
| tally.Add(ev) |
| + if ev.Kind == pi.KindDone { |
| + d.Text = red.Flush() |
| + } |
| + if d.Text != "" && text.Len() <= piTextLimit { |
| + text.WriteString(d.Text) |
| + if s := spec.stream; s != nil && s.delta != nil { |
| + s.delta(s.offset, d.Text) |
| + s.offset += int64(len(d.Text)) |
| + } |
| + } |
| switch ev.Kind { |
| case pi.KindDelta: |
| - var d pi.Delta |
| - _ = json.Unmarshal(ev.Payload, &d) |
| - if text.Len()+len(d.Text) > piTextLimit { |
| + if text.Len() > piTextLimit { |
| tail.add("The reply was too long") |
| go func() { |
| ictx, stop := context.WithTimeout(context.Background(), 2*time.Second) |
| @@ -309,21 +345,17 @@ func (e *Engineer) piRun(ctx context.Context, l *pi.Launcher, sess provider.Sess |
| }() |
| return finish(stepResult{reason: "provider_failed"}) |
| } |
| - text.WriteString(d.Text) |
| if s := spec.stream; s != nil { |
| s.mark(&s.timing.FirstEventMs) |
| s.mark(&s.timing.FirstTextMs) |
| s.timing.LastTextMs = max(time.Since(s.stdin).Milliseconds(), 1) |
| - if s.delta != nil { |
| - s.delta(s.offset, d.Text) |
| - } |
| - s.offset += int64(len(d.Text)) |
| } |
| case pi.KindToolCall: |
| // The final answer is what follows the last tool. |
| text.Reset() |
| var c pi.ToolCall |
| _ = json.Unmarshal(ev.Payload, &c) |
| + c.Args = red.RedactJSON(c.Args) |
| kind, subject, line := piActivity(c) |
| if line != "" { |
| tail.add(line) |
| @@ -353,7 +385,7 @@ func (e *Engineer) piRun(ctx context.Context, l *pi.Launcher, sess provider.Sess |
| return finish(piCancelled(ctx)) |
| } |
| if d.Error != "" { |
| - tail.add(d.Error) |
| + tail.add(red.Redact(d.Error)) |
| } |
| switch d.Failure { |
| case pi.FailAuth: |
diff --git a/internal/runner/thin/pistep_test.go b/internal/runner/thin/pistep_test.go| index b500ebe0..95d78d4b 100644 |
| --- a/internal/runner/thin/pistep_test.go |
| +++ b/internal/runner/thin/pistep_test.go |
| @@ -496,3 +496,33 @@ func TestPiOversizedReply(t *testing.T) { |
| t.Fatalf("report %s %s summary %d, spend unknown %v", rep.State, rep.Reason, len(rep.Summary), r.conn().SpendUnknown) |
| } |
| } |
| + |
| +// A step can read the key (connectors spec §2.1), so whatever Pi says is |
| +// redacted before the runner logs or reports it: the reply (the key split |
| +// across two deltas), the verdict it records through Equipo's tool, the |
| +// tail and the step's log. |
| +func TestPiRedactsTheKey(t *testing.T) { |
| + r := newPiRig(t, fakePiInstall(t), bigBudget) |
| + leave(t) |
| + var logs strings.Builder |
| + r.e.O.Logger = slog.New(slog.NewTextHandler(&logs, nil)) |
| + rep := r.runReview("echokey", 1) |
| + if rep.State != wire.StepDone { |
| + t.Fatalf("report %+v", rep) |
| + } |
| + raw, _ := json.Marshal(rep) |
| + steps, _ := os.ReadDir(r.e.root()) |
| + var stepLog []byte |
| + for _, d := range steps { |
| + b, _ := os.ReadFile(filepath.Join(r.e.root(), d.Name(), "log.jsonl")) |
| + stepLog = append(stepLog, b...) |
| + } |
| + for what, text := range map[string]string{"the report": string(raw), "the step log": string(stepLog), "the runner log": logs.String()} { |
| + if strings.Contains(text, fakepi.TestKey) { |
| + t.Errorf("%s holds the key", what) |
| + } |
| + } |
| + if !strings.Contains(string(stepLog), "[redacted]") || !strings.Contains(rep.Summary+rep.Headline, "[redacted]") { |
| + t.Fatalf("nothing was redacted: summary %q headline %q", rep.Summary, rep.Headline) |
| + } |
| +} |