Files
magpie/internal/update/mirror_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

237 lines
8.1 KiB
Go

package update
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func updateHome(t *testing.T) {
t.Helper()
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy", "NO_PROXY", "no_proxy"} {
t.Setenv(k, "")
}
}
// A release's file comes through a GitHub download mirror only when one is
// given (magpie update --mirror, or Settings' UpdateMirror): its prefix
// before the github.com URL. The feed, with the file's SHA-256, is never
// asked through it, and a file the mirror changed is refused (akic404 on
// Discord: install and update without a proxy).
func TestUpdateThroughAMirror(t *testing.T) {
updateHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
asset := Asset{URL: "https://github.com/yetone/magpie-releases/releases/download/v9.9.9/magpie-cli-linux-amd64", SHA256: hex.EncodeToString(sum[:])}
var mu sync.Mutex
var seen []string
tamper := false
mirror := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, r.URL.Path)
bad := tamper
mu.Unlock()
if bad {
w.Write([]byte("not magpie"))
return
}
w.Write(body)
}))
defer mirror.Close()
feed := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(Release{Version: "9.9.9", Assets: map[string]Asset{BinaryAsset(): asset}})
}))
defer feed.Close()
t.Setenv("MAGPIE_UPDATE_FEED", feed.URL+"/api/latest")
// none unless given: no mirror is magpie's own
if m := Mirror(context.Background()); m != "" {
t.Fatalf("a mirror by default: %q", m)
}
ctx := WithMirror(context.Background(), mirror.URL)
rel, err := Latest(ctx)
if err != nil {
t.Fatal(err)
}
path := filepath.Join(t.TempDir(), "magpie.new")
if err := download(ctx, rel.Assets[BinaryAsset()], path); err != nil {
t.Fatal(err)
}
if b, _ := os.ReadFile(path); string(b) != string(body) {
t.Fatalf("downloaded %q", b)
}
mu.Lock()
if want := []string{"/" + asset.URL}; !slices.Equal(seen, want) {
t.Fatalf("the mirror was asked %v, want %v (the feed never)", seen, want)
}
seen, tamper = nil, true
mu.Unlock()
// what the mirror changed doesn't match the feed's checksum
os.Remove(path)
err = download(ctx, asset, path)
if err == nil || !strings.Contains(err.Error(), "does not match its checksum") || !strings.Contains(err.Error(), "mirror") {
t.Fatalf("a tampered file: %v", err)
}
if _, serr := os.Stat(path); !os.IsNotExist(serr) {
t.Fatal("the tampered file was kept")
}
// Settings' mirror is the app's too; off turns it off for one run
mu.Lock()
tamper, seen = false, nil
mu.Unlock()
if err := settings.Save(settings.Settings{UpdateMirror: mirror.URL + "/"}); err != nil {
t.Fatal(err)
}
if m := Mirror(context.Background()); m != mirror.URL+"/" {
t.Fatalf("Settings' mirror: %q", m)
}
if err := download(context.Background(), asset, path); err != nil {
t.Fatal(err)
}
if m := Mirror(WithMirror(context.Background(), "off")); m != "" {
t.Fatalf("off: %q", m)
}
// a file not on GitHub goes as it is
if got := mirrored(mirror.URL, feed.URL+"/x"); got != feed.URL+"/x" {
t.Fatalf("a non-GitHub URL mirrored: %q", got)
}
if err := settings.Save(settings.Settings{UpdateMirror: "ftp://nope"}); err == nil {
t.Fatal("an ftp mirror was saved")
}
}
// magpie update --proxy takes the feed and the download through the proxy
// given, over Settings' and the environment's.
func TestUpdateThroughAProxyGiven(t *testing.T) {
updateHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
var mu sync.Mutex
var seen []string
px := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
seen = append(seen, r.URL.String())
mu.Unlock()
if r.URL.Path == "/asset" {
w.Write(body)
return
}
json.NewEncoder(w).Encode(Release{Version: "9.9.9", Assets: map[string]Asset{
BinaryAsset(): {URL: "http://releases.magpie.invalid/asset", SHA256: hex.EncodeToString(sum[:])},
}})
}))
defer px.Close()
if err := settings.Save(settings.Settings{Proxy: "direct"}); err != nil {
t.Fatal(err)
}
t.Setenv("MAGPIE_UPDATE_FEED", "http://feed.magpie.invalid/api/latest")
ctx := WithProxy(context.Background(), px.URL)
rel, err := Latest(ctx)
if err != nil {
t.Fatal(err)
}
if err := download(ctx, rel.Assets[BinaryAsset()], filepath.Join(t.TempDir(), "magpie.new")); err != nil {
t.Fatal(err)
}
mu.Lock()
defer mu.Unlock()
if want := []string{"http://feed.magpie.invalid/api/latest", "http://releases.magpie.invalid/asset"}; !slices.Equal(seen, want) {
t.Fatalf("through the proxy: %v, want %v", seen, want)
}
for _, bad := range []string{"ftp://127.0.0.1:21", "socks4://127.0.0.1:1080"} {
if CheckProxy(bad) == nil {
t.Errorf("%s taken as a proxy", bad)
}
}
for _, good := range []string{"127.0.0.1:7890", "http://127.0.0.1:7890", "socks5://127.0.0.1:1080", "socks5h://u:p@127.0.0.1:1080", "direct"} {
if err := CheckProxy(good); err != nil {
t.Errorf("%s: %v", good, err)
}
}
}
// #1886: CheckProxy's error, shown in the GUI and the CLI, names the
// proxy without its user and password.
func TestCheckProxyErrorShowsNoCredentials(t *testing.T) {
for _, c := range []struct{ proxy, shown string }{
{"ftp://alice:s3cret@10.0.0.2:21", `"ftp://10.0.0.2:21"`},
{"socks4://ali%40ce:s3%3Acret@10.0.0.2:1080", `"socks4://10.0.0.2:1080"`},
{"socks4://alice@10.0.0.2:1080", `"socks4://10.0.0.2:1080"`},
{"http://alice:s3cret@", ""},
{"alice:s3cret%zz@10.0.0.2:8080", ""},
} {
err := CheckProxy(c.proxy)
if err == nil {
t.Fatalf("%s taken as a proxy", c.proxy)
}
if c.shown != "" && !strings.Contains(err.Error(), c.shown) {
t.Errorf("%s: %v, want it to name %s", c.proxy, err, c.shown)
}
for _, secret := range []string{"ali", "s3"} {
if strings.Contains(err.Error(), secret) {
t.Errorf("%s: %v shows %q", c.proxy, err, secret)
}
}
}
}
// A download that fails through a mirror says it is the mirror's (#893):
// one that doesn't answer, answers 404, or sends another file, which is
// never kept; the GUI then offers GitHub itself. One from GitHub is no
// mirror's. Settings take a full https:// mirror only.
func TestMirrorFailuresAreTheMirrors(t *testing.T) {
updateHome(t)
body := []byte("new magpie")
sum := sha256.Sum256(body)
asset := Asset{URL: "https://github.com/yetone/magpie-releases/releases/download/v9.9.9/magpie-cli-linux-amd64", SHA256: hex.EncodeToString(sum[:])}
tampered := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("not magpie")) }))
defer tampered.Close()
missing := httptest.NewServer(http.NotFoundHandler())
defer missing.Close()
gone := httptest.NewServer(http.NotFoundHandler())
gone.Close()
for _, m := range []string{tampered.URL + "/", missing.URL + "/", gone.URL + "/"} {
path := filepath.Join(t.TempDir(), "magpie.new")
err := fetch(WithMirror(context.Background(), m), asset, path)
if err == nil || MirrorOf(err) != m || !strings.Contains(err.Error(), strings.TrimSuffix(m, "/")) {
t.Errorf("through %s: %v (mirror %q)", m, err, MirrorOf(err))
}
if _, serr := os.Stat(path); !os.IsNotExist(serr) {
t.Errorf("through %s the file was kept", m)
}
}
// straight from a host that fails: no mirror's
direct := Asset{URL: missing.URL + "/asset", SHA256: asset.SHA256}
if err := fetch(context.Background(), direct, filepath.Join(t.TempDir(), "x")); err == nil || MirrorOf(err) != "" {
t.Errorf("a direct failure: %v (mirror %q)", err, MirrorOf(err))
}
for _, bad := range []string{"", "http://mirror.example/", "mirror.example", "https://", "ftp://mirror.example/", "https://mirror.example/ x"} {
if CheckMirrorURL(bad) == nil {
t.Errorf("%q taken as a mirror", bad)
}
}
for _, good := range []string{"https://mirror.example/", "https://mirror.example/gh", " https://mirror.example "} {
if err := CheckMirrorURL(good); err != nil {
t.Errorf("%q: %v", good, err)
}
}
}