Files
magpie/internal/gateway/mtls_test.go
T
yetone efb75ad1f0 gateway: client certificates (mTLS), and Remote Magpie can show one (hhdaxia on X, #5596)
hhdaxia on X asked for mTLS so a shared magpie isn't open to the internet
on a key alone; clxsh's #5596 asks Remote Magpie to send a client
certificate and key (PEM) to an Nginx doing mTLS, the server still
verified, the Gateway Key still sent, for models, the test and streaming.

Server side (magpie's own CA, internal/mtls):
- Settings -> Network and sharing -> Require a client certificate, or
  `magpie mtls on|off|issue <device>|list|revoke`. Issue saves
  client.pem, client-key.pem and ca.pem to ~/Downloads/magpie-<device>;
  the key is kept nowhere else.
- With it on, a peer that isn't this computer or a WSL distro must finish
  the TLS handshake with a certificate the CA issued and hasn't revoked
  (RequireAndVerifyClientCert + VerifyConnection -> Admits). The TLS
  config is chosen per connection, so the switch needs no relisten;
  without --tls-cert the server certificate is made by the CA in memory.
- lanGuard and ClientCertGuard (magpie web's page) refuse 403 what still
  arrives without a verified one: plain HTTP, a proxy or tunnel on
  loopback (unless MAGPIE_TRUST_PROXY=1), the page port, a connection
  from before the switch, a certificate revoked since its handshake.
- Loopback, this computer's own address and a WSL agent with its key are
  answered as before.
- A page open from another computer can't shut itself out: without a
  certificate it can't turn mTLS on, and it can't revoke or reissue the
  certificate it came with.

Client side: a provider with a base URL of the user's own (Remote
Magpie included) takes ClientCert, ClientKey and ServerCA paths (editor:
Advanced -> Client certificate). netproxy.WithClientCert shows them to
that provider's own hosts only, on a transport per identity keyed on the
files' mtimes; through an https:// proxy such a request is refused. Save
refuses half a pair, a pair that doesn't match and a CA that isn't one.

Run (on mote, at 4024df0f9, this commit before its rebase):
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows.
- go test -count=1 ./internal/mtls ./internal/netproxy ./internal/provider
  ./internal/settings ./internal/settingsapi ./internal/cli ./internal/gui
  ./internal/gateway: ok (gateway 893s). The first run had three reds of
  this change, each green at base and fixed here:
  TestEveryRouteHasACommandOrIsNamed (the mtls routes now name `magpie
  mtls`), TestSettingsSaveKeepsWhatItDoesNotSend (MTLS kept), and
  TestGoroutineDumpAnswersWhileRequestsWait (mtlsRefuses asks whether the
  peer is remote before reading settings, so a local request never waits
  on the settings lock). TestHandedOffGatewayAnswersAConnectionNotedLate
  was reset because ServeTLS now always wraps the listener: tlsListener
  hands a connection it took as Close came to the server instead of
  dropping it. -count=3 of these and every TLS/MTLS/Handover/Relisten test
  ok.
- Every route without a certificate, with a foreign one, a revoked one
  and a good one: TestMTLSOtherComputerHandshake, TestMTLSGuardEveryRoute,
  TestMTLSThisComputerAndProxies, TestMTLSToggleWithoutRelisten,
  TestMTLSRevokeMidConnection, TestClientCertGuardPage. Remote Magpie
  against an httptest server asking RequireAndVerifyClientCert: models
  and the connection test fail without the certificate and pass with it
  (TestRemoteMagpieClientCert).
- GUI: the 92 files near the change (settings-*, provider-*, editor-*,
  lan-*, i18n*, allow-ips, remote-*, mtls-settings new) in Chromium and
  WebKit: 2904 pass, 74 fail, all in nine files red the same way at base
  0791c91c5 and listed in #5872 (editor-thumbs-batched also #5886;
  provider-fetch-again #5057 #5232; provider-many-keys, -models-rest,
  -picks-stay, -typed #5475 #4396 #5233 #4673; remote-magpie #4398;
  settings-hints, settings-lines-wrap run alone at base and here: the
  same 8 reds). The whole GUI suite and the whole Go suite were not run:
  mote's suite lock had about 15 runs queued at load 31-40.

Rebased on 031938ca9 (one conflict, internal/gui/api.go's route list,
both kept) and run again there: gofmt clean, vet on three systems, go
test ./internal/{gui,cli,mtls,netproxy,provider,settings,settingsapi} ok
(cli with the next commit's line), the gateway's TLS, MTLS, Handover,
Relisten, HandedOff, GoroutineDump, ClientCert and RemoteMagpie tests ok,
TestCoreScriptBudget ok, and mtls-settings, i18n-keys, allow-ips and
provider-insecure-tls in Chromium and WebKit: 143 pass, 0 fail.

Broken by hand, each red:
- lanGuard without refuseWithoutClientCert: TestMTLSGuardEveryRoute,
  TestMTLSThisComputerAndProxies, TestMTLSRevokeMidConnection.
- VerifyClientCertIfGiven and no Admits: TestMTLSOtherComputerHandshake.
- netproxy.With keeping another provider's certificate:
  TestClientCertOnlyItsHost.
- The page-lockout and own-serial guards in internal/gui/mtls.go:
  TestSettingsMTLSPageLockout.

Not tried: a real Nginx doing mTLS; a Windows or Linux run (vet only);
installing the certificate in a browser (there is no .p12 yet); a
proxy that checks certificates itself with MAGPIE_TRUST_PROXY=1.
2026-11-10 14:27:21 +08:00

360 lines
13 KiB
Go

package gateway
import (
"bufio"
"crypto/tls"
"crypto/x509"
"io"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/mtls"
"github.com/yetone/magpie/internal/settings"
)
// mtlsHome is a sandbox HOME with magpie shared, a gateway key, and mTLS
// on or off; it returns the key.
func mtlsHome(t *testing.T, on bool) string {
t.Helper()
fresh(t)
t.Setenv("MAGPIE_ADDR", "0.0.0.0:3499")
t.Setenv(AllowIPsEnv, "")
t.Setenv(TLSCertEnv, "")
t.Setenv(TLSKeyEnv, "")
key, err := access.Update("add-key", access.Change{Name: "Laptop"})
if err != nil {
t.Fatal(err)
}
if _, _, err := MTLSStore().CA(); err != nil {
t.Fatal(err)
}
setMTLS(t, on)
return key
}
func setMTLS(t *testing.T, on bool) {
t.Helper()
if err := settings.SetMTLS(on); err != nil {
t.Fatal(err)
}
if MTLSOn() != on {
t.Fatalf("MTLSOn() = %v after SetMTLS(%v)", !on, on)
}
}
// issued is a client certificate of st's for name, as a TLS client
// shows it.
func issued(t *testing.T, st mtls.Store, name string) tls.Certificate {
t.Helper()
iss, err := st.Issue(name)
if err != nil {
t.Fatal(err)
}
c, err := tls.X509KeyPair(iss.Cert, iss.Key)
if err != nil {
t.Fatal(err)
}
return c
}
// mtlsClient trusts magpie's CA for the server and shows cert (none when
// nil); keepAlive keeps one connection for every request.
func mtlsClient(t *testing.T, cert *tls.Certificate, keepAlive bool) *http.Client {
t.Helper()
pool, err := MTLSStore().Pool()
if err != nil {
t.Fatal(err)
}
cfg := &tls.Config{RootCAs: pool}
if cert != nil {
cfg.Certificates = []tls.Certificate{*cert}
}
tr := &http.Transport{TLSClientConfig: cfg, DisableKeepAlives: !keepAlive}
t.Cleanup(tr.CloseIdleConnections)
return &http.Client{Timeout: 5 * time.Second, Transport: tr}
}
// asFrom has every request look, to the guards, as if it came from addr:
// the listener judges the connection (fromThisComputer), the guards the
// request, as a WSL distro's or a connection made before mTLS was turned
// on is let through the first and judged by the second.
func asFrom(addr string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.RemoteAddr = addr
next.ServeHTTP(w, r)
})
}
// serveMTLSGateway serves the guard over a stub on a listener that sees
// its peers as another computer's (other) or this one's, each request as
// from remote ("" keeps the real loopback peer).
func serveMTLSGateway(t *testing.T, other bool, remote string) string {
t.Helper()
was := fromThisComputer
fromThisComputer = func(net.Conn) bool { return !other }
t.Cleanup(func() { fromThisComputer = was })
srvTLS, err := serverTLS()
if err != nil {
t.Fatal(err)
}
s := New()
s.tls = srvTLS
ln, err := s.listen("127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
var h http.Handler = lanGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "served "+r.URL.Path)
}))
if remote != "" {
h = asFrom(remote, h)
}
srv := &http.Server{Handler: h, ReadHeaderTimeout: 5 * time.Second}
done := make(chan struct{})
go func() { defer close(done); srv.Serve(ln) }()
t.Cleanup(func() { srv.Close(); <-done })
return ln.Addr().String()
}
func mcall(t *testing.T, c *http.Client, method, url, key string, header ...string) (int, string, error) {
t.Helper()
req, _ := http.NewRequest(method, url, strings.NewReader("{}"))
if key != "" {
req.Header.Set("Authorization", "Bearer "+key)
}
for i := 0; i+1 < len(header); i += 2 {
req.Header.Set(header[i], header[i+1])
}
res, err := c.Do(req)
if err != nil {
return 0, "", err
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
return res.StatusCode, string(b), nil
}
// the routes a remote agent calls, each refused the same without a
// certificate (a restriction covers every route that reaches the thing)
var mtlsRoutes = [][2]string{
{"GET", "/v1/models"},
{"POST", "/v1/chat/completions"},
{"POST", "/v1/messages"},
{"POST", "/v1/responses"},
{"GET", "/v1/magpie/quotas"},
{"POST", "/backend-api/codex/responses"},
{"GET", "/api/state"},
{"GET", "/"},
}
// Another computer's connection (#5596, hhdaxia on X): with mTLS on, the
// handshake ends without a client certificate magpie issued — none, one
// another CA signed, one revoked, or plain HTTP — before any HTTP is
// read; one with a good certificate and the gateway key is answered.
func TestMTLSOtherComputerHandshake(t *testing.T) {
key := mtlsHome(t, true)
st := MTLSStore()
addr := serveMTLSGateway(t, true, "")
good := issued(t, st, "laptop")
foreign := issued(t, mtls.Store{Dir: t.TempDir()}, "laptop")
revoked := issued(t, st, "old phone")
if _, err := st.Revoke("old phone"); err != nil {
t.Fatal(err)
}
for name, c := range map[string]*http.Client{
"no certificate": mtlsClient(t, nil, false),
"another CA's": mtlsClient(t, &foreign, false),
"a revoked one": mtlsClient(t, &revoked, false),
"plain HTTP": plain,
} {
scheme := "https://"
if strings.HasPrefix(name, "plain") {
scheme = "http://"
}
code, body, err := mcall(t, c, "GET", scheme+addr+"/v1/models", key)
if err == nil && (code < 400 || strings.Contains(body, "served")) {
t.Errorf("%s: %d %s, want the connection refused", name, code, body)
}
}
c := mtlsClient(t, &good, false)
for _, rt := range mtlsRoutes {
code, body, err := mcall(t, c, rt[0], "https://"+addr+rt[1], key)
if err != nil || code != 200 || body != "served "+rt[1] {
t.Errorf("good certificate, %s %s: %d %q %v", rt[0], rt[1], code, body, err)
}
}
}
// The guards judge each request: one from another machine that reached
// the gateway through a lenient listener (a WSL network's, a connection
// made before mTLS was turned on) is refused on every route without a
// good certificate, the gateway key notwithstanding; with one it still
// needs the key.
func TestMTLSGuardEveryRoute(t *testing.T) {
key := mtlsHome(t, true)
st := MTLSStore()
addr := serveMTLSGateway(t, false, "192.168.1.9:5000")
good := issued(t, st, "laptop")
foreign := issued(t, mtls.Store{Dir: t.TempDir()}, "x")
for _, rt := range mtlsRoutes {
for name, c := range map[string]*http.Client{
"plain HTTP": plain,
"no certificate": mtlsClient(t, nil, false),
} {
scheme := "https://"
if c == plain {
scheme = "http://"
}
code, body, err := mcall(t, c, rt[0], scheme+addr+rt[1], key)
if err != nil || code != http.StatusForbidden || !strings.Contains(body, "client certificate") {
t.Errorf("%s %s, %s: %d %q %v, want 403 saying why", rt[0], rt[1], name, code, body, err)
}
}
// another CA's: the lenient handshake verifies a certificate if
// given, and refuses one that doesn't verify
if code, body, err := mcall(t, mtlsClient(t, &foreign, false), rt[0], "https://"+addr+rt[1], key); err == nil && (code != http.StatusForbidden || strings.Contains(body, "served")) {
t.Errorf("%s %s, another CA's: %d %q", rt[0], rt[1], code, body)
}
c := mtlsClient(t, &good, false)
if code, body, err := mcall(t, c, rt[0], "https://"+addr+rt[1], key); err != nil || code != 200 || body != "served "+rt[1] {
t.Errorf("%s %s, good certificate and key: %d %q %v", rt[0], rt[1], code, body, err)
}
}
if code, body, _ := mcall(t, mtlsClient(t, &good, false), "POST", "https://"+addr+"/v1/messages", ""); code != http.StatusUnauthorized && code != http.StatusForbidden || strings.Contains(body, "served") {
t.Errorf("good certificate without the key: %d %q, want it refused", code, body)
}
}
// This computer is answered as before, over plain HTTP and without a
// key; a request through a proxy or tunnel on loopback is another
// machine's, and is refused without a certificate even with the key,
// unless MAGPIE_TRUST_PROXY says the proxy checks its clients.
func TestMTLSThisComputerAndProxies(t *testing.T) {
key := mtlsHome(t, true)
addr := serveMTLSGateway(t, false, "")
if code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", ""); err != nil || code != 200 {
t.Fatalf("loopback, plain: %d %q %v", code, body, err)
}
if code, body, err := mcall(t, mtlsClient(t, nil, false), "GET", "https://"+addr+"/v1/models", ""); err != nil || code != 200 {
t.Fatalf("loopback, TLS without a certificate: %d %q %v", code, body, err)
}
for _, h := range []string{"X-Forwarded-For", "Cf-Connecting-IP", "Forwarded"} {
code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", key, h, "203.0.113.5")
if err != nil || code != http.StatusForbidden || !strings.Contains(body, "proxy") {
t.Errorf("proxied (%s) with key: %d %q %v", h, code, body, err)
}
}
code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", key, PagePortHeader, "1")
if err != nil || code != http.StatusForbidden || !strings.Contains(body, "client certificate") {
t.Errorf("magpie web's page port with key: %d %q %v", code, body, err)
}
t.Setenv("MAGPIE_TRUST_PROXY", "1")
if code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", "", "X-Forwarded-For", "203.0.113.5"); err != nil || code != 200 {
t.Errorf("a trusted proxy: %d %q %v", code, body, err)
}
}
// Turning mTLS on or off takes effect at the next connection, without
// listening again: off, another computer is answered over plain HTTP with
// its key, as before; on, plain HTTP is refused and a certificate is
// asked for; off again, plain HTTP is answered.
func TestMTLSToggleWithoutRelisten(t *testing.T) {
key := mtlsHome(t, false)
addr := serveMTLSGateway(t, true, "")
good := issued(t, MTLSStore(), "laptop")
if code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", key); err != nil || code != 200 {
t.Fatalf("off: %d %q %v", code, body, err)
}
setMTLS(t, true)
if code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", key); err == nil && code < 400 {
t.Fatalf("on, plain: %d %q, want it refused", code, body)
}
if code, body, err := mcall(t, mtlsClient(t, &good, false), "GET", "https://"+addr+"/v1/models", key); err != nil || code != 200 {
t.Fatalf("on, certificate: %d %q %v", code, body, err)
}
setMTLS(t, false)
if code, body, err := mcall(t, plain, "GET", "http://"+addr+"/v1/models", key); err != nil || code != 200 {
t.Fatalf("off again: %d %q %v", code, body, err)
}
}
// A certificate revoked while its connection stays open is refused at
// the connection's next request, not only its next handshake.
func TestMTLSRevokeMidConnection(t *testing.T) {
key := mtlsHome(t, true)
st := MTLSStore()
addr := serveMTLSGateway(t, false, "192.168.1.9:5000")
good := issued(t, st, "laptop")
pool, _ := st.Pool()
conn, err := tls.Dial("tcp", addr, &tls.Config{RootCAs: pool, Certificates: []tls.Certificate{good}, ServerName: "localhost"})
if err != nil {
t.Fatal(err)
}
defer conn.Close()
br := bufio.NewReader(conn)
ask := func() (int, string) {
t.Helper()
io.WriteString(conn, "GET /v1/models HTTP/1.1\r\nHost: x\r\nAuthorization: Bearer "+key+"\r\n\r\n")
res, err := http.ReadResponse(br, nil)
if err != nil {
t.Fatal(err)
}
b, _ := io.ReadAll(res.Body)
res.Body.Close()
return res.StatusCode, string(b)
}
if code, body := ask(); code != 200 {
t.Fatalf("before: %d %q", code, body)
}
if _, err := st.Revoke("laptop"); err != nil {
t.Fatal(err)
}
if code, body := ask(); code != http.StatusForbidden || !strings.Contains(body, "revoked") {
t.Fatalf("after revoke, same connection: %d %q", code, body)
}
}
// magpie web's page refuses another machine's request without a good
// certificate (ClientCertGuard), and serves this computer's.
func TestClientCertGuardPage(t *testing.T) {
mtlsHome(t, true)
good := issued(t, MTLSStore(), "laptop")
leaf, _ := x509.ParseCertificate(good.Certificate[0])
h := ClientCertGuard(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "page") }))
call := func(from string, cs *tls.ConnectionState) int {
r := httptest.NewRequest("GET", "/", nil)
r.RemoteAddr, r.TLS = from, cs
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w.Code
}
verified := &tls.ConnectionState{PeerCertificates: []*x509.Certificate{leaf}, VerifiedChains: [][]*x509.Certificate{{leaf}}}
unverified := &tls.ConnectionState{PeerCertificates: []*x509.Certificate{leaf}}
for _, c := range []struct {
from string
cs *tls.ConnectionState
want int
}{
{"127.0.0.1:5000", nil, 200},
{"192.168.1.9:5000", nil, 403},
{"192.168.1.9:5000", &tls.ConnectionState{}, 403},
{"192.168.1.9:5000", unverified, 403},
{"192.168.1.9:5000", verified, 200},
} {
if got := call(c.from, c.cs); got != c.want {
t.Errorf("%s %v: %d, want %d", c.from, c.cs != nil, got, c.want)
}
}
setMTLS(t, false)
if got := call("192.168.1.9:5000", nil); got != 200 {
t.Errorf("mTLS off: %d", got)
}
}