From c66a1212ddd9645cda95a20b32ae3d74be95365b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sat, 14 Nov 2026 23:29:42 +0100 Subject: [PATCH] serve: pass the box and depth limits to the reader (avkit v0.18.0) Bump go-avkit/avkit to v0.18.0 and give ReaderLimits the request's clamped MaxBoxes and MaxDepth, so MP4 is bounded by the reader too. A test bypasses the pre-scan with a box flood inside moov: video.limit, small allocation; on v0.17.0 it is not refused as a limit and allocates 2.7 MB. Refs go-avkit/avkit#89 Co-Authored-By: Claude Sonnet 5.5 --- README.md | 8 +++++--- go.mod | 2 +- go.sum | 4 ++-- serve/lace_test.go | 30 ++++++++++++++++++++++++++++++ serve/poster.go | 1 + 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fb1a999..2cd6be9 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,11 @@ Block's lacing header, because the reader unlaces every block up front and a set of container elements the scan descends into is derived from ebml-go's element table, and a test fails if they differ. The pre-scan is the first line; the second is the reader's own bound (go-avkit/avkit v0.17.0, -`container.NewReaderWithLimits`), called with the same clamped limits, whose -`ErrReaderLimit` is answered `video.limit`; a test bypasses the pre-scan and the -reader still refuses. The MP4 sample tables are not pre-scanned: the reader's own bounds apply there. +`container.NewReaderWithLimits`, v0.18.0 for MP4 boxes), called with the same +clamped limits (frames per track and per file, `MaxBoxes`, `MaxDepth`), whose +`ErrReaderLimit` is answered `video.limit`; tests bypass the pre-scan, with a +laced Matroska bomb and an MP4 box flood inside `moov`, and the reader still +refuses. The MP4 sample tables are not pre-scanned: the reader's own bounds apply there. A panic in the reader or the decoder is answered as a `video.syntax` refusal, in `Server.Handle` and again at the worker boundary, and the worker keeps serving. diff --git a/go.mod b/go.mod index 3c24692..9753344 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.27.1 require ( github.com/at-wat/ebml-go v0.19.4 - github.com/go-avkit/avkit v0.17.0 + github.com/go-avkit/avkit v0.18.0 github.com/go-avkit/decode v0.5.4 ) diff --git a/go.sum b/go.sum index cc406d4..744f84d 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,8 @@ github.com/at-wat/ebml-go v0.19.4 h1:6F767ThzH/JqNUsF79PwRFP0pwUF5ALXWJ6pXuiolI8 github.com/at-wat/ebml-go v0.19.4/go.mod h1:wu3fQj+DWo6SOOl9c4yDi+gFs9Oa96Z2B5qxo1OxFYY= github.com/gen2brain/h265 v0.2.4 h1:JUSbGkRAYZpd7ucCL192yx4ViEo75ZO6OEdm1TQt9qM= github.com/gen2brain/h265 v0.2.4/go.mod h1:kefpLxLhGpNCUG/q/aabJcD6GqB9Nhet3fNi9xX9Wzw= -github.com/go-avkit/avkit v0.17.0 h1:N4VYofsCswBtQb/Lb7ouhMchvtasKYq6dCMLOMiWrXU= -github.com/go-avkit/avkit v0.17.0/go.mod h1:itL/gmFP0/hIVAQgxn0JTTvlGgY9vi5uL8XI2sOQl/c= +github.com/go-avkit/avkit v0.18.0 h1:7QffdFBr7Scui8aPwy3G/a61rPzAmnjCQZ5xfefaxmE= +github.com/go-avkit/avkit v0.18.0/go.mod h1:itL/gmFP0/hIVAQgxn0JTTvlGgY9vi5uL8XI2sOQl/c= github.com/go-avkit/bitstream v0.3.1 h1:hZL/9IyRQaXmDd8iEm8ZKT79Tec4pS+HZ+wnQiLaQSc= github.com/go-avkit/bitstream v0.3.1/go.mod h1:aA50I8nXKyJu6ElfTHM957BRrx5JjgSYUxs++n5VM8o= github.com/go-avkit/decode v0.5.4 h1:j4jJYR5MHdLzljxAz/nlOScBtLE31OSkzf4jh3G2hXU= diff --git a/serve/lace_test.go b/serve/lace_test.go index f751229..63a8351 100644 --- a/serve/lace_test.go +++ b/serve/lace_test.go @@ -6,6 +6,7 @@ package serve import ( "errors" "math" + "runtime" "strings" "testing" @@ -62,6 +63,35 @@ func TestReaderRefusesLacedBombWithoutThePrescan(t *testing.T) { } } +// The reader's box bound (avkit v0.18.0) is the second line for MP4: a flood of +// boxes inside moov, with the pre-scan bypassed, is refused as video.limit by +// the reader with a small allocation. avkit v0.17.0 has no such bound and this +// fails there. +func TestReaderRefusesMP4BoxFloodWithoutThePrescan(t *testing.T) { + var flood []byte + for range 20000 { + flood = append(flood, box("free", nil)...) + } + data := append(append([]byte{}, ftyp...), box("moov", flood)...) + lim := proto.DefaultLimits + lim.MaxBoxes = 1000 + if _, _, err := sniff(data, lim); err == nil { + t.Fatal("the pre-scan accepted it: this test would not reach the reader") + } + var before, after runtime.MemStats + runtime.GC() + runtime.ReadMemStats(&before) + _, err := open(data, "mp4", "video/mp4", lim) + runtime.ReadMemStats(&after) + var e *proto.Error + if !errors.As(err, &e) || e.Code != proto.CodeLimit || !strings.Contains(e.Msg, "reader limit") { + t.Errorf("open with the pre-scan bypassed: %v", err) + } + if got := after.TotalAlloc - before.TotalAlloc; got > 1<<20 { + t.Errorf("refusing cost %d bytes of allocation", got) + } +} + func TestBlockFrames(t *testing.T) { for name, tc := range map[string]struct { body []byte diff --git a/serve/poster.go b/serve/poster.go index d8e07e9..f704938 100644 --- a/serve/poster.go +++ b/serve/poster.go @@ -34,6 +34,7 @@ func open(data []byte, format, mime string, lim proto.Limits) (*file, error) { // the pre-scan: the same limits, a request can only lower them r, err := container.NewReaderWithLimits(data, container.ReaderLimits{ MaxTrackFrames: lim.MaxTrackSamples, MaxFrames: 2 * lim.MaxTrackSamples, + MaxBoxes: lim.MaxBoxes, MaxBoxDepth: lim.MaxDepth, }) if errors.Is(err, container.ErrReaderLimit) { return nil, refuse(proto.CodeLimit, "the %s container is over a reader limit: %v", format, err)