carlos / platform Public

assetcache: Open propagates disk errors and checks body sizes

A permission or I/O error reading a meta or statting a body now fails
Open instead of deleting the entry. Body and gzip files must match the
sizes the meta records, and overflowing sizes are rejected.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 4449a6b15d7faafb41b3f2afeb87cd540aa6d24a parent 1fa09af
3 files changed, +122 −18
  • internal/assetcache/meta.go +3 −2
  • internal/assetcache/store.go +42 −16
  • internal/assetcache/store_test.go +77 −0

CI failed — run details

diff --git a/internal/assetcache/meta.go b/internal/assetcache/meta.go
index 6a369abd9..b7eedd6f8 100644
--- a/internal/assetcache/meta.go
+++ b/internal/assetcache/meta.go
@@ -3,6 +3,7 @@ package assetcache
import (
"encoding/json"
"fmt"
+ "math"
"net/http"
"time"
)
@@ -35,8 +36,8 @@ func decodeMeta(raw []byte) (Meta, error) {
if !validVersion(m.Version) {
return Meta{}, fmt.Errorf("version %q is not a file name part", m.Version)
}
- if m.Size < 0 || m.GzipSize < 0 {
- return Meta{}, fmt.Errorf("negative size")
+ if m.Size < 0 || m.GzipSize < 0 || m.Size > math.MaxInt64-m.GzipSize {
+ return Meta{}, fmt.Errorf("sizes %d and %d are not usable", m.Size, m.GzipSize)
}
return m, nil
}
diff --git a/internal/assetcache/store.go b/internal/assetcache/store.go
index 6293da5ad..d31732cee 100644
--- a/internal/assetcache/store.go
+++ b/internal/assetcache/store.go
@@ -8,6 +8,7 @@ package assetcache
import (
"errors"
"fmt"
+ "math"
"os"
"path/filepath"
"strings"
@@ -105,8 +106,11 @@ func (s *Store) scan() error {
keep := make(map[string]bool) // body file names a parsed meta names
for _, name := range metas {
id := strings.TrimSuffix(name, ".meta")
- m, ok := s.loadMeta(id)
- if !ok {
+ m, valid, err := s.loadMeta(id)
+ if err != nil {
+ return err
+ }
+ if !valid {
if err := s.remove(name); err != nil {
return err
}
@@ -116,6 +120,9 @@ func (s *Store) scan() error {
if m.GzipSize > 0 {
keep[gzipName(id, m.Version)] = true
}
+ if s.used > math.MaxInt64-(m.Size+m.GzipSize) {
+ return errors.New("assetcache: indexed bytes overflow")
+ }
s.index(&entry{id: id, meta: m})
}
for _, name := range files {
@@ -128,28 +135,47 @@ func (s *Store) scan() error {
return nil
}
-// loadMeta reads and checks <id>.meta. It reports false for a file that
-// does not parse, belongs to another key, or lacks a body file it names.
-func (s *Store) loadMeta(id string) (Meta, bool) {
+// loadMeta reads and checks <id>.meta. It reports valid=false for an entry
+// that is certainly broken: a missing or malformed meta, one that belongs to
+// another key, or one whose body files are missing or not the size it
+// records. Any other failure to read the disk is returned as an error, so a
+// permission or I/O fault never leads to deleting a good entry.
+func (s *Store) loadMeta(id string) (m Meta, valid bool, err error) {
raw, err := os.ReadFile(filepath.Join(s.cfg.Dir, metaName(id)))
+ if errors.Is(err, os.ErrNotExist) {
+ return Meta{}, false, nil
+ }
if err != nil {
- return Meta{}, false
+ return Meta{}, false, fmt.Errorf("assetcache: %w", err)
}
- m, err := decodeMeta(raw)
+ m, err = decodeMeta(raw)
if err != nil || m.Key.ID() != id {
- return Meta{}, false
+ return Meta{}, false, nil
}
- names := []string{bodyName(id, m.Version)}
- if m.GzipSize > 0 {
- names = append(names, gzipName(id, m.Version))
+ ok, err := s.bodyMatches(bodyName(id, m.Version), m.Size)
+ if err != nil || !ok {
+ return Meta{}, false, err
}
- for _, n := range names {
- fi, err := os.Stat(filepath.Join(s.cfg.Dir, n))
- if err != nil || !fi.Mode().IsRegular() {
- return Meta{}, false
+ if m.GzipSize > 0 {
+ ok, err = s.bodyMatches(gzipName(id, m.Version), m.GzipSize)
+ if err != nil || !ok {
+ return Meta{}, false, err
}
}
- return m, true
+ return m, true, nil
+}
+
+// bodyMatches reports whether the named file is a regular file of exactly
+// size bytes. A missing file is false with no error.
+func (s *Store) bodyMatches(name string, size int64) (bool, error) {
+ fi, err := os.Stat(filepath.Join(s.cfg.Dir, name))
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil
+ }
+ if err != nil {
+ return false, fmt.Errorf("assetcache: %w", err)
+ }
+ return fi.Mode().IsRegular() && fi.Size() == size, nil
}
// remove deletes one file of the store's directory by name.
diff --git a/internal/assetcache/store_test.go b/internal/assetcache/store_test.go
index 9c6f8581e..bd16ae149 100644
--- a/internal/assetcache/store_test.go
+++ b/internal/assetcache/store_test.go
@@ -1,6 +1,7 @@
package assetcache
import (
+ "math"
"net/http"
"os"
"path/filepath"
@@ -270,3 +271,79 @@ func TestOpenDeletesSupersededVersions(t *testing.T) {
t.Fatalf("Stats = %+v, want 1 entry of 4 bytes", st)
}
}
+
+func TestOpenFailsOnUnreadableMetaAndKeepsEntry(t *testing.T) {
+ if os.Geteuid() == 0 {
+ t.Skip("running as root: a 0000-mode file is still readable, so this cannot provoke a read error")
+ }
+ dir := t.TempDir()
+ k := testKey("shop", "/a.css")
+ writeEntry(t, dir, k, "a", []byte("body"), []byte("gz"))
+ metaPath := filepath.Join(dir, k.ID()+".meta")
+ if err := os.Chmod(metaPath, 0); err != nil {
+ t.Fatal(err)
+ }
+ defer os.Chmod(metaPath, 0o600)
+
+ if _, err := Open(testConfig(dir)); err == nil {
+ t.Fatal("Open succeeded over an unreadable meta")
+ }
+ sameNames(t, listDir(t, dir), []string{k.ID() + ".meta", k.ID() + ".a.body", k.ID() + ".a.gz"})
+
+ // Once the fault clears the entry is still there.
+ if err := os.Chmod(metaPath, 0o600); err != nil {
+ t.Fatal(err)
+ }
+ if st := openStore(t, dir).Stats(); st.Entries != 1 || st.Bytes != 6 {
+ t.Fatalf("Stats = %+v, want the entry intact", st)
+ }
+}
+
+func TestOpenDropsEntryWhoseBodySizeDiffers(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ edit func(t *testing.T, dir string, k Key)
+ }{
+ {"identity truncated", func(t *testing.T, dir string, k Key) {
+ writeFile(t, filepath.Join(dir, k.ID()+".a.body"), []byte("bo"))
+ }},
+ {"identity longer than recorded", func(t *testing.T, dir string, k Key) {
+ writeFile(t, filepath.Join(dir, k.ID()+".a.body"), []byte("body and more"))
+ }},
+ {"gzip truncated", func(t *testing.T, dir string, k Key) {
+ writeFile(t, filepath.Join(dir, k.ID()+".a.gz"), []byte("g"))
+ }},
+ {"gzip longer than recorded", func(t *testing.T, dir string, k Key) {
+ writeFile(t, filepath.Join(dir, k.ID()+".a.gz"), []byte("gzip and more"))
+ }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ dir := t.TempDir()
+ k := testKey("shop", "/a.css")
+ writeEntry(t, dir, k, "a", []byte("body"), []byte("gz"))
+ tc.edit(t, dir, k)
+ s := openStore(t, dir)
+ if st := s.Stats(); st != (Stats{}) {
+ t.Fatalf("Stats = %+v, want the entry dropped", st)
+ }
+ sameNames(t, listDir(t, dir), nil)
+ })
+ }
+}
+
+func TestOpenDropsMetaWithOverflowingSizes(t *testing.T) {
+ dir := t.TempDir()
+ k := testKey("shop", "/a.css")
+ raw, err := encodeMeta(Meta{Key: k, Version: "a", Size: math.MaxInt64, GzipSize: math.MaxInt64})
+ if err != nil {
+ t.Fatal(err)
+ }
+ writeFile(t, filepath.Join(dir, k.ID()+".meta"), raw)
+ writeVersionFiles(t, dir, k.ID(), "a", []byte("body"), []byte("gz"))
+
+ s := openStore(t, dir)
+ if st := s.Stats(); st != (Stats{}) {
+ t.Fatalf("Stats = %+v, want the entry dropped", st)
+ }
+ sameNames(t, listDir(t, dir), nil)
+}