Skip to content

Truly independent web browser

LibMedia: Don't crash on bad WebM data

Zaggy10240b8ac8e8ab2e
diff --git a/Libraries/LibMedia/Containers/Matroska/Document.h b/Libraries/LibMedia/Containers/Matroska/Document.h
index 71c36711935..79dd1dbfb02 100644
--- a/Libraries/LibMedia/Containers/Matroska/Document.h
+++ b/Libraries/LibMedia/Containers/Matroska/Document.h
@@ -65,6 +65,23 @@ public:
Metadata = 33,
};
+ static TrackType track_type_from_element_value(u64 value)
+ {
+ switch (value) {
+ case Video:
+ case Audio:
+ case Complex:
+ case Logo:
+ case Subtitle:
+ case Buttons:
+ case Control:
+ case Metadata:
+ return static_cast<TrackType>(value);
+ default:
+ return Invalid;
+ }
+ }
+
enum class ColorRange : u8 {
Unspecified = 0,
Broadcast = 1,
@@ -179,17 +196,16 @@ inline TrackType track_type_from_matroska_track_type(TrackEntry::TrackType type)
case TrackEntry::TrackType::Audio:
return TrackType::Audio;
case TrackEntry::TrackType::Subtitle:
+ case TrackEntry::TrackType::Metadata:
return TrackType::Subtitles;
case TrackEntry::TrackType::Invalid:
- return TrackType::Unknown;
case TrackEntry::TrackType::Complex:
case TrackEntry::TrackType::Logo:
case TrackEntry::TrackType::Buttons:
case TrackEntry::TrackType::Control:
- case TrackEntry::TrackType::Metadata:
- break;
+ return TrackType::Unknown;
}
- VERIFY_NOT_REACHED();
+ return TrackType::Unknown;
}
struct AACCodecIdentifier {
diff --git a/Libraries/LibMedia/Containers/Matroska/Reader.cpp b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
index 9d931a2f829..87dd8a86514 100644
--- a/Libraries/LibMedia/Containers/Matroska/Reader.cpp
+++ b/Libraries/LibMedia/Containers/Matroska/Reader.cpp
@@ -522,7 +522,7 @@ DecoderErrorOr<NonnullRefPtr<TrackEntry>> Reader::parse_track_entry(Streamer& st
dbgln_if(MATROSKA_TRACE_DEBUG, "Read TrackUID attribute: {}", track_entry->track_uid());
break;
case TRACK_TYPE_ID:
- track_entry->set_track_type(static_cast<TrackEntry::TrackType>(TRY(streamer.read_u64())));
+ track_entry->set_track_type(TrackEntry::track_type_from_element_value(TRY(streamer.read_u64())));
dbgln_if(MATROSKA_TRACE_DEBUG, "Read TrackType attribute: {}", to_underlying(track_entry->track_type()));
break;
case TRACK_FLAG_DEFAULT_ID:
diff --git a/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp b/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp
index 4cc02ec8605..5abaa8c1ae7 100644
--- a/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp
+++ b/Libraries/LibMedia/MediaSourceExtensions/WebMByteStreamParser.cpp
@@ -171,6 +171,7 @@ Media::DecoderErrorOr<void> WebMByteStreamParser::parse_initialization_segment(M
m_video_tracks.clear();
m_audio_tracks.clear();
m_text_tracks.clear();
+ m_track_block_contexts.clear();
for (auto const& [track_number, track_entry] : m_track_entries) {
auto* maybe_tracks_for_type = [&] -> Vector<Media::Track>* {
@@ -251,7 +252,10 @@ Media::DecoderErrorOr<ParseMediaSegmentResult> WebMByteStreamParser::parse_media
return Reader::parse_block_group(streamer, timecode, m_segment_information->timestamp_scale(), m_track_block_contexts);
}());
- VERIFY(block.timestamp().has_value());
+ // NB: The parser calculates the timestamp from the context selected by the block's track identifier.
+ // Produce an error if one is not found.
+ if (!block.timestamp().has_value())
+ return Media::DecoderError::corrupted("Block belongs to an unknown track"sv);
// - Block & SimpleBlock elements are in time increasing order consistent with [WEBM].
if (block.timestamp().value() < last_block_timestamp)
diff --git a/Tests/LibMedia/TestParseMatroska.cpp b/Tests/LibMedia/TestParseMatroska.cpp
index c80f178b13c..4d8efcbb8c4 100644
--- a/Tests/LibMedia/TestParseMatroska.cpp
+++ b/Tests/LibMedia/TestParseMatroska.cpp
@@ -1137,3 +1137,14 @@ TEST_CASE(color_element_overrides_the_codec_private_configuration_record)
EXPECT(track.video_data().cicp.color_primaries() == Media::ColorPrimaries::BT709);
EXPECT(track.video_data().cicp.transfer_characteristics() == Media::TransferCharacteristics::BT709);
}
+
+TEST_CASE(unknown_track_type_values_are_invalid)
+{
+ EXPECT_EQ(Media::Matroska::TrackEntry::track_type_from_element_value(33), Media::Matroska::TrackEntry::TrackType::Metadata);
+ EXPECT_EQ(Media::Matroska::TrackEntry::track_type_from_element_value(5), Media::Matroska::TrackEntry::TrackType::Invalid);
+ EXPECT_EQ(Media::Matroska::TrackEntry::track_type_from_element_value(NumericLimits<u64>::max()), Media::Matroska::TrackEntry::TrackType::Invalid);
+
+ // Every value the element can carry maps to a track type without trapping.
+ EXPECT_EQ(Media::Matroska::track_type_from_matroska_track_type(Media::Matroska::TrackEntry::TrackType::Metadata), Media::TrackType::Subtitles);
+ EXPECT_EQ(Media::Matroska::track_type_from_matroska_track_type(Media::Matroska::TrackEntry::TrackType::Control), Media::TrackType::Unknown);
+}
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-webm-block-for-unknown-track.txt b/Tests/LibWeb/Text/expected/HTML/media-source-webm-block-for-unknown-track.txt
new file mode 100644
index 00000000000..9a9cae603c9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-webm-block-for-unknown-track.txt
@@ -0,0 +1,10 @@
+isTypeSupported: true
+SourceBuffer updatestart
+SourceBuffer update
+SourceBuffer updateend
+appending the media segment
+SourceBuffer updatestart
+SourceBuffer error
+SourceBuffer updateend
+MediaSource sourceended
+settled: updating false, readyState ended, buffered 0, sentinel error none
diff --git a/Tests/LibWeb/Text/expected/HTML/media-source-webm-metadata-track.txt b/Tests/LibWeb/Text/expected/HTML/media-source-webm-metadata-track.txt
new file mode 100644
index 00000000000..66bb58e5c19
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/media-source-webm-metadata-track.txt
@@ -0,0 +1,5 @@
+isTypeSupported: true
+SourceBuffer updatestart
+SourceBuffer update
+SourceBuffer updateend
+settled: updating false, readyState open, text tracks 1
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-webm-block-for-unknown-track.html b/Tests/LibWeb/Text/input/HTML/media-source-webm-block-for-unknown-track.html
new file mode 100644
index 00000000000..6af204978fa
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-webm-block-for-unknown-track.html
@@ -0,0 +1,62 @@
+<!DOCTYPE html>
+<video id="video"></video>
+<!-- A healthy element on the same media server: if the bad append crashes the server, every session reports an error,
+ this one included. -->
+<audio id="sentinel" src="../../../Assets/tone.mp3" preload="metadata"></audio>
+<script src="../include.js"></script>
+<script>
+ // A WebM media segment whose SimpleBlock names a track that the initialization segment did not describe must
+ // fail the append through the append error algorithm, never crash the parser.
+ asyncTest(async done => {
+ const video = document.getElementById("video");
+ const sentinel = document.getElementById("sentinel");
+ if (sentinel.readyState < HTMLMediaElement.HAVE_METADATA)
+ await new Promise(resolve => sentinel.addEventListener("loadedmetadata", resolve, { once: true }));
+
+ const mediaSource = new MediaSource();
+ video.src = URL.createObjectURL(mediaSource);
+
+ // An EBML header, a Segment of unknown size, Segment Information, and a Tracks element with one 2x2 VP8
+ // video track numbered 1.
+ const vp8InitSegment = new Uint8Array([
+ 0x1a, 0x45, 0xdf, 0xa3, 0x8b, 0x42, 0x82, 0x84, 0x77, 0x65, 0x62, 0x6d,
+ 0x42, 0x87, 0x81, 0x04, 0x18, 0x53, 0x80, 0x67, 0xff, 0x15, 0x49, 0xa9,
+ 0x66, 0x92, 0x2a, 0xd7, 0xb1, 0x83, 0x0f, 0x42, 0x40, 0x4d, 0x80, 0x88,
+ 0x4c, 0x61, 0x64, 0x79, 0x62, 0x69, 0x72, 0x64, 0x16, 0x54, 0xae, 0x6b,
+ 0x9b, 0xae, 0x99, 0xd7, 0x81, 0x01, 0x73, 0xc5, 0x81, 0x01, 0x83, 0x81,
+ 0x01, 0x86, 0x85, 0x56, 0x5f, 0x56, 0x50, 0x38, 0xe0, 0x86, 0xb0, 0x81,
+ 0x02, 0xba, 0x81, 0x02,
+ ]);
+
+ // A Cluster with Timestamp 0 and one SimpleBlock whose track number is 2.
+ const mediaSegmentForUnknownTrack = new Uint8Array([
+ 0x1f, 0x43, 0xb6, 0x75, 0x8a, 0xe7, 0x81, 0x00, 0xa3, 0x85, 0x82, 0x00,
+ 0x00, 0x80, 0x00,
+ ]);
+
+ mediaSource.addEventListener("sourceopen", () => {
+ println(`isTypeSupported: ${MediaSource.isTypeSupported('video/webm; codecs="vp8"')}`);
+
+ const sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
+
+ for (const eventName of ["updatestart", "update", "error", "abort", "updateend"])
+ sourceBuffer.addEventListener(eventName, () => println(`SourceBuffer ${eventName}`));
+ mediaSource.addEventListener("sourceended", () => println("MediaSource sourceended"));
+
+ sourceBuffer.addEventListener("updateend", () => {
+ println("appending the media segment");
+ sourceBuffer.appendBuffer(mediaSegmentForUnknownTrack);
+
+ sourceBuffer.addEventListener("updateend", () => {
+ setTimeout(() => setTimeout(() => {
+ println(`settled: updating ${sourceBuffer.updating}, readyState ${mediaSource.readyState}, ` +
+ `buffered ${sourceBuffer.buffered.length}, sentinel error ${sentinel.error?.code ?? "none"}`);
+ done();
+ }));
+ }, { once: true });
+ }, { once: true });
+
+ sourceBuffer.appendBuffer(vp8InitSegment);
+ }, { once: true });
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/media-source-webm-metadata-track.html b/Tests/LibWeb/Text/input/HTML/media-source-webm-metadata-track.html
new file mode 100644
index 00000000000..d301774708d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/media-source-webm-metadata-track.html
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<video id="video"></video>
+<script src="../include.js"></script>
+<script>
+ // A WebM initialization segment whose only track has the Matroska Metadata TrackType (33) must be handled as a
+ // text track or rejected as an append error, never crash the parser.
+ asyncTest(done => {
+ const video = document.getElementById("video");
+ const mediaSource = new MediaSource();
+ video.src = URL.createObjectURL(mediaSource);
+
+ // An EBML header, a Segment of unknown size, Segment Information, and a Tracks element with one TrackEntry:
+ // TrackNumber 1, TrackUID 1, TrackType 33 (Metadata), CodecID "D_WEBVTT/metadata".
+ const metadataTrackInitSegment = new Uint8Array([
+ 0x1a, 0x45, 0xdf, 0xa3, 0x8b, 0x42, 0x82, 0x84, 0x77, 0x65, 0x62, 0x6d,
+ 0x42, 0x87, 0x81, 0x04, 0x18, 0x53, 0x80, 0x67, 0xff, 0x15, 0x49, 0xa9,
+ 0x66, 0x92, 0x2a, 0xd7, 0xb1, 0x83, 0x0f, 0x42, 0x40, 0x4d, 0x80, 0x88,
+ 0x4c, 0x61, 0x64, 0x79, 0x62, 0x69, 0x72, 0x64, 0x16, 0x54, 0xae, 0x6b,
+ 0x9f, 0xae, 0x9d, 0xd7, 0x81, 0x01, 0x73, 0xc5, 0x81, 0x01, 0x83, 0x81,
+ 0x21, 0x86, 0x91, 0x44, 0x5f, 0x57, 0x45, 0x42, 0x56, 0x54, 0x54, 0x2f,
+ 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61,
+ ]);
+
+ mediaSource.addEventListener("sourceopen", () => {
+ println(`isTypeSupported: ${MediaSource.isTypeSupported('video/webm; codecs="vp8"')}`);
+
+ const sourceBuffer = mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
+
+ for (const eventName of ["updatestart", "update", "error", "abort", "updateend"])
+ sourceBuffer.addEventListener(eventName, () => println(`SourceBuffer ${eventName}`));
+ mediaSource.addEventListener("sourceended", () => println("MediaSource sourceended"));
+
+ sourceBuffer.appendBuffer(metadataTrackInitSegment);
+
+ sourceBuffer.addEventListener("updateend", () => {
+ setTimeout(() => setTimeout(() => {
+ println(`settled: updating ${sourceBuffer.updating}, readyState ${mediaSource.readyState}, ` +
+ `text tracks ${video.textTracks.length}`);
+ done();
+ }));
+ }, { once: true });
+ }, { once: true });
+ });
+</script>