Files
yetone 331f201faf plugin: a cross-signed root copy in Windows' ROOT store no longer shadows Bun's own root (youyo.p_10001)
youyo.p_10001 (Discord #feedback, magpie 0.1.2993 windows/amd64, Bun
1.4.4, Clash Verge on 127.0.0.1:7897): every MiniMax plugin request to
agent.minimaxi.com failed "unable to get issuer certificate", while the
same chain verified outside the plugin host.

Cause: magpie hands Bun the user's Windows ROOT store as
NODE_EXTRA_CA_CERTS. Bun (BoringSSL) builds the chain up through the
first trusted certificate with the subject it wants, the extra file's
ahead of its own bundle, and stops when that one's issuer is missing.
A ROOT store holding USERTrust RSA's copy cross-signed by AAA
Certificate Services (agent.minimaxi.com's chain goes leaf, WoTrus DV,
USERTrust RSA) gave Bun that copy ahead of the self-signed root it
ships, and AAA is in neither. Node, curl and Schannel go on to the
self-signed one, so outside magpie it worked.

Fix: rootsFirst writes the self-signed roots first and keeps another
certificate only when its issuer is in the file too. Bun never ends a
chain on one that isn't self-signed (no partial chains), so one left
out could only stand in front of a root. NODE_USE_SYSTEM_CA=1 stays: the
system store Bun reads itself comes after its bundle and shadows
nothing (tried, below).

Also, for the next report like it:
- the host logs its network settings when it starts, and when the
  proxy (or the gateway's address) moves, old to new: the proxy without
  user and password (netproxy.Redact), NO_PROXY, NODE_EXTRA_CA_CERTS,
  NODE_USE_SYSTEM_CA, SSL_CERT_FILE/DIR (netLine);
- a fetch Bun refuses for the certificate keeps Bun's words and its
  code and cause, and adds "(UNABLE_TO_GET_ISSUER_CERT: the plugin
  host's TLS didn't verify agent.minimaxi.com's certificate chain
  against the roots it trusts)", so it doesn't read as the vendor's
  fault (throwTLS in host.js).

The plugin side, a China-site account call that can't reach
agent.minimaxi.com going once to agent.minimax.cn, is
opencode-minimax-auth 0.2.3 (magpie-community/plugins#757).

Run:
- Reproduced on xiaomi-gpu (Windows 11, Bun 1.4.2) with the real host
  and the real ROOT store: the real cross-signed USERTrust RSA (valid to
  2028) imported into LocalMachine\Root, then a scratch test through
  bunCommand (NODE_USE_SYSTEM_CA=1 included) fetching
  https://agent.minimaxi.com/: the file as before ->
  UNABLE_TO_GET_ISSUER_CERT, rootsFirst's -> 200. Certificate removed
  after (checked); without it both 200. Also as NODE_EXTRA_CA_CERTS
  alone: cross copy -> UNABLE_TO_GET_ISSUER_CERT; self-signed then cross
  -> 200; cross then self-signed -> UNABLE_TO_GET_ISSUER_CERT; cross with
  AAA either order -> 200. The cross copy in CurrentUser\CA with
  NODE_USE_SYSTEM_CA=1 -> 200 (no shadowing from the system store).
- New tests: TestRootsFirstPutsTheSelfSignedCopyAhead,
  TestBunGetsPastACrossSignedRootCopy (a synthetic cross chain through
  Bun), TestPluginTLSRefusalSaysWhose (through the fake plugin's fetch;
  Bun's message there is the reporter's "unable to get issuer
  certificate"), TestNetLineSaysNoSecret. Green on mote (macOS, Bun
  1.4.2) and on xiaomi-gpu via win-run, with the systemca tests beside
  them.
- Broken by hand on mote: rootsFile without rootsFirst ->
  TestBunGetsPastACrossSignedRootCopy/the_cross-signed_copy_first red;
  throwTLS rethrowing as is -> TestPluginTLSRefusalSaysWhose red
  ("err = unable to get issuer certificate").
- mote, at this tree: gofmt -l clean; go vet, GOOS=linux go vet -tags
  nogui, GOOS=windows go vet of internal/plugin ok; go test
  ./internal/plugin ok (111.7s). xiaomi-gpu: go test -run
  'Proxy|CA|TLS|Roots|NetLine|LAN|Network' ./internal/plugin ok.

Not tried: the reporter's own machine and store (what put the cross
copy in their ROOT store is a guess from the error; the host's new log
line will show NODE_EXTRA_CA_CERTS, and the file is in the cache's bun
folder); Bun 1.4.4 (the reporter's; 1.4.2 here); macOS keychain copies
(rootsFirst applies to systemRoots there too, untried with a real cross
copy); Linux takes the distribution's bundle as it is, unchanged.
2026-11-05 16:58:34 +08:00

214 lines
6.7 KiB
Go

package plugin
import (
"bytes"
"crypto/sha256"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/yetone/magpie/internal/catalog"
)
// caEnv has Bun trust what this machine trusts. Bun checks TLS against
// the roots it ships, not the system's, so behind a box that re-signs
// HTTPS (a company proxy, a VPN, Surge's or Proxyman's MITM, an
// antivirus's HTTPS scan) whose root the user installed, magpie gets
// through and Bun doesn't: `bun add` fails with
// "UNABLE_TO_VERIFY_LEAF_SIGNATURE downloading package manifest" or
// UNABLE_TO_GET_ISSUER_CERT_LOCALLY, and a plugin's requests with "self
// signed certificate in certificate chain" (明霞 on Discord, Windows).
//
// NODE_USE_SYSTEM_CA=1 has Bun read the system's store itself, for the
// runtime's fetch and for bun add's downloads alike (both take
// us_get_default_ca_store, Bun 1.4.2): on Windows the ROOT, CA and
// TrustedPeople stores of the machine, the user, group policy and the
// enterprise, each certificate valid for server auth (an intermediate a
// proxy doesn't send, and a root a policy installed, among them); on
// macOS the keychains' trust settings; elsewhere $SSL_CERT_FILE and
// $SSL_CERT_DIR, else the distribution's bundle and folder. One the
// environment sets already, 0 too, is the user's and is left as it is.
//
// NODE_EXTRA_CA_CERTS adds a file of the system's roots as magpie reads
// them, as before: an older Bun the user points MAGPIE_BUN at has no
// NODE_USE_SYSTEM_CA. One the environment sets already is the user's and
// is left as it is; Bun takes it beside the system's store. dir is where
// Bun runs.
func caEnv(env []string, dir string) []string {
var out []string
if !envSet(env, "NODE_USE_SYSTEM_CA") {
out = append(out, "NODE_USE_SYSTEM_CA=1")
}
if !envSet(env, "NODE_EXTRA_CA_CERTS") {
if p := systemCAFile(); p != "" {
out = append(out, "NODE_EXTRA_CA_CERTS="+bunReadable(p, dir, runtime.GOOS))
}
}
return out
}
// envSet is whether env gives name a value that isn't blank.
func envSet(env []string, name string) bool {
for _, kv := range env {
k, v, _ := strings.Cut(kv, "=")
if strings.EqualFold(k, name) && strings.TrimSpace(v) != "" {
return true
}
}
return false
}
// bunReadable is p as a Bun on goos can open it. Bun on Windows reads
// NODE_EXTRA_CA_CERTS in the ANSI code page and takes it as UTF-8, so a
// path that isn't ASCII, as under a Chinese user name, comes out as
// "C:\Users\锟斤拷\..." and isn't found ("ignoring extra certs from ...,
// load failed: No such file or directory", then
// UNABLE_TO_VERIFY_LEAF_SIGNATURE, #1232; Bun 1.4.2). An 8.3 short name
// doesn't help: Windows gives none to a name like 测试. Bun opens a
// relative path from where it runs, and that is under the same user
// folder, so the way there from dir is ASCII.
func bunReadable(p, dir, goos string) string {
if goos != "windows" || ascii(p) || dir == "" {
return p
}
if rel, err := filepath.Rel(dir, p); err == nil && ascii(rel) {
return rel
}
return p
}
func ascii(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= 0x80 {
return false
}
}
return true
}
var (
systemCAOnce sync.Once
systemCAPath string
)
// systemCAFile is a PEM file of the roots the system trusts beyond Bun's
// own, "" when there are none to add; it is made once a run.
var systemCAFile = func() string {
systemCAOnce.Do(func() {
if p := systemCABundle(); p != "" {
systemCAPath = p
return
}
systemCAPath = rootsFile(systemRoots())
})
return systemCAPath
}
// rootsFile is a PEM file of certs as rootsFirst orders them, "" when
// none are left.
func rootsFile(certs [][]byte) string {
certs = rootsFirst(certs)
if len(certs) == 0 {
return ""
}
var b bytes.Buffer
for _, der := range certs {
_ = pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: der})
}
return writeCAFile(b.Bytes())
}
// rootsFirst is certs as Bun can be given them: the self-signed ones
// first, then each other one whose issuer is among them. Bun (BoringSSL,
// 1.4.2) builds a chain up through the first certificate it trusts with
// the subject it is after, NODE_EXTRA_CA_CERTS's before its own roots',
// and goes no further when that one's issuer is missing. A copy of a root
// that another root cross-signed (USERTrust RSA's, issued by AAA
// Certificate Services, which agent.minimaxi.com's chain goes up to) in
// the user's ROOT store came ahead of the self-signed one Bun ships, and
// every request there failed "unable to get issuer certificate"
// (UNABLE_TO_GET_ISSUER_CERT, youyo.p_10001 on Discord, Windows), while
// Node, curl and a browser got through. With the self-signed copy ahead
// of it, or with its issuer beside it, Bun got through. One whose issuer
// isn't here either is left out: Bun never ends a chain on a certificate
// that isn't self-signed, so it can only stand in front of a root. One
// Go can't parse is kept, after the rest, as before.
func rootsFirst(certs [][]byte) [][]byte {
type cert struct {
der []byte
x *x509.Certificate
}
var roots, rest []cert
var odd [][]byte
for _, der := range certs {
x, err := x509.ParseCertificate(der)
switch {
case err != nil:
odd = append(odd, der)
case bytes.Equal(x.RawSubject, x.RawIssuer):
roots = append(roots, cert{der, x})
default:
rest = append(rest, cert{der, x})
}
}
kept := append([]cert(nil), roots...)
issued := func(c cert) bool {
for _, k := range kept {
if bytes.Equal(k.x.RawSubject, c.x.RawIssuer) &&
(len(c.x.AuthorityKeyId) == 0 || len(k.x.SubjectKeyId) == 0 || bytes.Equal(c.x.AuthorityKeyId, k.x.SubjectKeyId)) {
return true
}
}
return false
}
// an intermediate's issuer can be another intermediate
for grew := true; grew; {
grew = false
left := rest[:0]
for _, c := range rest {
if issued(c) {
kept = append(kept, c)
grew = true
} else {
left = append(left, c)
}
}
rest = left
}
out := make([][]byte, 0, len(kept)+len(odd))
for _, k := range kept {
out = append(out, k.der)
}
return append(out, odd...)
}
// writeCAFile keeps pem beside Bun's install cache, named by its content so
// a Bun that is running keeps reading the file it was given.
func writeCAFile(b []byte) string {
sum := sha256.Sum256(b)
dir := filepath.Join(filepath.Dir(catalog.CachePath()), "bun")
p := filepath.Join(dir, "system-ca-"+hex.EncodeToString(sum[:6])+".pem")
if old, err := os.ReadFile(p); err == nil && bytes.Equal(old, b) {
return p
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return ""
}
tmp, err := os.CreateTemp(dir, ".system-ca-*")
if err != nil {
return ""
}
_, werr := tmp.Write(b)
cerr := tmp.Close()
if werr != nil || cerr != nil || os.Rename(tmp.Name(), p) != nil {
os.Remove(tmp.Name())
return ""
}
return p
}