Files
magpie/internal/netproxy/describe_test.go
T
yetone fa44fde461 netproxy: a proxy's user and password stay out of every log line, error and status that names it (#1886)
What: netproxy.Redact is the one place a proxy address is made fit to
show. It is the rule 708ae4144 (#852) gave the Settings status
(proxyAddress, now Redact): the userinfo is left out, the rest kept as
written (no scheme added to one written without), a malformed address
"". Every place a proxy value reached text now goes through it:
- provider/logins.go: the switch's "Claude Code sessions ... go through
  <user>'s proxy" log line (#1886's first).
- provider/claude_proxy_env.go: "<user>'s proxy is SOCKS" (#1886's
  second).
- agent/codex_proxy.go + reach.go: the proxied drift's Detail and Addr,
  shown in the GUI's pill and dialog; Codex's proxy comes from
  HTTP_PROXY/ALL_PROXY/the system, which can carry a user:password.
- update/mirror.go: CheckProxy's error echoed the raw --proxy value
  ("not %q"); it now names it redacted, or not at all when malformed.
- netproxy.Describe already went through it.

Why: an account proxy like http://alice:s3cret@10.0.0.2:8080 landed in
magpie's log, which users paste into issues and Discord.

Swept and left alone: settings.CheckProxy's error names no value;
netproxy.DialSOCKS's error names the host only; Go's transport errors
(proxyconnect, socks connect) name host:port, not userinfo; the GUI's
provider editor and Settings field return the proxy whole as the
user's own editable value; env injection (EnvWith, ForBun, wslrun)
must carry it whole.

Tests place the credential where the user writes it: http, https,
socks5, socks5h, with and without a scheme, percent-encoded user and
password, user alone, malformed. TestRedactLeavesOutUserAndPassword,
TestClaudeAccountProxyLogsNoCredentials (captures the log over real
switches; Claude Code's HTTPS_PROXY still gets the proxy whole),
TestCodexProxiedDriftShowsNoCredentials (the probe still goes through
the whole proxy), TestCheckProxyErrorShowsNoCredentials.

Run: each site reverted alone turns its test red (logins.go,
claude_proxy_env.go, reach.go, codex_proxy.go, mirror.go; Redact
keeping the userinfo turns Redact and Describe's tests red). gofmt -l
clean; go vet ./... on darwin, GOOS=linux -tags nogui, GOOS=windows.
go test ./... at 0dd821c13 + this, sandbox HOME: 47 packages ok;
agent, gateway and library timed out at 10m on a machine at load ~20,
then on rerun hit ENOSPC (the disk filled from other sessions). Rerun
with space: library ok in full; agent's TestClaudeOutputTokens and
gateway's TestGroupFirstTokenFailsOver (the ENOSPC ones) ok, with
every Proxy/Proxied test in both. Not tried in the GUI by hand.
2026-10-13 10:49:44 +08:00

97 lines
3.8 KiB
Go

package netproxy
import (
"net/url"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The settings status must not display credentials; requests still need them.
func TestDescribeHidesProxyCredentials(t *testing.T) {
for _, source := range []string{"settings", "environment", "system"} {
for _, tc := range []struct{ name, raw, want string }{
{"plain", "socks5://127.0.0.1:1080", "socks5://127.0.0.1:1080"},
{"authenticated", "socks5://alice:secret@127.0.0.1:1080", "socks5://127.0.0.1:1080"},
{"encoded", "socks5h://ali%40ce:p%3Aa%2Fss%25@[::1]:1080", "socks5h://[::1]:1080"},
{"username", "http://alice@127.0.0.1:7890", "http://127.0.0.1:7890"},
} {
t.Run(source+"/"+tc.name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
sysCache.at, sysCache.p = farFuture, Proxy{}
t.Cleanup(func() { sysCache.at = zero })
switch source {
case "settings":
if err := settings.Save(settings.Settings{Proxy: tc.raw}); err != nil {
t.Fatal(err)
}
case "environment":
t.Setenv("ALL_PROXY", tc.raw)
case "system":
sysCache.p.URL = tc.raw
}
if got, from := Describe(); got != tc.want || from != source {
t.Fatalf("description = %q (%s), want %q (%s)", got, from, tc.want, source)
}
u, _ := url.Parse("https://vendor.example/request")
if p, err := For(u); err != nil || p == nil || p.String() != tc.raw {
t.Fatalf("description changed the proxy used by requests: %v", err)
}
})
}
}
}
func TestDescribeHidesMalformedSystemProxy(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
sysCache.at, sysCache.p = farFuture, Proxy{URL: "socks5://alice:secret%zz@127.0.0.1:1080"}
t.Cleanup(func() { sysCache.at = zero })
if got, from := Describe(); got != "" || from != "system" {
t.Fatalf("malformed proxy description = %q (%s)", got, from)
}
}
// #1886: Redact is what every log line, error, status and GUI text shows
// of a proxy. The user and password are written where a user writes them:
// http and socks5, with and without a scheme, percent-encoded, a user
// without a password. None of it is shown; the rest is kept as written.
func TestRedactLeavesOutUserAndPassword(t *testing.T) {
for _, tc := range []struct{ raw, want string }{
{"http://alice:s3cret@10.0.0.2:8080", "http://10.0.0.2:8080"},
{"https://alice:s3cret@proxy.example:443", "https://proxy.example:443"},
{"alice:s3cret@10.0.0.2:8080", "10.0.0.2:8080"},
{"socks5://alice:s3cret@127.0.0.1:1080", "socks5://127.0.0.1:1080"},
{"socks5h://alice:s3cret@127.0.0.1:1080", "socks5h://127.0.0.1:1080"},
{"http://ali%40ce:s3%3Acr%2Fet%25@10.0.0.2:8080", "http://10.0.0.2:8080"},
{"socks5://ali%40ce:s3%40cret@[::1]:1080", "socks5://[::1]:1080"},
{"ali%40ce:s3%40cret@10.0.0.2:8080", "10.0.0.2:8080"},
{"http://alice@10.0.0.2:8080", "http://10.0.0.2:8080"},
{"socks5://alice@127.0.0.1:1080", "socks5://127.0.0.1:1080"},
{"alice@10.0.0.2:8080", "10.0.0.2:8080"},
{"http://alice:@10.0.0.2:8080", "http://10.0.0.2:8080"},
{"socks5://alice:s3cret%zz@127.0.0.1:1080", ""},
{"alice:s3cret%zz@10.0.0.2:8080", ""},
{"http://10.0.0.2:8080", "http://10.0.0.2:8080"},
{"10.0.0.2:8080", "10.0.0.2:8080"},
{"direct", "direct"},
{"", ""},
} {
got := Redact(tc.raw)
if got != tc.want {
t.Errorf("Redact(%q) = %q, want %q", tc.raw, got, tc.want)
}
for _, secret := range []string{"ali", "s3"} {
if strings.Contains(got, secret) {
t.Errorf("Redact(%q) = %q shows %q", tc.raw, got, secret)
}
}
}
}