明霞 (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.
143 lines
4.4 KiB
Go
143 lines
4.4 KiB
Go
package plugin
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/sha256"
|
|
"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
|
|
}
|
|
certs := systemRoots()
|
|
if len(certs) == 0 {
|
|
return
|
|
}
|
|
var b bytes.Buffer
|
|
for _, der := range certs {
|
|
_ = pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: der})
|
|
}
|
|
systemCAPath = writeCAFile(b.Bytes())
|
|
})
|
|
return systemCAPath
|
|
}
|
|
|
|
// 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
|
|
}
|