Files
magpie/internal/plugin/plugin_cliproxy_test.go
T
yetone f0c63264e5 plugin: a Node program a plugin starts takes magpie's proxy, as Node's fetch skips HTTPS_PROXY without NODE_USE_ENV_PROXY=1
larchsis on X (Windows 11, a proxy set in magpie, none in the system):
the "our free model" plugin's sign-in said 原渠道包下载失败. The plugin
downloads its channel pack in a Node program it starts. The host hands
such a program magpie's proxy as HTTPS_PROXY/HTTP_PROXY (since the grok
login fix), but Node's built-in fetch ignores those unless
NODE_USE_ENV_PROXY=1, so the download went direct and failed behind the
wall.

host.js's proxied() now also sets NODE_USE_ENV_PROXY=1 whenever it
hands the program a proxy, unless the plugin's env already names it
(a plugin's own value, 0 included, is kept). It is not lowercased.

Not changed: the Go side's netproxy.Env, which starts npm, Claude Code,
grok and devin. Those read *_PROXY themselves, and the proxy there may be
socks5://, which Node's env proxy does not take.

Run:
- TestPluginNodeTakesTheProxy (new) fails on the old host.js and passes
  now, on macOS and on the Windows box.
- go test ./internal/plugin/... passes.
- GOOS=windows and GOOS=linux go vet pass; gofmt is clean.
- End to end through the real host, on the Mac (Node 22.22) and on the
  Windows box (Node 22.22.2), with a throwaway plugin (not committed)
  that starts `node` to fetch https://raw.githubusercontent.com/ while
  magpie's proxy points at a fake 502 proxy:
  - old host.js: direct200, so it went past the proxy;
  - fixed: fetch failed at the proxy.
2026-10-10 18:11:00 +08:00

133 lines
4.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package plugin
import (
"context"
"net/url"
"os"
"path/filepath"
"testing"
"time"
)
// 𝕏 on Discord: the Grok plugin's sign-in said "grok login gave no link
// to open". The plugin runs `grok login`, and the host has magpie's proxy
// only as MAGPIE_*_PROXY, so the CLI went out with none where x.ai is
// reached only through one. A program a plugin starts has it, as the
// built-in's `grok login` had it.
func TestPluginCLIHasTheProxy(t *testing.T) {
sandbox(t)
const proxy = "http://127.0.0.1:7890"
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
for _, k := range []string{"inherited", "copied"} {
if got := u.Query().Get(k); got != proxy {
t.Errorf("a CLI started with the %s environment has HTTPS_PROXY %q, want %q", k, got, proxy)
}
}
}
// larchsis on 𝕏: a plugin's sign-in on Windows 11 said its download
// failed, with a proxy set in magpie and none in the system. The plugin
// downloads in a Node program it starts, and Node's fetch goes past
// HTTPS_PROXY unless NODE_USE_ENV_PROXY=1 says to take it, so the proxy
// the host handed on was never used. It is said with the proxy, and a
// value the plugin set itself is kept.
func TestPluginNodeTakesTheProxy(t *testing.T) {
sandbox(t)
const proxy = "http://127.0.0.1:7892"
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
t.Setenv("NODE_USE_ENV_PROXY", "") // restored after the test
os.Unsetenv("NODE_USE_ENV_PROXY")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
q := u.Query()
for _, k := range []string{"inheritedNode", "copiedNode"} {
if got := q.Get(k); got != "1" {
t.Errorf("%s: a program the plugin starts with the proxy has NODE_USE_ENV_PROXY %q, want 1", k, got)
}
}
if got := q.Get("ownNode"); got != "0" {
t.Errorf("the plugin's own NODE_USE_ENV_PROXY=0 became %q", got)
}
}
// #1363 (iamyhzhao): Grok, moved onto its plugin, read as signed out
// after nearly every restart of the computer, until moved back to the
// built-in and on again. magpie, started at login, started the plugin
// host before the proxy app had set the system's proxy, and the host
// keeps the proxy it was started with: `grok models`, which renews the
// sign-in, went out with none from then on. A host whose proxy is no
// longer magpie's is replaced at the next call.
func TestPluginHostTakesANewProxy(t *testing.T) {
sandbox(t)
prev := proxyLookEvery
proxyLookEvery = 0
t.Cleanup(func() { proxyLookEvery = prev })
// at login: no proxy yet in the environment
for _, k := range []string{"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "https_proxy", "http_proxy", "all_proxy"} {
t.Setenv(k, "")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
ask := func() string {
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
return u.Query().Get("inherited")
}
before := ask()
// then the proxy app sets one
const proxy = "http://127.0.0.1:7891"
if before == proxy {
t.Fatalf("the host had %s before it was set", proxy)
}
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
if got := ask(); got != proxy {
t.Errorf("after the proxy was set, a CLI the plugin starts has HTTPS_PROXY %q, want %q", got, proxy)
}
// and a proxy unchanged keeps the host
now := func() *host { hostMu.Lock(); defer hostMu.Unlock(); return current }
h := now()
ask()
if now() != h {
t.Error("the host was replaced with the proxy unchanged")
}
}