Files
magpie/internal/provider/server_pin_test.go
T
yetone 8016205e3f provider: trust a self-signed server's certificate by fingerprint, for Remote magpie and any https address of your own (Zhangzhangi on X)
Zhangzhangi on X couldn't add a Remote magpie at a domain with a
self-signed certificate ("x509: "api.magpie.local" certificate is not
standards compliant") and asked for a skip-verification option. A
follow-up asked for the same on other self-signed hosts.

This adds a pin (trust on first use), not a blind skip. It is off by
default and kept per provider:
- Advanced → Server certificate → "Trust its certificate…" fetches the
  certificate the server shows (provider/peekcert) and lists its SHA-256,
  subject, issuer and expiry. "Trust this certificate" keeps the
  fingerprint as the provider's serverPin.
- From then on only that certificate is accepted (netproxy pinTLS:
  InsecureSkipVerify plus VerifyConnection comparing the leaf's DER
  SHA-256). Another one fails with PinMismatchError, saying the server
  showed a certificate other than the one trusted.
- The editor shows a red "Certificate not verified: only this one is
  accepted" with the fingerprint, and "Verify it again" drops the pin.
- The pin is set only on the provider's own hosts (provider.PinHosts):
  - every address of Remote magpie and of a provider added by URL;
  - for a preset (Ollama, LM Studio, OpenAI at your own box), only https
    addresses whose host is not one of the preset's or its regions' own
    hosts. A vendor's host never takes a pin.
  - Never for an account, Vertex or a subscription. Save refuses one
    elsewhere.
- The client certificate and Server CA still go only to the providers
  that took them before (TakesClientCert). A preset gets the pin alone.
- The editor's certificate rows (Client certificate and the new Server
  certificate) are drawn by a lazily imported tlscerts.js. The Client
  certificate row moved out of app.js with it, so the core scripts stay
  under TestCoreScriptBudget without a raise.

Run (Go 1.26.8):
- WSL magpie-desktop, before the rebase:
  - go test -tags nogui -count=1 ./internal/netproxy ./internal/provider
    ./internal/gui all ok;
  - GOOS=darwin -tags nogui vet of gui fails on traywindows_darwin_test.go's
    undefined trayImagePNG, the same at the base (a cgo-only helper).
- mote and mica, after the rebase on ab5824aee:
  - gofmt -l clean; node --check app.js;
  - go vet darwin (cgo), GOOS=linux -tags nogui, GOOS=windows: ok;
  - go test ./internal/netproxy ./internal/provider: ok.
- mote, at the final tree: node --check app.js and tlscerts.js; go test
  ./internal/gui -run 'Budget|ServerPin|InsecureTLS|ClientCert|OnDemand|Lazy'
  ok (TestCoreScriptBudget under 1,533,802) at the base ab5824aee.
  Rebased on fe5a0cf6d, TestCoreScriptBudget is red, as it is on that
  main alone: main's core is 1,534,708 (16ed1a952 took it to 1,535,318);
  this change takes it to 1,534,484, 224 bytes less (#5918, commented).
  The rest of the run above passes at the pushed head.
- mica, GUI files at the final tree, both engines inside each file:
  - green: provider-server-pin (54), mtls-settings (16),
    provider-insecure-tls (60), provider-socket (60), provider-http1 (60),
    i18n-keys, i18n-en-no-cjk, editor-advanced-align, provider-advanced-fold
    (50);
  - old-webkit: headerpresets.js not loaded (#6030) and Safari 15's :has()
    (#5919), both on main too;
  - menu-scroll: 24 of 24 red on /api/update/using, the same alone at
    origin/main fe5a0cf6d (#5982, commented).
  - Earlier flakes of provider-http1 beside another session's suite:
    #6159.
- Broken by hand, each red:
  - presets never take a pin: TestServerPinSaveChecks,
    TestServerPinPresetAtOwnServer, TestServerPinNoVendorHost;
  - vendor hosts pinned (the own-host check dropped):
    TestServerPinNoVendorHost;
  - client cert and CA sent to presets: TestServerPinPresetAtOwnServer;
  - the pin never compared: TestPinOnlyThatCertificate,
    TestServerPinRemoteMagpie.
  Restored: both packages ok.

Not tried:
- a real self-signed domain from the macOS app (httptest servers with
  their own certificates only);
- the Responses WebSocket dial (read only: it takes the same netproxy
  transport);
- go test ./internal/cli and the whole GUI suite before the tag (the
  full Go suite runs after it, under suite.lock).
2026-11-11 21:13:43 +08:00

257 lines
8.7 KiB
Go

package provider
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"io"
"log"
"math/big"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/netproxy"
)
// Zhangzhangi on X: a Remote magpie on a domain of its own with a
// self-signed certificate. Its editor peeks at the certificate, the user
// trusts it (ServerPin), and then its models and its test go through;
// before, both fail on the certificate. A pin of another certificate is
// refused at the handshake, and another server at the same pin too.
func TestServerPinRemoteMagpie(t *testing.T) {
azureHome(t)
dialsOut(t)
srv := selfSignedVendor(t)
other := freshVendor(t)
ctx := context.Background()
if err := Save(Provider{ID: "home", Preset: RemoteMagpiePreset, Key: "remote-key", Chat: srv.URL}); err != nil {
t.Fatal(err)
}
p, _ := Find("home")
if _, err := p.List(ctx); !certError(err) {
t.Fatalf("not pinned: want a certificate error, got %v", err)
}
if rs := p.Test(ctx); len(rs) == 0 || rs[0].OK {
t.Fatalf("its test not pinned: %+v", rs)
}
seen, err := p.PeekServerCert(ctx)
if err != nil {
t.Fatal(err)
}
if want := netproxy.CertSHA256(srv.Certificate()); seen.SHA256 != want {
t.Fatalf("peeked %s, the server's is %s", seen.SHA256, want)
}
if seen.Checks || seen.Why == "" {
t.Fatalf("a self-signed certificate checks? %+v", seen)
}
p.ServerPin = netproxy.ColonHex(seen.SHA256) // as people copy it
if err := Save(*p); err != nil {
t.Fatal(err)
}
p, _ = Find("home")
if p.ServerPin != seen.SHA256 {
t.Fatalf("pin kept as %q", p.ServerPin)
}
if ms, err := p.List(ctx); err != nil || len(ms) != 1 {
t.Fatalf("pinned: %v %v", ms, err)
}
if rs := p.Test(ctx); len(rs) == 0 || !rs[0].OK {
t.Fatalf("its test pinned: %+v", rs)
}
// the same pin, another server answering at the provider's address
moved := *p
for _, f := range []*string{&moved.Chat, &moved.Responses, &moved.Anthropic, &moved.Gemini, &moved.Catalog, &moved.Decide, &moved.ModelsURL} {
*f = strings.ReplaceAll(*f, srv.URL, other.URL)
}
const other1 = "showed a certificate other than the one trusted"
if _, err := moved.List(ctx); err == nil || !strings.Contains(err.Error(), other1) {
t.Fatalf("another certificate at a pinned host: %v", err)
}
// a pin of another certificate
p.ServerPin = netproxy.CertSHA256(other.Certificate())
if err := Save(*p); err != nil {
t.Fatal(err)
}
p, _ = Find("home")
if _, err := p.List(ctx); err == nil || !strings.Contains(err.Error(), other1) || !strings.Contains(err.Error(), "trust the new one") {
t.Fatalf("pinned to another certificate: %v", err)
}
// taken off: checked as before
p.ServerPin = ""
if err := Save(*p); err != nil {
t.Fatal(err)
}
p, _ = Find("home")
if _, err := p.List(ctx); !certError(err) || strings.Contains(err.Error(), other1) {
t.Fatalf("pin taken off: %v", err)
}
}
// What Save refuses: a pin that isn't a SHA-256, and one on a preset's
// provider that takes no certificate of its own.
func TestServerPinSaveChecks(t *testing.T) {
azureHome(t)
good := strings.Repeat("ab", 32)
for name, c := range map[string]struct {
p Provider
ok bool
}{
"own": {Provider{ID: "home", Name: "Home", Chat: "https://home.example/v1", Key: "k", Models: []string{"m"}, ServerPin: good}, true},
"colons": {Provider{ID: "home2", Name: "Home2", Chat: "https://home2.example/v1", Key: "k", Models: []string{"m"}, ServerPin: "SHA256:" + netproxy.ColonHex(good)}, true},
"short": {Provider{ID: "home3", Name: "Home3", Chat: "https://home3.example/v1", Key: "k", Models: []string{"m"}, ServerPin: "abcd"}, false},
"not hex": {Provider{ID: "home4", Name: "Home4", Chat: "https://home4.example/v1", Key: "k", Models: []string{"m"}, ServerPin: strings.Repeat("zz", 32)}, false},
"a preset's": {Provider{ID: "openrouter", Preset: "openrouter", Key: "k", ServerPin: good}, false},
"a preset at home": {Provider{ID: "lmstudio", Preset: "lmstudio", Chat: "https://box.lan:1234/v1", ServerPin: good}, true},
} {
err := Save(c.p)
if (err == nil) != c.ok {
t.Errorf("%s: %v", name, err)
}
if c.ok {
if p, _ := Find(c.p.ID); p == nil || p.ServerPin != good {
t.Errorf("%s: kept as %+v", name, p)
}
}
}
}
// A preset pointed at a server of the user's own (LM Studio on another
// computer behind https) trusts its certificate by fingerprint too
// (Zhangzhangi on X: 建议对别的自签名域名也生效), with no client
// certificate sent there.
func TestServerPinPresetAtOwnServer(t *testing.T) {
azureHome(t)
dialsOut(t)
srv := selfSignedVendor(t)
ctx := context.Background()
if err := Save(Provider{ID: "lmstudio", Preset: "lmstudio", Chat: srv.URL + "/v1"}); err != nil {
t.Fatal(err)
}
p, _ := Find("lmstudio")
if !p.TakesServerPin() || len(p.PinHosts()) != 1 {
t.Fatalf("a preset at the user's own https host takes no pin: %v", p.PinHosts())
}
if _, err := p.List(ctx); !certError(err) {
t.Fatalf("not pinned: want a certificate error, got %v", err)
}
seen, err := p.PeekServerCert(ctx)
if err != nil {
t.Fatal(err)
}
p.ServerPin = seen.SHA256
if err := Save(*p); err != nil {
t.Fatal(err)
}
p, _ = Find("lmstudio")
if ms, err := p.List(ctx); err != nil || len(ms) != 1 {
t.Fatalf("pinned preset: %v %v", ms, err)
}
// a client certificate stays one for a provider that takes it
p.ClientCert, p.ServerCA = "/nowhere/client.pem", "/nowhere/ca.pem"
if _, err := p.List(ctx); err != nil {
t.Fatalf("a preset's client certificate was sent: %v", err)
}
}
// Every preset at the vendor's own addresses, its regions' too, takes no
// pin: a vendor's host is never trusted by a fingerprint.
func TestServerPinNoVendorHost(t *testing.T) {
for _, pr := range Presets() {
ps := []Provider{{Preset: pr.ID, Chat: pr.Chat, Responses: pr.Responses, Anthropic: pr.Anthropic, Decide: pr.Decide}}
for _, r := range pr.Regions {
ps = append(ps, Provider{Preset: pr.ID, Chat: r.Chat, Responses: r.Responses, Anthropic: r.Anthropic})
}
for _, p := range ps {
if p.IsRemoteMagpie() {
continue
}
if hs := p.PinHosts(); len(hs) > 0 {
t.Errorf("%s pins its vendor's %v", pr.ID, hs)
}
}
}
if (Provider{Preset: "openai", Chat: "http://box.lan/v1"}).TakesServerPin() {
t.Error("an http:// address has no certificate to pin")
}
if !(Provider{Preset: "openai", Chat: "https://box.lan/v1"}).TakesServerPin() {
t.Error("a preset at the user's own https host takes no pin")
}
}
// A server signed by the provider's own Server CA checks already, so the
// peek offers nothing to pin.
func TestServerPinPeekTakesItsCA(t *testing.T) {
azureHome(t)
dialsOut(t)
srv := selfSignedVendor(t)
ca := filepath.Join(t.TempDir(), "ca.pem")
if err := os.WriteFile(ca, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: srv.Certificate().Raw}), 0o600); err != nil {
t.Fatal(err)
}
p := Provider{ID: "home", Preset: RemoteMagpiePreset, Key: "remote-key", Chat: srv.URL}
seen, err := p.PeekServerCert(context.Background())
if err != nil || seen.Checks {
t.Fatalf("without its CA: %+v %v", seen, err)
}
p.ServerCA = ca
if seen, err = p.PeekServerCert(context.Background()); err != nil || !seen.Checks || seen.Why != "" {
t.Fatalf("with its CA: %+v %v", seen, err)
}
}
// freshCert is a self-signed certificate of its own for 127.0.0.1:
// httptest's servers all show one built-in certificate, so a server that
// must show another needs this.
func freshCert(t *testing.T) tls.Certificate {
t.Helper()
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(time.Now().UnixNano()),
Subject: pkix.Name{CommonName: "api.magpie.local"},
DNSNames: []string{"api.magpie.local"},
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1)},
NotBefore: time.Now().Add(-time.Hour),
NotAfter: time.Now().Add(24 * time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
if err != nil {
t.Fatal(err)
}
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key}
}
// freshVendor is selfSignedVendor with a certificate of its own.
func freshVendor(t *testing.T) *httptest.Server {
t.Helper()
s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{"data":[{"id":"qwen3-32b"}]}`)
}))
s.Config.ErrorLog = log.New(io.Discard, "", 0)
s.TLS = &tls.Config{Certificates: []tls.Certificate{freshCert(t)}}
s.StartTLS()
t.Cleanup(s.Close)
return s
}