1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:47:35 +00:00

Browser+WebContent+WebDriver: Move Get All Cookies to WebContent

There are a couple changes here from the existing Get All Cookies
implementation.

1. Previously, WebDriver actually returned *all* cookies in the cookie
   jar. The spec dictates that we only return cookies that match the
   document's URL. Specifically, it calls out that we must run just the
   first step of RFC 6265 section 5.4 to perform domain matching.

   This change adds a special mode to our implementation of that section
   to skip the remaining steps.

2. We now fill in the SameSite cookie attribute when serializing the
   cookie to JSON (this was a trival FIXME that didn't get picked up
   when SameSite was implemented).
This commit is contained in:
Timothy Flynn 2022-11-11 09:24:07 -05:00 committed by Linus Groh
parent d2c1957d8f
commit c77260c480
11 changed files with 89 additions and 29 deletions

View file

@ -116,6 +116,24 @@ Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies() const
return cookies;
}
// https://w3c.github.io/webdriver/#dfn-associated-cookies
Vector<Web::Cookie::Cookie> CookieJar::get_all_cookies(URL const& url)
{
auto domain = canonicalize_domain(url);
if (!domain.has_value())
return {};
auto cookie_list = get_matching_cookies(url, domain.value(), Web::Cookie::Source::Http, MatchingCookiesSpecMode::WebDriver);
Vector<Web::Cookie::Cookie> cookies;
cookies.ensure_capacity(cookie_list.size());
for (auto const& cookie : cookie_list)
cookies.unchecked_append(cookie);
return cookies;
}
Optional<String> CookieJar::canonicalize_domain(const URL& url)
{
// https://tools.ietf.org/html/rfc6265#section-5.1.2
@ -281,7 +299,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
m_cookies.set(key, move(cookie));
}
Vector<Web::Cookie::Cookie&> CookieJar::get_matching_cookies(const URL& url, String const& canonicalized_domain, Web::Cookie::Source source)
Vector<Web::Cookie::Cookie&> CookieJar::get_matching_cookies(const URL& url, String const& canonicalized_domain, Web::Cookie::Source source, MatchingCookiesSpecMode mode)
{
// https://tools.ietf.org/html/rfc6265#section-5.4
@ -310,6 +328,12 @@ Vector<Web::Cookie::Cookie&> CookieJar::get_matching_cookies(const URL& url, Str
if (cookie.value.http_only && (source != Web::Cookie::Source::Http))
continue;
// NOTE: The WebDriver spec expects only step 1 above to be executed to match cookies.
if (mode == MatchingCookiesSpecMode::WebDriver) {
cookie_list.append(cookie.value);
continue;
}
// 2. The user agent SHOULD sort the cookie-list in the following order:
// - Cookies with longer paths are listed before cookies with shorter paths.
// - Among cookies that have equal-length path fields, cookies with earlier creation-times are listed before cookies with later creation-times.