LibWeb: Disallow creating a FileAPI::FileList with a vector of files

This factory forced callers to make a list of GC-allocated FileAPI::File
objects. This isn't safe - this opens a window for these files to be
garbage collected before the FileList object stores / visits the list.

Instead, only allow creating an empty FileList and incrementally adding
files to that list.
This commit is contained in:
Timothy Flynn 2024-08-18 10:48:22 -04:00 committed by Andreas Kling
commit 8b4d28b5fd
Notes: github-actions[bot] 2024-08-19 11:30:30 +00:00
3 changed files with 8 additions and 21 deletions

View file

@ -14,23 +14,11 @@ namespace Web::FileAPI {
JS_DEFINE_ALLOCATOR(FileList);
JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
{
return realm.heap().allocate<FileList>(realm, realm, move(files));
}
JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm)
{
return realm.heap().allocate<FileList>(realm, realm);
}
FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
: Bindings::PlatformObject(realm)
, m_files(move(files))
{
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
}
FileList::FileList(JS::Realm& realm)
: Bindings::PlatformObject(realm)
{

View file

@ -22,9 +22,10 @@ class FileList
JS_DECLARE_ALLOCATOR(FileList);
public:
[[nodiscard]] static JS::NonnullGCPtr<FileList> create(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
[[nodiscard]] static JS::NonnullGCPtr<FileList> create(JS::Realm&);
void add_file(JS::NonnullGCPtr<File> file) { m_files.append(file); }
virtual ~FileList() override;
// https://w3c.github.io/FileAPI/#dfn-length
@ -49,7 +50,6 @@ public:
virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory&) override;
private:
FileList(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
explicit FileList(JS::Realm&);
virtual void initialize(JS::Realm&) override;