From a0cada043379f5237501c4b28d021d0e40ea6f49 Mon Sep 17 00:00:00 2001 From: hanming5 Date: Mon, 16 Nov 2026 13:42:49 +0900 Subject: [PATCH] fix(certs): replace the CA when the TLD changes The root is name-constrained to the TLD it was made for, and Ensure kept loading it after the TLD changed, so browsers rejected every certificate for the new TLD even with the CA trusted. Ensure now makes a new CA when the existing one doesn't permit the TLD. Site certificates follow, since they are reissued when signed by another CA, and `flock install` trusts the new root because its trust checks go by fingerprint. Fixes #180 --- docs/https.md | 2 +- internal/certs/certs.go | 20 +++++++++++++++++--- internal/certs/certs_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/docs/https.md b/docs/https.md index ad588a03..154ee83b 100644 --- a/docs/https.md +++ b/docs/https.md @@ -32,7 +32,7 @@ A secured site redirects plain `http://` requests to `https://`. If you open a s When you install Flock, it creates its own certificate authority (CA) and adds it to your system's trust store: your login keychain on macOS, Trusted Root on Windows. On Linux, it goes into the system's trust store, into Firefox through a policy, and into the certificate databases of Chrome and other Chromium browsers, Snap and Flatpak versions included. -The CA's root certificate is **name-constrained** to your local TLD and `localhost`. Even if its private key ever leaked, it could not be used to issue certificates for real websites. +The CA's root certificate is **name-constrained** to your local TLD and `localhost`. Even if its private key ever leaked, it could not be used to issue certificates for real websites. When you change the TLD, Flock creates a new CA for it, and `flock install` adds that one to the trust store. Each secured site gets a certificate for `acme.test` and `*.acme.test`. Certificates for deeper subdomains and for the site's aliases are issued on demand the first time they're requested. diff --git a/internal/certs/certs.go b/internal/certs/certs.go index e6de6bc9..f2070bcd 100644 --- a/internal/certs/certs.go +++ b/internal/certs/certs.go @@ -20,6 +20,7 @@ import ( "os/user" "path/filepath" "slices" + "strings" "time" ) @@ -37,13 +38,17 @@ type CA struct { key *ecdsa.PrivateKey } -// Ensure loads the CA from dir, creating it on first use. +// Ensure loads the CA from dir, creating it on first use. A CA made for another TLD is replaced: +// its name constraints make browsers reject what it signs for tld. The new one isn't trusted +// yet, which `flock install` notices by its fingerprint and fixes. func Ensure(dir, tld string) (*CA, error) { ca := &CA{dir: dir} - if err := ca.load(); err == nil { + err := ca.load() + switch { + case err == nil && ca.permits(tld): return ca, nil - } else if !errors.Is(err, os.ErrNotExist) { + case err != nil && !errors.Is(err, os.ErrNotExist): return nil, err } @@ -54,6 +59,15 @@ func Ensure(dir, tld string) (*CA, error) { return ca, nil } +// permits reports whether the CA's name constraints allow names under tld. A CA without +// constraints allows every name. +func (ca *CA) permits(tld string) bool { + permitted := ca.cert.PermittedDNSDomains + return len(permitted) == 0 || slices.ContainsFunc(permitted, func(d string) bool { + return strings.EqualFold(d, "."+tld) + }) +} + // CertPath is the PEM file of the root certificate (what gets trusted by the OS). func (ca *CA) CertPath() string { return filepath.Join(ca.dir, "ca.crt") } diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index f9d21735..a3b612ff 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -59,6 +59,41 @@ func TestReusesCAAndCurrentCerts(t *testing.T) { } } +func TestReplacesCAWhenTheTLDChanges(t *testing.T) { + dir := t.TempDir() + old, _ := Ensure(dir, "wsl") + if _, _, err := old.SiteCert("app.wsl"); err != nil { + t.Fatal(err) + } + + ca, err := Ensure(dir, "test") + if err != nil { + t.Fatal(err) + } + if ca.Fingerprint() == old.Fingerprint() { + t.Fatal("CA for .wsl was kept for .test") + } + certPath, _, err := ca.SiteCert("app.test") + if err != nil { + t.Fatal(err) + } + if err := verify(t, ca, certPath, "app.test"); err != nil { + t.Fatalf("app.test: %v", err) + } + + again, err := Ensure(dir, "test") + if err != nil || again.Fingerprint() != ca.Fingerprint() { + t.Fatalf("new CA was not reused: %v", err) + } +} + +func TestKeepsCAWithoutNameConstraints(t *testing.T) { + ca := &CA{cert: &x509.Certificate{}} + if !ca.permits("test") { + t.Fatal("a CA without name constraints allows every TLD") + } +} + func TestRootIsNameConstrainedToTheTLD(t *testing.T) { ca, _ := Ensure(t.TempDir(), "test")