akic404 on Discord (#feedback, 10-31): "ip的脱敏策略需要改进,会把公网ip直接
脱敏成内网ip,能不能沿用其他的方法用{ip:…}的代替". Since c2aad3db0 (10-28) a
masked public IP went as an address from RFC 5737's ranges (192.0.2.,
198.51.100., 203.0.113.). A 192.0.2. one reads as a LAN address to a
person, and every model knows the ranges: asked why ssh to the address
timed out, the models told the user they had an example address, not
their server's.
Now a public IPv4 or IPv6 address goes as public-ip- and eight lowercase
letters (public-ip-kvxqmrtz): visibly masked, still says it is public,
and a host name models ssh to. Same stand-in for an address every turn,
never shared, put back only as a whole name (no letter, digit, - or _
against it, no dot before it or before a name after it), in streamed
tool-call arguments too. A name with no dot reaches nobody if the vendor
tries it. A documentation address the user writes is no stand-in any
more, so it can't come back as someone's real address.
Run:
- Real models through the local gateway (deepseek-v4-pro, glm-5.3,
kimi-k3, claude-sonnet-5-5, gpt-6-luna), two prompts each: "我的服务器
是 X,帮我 ssh 上去看看磁盘" with a bash tool, and "阿里云服务器 X 从家里
ssh 超时,是公网还是内网地址?最可能的原因?".
- 203.0.113.147 / 198.51.100.147 / 192.0.2.183: ssh 5/5; diagnosis:
4/5 (deepseek, kimi, gpt, glm) said "example address, use your real
one", sonnet named it TEST-NET too.
- {{IP_k3v9x2mq}}: ssh 0/5 (asked for the real IP, or grepped env);
diagnosis fine.
- public-ip-kvxqmrtz: ssh 5/5; diagnosis 5/5 security group or
"public address", none said wrong/example address.
- ip-kvxqmrtz (no "public"): ssh 4/5, but 4/5 called it an internal
host name. Hence the public- head.
- On mote: gofmt -l, go vet ./internal/redact ./internal/gateway,
go test -count=1 -tags nogui ./internal/redact ./internal/maskrec,
and -run "Redact|Mask|Restore|IP|Privacy" ./internal/gateway: ok.
- Broken by hand: restoreAddr without its boundary check turns
TestIPGoesAsAPublicIPName red; stand-ins made as 203.0.113.x again turn
TestIPGoesAsAPublicIPName, TestIPv6GoesAsAPublicIPName,
TestIPStandInsNeverShared and gateway TestRedactedKinds red.
Not tried:
- a real magpie build end to end with a real model (the gateway test runs
the whole path against a fake upstream; the real models saw the
stand-in text itself);
- the full suite, and Linux/Windows vet (no OS-specific code touched);
- an agent that resolves host names through a DNS search domain would
look up public-ip-x.<domain> if a stand-in ever came back unrestored.
211 lines
7.6 KiB
Go
211 lines
7.6 KiB
Go
package redact
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// standInRe is an address's stand-in: public-ip- and eight lowercase
|
|
// letters, no address of any kind.
|
|
var standInRe = regexp.MustCompile(`^public-ip-[a-z]{8}$`)
|
|
|
|
func isIPStandIn(f string) bool { return standInRe.MatchString(f) }
|
|
|
|
// A server's address the user gives goes as a public-ip- name, neither
|
|
// {{IP_…}} nor another address: told "my server is {{IP_…}}", models asked
|
|
// the user for the real one or searched ~/.ssh for it (0x0_air on X,
|
|
// 10-28); told a documentation address, they said it was an example and
|
|
// not the user's server, and a 192.0.2. one read as a LAN address
|
|
// (akic404 on Discord, 10-31). The model's ssh with the stand-in comes
|
|
// back with the real address, in a whole reply and a stream.
|
|
func TestIPGoesAsAPublicIPName(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, "{{") || !isIPStandIn(f) {
|
|
t.Fatalf("sent %q (address %q), want the address as a public-ip- name", 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},
|
|
{"http://" + f + ":8080/health", "http://" + ip + ":8080/health"},
|
|
{"`" + f + "`", "`" + ip + "`"},
|
|
{"**" + f + "**", "**" + ip + "**"},
|
|
{f + ",用户 root", ip + ",用户 root"},
|
|
{"Host " + f + "\n", "Host " + ip + "\n"},
|
|
{f + "0", f + "0"},
|
|
{f + "a", f + "a"},
|
|
{f + "-2", f + "-2"},
|
|
{f + "_x", f + "_x"},
|
|
{"my-" + f, "my-" + f},
|
|
{"x" + f, "x" + f},
|
|
{"db." + f, "db." + f},
|
|
{f + ".lan", f + ".lan"},
|
|
} {
|
|
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 a public-ip- name too, and comes back, in a
|
|
// URL's brackets as well.
|
|
func TestIPv6GoesAsAPublicIPName(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@")
|
|
if n != 1 || !isIPStandIn(f) {
|
|
t.Fatalf("sent %q", out)
|
|
}
|
|
if back := Restore(`{"command":"ping6 `+f+`"}`, true); back != `{"command":"ping6 `+ip+`"}` {
|
|
t.Errorf("put back as %s", back)
|
|
}
|
|
if back := Restore("curl http://["+f+"]:8080/", false); back != "curl http://["+ip+"]:8080/" {
|
|
t.Errorf("put back as %s", back)
|
|
}
|
|
}
|
|
|
|
// Two addresses never share a stand-in, every one comes back as itself,
|
|
// and a documentation address the user writes goes and comes back as
|
|
// written: it is no stand-in, so nothing turns it into another address.
|
|
func TestIPStandInsNeverShared(t *testing.T) {
|
|
Forget()
|
|
t.Cleanup(Forget)
|
|
o := Options{Personal: true, Kinds: map[string]bool{"ip": true}}
|
|
seen := map[string]string{}
|
|
for i := 0; i < 1000; 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 || !isIPStandIn(f) {
|
|
t.Fatalf("%s (#%d) went as %q", ip, i, out)
|
|
}
|
|
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)
|
|
}
|
|
}
|
|
const doc = "203.0.113.147"
|
|
if out, n := Mask("例子里写的是 "+doc, o); n != 0 || out != "例子里写的是 "+doc {
|
|
t.Errorf("a documentation address went as %q", out)
|
|
}
|
|
if back := Restore("ssh root@"+doc, false); back != "ssh root@"+doc {
|
|
t.Errorf("a documentation address came back as %q", back)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
}
|