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 base0791c91c5and 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 on031938ca9(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.
195 lines
7.4 KiB
Go
195 lines
7.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"encoding/pem"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func mtlsMux(t *testing.T, w Windows) func(r *http.Request) (int, settingsJSON, string) {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
mtlsRoutes(mux, w)
|
|
return func(r *http.Request) (int, settingsJSON, string) {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
mux.ServeHTTP(rec, r)
|
|
var s settingsJSON
|
|
json.Unmarshal(rec.Body.Bytes(), &s)
|
|
return rec.Code, s, rec.Body.String()
|
|
}
|
|
}
|
|
|
|
func mtlsReq(path, body string) *http.Request {
|
|
return httptest.NewRequest("POST", path, strings.NewReader(body))
|
|
}
|
|
|
|
// leafFile reads the certificate an issue saved.
|
|
func leafFile(t *testing.T, path string) *x509.Certificate {
|
|
t.Helper()
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p, _ := pem.Decode(b)
|
|
c, err := x509.ParseCertificate(p.Bytes)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Settings → Network and sharing (hhdaxia on X, #5596): turning mTLS on
|
|
// makes the CA, Issue saves a device's three files to Downloads and lists
|
|
// it, Revoke takes it off the list and out of the admitted.
|
|
func TestSettingsMTLSIssueRevoke(t *testing.T) {
|
|
h := sandboxHome(t)
|
|
os.MkdirAll(filepath.Join(h, "Downloads"), 0o755)
|
|
post := mtlsMux(t, folderOnly{})
|
|
|
|
code, s, out := post(mtlsReq("/api/settings/mtls", `{"on":true}`))
|
|
if code != 200 || !s.MTLS || !settings.Shared().MTLS {
|
|
t.Fatalf("on: %d %s", code, out)
|
|
}
|
|
if _, _, err := gateway.MTLSStore().CA(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
code, s, out = post(mtlsReq("/api/settings/mtls/issue", `{"name":"laptop"}`))
|
|
if code != 200 || len(s.MTLSCerts) != 1 || s.MTLSCerts[0].Name != "laptop" || s.MTLSSaved == "" {
|
|
t.Fatalf("issue: %d %s", code, out)
|
|
}
|
|
dir := filepath.Join(h, "Downloads", "magpie-laptop")
|
|
for _, f := range []string{"client.pem", "client-key.pem", "ca.pem"} {
|
|
if _, err := os.Stat(filepath.Join(dir, f)); err != nil {
|
|
t.Fatalf("not saved: %v", err)
|
|
}
|
|
}
|
|
leaf := leafFile(t, filepath.Join(dir, "client.pem"))
|
|
if err := gateway.MTLSStore().Admits(leaf); err != nil {
|
|
t.Fatalf("issued, not admitted: %v", err)
|
|
}
|
|
if code, _, _ := post(mtlsReq("/api/settings/mtls/issue", `{"name":"a/b"}`)); code == 200 {
|
|
t.Fatal("a name with a slash issued")
|
|
}
|
|
code, s, out = post(mtlsReq("/api/settings/mtls/revoke", `{"serial":"`+s.MTLSCerts[0].Serial+`"}`))
|
|
if code != 200 || len(s.MTLSCerts) != 0 {
|
|
t.Fatalf("revoke: %d %s", code, out)
|
|
}
|
|
if gateway.MTLSStore().Admits(leaf) == nil {
|
|
t.Fatal("revoked, still admitted")
|
|
}
|
|
if code, s, _ = post(mtlsReq("/api/settings/mtls", `{"on":false}`)); code != 200 || s.MTLS || settings.Shared().MTLS {
|
|
t.Fatal("still on")
|
|
}
|
|
}
|
|
|
|
// A page open from another computer never shuts itself out: without a
|
|
// certificate it can't turn mTLS on; with one it can't revoke that one
|
|
// or issue its name again. The computer magpie runs on can do all three.
|
|
func TestSettingsMTLSPageLockout(t *testing.T) {
|
|
h := sandboxHome(t)
|
|
os.MkdirAll(filepath.Join(h, "Downloads"), 0o755)
|
|
app := mtlsMux(t, folderOnly{})
|
|
app(mtlsReq("/api/settings/mtls", `{"on":true}`))
|
|
_, s, _ := app(mtlsReq("/api/settings/mtls/issue", `{"name":"phone"}`))
|
|
app(mtlsReq("/api/settings/mtls", `{"on":false}`))
|
|
if len(s.MTLSCerts) != 1 {
|
|
t.Fatal("not issued")
|
|
}
|
|
serial := s.MTLSCerts[0].Serial
|
|
leaf := leafFile(t, filepath.Join(h, "Downloads", "magpie-phone", "client.pem"))
|
|
web := mtlsMux(t, webHost{})
|
|
|
|
far := mtlsReq("/api/settings/mtls", `{"on":true}`)
|
|
far.RemoteAddr = "192.168.1.30:50000"
|
|
if code, _, out := web(far); code == 200 || !strings.Contains(out, "without a client certificate") || settings.Shared().MTLS {
|
|
t.Fatalf("a page from another computer without a certificate turned it on: %d %s", code, out)
|
|
}
|
|
near := mtlsReq("/api/settings/mtls", `{"on":true}`)
|
|
near.RemoteAddr = "127.0.0.1:50000"
|
|
if code, _, out := web(near); code != 200 || !settings.Shared().MTLS {
|
|
t.Fatalf("this computer's page: %d %s", code, out)
|
|
}
|
|
// the page that came with phone's certificate
|
|
withCert := func(path, body string) *http.Request {
|
|
r := mtlsReq(path, body)
|
|
r.RemoteAddr = "192.168.1.30:50000"
|
|
r.TLS = &tls.ConnectionState{PeerCertificates: []*x509.Certificate{leaf}, VerifiedChains: [][]*x509.Certificate{{leaf}}}
|
|
return r
|
|
}
|
|
if code, _, out := web(withCert("/api/settings/mtls/revoke", `{"serial":"`+strings.ToUpper(serial)+`"}`)); code == 200 || !strings.Contains(out, "this page came with that certificate") {
|
|
t.Fatalf("revoked its own: %d %s", code, out)
|
|
}
|
|
if code, _, out := web(withCert("/api/settings/mtls/issue", `{"name":" PHONE "}`)); code == 200 || !strings.Contains(out, "this page came with that certificate") {
|
|
t.Fatalf("issued its own name again: %d %s", code, out)
|
|
}
|
|
if gateway.MTLSStore().Admits(leaf) != nil {
|
|
t.Fatal("its certificate was revoked")
|
|
}
|
|
if code, _, out := web(withCert("/api/settings/mtls", `{"on":true}`)); code != 200 {
|
|
t.Fatalf("a page with a good certificate: %d %s", code, out)
|
|
}
|
|
if code, _, out := app(mtlsReq("/api/settings/mtls/revoke", `{"serial":"`+serial+`"}`)); code != 200 {
|
|
t.Fatalf("the app's revoke: %d %s", code, out)
|
|
}
|
|
}
|
|
|
|
// The provider editor's certificate paths (#5596): saved typed with ~,
|
|
// shown back with it, kept by a Save that leaves them out, and cleared by
|
|
// one that sends them empty.
|
|
func TestProviderSaveClientCert(t *testing.T) {
|
|
h := sandboxHome(t)
|
|
os.MkdirAll(filepath.Join(h, "Downloads"), 0o755)
|
|
app := mtlsMux(t, folderOnly{})
|
|
if code, _, out := app(mtlsReq("/api/settings/mtls/issue", `{"name":"laptop"}`)); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(body string) (int, string) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
|
|
return w.Code, w.Body.String()
|
|
}
|
|
base := `{"id":"home","name":"Home","key":"k","chat":"https://home.example:3425/v1","icon":"generic"`
|
|
certs := `"clientCert":"~/Downloads/magpie-laptop/client.pem","clientKey":"~/Downloads/magpie-laptop/client-key.pem","serverCA":"~/Downloads/magpie-laptop/ca.pem"`
|
|
if code, out := post(base + `,"new":true,` + certs + `}`); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
p, _ := provider.Find("home")
|
|
if p.ClientCert != filepath.Join(h, "Downloads", "magpie-laptop", "client.pem") || p.ServerCA == "" {
|
|
t.Fatalf("saved %q %q", p.ClientCert, p.ServerCA)
|
|
}
|
|
info := providerInfo(*p, nil)
|
|
if !info.TakesClientCert || info.ClientCert != "~/Downloads/magpie-laptop/client.pem" {
|
|
t.Fatalf("shown %v %q", info.TakesClientCert, info.ClientCert)
|
|
}
|
|
if code, out := post(base + `,"from":"home","models":[],"headers":{}}`); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
if p, _ = provider.Find("home"); p.ClientCert == "" || p.ClientKey == "" {
|
|
t.Fatal("a Save that left them out cleared them")
|
|
}
|
|
if code, out := post(base + `,"from":"home","models":[],"headers":{},"clientCert":"","clientKey":"","serverCA":""}`); code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
if p, _ = provider.Find("home"); p.ClientCert != "" || p.ServerCA != "" {
|
|
t.Fatal("sent empty, still kept")
|
|
}
|
|
if code, _ := post(base + `,"from":"home","models":[],"headers":{},"clientCert":"~/Downloads/magpie-laptop/client.pem"}`); code == 200 {
|
|
t.Fatal("a certificate without its key saved")
|
|
}
|
|
}
|