edge: the local import copy is written without reading the entry (#1429)
Astra R3 P2-3: reconcileLocalImport called local.Get, which follows a symlink and blocks on a FIFO, even after finding the entry was not a regular file. syncLocal only calls it for bytes this process has not reconciled yet, so it now never reads the local entry: a non-regular entry is removed (os.Remove takes the link or FIFO itself), and the bucket's bytes are written through the cache's Put (0600, temp file and rename). The cost is one rewrite of each unchanged import per process start. Tests: a symlink and a FIFO under the import's name are replaced by a regular 0600 file with the bucket's bytes, the symlink's target is untouched, and nothing blocks; reading first blocks on the FIFO.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+92
−15
internal/edge/certimport.go+17 −15internal/edge/certimport_local_unix_test.go+75 −0
diff --git a/internal/edge/certimport.go b/internal/edge/certimport.go| index dfe5600f0..54f076e29 100644 |
| --- a/internal/edge/certimport.go |
| +++ b/internal/edge/certimport.go |
| @@ -21,7 +21,6 @@ package edge |
| // pointing) is in issuerHostPolicyAt. |
| import ( |
| - "bytes" |
| "context" |
| "crypto/tls" |
| "errors" |
| @@ -212,24 +211,27 @@ func (m *certMirror) pruneLocalImports(listed map[string]bool) { |
| } |
| } |
| -// reconcileLocalImport writes one import into the local cache by IDENTITY: |
| -// whenever the local copy's bytes differ from the bucket's, or the local |
| -// entry is not a regular file, whatever the expiry dates say. The cache's |
| -// Put writes mode 0600 (autocert.DirCache: a temp file, then a rename). |
| +// reconcileLocalImport writes one import into the local cache by IDENTITY, |
| +// whatever the expiry dates say. syncLocal calls it only for bytes this |
| +// process has not yet reconciled (its synced memo: a new or changed bucket |
| +// entry, or the first pass after a start), so it writes unconditionally and |
| +// never reads the local entry at all: nothing here can follow a symlink or |
| +// block on a FIFO someone left in the cache directory (Astra R3 P2-3). |
| +// |
| +// A local entry that is not a regular file is removed first (os.Remove |
| +// takes the link or the FIFO itself, never its target), then the bucket's |
| +// bytes are written through the cache's Put: autocert.DirCache writes a 0600 |
| +// temp file and renames it over the name. The one cost of not comparing is a |
| +// rewrite of each import's unchanged file once per process start. |
| func (m *certMirror) reconcileLocalImport(ctx context.Context, name string, body []byte) error { |
| - regular := true |
| if dir, ok := m.local.(autocert.DirCache); ok { |
| - if fi, err := os.Lstat(filepath.Join(string(dir), name)); err == nil && !fi.Mode().IsRegular() { |
| - regular = false |
| + p := filepath.Join(string(dir), name) |
| + if fi, err := os.Lstat(p); err == nil && !fi.Mode().IsRegular() { |
| + if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) { |
| + return fmt.Errorf("remove the non-regular %q: %w", name, err) |
| + } |
| } |
| } |
| - local, err := m.local.Get(ctx, name) |
| - if err != nil && !errors.Is(err, autocert.ErrCacheMiss) && regular { |
| - return fmt.Errorf("read %q: %w", name, err) |
| - } |
| - if err == nil && regular && bytes.Equal(local, body) { |
| - return nil |
| - } |
| if err := m.local.Put(ctx, name, body); err != nil { |
| return fmt.Errorf("write %q: %w", name, err) |
| } |
diff --git a/internal/edge/certimport_local_unix_test.go b/internal/edge/certimport_local_unix_test.go| new file mode 100644 |
| index 000000000..d3d54f3c7 |
| --- /dev/null |
| +++ b/internal/edge/certimport_local_unix_test.go |
| @@ -0,0 +1,75 @@ |
| +//go:build linux || darwin |
| + |
| +package edge |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "os" |
| + "path/filepath" |
| + "syscall" |
| + "testing" |
| + "time" |
| + |
| + "golang.org/x/crypto/acme/autocert" |
| + |
| + "github.com/carlosframework/platform/internal/domains" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// A symlink or a FIFO left in the cache directory under an import's name is |
| +// never followed, read or blocked on (Astra R3 P2-3): the entry itself is |
| +// removed and the bucket's bytes are written as a regular 0600 file, and a |
| +// symlink's target is left untouched. |
| +func TestImportLocalCopyReplacesSymlinksAndFIFOs(t *testing.T) { |
| + e2eQuietLogs(t) |
| + ca := newE2ECA(t) |
| + name := domains.ImportName(importHost) |
| + body := goodImport(t, ca, importHost, false) |
| + for _, kind := range []string{"symlink", "fifo"} { |
| + t.Run(kind, func(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + putBucket(t, s, name, body) |
| + dir := t.TempDir() |
| + p := filepath.Join(dir, name) |
| + target := filepath.Join(t.TempDir(), "elsewhere") |
| + const decoy = "not the import" |
| + switch kind { |
| + case "symlink": |
| + if err := os.WriteFile(target, []byte(decoy), 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(target, p); err != nil { |
| + t.Fatal(err) |
| + } |
| + case "fifo": |
| + if err := syscall.Mkfifo(p, 0o600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + m := newCertMirror(s, autocert.DirCache(dir)) |
| + done := make(chan error, 1) |
| + go func() { done <- m.Refresh(context.Background()) }() |
| + select { |
| + case err := <-done: |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + case <-time.After(10 * time.Second): |
| + t.Fatal("the refresh blocked on the cache entry") |
| + } |
| + fi, err := os.Lstat(p) |
| + if err != nil || !fi.Mode().IsRegular() || fi.Mode().Perm() != 0o600 { |
| + t.Fatalf("the entry is %v (%v); want a regular 0600 file", fi.Mode(), err) |
| + } |
| + if got, err := os.ReadFile(p); err != nil || !bytes.Equal(got, body) { |
| + t.Fatalf("the entry does not hold the bucket's bytes (%v)", err) |
| + } |
| + if kind == "symlink" { |
| + if got, err := os.ReadFile(target); err != nil || string(got) != decoy { |
| + t.Fatalf("the symlink's target was touched (%q, %v)", got, err) |
| + } |
| + } |
| + }) |
| + } |
| +} |