Tomyu_2034 on X: "magpie 里面的 masking 很容易把参数变量名当成 user name
给 mask 掉". No sample was given, so the class was measured: with "User
name in home folders" and "User and computer names" on, two finders took
names in code for the user's.
- Known values (known.go). A user name the home rule found once
(/Users/developer/…) is known from then on when it is 8 bytes or more,
and standsAlone's default bound is a letter or a digit only, so it was
masked wherever the word stood between anything else: def
run(developer, developer_dir), developer_mode = True, const developer
= 1, {"developer": true}, - developer_count := 0 in a diff, a tool
call's old_string. A user name such as developer or workspace1 is a
word code has too.
- The "User:" line rule (more.go). Its unquoted value was any
login-shaped word, so user = current_user, user: UserInfo, login =
loginName, username = target_name, a Go struct's "\tUser UserProfile"
and a TS interface's "user: CurrentUser" went as made-up names.
nameAlone now decides a known USERNAME: it is the user's name as a word
of its own in text (a path's part, name@host, --author=name, ls -l's
owner, uid=501(name), prose, a quoted string, chown name:staff), and
code's where it is joined to another part (_ $ a letter or digit beside
it, or a . or - with one past it), declared (const/let/var/def/func/
class/return/import…), assigned or called (name =, name(, name[,
name :=, x = name), a JSON key ("name":), or a parameter ((name, ,name)
name: str, name string), but for id's 501(name)). Nothing is exempted
by a key's name: a tool call's arguments are read by the same rule as
any text, and a quoted value in them is masked as before. The "User:"
rule leaves an unquoted value made of two or more words (split at _ .
and camel humps) one of which says it holds a user (identParts: user,
name, login, current, target, info, obj, id…); a quoted one, and a
login of words of its own (deploy_bot, john_doe), are masked still.
The boundary costs this: prose that puts the name alone in a list
"(developer, alice)" or before "=" now goes as written; such text reads
as code to this rule. A name under 8 bytes was never matched as known,
so for those only the "User:" half changes anything.
Tests (identname_test.go), with "developer" (a common word) and
"rinakato" (a handle), known from a working directory first:
- TestUserNameInCodeIsNoUserName: a Python and a JS file read, a JSON
file, Go code, a diff and an edit tool call's arguments go as written;
a home path, --author=, ls -l, uid=501(), prose, a quoted string, a
JSON value, chown and a bash tool call's ~name go as the made-up name.
- TestUserLineInCodeIsNoUserName: seven code lines unmasked; User:
deploy_bot, USER=rinak, user: "current_user", ssh config's User and
user = john_doe masked.
Broken by hand: with more.go and known.go as on main both tests are red
(12 and 7 failures); with standsAlone's USERNAME case returning true,
TestUserNameInCodeIsNoUserName alone is red.
Run at this head (on origin/main 89ffc58f5; nothing since touches
internal/redact or the doc), on mote:
- gofmt -l clean; go vet ./internal/redact ./internal/masking, and
GOOS=linux -tags nogui and GOOS=windows go vet ./internal/redact: clean.
- go test ./internal/redact ./internal/masking ./internal/otlp
./internal/pii ./internal/sessions ./internal/settings: ok.
- go test ./internal/gateway -run '(?i)mask|redact|privacy|personal|
standin|restore': ok.
- go test ./internal/gui: TestMainWindowCorners/macOS_26_SDK red, the
AppKit title bar on mote's macOS 27 (#3949, open); nothing else.
Not tried: the full internal/gateway package and go test ./... (the
gateway takes ~45 min; pushed at once per the owner's ship rule); the
local model's PERSON finds, which already leave snake_case and
lower-case words (modelTakes, programWords); the home rule's
"/home/<segment>" in a web route ('/home/dashboard'), which reads as a
home folder and is unchanged; a real request with the reporter's code.
110 lines
4.5 KiB
Go
110 lines
4.5 KiB
Go
package redact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A user name a rule found in a home folder is masked again wherever it
|
|
// stands as the user's name, but not where code has the same word as a
|
|
// name of its own: a parameter, a variable, a JSON key, a part of a
|
|
// snake_case or camelCase name (Tomyu_2034 on X: "masking 很容易把参数变量名
|
|
// 当成 user name 给 mask 掉"). A user name such as developer is a word code
|
|
// has too. The texts are what an agent sends: a file it read, a diff, a
|
|
// tool call's arguments.
|
|
func TestUserNameInCodeIsNoUserName(t *testing.T) {
|
|
Forget()
|
|
defer Forget()
|
|
o := Options{Personal: true, Kinds: map[string]bool{"home": true, "userhost": true}}
|
|
for _, name := range []string{"developer", "rinakato"} {
|
|
f := standInOf(name)
|
|
// the system prompt's working folder: the name is known from here on
|
|
if out, _ := Mask("Working directory: /Users/"+name+"/proj", o); !strings.Contains(out, "/Users/"+f+"/proj") {
|
|
t.Fatalf("%s: home folder sent as %q", name, out)
|
|
}
|
|
r := strings.NewReplacer("NAME", name)
|
|
for _, s := range []string{
|
|
// a file read
|
|
r.Replace("def run(NAME, NAME_dir: str):\n NAME_mode = True\n self.NAME = NAME\n return NAME\n"),
|
|
r.Replace("const NAME = loadUser();\nlet NAMEConfig = {};\nfunction greet(NAME) { return NAME(1) }\n"),
|
|
r.Replace(`{"NAME": true, "NAME_id": 3, "NAME-config": "x"}`),
|
|
r.Replace("import NAME from './NAME.js'\nfunc New(NAME string, opts Options) {}\nNAME := cfg.NAME\n"),
|
|
// a diff
|
|
r.Replace("@@ -1,3 +1,3 @@\n- NAME_count := 0\n+ NAMECount := 0\n var NAME int\n if NAME == 0 {\n"),
|
|
} {
|
|
if out, _ := Mask(s, o); out != s {
|
|
t.Errorf("%s: code sent as\n%s\nwant\n%s", name, out, s)
|
|
}
|
|
}
|
|
// a tool call's arguments
|
|
body := `{"messages":[{"role":"assistant","tool_calls":[{"id":"call_1","type":"function","function":{"name":"edit","arguments":` +
|
|
jsonString(r.Replace(`{"file_path":"src/app.py","old_string":"def run(NAME):","new_string":"def run(NAME, NAME_dir):"}`)) + `}}]}]}`
|
|
if out, _ := MaskJSON([]byte(body), o); string(out) != body {
|
|
t.Errorf("%s: arguments sent as\n%s", name, out)
|
|
}
|
|
|
|
// and where it is the user's name, it goes as the made-up name
|
|
for _, c := range [][2]string{
|
|
{"cat /Users/NAME/.zshrc", "cat /Users/F/.zshrc"},
|
|
{"git log --author=NAME", "git log --author=F"},
|
|
{"drwxr-xr-x 5 NAME staff 160 Nov 2 10:00 proj", "drwxr-xr-x 5 F staff 160 Nov 2 10:00 proj"},
|
|
{"uid=501(NAME) gid=20(staff)", "uid=501(F) gid=20(staff)"},
|
|
{"Thanks, NAME! The build is green.", "Thanks, F! The build is green."},
|
|
{"I'm NAME.", "I'm F."},
|
|
{`os.chown(path, "NAME")`, `os.chown(path, "F")`},
|
|
{`{"owner": "NAME"}`, `{"owner": "F"}`},
|
|
{"chown NAME:staff app.log", "chown F:staff app.log"},
|
|
} {
|
|
s, want := r.Replace(c[0]), strings.NewReplacer("F", f).Replace(r.Replace(c[1]))
|
|
if out, _ := Mask(s, o); out != want {
|
|
t.Errorf("%s: %q sent as %q, want %q", name, s, out, want)
|
|
}
|
|
}
|
|
args := `{"messages":[{"role":"assistant","tool_calls":[{"id":"call_2","type":"function","function":{"name":"bash","arguments":` +
|
|
jsonString(r.Replace(`{"command":"ls -la ~NAME && echo NAME"}`)) + `}}]}]}`
|
|
if out, _ := MaskJSON([]byte(args), o); strings.Contains(string(out), name) || !strings.Contains(string(out), "~"+f+" && echo "+f) {
|
|
t.Errorf("%s: arguments sent as\n%s", name, out)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A "User:" line's value in code is a variable's or a type's name, not a
|
|
// login: user = current_user, user: UserInfo, a Go struct's User field.
|
|
// Written out (quoted, or a login of words of its own), it is masked.
|
|
func TestUserLineInCodeIsNoUserName(t *testing.T) {
|
|
Forget()
|
|
defer Forget()
|
|
o := Options{Personal: true, Kinds: map[string]bool{"userhost": true}}
|
|
for _, s := range []string{
|
|
"user = current_user",
|
|
" user: UserInfo",
|
|
"login = loginName",
|
|
"username = target_name",
|
|
"user = user_obj",
|
|
"type Session struct {\n\tUser UserProfile\n}",
|
|
"export interface Props {\n user: CurrentUser\n}",
|
|
} {
|
|
if out, n := Mask(s, o); n != 0 {
|
|
t.Errorf("%q sent as %q", s, out)
|
|
}
|
|
}
|
|
for _, c := range [][2]string{
|
|
{"User: deploy_bot", "deploy_bot"},
|
|
{"USER=rinak", "rinak"},
|
|
{`user: "current_user"`, "current_user"},
|
|
{"Host box\n User rinak", "rinak"},
|
|
{"user = john_doe", "john_doe"},
|
|
} {
|
|
if out, n := Mask(c[0], o); n != 1 || strings.Contains(out, c[1]) {
|
|
t.Errorf("%q sent as %q, want %s masked", c[0], out, c[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
// jsonString is s as a JSON string.
|
|
func jsonString(s string) string {
|
|
b, _ := json.Marshal(s)
|
|
return string(b)
|
|
}
|