0x0_air on X (10-28): with masking on, the agent stopped and asked again.
Their EVM address (0x89dfBD9b…, public) went as a secret placeholder and
the vendor's model told them to rotate it in case it was a private key;
the server IP they typed went as {{IP_…}} and the model refused to ssh
to it and asked for the real host.
- model.go: the local model's SECRET/ACCOUNT span on 0x and 40 hex
digits (evmRe) is left as written; no rule matched it, the model's
label did (the 0x prefix and mixed case got past hexRe). A 64-hex
private key is masked still, and so is a 40-hex value called a key
(secretCued).
- shape.go: an IP the IP rule masks goes as an address from the
documentation ranges (192.0.2/198.51.100/203.0.113 .101-.254, IPv6
2001:db8::/32), stable for the value, never shared by two values
(falls back to {{IP_…}} past 462 IPv4 ones), and put back only as a
whole address in replies and streamed tool calls (restoreAddr).
Preview shows the same stand-in.
- docs: gateway-routing.md, and masking.html in en/zh/ja.
Run:
- Real models through the local gateway, the screenshot's ssh prompt
with the placeholder vs a documentation address: with
{{IP_k3v9x2mq}}, deepseek-v4-pro, glm-5.3, kimi-k3 and
claude-sonnet-5-5 mostly searched ~/.ssh or asked for the real IP;
with 203.0.113.147 or 192.0.2.183 all 20 runs over 5 models ran ssh
at once.
- New tests (internal/redact/standin_ip_test.go): the screenshot's
message, the tool call restored whole and streamed cut at every byte,
boundary cases, IPv6, 472 addresses never sharing, the screenshot's
address under labels secret and account_number with every category
on, and a 64-hex key still masked. gateway TestRedactedKinds now
wants a documentation address upstream.
- Broken by hand: without the evmRe check TestEVMAddressGoesAsWritten
is red for every text and label; without restoreAddr the boundary
cases are red; without the IP stand-in all three IP tests and
gateway TestRedactedKinds are red.
- On mote at this head (rebased on 27bf14790): gofmt -l, go vet
./... for darwin, linux (-tags nogui) and windows, go test -count=1 -timeout 45m
-tags nogui ./... (and go build for
darwin, linux -tags nogui and windows): every package ok, exit 0.
The run before at 56602426d had TestMoreKinds red, which wanted
{{IP_…}}; it now wants the documentation address.
- Rebased again on 56d26f0bb (two gateway commits, #4004): go vet
and go test -tags nogui of internal/redact, internal/gateway and
internal/maskrec on mote at this head: ok.
Not tried:
- the real AstrLink model on an EVM address (the fake model gives it
the labels it can give);
- a host name (user@host) still goes as {{HOSTNAME_…}};
- a documentation address the user writes themselves can match a
stand-in in a reply and be put back as the real one;
- stand-ins are not kept across a restart or once their slot is
evicted, like every placeholder.
199 lines
7.0 KiB
Go
199 lines
7.0 KiB
Go
package redact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"net/netip"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// docAddr says f is an IPv4 stand-in: in RFC 5737's ranges, .101 to .254.
|
|
func docAddr(f string) bool {
|
|
a, err := netip.ParseAddr(f)
|
|
if err != nil || !a.Is4() {
|
|
return false
|
|
}
|
|
b := a.As4()
|
|
inNet := b[0] == 192 && b[1] == 0 && b[2] == 2 || b[0] == 198 && b[1] == 51 && b[2] == 100 || b[0] == 203 && b[1] == 0 && b[2] == 113
|
|
return inNet && b[3] >= 101 && b[3] <= 254
|
|
}
|
|
|
|
// A server's address the user gives goes as an address from the
|
|
// documentation ranges, not {{IP_…}}: told "my server is {{IP_…}}", models
|
|
// asked the user for the real one or searched ~/.ssh for it (0x0_air on X,
|
|
// 10-28: "你消息里的 IP 被脱敏成 …,我无法连接"). The model's ssh with the
|
|
// stand-in comes back with the real address, in a whole reply and a stream.
|
|
func TestIPGoesAsADocumentationAddress(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
const ip = "47.96.123.45"
|
|
o := Options{Personal: true, Kinds: map[string]bool{"ip": true}}
|
|
msg := "我的服务器是 " + ip + ",用户 root,已经配好密钥登录。先 ssh 上去看看 uname -a 和磁盘空间。"
|
|
out, n := Mask(msg, o)
|
|
f := strings.TrimSuffix(strings.Fields(strings.TrimPrefix(out, "我的服务器是 "))[0], ",用户")
|
|
if n != 1 || strings.Contains(out, ip) || strings.Contains(out, "{{") || !docAddr(f) {
|
|
t.Fatalf("sent %q (address %q), want the address as a documentation one", out, f)
|
|
}
|
|
if again, _ := Mask(msg, o); again != out {
|
|
t.Errorf("not the same stand-in twice: %q, %q", out, again)
|
|
}
|
|
if p := Preview(msg, o); p != out {
|
|
t.Errorf("preview %q, sent %q", p, out)
|
|
}
|
|
if back := Restore(out, false); back != msg {
|
|
t.Errorf("put back as %q", back)
|
|
}
|
|
// the model's tool call, as the probe's models wrote it
|
|
call := `{"command":"ssh -o BatchMode=yes -o ConnectTimeout=10 root@` + f + ` 'uname -a; echo ---; df -h'"}`
|
|
if back := Restore(call, true); back != strings.Replace(call, f, ip, 1) {
|
|
t.Errorf("tool call put back as %s", back)
|
|
}
|
|
// only a whole address is put back
|
|
for _, c := range []struct{ s, want string }{
|
|
{f + ":22", ip + ":22"},
|
|
{"ssh to " + f + ".", "ssh to " + ip + "."},
|
|
{"[" + f + "]", "[" + ip + "]"},
|
|
{"ip:" + f, "ip:" + ip},
|
|
{f + "0", f + "0"},
|
|
{"1" + f, "1" + f},
|
|
{"10." + f, "10." + f},
|
|
{f + ".5", f + ".5"},
|
|
{f + "a", f + "a"},
|
|
} {
|
|
if got := Restore(c.s, false); got != c.want {
|
|
t.Errorf("Restore(%q) = %q, want %q", c.s, got, c.want)
|
|
}
|
|
}
|
|
|
|
// streamed: the tool call's arguments cut at every place in the address
|
|
whole := `{"command":"ssh root@` + f + ` 'uname -a'"}`
|
|
for cut := strings.Index(whole, f); cut <= strings.Index(whole, f)+len(f); cut++ {
|
|
ev := func(args string) string {
|
|
b, _ := json.Marshal(map[string]any{"choices": []any{map[string]any{"index": 0, "delta": map[string]any{
|
|
"tool_calls": []any{map[string]any{"index": 0, "function": map[string]any{"arguments": args}}}}}}})
|
|
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 args 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 {
|
|
ToolCalls []struct{ Function struct{ Arguments string } } `json:"tool_calls"`
|
|
}
|
|
}
|
|
}
|
|
if err := json.Unmarshal([]byte(d), &x); err != nil {
|
|
t.Fatalf("event %q: %v", d, err)
|
|
}
|
|
for _, c := range x.Choices[0].Delta.ToolCalls {
|
|
args.WriteString(c.Function.Arguments)
|
|
}
|
|
}
|
|
if want := strings.Replace(whole, f, ip, 1); args.String() != want {
|
|
t.Errorf("cut at %d: arguments %s, want %s", cut, args.String(), want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An IPv6 address goes as one in 2001:db8::/32, and comes back.
|
|
func TestIPv6GoesAsADocumentationAddress(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
const ip = "2400:cb00:2048:1::c629:d7a2"
|
|
o := Options{Personal: true, Kinds: map[string]bool{"ip": true}}
|
|
out, n := Mask("ssh root@"+ip, o)
|
|
f := strings.TrimPrefix(out, "ssh root@")
|
|
a, err := netip.ParseAddr(f)
|
|
if n != 1 || err != nil || !netip.MustParsePrefix("2001:db8::/32").Contains(a) {
|
|
t.Fatalf("sent %q", out)
|
|
}
|
|
if back := Restore(`{"command":"ping6 `+f+`"}`, true); back != `{"command":"ping6 `+ip+`"}` {
|
|
t.Errorf("put back as %s", back)
|
|
}
|
|
}
|
|
|
|
// Two addresses never share a stand-in: past the 462 IPv4 ones, an
|
|
// address goes as its placeholder, and every one comes back as itself.
|
|
func TestIPStandInsNeverShared(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
o := Options{Personal: true, Kinds: map[string]bool{"ip": true}}
|
|
seen := map[string]string{}
|
|
const total = len(ipNets)*ipHosts + 10
|
|
for i := 0; i < total; i++ {
|
|
ip := fmt.Sprintf("47.96.%d.%d", i/200, 1+i%200)
|
|
out, n := Mask("host "+ip, o)
|
|
f := strings.TrimPrefix(out, "host ")
|
|
if n != 1 {
|
|
t.Fatalf("%s: sent %q", ip, out)
|
|
}
|
|
if i < len(ipNets)*ipHosts && !docAddr(f) || i >= len(ipNets)*ipHosts && !strings.HasPrefix(f, "{{IP_") {
|
|
t.Fatalf("%s (#%d) went as %q", ip, i, f)
|
|
}
|
|
if other, ok := seen[f]; ok {
|
|
t.Fatalf("%s and %s both went as %s", other, ip, f)
|
|
}
|
|
seen[f] = ip
|
|
if again, _ := Mask("host "+ip, o); again != out {
|
|
t.Fatalf("%s: %q then %q", ip, out, again)
|
|
}
|
|
}
|
|
for f, ip := range seen {
|
|
if back := Restore("ssh "+f, false); back != "ssh "+ip {
|
|
t.Fatalf("%s put back as %q, want %s", f, back, ip)
|
|
}
|
|
}
|
|
}
|
|
|
|
// An EVM wallet's or contract's address is public, and goes as written
|
|
// whatever the local model takes it for: masked, the vendor's model took
|
|
// the placeholder for a private key and told the user to rotate it (0x0_air
|
|
// on X, 10-28). A private key, 64 hex digits, is masked still, and so is
|
|
// 0x and 40 hex digits called a key.
|
|
func TestEVMAddressGoesAsWritten(t *testing.T) {
|
|
const addr = "0x89dfBD9b4C2e7A1f3D5b6E8c0A9F2d4B7e1C3a5D"
|
|
const pk = "0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318"
|
|
all := map[string]bool{}
|
|
for _, c := range Categories {
|
|
all[c.ID] = true
|
|
}
|
|
for _, text := range []string{
|
|
"挖矿收益打到这个合约地址 " + addr + " 就行,不需要签名",
|
|
"wallet: " + addr,
|
|
`{"to":"` + addr + `","value":"0x0"}`,
|
|
"address is " + addr + ".",
|
|
} {
|
|
for _, label := range []string{"secret", "account_number"} {
|
|
f := &fakeModel{values: map[string]string{addr: label}}
|
|
if out, n := Mask(text, Options{Secrets: true, Personal: true, Kinds: all, Model: f.find}); n != 0 {
|
|
t.Errorf("%q as %s: masked %q", text, label, out)
|
|
}
|
|
}
|
|
}
|
|
for _, c := range []struct{ text, value string }{
|
|
{"私钥 " + pk + " 别外传", pk},
|
|
{"the key is " + pk, pk},
|
|
{"api key: " + addr, addr},
|
|
{"token is " + addr, addr},
|
|
} {
|
|
f := &fakeModel{values: map[string]string{c.value: "secret"}}
|
|
if out, _ := Mask(c.text, Options{Secrets: true, Personal: true, Model: f.find}); strings.Contains(out, c.value) {
|
|
t.Errorf("%q: left in %q", c.value, out)
|
|
}
|
|
}
|
|
}
|