redact: words in lower case the model took for a secret go as written unless assigned to a secret's name (dniray on Discord)

dniray, on v0.1.1976 after 300b7eee0: drizzle-orm still went out as
{{SECRET_o2tu7vej}}, now in a tsup config in a Markdown fence:
  external: [/@nestjs/, 'reflect-metadata', 'rxjs', 'mysql2', 'drizzle-orm'],
v0.1.1932 and v0.1.1976 each let a package's name go in more places
(import lines, manifests, errors, installs, pins); a list in a build
config was none of them, and the places can't all be listed.

Cause, reproduced with the real model on mote: AstrLink Guard finds
drizzle-orm as a secret on that line alone at 0.515 (and pieces of it,
"zzle-orm" 0.755, "-orm", which modelFits already drops, elsewhere in
the block). Under Guard's default slider (0.8) it isn't masked; with the
slider at or below 0.515 origin/main masks it ('{{SECRET_…}}'), and once
masked it is known and masked in the whole block and every later
request. Privacy Filter finds nothing in it.

The rule, by shape instead of place:
- modelFits: a secret the model found that is lower-case letters only,
  words joined by - . or _ (wordsOnly: drizzle-orm, reflect-metadata,
  lodash.merge) is masked only where it is assigned to a secret's name
  (assignedSecret: a name holding password/pass/pwd/key/token/secret/
  credential or 密码/密钥/令牌, then =, :, is/was, 是/为, right before
  it). A list's comma is no assignment: 'jsonwebtoken', 'drizzle-orm'
  is not cued, where secretCued would take the token in it.
- knownSpans: a known SECRET of that shape whose score is the model's
  alone (scores < ruleFound) is masked again only where assigned. A
  rule's find, the user's rules' and a value with a digit, a capital or
  a symbol are matched as before. magpie's own SECRET rules already ask a
  digit of a value after password= (secretValue).
- Old data: the known store is in memory only, so updating (a restart)
  forgets a remembered drizzle-orm anyway; in a magpie still running,
  the knownSpans check lets one the model remembered go on the next
  request with no action from the user.
- Cost: a lower-case-letters-only password the model alone found under a
  password's name stays masked there, but where it comes back with no
  name before it (an echo in prose) it now goes as written.

Tests: package_words_test.go. TestModelWordsArentSecrets: the literal
block in a user message as Markdown, in a Read tool_result with Claude
Code's line numbers, prose and the name alone, with a fake model at
0.93; red on origin/main ({{SECRET_…}} in all). TestKnownModelWordsGoAsWritten:
drizzle-orm and reflect-metadata remembered as the model's, then the
block with no model and in a body; red on origin/main; and
"DB_PASSWORD: drizzle-orm" still masked. TestWordSecretsStillMasked:
PASSWORD=opensesame, {"password": "letmeinplease"}, "the wifi password
is purplemonkey", api_key: correct-horse-battery, hunter2pass,
Zq8vR2mT6wLp, Tr0ub4dor&3xq still masked by the model; a rule's
"token 是 bluewhalesinging" masked again as known in prose.

Run, at 926ef769f + this change, on mote:
- go test -count=1 -timeout 45m ./internal/redact/... ./internal/masking/...
  ./internal/otlp/... ./internal/pii/... ./internal/sessions/...
  ./internal/settings/... ./internal/gwmw/... ./plugins/redact/...: ok
  (TestMaskEval's recordings pass unchanged, no Known entry flipped)
- go test -run "Redact|Mask|Privacy|Unredact|Known|Restore"
  ./internal/gateway ./internal/gui: ok
- gofmt -l internal/redact: clean; go vet, GOOS=linux -tags nogui and
  GOOS=windows go vet on ./internal/redact/ ./internal/masking/: ok
- the real AstrLink Guard and Privacy Filter (zzcache) over the literal
  block, a fragment, the lone line, the block in a Markdown doc and a
  .env, each also with Read line numbers, slider at 0.3: origin/main
  masks drizzle-orm in the lone line (Guard); with this change nothing
  masks it, and PASSWORD=opensesame (Guard 0.908/0.962), hunter2pass and
  the JWT secret are masked still.

Broken by hand, each red: the modelFits check off
(TestModelWordsArentSecrets, all five); the knownSpans check off
(TestKnownModelWordsGoAsWritten, both); assignedSecret always false
(TestWordSecretsStillMasked, four cued cases, and the assigned case).

Not tried: dniray's own text and slider setting (the screenshot's block
is cut off at the top); the full go test ./... and the whole gateway
and gui suites; Windows and Linux runs (vet only). No eval case added
(recordings not re-recorded).
This commit is contained in:
yetone
2026-10-29 08:45:58 +08:00
parent 926ef769ff
commit b9f1fcddde
3 changed files with 174 additions and 1 deletions
+8 -1
View File
@@ -199,9 +199,16 @@ func knownSpans(s string, o Options) []span {
if !o.coversKind(k.kind) {
continue
}
if sc, ok := scores[k.v]; ok && sc < o.MinScore {
sc, scored := scores[k.v]
if scored && sc < o.MinScore {
continue // the model's alone, under the slider
}
if scored && sc < ruleFound && k.kind == "SECRET" && wordsOnly(k.v) && !assignedSecret(s, i) {
// words the model alone took for a secret, as modelFits
// lets go now: what a magpie before it had remembered
// (drizzle-orm, dniray on Discord, v0.1.1976)
continue
}
if best != nil && len(k.v) <= len(best.v) || !strings.HasPrefix(s[i:], k.v) || !standsAlone(k.kind, s, i, i+len(k.v)) {
continue
}
+47
View File
@@ -2,6 +2,7 @@ package redact
import (
"regexp"
"slices"
"strings"
"unicode"
"unicode/utf8"
@@ -359,6 +360,9 @@ func modelFits(s string, a, b int, kind string) bool {
if pinnedAfter(s, b) {
return false
}
if wordsOnly(v) && !assignedSecret(s, a) {
return false
}
return !hexRe.MatchString(v) || secretCued(s, a)
case "EMAIL":
// a URL's user:password@host is no address, as for the rule's (#3538)
@@ -367,6 +371,49 @@ func modelFits(s string, a, b int, kind string) bool {
return true
}
// wordsOnlyRe is words in lower case and nothing else, joined by - . or
// _: drizzle-orm, reflect-metadata, lodash.merge, secretstorage.
var wordsOnlyRe = regexp.MustCompile(`^[a-z]+(?:[-._][a-z]+)*$`)
// wordsOnly says v, which the model took for a secret, is made of words
// in lower case alone, as a package's, a tool's or a setting's name is.
// A key is random: it has a digit, a capital or a symbol in it, and the
// SECRET rules ask a digit of a value after password= too (secretValue).
// AstrLink Guard took drizzle-orm for a secret in a tsup config's
// external list in a Markdown fence (dniray on Discord, v0.1.1976; 0.515
// on that line alone), after v0.1.1932 and v0.1.1976 had each let it go
// in more places (an import, package.json, a tsc error): the places a
// package's name is written can't all be listed, so the shape decides. Such a value is a
// secret only where it is assigned to a secret's name (assignedSecret):
// PASSWORD=opensesame, "password": "letmeinplease", the wifi password is
// purplemonkey.
func wordsOnly(v string) bool { return wordsOnlyRe.MatchString(v) }
// assignedSecret says the value at s[a] is given to a secret's name: the
// name, then =, :, is or 是 (and the quotes around them), right before it.
// A list's comma before it is no assignment, whatever is further back:
// 'jsonwebtoken', 'drizzle-orm' (secretCued takes the token in that).
func assignedSecret(s string, a int) bool {
before := strings.ToLower(strings.TrimRight(s[max(0, a-64):a], " \t\"'`“「"))
for _, sep := range []string{"=", ":", ":", " is", " was", "是", "为", "為"} {
if !strings.HasSuffix(before, sep) {
continue
}
name := strings.TrimRight(before[:len(before)-len(sep)], " \t\"'`:")
i := len(name)
for i > 0 && (wordByte(name[i-1]) || name[i-1] == '.' || name[i-1] == '-') {
i--
}
if i == len(name) {
// a Chinese name: 密码是, 令牌为
return slices.ContainsFunc(secretCues, func(c string) bool { return c[0] >= 0x80 && strings.HasSuffix(name, c) })
}
return slices.ContainsFunc(secretCues, func(c string) bool { return c[0] < 0x80 && strings.Contains(name[i:], c) }) ||
strings.Contains(name[i:], "pass") || strings.Contains(name[i:], "pwd") || strings.Contains(name[i:], "credential")
}
return false
}
// pinnedAfter says the value that ends at s[b] is a package's name, as
// a requirement pins its version after it: django-rest-passwordreset==1.4.0,
// tokenizers>=0.15, a-lib~=2.1 (pip's comparisons). AstrLink Guard took a
+119
View File
@@ -0,0 +1,119 @@
package redact
import (
"encoding/json"
"strings"
"testing"
)
// dniray on Discord, v0.1.1976 after 300b7eee0: drizzle-orm still went
// out as {{SECRET_o2tu7vej}}, now in a tsup config in a Markdown fence,
// where nothing says a package is loaded or pinned:
// external: [/@nestjs/, 'reflect-metadata', 'rxjs', 'mysql2', 'drizzle-orm'].
// The places a package's name is written can't all be listed. A value of
// words in lower case alone has no randomness for a key to be made of:
// the model's secret of that shape is a secret only where it is assigned
// to a password's, a token's or a key's name.
const dnirayBlock = "```ts\nexport default defineConfig({\n entry: ['src/main.ts'],\n format: [\n 'cjs',\n clean: true,\n noExternal: [/@nexus\\/shared/],\n external: [/@nestjs/, 'reflect-metadata', 'rxjs', 'mysql2', 'drizzle-orm'],\n sourcemap: true,\n})\n```\n"
// scoredModel finds each of values as a secret at a score, as the real
// models give one (a fake's 0 counts as a rule's find).
func scoredModel(values ...string) Model {
f := &fakeModel{values: map[string]string{}}
for _, v := range values {
f.values[v] = "secret"
}
return func(texts []string) [][]Found {
out := f.find(texts)
for i := range out {
for j := range out[i] {
out[i][j].Score = 0.93
}
}
return out
}
}
func TestModelWordsArentSecrets(t *testing.T) {
const line = " external: [/@nestjs/, 'reflect-metadata', 'rxjs', 'mysql2', 'drizzle-orm'],"
o := Options{Secrets: true, Personal: true, Model: scoredModel("drizzle-orm", "reflect-metadata")}
// in a user's message, as Markdown
body, _ := json.Marshal(map[string]any{"model": "m", "messages": []any{
map[string]any{"role": "user", "content": "tsup.config.ts:\n\n" + dnirayBlock + "\nwhy is drizzle-orm bundled?"},
}})
out, _ := MaskJSON(body, o)
if s := string(out); strings.Contains(s, "{{SECRET_") || !strings.Contains(s, line) {
t.Errorf("in a message: %s", out)
}
// in a Read's tool_result, with Claude Code's line numbers
var read strings.Builder
for i, l := range strings.Split(strings.TrimSuffix(dnirayBlock, "\n"), "\n") {
read.WriteString(strings.Repeat(" ", 5) + string(rune('1'+i%9)) + "→" + l + "\n")
}
body, _ = json.Marshal(map[string]any{"model": "m", "messages": []any{
map[string]any{"role": "assistant", "content": []any{map[string]any{"type": "tool_use", "id": "toolu_01", "name": "Read", "input": map[string]any{"file_path": "/w/docs/build.md"}}}},
map[string]any{"role": "user", "content": []any{map[string]any{"type": "tool_result", "tool_use_id": "toolu_01", "content": read.String()}}},
}})
if out, _ := MaskJSON(body, o); strings.Contains(string(out), "{{SECRET_") {
t.Errorf("in a tool_result: %s", out)
}
// prose and a name alone
for _, s := range []string{"we keep drizzle-orm external", "drizzle-orm", "see reflect-metadata."} {
if out, n := Mask(s, o); n != 0 {
t.Errorf("%q: %q", s, out)
}
}
}
// A value the model took for a secret before this, remembered as known, is
// masked again only where it is assigned to a secret's name: what a user's
// running magpie had learned goes as written in the next request, the
// model finding nothing in it.
func TestKnownModelWordsGoAsWritten(t *testing.T) {
// as an older magpie remembered it: masked as the model's, at its score
p := placeholder("SECRET", "drizzle-orm")
placeholder("SECRET", "reflect-metadata")
for _, v := range []string{"drizzle-orm", "reflect-metadata"} {
noteScores(v, []span{{0, len(v), "SECRET", ByModel, 0.93, false}})
}
o := Options{Secrets: true, Personal: true}
if out, n := Mask(dnirayBlock, o); n != 0 || strings.Contains(out, p) {
t.Errorf("as known: %q", out)
}
body, _ := json.Marshal(map[string]any{"messages": []any{map[string]any{"role": "user", "content": "and now\n" + dnirayBlock}}})
o.Model = scoredModel()
if out, _ := MaskJSON(body, o); strings.Contains(string(out), "{{SECRET_") {
t.Errorf("as known, in a body: %s", out)
}
// where it is assigned to a password, it is the password still
if out, _ := Mask("DB_PASSWORD: drizzle-orm", o); strings.Contains(out, "drizzle-orm") {
t.Errorf("assigned: %q", out)
}
}
// What stays a secret: words the model finds where they are assigned to a
// secret's name, any value with a digit, a capital or a symbol in it, and
// whatever a rule found, words alone too, wherever it comes again.
func TestWordSecretsStillMasked(t *testing.T) {
for _, c := range []struct{ text, value string }{
{"PASSWORD=opensesame", "opensesame"},
{"{\"password\": \"letmeinplease\"}", "letmeinplease"},
{"the wifi password is purplemonkey", "purplemonkey"},
{"api_key: correct-horse-battery", "correct-horse-battery"},
{"login with hunter2pass", "hunter2pass"},
{"use Zq8vR2mT6wLp to sign in", "Zq8vR2mT6wLp"},
{"the code is Tr0ub4dor&3xq", "Tr0ub4dor&3xq"},
} {
o := Options{Secrets: true, Personal: true, Model: scoredModel(c.value)}
if out, _ := Mask(c.text, o); strings.Contains(out, c.value) {
t.Errorf("%q left in %q", c.value, out)
}
}
// a rule's find of words is masked as known wherever it comes again
if out, _ := Mask("token 是 bluewhalesinging", Options{Secrets: true}); strings.Contains(out, "bluewhalesinging") {
t.Fatalf("not masked by the rule: %q", out)
}
if out, _ := Mask("I typed bluewhalesinging there", Options{Secrets: true}); strings.Contains(out, "bluewhalesinging") {
t.Errorf("rule's known value left: %q", out)
}
}