Truly independent web browser
LibWebView+LibDevTools: Key DevTools storage edits by the page's key
sideshowbarkerdf92566c9e69
diff --git a/Libraries/LibDevTools/StorageHelpers.cpp b/Libraries/LibDevTools/StorageHelpers.cppindex e3743ebcda7..3558179e493 100644--- a/Libraries/LibDevTools/StorageHelpers.cpp+++ b/Libraries/LibDevTools/StorageHelpers.cpp@@ -8,6 +8,7 @@#include <LibDevTools/StorageHelpers.h>#include <LibURL/Parser.h>#include <LibURL/URL.h>+#include <LibWebCommon/StorageAPI/StorageKey.h>namespace DevTools {@@ -53,6 +54,17 @@ Optional<String> storage_host_for_url(URL::URL const& url)return {};}+Optional<String> storage_key_for_host(String const& storage_host)+{+ auto url = URL::Parser::basic_parse(storage_host);+ if (!url.has_value())+ return {};+ auto key = Web::StorageAPI::obtain_a_storage_key(url->origin());+ if (!key.has_value())+ return {};+ return key->to_string();+}+Optional<String> storage_host_name(String const& storage_host){auto url = URL::Parser::basic_parse(storage_host);diff --git a/Libraries/LibDevTools/StorageHelpers.h b/Libraries/LibDevTools/StorageHelpers.hindex 98bf4967a3c..e11404d5bfe 100644--- a/Libraries/LibDevTools/StorageHelpers.h+++ b/Libraries/LibDevTools/StorageHelpers.h@@ -18,6 +18,10 @@ namespace DevTools {DEVTOOLS_API Optional<String> storage_host_for_url(String const&);DEVTOOLS_API Optional<String> storage_host_for_url(URL::URL const& url);DEVTOOLS_API Optional<String> storage_host_name(String const&);+// The storage key of the bottle a storage host's pages keep their storage in, or none when pages of that host have+// no storage (an "about:" or "javascript:" host, e.g.). A host is a URL as far as its origin goes, and the key is+// the origin's, so every "file:" host names the one "file:" bottle.+DEVTOOLS_API Optional<String> storage_key_for_host(String const&);enum class StorageFieldType : u8 {Immutable,diff --git a/Libraries/LibWeb/StorageAPI/StorageKey.cpp b/Libraries/LibWeb/StorageAPI/StorageKey.cppindex 967aa41987c..4a35eca04f3 100644--- a/Libraries/LibWeb/StorageAPI/StorageKey.cpp+++ b/Libraries/LibWeb/StorageAPI/StorageKey.cpp@@ -16,18 +16,8 @@ Optional<StorageKey> obtain_a_storage_key(HTML::Environment const& environment)// 1. Let key be the result of running obtain a storage key for non-storage purposes with environment.auto key = obtain_a_storage_key_for_non_storage_purposes(environment);- // AD-HOC: file:// URLs are opaque, but other browsers support storage on file:// URLs.- if (key.origin.is_opaque_file_origin())- key.origin = URL::Origin { "file"_string, String {}, {} };-- // 2. If key’s origin is an opaque origin, then return failure.- if (key.origin.is_opaque())- return {};-- // FIXME: 3. If the user has disabled storage, then return failure.-- // 4. Return key.- return key;+ // 2-4. The rest is the same for every origin; see the overload taking one.+ return obtain_a_storage_key(key.origin);}// https://storage.spec.whatwg.org/#obtain-a-storage-key-for-non-storage-purposesdiff --git a/Libraries/LibWebCommon/StorageAPI/StorageKey.cpp b/Libraries/LibWebCommon/StorageAPI/StorageKey.cppindex 93a7f7e3500..92cbcacd1ce 100644--- a/Libraries/LibWebCommon/StorageAPI/StorageKey.cpp+++ b/Libraries/LibWebCommon/StorageAPI/StorageKey.cpp@@ -16,6 +16,27 @@ StorageKey obtain_a_storage_key_for_non_storage_purposes(URL::Origin const& origreturn { origin };}+// https://storage.spec.whatwg.org/#obtain-a-storage-key+Optional<StorageKey> obtain_a_storage_key(URL::Origin const& origin)+{+ // 1. Let key be the result of running obtain a storage key for non-storage purposes with environment.+ // NB: The caller ran step 1; this is its key's origin.+ auto key = obtain_a_storage_key_for_non_storage_purposes(origin);++ // AD-HOC: "file://" URLs are opaque, but other browsers support storage on "file://" URLs.+ if (key.origin.is_opaque_file_origin())+ key.origin = URL::Origin { "file"_string, String {}, {} };++ // 2. If key's origin is an opaque origin, then return failure.+ if (key.origin.is_opaque())+ return {};++ // FIXME: 3. If the user has disabled storage, then return failure.++ // 4. Return key.+ return key;+}+}namespace IPC {diff --git a/Libraries/LibWebCommon/StorageAPI/StorageKey.h b/Libraries/LibWebCommon/StorageAPI/StorageKey.hindex 5c95dda00a5..631e91bcdea 100644--- a/Libraries/LibWebCommon/StorageAPI/StorageKey.h+++ b/Libraries/LibWebCommon/StorageAPI/StorageKey.h@@ -38,6 +38,10 @@ struct StorageKey {WEBCOMMON_API StorageKey obtain_a_storage_key_for_non_storage_purposes(URL::Origin const&);+// Steps 2 to 4 of "obtain a storage key", for a caller that has the environment's origin in hand. Every process+// that names a bottle goes through this, so they all name it the same way.+WEBCOMMON_API Optional<StorageKey> obtain_a_storage_key(URL::Origin const&);+}namespace AK {diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex bef5bb87249..938fda09fc4 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1613,16 +1613,11 @@ void Application::launch_spare_web_content_process()});}-// DevTools hears of every change to a jar, whoever made it, keyed on the host of the page that made it — or on the-// storage key, when the change came from the browser itself.-static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange const& change, Optional<String> const& url)+// DevTools hears of every change to a jar, whoever made it. Each tab whose page keeps its storage in the changed+// bottle hears of it under that tab's own storage host: the host DevTools shows for a page and the key the browser+// keeps the page's storage under are two namings of one origin, and the tab's is the one its storage panel knows.+static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange const& change){- Optional<String> host;- if (url.has_value())- host = DevTools::storage_host_for_url(*url);- if (!host.has_value())- host = change.storage_key;-DevTools::DevToolsDelegate::StorageChange::Type type;switch (change.type) {case StorageChange::Type::Set:@@ -1640,7 +1635,7 @@ static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange conVector<DevTools::DevToolsDelegate::StorageChange> devtools_changes;devtools_changes.append({.storage_endpoint = change.storage_endpoint,- .host = *host,+ .host = {},.type = type,.key = change.key.map([](auto const& key) { return key.to_utf8(); }),});@@ -1651,7 +1646,7 @@ static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange confor (auto const& item : jar.get_all_items(change.storage_endpoint, change.storage_key)) {devtools_changes.append({.storage_endpoint = change.storage_endpoint,- .host = *host,+ .host = {},.type = DevTools::DevToolsDelegate::StorageChange::Type::Added,.key = item.key.to_utf8(),});@@ -1660,9 +1655,14 @@ static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange conViewImplementation::for_each_view([&](ViewImplementation& view) {bool owns_jar = view.session().storage_jar.ptr() == &jar || (view.has_traversable() && &view.traversable().session_storage() == &jar);- if (owns_jar) {- for (auto const& devtools_change : devtools_changes)- view.notify_storage_changed(devtools_change);+ if (!owns_jar)+ return IterationDecision::Continue;+ auto host = DevTools::storage_host_for_url(view.url());+ if (!host.has_value() || DevTools::storage_key_for_host(*host) != change.storage_key)+ return IterationDecision::Continue;+ for (auto devtools_change : devtools_changes) {+ devtools_change.host = *host;+ view.notify_storage_changed(move(devtools_change));}return IterationDecision::Continue;});@@ -1671,10 +1671,10 @@ static void notify_devtools_of_storage_change(StorageJar& jar, StorageChange conErrorOr<void> Application::launch_services(){m_default_session = BrowsingSession::create(IsPrivate::No);- StorageAreaRegistry::the().on_change_for_devtools = [this](StorageJar& jar, WebView::StorageChange const& change, Optional<String> const& url) {+ StorageAreaRegistry::the().on_change_for_devtools = [this](StorageJar& jar, WebView::StorageChange const& change) {// Only an attached DevTools has anyone to tell, and the lookup below isn't free on a page's write path.if (m_devtools)- notify_devtools_of_storage_change(jar, change, url);+ notify_devtools_of_storage_change(jar, change);};m_settings_observer = make<ApplicationSettingsObserver>();@@ -3400,23 +3400,35 @@ static ErrorOr<StorageJar*> storage_jar_for_devtools(ViewImplementation& view, Wreturn view.session().storage_jar.ptr();}-ErrorOr<Optional<String>> Application::set_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key, String const& key, String const& value) const+// The bottle the pages of a DevTools storage host keep their storage in: the host names an origin, and the key is+// that origin's, the way every page names it.+static ErrorOr<String> storage_key_for_devtools_host(String const& host)+{+ auto storage_key = DevTools::storage_key_for_host(host);+ if (!storage_key.has_value())+ return Error::from_string_literal("Storage host is not available");+ return storage_key.release_value();+}++ErrorOr<Optional<String>> Application::set_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& host, String const& key, String const& value) const{auto view = ViewImplementation::find_view_by_id(description.id);if (!view.has_value())return Error::from_string_literal("Unable to locate tab");auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));+ auto storage_key = TRY(storage_key_for_devtools_host(host));return TRY(storage_set_result_to_error_or_old_value(storage_jar->set_item(storage_endpoint, storage_key, Utf16String::from_utf8(key), Utf16String::from_utf8(value))));}-ErrorOr<Optional<String>> Application::remove_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key, String const& key) const+ErrorOr<Optional<String>> Application::remove_storage_item(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& host, String const& key) const{auto view = ViewImplementation::find_view_by_id(description.id);if (!view.has_value())return Error::from_string_literal("Unable to locate tab");auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));+ auto storage_key = TRY(storage_key_for_devtools_host(host));auto key_utf16 = Utf16String::from_utf8(key);auto old_value = storage_jar->get_item(storage_endpoint, storage_key, key_utf16);if (!old_value.has_value())@@ -3426,13 +3438,14 @@ ErrorOr<Optional<String>> Application::remove_storage_item(DevTools::TabDescriptreturn old_value->to_utf8();}-ErrorOr<void> Application::clear_storage(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& storage_key) const+ErrorOr<void> Application::clear_storage(DevTools::TabDescription const& description, Web::StorageAPI::StorageEndpointType storage_endpoint, String const& host) const{auto view = ViewImplementation::find_view_by_id(description.id);if (!view.has_value())return Error::from_string_literal("Unable to locate tab");auto* storage_jar = TRY(storage_jar_for_devtools(*view, storage_endpoint));+ auto storage_key = TRY(storage_key_for_devtools_host(host));storage_jar->clear_storage_key(storage_endpoint, storage_key);return {};}diff --git a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cppindex 360afe3d5e7..2e30e014a01 100644--- a/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp+++ b/Libraries/LibWebView/CanonicalEnvironmentSettingsObject.cpp@@ -68,18 +68,8 @@ Optional<Web::StorageAPI::StorageKey> obtain_a_storage_key(CanonicalEnvironmentS// 1. Let key be the result of running obtain a storage key for non-storage purposes with environment.auto key = obtain_a_storage_key_for_non_storage_purposes(environment);- // AD-HOC: file:// URLs are opaque, but other browsers support storage on file:// URLs.- if (key.origin.is_opaque_file_origin())- key.origin = URL::Origin { "file"_string, String {}, {} };-- // 2. If key’s origin is an opaque origin, then return failure.- if (key.origin.is_opaque())- return {};-- // FIXME: 3. If the user has disabled storage, then return failure.-- // 4. Return key.- return key;+ // 2-4. The rest is the same for every origin; see the overload taking one.+ return Web::StorageAPI::obtain_a_storage_key(key.origin);}}diff --git a/Libraries/LibWebView/StorageAreaRegistry.cpp b/Libraries/LibWebView/StorageAreaRegistry.cppindex bb4e3b7de81..0cff5615b2e 100644--- a/Libraries/LibWebView/StorageAreaRegistry.cpp+++ b/Libraries/LibWebView/StorageAreaRegistry.cpp@@ -170,7 +170,7 @@ void StorageAreaRegistry::did_change(Badge<StorageJar>, StorageJar& jar, Storage}if (on_change_for_devtools)- on_change_for_devtools(jar, change, writer_url);+ on_change_for_devtools(jar, change);}}diff --git a/Libraries/LibWebView/StorageAreaRegistry.h b/Libraries/LibWebView/StorageAreaRegistry.hindex f5c0dcd84ae..adad0676f78 100644--- a/Libraries/LibWebView/StorageAreaRegistry.h+++ b/Libraries/LibWebView/StorageAreaRegistry.h@@ -72,8 +72,8 @@ public:void remove_item(StorageAreaListener&, Web::StorageAPI::StorageAreaId, Utf16String const& key, String const& url);void clear(StorageAreaListener&, Web::StorageAPI::StorageAreaId, String const& url);- // Every change to every jar, with the URL of the page that made it when a page did.- Function<void(StorageJar&, StorageChange const&, Optional<String> const& url)> on_change_for_devtools;+ // Every change to every jar, whoever made it.+ Function<void(StorageJar&, StorageChange const&)> on_change_for_devtools;void did_change(Badge<StorageJar>, StorageJar&, StorageChange const&);void remove_areas_of(Badge<StorageJar>, StorageJar&);diff --git a/Tests/LibDevTools/CMakeLists.txt b/Tests/LibDevTools/CMakeLists.txtindex 8350efe9315..2634e0afbd4 100644--- a/Tests/LibDevTools/CMakeLists.txt+++ b/Tests/LibDevTools/CMakeLists.txt@@ -1,5 +1,6 @@set(TEST_SOURCESTestDevToolsProtocol.cpp+ TestStorageHelpers.cpp)foreach(source IN LISTS TEST_SOURCES)diff --git a/Tests/LibDevTools/TestStorageHelpers.cpp b/Tests/LibDevTools/TestStorageHelpers.cppnew file mode 100644index 00000000000..a6be1b35223--- /dev/null+++ b/Tests/LibDevTools/TestStorageHelpers.cpp@@ -0,0 +1,36 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibDevTools/StorageHelpers.h>+#include <LibTest/TestCase.h>++// The host DevTools shows for a page and the storage key the browser keeps that page's storage under are two+// different namings of one origin; a storage edit made through DevTools has to land in the page's own bottle.+TEST_CASE(storage_key_for_host_names_the_bottle_of_an_http_host)+{+ EXPECT_EQ(DevTools::storage_key_for_host("http://example.com:8080"_string), Optional<String> { "http://example.com:8080"_string });+ EXPECT_EQ(DevTools::storage_key_for_host("https://example.com"_string), Optional<String> { "https://example.com"_string });++ // Round trip from a page URL: what the host becomes is what the key is made from.+ auto host = DevTools::storage_host_for_url("http://example.com:8080/some/page.html"_string);+ EXPECT(host.has_value());+ EXPECT_EQ(DevTools::storage_key_for_host(*host), Optional<String> { "http://example.com:8080"_string });+}++TEST_CASE(storage_key_for_host_maps_every_file_host_to_the_shared_file_bottle)+{+ auto host = DevTools::storage_host_for_url("file:///Users/someone/page.html"_string);+ EXPECT(host.has_value());+ EXPECT_EQ(DevTools::storage_key_for_host(*host), Optional<String> { "file://"_string });+ EXPECT_EQ(DevTools::storage_key_for_host("file:///tmp/other.html"_string), Optional<String> { "file://"_string });+}++TEST_CASE(storage_key_for_host_has_none_for_hosts_without_storage)+{+ EXPECT(!DevTools::storage_key_for_host("about:blank"_string).has_value());+ EXPECT(!DevTools::storage_key_for_host("javascript:void(0)"_string).has_value());+ EXPECT(!DevTools::storage_key_for_host("not a url"_string).has_value());+}diff --git a/Tests/LibWebCommon/CMakeLists.txt b/Tests/LibWebCommon/CMakeLists.txtindex 7657085cb0b..93c94794026 100644--- a/Tests/LibWebCommon/CMakeLists.txt+++ b/Tests/LibWebCommon/CMakeLists.txt@@ -6,6 +6,7 @@ set(TEST_SOURCESTestPermissionsPolicy.cppTestSiteCompatibility.cppTestStorageEndpoint.cpp+ TestStorageKey.cppTestStrings.cppTestWebDriverInputResponse.cpp)@@ -19,3 +20,4 @@ target_link_libraries(TestGamepadStateBuffer PRIVATE LibCore LibIPC)target_link_libraries(TestKeyEvent PRIVATE LibIPC)target_link_libraries(TestPermissionsPolicy PRIVATE LibHTTP LibIPC LibURL)target_link_libraries(TestStorageEndpoint PRIVATE LibIPC)+target_link_libraries(TestStorageKey PRIVATE LibURL)diff --git a/Tests/LibWebCommon/TestStorageKey.cpp b/Tests/LibWebCommon/TestStorageKey.cppnew file mode 100644index 00000000000..dbe40d7e9f7--- /dev/null+++ b/Tests/LibWebCommon/TestStorageKey.cpp@@ -0,0 +1,43 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>+#include <LibURL/Parser.h>+#include <LibWebCommon/StorageAPI/StorageKey.h>++static URL::Origin origin_of(StringView url)+{+ return URL::Parser::basic_parse(url)->origin();+}++TEST_CASE(a_tuple_origin_is_its_own_storage_key)+{+ auto key = Web::StorageAPI::obtain_a_storage_key(origin_of("http://example.com:8080/path"sv));+ EXPECT(key.has_value());+ EXPECT_EQ(key->to_string(), "http://example.com:8080"_string);++ key = Web::StorageAPI::obtain_a_storage_key(origin_of("https://example.com:443/"sv));+ EXPECT(key.has_value());+ EXPECT_EQ(key->to_string(), "https://example.com"_string);+}++// Every "file:" document shares one bottle, whatever its path: the opaque file origin maps to the "file:" origin.+TEST_CASE(a_file_origin_gets_the_shared_file_storage_key)+{+ auto key = Web::StorageAPI::obtain_a_storage_key(origin_of("file:///Users/someone/page.html"sv));+ EXPECT(key.has_value());+ EXPECT_EQ(key->to_string(), "file://"_string);++ auto other = Web::StorageAPI::obtain_a_storage_key(origin_of("file:///tmp/other.html"sv));+ EXPECT(other.has_value());+ EXPECT_EQ(*key, *other);+}++TEST_CASE(an_opaque_origin_has_no_storage_key)+{+ EXPECT(!Web::StorageAPI::obtain_a_storage_key(origin_of("data:text/html,hi"sv)).has_value());+ EXPECT(!Web::StorageAPI::obtain_a_storage_key(URL::Origin::create_opaque()).has_value());+}diff --git a/Tests/LibWebView/TestStorageAreaRegistry.cpp b/Tests/LibWebView/TestStorageAreaRegistry.cppindex 5de44c1c269..d21a584c0f9 100644--- a/Tests/LibWebView/TestStorageAreaRegistry.cpp+++ b/Tests/LibWebView/TestStorageAreaRegistry.cpp@@ -340,18 +340,14 @@ TEST_CASE(connect_marks_the_bottle_accessed)EXPECT_EQ(jar->get_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16), Optional<Utf16String> {});}-TEST_CASE(devtools_hears_every_change_with_the_writer_url)+TEST_CASE(devtools_hears_every_change_whoever_made_it){auto& registry = WebView::StorageAreaRegistry::the();auto jar = WebView::StorageJar::create();- struct Seen {- WebView::StorageChange::Type type;- Optional<String> url;- };- Vector<Seen> seen;- registry.on_change_for_devtools = [&](WebView::StorageJar& changed_jar, WebView::StorageChange const& change, Optional<String> const& url) {+ Vector<WebView::StorageChange::Type> seen;+ registry.on_change_for_devtools = [&](WebView::StorageJar& changed_jar, WebView::StorageChange const& change) {EXPECT_EQ(&changed_jar, jar.ptr());- seen.append({ change.type, url });+ seen.append(change.type);};FakeListener a;@@ -360,9 +356,7 @@ TEST_CASE(devtools_hears_every_change_with_the_writer_url)jar->remove_item(StorageEndpointType::LocalStorage, KEY, "k"_utf16);EXPECT_EQ(seen.size(), 2u);- EXPECT_EQ(seen[0].type, WebView::StorageChange::Type::Set);- EXPECT_EQ(seen[0].url, Optional<String> { URL });- EXPECT_EQ(seen[1].type, WebView::StorageChange::Type::Removed);- EXPECT_EQ(seen[1].url, Optional<String> {});+ EXPECT_EQ(seen[0], WebView::StorageChange::Type::Set);+ EXPECT_EQ(seen[1], WebView::StorageChange::Type::Removed);registry.on_change_for_devtools = nullptr;}