carlos / platform Public

domains: ReadImportedCert, one bounded read of a cached import (#1429)

Astra R3 P2-3 (census rejection of UsableImportedCert).

ReadImportedCert opens the entry once and takes everything from the
bytes read through that descriptor: on linux and darwin with
O_NOFOLLOW|O_NONBLOCK, then Fstat (a regular file of at most 64 KiB);
elsewhere Lstat, Open, Fstat and os.SameFile. ParseImport and
ImportUsable run on those bytes, and the binding is returned so a caller
checks account and app without a second read. UsableImportedCert is a
thin wrapper. Tests: symlink, FIFO (no hang), oversize, expired, and a
swap between the portable opener's Lstat and Open.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 51f26ac65ed9d24b5bf8d5e997d7c10cd49f3c09 parent 4f4dac4
6 files changed, +236 −29
  • internal/domains/certimport.go +7 −29
  • internal/domains/certimport_read.go +76 −0
  • internal/domains/certimport_read_fifo_test.go +32 −0
  • internal/domains/certimport_read_nofollow.go +25 −0
  • internal/domains/certimport_read_portable.go +8 −0
  • internal/domains/certimport_read_test.go +88 −0
diff --git a/internal/domains/certimport.go b/internal/domains/certimport.go
index 5b98e3fdd..25e644bad 100644
--- a/internal/domains/certimport.go
+++ b/internal/domains/certimport.go
@@ -6,9 +6,6 @@ import (
"crypto/x509"
"encoding/pem"
"errors"
- "io"
- "os"
- "path/filepath"
"strings"
"time"
)
@@ -121,31 +118,12 @@ func ImportUsable(b ImportBinding, cert *tls.Certificate, host string, now time.
}
// UsableImportedCert reports whether acmeDir holds an imported certificate
-// for host, bound to account, that would serve right now: a regular file
-// (a symlink is not followed) named host+ImportSuffix. It is the edges-file
-// writer's readiness test for publishing an alias before its DNS points
-// here (#1429), the import counterpart of UsablePerHostCert.
+// for host, bound to account, that would serve right now. It is
+// ReadImportedCert's one read with the account checked on the binding it
+// returned: the edges-file writer's readiness test for publishing an alias
+// before its DNS points here (#1429), the import counterpart of
+// UsablePerHostCert.
func UsableImportedCert(acmeDir, host, account string, now time.Time) bool {
- if acmeDir == "" || account == "" || !validHostShape(host) {
- return false
- }
- p := filepath.Join(acmeDir, ImportName(host))
- fi, err := os.Lstat(p)
- if err != nil || !fi.Mode().IsRegular() || fi.Size() > maxCertEntry {
- return false
- }
- f, err := os.Open(p)
- if err != nil {
- return false
- }
- defer f.Close()
- raw, err := io.ReadAll(io.LimitReader(f, maxCertEntry))
- if err != nil {
- return false
- }
- b, cert, err := ParseImport(raw)
- if err != nil || b.Account != account {
- return false
- }
- return ImportUsable(b, cert, host, now)
+ b, _, ok := ReadImportedCert(acmeDir, host, now)
+ return ok && account != "" && b.Account == account
}
diff --git a/internal/domains/certimport_read.go b/internal/domains/certimport_read.go
new file mode 100644
index 000000000..76dfbf988
--- /dev/null
+++ b/internal/domains/certimport_read.go
@@ -0,0 +1,76 @@
+package domains
+
+import (
+ "crypto/tls"
+ "io"
+ "os"
+ "path/filepath"
+ "time"
+)
+
+// maxImportEntry bounds an import cache entry: a binding, a key and at most
+// five certificates fit in a fraction of it.
+const maxImportEntry = 64 << 10
+
+// ReadImportedCert reads host's import from the cache directory acmeDir
+// once, and reports it only when it would serve right now. The file is
+// opened once, without following a symlink and (where the system allows)
+// without blocking on a FIFO; the descriptor must be a regular file of at
+// most 64 KiB; and the binding, the key pair and the usability checks all
+// come from the bytes read through that one descriptor. A caller that
+// needs more (the binding's account or app) checks the binding returned
+// here, never a second read of the file.
+func ReadImportedCert(acmeDir, host string, now time.Time) (ImportBinding, *tls.Certificate, bool) {
+ if acmeDir == "" || !validHostShape(host) {
+ return ImportBinding{}, nil, false
+ }
+ f, ok := openImportEntry(filepath.Join(acmeDir, ImportName(host)))
+ if !ok {
+ return ImportBinding{}, nil, false
+ }
+ defer f.Close()
+ raw, err := io.ReadAll(io.LimitReader(f, maxImportEntry+1))
+ if err != nil || len(raw) > maxImportEntry {
+ return ImportBinding{}, nil, false
+ }
+ b, cert, err := ParseImport(raw)
+ if err != nil || !ImportUsable(b, cert, host, now) {
+ return ImportBinding{}, nil, false
+ }
+ return b, cert, true
+}
+
+// importEntryOK is the descriptor check both openers apply: a regular file
+// no larger than the bound.
+func importEntryOK(fi os.FileInfo) bool {
+ return fi.Mode().IsRegular() && fi.Size() <= maxImportEntry
+}
+
+// importOpenSwapHook, when set (tests only), runs between the portable
+// opener's Lstat and its Open: the window a swap would use.
+var importOpenSwapHook func()
+
+// openImportEntryPortable is the opener for systems without O_NOFOLLOW:
+// Lstat (a regular file), Open, then Fstat, and the two must be the same
+// file, so a symlink or another file swapped in between is refused. It
+// cannot stop a FIFO swapped in between the Lstat and the Open from
+// blocking that Open; the systems that use it have no O_NONBLOCK open.
+func openImportEntryPortable(path string) (*os.File, bool) {
+ before, err := os.Lstat(path)
+ if err != nil || !importEntryOK(before) {
+ return nil, false
+ }
+ if importOpenSwapHook != nil {
+ importOpenSwapHook()
+ }
+ f, err := os.Open(path)
+ if err != nil {
+ return nil, false
+ }
+ after, err := f.Stat()
+ if err != nil || !importEntryOK(after) || !os.SameFile(before, after) {
+ f.Close()
+ return nil, false
+ }
+ return f, true
+}
diff --git a/internal/domains/certimport_read_fifo_test.go b/internal/domains/certimport_read_fifo_test.go
new file mode 100644
index 000000000..d80623079
--- /dev/null
+++ b/internal/domains/certimport_read_fifo_test.go
@@ -0,0 +1,32 @@
+//go:build linux || darwin
+
+package domains
+
+import (
+ "path/filepath"
+ "syscall"
+ "testing"
+ "time"
+)
+
+// TestReadImportedCertRefusesAFIFO: a FIFO named like an entry is refused
+// at once, not opened and waited on.
+func TestReadImportedCertRefusesAFIFO(t *testing.T) {
+ dir := t.TempDir()
+ if err := syscall.Mkfifo(filepath.Join(dir, ImportName(simHost)), 0o600); err != nil {
+ t.Skipf("mkfifo: %v", err)
+ }
+ done := make(chan bool, 1)
+ go func() {
+ _, _, ok := ReadImportedCert(dir, simHost, time.Now())
+ done <- ok
+ }()
+ select {
+ case ok := <-done:
+ if ok {
+ t.Fatal("a FIFO read as an entry")
+ }
+ case <-time.After(10 * time.Second):
+ t.Fatal("ReadImportedCert blocked on a FIFO")
+ }
+}
diff --git a/internal/domains/certimport_read_nofollow.go b/internal/domains/certimport_read_nofollow.go
new file mode 100644
index 000000000..5e2b2e325
--- /dev/null
+++ b/internal/domains/certimport_read_nofollow.go
@@ -0,0 +1,25 @@
+//go:build linux || darwin
+
+package domains
+
+import (
+ "os"
+ "syscall"
+)
+
+// openImportEntry opens path once: O_NOFOLLOW refuses a symlink at the
+// last component, O_NONBLOCK keeps a FIFO from blocking the open, and the
+// Fstat on that descriptor must show a regular file within the bound.
+func openImportEntry(path string) (*os.File, bool) {
+ fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
+ if err != nil {
+ return nil, false
+ }
+ f := os.NewFile(uintptr(fd), path)
+ fi, err := f.Stat()
+ if err != nil || !importEntryOK(fi) {
+ f.Close()
+ return nil, false
+ }
+ return f, true
+}
diff --git a/internal/domains/certimport_read_portable.go b/internal/domains/certimport_read_portable.go
new file mode 100644
index 000000000..8838794ae
--- /dev/null
+++ b/internal/domains/certimport_read_portable.go
@@ -0,0 +1,8 @@
+//go:build !(linux || darwin)
+
+package domains
+
+import "os"
+
+// openImportEntry is the portable opener where O_NOFOLLOW is not used.
+func openImportEntry(path string) (*os.File, bool) { return openImportEntryPortable(path) }
diff --git a/internal/domains/certimport_read_test.go b/internal/domains/certimport_read_test.go
new file mode 100644
index 000000000..8d9e06d30
--- /dev/null
+++ b/internal/domains/certimport_read_test.go
@@ -0,0 +1,88 @@
+package domains
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+ "time"
+)
+
+func writeImportFile(t *testing.T, dir, host string, b ImportBinding) string {
+ t.Helper()
+ pkcs8, leaf := simMaterial(t)
+ p := filepath.Join(dir, ImportName(host))
+ if err := os.WriteFile(p, EncodeImport(b, pkcs8, [][]byte{leaf}), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ return p
+}
+
+func TestReadImportedCert(t *testing.T) {
+ dir := t.TempDir()
+ now := time.Now()
+ b := ImportBinding{Host: simHost, Account: "acca", App: "web", ClaimCreated: "2026-11-20T10:00:00Z"}
+ writeImportFile(t, dir, simHost, b)
+
+ got, cert, ok := ReadImportedCert(dir, simHost, now)
+ if !ok || got != b || cert == nil || cert.Leaf == nil {
+ t.Fatalf("good entry: %+v %v %v", got, cert, ok)
+ }
+ if !UsableImportedCert(dir, simHost, "acca", now) || UsableImportedCert(dir, simHost, "accb", now) {
+ t.Fatal("UsableImportedCert does not follow the binding's account")
+ }
+ if _, _, ok := ReadImportedCert(dir, simHost, now.Add(365*24*time.Hour)); ok {
+ t.Fatal("an expired entry reads as usable")
+ }
+
+ // A symlink to a good entry is refused, by both openers.
+ link := filepath.Join(dir, ImportName("link.example.com"))
+ if err := os.Symlink(filepath.Join(dir, ImportName(simHost)), link); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, ok := ReadImportedCert(dir, "link.example.com", now); ok {
+ t.Fatal("a symlink was followed")
+ }
+ if f, ok := openImportEntryPortable(link); ok {
+ f.Close()
+ t.Fatal("the portable opener followed a symlink")
+ }
+
+ // Over 64 KiB is refused before it is parsed.
+ big := filepath.Join(dir, ImportName("big.example.com"))
+ if err := os.WriteFile(big, []byte(strings.Repeat("A", maxImportEntry+1)), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if _, _, ok := ReadImportedCert(dir, "big.example.com", now); ok {
+ t.Fatal("an oversized entry was read")
+ }
+ if f, ok := openImportEntryPortable(big); ok {
+ f.Close()
+ t.Fatal("the portable opener took an oversized entry")
+ }
+}
+
+// TestReadImportedCertPortableDetectsASwap: a file swapped in between the
+// portable opener's Lstat and its Open is not the file it checked.
+func TestReadImportedCertPortableDetectsASwap(t *testing.T) {
+ dir := t.TempDir()
+ b := ImportBinding{Host: simHost, Account: "acca", App: "web", ClaimCreated: "2026-11-20T10:00:00Z"}
+ p := writeImportFile(t, dir, simHost, b)
+ other := writeImportFile(t, t.TempDir(), simHost, ImportBinding{Host: simHost, Account: "accb", App: "web", ClaimCreated: b.ClaimCreated})
+
+ if f, ok := openImportEntryPortable(p); !ok {
+ t.Fatal("the portable opener refused a good entry")
+ } else {
+ f.Close()
+ }
+ importOpenSwapHook = func() {
+ if err := os.Rename(other, p); err != nil {
+ t.Error(err)
+ }
+ }
+ t.Cleanup(func() { importOpenSwapHook = nil })
+ if f, ok := openImportEntryPortable(p); ok {
+ f.Close()
+ t.Fatal("the portable opener took a file swapped in after its Lstat")
+ }
+}