The owner found UUIDs masked, and a backend looks an id up by every
character: a masked one, or half of one, finds nothing or the wrong row.
A probe with every kind on and the model labelling each value found five
ways a UUID went masked:
- a piece of one: PHONE took 13800138000 at the end of
…-a13800138000, BANK_CARD took 4111-1111-1111-9007 across a UUID's
groups, and the id went half a placeholder;
- the SECRET rule in a field named for an id, a cursor or a request:
secret_id, token_id, api_key_id, access_key_id, next_page_token,
nextPageToken, csrf_token, trace_token, secret_hash;
- the bare "key" rule ("key": "<uuid>", Idempotency-Key);
- the serial rule (sn:, Serial Number:);
- the model labelling a UUID a name, an account and so on (only
"secret" was held back before), and a phone number masked earlier
being found again inside a UUID.
noIDs (internal/redact/ids.go) drops, after every finder, a span that is
a piece of an id, and a whole id unless the user's own words or rules
found it, a PASSWORD/API_KEY/TOKEN/PRIVATE_KEY rule did, the SECRET rule
did in a field named for a credential (password=, api_key=, X-Api-Key,
refresh_token, session_token, token), or it was masked before in full.
32 hex digits without hyphens the model calls a secret in prose ("the api
key is 9f8e…") stay masked; a UUID it calls one does not, as before.
Run in ~/workspace/projects/magpie-wt/mask-act at this tree:
- TestIDsGoAsWritten: 7 id shapes × 22 contexts kept, 7 credential
contexts masked, the model's every label kept, a known phone not found
inside an id, a phone and a card number of their own masked, the user's
word masked. With noIDs taken out of mask(): 108 failures; restored: ok.
- The first full redact run caught two of my own mistakes, now fixed:
idAround didn't check the run's inside, so a 33-character IPv6 address
counted as an id (TestMoreKinds), and the model's 32-hex API key went
unmasked (TestModelSecretIsNoID).
- go test ./internal/redact ./internal/maskrec ./internal/gwmw
./internal/settings ./internal/gateway (-timeout 45m, -count=1): all ok (gateway 439s)
- gofmt -l: none; go vet ./internal/redact for darwin, linux, windows: ok.
Not tried: a real request through a running gateway with the local model
on; the model's labels are simulated with fakeModel.
95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package redact
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// An id goes as written wherever it is and whatever is around it: a UUID,
|
|
// one whose last group holds a phone number's digits, one whose groups read
|
|
// as a card number, without hyphens, in capitals, a commit's hash. It is
|
|
// masked whole only in a field named for a credential.
|
|
func TestIDsGoAsWritten(t *testing.T) {
|
|
forgetIDs := func() {
|
|
mu.Lock()
|
|
values = map[string]string{}
|
|
reindexKnown()
|
|
mu.Unlock()
|
|
}
|
|
ids := []string{
|
|
"3f2b8c1e-9a4d-4e7b-b2c1-7d6e5f4a3b21",
|
|
"3F2B8C1E-9A4D-4E7B-B2C1-7D6E5F4A3B21",
|
|
"3f2b8c1e-9a4d-4e7b-b2c1-a13800138000", // PHONE in its last group
|
|
"abcd4111-1111-1111-9007-abcdef123456", // BANK_CARD across its groups
|
|
"3f2b8c1e9a4d4e7bb2c1a13800138000", // no hyphens
|
|
"9f8e7d6cabcd4111111111111111900713800138000a", // a long hash with both
|
|
"12345678-1234-1234-1234-123456789012",
|
|
}
|
|
kinds := map[string]bool{}
|
|
for _, c := range Categories {
|
|
kinds[c.ID] = true
|
|
}
|
|
o := Options{Secrets: true, Personal: true, Kinds: kinds}
|
|
asWritten := []string{
|
|
"%s", "id %s", `{"id":"%s"}`, "request_id: %s", "/users/%s/profile", "urn:uuid:%s", "{%s}",
|
|
"next_page_token: %s", `"nextPageToken": "%s"`, `\"csrf_token\": \"%s\"`, `"token_id": "%s"`, "secret_id: %s",
|
|
`"api_key_id": "%s"`, "access_key_id = %s", `"key": "%s"`, "Idempotency-Key: %s", "X-Request-Id: %s",
|
|
"trace_token=%s", "secret_hash: %s", "sn: %s", "Serial Number: %s", "tel: %s",
|
|
}
|
|
for _, id := range ids {
|
|
for _, c := range asWritten {
|
|
forgetIDs()
|
|
in := strings.ReplaceAll(c, "%s", id)
|
|
if out, n := Mask(in, o); out != in || n != 0 {
|
|
t.Errorf("%q went as %q", in, out)
|
|
}
|
|
}
|
|
}
|
|
credentials := []string{"password=%s", "api_key=%s", "X-Api-Key: %s", "token: %s", `"refresh_token": "%s"`, "session_token=%s", "postgres://app:%s@db:5432/x"}
|
|
for _, id := range ids[:2] {
|
|
for _, c := range credentials {
|
|
forgetIDs()
|
|
in := strings.ReplaceAll(c, "%s", id)
|
|
if out, n := Mask(in, o); strings.Contains(out, id) || n != 1 {
|
|
t.Errorf("a credential went out: %q as %q", in, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// the model takes an id for anything at all: it goes as written
|
|
for label := range modelKinds {
|
|
forgetIDs()
|
|
id := ids[0]
|
|
mo := o
|
|
mo.Model = (&fakeModel{values: map[string]string{id: label}}).find
|
|
body := `{"messages":[{"role":"user","content":"look at order ` + id + ` please"}]}`
|
|
if got, n := MaskJSON([]byte(body), mo); string(got) != body || n != 0 {
|
|
t.Errorf("the model's %s: %s", label, got)
|
|
}
|
|
}
|
|
|
|
// a phone number masked before is not masked inside an id after
|
|
forgetIDs()
|
|
Mask("call 13800138000", o)
|
|
if in := "order " + ids[2]; func() bool { out, _ := Mask(in, o); return out != in }() {
|
|
t.Errorf("a known phone number was masked inside %q", ids[2])
|
|
}
|
|
|
|
// a phone number and a card number of their own are still masked
|
|
for _, in := range []string{"call 13800138000", "card 4111-1111-1111-1111", "call a13800138000"} {
|
|
forgetIDs()
|
|
if out, n := Mask(in, o); n != 1 || out == in {
|
|
t.Errorf("%q went as %q", in, out)
|
|
}
|
|
}
|
|
|
|
// what the user chose stays theirs: their words, their rules
|
|
forgetIDs()
|
|
uo := o
|
|
uo.Words = []string{ids[0]}
|
|
if out, n := Mask("order "+ids[0], uo); strings.Contains(out, ids[0]) || n != 1 {
|
|
t.Errorf("the user's word went out: %q", out)
|
|
}
|
|
forgetIDs()
|
|
}
|