Skip to content

Truly independent web browser

LibWeb: Return nothing for a SourceBufferList index out of range

Andreas Klinga3267db78238
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.cpp b/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.cpp
index 47cb179f4fe..e52c329a71a 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.cpp
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.cpp
@@ -60,8 +60,14 @@ size_t SourceBufferList::length() const
return m_buffers.size();
}
-GC::Ref<SourceBuffer> const& SourceBufferList::item(u32 index) const
+// https://w3c.github.io/media-source/#dfn-sourcebufferlist-getter
+GC::Ptr<SourceBuffer> SourceBufferList::item(u32 index) const
{
+ // 1. If index is greater than or equal to the length attribute then return undefined and abort these steps.
+ if (index >= m_buffers.size())
+ return nullptr;
+
+ // 2. Return the index'th SourceBuffer object in the list.
return m_buffers[index];
}
diff --git a/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.h b/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.h
index 83c4993e984..0bde8e4a249 100644
--- a/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.h
+++ b/Libraries/LibWeb/MediaSourceExtensions/SourceBufferList.h
@@ -20,7 +20,7 @@ public:
void remove_all_buffers(Badge<MediaSource>);
size_t length() const;
- GC::Ref<SourceBuffer> const& item(u32 index) const;
+ GC::Ptr<SourceBuffer> item(u32 index) const;
void set_onaddsourcebuffer(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onaddsourcebuffer();
diff --git a/Meta/Generators/libweb_bindings/named_and_indexed_properties.py b/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
index eca24223f2b..c0b93c421b8 100644
--- a/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
+++ b/Meta/Generators/libweb_bindings/named_and_indexed_properties.py
@@ -171,8 +171,6 @@ def indexed_property_getter_value_mode(interface: Interface) -> str:
return "optional"
if interface.name in ("SVGLengthList", "SVGNumberList", "SVGPointList", "SVGStringList", "SVGTransformList"):
return "svg_list"
- if interface.name in ("SourceBufferList",):
- return "value"
return "pointer"
diff --git a/Tests/LibWeb/Crash/MediaSourceExtensions/source-buffer-list-index-out-of-range.html b/Tests/LibWeb/Crash/MediaSourceExtensions/source-buffer-list-index-out-of-range.html
new file mode 100644
index 00000000000..069930afc37
--- /dev/null
+++ b/Tests/LibWeb/Crash/MediaSourceExtensions/source-buffer-list-index-out-of-range.html
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<script>
+ const mediaSource = new MediaSource();
+ mediaSource.sourceBuffers[0];
+ mediaSource.activeSourceBuffers[7];
+ mediaSource.sourceBuffers[4294967294];
+</script>
diff --git a/Tests/LibWeb/Text/expected/HTML/source-buffer-list-index-out-of-range.txt b/Tests/LibWeb/Text/expected/HTML/source-buffer-list-index-out-of-range.txt
new file mode 100644
index 00000000000..956233703e5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/source-buffer-list-index-out-of-range.txt
@@ -0,0 +1,18 @@
+sourceBuffers[0] is undefined: true
+sourceBuffers has index 0: false
+sourceBuffers has an own property at 0: false
+sourceBuffers[7] is undefined: true
+sourceBuffers has index 7: false
+sourceBuffers has an own property at 7: false
+sourceBuffers[4294967294] is undefined: true
+sourceBuffers has index 4294967294: false
+sourceBuffers has an own property at 4294967294: false
+activeSourceBuffers[0] is undefined: true
+activeSourceBuffers has index 0: false
+activeSourceBuffers has an own property at 0: false
+activeSourceBuffers[7] is undefined: true
+activeSourceBuffers has index 7: false
+activeSourceBuffers has an own property at 7: false
+activeSourceBuffers[4294967294] is undefined: true
+activeSourceBuffers has index 4294967294: false
+activeSourceBuffers has an own property at 4294967294: false
diff --git a/Tests/LibWeb/Text/input/HTML/source-buffer-list-index-out-of-range.html b/Tests/LibWeb/Text/input/HTML/source-buffer-list-index-out-of-range.html
new file mode 100644
index 00000000000..a2479e68d4e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/source-buffer-list-index-out-of-range.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const mediaSource = new MediaSource();
+ for (const name of ["sourceBuffers", "activeSourceBuffers"]) {
+ const buffers = mediaSource[name];
+ for (const index of [0, 7, 4294967294]) {
+ println(`${name}[${index}] is undefined: ${buffers[index] === undefined}`);
+ println(`${name} has index ${index}: ${index in buffers}`);
+ println(`${name} has an own property at ${index}: ${Object.hasOwn(buffers, index)}`);
+ }
+ }
+ });
+</script>