LibWeb: Permit storage keys to use non-opaque origins for file:// URLs

Features like localStorage, sessionStorage, and CacheStorage all work on
file:// URLs in other browsers. The spec is a bit uncertain as to when
such URLs should be treated as non-opaque, and leave it "as an exercise
to the reader".

Note that we perform this check in obtain-a-storage-key rather than in
the non-storage method, as the latter is also used for e.g. blob://
URL storage.
This commit is contained in:
Timothy Flynn
2026-04-01 16:55:14 -04:00
committed by Shannon Booth
parent ef4ff5d490
commit 523e32bbbb
Notes: github-actions[bot] 2026-04-02 08:22:19 +00:00
2 changed files with 13 additions and 25 deletions

View File

@@ -16,6 +16,10 @@ 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 keys origin is an opaque origin, then return failure.
if (key.origin.is_opaque())
return {};
@@ -36,11 +40,14 @@ StorageKey obtain_a_storage_key_for_non_storage_purposes(URL::Origin const& orig
StorageKey obtain_a_storage_key_for_non_storage_purposes(HTML::Environment const& environment)
{
// 1. Let origin be environments origin if environment is an environment settings object; otherwise environments creation URLs origin.
if (is<HTML::EnvironmentSettingsObject>(environment)) {
auto const& settings = static_cast<HTML::EnvironmentSettingsObject const&>(environment);
return { settings.origin() };
}
return { environment.creation_url.origin() };
auto origin = [&]() {
if (auto const* settings = as_if<HTML::EnvironmentSettingsObject>(environment))
return settings->origin();
return environment.creation_url.origin();
}();
// 2. Return a tuple consisting of origin.
return { move(origin) };
}
}