Files
magpie/internal/settings/masking.go
T
yetone b00c82acfa masking: each kind is masked, sent with a warning or held back; a Never mask list and Try it (#1977)
Settings › Privacy gets two sections under What to mask:

- When a value is found: Mask, Warn or Block per kind (redactActions;
  a kind left out is masked). Warn sends the value as written and records
  it with a placeholder nobody remembers, not counted as masked. Block
  masks it as before and the gateway answers 400 naming the kind and the
  value's hint, never the value, with nothing sent (maskBlocked); the
  record is kept as Blocked. The secret kinds come first, the personal
  ones behind Show all kinds. A Never mask list (redactAllow) lets a whole
  value go as written in any case; a longer value it is part of is masked.
- Try it: POST /api/masking/preview masks a text as a request would
  (redact.Preview) and remembers nothing: no placeholder stands for it
  afterwards and its values aren't masked later as seen before. The page
  shows the vendor's text, what was found and what would be done, and
  keeps the text in no storage.

The masking card counts requests held back and values sent as written; a
call whose values only went as written gets an amber badge on Recent calls
and the Routing log. zh, zh-TW, ja and de throughout. Docs: gateway-routing
and gui-shell.

Broken by hand: the new GUI badge assertion fails on the old maskedBadge
(no .warn badge drawn); TestSettingsSaveKeepsWhatItDoesNotSend went red
until the two settings were in its fixture; "{n} held back" turned
i18n-keys red against the routing plugin's "{n} held" and was renamed.

Run at this tree (base origin/main 9b0f48612):
- gofmt clean; go vet on macOS, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 -timeout 45m redact, maskrec, settings, gwmw, gui,
  gateway: ok.
- GUI, both engines: every Privacy file, mask-actions, masking-records,
  routing-bodies, i18n-keys, old-webkit: 263/263.
- Whole GUI suite earlier at 083b1c8e7's base, beside 7 other runs:
  53 red; alone, the ones left red fail the same on origin/main
  6fc532198 and are filed (#3179, #2228, #2448, #2276, #2549, #2163,
  #2399), except privacy-usage-stats and account-discovery, which this
  change's taller page moved off screen; both fixed here.

Not tried: a real vendor request held back by Block from a real agent.
2026-10-22 04:07:55 +08:00

61 lines
1.5 KiB
Go

package settings
import (
"fmt"
"strings"
"github.com/yetone/magpie/internal/redact"
)
// How many values the masking allowlist may hold, and how long each.
const (
maxAllow = 200
maxAllowLen = 256
)
// redactActions is m with only the actions redact knows, each for a kind
// named as a placeholder names one (API_KEY, a rule's own); masking, the
// default, is left out.
func redactActions(m map[string]string) (map[string]string, error) {
out := map[string]string{}
for k, a := range m {
k, a = strings.TrimSpace(k), strings.ToLower(strings.TrimSpace(a))
switch a {
case "", "mask":
continue
case redact.Warn, redact.Block:
default:
return nil, fmt.Errorf("what is done with a kind of value found must be mask, %s or %s, not %q", redact.Warn, redact.Block, a)
}
if !redact.KindName(k) {
return nil, fmt.Errorf("%q isn't a kind of value masking finds", k)
}
out[k] = a
}
if len(out) == 0 {
return nil, nil
}
return out, nil
}
// redactAllow is the allowlist trimmed, each value once whatever its case.
func redactAllow(vs []string) ([]string, error) {
var out []string
seen := map[string]bool{}
for _, v := range vs {
v = strings.TrimSpace(v)
if v == "" || seen[strings.ToLower(v)] {
continue
}
if len(v) > maxAllowLen {
return nil, fmt.Errorf("a value the masking lets go is at most %d characters", maxAllowLen)
}
seen[strings.ToLower(v)] = true
out = append(out, v)
}
if len(out) > maxAllow {
return nil, fmt.Errorf("the masking lets at most %d values go", maxAllow)
}
return out, nil
}