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).
124 lines
4.0 KiB
Go
124 lines
4.0 KiB
Go
package netproxy
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"errors"
|
|
"io"
|
|
"log"
|
|
"math/big"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/mtls"
|
|
)
|
|
|
|
func pinServer(t *testing.T, answer string) *httptest.Server {
|
|
t.Helper()
|
|
s := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, answer) }))
|
|
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
|
|
}
|
|
|
|
// A pinned server certificate (Zhangzhangi on X) is taken at the hosts it
|
|
// is given for, unchecked otherwise: another host is checked as before,
|
|
// another certificate at a pinned host is refused, a certificate a
|
|
// trusted CA signed included, and a request made for another provider
|
|
// (With) doesn't carry the pin.
|
|
func TestPinOnlyThatCertificate(t *testing.T) {
|
|
own := pinServer(t, "own")
|
|
other := pinServer(t, "other")
|
|
d := Dispatch(&http.Transport{Proxy: Func, ForceAttemptHTTP2: true}).(*dispatch)
|
|
t.Cleanup(d.CloseIdleConnections)
|
|
c := &http.Client{Transport: d}
|
|
bg := context.Background()
|
|
pin := CertSHA256(own.Certificate())
|
|
|
|
if _, err := body(t, c, bg, own.URL); !certRefused(err) {
|
|
t.Fatalf("not pinned: %v", err)
|
|
}
|
|
ctx := WithClientCert(bg, "", "", "", ColonHex(pin), own.URL+"/v1")
|
|
if got, err := body(t, c, ctx, own.URL+"/v1/models"); err != nil || got != "own" {
|
|
t.Fatalf("pinned: %q, %v", got, err)
|
|
}
|
|
if _, err := body(t, c, ctx, other.URL); !certRefused(err) {
|
|
t.Fatalf("another host under the same ctx: %v", err)
|
|
}
|
|
if _, err := body(t, c, With(ctx, ""), own.URL); !certRefused(err) {
|
|
t.Fatalf("made for another provider: %v", err)
|
|
}
|
|
var mis *PinMismatchError
|
|
wrong := WithClientCert(bg, "", "", "", pin, other.URL)
|
|
if _, err := body(t, c, wrong, other.URL); !errors.As(err, &mis) || mis.Got != CertSHA256(other.Certificate()) {
|
|
t.Fatalf("another certificate at a pinned host: %v", err)
|
|
}
|
|
|
|
// a server the given CA signs, pinned to another certificate: the pin
|
|
// wins over the CA
|
|
st := mtls.Store{Dir: t.TempDir()}
|
|
signed := mtlsServer(t, st, false, nil)
|
|
_, _, ca := device(t, st, "x")
|
|
if _, err := body(t, c, WithClientCert(bg, "", "", ca, "", signed.URL), signed.URL); err != nil {
|
|
t.Fatalf("CA alone: %v", err)
|
|
}
|
|
if _, err := body(t, c, WithClientCert(bg, "", "", ca, pin, signed.URL), signed.URL); !errors.As(err, &mis) {
|
|
t.Fatalf("CA and another pin: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalPin(t *testing.T) {
|
|
h := strings.Repeat("0a", 32)
|
|
for in, want := range map[string]string{
|
|
h: h,
|
|
strings.ToUpper(h): h,
|
|
ColonHex(h): h,
|
|
"sha256:" + ColonHex(h): h,
|
|
" " + strings.ToUpper(h) + "\n": h,
|
|
"abcd": "",
|
|
strings.Repeat("zz", 32): "",
|
|
"": "",
|
|
} {
|
|
if got := NormalPin(in); got != want {
|
|
t.Errorf("NormalPin(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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}
|
|
}
|