carlos / platform Public

applog: bound a chunk's decoded size before the console reads it (H5-M7)

A box writes its own log and error chunks, and DecodeJSONL decoded the
whole gzip stream before any filter or collect cap: 1.2 KB of gzip cost
the console 374 MiB. Decoding now stops at 8192 lines or 64 MiB (well
over the shipper's 4096-record, 8 KiB-line chunks), read through a
LimitedReader one byte over. ReadChunks leaves a refused chunk out and
reports the answer as trimmed. Census: applog envelope re-pinned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev c2eccb66d26955e9b24cb05bb3b2f767ba5c7d27 parent 9dbc7b1
8 files changed, +176 −5
  • docs/superpowers/notes/census-proposal-sec-console-dos.md +11 −0
  • internal/applog/chunk.go +30 −1
  • internal/applog/chunk_test.go +26 −0
  • internal/applog/read.go +17 −4
  • internal/applog/read_test.go +29 −0
  • internal/console/hunt_H5-M7_test.go +59 −0
  • internal/provision/testdata/plan14-root-reads-chain.tsv +1 −0
  • internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv +3 −0
diff --git a/docs/superpowers/notes/census-proposal-sec-console-dos.md b/docs/superpowers/notes/census-proposal-sec-console-dos.md
new file mode 100644
index 000000000..4871c5edf
--- /dev/null
+++ b/docs/superpowers/notes/census-proposal-sec-console-dos.md
@@ -0,0 +1,11 @@
+# Census approval: sec-console-dos
+
+Ruling `census-ruling-sec-console-dos`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26).
+
+Envelopes re-pinned: `internal/applog`.
+
+Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner.
+
+Seal: `4eda67fee53800e2cfa4177b954c7f0fce6bdfcfbfe1aa2547ddcec3027b668b` → `4eda67fee53800e2cfa4177b954c7f0fce6bdfcfbfe1aa2547ddcec3027b668b`.
+
+Overlay: `internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`.
diff --git a/internal/applog/chunk.go b/internal/applog/chunk.go
index b7935d7fa..f24bd43fc 100644
--- a/internal/applog/chunk.go
+++ b/internal/applog/chunk.go
@@ -184,25 +184,54 @@ func EncodeJSONL[T any](recs []T) ([]byte, error) {
return buf.Bytes(), nil
}
+// MaxChunkLines and MaxChunkBytes bound what DecodeJSONL will decode from
+// one chunk (H5-M7). A chunk is written by a box, so the reader cannot
+// trust its size: a few KiB of gzip can expand to hundreds of thousands of
+// records, all decoded before any filter or collect cap could drop them.
+// The bounds sit well above anything a shipper writes: host's logship and
+// errship put at most logshipMaxPending (4096) records in one chunk, and an
+// app line is at most execLogLineMax (8 KiB) before the tee truncates it,
+// so a genuine chunk is under 4096 lines and about 32 MiB decoded.
+const (
+ MaxChunkLines = 8192
+ MaxChunkBytes = 64 << 20
+)
+
+// ErrChunkTooLarge is DecodeJSONL refusing a chunk past MaxChunkLines or
+// MaxChunkBytes. No shipper writes one, so it is not a log to show.
+var ErrChunkTooLarge = errors.New("applog: chunk decodes past its bound")
+
// DecodeJSONL parses a chunk, skipping malformed lines rather than failing
// the whole object — one corrupt line must not cost the member the rest of
// the hour. Corrupt gzip is an error: that is a broken object, not a line.
+// A chunk that decodes past MaxChunkLines lines or MaxChunkBytes bytes is
+// refused whole with ErrChunkTooLarge, and decoding stops at the bound: the
+// gzip stream is read through a limit one byte over MaxChunkBytes, which is
+// how an over-long stream shows itself.
func DecodeJSONL[T any](b []byte) ([]T, error) {
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return nil, err
}
defer zr.Close()
+ lr := &io.LimitedReader{R: zr, N: MaxChunkBytes + 1}
var out []T
- sc := bufio.NewScanner(zr)
+ sc := bufio.NewScanner(lr)
sc.Buffer(nil, 1<<20) // lines are capped well below this at the tee
+ lines := 0
for sc.Scan() {
+ if lines++; lines > MaxChunkLines {
+ return nil, ErrChunkTooLarge
+ }
var r T
if json.Unmarshal(sc.Bytes(), &r) != nil {
continue
}
out = append(out, r)
}
+ if lr.N <= 0 {
+ return nil, ErrChunkTooLarge
+ }
if err := sc.Err(); err != nil && err != io.EOF {
return out, err
}
diff --git a/internal/applog/chunk_test.go b/internal/applog/chunk_test.go
index fb9d00c32..a2b440526 100644
--- a/internal/applog/chunk_test.go
+++ b/internal/applog/chunk_test.go
@@ -3,6 +3,7 @@ package applog
import (
"bytes"
"compress/gzip"
+ "errors"
"io"
"testing"
"time"
@@ -188,3 +189,28 @@ func must(t testing.TB) func(string, error) string {
return k
}
}
+
+// A chunk is box-written, so its decoded size is not trusted (H5-M7):
+// past MaxChunkLines lines or MaxChunkBytes bytes it is refused whole,
+// and a chunk at the bounds still decodes.
+func TestDecodeJSONLRefusesPastItsBounds(t *testing.T) {
+ line := `{"msg":"x"}` + "\n"
+ atLines := mustGzip(t, bytes.Repeat([]byte(line), MaxChunkLines))
+ if recs, err := DecodeChunk(atLines); err != nil || len(recs) != MaxChunkLines {
+ t.Fatalf("chunk of exactly MaxChunkLines: %d records, %v", len(recs), err)
+ }
+ overLines := mustGzip(t, bytes.Repeat([]byte(line), MaxChunkLines+1))
+ if recs, err := DecodeChunk(overLines); !errors.Is(err, ErrChunkTooLarge) || recs != nil {
+ t.Fatalf("chunk of MaxChunkLines+1: %d records, %v; want ErrChunkTooLarge", len(recs), err)
+ }
+ // Few lines, many bytes: whitespace padding carries the size.
+ pad := bytes.Repeat([]byte(" "), 1<<20-1)
+ var big []byte
+ for len(big) <= MaxChunkBytes {
+ big = append(big, pad...)
+ big = append(big, '\n')
+ }
+ if recs, err := DecodeChunk(mustGzip(t, big)); !errors.Is(err, ErrChunkTooLarge) || recs != nil {
+ t.Fatalf("chunk over MaxChunkBytes: %d records, %v; want ErrChunkTooLarge", len(recs), err)
+ }
+}
diff --git a/internal/applog/read.go b/internal/applog/read.go
index 207a4946c..16067faa0 100644
--- a/internal/applog/read.go
+++ b/internal/applog/read.go
@@ -2,6 +2,7 @@ package applog
import (
"context"
+ "errors"
"strings"
"sync"
"time"
@@ -23,8 +24,10 @@ const readConcurrency = 16
// order. Hour pruning happens at the PREFIX level — an hour outside the
// window is never listed, which is what makes a 1h read over 7d of history
// cheap (spec §3). collectCap bounds memory per box: past it the OLDEST
-// records go and trimmed says so. Store errors fail the read — a partial
-// answer that looks whole is the one thing this must never produce.
+// records go and trimmed says so. A chunk DecodeJSONL refuses as too large
+// (ErrChunkTooLarge) is left out, and trimmed says that too. Store errors
+// fail the read — a partial answer that looks whole is the one thing this
+// must never produce.
//
// Listings and GETs run readConcurrency at a time, but the answer is
// assembled strictly in listing order, so it is exactly what a serial walk
@@ -87,8 +90,9 @@ func ReadChunks[T any](ctx context.Context, s release.Store, prefix string, sinc
// fetch whose result is never read (the read failed first) still ends;
// the feeder stops at cancel.
type fetched struct {
- recs []T
- err error
+ recs []T
+ refused bool // DecodeJSONL refused the chunk as too large
+ err error
}
slots := make([]chan fetched, len(chunks))
for i := range slots {
@@ -109,6 +113,12 @@ func ReadChunks[T any](ctx context.Context, s release.Store, prefix string, sinc
return
}
all, err := DecodeJSONL[T](body)
+ if errors.Is(err, ErrChunkTooLarge) {
+ // Not a chunk any shipper writes: leave it out and
+ // say the answer is not the whole window.
+ slots[i] <- fetched{refused: true}
+ return
+ }
if err != nil {
slots[i] <- fetched{err: err}
return
@@ -136,6 +146,9 @@ func ReadChunks[T any](ctx context.Context, s release.Store, prefix string, sinc
if f.err != nil {
return nil, false, f.err
}
+ if f.refused {
+ trimmed = true
+ }
recs := append(perBox[c.box], f.recs...)
if over := len(recs) - collectCap; over > 0 {
recs = recs[over:]
diff --git a/internal/applog/read_test.go b/internal/applog/read_test.go
index 316484154..ba3aae73a 100644
--- a/internal/applog/read_test.go
+++ b/internal/applog/read_test.go
@@ -223,3 +223,32 @@ func TestReadChunksConcurrentIsSerialOrder(t *testing.T) {
t.Fatal("a failed chunk must fail the read, never return a partial answer")
}
}
+
+// A chunk DecodeJSONL refuses as too large (H5-M7) is left out of the
+// answer, which says it is not the whole window; the other chunks read.
+func TestReadChunksSkipsARefusedChunk(t *testing.T) {
+ ctx := context.Background()
+ s := release.NewDirStore(t.TempDir())
+ now := time.Date(2026, 9, 7, 10, 30, 0, 0, time.UTC)
+ good, _ := EncodeChunk([]Record{{TS: now, Box: "box1", Seq: 1, Stream: StreamApp, Msg: "kept"}})
+ var lines []Record
+ for i := 0; i <= MaxChunkLines; i++ {
+ lines = append(lines, Record{TS: now, Box: "box1", Seq: uint64(2 + i), Stream: StreamApp})
+ }
+ bad, _ := EncodeChunk(lines)
+ for seq, body := range map[uint64][]byte{1: good, 2: bad} {
+ if _, err := s.Put(ctx, must(t)(ChunkKey("bde", "ledgerpay", "box1", HourOf(now), seq, len(body))), body, ""); err != nil {
+ t.Fatal(err)
+ }
+ }
+ perBox, trimmed, err := ReadChunks[Record](ctx, s, must(t)(Prefix("bde", "ledgerpay")), time.Time{}, time.Time{}, func(Record) bool { return true }, 1000)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !trimmed {
+ t.Error("trimmed = false; a refused chunk must mark the answer partial")
+ }
+ if len(perBox) != 1 || len(perBox[0]) != 1 || perBox[0][0].Msg != "kept" {
+ t.Fatalf("perBox = %+v; want only the good chunk's record", perBox)
+ }
+}
diff --git a/internal/console/hunt_H5-M7_test.go b/internal/console/hunt_H5-M7_test.go
new file mode 100644
index 000000000..e86c330ff
--- /dev/null
+++ b/internal/console/hunt_H5-M7_test.go
@@ -0,0 +1,59 @@
+package console
+
+// Hunt H5-M7 (verifier: opus). A fleet box may PUT any body up to 32 MiB
+// under apps/<own sqid>/<app>/logs/** through the edge store door
+// (storedoor.go aclWriteAppFamily + io.ReadAll(MaxBytesReader(32 MiB)) ->
+// store.Put, no content check). The console's logs door then hands every
+// chunk in the window to applog.ReadChunks, whose DecodeJSONL decodes the
+// whole gzip stream into one []T before the filter or collectCap run.
+//
+// This test writes the chunk straight into the deployment store (the bytes
+// the store door would have stored verbatim) and drives the REAL CLI logs
+// route with limit=1. It asserts the secure behaviour: a chunk a few KiB
+// compressed must not cost the console tens of MiB of heap.
+
+import (
+ "bytes"
+ "compress/gzip"
+ "context"
+ "net/http"
+ "runtime"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/applog"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+func TestHunt_H5_M7_GzipChunkFullyDecoded(t *testing.T) {
+ fx, token := instanceFixture(t, "code-h5m7")
+ now := time.Now().UTC()
+
+ const lines = 400_000
+ var buf bytes.Buffer
+ zw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
+ zw.Write([]byte(strings.Repeat("{}\n", lines)))
+ zw.Close()
+ body := buf.Bytes()
+ key := mustKey(t)(applog.ChunkKey(release.OpsAccount, "titogo", "box1", applog.HourOf(now), 1, len(body)))
+ if _, err := fx.rel.Put(context.Background(), key, body, ""); err != nil {
+ t.Fatal(err)
+ }
+
+ runtime.GC()
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ status, _, out := rawReq(t, http.MethodGet, logsURL(fx, fx.sqid, "titogo", "limit=1"), token, "")
+ runtime.ReadMemStats(&after)
+ if status != http.StatusOK {
+ t.Fatalf("GET logs = %d %s", status, out)
+ }
+ alloc := after.TotalAlloc - before.TotalAlloc
+ const budget = 32 << 20
+ if alloc > budget {
+ t.Fatalf("H5-M7: one logs read (limit=1) of a %d-byte gzip chunk allocated %d MiB in the console "+
+ "(%d records decoded before any filter or cap; ratio %dx). A 32 MiB chunk at this ratio is ~%d GiB of records. Want <= %d MiB.",
+ len(body), alloc>>20, lines, alloc/uint64(len(body)), (alloc*uint64(32<<20)/uint64(len(body)))>>30, budget>>20)
+ }
+}
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv
index 60fa52cca..87e6c14ef 100644
--- a/internal/provision/testdata/plan14-root-reads-chain.tsv
+++ b/internal/provision/testdata/plan14-root-reads-chain.tsv
@@ -232,3 +232,4 @@ plan14-root-reads-omr-run.tsv f82b5e513d7434e1440c13aa4aa8bc06348745749a21593cf4
plan14-root-reads-fleet-first-reconcile-speed.tsv 9206205b2a3bf27b8b85310f4a44f94ef0c5886d85d6e55dbf2f22e073a8af11
plan14-root-reads-free-storage-250.tsv 9206205b2a3bf27b8b85310f4a44f94ef0c5886d85d6e55dbf2f22e073a8af11
plan14-root-reads-fleet-first-reconcile-speed-4.tsv 4eda67fee53800e2cfa4177b954c7f0fce6bdfcfbfe1aa2547ddcec3027b668b
+plan14-root-reads-sec-console-dos.tsv 4eda67fee53800e2cfa4177b954c7f0fce6bdfcfbfe1aa2547ddcec3027b668b
diff --git a/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv b/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv
new file mode 100644
index 000000000..22ff36464
--- /dev/null
+++ b/internal/provision/testdata/plan14-root-reads-sec-console-dos.tsv
@@ -0,0 +1,3 @@
+# Census overlay sec-console-dos, written by census-propose (#369). census-ruling-sec-console-dos, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-sec-console-dos.md.
+# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended.
+@coverage internal/applog 174a18f85513dff5db6c0b7c3e5e41a72af3ba607abfb97550fe86cef476b2aa