Files
magpie/internal/plugin/systemca_test.go
T
yetone 955cb43812 plugin: Bun reads the system's trust store itself (NODE_USE_SYSTEM_CA)
明霞 (Discord, Windows) behind an HTTPS-scanning box whose root is in the
OS store: every plugin request answered "self signed certificate in
certificate chain" (WorkBuddy AI on Routing), and updating the Qoder
plugin failed with UNABLE_TO_GET_ISSUER_CERT_LOCALLY.

magpie gave Bun only NODE_EXTRA_CA_CERTS, a file of the roots as magpie
read them: on Windows the current user's ROOT view only. Bun's own system
loader (root_certs_windows.cpp, Bun 1.4.2) reads ROOT, CA and
TrustedPeople of the machine, the user, group policy and the enterprise,
server-auth certs only: an intermediate a proxy doesn't send and a root
pushed by policy are there and weren't in magpie's file. bunCommand,
which starts every bun (the plugin host, bun add/update, the dist-branch
install), now also sets NODE_USE_SYSTEM_CA=1. Bun's runtime fetch and
bun add's HTTP client both build their store in us_get_default_ca_store,
which merges the system store when that variable is 1 (checked in
Bun's source at bun-v1.4.2, and by running it). Verification is never
turned off. A NODE_USE_SYSTEM_CA the user set (0 too) is kept; a
NODE_EXTRA_CA_CERTS the user set is kept and Bun takes it beside the
system store. magpie's own file is still given when the user has none,
for an older MAGPIE_BUN without the variable.

Go side unchanged: magpie's own clients (the dist-branch tarball
included) use crypto/x509's platform verifier on Windows and macOS and
SSL_CERT_FILE/SSL_CERT_DIR plus the distribution bundle on Linux; no
RootCAs is overridden anywhere.

Run:
- TestBunTrustsTheSystemsStore (new): a local TLS server signed by a test
  root, sending leaf+root, serving a one-package npm registry; real Bun
  1.4.2 fetch and bun add through bunCommand. Root in neither store:
  both refused. Root in the system store (Linux: SSL_CERT_DIR, the
  machine's store untouched): both get through. Root in the user's
  NODE_EXTRA_CA_CERTS: both get through.
  Linux (wsl-run, magpie-desktop): pass. Old systemca.go: the system-store
  case is red (fetch and bun add both fail), as is TestBunReadsTheSystemsStore.
  Windows (win-run, xiaomi-gpu) and macOS (mote): pass; the system-store
  case is skipped there, since it needs a change to the real trust store.
- TestBunReadsTheSystemsStore (new), TestBunIsGivenTheSystemsRoots: Linux,
  Windows, macOS pass.
- go test -tags nogui ./internal/plugin on mote: ok (116s). Windows
  TestHost* on xiaomi-gpu: ok.
- go build ./..., GOOS=windows and GOOS=linux go vet ./internal/plugin on
  mote; gofmt -l clean.
- On xiaomi-gpu, bun 1.4.2 with NODE_USE_SYSTEM_CA=1:
  tls.getCACertificates("default") 183 (121 bundled + 62 system); without
  it 121. magpie's own ROOT file there loads whole (35 certs).

Not run: full suite.
Not tried: a root actually installed in the Windows or macOS store (the
real trust stores were left alone), and the reporter's antivirus/proxy.
2026-10-28 13:50:41 +08:00

120 lines
4.8 KiB
Go

package plugin
import (
"context"
"runtime"
"slices"
"strings"
"testing"
)
// Bun checks TLS against its own roots: behind a proxy that re-signs HTTPS
// with a root the user installed, `bun add` failed with
// UNABLE_TO_VERIFY_LEAF_SIGNATURE (chenyyi001 on X, installing
// @magpie-community/opencode-kiro-auth). Every bun magpie runs is given
// the system's roots, unless the user names a file of their own.
func TestBunIsGivenTheSystemsRoots(t *testing.T) {
old := systemCAFile
t.Cleanup(func() { systemCAFile = old })
systemCAFile = func() string { return "/cache/bun/system-ca-abc.pem" }
// one the environment sets already is the user's and wins (below);
// the ambient one a shell may have isn't this test's
t.Setenv("NODE_EXTRA_CA_CERTS", "")
cmd := bunCommand(context.Background(), "bun", t.TempDir(), "add", "x")
if !slices.Contains(cmd.Env, "NODE_EXTRA_CA_CERTS=/cache/bun/system-ca-abc.pem") {
t.Fatalf("bun add isn't given the system's roots: %v", caVars(cmd.Env))
}
t.Setenv("NODE_EXTRA_CA_CERTS", "/mine.pem")
cmd = bunCommand(context.Background(), "bun", t.TempDir(), "add", "x")
if got := caVars(cmd.Env); !slices.Equal(got, []string{"NODE_EXTRA_CA_CERTS=/mine.pem"}) {
t.Fatalf("the user's own NODE_EXTRA_CA_CERTS = %v, want it alone", got)
}
systemCAFile = func() string { return "" }
if got := caEnv(nil, ""); !slices.Equal(got, []string{"NODE_USE_SYSTEM_CA=1"}) {
t.Fatalf("with no file to add: %v, want Bun to read the system's store itself", got)
}
}
// Bun reads the system's store itself (NODE_USE_SYSTEM_CA): magpie's copy
// of the ROOT store missed what Bun's own reading has (the CA store's
// intermediates a proxy doesn't send, the enterprise's and group
// policy's roots), and a plugin answered "self signed certificate in
// certificate chain" while bun add failed with
// UNABLE_TO_GET_ISSUER_CERT_LOCALLY (明霞 on Discord). A value the user
// gives it, 0 too, is theirs.
func TestBunReadsTheSystemsStore(t *testing.T) {
old := systemCAFile
t.Cleanup(func() { systemCAFile = old })
systemCAFile = func() string { return "/cache/bun/system-ca-abc.pem" }
t.Setenv("NODE_EXTRA_CA_CERTS", "")
t.Setenv("NODE_USE_SYSTEM_CA", "")
cmd := bunCommand(context.Background(), "bun", t.TempDir(), "run", "host.js")
if got := envVars(cmd.Env, "NODE_USE_SYSTEM_CA"); !slices.Equal(got, []string{"NODE_USE_SYSTEM_CA=1"}) {
t.Fatalf("Bun isn't told to read the system's store: %v", got)
}
// the user's own file of roots is taken beside the system's store
t.Setenv("NODE_EXTRA_CA_CERTS", "/mine.pem")
cmd = bunCommand(context.Background(), "bun", t.TempDir(), "add", "x")
if got := envVars(cmd.Env, "NODE_USE_SYSTEM_CA"); !slices.Equal(got, []string{"NODE_USE_SYSTEM_CA=1"}) {
t.Fatalf("with the user's NODE_EXTRA_CA_CERTS: %v", got)
}
t.Setenv("NODE_USE_SYSTEM_CA", "0")
cmd = bunCommand(context.Background(), "bun", t.TempDir(), "add", "x")
if got := envVars(cmd.Env, "NODE_USE_SYSTEM_CA"); !slices.Equal(got, []string{"NODE_USE_SYSTEM_CA=0"}) {
t.Fatalf("the user's own NODE_USE_SYSTEM_CA = %v, want it alone", got)
}
}
func envVars(env []string, name string) []string {
var out []string
for _, kv := range env {
// a blank one (t.Setenv's "") is no value; exec takes the last
if strings.HasPrefix(strings.ToUpper(kv), name+"=") && kv != name+"=" {
out = append(out, kv)
}
}
return out
}
func caVars(env []string) []string {
var out []string
for _, kv := range env {
if strings.HasPrefix(strings.ToUpper(kv), "NODE_EXTRA_CA_CERTS=") {
out = append(out, kv)
}
}
return out
}
// Bun on Windows can't open a NODE_EXTRA_CA_CERTS path that isn't ASCII
// (#1232: a Chinese user name), so there it is given the way to the file
// from where it runs, which is under the same user folder.
func TestBunIsGivenAnASCIIWayToTheRoots(t *testing.T) {
user := `C:\Users\张三`
ca := user + `\.cache\magpie\bun\system-ca-abc.pem`
for _, c := range []struct{ name, p, dir, goos, want string }{
{"under a Chinese user name", ca, user + `\.config\magpie\plugins`, "windows", `..\..\..\.cache\magpie\bun\system-ca-abc.pem`},
{"an ASCII path as it is", `C:\Users\bob\.cache\magpie\bun\system-ca-abc.pem`, `C:\Users\bob\x`, "windows", `C:\Users\bob\.cache\magpie\bun\system-ca-abc.pem`},
{"another drive, no way there", ca, `D:\plugins`, "windows", ca},
{"no way there in ASCII", ca, `C:\其他\plugins`, "windows", ca},
{"Bun elsewhere reads it whole", "/home/张三/.cache/magpie/bun/system-ca-abc.pem", "/home/张三/.config/magpie/plugins", "linux", "/home/张三/.cache/magpie/bun/system-ca-abc.pem"},
} {
if c.goos == "windows" && runtime.GOOS != "windows" {
// filepath.Rel works on this OS's paths; Windows' are tried there
continue
}
if got := bunReadable(c.p, c.dir, c.goos); got != c.want {
t.Errorf("%s: %q, want %q", c.name, got, c.want)
}
}
if !ascii(`..\..\x.pem`) || ascii(`C:\Users\张三`) {
t.Fatal("ascii")
}
}