diff --git a/README.md b/README.md index 89f7fba..fb1a999 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,12 @@ Block's lacing header, because the reader unlaces every block up front and a ([go-avkit/avkit#120](https://github.com/go-avkit/avkit/issues/120)). More than `MaxTrackSamples` frames in a track, or twice that in the file, is `video.limit` (a 70 KB file of 10,000 laced blocks is refused with under 3 KB allocated). The -MP4 sample tables are not pre-scanned: the reader's own bounds apply there. +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. 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 0ef89cd..3c24692 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.16.0 + github.com/go-avkit/avkit v0.17.0 github.com/go-avkit/decode v0.5.4 ) diff --git a/go.sum b/go.sum index a5f0504..cc406d4 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.16.0 h1:QoFoIgAHy8xv+kdnROQ8P3GSqveV5/BJtxPa0i+ZM64= -github.com/go-avkit/avkit v0.16.0/go.mod h1:itL/gmFP0/hIVAQgxn0JTTvlGgY9vi5uL8XI2sOQl/c= +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/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 c077168..f751229 100644 --- a/serve/lace_test.go +++ b/serve/lace_test.go @@ -4,7 +4,9 @@ package serve import ( + "errors" "math" + "strings" "testing" "github.com/go-avkit/worker/proto" @@ -43,6 +45,23 @@ func laced(blocks, n, tracks int, group bool) []byte { return d } +// The reader's own bound (avkit v0.17.0) is a second line: with the pre-scan +// bypassed (open called directly), the reader still refuses the bomb as +// video.limit, and a lowered MaxTrackSamples is passed on to it. +func TestReaderRefusesLacedBombWithoutThePrescan(t *testing.T) { + data := laced(1000, 256, 1, false) // 256,000 frames + lim := proto.DefaultLimits + lim.MaxTrackSamples = 100000 + if _, _, err := sniff(data, lim); err == nil { + t.Fatal("the pre-scan accepted it: this test would not reach the reader") + } + _, err := open(data, "webm", "video/webm", lim) + 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) + } +} + 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 b752b18..d8e07e9 100644 --- a/serve/poster.go +++ b/serve/poster.go @@ -30,7 +30,14 @@ type file struct { // open reads the description of data, which sniff has accepted. func open(data []byte, format, mime string, lim proto.Limits) (*file, error) { - r, err := container.NewReader(data) + // the reader's own bound on the frames it unpacks is the second line, after + // 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, + }) + if errors.Is(err, container.ErrReaderLimit) { + return nil, refuse(proto.CodeLimit, "the %s container is over a reader limit: %v", format, err) + } if err != nil { return nil, refuse(proto.CodeSyntax, "the %s container cannot be read: %v", format, err) }