yapechan (Discord #feedback): with masking on, a person's name sometimes
came back as letters that are no one's, with no {{…}} braces. A user name
in a home folder goes to the vendor as eight made-up lowercase letters
(standInOf). When the login name is the user's own name (yaobin), a model
takes those letters for the user's name and writes them as a name is
written, "作者:Tmhdfetm", "Hi Tmhdfetm,". restoreShapes put a stand-in
back only as written, so a capitalised or upper-case one reached the user
as garbage. An IP stand-in (public-ip-…), a host name that is the same in
any case, had the same gap.
Now a USERNAME or IP stand-in is put back in any ASCII case: in Restore,
RestoreJSON (hasShapes), a stream's held tail (shapeTail) and
outsideShapes. Matching uses an ASCII-lowered copy of the same length
(lowerASCII, which now returns s itself with no capital in it, so there
is no copy), and strings.Index runs once per stand-in as before. An IP
stand-in is still put back only as a whole name. A key's made-up form
(Options.Shape) is still put back only as written, because a key's case
is part of it.
The restart question: the placeholder-to-value map is in memory only.
But a placeholder is an HMAC of its value under a key kept on disk
(redact.key, 0600, SetKeyPath from the gateway), and an agent resends its
history with the values on every turn. So after a restart the same value
goes as the same placeholder and the reply comes back whole.
TestPlaceholdersOutliveARestart now guards that, using the reporter's
姚斌 as a user word. Nothing new is written to disk.
Not found in magpie: a homophone (姚斌 -> 姚兵). Nothing in redact, pii
or restore maps a name by sound. A value goes back only for its own
placeholder or stand-in. The test records that 姚兵 goes back as the
vendor wrote it. Most likely the vendor model wrote it from a
romanisation it saw (a login name, an address); this is not reproduced.
Run at 19e9f7a82 (rebased on 4fc34828e), on mote:
- go test ./internal/redact ./internal/masking ./plugins/redact: ok
- go test -run 'Redact|Mask|Placeholder|Restore' ./internal/gateway: ok
- before the rebase, the whole ./internal/gateway (-timeout 45m): one red,
TestCodexPoolSaysTheClientsVersion, which is #4807 (another test's
Codex identity reaches it in a full run). It passes alone -count=3 at
this head, and it does not touch masking.
- GOOS=linux and GOOS=windows go vet ./internal/redact ./internal/gateway: ok
- gofmt -l: clean
Broken by hand: with the old shape.go, both InAnyCase tests are red
(Restore, RestoreJSON and the stream at every cut). With loadKey ignoring
the key file, TestPlaceholdersOutliveARestart is red (new placeholders,
reply left as {{TERM_…}}).
Not tried: a real vendor writing a capitalised stand-in. The reporter's
own session and records were not seen. The GUI was not run (no GUI
change).
180 lines
6.2 KiB
Go
180 lines
6.2 KiB
Go
package redact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// A user name's made-up name comes back as the user's in any case the
|
|
// model writes it. Its login name was the user's own (yaobin), and a model
|
|
// took the made-up one for the user's name and wrote it as a name is
|
|
// written, capitalised; it came back to the user as letters that are no
|
|
// one's, with no braces (yapechan on Discord).
|
|
func TestUserNameStandInPutBackInAnyCase(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
o := Options{Personal: true, Kinds: map[string]bool{"home": true}}
|
|
out, n := Mask("cd /Users/yaobin/workspace/report && git log", o)
|
|
f := standInOf("yaobin")
|
|
if n != 1 || !strings.Contains(out, "/Users/"+f+"/") {
|
|
t.Fatalf("sent %q, want the user name as %q", out, f)
|
|
}
|
|
title := strings.ToUpper(f[:1]) + f[1:]
|
|
upper := strings.ToUpper(f)
|
|
for _, c := range []struct{ s, want string }{
|
|
{"作者:" + title + ",路径 /Users/" + f + "/workspace", "作者:yaobin,路径 /Users/yaobin/workspace"},
|
|
{"Hi " + title + ", the report is done.", "Hi yaobin, the report is done."},
|
|
{"OWNER=" + upper, "OWNER=yaobin"},
|
|
{"/Users/" + title + "/x", "/Users/yaobin/x"},
|
|
} {
|
|
if got := Restore(c.s, false); got != c.want {
|
|
t.Errorf("Restore(%q) = %q, want %q", c.s, got, c.want)
|
|
}
|
|
}
|
|
if !hasShapes([]byte(`{"text":"` + title + `"}`)) {
|
|
t.Errorf("a reply with %q is not looked at", title)
|
|
}
|
|
if got := string(RestoreJSON([]byte(`{"text":"致 ` + title + `"}`))); got != `{"text":"致 yaobin"}` {
|
|
t.Errorf("RestoreJSON = %s", got)
|
|
}
|
|
|
|
// streamed, cut at every place in the capitalised name
|
|
whole := "报告作者:" + title + "。"
|
|
for cut := strings.Index(whole, title); cut <= strings.Index(whole, title)+len(title); cut++ {
|
|
ev := func(text string) string {
|
|
b, _ := json.Marshal(map[string]any{"choices": []any{map[string]any{"index": 0, "delta": map[string]any{"content": text}}}})
|
|
return "data: " + string(b) + "\n\n"
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
rec.Header().Set("Content-Type", "text/event-stream")
|
|
w := NewWriter(rec)
|
|
w.WriteHeader(200)
|
|
for _, e := range []string{ev(whole[:cut]), ev(whole[cut:]), "data: [DONE]\n\n"} {
|
|
w.Write([]byte(e))
|
|
}
|
|
w.Finish()
|
|
var got strings.Builder
|
|
for _, e := range strings.Split(rec.Body.String(), "\n\n") {
|
|
d, ok := strings.CutPrefix(e, "data: ")
|
|
if !ok || d == "[DONE]" {
|
|
continue
|
|
}
|
|
var x struct {
|
|
Choices []struct{ Delta struct{ Content string } }
|
|
}
|
|
if json.Unmarshal([]byte(d), &x) == nil && len(x.Choices) > 0 {
|
|
got.WriteString(x.Choices[0].Delta.Content)
|
|
}
|
|
}
|
|
if got.String() != "报告作者:yaobin。" {
|
|
t.Errorf("cut at %d: streamed %q", cut, got.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
// An address's stand-in is a host's name, the same in any case, and comes
|
|
// back so; still only where it is a whole name.
|
|
func TestIPStandInPutBackInAnyCase(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
const ip = "47.96.123.45"
|
|
out, _ := Mask("服务器 "+ip+" 连不上", Options{Personal: true, Kinds: map[string]bool{"ip": true}})
|
|
f := strings.Fields(out)[1]
|
|
if !isIPStandIn(f) {
|
|
t.Fatalf("sent %q", out)
|
|
}
|
|
up := strings.ToUpper(f)
|
|
for _, c := range []struct{ s, want string }{
|
|
{"ssh root@" + up, "ssh root@" + ip},
|
|
{"Host " + strings.ToUpper(f[:1]) + f[1:] + ":22", "Host " + ip + ":22"},
|
|
{"my-" + up, "my-" + up},
|
|
{up + ".lan", up + ".lan"},
|
|
} {
|
|
if got := Restore(c.s, false); got != c.want {
|
|
t.Errorf("Restore(%q) = %q, want %q", c.s, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A key's made-up form keeps its case: one that differs from it in case
|
|
// is another key, and goes back as written.
|
|
func TestShapedKeyPutBackOnlyAsWritten(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
const key = "sk_w3axCByTQgK1Px9bLmQr7Zt"
|
|
out, _ := Mask("OPENAI_API_KEY="+key, Options{Secrets: true, Shape: true})
|
|
f := strings.TrimPrefix(out, "OPENAI_API_KEY=")
|
|
if f == key || strings.Contains(f, "{{") {
|
|
t.Fatalf("sent %q", out)
|
|
}
|
|
if got := Restore(f, false); got != key {
|
|
t.Errorf("Restore(%q) = %q", f, got)
|
|
}
|
|
if got := Restore(strings.ToLower(f), false); got != strings.ToLower(f) {
|
|
t.Errorf("a key differing in case put back as %q", got)
|
|
}
|
|
}
|
|
|
|
// What magpie remembers of placeholders is in memory, but a placeholder is
|
|
// made from its value and a key kept on disk (SetKeyPath), so after a
|
|
// restart (an update in the background) the same value goes as the same
|
|
// placeholder, and the conversation the agent sends again has its values
|
|
// put back in the reply (yapechan on Discord asked). Names from the
|
|
// reporter: 姚斌 as one of the user's own words.
|
|
func TestPlaceholdersOutliveARestart(t *testing.T) {
|
|
Forget()
|
|
mu.Lock()
|
|
oldKey, oldPath := keyOf, keyPath
|
|
keyOf, keyPath = nil, filepath.Join(t.TempDir(), "redact.key")
|
|
mu.Unlock()
|
|
t.Cleanup(func() {
|
|
Forget()
|
|
mu.Lock()
|
|
keyOf, keyPath = oldKey, oldPath
|
|
mu.Unlock()
|
|
})
|
|
path := keyPath
|
|
o := Options{Words: []string{"姚斌"}}
|
|
history := "请把周报发给姚斌,抄送姚斌的主管。"
|
|
before, n := Mask(history, o)
|
|
if n != 2 || strings.Contains(before, "姚斌") {
|
|
t.Fatalf("sent %q", before)
|
|
}
|
|
p := placeholderRe.FindString(before)
|
|
reply := "好的,已发给 " + p + ",并抄送 " + p + " 的主管。"
|
|
|
|
// magpie starts again: nothing in memory, the key read from disk
|
|
Forget()
|
|
mu.Lock()
|
|
keyOf = nil
|
|
mu.Unlock()
|
|
SetKeyPath(path)
|
|
if b, err := os.ReadFile(path); err != nil || len(b) < 32 {
|
|
t.Fatalf("key not kept: %v", err)
|
|
}
|
|
after, _ := Mask(history, o)
|
|
if after != before {
|
|
t.Errorf("after a restart the history went as %q, before as %q", after, before)
|
|
}
|
|
want := "好的,已发给 姚斌,并抄送 姚斌 的主管。"
|
|
if got := Restore(reply, false); got != want {
|
|
t.Errorf("the reply put back as %q, want %q", got, want)
|
|
}
|
|
// as a model writes it back: without its braces, in another case
|
|
bare := strings.Trim(p, "{}")
|
|
for _, s := range []string{bare, strings.ToLower(bare), "{{ " + bare + " }}", "{" + bare + "}"} {
|
|
if got := Restore("发给 "+s+"。", false); got != "发给 姚斌。" {
|
|
t.Errorf("Restore(%q) = %q", s, got)
|
|
}
|
|
}
|
|
// nothing puts back a name that sounds like it: 姚兵 is the vendor's
|
|
// own text, and goes to the user as the vendor wrote it
|
|
if got := Restore("发给姚兵。", false); got != "发给姚兵。" {
|
|
t.Errorf("Restore(姚兵) = %q", got)
|
|
}
|
|
}
|