Skip to content

isobmff: throw on box sizes smaller than their header - #1939

Open
peaBerberian wants to merge 2 commits into
devfrom
fix/isobmff-box-sizes
Open

peaBerberian wants to merge 2 commits into
devfrom
fix/isobmff-box-sizes

Conversation

@peaBerberian

@peaBerberian peaBerberian commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

I recently ran an LLM on our ISOBMFF-related code, hoping to catch issues and to make it more resilient and more aligned with what other tools do with badly-formed data. This led to multiple changes, some still to come.

The problem

When a box announces a size smaller than its own header (8, or 16 with a 64-bit size), our functions looking for a box in ISOBMFF data (getBoxOffsets and getUuidContent) did not see it as an issue:

  • with a 64-bit size of 0 we did not move forward and looped forever
  • for other small sizes, we ended up in the middle of the data and read garbage as the next boxes, or returned an empty box

They had a check for this, but it only looked for negative sizes, which never happens as we read sizes as unsigned.

Other implementations don't agree on what to do here: browsers, GPAC and ExoPlayer generally fail, where most web players just go on. With the changes here, we go on when we can and fail only when we cannot do anything without that box.

What I did here

Those functions, and getNextBoxOffsets, now throw when the size is smaller than the header. I chose to throw instead of returning "no box", as a missing box and data we cannot read are not the same thing and callers may want to do something different for each.

To note that a 32-bit size of 4 is still skipped: it always worked here by luck and GPAC accepts it as a 4-byte filler, so I preferred not breaking such contents.

Then I went through all callers one by one:

  • When the information is optional or when we have a fallback (key id, timing, emsg, mdhd timescale, Smooth's tfrf/tfxd, senc, PSSH...), I catch and consider the box as not there, with a warning log. So no behavior change for those.
  • For emsg and the trun samples of WebVTT embedded in MP4, I keep what was read before the bad box, like most other players seem to do. For this, I moved the loop on moof boxes of getTrunSamples to the WebVTT code, the per-traf part staying in the ISOBMFF utils. I did not do the same for segment durations, as a partial duration would just be wrong without us knowing it.
  • When we cannot do anything without it (text mdat, WebVTT in MP4, Smooth's patchSegment required boxes, sidx when the container is known to be mp4 and an indexRange is announced), we let it throw.

Behavior changes

  • A malformed text segment now makes the text stream fail for the whole Period, where before we just had no subtitle for it. I added a TODO in period_stream.ts as I would prefer to only skip that segment.
  • In low-latency mode, such error on a chunked text segment goes through how we currently handle parsing errors during chunked requests: it is considered as a request error, and is retried or another CDN is tried. This is an already-existing issue for any parsing error, which I will fix separately.
  • A 64-bit size of 8, that ffmpeg reads as "the box goes until the end of the file", now throws. I added a TODO for it.

NOTE: The diff is huge, but it's mostly unit tests as that area was not unit tested before. Most unit tests were LLM-generated, I controlled and reviewed that each does test the behavior we actually want, not accidental one and that the testing style correspond to ours. For the actual source code change, I've personally chose the change on a case by case basis.

@github-actions

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit 0e17d31ca7be3309d5def37aa2fd0b42f54a0532 with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 22.57ms -> 22.35ms (corrected: -0.699ms, A/A bias: 0.915ms, z: 1.05848) 19.35ms -> 19.00ms (corrected: -0.125ms, A/A bias: 0.375ms, z: 0.91414)
seeking 5.71ms -> 5.66ms (corrected: -4.918ms, A/A bias: 4.968ms, z: 0.09623) 5.10ms -> 5.10ms (corrected: 0.100ms, A/A bias: -0.050ms, z: 1.72724)
audio-track-reload 18.62ms -> 18.59ms (corrected: -0.101ms, A/A bias: 0.137ms, z: 0.46188) 17.90ms -> 17.80ms (corrected: 0.000ms, A/A bias: -0.000ms, z: 0.25019)
loading over active content 12.26ms -> 12.48ms (corrected: -0.010ms, A/A bias: -0.208ms, z: 0.39933) 11.65ms -> 11.70ms (corrected: 0.200ms, A/A bias: -0.750ms, z: 0.39933)
large multi-period manifest 61.37ms -> 61.21ms (corrected: 0.207ms, A/A bias: -0.056ms, z: 0.32717) 56.40ms -> 56.70ms (corrected: -1.225ms, A/A bias: 1.500ms, z: 0.32717)
cold loading multithread 38.48ms -> 35.38ms (corrected: 3.166ms, A/A bias: -0.063ms, z: 2.53072) 35.20ms -> 34.50ms (corrected: 0.700ms, A/A bias: 0.000ms, z: 4.92191)
seeking multithread 30.24ms -> 30.27ms (corrected: -15.069ms, A/A bias: 15.035ms, z: 0.66395) 4.90ms -> 4.90ms (corrected: -0.025ms, A/A bias: 0.025ms, z: 0.25500)
audio-track-reload multithread 18.16ms -> 18.03ms (corrected: -0.164ms, A/A bias: 0.292ms, z: 0.48113) 17.60ms -> 17.50ms (corrected: -0.050ms, A/A bias: 0.100ms, z: 0.33679)
hot loading multithread 10.91ms -> 10.84ms (corrected: -2.318ms, A/A bias: 2.393ms, z: 1.04885) 10.55ms -> 10.40ms (corrected: 0.100ms, A/A bias: -0.000ms, z: 1.89082)

@github-actions

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit 4c82e9f057398da9d1c1d8a03b7e61e259ac0b71 with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 22.38ms -> 21.94ms (corrected: -0.424ms, A/A bias: 0.858ms, z: 1.01999) 18.50ms -> 18.50ms (corrected: -0.275ms, A/A bias: 0.225ms, z: 2.01110)
seeking 15.64ms -> 10.65ms (corrected: 7.505ms, A/A bias: -2.518ms, z: 0.43782) 5.00ms -> 5.00ms (corrected: 0.025ms, A/A bias: 0.025ms, z: 0.36084)
audio-track-reload 18.32ms -> 18.40ms (corrected: 0.076ms, A/A bias: -0.155ms, z: 0.00962) 17.70ms -> 17.80ms (corrected: 0.075ms, A/A bias: 0.000ms, z: 0.57254)
loading over active content 11.89ms -> 12.02ms (corrected: -0.514ms, A/A bias: 0.389ms, z: 0.79386) 11.25ms -> 11.65ms (corrected: -1.050ms, A/A bias: 1.025ms, z: 0.79386)
large multi-period manifest 61.23ms -> 61.43ms (corrected: 1.089ms, A/A bias: -1.291ms, z: 0.52924) 56.35ms -> 55.95ms (corrected: 3.225ms, A/A bias: -2.950ms, z: 0.52924)
cold loading multithread 35.55ms -> 34.87ms (corrected: 0.669ms, A/A bias: 0.006ms, z: 2.45855) 34.70ms -> 34.20ms (corrected: 0.300ms, A/A bias: 0.100ms, z: 3.47372)
seeking multithread 42.69ms -> 27.74ms (corrected: 7.276ms, A/A bias: 7.672ms, z: 0.61584) 4.85ms -> 4.80ms (corrected: -0.050ms, A/A bias: 0.100ms, z: 1.02961)
audio-track-reload multithread 17.75ms -> 17.92ms (corrected: -0.107ms, A/A bias: -0.061ms, z: 0.69282) 17.20ms -> 17.30ms (corrected: -0.100ms, A/A bias: -0.100ms, z: 0.34160)
hot loading multithread 10.71ms -> 10.74ms (corrected: -0.015ms, A/A bias: -0.020ms, z: 0.29830) 10.30ms -> 10.35ms (corrected: -0.000ms, A/A bias: 0.025ms, z: 0.81791)

@github-actions

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit ab5280c8fbf926da902c8b39cb65d9d7e976317a with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 24.23ms -> 23.74ms (corrected: 0.523ms, A/A bias: -0.042ms, z: 0.50037) 20.40ms -> 20.20ms (corrected: 0.250ms, A/A bias: -0.250ms, z: 0.88527)
seeking 8.45ms -> 8.33ms (corrected: 5.285ms, A/A bias: -5.168ms, z: 1.63101) 5.30ms -> 5.30ms (corrected: 0.150ms, A/A bias: -0.025ms, z: 2.28534)
audio-track-reload 19.28ms -> 19.25ms (corrected: 0.187ms, A/A bias: -0.156ms, z: 1.01999) 18.50ms -> 18.50ms (corrected: 0.150ms, A/A bias: -0.150ms, z: 1.03442)
loading over active content 12.76ms -> 13.40ms (corrected: -0.228ms, A/A bias: -0.417ms, z: 0.07217) 11.90ms -> 12.30ms (corrected: 1.275ms, A/A bias: -1.375ms, z: 0.07217)
large multi-period manifest 63.94ms -> 63.34ms (corrected: 0.245ms, A/A bias: 0.356ms, z: 0.14434) 57.85ms -> 57.50ms (corrected: -1.250ms, A/A bias: 1.700ms, z: 0.14434)
cold loading multithread 37.26ms -> 36.59ms (corrected: 0.688ms, A/A bias: -0.019ms, z: 1.92450) 36.30ms -> 35.70ms (corrected: 0.550ms, A/A bias: 0.025ms, z: 5.03738)
seeking multithread 37.96ms -> 32.82ms (corrected: -17.405ms, A/A bias: 22.547ms, z: 0.98150) 5.00ms -> 4.90ms (corrected: 0.000ms, A/A bias: 0.050ms, z: 0.15396)
audio-track-reload multithread 18.93ms -> 18.75ms (corrected: 0.053ms, A/A bias: 0.125ms, z: 0.01925) 18.10ms -> 18.10ms (corrected: 0.275ms, A/A bias: 0.050ms, z: 0.84197)
hot loading multithread 13.93ms -> 11.43ms (corrected: 2.418ms, A/A bias: 0.077ms, z: 0.34641) 11.00ms -> 10.95ms (corrected: -0.025ms, A/A bias: 0.075ms, z: 0.30792)

@github-actions

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit 73090f8af1d1bd812b31ef7567b5e06c40e05d8b with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 32.89ms -> 32.79ms (corrected: 0.492ms, A/A bias: -0.394ms, z: 0.23094) 27.70ms -> 27.60ms (corrected: 0.150ms, A/A bias: -0.150ms, z: 0.52924)
seeking 14.21ms -> 11.87ms (corrected: -5.161ms, A/A bias: 7.504ms, z: 0.94782) 8.60ms -> 8.60ms (corrected: -0.050ms, A/A bias: 0.075ms, z: 0.46188)
audio-track-reload 28.82ms -> 28.98ms (corrected: 0.009ms, A/A bias: -0.168ms, z: 0.00000) 27.90ms -> 27.90ms (corrected: 0.125ms, A/A bias: 0.100ms, z: 0.42820)
loading over active content 19.53ms -> 19.75ms (corrected: -0.274ms, A/A bias: 0.053ms, z: 0.15877) 19.05ms -> 19.20ms (corrected: -0.675ms, A/A bias: 0.175ms, z: 0.15877)
large multi-period manifest 97.45ms -> 98.10ms (corrected: -2.830ms, A/A bias: 2.185ms, z: 0.65914) 84.15ms -> 85.10ms (corrected: -4.575ms, A/A bias: 4.650ms, z: 0.65914)
cold loading multithread 52.90ms -> 49.67ms (corrected: 3.294ms, A/A bias: -0.060ms, z: 2.08327) 49.40ms -> 48.50ms (corrected: 1.150ms, A/A bias: -0.050ms, z: 5.66284)
seeking multithread 78.87ms -> 108.66ms (corrected: -37.204ms, A/A bias: 7.411ms, z: 1.13546) 8.30ms -> 8.10ms (corrected: 0.075ms, A/A bias: 0.050ms, z: 1.19800)
audio-track-reload multithread 28.81ms -> 28.62ms (corrected: 0.024ms, A/A bias: 0.164ms, z: 0.48113) 28.10ms -> 28.00ms (corrected: 0.300ms, A/A bias: -0.125ms, z: 0.12509)
hot loading multithread 16.90ms -> 16.82ms (corrected: 0.213ms, A/A bias: -0.128ms, z: 0.98150) 16.40ms -> 16.50ms (corrected: 0.075ms, A/A bias: -0.175ms, z: 0.50518)

@github-actions

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit 074f69d7289e41516aeee03e5a6ead12c0e0c7b9 with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 31.23ms -> 31.49ms (corrected: -2.334ms, A/A bias: 2.069ms, z: 1.48187) 26.20ms -> 26.50ms (corrected: -0.400ms, A/A bias: 0.075ms, z: 2.34308)
seeking 9.03ms -> 16.65ms (corrected: -2.568ms, A/A bias: -5.057ms, z: 0.27905) 8.30ms -> 8.30ms (corrected: -0.100ms, A/A bias: 0.050ms, z: 1.18357)
audio-track-reload 27.75ms -> 28.11ms (corrected: -0.608ms, A/A bias: 0.250ms, z: 1.81865) 26.80ms -> 26.90ms (corrected: -0.325ms, A/A bias: 0.225ms, z: 1.78979)
loading over active content 19.20ms -> 18.92ms (corrected: -0.009ms, A/A bias: 0.281ms, z: 0.54367) 17.80ms -> 18.00ms (corrected: -1.625ms, A/A bias: 0.625ms, z: 0.54367)
large multi-period manifest 95.39ms -> 95.92ms (corrected: -1.140ms, A/A bias: 0.607ms, z: 0.32717) 83.90ms -> 85.45ms (corrected: -2.300ms, A/A bias: 2.050ms, z: 0.32717)
cold loading multithread 48.73ms -> 48.19ms (corrected: 3.054ms, A/A bias: -2.513ms, z: 1.38564) 47.70ms -> 47.15ms (corrected: 0.350ms, A/A bias: 0.100ms, z: 2.56440)
seeking multithread 56.02ms -> 61.16ms (corrected: 19.682ms, A/A bias: -24.828ms, z: 0.31754) 7.90ms -> 8.10ms (corrected: -0.400ms, A/A bias: 0.125ms, z: 2.57402)
audio-track-reload multithread 27.57ms -> 27.81ms (corrected: -0.376ms, A/A bias: 0.142ms, z: 1.08734) 26.90ms -> 27.00ms (corrected: -0.225ms, A/A bias: 0.125ms, z: 1.12102)
hot loading multithread 18.32ms -> 15.94ms (corrected: 2.479ms, A/A bias: -0.097ms, z: 0.06736) 15.50ms -> 15.40ms (corrected: 0.075ms, A/A bias: -0.100ms, z: 0.32717)

peaBerberian added a commit that referenced this pull request Nov 30, 2026
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already
lists all segments. Still, when the MPD also had an `indexRange`, we
requested the index bytes with the initialization segment and parsed
the `sidx` in it. The segment list we got from it was then ignored.

Since #1939, reading that `sidx` can fail on malformed data, and that
error stops playback (or disables subtitles for the Period). So such
contents could now fail where they played before, because of a `sidx`
we don't even use.

I now only keep `indexRange` for SegmentBase, where we need the `sidx`
to know the segments.

To note that dash.js does the opposite: when an `indexRange` is
present, it uses the `sidx` to get the segments, even with
SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player
and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
peaBerberian added a commit that referenced this pull request Nov 30, 2026
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already
lists all segments. Still, when the MPD also had an `indexRange`, we
requested the index bytes with the initialization segment and parsed
the `sidx` in it. The segment list we got from it was then ignored.

Since #1939, reading that `sidx` can fail on malformed data, and that
error stops playback (or disables subtitles for the Period). So such
contents could now fail where they played before, because of a `sidx`
we don't even use.

I now only keep `indexRange` for SegmentBase, where we need the `sidx`
to know the segments.

To note that dash.js does the opposite: when an `indexRange` is
present, it uses the `sidx` to get the segments, even with
SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player
and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
I recently ran an LLM on our ISOBMFF-related code, hoping to catch
issues and to make it more resilient and more aligned with what other
tools do with badly-formed data. This led to multiple changes, some
already done (e.g. the infinite loop on a pssh with a size of 0) and
some still to come. This commit is one of them.

The problem
-----------

When a box announces a size smaller than its own header (`8`, or `16`
with a 64-bit size), our functions looking for a box in ISOBMFF data
(`getBoxOffsets` and `getUuidContent`) did not see it as an issue:
  - with a 64-bit size of `0` we did not move forward and looped
    forever
  - for other small sizes, we ended up in the middle of the data and
    read garbage as the next boxes, or returned an empty box

They had a check for this, but it only looked for negative sizes,
which never happens as we read sizes as unsigned.

What I did here
---------------

Those functions, and `getNextBoxOffsets`, now throw when the size is
smaller than the header. I chose to throw instead of returning "no
box", as a missing box and data we cannot read are not the same thing
and callers may want to do something different for each.

To note that a 32-bit size of `4` is still skipped: it always worked
here by luck and GPAC accepts it as a 4-byte filler, so I preferred
not breaking such contents.

Then I went through all callers one by one:
  - When the information is optional or when we have a fallback (key
    id, timing, `emsg`, `mdhd` timescale, Smooth's `tfrf`/`tfxd`,
    `senc`, PSSH...), I catch and consider the box as not there, with
    a warning log. So no behavior change for those.
  - For `emsg` and the `trun` samples of WebVTT embedded in MP4, I keep
    what was read before the bad box, like most other players seem to
    do. For this, I moved the loop on `moof` boxes of `getTrunSamples`
    to the WebVTT code, the per-`traf` part staying in the ISOBMFF
    utils.
    I did not do the same for segment durations, as a partial
    duration would just be wrong without us knowing it.
  - When we cannot do anything without it (text `mdat`, WebVTT in
    MP4, Smooth's `patchSegment` required boxes, `sidx` when the
    container is known to be mp4 and an `indexRange` is announced),
    we let it throw.

Other implementations don't agree on what to do here: browsers, GPAC
and ExoPlayer generally fail, where most web players just go on. With
those changes, we go on when we can and fail only when we cannot do
anything without that box.

Behavior changes
----------------

  - A malformed text segment now makes the text stream fail for the
    whole Period, where before we just had no subtitle for it. I added
    a TODO in `period_stream.ts` as I would prefer to only skip that
    segment.
  - In low-latency mode, such error on a chunked text segment goes
    through how we currently handle parsing errors during chunked
    requests: it is considered as a request error, and is retried or
    another CDN is tried. This is an already-existing issue for any
    parsing error, which I will fix separately.
  - A 64-bit size of `8`, that ffmpeg reads as "the box goes until the
    end of the file", now throws. I added a TODO for it.
peaBerberian added a commit that referenced this pull request Dec 1, 2026
With SegmentTemplate, SegmentList and SegmentTimeline, the MPD already
lists all segments. Still, when the MPD also had an `indexRange`, we
requested the index bytes with the initialization segment and parsed
the `sidx` in it. The segment list we got from it was then ignored.

Since #1939, reading that `sidx` can fail on malformed data, and that
error stops playback (or disables subtitles for the Period). So such
contents could now fail where they played before, because of a `sidx`
we don't even use.

I now only keep `indexRange` for SegmentBase, where we need the `sidx`
to know the segments.

To note that dash.js does the opposite: when an `indexRange` is
present, it uses the `sidx` to get the segments, even with
SegmentTemplate or SegmentList (see dash.js issue 1457). shaka-player
and Media3 ignore `indexRange` outside of SegmentBase, like we now do.
@peaBerberian
peaBerberian force-pushed the fix/isobmff-box-sizes branch from b3cb638 to cca7d43 Compare December 1, 2026 15:33
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

✅ Automated performance checks have passed on commit 808c394c15d8e279d697bf64ba6ce10f9e277519 with the base branch dev.

Details

Performance tests 1st run output

No significative change in performance for tests:

Name Mean Median
loading 23.42ms -> 24.35ms (corrected: -1.135ms, A/A bias: 0.211ms, z: 0.45226) 19.60ms -> 19.65ms (corrected: -0.075ms, A/A bias: 0.175ms, z: 0.25019)
seeking 15.68ms -> 15.60ms (corrected: 2.598ms, A/A bias: -2.514ms, z: 0.42339) 5.00ms -> 5.00ms (corrected: -0.050ms, A/A bias: 0.100ms, z: 0.63027)
audio-track-reload 19.98ms -> 19.85ms (corrected: 0.486ms, A/A bias: -0.358ms, z: 1.56366) 18.80ms -> 18.90ms (corrected: 0.175ms, A/A bias: -0.150ms, z: 0.63990)
loading over active content 15.44ms -> 15.02ms (corrected: 0.509ms, A/A bias: -0.088ms, z: 0.65433) 13.95ms -> 13.75ms (corrected: 2.825ms, A/A bias: -0.550ms, z: 0.65433)
large multi-period manifest 66.98ms -> 67.03ms (corrected: 0.031ms, A/A bias: -0.085ms, z: 0.36084) 59.40ms -> 59.80ms (corrected: 4.975ms, A/A bias: -1.750ms, z: 0.36084)
cold loading multithread 36.62ms -> 36.04ms (corrected: 0.553ms, A/A bias: 0.027ms, z: 1.13064) 35.50ms -> 35.20ms (corrected: 0.700ms, A/A bias: -0.175ms, z: 3.82495)
seeking multithread 62.66ms -> 95.29ms (corrected: -27.848ms, A/A bias: -4.779ms, z: 1.32791) 4.70ms -> 4.70ms (corrected: -0.125ms, A/A bias: 0.100ms, z: 1.67432)
audio-track-reload multithread 19.33ms -> 19.46ms (corrected: -0.302ms, A/A bias: 0.169ms, z: 1.47224) 18.60ms -> 18.70ms (corrected: -0.375ms, A/A bias: 0.275ms, z: 1.56847)
hot loading multithread 13.46ms -> 13.39ms (corrected: 0.146ms, A/A bias: -0.080ms, z: 0.69282) 13.00ms -> 12.85ms (corrected: 0.100ms, A/A bias: -0.125ms, z: 0.68801)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant