Files
magpie/internal/redact/nextfield_test.go
T
yetone 8d4d8b2d5f redact: a field set to nothing takes nothing from the next one (mx_albert)
mx_albert on Discord (#feedback): with Secrets masking on, this command
line went to the vendor with 127.0.0.1 replaced:

  env -i PATH HOME UI_USER= UI_PASSWORD= UI_ADDR=127.0.0.1:39117 DATA_DIR=/tmp/x ./syncer serve

The SECRET rule (a name with password/secret/token/api_key, then = or :)
lets a space stand after the separator, for `password = hunter2` in a
config. UI_PASSWORD is set to nothing, so the rule took the next word,
UI_ADDR=127.0.0.1, as its value and masked it as {{SECRET_…}}:39117;
once masked it was a known value and went masked wherever it came again.
The other field rules (*_KEY / *_PASS, key, 密码, 密钥, "token 是") had the
same shape.

Now a value that is an assignment of its own after a space (UI_ADDR=,
addr=, --listen=; an = with something other than = after it, so a
base64 value's padding is never one) is the next field: the match is
dropped and the search goes on from that value, which is read with its
own name (fieldMatches, nextField). UI_ADDR's address goes as written;
UI_PASSWORD= UI_TOKEN=t0k3nValue2 still masks the token, as UI_TOKEN's.

Old data: what was masked before is known only in memory, so after the
update's restart 127.0.0.1 in that line goes as written for every user;
a conversation carrying it sends it as written from then on (one prompt
cache miss).

Tests: TestEmptyFieldTakesNotTheNext (the reporter's literal line, plus
0.0.0.0, [::1], localhost:port, logfmt, --flag=, and the CJK and
"token 是" rules, every category on) and TestSecretBesideEmptyFieldMasks
(hunter2pass9, a token after the empty field, a *_KEY, a base64 value,
password = with spaces).

Run at this head on mote: go test ./internal/redact ./internal/masking
./internal/otlp ./internal/pii ./internal/sessions ./internal/settings
./plugins/redact all ok; ./internal/gateway and ./internal/gui (nogui)
-run 'Redact|Mask|Privacy|Secret' ok, gateway
TestLoopGuardLeavesOrdinaryRepliesAlone ok (it reads the doc page);
GOOS=linux and GOOS=windows go vet ./internal/redact ok; gofmt -l clean.

Broken by hand: findAll with the field check turned off. Both new tests
go red: the reporter's line masks {{SECRET_…}}:39117, and 17 of the
other cases fail (UI_TOKEN= after an empty field masked with its name,
UI_ADDR=127.0.0.1 then masked as known in the next case).

Not tried: the full gateway and gui packages (another session's suite
was on mote), the GUI, a real request through a running gateway.
UI_PASSWORD=hunter2 itself is not masked by a rule, before or after
this: the rule asks 8 characters of a value, hunter2 has 7.
2026-11-03 02:22:05 +08:00

66 lines
2.7 KiB
Go

package redact
import (
"strings"
"testing"
)
// everything is every category of masking on, the ones off by default
// (the IP address, the user and host) too.
func everything() Options {
o := Options{Secrets: true, Personal: true, Kinds: map[string]bool{}}
for _, c := range Categories {
o.Kinds[c.ID] = true
}
return o
}
// A field set to nothing takes nothing from the next one: mx_albert on
// Discord, the Secrets category masked 127.0.0.1 in this command line, as
// the SECRET rule took UI_ADDR=127.0.0.1 for UI_PASSWORD's value. An
// address, a port, a path or a flag after an empty field goes as written,
// on any category.
func TestEmptyFieldTakesNotTheNext(t *testing.T) {
for _, in := range []string{
"env -i PATH HOME UI_USER= UI_PASSWORD= UI_ADDR=127.0.0.1:39117 DATA_DIR=/tmp/x ./syncer serve",
"UI_PASSWORD= UI_ADDR=0.0.0.0:39117 ./syncer serve",
"UI_TOKEN= LISTEN=[::1]:39117 ./syncer",
"API_KEY= ADDR=localhost:39117 ./syncer",
"DB_PASS= DB_HOST=127.0.0.1 DB_PORT=5432 psql",
"SERVICE_KEY=\tBIND_ADDR=127.0.0.1:8080",
"level=info password= addr=127.0.0.1:39117 data_dir=/tmp/x1",
"client_secret: listen_addr=127.0.0.1:8080",
"./syncer --password= --listen=127.0.0.1:39117",
"secret= --data-dir=/var/lib/syncer2",
"key= UPSTREAM_URL2=http://127.0.0.1:39117/v1",
"密码= UI_ADDR=127.0.0.1:39117",
"密钥: UI_ADDR=127.0.0.1:39117 ./syncer",
"token 是 UI_ADDR=127.0.0.1:39117",
} {
if out, n := Mask(in, everything()); n != 0 {
t.Errorf("%q: %d masked: %q", in, n, out)
}
}
}
// A secret still masks beside an empty field: its own, after its own
// name, and a value after a space that is no assignment (password =
// hunter2pass9, a base64 value with its padding).
func TestSecretBesideEmptyFieldMasks(t *testing.T) {
for _, c := range []struct{ in, secret, kept string }{
{"env -i UI_USER= UI_PASSWORD=hunter2pass9 UI_ADDR=127.0.0.1:39117 ./syncer serve", "hunter2pass9", "UI_ADDR=127.0.0.1:39117"},
{"UI_PASSWORD= UI_TOKEN=t0k3nValue2 UI_ADDR=127.0.0.1:39117", "t0k3nValue2", "UI_ADDR=127.0.0.1:39117"},
{"UI_PASSWORD= SERVICE_KEY=AbC123dEf456GhI789 ./syncer", "AbC123dEf456GhI789", "SERVICE_KEY="},
{"password= api_key=AbC123dEf456GhI789", "AbC123dEf456GhI789", "api_key="},
{"password = hunter2pass9", "hunter2pass9", "password = "},
{"password: dGVzdDEyMzQ1Ng==", "dGVzdDEyMzQ1Ng==", "password: "},
{"UI_PASSWORD= hunter2pass9", "hunter2pass9", "UI_PASSWORD= "},
{"密码= UI_TOKEN=t0k3nValue2", "t0k3nValue2", "UI_TOKEN="},
} {
out, n := Mask(c.in, everything())
if n == 0 || strings.Contains(out, c.secret) || !strings.Contains(out, c.kept) {
t.Errorf("%q: %d masked: %q, want %q masked and %q kept", c.in, n, out, c.secret, c.kept)
}
}
}