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.
69 lines
2.8 KiB
Go
69 lines
2.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
// docIP is an address's stand-in: public-ip- and eight lowercase letters
|
|
var docIP = regexp.MustCompile(`public-ip-[a-z]{8}`)
|
|
|
|
// The kinds of personal data the user turned on or off in settings
|
|
// (redactKinds) are what the vendor doesn't see, the others as their
|
|
// defaults say; what it answers with a placeholder comes back as the value.
|
|
func TestRedactedKinds(t *testing.T) {
|
|
const ssn, ip, bucket, user = "536-22-1847", "34.117.59.81", "acme-prod-logs", "rinakato"
|
|
var got []byte
|
|
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
got, _ = io.ReadAll(r.Body)
|
|
p := docIP.FindString(string(got))
|
|
chunk := func(s string) string {
|
|
b, _ := json.Marshal(map[string]any{"id": "c1", "model": "m1", "choices": []any{map[string]any{"index": 0, "delta": map[string]any{"content": s}}}})
|
|
return "data: " + string(b)
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
io.WriteString(w, sse(chunk("ping "+p), `data: {"id":"c1","model":"m1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2}}`, "data: [DONE]"))
|
|
}))
|
|
defer up.Close()
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
|
if err := provider.Save(provider.Provider{ID: "fake", Name: "Fake", Key: "k", Models: []string{"m1"}, Chat: up.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := settings.Save(settings.Settings{Redact: true, RedactPersonal: true, RedactKinds: map[string]bool{"ip": true}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req := `{"model":"fake/m1","max_tokens":10,"stream":true,"messages":[{"role":"user","content":"SSN ` + ssn + `, server ` + ip + `, logs in s3://` + bucket + `/x, cwd /Users/` + user + `/src"}]}`
|
|
code, body := post(t, "/v1/messages", req)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, body)
|
|
}
|
|
sent := string(got)
|
|
// an address goes as a public-ip- name, not a placeholder, so the
|
|
// model uses it rather than asking for the real one
|
|
if strings.Contains(sent, ssn) || strings.Contains(sent, ip) || !strings.Contains(sent, "{{SSN_") || !docIP.MatchString(sent) || strings.Contains(sent, "{{IP_") {
|
|
t.Fatalf("SSN (on by default) or IP (turned on) went out: %s", sent)
|
|
}
|
|
if !strings.Contains(sent, "s3://"+bucket+"/x") || !strings.Contains(sent, "/Users/"+user+"/src") {
|
|
t.Fatalf("a bucket or a home folder (off by default) masked: %s", sent)
|
|
}
|
|
var text strings.Builder
|
|
for _, e := range events(body) {
|
|
if d, ok := e["delta"].(map[string]any); ok {
|
|
s, _ := d["text"].(string)
|
|
text.WriteString(s)
|
|
}
|
|
}
|
|
if text.String() != "ping "+ip {
|
|
t.Fatalf("text %q in:\n%s", text.String(), body)
|
|
}
|
|
}
|