Files
magpie/internal/redact/redact_test.go
T
ggbdpq 9a05a7bcec redact: the mask covers ark- keys, _KEY/_PASS names, auth headers, and JSON field names (#2946)
What changed (one per gap the reporter reproduced on main):

- API_KEY gains the ark- value shape (#2946): Volcano Ark and the relays
  in front of it issue keys with an ark- prefix, and no value rule knew
  one. ARK_API_KEY=<ark key> was masked already (its name has api_key in
  it); ARK_KEY=<ark key>, a key in prose and one in a request body went
  as they were.
- A new SECRET rule takes an upper-case env var whose name only ends in
  KEY or PASS: SERVICE_X_KEY=…, FOO_KEY=…, DB_PASS=… — quoted or not.
  Upper case is the boundary: in the lower case a name that ends in key
  is code's (a sort_key:, a cache_key:, a public key =), so nothing of
  TestMaskGatewayKey's rows moves; notAPlainKey turns the upper-case
  SORT_KEY/PUBLIC_KEY/PROMPT_CACHE_KEY/CACHE_KEY away too.
- A new SECRET rule takes the value of an Authorization: or Cookie:
  header line, whatever it looks like — the opaque bearer tokens and
  session values the shape rules had no eye for (#2946).
- MaskJSON masks the value of a field named for a secret by its name,
  before the ids and names that would keep it: {"api_key": "…"} with a
  value no rule knows, {"db_pass": "…"}, an AKIA under access_key_id
  (its _id kept it from the value's own AKIA rule), {"Authorization":
  "Bearer …"} and {"Cookie": "…"} (#2946). signature,
  encrypted_content and data: values still go as written (#1911), and
  the tools' definitions are untouched. SecretName now says a name
  ending in pass is a secret's name too, so the archive (ScrubJSON) and
  MaskJSON judge a field by the same names.

Why: the reporter's five examples each leak a credential whose value has
no key's shape; the rules judged values by shape alone and field names
only ever kept a value. The boundary the reporter warned about —
sort_key, cache_key, prompt_cache_key, public_key must stay unmasked —
is held by case (lower-case names never enter the new rules) and by
notAPlainKey for the upper-case ones.

Run (rebased onto 9ae79122, upstream/main 10-27): the 18 upstream redact commits since
9b0f4861 (714ab8e8 CJK password rule .. 25341b4c address narrowing) add CJK-named
secret rules, made-up key forms and narrow emails and addresses — none covers
the five families here, so the rules take them side by side; the shared
secretNames rule carries upstream's new notCode boundary (511c4a76). The
_KEY/_PASS rule's boundary is notPlainKeyOrCode (plain key names and code
naming a secret both stay), the same resolution 2ea09ec3 on
codex/verify-2946-redact carried and the issue thread endorsed:
Refs #2946

Review: the ark- key, _KEY/_PASS assignment and Authorization/Cookie
rules land as written here. Two parts were narrowed in this batch's
redact follow-up ("a secret's field name masks only in a tool's
arguments"): a secret's JSON field name now masks only where the user's
data sits (a tool call's arguments and a tool's result, with secrets
on), never across the whole body by name, and an Authorization or Cookie
line masks only when what follows has a credential's shape. None of the
redact commits on main since 10-27 covered these five families.

Landing record (yetone):
Run at the pushed batch head (tree a6b084d0b, on origin/main 616405061),
go1.26.8:
- go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway,
internal/redact, internal/provider and internal/plugin (this batch's
packages and those main changed since the full run): all ok. gofmt -l
clean; go vet ./... (darwin), GOOS=linux and GOOS=windows go vet -tags
nogui ./... clean. internal/gui on macOS (mote-run):
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners
(#3949), both red at main too.
- The full go test ./... -timeout 45m -count=1 ran one rebase earlier,
at tree 66a7a4c17 on origin/main 3a3bdb9f2 (main then gained 5bfa9f076's
group stand-in, b19d08593, f856d381a and 616405061). It ran on Linux
(wsl-run): 82 packages ok. internal/agent has 4 reds, all red at
origin/main on that box as well: TestInstalledVersion,
TestGrokInstalled, TestOmpDetectedWithPiDir and
TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, #3548). The root
package and internal/cli need gtk4 to build, so they ran on macOS
(mote-run): ok. TestColdTurnsTaken (#4510) was red there once. An
earlier full run at base d307aa648 was red once in
TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone at both
the head and main.
- Each landed patch's diff matches the head ggbdpq posted last on its
issue (patch-id).
- No GUI file is touched, so no GUI test file was run.
Broken by hand: gua's ablations, in the thread. The narrowing is broken
by hand in that follow-up.
Not tried: a real vendor request carrying these values through the
gateway; the rules were tested in internal/redact only.

Review: the ark- key, _KEY/_PASS assignment and Authorization/Cookie
rules land as written here. Two parts were narrowed in this batch's
redact follow-up ("a secret's field name masks only in a tool's
arguments"): a secret's JSON field name now masks only where the user's
data sits (a tool call's arguments and a tool's result, with secrets
on), never across the whole body by name, and an Authorization or Cookie
line masks only when what follows has a credential's shape. None of the
redact commits on main since 10-27 covered these five families.

Landing record (yetone):
Run at tree 7d7955eb9 (this batch on origin/main 3cfba2e37), go1.26.8:
- go test -count=1 -timeout 45m on Linux (wsl-run) of internal/gateway,
internal/redact, internal/provider, internal/agent and internal/usage
(this batch's packages, plus those main changed since the full run): all
ok except internal/agent's TestInstalledVersion, TestGrokInstalled and
TestOmpDetectedWithPiDir, red at origin/main on that box too (#4033,
#3548). gofmt -l clean; go vet ./... (darwin), GOOS=linux and
GOOS=windows go vet -tags nogui ./... clean. On macOS (mote-run), the
root package and internal/cli ok; internal/gui red in
TestSettingsSaveKeepsWhatItDoesNotSend (#4514), TestMainWindowCorners
(#3949) and TestColdTurnsTaken (#4510), the first two red at main too.
- The full go test ./... -timeout 45m -count=1 ran two rebases earlier,
at tree 66a7a4c17 on origin/main 3a3bdb9f2, on Linux (wsl-run): 82
packages ok, internal/agent's 4 reds as above, plus
TestCopilotFolderFromAnIDEPluginIsNotTheCLI (#4033, red at main). The
root package, internal/cli and internal/gui need gtk4 to build, so they
ran on macOS as above. An earlier full run at base d307aa648 was red
once in TestUnroutedHeldReportSaved (#4300), which passes 5 of 5 alone
at both the head and main.
- Each landed patch's diff matches the head ggbdpq posted last on its
issue (patch-id).
- No GUI file is touched, so no GUI test file was run.
- Rebased after these runs onto origin/main c1457f2ee. Its two commits
since 3cfba2e37 (3f2faef62 gui, c1457f2ee agent) touch neither
internal/gateway nor internal/redact, nor any file this batch changes,
and were not run again here.
Broken by hand: gua's ablations, in the thread. The narrowing is broken
by hand in that follow-up.
Not tried: a real vendor request carrying these values through the
gateway; the rules were tested in internal/redact only.

Generated-by: GLM-5.3-Flash (ZCode)
2026-11-01 06:38:00 +08:00

307 lines
14 KiB
Go

package redact
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/claudeseal"
)
const (
openaiKey = "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
ghToken = "ghp_0123456789abcdefghijABCDEFGHIJ0123"
)
// unmarked is s without its placeholders. A placeholder's suffix is a keyed
// hash, so with a random key it can spell a short value the test checks is
// gone: "ops" in {{HOSTNAME_mlopsiy3}} turned TestMoreKinds red on macOS
// CI at 6c099edd.
func unmarked(s string) string { return placeholderRe.ReplaceAllString(s, "") }
func TestMask(t *testing.T) {
cases := []struct {
in string
o Options
gone []string // not in what is sent
kept []string // still there
count int
}{
{in: "export OPENAI_API_KEY=" + openaiKey, gone: []string{openaiKey}, kept: []string{"export OPENAI_API_KEY="}, count: 1},
{in: "token " + ghToken + " and AKIAIOSFODNN7EXAMPLE", gone: []string{ghToken, "AKIAIOSFODNN7EXAMPLE"}, count: 2},
{in: "postgres://app:s3cretPass@db.internal:5432/app", gone: []string{"s3cretPass"}, kept: []string{"postgres://app:", "@db.internal"}, count: 1},
{in: "postgres://app:${DB_PASS}@db/app", kept: []string{"${DB_PASS}"}},
{in: `DB_PASSWORD="hunter2hunter2x9"`, gone: []string{"hunter2hunter2x9"}, count: 1},
{in: "password = getPassword()", kept: []string{"getPassword()"}},
{in: "the tokenizer splits words", kept: []string{"tokenizer"}},
{in: "-----BEGIN RSA PRIVATE KEY-----\nMIIEow\n-----END RSA PRIVATE KEY-----", gone: []string{"MIIEow"}, count: 1},
{in: "mail me: jane.doe@acme.io", kept: []string{"jane.doe@acme.io"}}, // personal off
{in: "mail me: jane.doe@acme.io or 13812345678", o: Options{Secrets: true, Personal: true}, gone: []string{"jane.doe@acme.io", "13812345678"}, count: 2},
{in: "see user@example.com", o: Options{Secrets: true, Personal: true}, kept: []string{"user@example.com"}},
{in: "id 11010519491231002X card 4111 1111 1111 1111", o: Options{Secrets: true, Personal: true}, gone: []string{"11010519491231002X", "4111 1111 1111 1111"}, count: 2},
{in: "order 4111111111111112", o: Options{Secrets: true, Personal: true}, kept: []string{"4111111111111112"}}, // fails Luhn
{in: "Project Nightjar ships", o: Options{Secrets: true, Words: []string{"Nightjar"}}, gone: []string{"Nightjar"}, count: 1},
}
for _, c := range cases {
if !c.o.Personal && c.o.Words == nil {
c.o.Secrets = true
}
out, n := Mask(c.in, c.o)
if n != c.count {
t.Errorf("%q: %d masked, want %d: %q", c.in, n, c.count, out)
}
for _, g := range c.gone {
if strings.Contains(unmarked(out), g) {
t.Errorf("%q: %q still in %q", c.in, g, out)
}
}
for _, k := range c.kept {
if !strings.Contains(out, k) {
t.Errorf("%q: %q gone from %q", c.in, k, out)
}
}
if back := Restore(out, false); back != c.in {
t.Errorf("round trip: %q → %q → %q", c.in, out, back)
}
}
// the same value, the same placeholder
a, _ := Mask("k="+openaiKey, Options{Secrets: true})
b, _ := Mask("other "+openaiKey, Options{Secrets: true})
if pa, pb := placeholderRe.FindString(a), placeholderRe.FindString(b); pa == "" || pa != pb {
t.Fatalf("placeholders differ: %q %q", a, b)
}
}
func TestMaskJSON(t *testing.T) {
body := `{"model":"sk-proj-notmasked-because-model-key","messages":[{"role":"user","content":[` +
`{"type":"text","text":"key: ` + openaiKey + `\nok"},` +
`{"type":"thinking","thinking":"hm","signature":"` + openaiKey + `"}]}],"n":1,"stream":true}`
out, n := MaskJSON([]byte(body), Options{Secrets: true})
if n != 1 || strings.Count(string(out), openaiKey) != 1 {
t.Fatalf("%d: %s", n, out)
}
if !json.Valid(out) || !strings.Contains(string(out), `"model":"sk-proj-notmasked-because-model-key"`) || !strings.Contains(string(out), `\nok"`) {
t.Fatalf("body: %s", out)
}
if back := RestoreJSON(out); string(back) != body {
t.Fatalf("restored:\n%s\n%s", back, body)
}
// untouched bodies come back byte for byte
plain := `{ "a" : [1, 2.5e3, true, null, "xé"] }`
if out, n := MaskJSON([]byte(plain), Options{Secrets: true}); n != 0 || string(out) != plain {
t.Fatalf("plain: %s", out)
}
}
// what the model writes back with a placeholder in it, streamed as vendors do
func TestWriterStream(t *testing.T) {
masked, _ := Mask(openaiKey, Options{Secrets: true})
pw := masked
secret := openaiKey
// Anthropic text split inside the placeholder, and a tool call's JSON
tool := `{"cmd":"echo ` + pw + `"}`
var events []string
textEvent := func(s string) string {
b, _ := json.Marshal(map[string]any{"type": "content_block_delta", "index": 0, "delta": map[string]any{"type": "text_delta", "text": s}})
return "event: content_block_delta\ndata: " + string(b) + "\n\n"
}
jsonEvent := func(s string) string {
b, _ := json.Marshal(map[string]any{"type": "content_block_delta", "index": 1, "delta": map[string]any{"type": "input_json_delta", "partial_json": s}})
return "event: content_block_delta\ndata: " + string(b) + "\n\n"
}
events = append(events, textEvent("use "+pw[:5]), textEvent(pw[5:12]), textEvent(pw[12:]+" now {"),
"event: content_block_stop\ndata: {\"type\":\"content_block_stop\",\"index\":0}\n\n",
jsonEvent(tool[:14]), jsonEvent(tool[14:]), "data: [DONE]\n\n")
rec := httptest.NewRecorder()
rec.Header().Set("Content-Type", "text/event-stream")
w := NewWriter(rec)
w.WriteHeader(200)
all := strings.Join(events, "")
// in awkward pieces
for i := 0; i < len(all); i += 7 {
w.Write([]byte(all[i:min(i+7, len(all))]))
}
w.Finish()
var text, js strings.Builder
for _, ev := range strings.Split(rec.Body.String(), "\n\n") {
_, d, ok := strings.Cut(ev, "data: ")
if !ok || d == "[DONE]" {
continue
}
var e struct {
Delta struct {
Text string
PartialJSON string `json:"partial_json"`
}
}
if err := json.Unmarshal([]byte(d), &e); err != nil {
t.Fatalf("event %q: %v", d, err)
}
text.WriteString(e.Delta.Text)
js.WriteString(e.Delta.PartialJSON)
}
if text.String() != "use "+secret+" now {" {
t.Fatalf("text: %q", text.String())
}
var args map[string]string
if err := json.Unmarshal([]byte(js.String()), &args); err != nil || args["cmd"] != "echo "+secret {
t.Fatalf("args %q: %v", js.String(), err)
}
if strings.Count(rec.Body.String(), "\n\n") != len(events) {
t.Fatalf("events changed:\n%s", rec.Body.String())
}
}
func TestWriterWhole(t *testing.T) {
masked, _ := Mask(ghToken, Options{Secrets: true})
b, _ := json.Marshal(map[string]any{"output": []any{
map[string]any{"type": "function_call", "arguments": `{"t":"` + masked + `"}`},
map[string]any{"type": "message", "content": []any{map[string]any{"type": "output_text", "text": "got " + masked}}},
}})
rec := httptest.NewRecorder()
rec.Header().Set("Content-Type", "application/json")
rec.Header().Set("Content-Length", "999")
w := NewWriter(rec)
w.WriteHeader(http.StatusOK)
w.Write(b)
w.Finish()
if rec.Header().Get("Content-Length") != "" || strings.Contains(rec.Body.String(), "{{") ||
!strings.Contains(rec.Body.String(), "got "+ghToken) || !json.Valid(rec.Body.Bytes()) {
t.Fatalf("%v %s", rec.Header(), rec.Body.String())
}
}
// What a vendor sealed goes back as the vendor wrote it: the values masked
// elsewhere in the request as their placeholders, its own words as they
// are; the same text unsealed is masked as any other.
func TestMaskJSONSignedGoesBackAsWritten(t *testing.T) {
o := Options{Secrets: true, Personal: true}
const email, key = "jeremy.zhou@gmail.com", "sk-proj-abcdEFGH1234ijklMNOP5678qrst"
pe, _ := Mask(email, o)
pk, _ := Mask(key, o)
// what the agent has back of what the vendor wrote: the values in it
// in other words around them, and the vendor's own number
agent := "Order 13800138000 is for " + email + ". Its key is the one ending " + key + "."
vendor := "Order 13800138000 is for " + pe + ". Its key is the one ending " + pk + "."
user := `{"role":"user","content":"I am ` + email + `, key ` + key + `"}`
for _, c := range []struct{ name, body, want string }{
{"anthropic", `{"messages":[` + user + `,{"role":"assistant","content":[{"type":"thinking","thinking":"` + agent + `","signature":"Eqk1"}]}]}`, vendor},
{"gemini", `{"contents":[{"role":"user","parts":[{"text":"I am ` + email + `, key ` + key + `"}]},{"role":"model","parts":[{"text":"` + agent + `","thought":true,"thoughtSignature":"Cs4B"}]}]}`, vendor},
{"gemini call", `{"contents":[{"role":"user","parts":[{"text":"I am ` + email + `, key ` + key + `"}]},{"role":"model","parts":[{"functionCall":{"name":"note","args":{"text":"` + agent + `"}},"thoughtSignature":"Cs4B"}]}]}`, vendor},
{"responses", `{"input":[` + user + `,{"type":"reasoning","summary":[{"type":"summary_text","text":"` + agent + `"}],"encrypted_content":"gAAAA"}]}`, vendor},
} {
out, _ := MaskJSON([]byte(c.body), o)
if !strings.Contains(string(out), `"`+c.want+`"`) {
t.Errorf("%s: sealed text not as the vendor wrote it:\n%s", c.name, out)
}
if strings.Contains(string(out), email) || strings.Contains(string(out), key) {
t.Errorf("%s: a value went out: %s", c.name, out)
}
}
// no signature, nothing to match: masked by the rules
out, _ := MaskJSON([]byte(`{"messages":[{"role":"assistant","content":[{"type":"thinking","thinking":"call 13800138000 now","signature":""}]}]}`), o)
if strings.Contains(string(out), "13800138000") {
t.Errorf("unsigned thinking left unmasked: %s", out)
}
}
// Claude's block magpie gave a Responses client to carry back
// (claudeseal) goes back byte for byte: Claude's own text in it, with the
// placeholders Claude was sent, is what its signature is checked against.
func TestMaskJSONKeepsClaudeBlocks(t *testing.T) {
o := Options{Secrets: true, Personal: true}
const email = "jeremy.zhou@gmail.com"
pe, _ := Mask(email, o)
sealed := claudeseal.Seal([]byte(`{"type":"thinking","thinking":"Write to ` + pe + `, or call 13800138000.","signature":"Eqk1"}`))
body := `{"input":[{"role":"user","content":"I am ` + email + `"},{"type":"reasoning","summary":[{"type":"summary_text","text":"Write to ` + email + `"}],"encrypted_content":"` + sealed + `"}]}`
out, _ := MaskJSON([]byte(body), o)
if !strings.Contains(string(out), `"`+sealed+`"`) || strings.Contains(string(out), email) {
t.Errorf("Claude's block not as it was sent, or a value went out:\n%s", out)
}
}
// A field named for a secret holds one, whatever its value looks like
// (#2946): the rules judge a value by its shape, so {"api_key": "…"} with a
// value no rule knows went out as it was, and access_key_id's _id kept even
// an AKIA from the value's own rule. A name that only ends in key is no
// secret: a sort key, a cache key, a public key stay, and so do a
// signature and a data value the vendor reads.
func TestMaskJSONNamedSecrets(t *testing.T) {
o := Options{Secrets: true}
for _, c := range []struct {
key, value string
gone bool
}{
{"api_key", "Qm27wVb9nLx4tRk8Ps3d", true},
{"client_secret", "randomvalue42x", true},
{"db_pass", "Hr7jTm2qwXz94", true},
{"access_key_id", "AKIAIOSFODNN7EXAMPLE", true},
{"Authorization", "Bearer zq83Mw2vNx7kRt5", true},
{"Cookie", "sid=Yn41Pc8wLd2mQs6", true},
{"apiKey", "plainrandomvalue1", true},
{"sort_key", "created_at_2024", false},
{"cache_key", "lru_items_01", false},
{"prompt_cache_key", "conv_12345678", false},
{"public_key", "04a1b2c3d4e5f6a7", false},
{"signature", "sigofvendor42", false},
{"data", "rawbytes12345", false},
} {
body := `{"` + c.key + `":"` + c.value + `"}`
out, n := MaskJSON([]byte(body), o)
if !c.gone {
if n != 0 || string(out) != body {
t.Errorf("%s: %d masked: %s", c.key, n, out)
continue
}
} else {
if strings.Contains(string(out), c.value) || !strings.Contains(string(out), "{{SECRET_") {
t.Errorf("%s: not masked as a secret: %s", c.key, out)
continue
}
}
if back := RestoreJSON(out); string(back) != body {
t.Errorf("%s: restored %s", c.key, back)
}
}
}
// An id goes as the vendor gave it, on both sides of what it matches.
func TestMaskJSONKeepsIDs(t *testing.T) {
o := Options{Secrets: true, Personal: true}
const id = "call_-6090648166611204696" // 19 digits that pass Luhn
for _, k := range []string{"id", "tool_call_id", "tool_use_id", "call_id", "approval_request_id", "toolCallId"} {
body := `{"` + k + `":"` + id + `"}`
if out, _ := MaskJSON([]byte(body), o); string(out) != body {
t.Errorf("%s masked: %s", k, out)
}
}
if out, _ := MaskJSON([]byte(`{"content":"card `+id[6:]+`"}`), o); strings.Contains(string(out), id[6:]) {
t.Errorf("the same digits in text should be masked: %s", out)
}
}
// A tool call's arguments are the agent's: a key there named like a
// signature or an id doesn't keep its value, or its neighbours', from the
// rules.
func TestMaskJSONToolArgsMasked(t *testing.T) {
o := Options{Secrets: true, Personal: true}
for _, c := range []struct{ name, body string }{
{"signature key", `{"messages":[{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"send_email","input":{"signature":"Best, Bob","body":"DB_PASSWORD=hunter2abc1 call 13800138000"}}]}]}`},
{"id keys", `{"messages":[{"role":"assistant","content":[{"type":"tool_use","id":"toolu_1","name":"lookup","input":{"user_id":"DB_PASSWORD=hunter2abc1","phone_id":"13800138000"}}]}]}`},
{"gemini args", `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"lookup","args":{"thoughtSignature":"x","customer_phone":"13800138000","note":"DB_PASSWORD=hunter2abc1"}}}]}]}`},
{"text block", `{"messages":[{"role":"user","content":[{"type":"text","text":"DB_PASSWORD=hunter2abc1 call 13800138000","signature":"x"}]}]}`},
} {
out, _ := MaskJSON([]byte(c.body), o)
if strings.Contains(string(out), "hunter2abc1") || strings.Contains(string(out), "13800138000") {
t.Errorf("%s: a value went out: %s", c.name, out)
}
if !strings.Contains(string(out), `"toolu_1"`) && strings.Contains(c.body, "toolu_1") {
t.Errorf("%s: the call's id changed: %s", c.name, out)
}
}
}