Settings › Privacy's masked words (脱敏词) were matched as written:
Nightjar masked "Nightjar" and let NIGHTJAR and nightjar go to the
vendor. A new row under them, "Masked words in any case", makes each word
match in any capitals (Unicode simple folding, so МОСКВА matches Москва
too), with no rule to write. It is off by default and for every existing
user: settings.json without redactWordsAnyCase reads false, and a page
from before it that saves without the field keeps what was set
(api.go, as noAutoDownload). Each match goes as {{TERM_…}} and comes
back as it was written in the request. Translated in zh, zh-TW, ja, de.
redact.Options.WordsAnyCase; settings.RedactWordsAnyCase
(redactWordsAnyCase) reaches it through internal/masking; prefsKeep
sends it on every Settings save.
Tests, each red when its part is broken by hand:
- TestWordsAnyCase (internal/redact): with the any-case branch skipped,
FAIL "any case: \"NIGHTJAR\" still in …" and NIGHTJAR left in a request.
- TestCountTokensRedacted/words_any_case (internal/gateway): with
masking.go not passing the setting, FAIL "system, message and tool
description must be masked: …NIGHTJAR…"; words_as_written holds that
the default leaves NIGHTJAR alone.
- TestSettingsRedactWordsAnyCaseKept (internal/gui): with the
not-sent guard removed, FAIL "after {…no field…}: any case false".
- redact-words-any-case.test.cjs: 20 of 20 (Chromium and WebKit, en,
zh, zh-TW, ja, de, 1100 and 640px). With the old app.js it times out
waiting for #redactWordsAnyCaseRow; with only prefsKeep's field
removed, WebKit fails "a later save keeps it".
Not tried: a real vendor request with the switch on; the gateway test checks the body that goes out.
Rebased over b00c82acf (#1977's Warn/Block and Never mask list):
masking.go and settings.go keep its Actions and Allow beside
WordsAnyCase; redact, settings and provider ran ok after it.
Run for this batch of seven commits (devin, routing zh, qoder, masked words
any case, Long conversations, Claude subagents, user-select), in a worktree,
sandbox HOME, never against live configs:
- At e352cc823 (tree ea497c9f5, on origin/main 7dd965ed2): go test
-timeout 45m ./... with pipefail, tee'd: 68 packages ok, exit 0, no
ENOSPC. gofmt -l clean; go vet ./... for darwin, GOOS=linux -tags nogui
and GOOS=windows: clean.
- The whole GUI suite at e352cc823 (node --test --test-concurrency=3
internal/gui/tests/*.test.cjs, Chromium and WebKit, every language):
11725 tests, 11679 pass, 46 fail, none ENOSPC. Each red file run alone at
both 7dd965ed2 and e352cc823 gives the same counts on both, so none is
from this batch: usage-agents-hide 5/16 (#2276), usage-ledger 28/34
(#3104), usage-fold, quota-count-wrap, quota-name-translated (#2399),
usage-checkin 26/28 (#2549), plugin-github 14/18 (#3218), s3-sync 3/4
(#2448), modal-morph (#3179), fonts (filed as #3227, 1 in 9 alone on
main). tray-usages (#2453), plugin-card-update (#3079) and
proto-menu-overflow (filed as #3229) were red only under the full run
and pass alone on both.
- Rebased onto origin/main 4a87c4476 (only Zed's mover min changed), tree
8944b5dbb: go test ./internal/provider ./internal/gui (TestCoreScriptBudget
and the app.js budget) ./internal/redact ./internal/settings
./internal/agent: all five ok; gofmt -l clean.
109 lines
3.4 KiB
Go
109 lines
3.4 KiB
Go
// Package masking is Settings → Privacy's masking as the gateway's masker
|
|
// (gwmw.Masker): secrets, personal data and the user's own words go to a
|
|
// vendor as placeholders (internal/redact), and come back as they were.
|
|
// It reads the settings on each call. plugins/redact serves it.
|
|
package masking
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/yetone/magpie/internal/gwmw"
|
|
"github.com/yetone/magpie/internal/pii"
|
|
"github.com/yetone/magpie/internal/redact"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// Masker masks as the settings say, read on each call.
|
|
type Masker struct{}
|
|
|
|
// Wanted says settings st ask for anything to be masked.
|
|
func Wanted(st settings.Settings) bool {
|
|
return st.Redact || st.RedactPersonal || len(st.RedactWords) > 0
|
|
}
|
|
|
|
func options() redact.Options {
|
|
st := settings.Shared()
|
|
o := redact.Options{Secrets: st.Redact, Personal: st.RedactPersonal, Kinds: st.RedactKinds, Words: st.RedactWords, WordsAnyCase: st.RedactWordsAnyCase, Rules: st.RedactRules,
|
|
Actions: st.RedactActions, Allow: st.RedactAllow}
|
|
if st.RedactModel {
|
|
o.Model = model
|
|
}
|
|
return o
|
|
}
|
|
|
|
// model is the local model (internal/pii) as redact's Model. One that
|
|
// can't answer panics, and the request is held back (gwmw.Safely) rather
|
|
// than sent with only the rules' masking: the user asked for the model's.
|
|
func model(texts []string) [][]redact.Found {
|
|
got, err := pii.Find(texts)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func (Masker) JSON(body []byte) ([]byte, int) {
|
|
o := options()
|
|
if !o.Secrets && !o.Personal && len(o.Words) == 0 {
|
|
return body, 0
|
|
}
|
|
return redact.MaskJSON(body, o)
|
|
}
|
|
|
|
func (Masker) Text(s string) (string, int) { return redact.Mask(s, options()) }
|
|
|
|
// JSONHits is JSON, with what it masked (gwmw.Teller).
|
|
func (Masker) JSONHits(body []byte) ([]byte, []gwmw.Hit) {
|
|
o := options()
|
|
if !o.Secrets && !o.Personal && len(o.Words) == 0 {
|
|
return body, nil
|
|
}
|
|
var hits []gwmw.Hit
|
|
o.Seen = told(&hits)
|
|
out, _ := redact.MaskJSON(body, o)
|
|
return out, hits
|
|
}
|
|
|
|
// TextHits is Text, with what it masked.
|
|
func (Masker) TextHits(s string) (string, []gwmw.Hit) {
|
|
o := options()
|
|
var hits []gwmw.Hit
|
|
o.Seen = told(&hits)
|
|
out, _ := redact.Mask(s, o)
|
|
return out, hits
|
|
}
|
|
|
|
func (Masker) Value(p string) (string, bool) { return redact.Value(p) }
|
|
|
|
// told adds each value found to hits, with its hint: the value itself
|
|
// stays in redact's memory, or, sent as written, nowhere.
|
|
func told(hits *[]gwmw.Hit) func(redact.Hit) {
|
|
return func(h redact.Hit) {
|
|
*hits = append(*hits, gwmw.Hit{Kind: h.Kind, By: h.By, Placeholder: h.Placeholder, Action: h.Action, Head: h.Head, Len: h.Len})
|
|
}
|
|
}
|
|
|
|
// Preview is s as the settings would send it, and what was found in it,
|
|
// with nothing remembered: no placeholder is kept for the replies, and
|
|
// none of its values is masked again later as one seen before. With
|
|
// masking off it is as masking would be turned on (Off says so), so the
|
|
// user can try it first.
|
|
func Preview(s string) (out string, hits []gwmw.Hit, off bool) {
|
|
o := options()
|
|
if !o.Secrets && !o.Personal && len(o.Words) == 0 {
|
|
o.Secrets, o.Personal, off = true, true, true
|
|
}
|
|
o.Seen = told(&hits)
|
|
out = redact.Preview(s, o)
|
|
return out, hits, off
|
|
}
|
|
|
|
func (Masker) Restore(s string, escaped bool) string { return redact.Restore(s, escaped) }
|
|
|
|
func (Masker) Writer(w http.ResponseWriter) (http.ResponseWriter, func()) {
|
|
rw := redact.NewWriter(w)
|
|
return rw, rw.Finish
|
|
}
|
|
|
|
var _ gwmw.Teller = Masker{}
|