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/maina3ebed202): - go vet clean on macOS, GOOS=linux -tags nogui and GOOS=windows; go test ./internal/gateway ok (561s). - On origin/mainc922225f1(one commit back, the packages either side touches): agent, gateway, gui, gwmw, maskrec, provider, redact ok. - On origin/main592899467: 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, on592899467, 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 since2543fb2c1. Run on the previous base, origin/mainc84864ffc(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 onc84864ffc). - 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.
141 lines
4.2 KiB
Go
141 lines
4.2 KiB
Go
package redact
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// Rule is one the user added (Settings → Privacy) for a secret magpie's own
|
|
// rules don't know, a gateway's key say (#195): what its placeholders are
|
|
// called, and the prefix its values start with or a pattern for them. Its
|
|
// values are masked with the secrets, as they are: the same placeholder
|
|
// for the same value, put back in what the vendor answers.
|
|
type Rule struct {
|
|
Kind string `json:"kind"`
|
|
Prefix string `json:"prefix,omitempty"`
|
|
// Regex is an RE2 pattern; the value is its group 1 when it has one,
|
|
// else all of what it matches, as with magpie's own rules.
|
|
Regex string `json:"regex,omitempty"`
|
|
}
|
|
|
|
// How many rules, and how long each part of one, a user may have.
|
|
const (
|
|
MaxRules = 32
|
|
maxKind = 24
|
|
maxPrefix = 64
|
|
minPrefix = 3
|
|
maxRegex = 300
|
|
prefixTail = `[A-Za-z0-9_\-]{8,}`
|
|
customKind = "CUSTOM"
|
|
minCustomLen = 4
|
|
)
|
|
|
|
// RuleKind is kind as a placeholder can have it: upper case letters, digits
|
|
// and _, starting with a letter, at most 24 long; "" is CUSTOM.
|
|
func RuleKind(kind string) string {
|
|
var b strings.Builder
|
|
for _, r := range strings.ToUpper(strings.TrimSpace(kind)) {
|
|
switch {
|
|
case r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_':
|
|
b.WriteRune(r)
|
|
case r == ' ' || r == '-' || r == '.':
|
|
b.WriteByte('_')
|
|
}
|
|
}
|
|
k := strings.Trim(b.String(), "_")
|
|
if k == "" {
|
|
return customKind
|
|
}
|
|
if k[0] < 'A' || k[0] > 'Z' {
|
|
k = customKind + "_" + k
|
|
}
|
|
if len(k) > maxKind {
|
|
k = strings.TrimRight(k[:maxKind], "_")
|
|
}
|
|
return k
|
|
}
|
|
|
|
// CheckRules tidies the user's rules (the kind as a placeholder has it,
|
|
// spaces trimmed, the empty and the repeated dropped) and says what is
|
|
// wrong with one that can't be used: a pattern that doesn't compile, or
|
|
// matches nothing at all, a prefix too short, too many or too long.
|
|
func CheckRules(in []Rule) ([]Rule, error) {
|
|
var out []Rule
|
|
for _, r := range in {
|
|
r.Prefix, r.Regex = strings.TrimSpace(r.Prefix), strings.TrimSpace(r.Regex)
|
|
if r.Prefix == "" && r.Regex == "" {
|
|
continue
|
|
}
|
|
r.Kind = RuleKind(r.Kind)
|
|
if r.Prefix != "" && r.Regex != "" {
|
|
return nil, fmt.Errorf("a masking rule has a prefix or a regular expression, not both: %s", r.Kind)
|
|
}
|
|
if r.Prefix != "" && (len(r.Prefix) < minPrefix || len(r.Prefix) > maxPrefix) {
|
|
return nil, fmt.Errorf("a masking rule's prefix is %d to %d characters long, not %q", minPrefix, maxPrefix, r.Prefix)
|
|
}
|
|
if len(r.Regex) > maxRegex {
|
|
return nil, fmt.Errorf("a masking rule's regular expression is at most %d characters long", maxRegex)
|
|
}
|
|
re, err := r.compile()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("the masking rule %s has a regular expression that doesn't work: %v", r.Kind, err)
|
|
}
|
|
if re.MatchString("") {
|
|
return nil, fmt.Errorf("the masking rule %s matches nothing at all, so it would mask everywhere: %s", r.Kind, r.Regex)
|
|
}
|
|
dup := false
|
|
for _, o := range out {
|
|
dup = dup || o == r
|
|
}
|
|
if !dup {
|
|
out = append(out, r)
|
|
}
|
|
}
|
|
if len(out) > MaxRules {
|
|
return nil, fmt.Errorf("at most %d masking rules, not %d", MaxRules, len(out))
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (r Rule) compile() (*regexp.Regexp, error) {
|
|
if r.Prefix != "" {
|
|
return regexp.Compile(regexp.QuoteMeta(r.Prefix) + prefixTail)
|
|
}
|
|
return regexp.Compile(r.Regex)
|
|
}
|
|
|
|
// compiled holds each rule's pattern once it is made, nil for one that
|
|
// doesn't compile (a settings file written by hand): Mask runs on every
|
|
// string of every request.
|
|
var compiled sync.Map // Rule → *regexp.Regexp
|
|
|
|
// customRules are the user's rules as Mask runs them, the ones that work.
|
|
func customRules(in []Rule) []rule {
|
|
var out []rule
|
|
for _, r := range in {
|
|
if strings.TrimSpace(r.Prefix) == "" && strings.TrimSpace(r.Regex) == "" || len(r.Regex) > maxRegex {
|
|
continue
|
|
}
|
|
v, ok := compiled.Load(r)
|
|
if !ok {
|
|
re, err := r.compile()
|
|
if err != nil || re.MatchString("") {
|
|
re = nil
|
|
}
|
|
v, _ = compiled.LoadOrStore(r, re)
|
|
}
|
|
re := v.(*regexp.Regexp)
|
|
if re == nil {
|
|
continue
|
|
}
|
|
c := rule{kind: RuleKind(r.Kind), yours: true, re: re, ok: func(v string) bool { return len(v) >= minCustomLen }}
|
|
if r.Prefix != "" {
|
|
c.markers = []string{r.Prefix}
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out
|
|
}
|