Files
magpie/internal/settings/masking_test.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

45 lines
1.5 KiB
Go

package settings
import (
"slices"
"strings"
"testing"
)
// Only warn and block are kept, for a kind masking names; mask, the
// default, is left out, and anything else is refused (#1977).
func TestRedactActions(t *testing.T) {
got, err := redactActions(map[string]string{"API_KEY": "Block", " EMAIL ": "warn", "PHONE": "mask", "SSN": ""})
if err != nil || len(got) != 2 || got["API_KEY"] != "block" || got["EMAIL"] != "warn" {
t.Fatalf("%v %v", got, err)
}
if got, err := redactActions(map[string]string{"PHONE": "mask"}); err != nil || got != nil {
t.Fatalf("all masked: %v %v", got, err)
}
if _, err := redactActions(map[string]string{"API_KEY": "drop"}); err == nil {
t.Fatal("an unknown action was kept")
}
if _, err := redactActions(map[string]string{"api key": "warn"}); err == nil {
t.Fatal("a kind masking can't name was kept")
}
}
// The allow list is trimmed, each value once whatever its case, and
// bounded.
func TestRedactAllow(t *testing.T) {
got, err := redactAllow([]string{" ops@acme.io ", "OPS@acme.io", "", "build-01"})
if err != nil || !slices.Equal(got, []string{"ops@acme.io", "build-01"}) {
t.Fatalf("%q %v", got, err)
}
if _, err := redactAllow([]string{strings.Repeat("x", maxAllowLen+1)}); err == nil {
t.Fatal("a value too long was kept")
}
many := make([]string, maxAllow+1)
for i := range many {
many[i] = strings.Repeat("v", i+1)
}
if _, err := redactAllow(many); err == nil {
t.Fatal("too many values were kept")
}
}