Skip to content

Commit d91d6ee

Browse files
committed
LibWeb: Stop leaking entire realms via Blob URLs
This patch implements the File API spec's supplemental steps for document's "unloading document cleanup steps" so that we now remove blob URLs associated with the document's relevant settings object when the document is being unloaded. Fixes two realm leaks when running our test suite.
1 parent 696cf7b commit d91d6ee

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Userland/Libraries/LibWeb/DOM/Document.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
2+
* Copyright (c) 2018-2024, Andreas Kling <kling@serenityos.org>
33
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
44
* Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
55
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
@@ -52,6 +52,7 @@
5252
#include <LibWeb/DOM/Text.h>
5353
#include <LibWeb/DOM/TreeWalker.h>
5454
#include <LibWeb/Dump.h>
55+
#include <LibWeb/FileAPI/BlobURLStore.h>
5556
#include <LibWeb/HTML/AttributeNames.h>
5657
#include <LibWeb/HTML/BrowsingContext.h>
5758
#include <LibWeb/HTML/CustomElements/CustomElementDefinition.h>
@@ -2988,6 +2989,8 @@ void Document::run_unloading_cleanup_steps()
29882989
// 2. Clear window's map of active timers.
29892990
window->clear_map_of_active_timers();
29902991
}
2992+
2993+
FileAPI::run_unloading_cleanup_steps(*this);
29912994
}
29922995

29932996
// https://html.spec.whatwg.org/multipage/document-lifecycle.html#destroy-a-document

Userland/Libraries/LibWeb/FileAPI/BlobURLStore.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/*
22
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
3+
* Copyright (c) 2024, Andreas Kling <kling@serenityos.org>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
67

78
#include <AK/StringBuilder.h>
89
#include <LibURL/URL.h>
910
#include <LibWeb/Crypto/Crypto.h>
11+
#include <LibWeb/DOM/Document.h>
1012
#include <LibWeb/FileAPI/Blob.h>
1113
#include <LibWeb/FileAPI/BlobURLStore.h>
1214
#include <LibWeb/HTML/Origin.h>
@@ -89,4 +91,19 @@ ErrorOr<void> remove_entry_from_blob_url_store(StringView url)
8991
return {};
9092
}
9193

94+
// https://w3c.github.io/FileAPI/#lifeTime
95+
void run_unloading_cleanup_steps(JS::NonnullGCPtr<DOM::Document> document)
96+
{
97+
// 1. Let environment be the Document's relevant settings object.
98+
auto& environment = document->relevant_settings_object();
99+
100+
// 2. Let store be the user agent’s blob URL store;
101+
auto& store = FileAPI::blob_url_store();
102+
103+
// 3. Remove from store any entries for which the value's environment is equal to environment.
104+
store.remove_all_matching([&](auto&, auto& value) {
105+
return value.environment == &environment;
106+
});
107+
}
108+
92109
}

Userland/Libraries/LibWeb/FileAPI/BlobURLStore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ ErrorOr<String> generate_new_blob_url();
2828
ErrorOr<String> add_entry_to_blob_url_store(JS::NonnullGCPtr<Blob> object);
2929
ErrorOr<void> remove_entry_from_blob_url_store(StringView url);
3030

31+
void run_unloading_cleanup_steps(JS::NonnullGCPtr<DOM::Document>);
32+
3133
}

0 commit comments

Comments
 (0)