devbox mode (#474): final review fixes, rule-file bounds and minors
The _headers and _redirects loaders read at most the parser's limit plus one byte; a larger file is logged and the tree served without its rules, as for any unreadable rule file. The registrar refuses a folder upload whose root _headers or _redirects is over that limit at admission, with folder-too-big. Minors: the devbox drain comment says Server.Close closes connections without waiting for handlers; TestLocalStaticCacheIsBounded is renamed for what it checks (a swap replaces the cached target); a _redirects change is tested across a swap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+176
−9
internal/devbox/registrar.go+7 −1internal/devbox/registrar_test.go+30 −0internal/edge/devbox.go+5 −3internal/edge/localstatic_test.go+96 −3internal/edge/static.go+38 −2
diff --git a/internal/devbox/registrar.go b/internal/devbox/registrar.go| index ef806fe4c..7c3a2b4c0 100644 |
| --- a/internal/devbox/registrar.go |
| +++ b/internal/devbox/registrar.go |
| @@ -710,7 +710,8 @@ func spoolUsage(root string) (int64, error) { |
| // and every distinct directory, whether the archive names it or |
| // ExtractSite makes it implicitly for a file's parents. It refuses what |
| // ExtractSite would refuse (bad-request), and an archive that would unpack |
| -// past limit or hold too many entries (folder-too-big). Decompression stops |
| +// past limit, hold too many entries, or hold a _headers or _redirects over |
| +// the edge's limit for it (folder-too-big). Decompression stops |
| // as soon as the declared sizes pass limit, so a gzip bomb costs little. |
| func scanSite(data []byte, limit int64) (int64, *refusal) { |
| gz, err := gzip.NewReader(bytes.NewReader(data)) |
| @@ -775,6 +776,11 @@ func scanSite(data []byte, limit int64) (int64, *refusal) { |
| if ref := addDir(filepath.Dir(name), entries); ref != nil { |
| return 0, ref |
| } |
| + // A rule file the edge would refuse to read whole is refused |
| + // here, so a small archive cannot carry a huge one. |
| + if limit, ok := edge.RuleFileLimit(name); ok && hdr.Size > limit { |
| + return 0, tooBig |
| + } |
| total += blocks(hdr.Size) |
| default: |
| return 0, refuse(http.StatusBadRequest, CodeBadRequest) |
diff --git a/internal/devbox/registrar_test.go b/internal/devbox/registrar_test.go| index 4a8027e31..75ce97a70 100644 |
| --- a/internal/devbox/registrar_test.go |
| +++ b/internal/devbox/registrar_test.go |
| @@ -1263,3 +1263,33 @@ func TestUnshareFinishesWhenRecordCannotReturn(t *testing.T) { |
| t.Fatalf("spool dir left: %v", err) |
| } |
| } |
| + |
| +// A _headers or _redirects at the root over the edge's limit is refused at |
| +// admission, before anything is written, however well it compresses. At |
| +// the limit, or deeper in the tree where the edge never reads it as rules, |
| +// the same file is accepted. |
| +func TestShareFolderRefusesOversizedRuleFiles(t *testing.T) { |
| + for _, file := range []string{"_headers", "_redirects"} { |
| + t.Run(file, func(t *testing.T) { |
| + limit, ok := edge.RuleFileLimit(file) |
| + if !ok { |
| + t.Fatalf("edge has no limit for %s", file) |
| + } |
| + f := newFixture(t) |
| + for _, name := range []string{file, "./" + file} { |
| + over := rawTar(t, &tar.Header{Name: name, Typeflag: tar.TypeReg, Mode: 0o644, Size: limit + 1}) |
| + wantCode(t, f.shareFolder(t, 1001, "shop", over), CodeFolderTooBig) |
| + } |
| + if _, err := os.Lstat(filepath.Join(f.spool, "shop")); !os.IsNotExist(err) { |
| + t.Fatalf("refused upload left a spool dir: %v", err) |
| + } |
| + if _, ok := f.r.Links.Get("shop"); ok { |
| + t.Fatal("refused upload wrote a link") |
| + } |
| + at := rawTar(t, &tar.Header{Name: file, Typeflag: tar.TypeReg, Mode: 0o644, Size: limit}) |
| + wantOK(t, f.shareFolder(t, 1001, "shop", at)) |
| + nested := rawTar(t, &tar.Header{Name: "sub/" + file, Typeflag: tar.TypeReg, Mode: 0o644, Size: limit + 1}) |
| + wantOK(t, f.shareFolder(t, 1001, "deep", nested)) |
| + }) |
| + } |
| +} |
diff --git a/internal/edge/devbox.go b/internal/edge/devbox.go| index f6b6eda52..4d1605e9c 100644 |
| --- a/internal/edge/devbox.go |
| +++ b/internal/edge/devbox.go |
| @@ -171,9 +171,11 @@ func serveDevbox(ctx context.Context, cfg DevboxConfig, seams devboxSeams) error |
| defer cancel() |
| if err := srv.Shutdown(sctx); err != nil { |
| // Shutdown gives up at the deadline without touching the connections |
| - // still active. Close them, so no handler outlives the return, and |
| - // say the drain failed. Close does not reach hijacked connections |
| - // (proxied WebSockets); as in Run, those are not drained either. |
| + // still active. Close closes them and says the drain failed. It does |
| + // not wait for their handlers, which may still be running when this |
| + // returns; the devbox process exits right after, which ends them. |
| + // Close does not reach hijacked connections (proxied WebSockets); |
| + // as in Run, those are not drained either. |
| if cerr := srv.Close(); cerr != nil { |
| err = errors.Join(err, cerr) |
| } |
diff --git a/internal/edge/localstatic_test.go b/internal/edge/localstatic_test.go| index 223f1bab8..d6a6890b9 100644 |
| --- a/internal/edge/localstatic_test.go |
| +++ b/internal/edge/localstatic_test.go |
| @@ -5,6 +5,7 @@ import ( |
| "net/http/httptest" |
| "os" |
| "path/filepath" |
| + "runtime" |
| "strings" |
| "testing" |
| @@ -132,9 +133,10 @@ func TestLocalStaticRefusesDotDot(t *testing.T) { |
| } |
| } |
| -// TestLocalStaticCacheIsBounded: a long run of swaps keeps one parsed rule |
| -// set, the latest target's, not one per snapshot ever served. |
| -func TestLocalStaticCacheIsBounded(t *testing.T) { |
| +// TestLocalStaticSwapReplacesCachedTarget: each swap replaces the one parsed |
| +// rule set with the new target's, so after a run of swaps the cache holds |
| +// the latest target, not one entry per snapshot ever served. |
| +func TestLocalStaticSwapReplacesCachedTarget(t *testing.T) { |
| root := t.TempDir() |
| h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root).(*localStatic) |
| for _, n := range []string{"t1", "t2", "t3"} { |
| @@ -178,3 +180,94 @@ func TestStoreStaticUnchangedWithoutLocalRoot(t *testing.T) { |
| t.Fatalf("GET / = %d %q, want the 404 stub", rec.Code, rec.Body.String()) |
| } |
| } |
| + |
| +// A _redirects change takes effect across a swap, as _headers does: the |
| +// rules are parsed again for the new target. |
| +func TestLocalStaticRedirectsFollowSwap(t *testing.T) { |
| + root := t.TempDir() |
| + localTree(t, root, "shop", "t1", map[string]string{"index.html": "one", "_redirects": "/go /one.html 302\n"}) |
| + localTree(t, root, "shop", "t2", map[string]string{"index.html": "two", "_redirects": "/go /two.html 301\n"}) |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + if rec := localGet(t, h, "/go"); rec.Code != 302 || rec.Header().Get("Location") != "/one.html" { |
| + t.Fatalf("before swap: %d %q", rec.Code, rec.Header().Get("Location")) |
| + } |
| + pointCurrent(t, root, "shop", "t2") |
| + if rec := localGet(t, h, "/go"); rec.Code != 301 || rec.Header().Get("Location") != "/two.html" { |
| + t.Fatalf("after swap: %d %q, want 301 /two.html", rec.Code, rec.Header().Get("Location")) |
| + } |
| + // A swap to a tree with no _redirects drops the rules. |
| + localTree(t, root, "shop", "t3", map[string]string{"index.html": "three"}) |
| + pointCurrent(t, root, "shop", "t3") |
| + if rec := localGet(t, h, "/go"); rec.Code != http.StatusNotFound { |
| + t.Fatalf("after swap to no rules: %d %q", rec.Code, rec.Header().Get("Location")) |
| + } |
| +} |
| + |
| +// A _headers or _redirects far over the parser's limit is read only to |
| +// the limit plus one byte, never whole, and the tree is served without its |
| +// rules. The files are sparse, so the test costs no disk; a loader that |
| +// read them whole would allocate their full size. |
| +func TestLocalStaticOversizedRuleFiles(t *testing.T) { |
| + const huge = 64 << 20 |
| + for _, c := range []struct{ file, good string }{ |
| + {headersFile, "/*\n Cache-Control: max-age=60\n"}, |
| + {redirectsFile, "/go /one.html 302\n"}, |
| + } { |
| + t.Run(c.file, func(t *testing.T) { |
| + root := t.TempDir() |
| + dir := localTree(t, root, "shop", "t1", map[string]string{"index.html": "one", c.file: c.good}) |
| + if err := os.Truncate(filepath.Join(dir, c.file), huge); err != nil { |
| + t.Fatal(err) |
| + } |
| + pointCurrent(t, root, "shop", "t1") |
| + h := newLocalStaticHandler(registry.Route{Kind: "static", App: "shop"}, root) |
| + |
| + var before, after runtime.MemStats |
| + runtime.GC() |
| + runtime.ReadMemStats(&before) |
| + rec := localGet(t, h, "/") |
| + runtime.ReadMemStats(&after) |
| + if grew := after.TotalAlloc - before.TotalAlloc; grew > 8<<20 { |
| + t.Fatalf("serving allocated %d bytes for a %d byte %s", grew, huge, c.file) |
| + } |
| + if rec.Code != 200 || rec.Body.String() != "one" { |
| + t.Fatalf("GET / = %d %q", rec.Code, rec.Body.String()) |
| + } |
| + if got := rec.Header().Get("Cache-Control"); got == "max-age=60" { |
| + t.Fatalf("rules applied from an oversized %s", c.file) |
| + } |
| + if rec := localGet(t, h, "/go"); rec.Code != http.StatusNotFound { |
| + t.Fatalf("GET /go = %d, want 404 with no rules", rec.Code) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +// readRuleFile reads at most limit+1 bytes: a file at the limit is read |
| +// whole, one byte over is refused. |
| +func TestReadRuleFileBound(t *testing.T) { |
| + dir := t.TempDir() |
| + at := filepath.Join(dir, "at") |
| + over := filepath.Join(dir, "over") |
| + if err := os.WriteFile(at, []byte(strings.Repeat("#", headersMaxBytes)), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(over, []byte(strings.Repeat("#", headersMaxBytes+1)), 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + if data, err := readRuleFile(at, headersMaxBytes); err != nil || len(data) != headersMaxBytes { |
| + t.Fatalf("at the limit: %d bytes, %v", len(data), err) |
| + } |
| + if _, err := readRuleFile(over, headersMaxBytes); err == nil { |
| + t.Fatal("one byte over the limit was read") |
| + } |
| + for _, n := range []string{headersFile, redirectsFile} { |
| + if l, ok := RuleFileLimit(n); !ok || l != 256<<10 { |
| + t.Fatalf("RuleFileLimit(%q) = %d, %v", n, l, ok) |
| + } |
| + } |
| + if _, ok := RuleFileLimit("index.html"); ok { |
| + t.Fatal("RuleFileLimit names index.html") |
| + } |
| +} |
diff --git a/internal/edge/static.go b/internal/edge/static.go| index bddfeadbe..8b91f7da5 100644 |
| --- a/internal/edge/static.go |
| +++ b/internal/edge/static.go |
| @@ -287,12 +287,48 @@ func (s *staticSite) served() servedTree { |
| return servedTree{dir: s.dir(s.name), version: s.version, redirects: s.redirects, headers: s.headers} |
| } |
| +// readRuleFile reads a _headers or _redirects file, never more than its |
| +// parser's limit plus one byte, so an oversized file costs a bounded read |
| +// whatever its size on disk (a devbox folder link comes straight from a |
| +// local user, with no ship check in between). A file over the limit is an |
| +// error, which the loaders log before serving without its rules, as they do |
| +// for any file they cannot read. |
| +func readRuleFile(name string, limit int) ([]byte, error) { |
| + f, err := os.Open(name) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer f.Close() |
| + data, err := io.ReadAll(io.LimitReader(f, int64(limit)+1)) |
| + if err != nil { |
| + return nil, err |
| + } |
| + if len(data) > limit { |
| + return nil, fmt.Errorf("over the %d byte limit", limit) |
| + } |
| + return data, nil |
| +} |
| + |
| +// RuleFileLimit reports the most bytes the edge reads of the rule file a |
| +// tree holds at its root path name (_headers or _redirects), and false for |
| +// any other name. The devbox registrar refuses an upload whose rule file |
| +// is larger. |
| +func RuleFileLimit(name string) (int64, bool) { |
| + switch name { |
| + case headersFile: |
| + return headersMaxBytes, true |
| + case redirectsFile: |
| + return redirectsMaxBytes, true |
| + } |
| + return 0, false |
| +} |
| + |
| // loadRedirects parses the _redirects at the root of an extracted tree, once |
| // per version swap. A tree without one has no rules. A line that does not |
| // parse is logged and skipped: ship refuses such a tree (PackDir), so only a |
| // tree packed some other way gets here, and its remaining rules still apply. |
| func (s *staticSite) loadRedirects(dir, version string) *redirectRules { |
| - data, err := os.ReadFile(filepath.Join(dir, redirectsFile)) |
| + data, err := readRuleFile(filepath.Join(dir, redirectsFile), redirectsMaxBytes) |
| if errors.Is(err, fs.ErrNotExist) { |
| return nil |
| } |
| @@ -311,7 +347,7 @@ func (s *staticSite) loadRedirects(dir, version string) *redirectRules { |
| // version swap, as loadRedirects does _redirects: no file is no rules, and a |
| // line that does not parse is logged and skipped (ship refuses such a tree). |
| func (s *staticSite) loadHeaders(dir, version string) *headerRules { |
| - data, err := os.ReadFile(filepath.Join(dir, headersFile)) |
| + data, err := readRuleFile(filepath.Join(dir, headersFile), headersMaxBytes) |
| if errors.Is(err, fs.ErrNotExist) { |
| return nil |
| } |