carlos / platform Public

edge: budget the directory names site extraction keeps

siteDirs kept every made directory's full name for the whole extraction,
bounded only by 100,000 inodes of up to 4,096 bytes: about 400 MB of edge
heap from a ~1 MB archive (O4 R2, H5-M6). Each kept name is now its own
copy, charged before its directory is made against siteMaxDirNames
(8 MiB), and refused past it like the inode budget.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 51a4c619fc6487ea1663011e486fda502a4196e3 parent d17fa61
2 files changed, +101 −8
  • internal/edge/hunt_H5-M6_test.go +78 −0
  • internal/edge/static.go +23 −8
diff --git a/internal/edge/hunt_H5-M6_test.go b/internal/edge/hunt_H5-M6_test.go
index ade48af86..3e4944d14 100644
--- a/internal/edge/hunt_H5-M6_test.go
+++ b/internal/edge/hunt_H5-M6_test.go
@@ -27,6 +27,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
+ "strings"
"testing"
"time"
@@ -309,3 +310,80 @@ func TestSiteTarImpliedDirectoriesCountAgainstTheInodeBudget(t *testing.T) {
}
})
}
+
+// buildLongDirTarGz writes entries directory entries under one shared deep
+// prefix of components of 240 bytes, each with its own leaf, so every entry
+// makes one directory whose full name is about prefixLen bytes long.
+func buildLongDirTarGz(t *testing.T, entries, components int) []byte {
+ t.Helper()
+ var buf bytes.Buffer
+ zw := gzip.NewWriter(&buf)
+ tw := tar.NewWriter(zw)
+ prefix := ""
+ for j := 0; j < components; j++ {
+ prefix += strings.Repeat(string(rune('a'+j%26)), 240) + "/"
+ }
+ for i := 0; i < entries; i++ {
+ if err := tw.WriteHeader(&tar.Header{Name: fmt.Sprintf("%sleaf%d/", prefix, i), Mode: 0o755, Typeflag: tar.TypeDir}); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := tw.Close(); err != nil {
+ t.Fatal(err)
+ }
+ if err := zw.Close(); err != nil {
+ t.Fatal(err)
+ }
+ return buf.Bytes()
+}
+
+// The directory names extraction keeps, to count each directory once, are
+// budgeted in bytes (siteMaxDirNames), as the inodes are in number (O4
+// round 2, H5-M6's family). Before, every made directory's full name was
+// kept for the whole extraction, bounded only by 100,000 inodes of up to
+// 4,096 bytes: about 400 MB of heap from an archive of about 1 MB.
+// Through the production wiring: a shipped, promoted release served by
+// the real static handler; the budget is lowered so the proof is small.
+func TestSiteTarRetainedDirectoryNamesAreBudgeted(t *testing.T) {
+ old := siteMaxDirNames
+ siteMaxDirNames = 64 << 10
+ t.Cleanup(func() { siteMaxDirNames = old })
+ // 40 entries under a 16-component prefix: 56 inodes, far under the
+ // inode budget, but about 33 KB of names for the prefix's directories
+ // and 40 x 3.9 KB for the leaves, 187 KB in all.
+ site := buildLongDirTarGz(t, 40, 16)
+
+ store := release.NewDirStore(t.TempDir())
+ priv, pub := testKeys(t)
+ m := release.Manifest{Schema: 1, App: "site", Version: "v1", Kind: "static",
+ Artifacts: map[string]release.Artifact{siteArtifactFile: {}}}
+ if err := release.Ship(context.Background(), store, release.OpsAccount, m, map[string][]byte{siteArtifactFile: site}); err != nil {
+ t.Fatal(err)
+ }
+ if err := release.Promote(context.Background(), store, priv, release.OpsAccount, "site", "canary/x", "v1"); err != nil {
+ t.Fatal(err)
+ }
+ cacheDir := t.TempDir()
+ rt := registry.Route{Host: "site.example.com", Kind: "static", App: "site", Channel: "canary/x", Status: "ready"}
+ h := newStaticHandler(rt, store, pub, cacheDir).(*staticSite)
+ h.recheck = 0
+ req, _ := http.NewRequest("GET", "http://site.example.com/", nil)
+ w := httptest.NewRecorder()
+ h.ServeHTTP(w, req)
+ if w.Code != http.StatusBadGateway {
+ t.Fatalf("serve: %d, want 502 for a site refused at extraction", w.Code)
+ }
+ h.mu.Lock()
+ _, err := h.resolveLocked(context.Background())
+ h.mu.Unlock()
+ if !errors.Is(err, errSiteTooLarge) {
+ t.Fatalf("resolve: %v, want errSiteTooLarge for directory names over a budget of %d bytes", err, siteMaxDirNames)
+ }
+
+ t.Run("a tree whose names fit", func(t *testing.T) {
+ // The same prefix with 4 leaves: about 48 KB of names.
+ if err := ExtractSite(buildLongDirTarGz(t, 4, 16), t.TempDir()); err != nil {
+ t.Fatalf("ExtractSite: %v", err)
+ }
+ })
+}
diff --git a/internal/edge/static.go b/internal/edge/static.go
index 41a7fde33..e6c94d6b6 100644
--- a/internal/edge/static.go
+++ b/internal/edge/static.go
@@ -589,12 +589,19 @@ func ExtractSite(data []byte, dst string) error { return extractSiteTar(bytes.Ne
// - siteMaxName, 4,096 bytes for an entry's name: PATH_MAX, past which
// no file could be made anyway. It bounds the work a name costs, as
// the inode budget bounds the names.
+// - siteMaxDirNames, 8 MiB: the full names of the directories made,
+// which extraction keeps (siteDirs) so each is counted once. A real
+// site's are a few bytes to a hundred each: 8 MiB is 80,000
+// directories at 100 bytes. Without it, the other limits allowed
+// 100,000 names of 4,096 bytes, about 400 MB of edge heap from an
+// archive of about 1 MB (O4 round 2).
//
// Vars, not consts, so a test can lower them.
var (
siteMaxBytes int64 = 1 << 30
siteMaxFileBytes int64 = 256 << 20
siteMaxEntries = 100_000
+ siteMaxDirNames = 8 << 20
)
const siteMaxName = 4096
@@ -606,8 +613,8 @@ var errSiteTooLarge = errors.New("site tar: over the expanded size limit")
// that would write outside it: absolute names, ".." traversal, and non-file
// entry types (a symlink in a site tar could alias the extraction outside
// the cache — PackDir never emits one, so none is ever legitimate). A tree
-// over siteMaxBytes, siteMaxFileBytes or siteMaxEntries, or with a name
-// over siteMaxName, is refused with errSiteTooLarge; the caller removes
+// over siteMaxBytes, siteMaxFileBytes, siteMaxEntries or siteMaxDirNames,
+// or with a name over siteMaxName, is refused with errSiteTooLarge; the caller removes
// what was written.
func extractSiteTar(src io.Reader, dst string) error {
gz, err := gzip.NewReader(src)
@@ -690,11 +697,13 @@ func extractSiteTar(src io.Reader, dst string) error {
}
// siteDirs is extractSiteTar's inode budget and the directories it has
-// made under dst, so each is counted once, when it is made.
+// made under dst, so each is counted once, when it is made. nameBytes is
+// what made holds in names, budgeted by siteMaxDirNames.
type siteDirs struct {
- dst string
- made map[string]struct{}
- inodes int
+ dst string
+ made map[string]struct{}
+ inodes int
+ nameBytes int
}
// count charges one inode to the budget, refusing past siteMaxEntries.
@@ -710,7 +719,10 @@ func (x *siteDirs) count() error {
// stops at the first directory already made, whose parents were made with
// it, so a directory costs one lookup per entry under it and its making
// once; the name cap bounds the walk. made holds at most siteMaxEntries
-// keys of at most siteMaxName bytes each. It returns how many it made.
+// keys and siteMaxDirNames bytes of them, each charged before its
+// directory is made. A key is its own copy: a slice of the entry's name
+// would keep all of that name alive for the price of its prefix. It
+// returns how many it made.
func (x *siteDirs) mkdirs(rel string) (int, error) {
var missing []string
for d := rel; d != "."; d = filepath.Dir(d) {
@@ -723,10 +735,13 @@ func (x *siteDirs) mkdirs(rel string) (int, error) {
if err := x.count(); err != nil {
return 0, err
}
+ if x.nameBytes += len(missing[i]); x.nameBytes > siteMaxDirNames {
+ return 0, fmt.Errorf("%w: more than %d bytes of directory names", errSiteTooLarge, siteMaxDirNames)
+ }
if err := os.Mkdir(filepath.Join(x.dst, missing[i]), 0o755); err != nil && !existingDir(filepath.Join(x.dst, missing[i]), err) {
return 0, err
}
- x.made[missing[i]] = struct{}{}
+ x.made[strings.Clone(missing[i])] = struct{}{}
}
return len(missing), nil
}