Files
magpie/internal/redact/hits_test.go
T
yetone f21dd99f5a privacy: every masking is recorded, and Settings › Privacy shows it
What was masked is now kept, so the user can see it. Settings › Privacy
opens on a summary card: how many values were kept from vendors over 1,
7 or 30 days, by kind, by finder (rules, your words, the local model,
known values) and by where they went, with a day strip. The page is
grouped into What to mask, How it's found (rules and the local model are
layers, not a toggle: the model adds names and addresses rules can't
see) and Sharing. A records view lists each masked request: its kinds,
finders, placeholders and, while magpie runs, the line around each
value. Gateway › Recent calls and the Routing log carry a masked-N badge
that opens the record.

Originals never go to disk. internal/maskrec writes day files under
~/.config/magpie/masking (0600, 30 days, 100MB at most) holding kinds,
finders, placeholders, a first character or key prefix and a length.
The lines around values are kept in memory for the last 400 records
only, and /api/masking/value answers only while the masker remembers a
placeholder. Nothing is written under MAGPIE_TEST_SANDBOX.

A masker says what it masked through gwmw.Teller (JSONHits, TextHits,
Value); a plugin masker that doesn't is counted only, so older redact
plugins keep working. Every route that masks notes it: chat, Responses,
Codex backend and search, embeddings, images, video, transcription,
decide and the search MCP query. Masking still fails closed: a masker
that panics on any path refuses the request (TestMaskingOnAndOff's fake
now panics on JSONHits and TextHits too, since the gateway asks a Teller
through those).

app.js grows to 1,314,432 bytes, so its budget rises to 1,335,000 and
the core's to 1,528,800 (+1.5%): Settings is the core's own page.

Run at this head (rebased on origin/main a3ebed202):
- go vet clean on macOS, GOOS=linux -tags nogui and GOOS=windows;
  go test ./internal/gateway ok (561s).
- On origin/main c922225f1 (one commit back, the packages either side
  touches): agent, gateway, gui, gwmw, maskrec, provider, redact ok.
- On origin/main 592899467: go test ./... -timeout 45m, 65 packages ok;
  agent and gateway hit the 45m timeout under load ~26 (each mid-way
  through a short test, not a hang) and pass alone with -timeout 100m
  (1133s, 890s).
- GUI, on 592899467, the 42 files this change or main's new commits reach (privacy-*,
  redact-*, masking-records, settings-*, gateway-*, routing-bodies,
  routing-request-metrics, routing-log-icons, account-discovery,
  click-scroll, i18n-keys, old-webkit), both engines, every language:
  475 tests, 475 pass. routing-bodies is green here since 2543fb2c1.

Run on the previous base, origin/main c84864ffc (the whole suites):
- go test ./... -timeout 45m: every package ok (66), exit 0. An earlier
  run found TestMaskingOnAndOff/panics (its fake, above) and
  TestCoreScriptBudget (the budget, above); both fixed.
- gofmt -l clean; go vet on macOS, GOOS=linux -tags nogui and
  GOOS=windows clean; node --check on the changed scripts.
- GUI suite, both engines, every language, --test-concurrency=6:
  11533 tests, 11483 pass, 50 fail. Each failing file rerun alone here
  and on origin/main:
  - red the same on main, filed: codex-predictions (#2624),
    usage-checkin (#2549), usage-agents-hide (#2276), usage-fold
    (#2163), s3-sync (#2448), quota-count-wrap and
    quota-name-translated (#2228), routing-request-metrics (#2571),
    routing-bodies (#2559: red at its "second request pressed" step;
    this change's badge checks before and after it pass),
    routing-rule-remove (#3096, filed now: 0 2 1 alone here, 0 0 2 on
    c84864ffc).
  - load, pass alone: group-member-weight (#2387), routing-model-rest,
    context-tune (Chromium didn't launch in 180s at load average ~45;
    0 0 0 alone on both). An earlier whole run's library-chip-fan red is
    #3072 (filed), plugin-card-update #3079, lightweight #2638,
    segs-glide #2683.
  - mine, fixed: old-webkit, three ":hover, :focus-visible" lists
    Safari before 15.4 drops; split, 6/6.
- Broken by hand, each went red: masked values kept on disk
  (TestNoValueOnDisk), a snip that runs past an escaped quote
  (TestSnipStaysInItsString), the shared-line dedup in the records view
  (masking-records, 10 of 12).
- Real app in a sandbox HOME with the local model on: the summary card,
  flow, chips and records view in light and dark; persons and
  addresses, Chinese names among them, recorded by=model; a grep of the
  masking folder and the sandbox config for the masked values found
  none.
2026-10-21 06:12:37 +08:00

81 lines
2.9 KiB
Go

package redact
import (
"strings"
"testing"
)
// Each value masked is told to Seen with the finder that found it: the
// user's word, the user's rule, magpie's rules, the model (with the
// Chinese names it brings), and, in a later request, a value masked
// before. The hit holds the placeholder, never the value.
func TestSeenSaysWhoFoundIt(t *testing.T) {
const word, mine, key, name, zh = "Bluefin", "ACME-7731-QXZ9", "sk-proj-Hq3vX9tLm2Wc8RzK4nYp", "Rina Kowalczyk", "张伟"
f := &fakeModel{values: map[string]string{name: "private_person"}}
s := "Project " + word + " ref " + mine + " key " + key + " ask " + name + ",并联系" + zh + "老师。"
var hits []Hit
o := Options{Secrets: true, Personal: true, Words: []string{word},
Rules: []Rule{{Kind: "ticket", Prefix: "ACME-"}}, Model: f.find,
Seen: func(h Hit) { hits = append(hits, h) }}
out, n := Mask(s, o)
if n != len(hits) {
t.Fatalf("masked %d, told of %d: %v", n, len(hits), hits)
}
by := map[string]string{}
for _, h := range hits {
v, ok := Value(h.Placeholder)
if !ok || !strings.Contains(out, h.Placeholder) {
t.Fatalf("hit %v: no value or not in %q", h, out)
}
if strings.Contains(h.Placeholder, v) {
t.Fatalf("hit %v holds its value", h)
}
by[v] = h.By
}
want := map[string]string{word: ByWord, mine: ByYours, key: ByRule, name: ByModel, zh: ByModel}
for v, b := range want {
if by[v] != b {
t.Errorf("%q found by %q, want %q (all: %v)", v, by[v], b, by)
}
}
// a later request without the model: the name it found before is
// masked as a value known before
hits = nil
if out, _ := Mask("Thanks, "+name+"!", Options{Personal: true, Seen: o.Seen}); strings.Contains(out, name) || len(hits) != 1 || hits[0].By != ByKnown {
t.Errorf("a value known before: %q %v", out, hits)
}
}
// What JSON bodies mask is told the same way, once per place it is.
func TestSeenInJSON(t *testing.T) {
const key = "sk-proj-Hq3vX9tLm2Wc8RzK4nYp"
var hits []Hit
body := `{"messages":[{"role":"user","content":"key ` + key + `"},{"role":"user","content":"again ` + key + `"}]}`
_, n := MaskJSON([]byte(body), Options{Secrets: true, Seen: func(h Hit) { hits = append(hits, h) }})
if n != 2 || len(hits) != 2 || hits[0].Placeholder != hits[1].Placeholder || hits[0].By != ByRule {
t.Fatalf("n=%d hits=%v", n, hits)
}
}
// A hint keeps a key's prefix and a name's first character, and how long
// each is, nothing more.
func TestHint(t *testing.T) {
for _, c := range []struct {
kind, v, head string
n int
}{
{"API_KEY", "sk-proj-Hq3vX9tLm2Wc8RzK4nYp", "sk-", 28},
{"API_KEY", "ghp_abcdefghijklmnopqrstu", "ghp_", 25},
{"PERSON", "张伟", "张", 2},
{"PERSON", "Rina Kowalczyk", "R", 14},
{"EMAIL", "rina@example.com", "r", 16},
{"TERM", "Bluefin-codename-x", "B", 18},
{"PHONE", "1", "", 1},
} {
if head, n := Hint(c.kind, c.v); head != c.head || n != c.n {
t.Errorf("Hint(%s, %q) = %q, %d; want %q, %d", c.kind, c.v, head, n, c.head, c.n)
}
}
}