Skip to content

Truly independent web browser

LibWebView: Key stored cookies by their host-only flag

Shannon Booth28a7ece133da
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 2504fd5d5cc..9fd12288beb 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -3325,7 +3325,7 @@ ErrorOr<void> Application::set_cookie(DevTools::TabDescription const& descriptio
Optional<CookieStorageKey> old_key;
if (old_cookie.has_value())
- old_key = CookieStorageKey { old_cookie->name, old_cookie->domain, old_cookie->path, old_cookie->partition_key };
+ old_key = CookieStorageKey { old_cookie->name, old_cookie->domain, old_cookie->host_only, old_cookie->path, old_cookie->partition_key };
TRY(view->session().cookie_jar->set_cookie_from_devtools(*url, move(old_key), move(cookie)));
return {};
@@ -3338,7 +3338,7 @@ void Application::delete_cookies(DevTools::TabDescription const& description, Ve
return;
for (auto const& cookie : cookies)
- view->session().cookie_jar->delete_cookie({ cookie.name, cookie.domain, cookie.path, cookie.partition_key });
+ view->session().cookie_jar->delete_cookie({ cookie.name, cookie.domain, cookie.host_only, cookie.path, cookie.partition_key });
}
void Application::listen_for_host_cookie_changes(DevTools::TabDescription const& description, OnHostCookieChange on_host_cookie_change) const
diff --git a/Libraries/LibWebView/CookieJar.cpp b/Libraries/LibWebView/CookieJar.cpp
index 93ecc3f3903..6cf142879aa 100644
--- a/Libraries/LibWebView/CookieJar.cpp
+++ b/Libraries/LibWebView/CookieJar.cpp
@@ -29,10 +29,11 @@ static constexpr auto DATABASE_SYNCHRONIZATION_TIMER = AK::Duration::from_second
static constexpr u32 COOKIES_SCHEMA_BASELINE_VERSION = 1u;
static constexpr u32 COOKIES_SCHEMA_PARTITION_KEY_VERSION = 2u;
+static constexpr u32 COOKIES_SCHEMA_HOST_ONLY_KEY_VERSION = 3u;
static CookieStorageKey storage_key_for_cookie(HTTP::Cookie::Cookie const& cookie)
{
- return { cookie.name, cookie.domain, cookie.path, cookie.partition_key };
+ return { cookie.name, cookie.domain, cookie.host_only, cookie.path, cookie.partition_key };
}
// AD-HOC: The partition of the cookies that a context uses for url. See HTTP::Cookie::Cookie::partition_key.
@@ -108,6 +109,33 @@ ErrorOr<Database::MigrationOutcome> CookieJar::migrate_schema(Database::Database
ALTER TABLE PartitionedCookies RENAME TO Cookies;
)#"sv,
},
+ {
+ // A cookie set with a domain attribute equal to the host is distinct from a host-only cookie of that host,
+ // so the host-only-flag joins the primary key.
+ .version = COOKIES_SCHEMA_HOST_ONLY_KEY_VERSION,
+ .sql = R"#(
+ CREATE TABLE HostOnlyKeyedCookies (
+ name TEXT,
+ value TEXT,
+ same_site INTEGER CHECK (same_site >= 0 AND same_site <= 3),
+ creation_time INTEGER,
+ last_access_time INTEGER,
+ expiry_time INTEGER,
+ domain TEXT,
+ path TEXT,
+ secure BOOLEAN,
+ http_only BOOLEAN,
+ host_only BOOLEAN,
+ persistent BOOLEAN,
+ partition_key TEXT NOT NULL DEFAULT '',
+ PRIMARY KEY(name, domain, host_only, path, partition_key)
+ );
+ INSERT INTO HostOnlyKeyedCookies (name, value, same_site, creation_time, last_access_time, expiry_time, domain, path, secure, http_only, host_only, persistent, partition_key)
+ SELECT name, value, same_site, creation_time, last_access_time, expiry_time, domain, path, secure, http_only, host_only, persistent, partition_key FROM Cookies;
+ DROP TABLE Cookies;
+ ALTER TABLE HostOnlyKeyedCookies RENAME TO Cookies;
+ )#"sv,
+ },
});
return database.migrate("Cookies"sv, migrations, mode);
@@ -457,7 +485,7 @@ void CookieJar::set_cookie(URL::URL const& url, HTTP::Cookie::ParsedCookie const
// 23. If the cookie store contains a cookie with the same name, domain, host-only-flag, and path as the
// newly-created cookie:
- if (auto const& old_cookie = m_transient_storage.get_cookie(key); old_cookie.has_value() && old_cookie->host_only == cookie.host_only) {
+ if (auto const& old_cookie = m_transient_storage.get_cookie(key); old_cookie.has_value()) {
// 1. Let old-cookie be the existing cookie with the same name, domain, host-only-flag, and path as the
// newly-created cookie. (Notice that this algorithm maintains the invariant that there is at most one such
// cookie.)
@@ -487,7 +515,7 @@ void CookieJar::update_cookie(HTTP::Cookie::Cookie cookie)
// 23. If the cookie store contains a cookie with the same name, domain, host-only-flag, and path as the
// newly-created cookie:
- if (auto const& old_cookie = m_transient_storage.get_cookie(key); old_cookie.has_value() && old_cookie->host_only == cookie.host_only) {
+ if (auto const& old_cookie = m_transient_storage.get_cookie(key); old_cookie.has_value()) {
// 3. Update the creation-time of the newly-created cookie to match the creation-time of the old-cookie.
cookie.creation_time = old_cookie->creation_time;
diff --git a/Libraries/LibWebView/CookieJar.h b/Libraries/LibWebView/CookieJar.h
index 8441d7c8926..a988d0612bb 100644
--- a/Libraries/LibWebView/CookieJar.h
+++ b/Libraries/LibWebView/CookieJar.h
@@ -28,6 +28,7 @@ struct CookieStorageKey {
String name;
String domain;
+ bool host_only { false };
String path;
Utf16String partition_key;
};
@@ -145,6 +146,7 @@ struct AK::Traits<WebView::CookieStorageKey> : public AK::DefaultTraits<WebView:
unsigned hash = 0;
hash = pair_int_hash(hash, key.name.hash());
hash = pair_int_hash(hash, key.domain.hash());
+ hash = pair_int_hash(hash, key.host_only);
hash = pair_int_hash(hash, key.path.hash());
hash = pair_int_hash(hash, key.partition_key.hash());
return hash;
diff --git a/Tests/LibWebView/TestCookieJar.cpp b/Tests/LibWebView/TestCookieJar.cpp
index aeb84242046..b7250e205d0 100644
--- a/Tests/LibWebView/TestCookieJar.cpp
+++ b/Tests/LibWebView/TestCookieJar.cpp
@@ -5,6 +5,7 @@
*/
#include <AK/NonnullOwnPtr.h>
+#include <AK/QuickSort.h>
#include <LibDatabase/Database.h>
#include <LibHTTP/Cookie/ParsedCookie.h>
#include <LibTest/TestCase.h>
@@ -146,7 +147,7 @@ TEST_CASE(unversioned_cookie_table_is_stamped_and_preserved)
Optional<u32> version;
auto statement = TRY_OR_FAIL(database->prepare_statement("SELECT version FROM SchemaVersions WHERE store = 'Cookies';"sv));
database->execute_statement(statement, [&](auto statement_id) -> ErrorOr<void> { version = database->result_column<u32>(statement_id, 0); return {}; });
- EXPECT_EQ(version, Optional<u32> { 2u });
+ EXPECT_EQ(version, Optional<u32> { 3u });
auto jar = TRY_OR_FAIL(WebView::CookieJar::create(*database));
@@ -221,3 +222,48 @@ TEST_CASE(third_party_cookies_are_partitioned_by_top_level_site)
EXPECT(cookie.partition_key.is_empty());
}
}
+
+TEST_CASE(host_only_cookie_is_stored_apart_from_domain_cookie_for_same_host)
+{
+ auto database = TRY_OR_FAIL(Database::Database::create_memory_backed());
+ EXPECT_EQ(TRY_OR_FAIL(WebView::CookieJar::migrate_schema(*database)), Database::MigrationOutcome::Success);
+
+ auto url = parse_url("https://www.example.com/"sv);
+ auto set_cookie = [&](WebView::CookieJar& jar, StringView value, Optional<String> domain) {
+ HTTP::Cookie::ParsedCookie cookie {
+ .name = "a"_string,
+ .value = MUST(String::from_utf8(value)),
+ .expiry_time_from_expires_attribute = UnixDateTime::now() + AK::Duration::from_seconds(3600),
+ .domain = move(domain),
+ .path = "/"_string,
+ };
+ jar.set_cookie(url, cookie, HTTP::Cookie::Source::NonHttp, {});
+ };
+ auto values = [](WebView::CookieJar& jar) {
+ Vector<String> values;
+ for (auto const& cookie : jar.get_all_cookies())
+ values.append(cookie.value);
+ quick_sort(values);
+ return values;
+ };
+
+ {
+ auto jar = TRY_OR_FAIL(WebView::CookieJar::create(*database));
+ set_cookie(*jar, "domain"sv, "www.example.com"_string);
+ set_cookie(*jar, "host-only"sv, {});
+ EXPECT_EQ(values(*jar), (Vector { "domain"_string, "host-only"_string }));
+ }
+
+ auto jar = TRY_OR_FAIL(WebView::CookieJar::create(*database));
+ EXPECT_EQ(values(*jar), (Vector { "domain"_string, "host-only"_string }));
+
+ HTTP::Cookie::ParsedCookie expired_cookie {
+ .name = "a"_string,
+ .value = {},
+ .expiry_time_from_expires_attribute = UnixDateTime::earliest(),
+ .domain = "www.example.com"_string,
+ .path = "/"_string,
+ };
+ jar->set_cookie(url, expired_cookie, HTTP::Cookie::Source::NonHttp, {});
+ EXPECT_EQ(values(*jar), Vector { "host-only"_string });
+}