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.
208 lines
7.8 KiB
Go
208 lines
7.8 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"math/big"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/testenv"
|
|
)
|
|
|
|
// crossChain is a server whose chain goes up to a root (root) that
|
|
// another root (top) also signed (cross): leaf, then int, then root's
|
|
// subject, as USERTrust RSA's is for agent.minimaxi.com. The server sends
|
|
// leaf and int, as that one does.
|
|
type crossChain struct {
|
|
top, root, cross, int, leaf []byte
|
|
url string
|
|
}
|
|
|
|
func newCrossChain(t *testing.T) crossChain {
|
|
t.Helper()
|
|
key := func() *ecdsa.PrivateKey {
|
|
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return k
|
|
}
|
|
serial := int64(0)
|
|
ca := func(name string, skid byte) *x509.Certificate {
|
|
serial++
|
|
return &x509.Certificate{
|
|
SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: name},
|
|
NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour),
|
|
IsCA: true, BasicConstraintsValid: true, KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
|
|
SubjectKeyId: []byte{skid},
|
|
}
|
|
}
|
|
mk := func(tpl, parent *x509.Certificate, pub *ecdsa.PublicKey, priv *ecdsa.PrivateKey) ([]byte, *x509.Certificate) {
|
|
der, err := x509.CreateCertificate(rand.Reader, tpl, parent, pub, priv)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
x, _ := x509.ParseCertificate(der)
|
|
return der, x
|
|
}
|
|
topKey, rootKey, intKey, leafKey := key(), key(), key(), key()
|
|
var c crossChain
|
|
topTpl := ca("magpie test AAA root", 1)
|
|
var top *x509.Certificate
|
|
c.top, top = mk(topTpl, topTpl, &topKey.PublicKey, topKey)
|
|
rootTpl := ca("magpie test USERTrust root", 2)
|
|
var root *x509.Certificate
|
|
c.root, root = mk(rootTpl, rootTpl, &rootKey.PublicKey, rootKey)
|
|
c.cross, _ = mk(ca("magpie test USERTrust root", 2), top, &rootKey.PublicKey, topKey)
|
|
intTpl := ca("magpie test DV server CA", 3)
|
|
var in *x509.Certificate
|
|
c.int, in = mk(intTpl, root, &intKey.PublicKey, rootKey)
|
|
serial++
|
|
c.leaf, _ = mk(&x509.Certificate{
|
|
SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: "127.0.0.1"},
|
|
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)}, DNSNames: []string{"localhost"},
|
|
NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour),
|
|
KeyUsage: x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
}, in, &leafKey.PublicKey, intKey)
|
|
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "through") }))
|
|
srv.TLS = &tls.Config{Certificates: []tls.Certificate{{Certificate: [][]byte{c.leaf, c.int}, PrivateKey: leafKey}}}
|
|
srv.StartTLS()
|
|
t.Cleanup(srv.Close)
|
|
c.url = srv.URL
|
|
return c
|
|
}
|
|
|
|
// The roots magpie hands Bun go self-signed first, and a certificate whose
|
|
// issuer isn't among them is left out (rootsFirst).
|
|
func TestRootsFirstPutsTheSelfSignedCopyAhead(t *testing.T) {
|
|
c := newCrossChain(t)
|
|
name := map[string]string{string(c.top): "top", string(c.root): "root", string(c.cross): "cross", string(c.int): "int"}
|
|
names := func(certs [][]byte) string {
|
|
var s []string
|
|
for _, d := range certs {
|
|
s = append(s, name[string(d)])
|
|
}
|
|
return strings.Join(s, " ")
|
|
}
|
|
for _, tc := range []struct {
|
|
in [][]byte
|
|
want string
|
|
}{
|
|
{[][]byte{c.cross, c.root}, "root"},
|
|
{[][]byte{c.cross, c.top}, "top cross"},
|
|
{[][]byte{c.cross, c.root, c.top}, "root top cross"},
|
|
{[][]byte{c.cross}, ""},
|
|
{[][]byte{c.int, c.cross, c.top}, "top cross int"},
|
|
{[][]byte{c.int}, ""},
|
|
{[][]byte{c.root, []byte("not a certificate")}, "root "},
|
|
} {
|
|
if got := names(rootsFirst(tc.in)); got != tc.want {
|
|
t.Errorf("rootsFirst(%s) = %q, want %q", names(tc.in), got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// youyo.p_10001 (Discord, Windows): every request the MiniMax plugin made
|
|
// to agent.minimaxi.com failed "unable to get issuer certificate", while
|
|
// the same chain verified outside the plugin host. USERTrust RSA's copy
|
|
// cross-signed by AAA Certificate Services was in the user's ROOT store,
|
|
// so in the file magpie gives Bun, and Bun took it over the self-signed
|
|
// one and found no AAA. Here the ROOT store holds the cross-signed copy
|
|
// ahead of the self-signed one; Bun gets through, as it does with the
|
|
// copy's issuer beside it, and still refuses with neither.
|
|
func TestBunGetsPastACrossSignedRootCopy(t *testing.T) {
|
|
bun := testenv.Bun(t)
|
|
sandbox(t)
|
|
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy",
|
|
"NODE_EXTRA_CA_CERTS", "NODE_USE_SYSTEM_CA", "NODE_TLS_REJECT_UNAUTHORIZED", "SSL_CERT_FILE", "SSL_CERT_DIR"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
t.Setenv("NO_PROXY", "127.0.0.1,localhost")
|
|
c := newCrossChain(t)
|
|
old := systemCAFile
|
|
t.Cleanup(func() { systemCAFile = old })
|
|
fetch := `try { const r = await fetch(` + strconv.Quote(c.url) + `); console.log(await r.text()) } catch (e) { console.log("refused", e?.code) }`
|
|
try := func(t *testing.T, store [][]byte, want bool) {
|
|
t.Helper()
|
|
systemCAFile = func() string { return rootsFile(store) }
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"name":"p","private":true}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := bunCommand(ctx, bun, dir, "-e", fetch).CombinedOutput()
|
|
if got := err == nil && strings.Contains(string(out), "through"); got != want {
|
|
t.Errorf("Bun's fetch got through: %v, want %v (%v)\n%s", got, want, err, out)
|
|
}
|
|
}
|
|
t.Run("the cross-signed copy first", func(t *testing.T) { try(t, [][]byte{c.cross, c.root}, true) })
|
|
t.Run("with its issuer", func(t *testing.T) { try(t, [][]byte{c.cross, c.top}, true) })
|
|
t.Run("the self-signed one alone", func(t *testing.T) { try(t, [][]byte{c.root}, true) })
|
|
t.Run("only the cross-signed copy", func(t *testing.T) { try(t, [][]byte{c.cross}, false) })
|
|
t.Run("neither", func(t *testing.T) { try(t, [][]byte{c.top}, false) })
|
|
}
|
|
|
|
// A plugin's request whose server's chain Bun refuses says so, with the
|
|
// code and the host, after Bun's own words: "unable to get issuer
|
|
// certificate" alone read like the vendor's fault (youyo.p_10001).
|
|
func TestPluginTLSRefusalSaysWhose(t *testing.T) {
|
|
testenv.Bun(t)
|
|
sandbox(t)
|
|
for _, k := range []string{"HTTPS_PROXY", "https_proxy", "HTTP_PROXY", "http_proxy", "ALL_PROXY", "all_proxy",
|
|
"NODE_EXTRA_CA_CERTS", "NODE_USE_SYSTEM_CA", "SSL_CERT_FILE", "SSL_CERT_DIR"} {
|
|
t.Setenv(k, "")
|
|
}
|
|
t.Setenv("NO_PROXY", "127.0.0.1,localhost")
|
|
c := newCrossChain(t)
|
|
old := systemCAFile
|
|
t.Cleanup(func() { systemCAFile = old })
|
|
// the cross-signed copy alone: Bun's words are the reporter's
|
|
systemCAFile = func() string { return writeCAFile(pemOf(c.cross)) }
|
|
Restart()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
abs, _ := filepath.Abs("testdata/fake/index.js")
|
|
if _, err := Add(ctx, abs); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Providers(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := APIKey(ctx, "fakeco", 0, nil, "k1", NewAccount); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
res, err := Fetch(ctx, FetchRequest{Provider: "fakeco", Model: "fake-1", NPM: "@ai-sdk/openai-compatible",
|
|
URL: c.url + "/v1/chat/completions", Method: "POST", Body: []byte(`{}`)})
|
|
if err == nil {
|
|
res.Body.Close()
|
|
t.Fatalf("got %d, want the chain refused", res.StatusCode)
|
|
}
|
|
if !strings.Contains(err.Error(), "unable to get issuer certificate (UNABLE_TO_GET_ISSUER_CERT: the plugin host's TLS didn't verify 127.0.0.1's certificate chain against the roots it trusts)") {
|
|
t.Errorf("err = %v, want Bun's words, then the code and whose chain", err)
|
|
}
|
|
}
|
|
|
|
func pemOf(ders ...[]byte) []byte {
|
|
var b []byte
|
|
for _, d := range ders {
|
|
b = append(b, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: d})...)
|
|
}
|
|
return b
|
|
}
|