mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-06 01:26:22 +00:00
LibWeb+LibWebView: Move the cookie domain matching algorithm to LibWeb
This will be needed outside of LibWebView.
This commit is contained in:
parent
527218da19
commit
8988e7ef8d
Notes:
github-actions[bot]
2024-10-23 07:06:28 +00:00
Author: https://github.com/trflynn89
Commit: 8988e7ef8d
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1926
4 changed files with 28 additions and 28 deletions
|
@ -468,6 +468,30 @@ Optional<UnixDateTime> parse_date_time(StringView date_string)
|
||||||
return parsed_cookie_date;
|
return parsed_cookie_date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.3
|
||||||
|
bool domain_matches(StringView string, StringView domain_string)
|
||||||
|
{
|
||||||
|
// A string domain-matches a given domain string if at least one of the following conditions hold:
|
||||||
|
|
||||||
|
// * The domain string and the string are identical. (Note that both the domain string and the string will have been
|
||||||
|
// canonicalized to lower case at this point.)
|
||||||
|
if (string == domain_string)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// * All of the following conditions hold:
|
||||||
|
// - The domain string is a suffix of the string.
|
||||||
|
if (!string.ends_with(domain_string))
|
||||||
|
return false;
|
||||||
|
// - The last character of the string that is not included in the domain string is a %x2E (".") character.
|
||||||
|
if (string[string.length() - domain_string.length() - 1] != '.')
|
||||||
|
return false;
|
||||||
|
// - The string is a host name (i.e., not an IP address).
|
||||||
|
if (AK::IPv4Address::from_string(string).has_value())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
|
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
|
||||||
String default_path(URL::URL const& url)
|
String default_path(URL::URL const& url)
|
||||||
{
|
{
|
||||||
|
|
|
@ -29,6 +29,7 @@ struct ParsedCookie {
|
||||||
|
|
||||||
Optional<ParsedCookie> parse_cookie(URL::URL const&, StringView cookie_string);
|
Optional<ParsedCookie> parse_cookie(URL::URL const&, StringView cookie_string);
|
||||||
bool cookie_contains_invalid_control_character(StringView);
|
bool cookie_contains_invalid_control_character(StringView);
|
||||||
|
bool domain_matches(StringView string, StringView domain_string);
|
||||||
String default_path(URL::URL const&);
|
String default_path(URL::URL const&);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -234,30 +234,6 @@ Optional<String> CookieJar::canonicalize_domain(const URL::URL& url)
|
||||||
return MUST(MUST(url.serialized_host()).to_lowercase());
|
return MUST(MUST(url.serialized_host()).to_lowercase());
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.3
|
|
||||||
bool CookieJar::domain_matches(StringView string, StringView domain_string)
|
|
||||||
{
|
|
||||||
// A string domain-matches a given domain string if at least one of the following conditions hold:
|
|
||||||
|
|
||||||
// * The domain string and the string are identical. (Note that both the domain string and the string will have been
|
|
||||||
// canonicalized to lower case at this point.)
|
|
||||||
if (string == domain_string)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// * All of the following conditions hold:
|
|
||||||
// - The domain string is a suffix of the string.
|
|
||||||
if (!string.ends_with(domain_string))
|
|
||||||
return false;
|
|
||||||
// - The last character of the string that is not included in the domain string is a %x2E (".") character.
|
|
||||||
if (string[string.length() - domain_string.length() - 1] != '.')
|
|
||||||
return false;
|
|
||||||
// - The string is a host name (i.e., not an IP address).
|
|
||||||
if (AK::IPv4Address::from_string(string).has_value())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
|
// https://www.ietf.org/archive/id/draft-ietf-httpbis-rfc6265bis-15.html#section-5.1.4
|
||||||
bool CookieJar::path_matches(StringView request_path, StringView cookie_path)
|
bool CookieJar::path_matches(StringView request_path, StringView cookie_path)
|
||||||
{
|
{
|
||||||
|
@ -375,7 +351,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
|
||||||
// 10. If the domain-attribute is non-empty:
|
// 10. If the domain-attribute is non-empty:
|
||||||
if (!domain_attribute.is_empty()) {
|
if (!domain_attribute.is_empty()) {
|
||||||
// 1. If the canonicalized request-host does not domain-match the domain-attribute:
|
// 1. If the canonicalized request-host does not domain-match the domain-attribute:
|
||||||
if (!domain_matches(canonicalized_domain, domain_attribute)) {
|
if (!Web::Cookie::domain_matches(canonicalized_domain, domain_attribute)) {
|
||||||
// 1. Abort these steps and ignore the cookie entirely.
|
// 1. Abort these steps and ignore the cookie entirely.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -442,7 +418,7 @@ void CookieJar::store_cookie(Web::Cookie::ParsedCookie const& parsed_cookie, con
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
|
|
||||||
// 3. Their domain domain-matches the domain of the newly-created cookie, or vice-versa.
|
// 3. Their domain domain-matches the domain of the newly-created cookie, or vice-versa.
|
||||||
if (!domain_matches(old_cookie.domain, cookie.domain) && !domain_matches(cookie.domain, old_cookie.domain))
|
if (!Web::Cookie::domain_matches(old_cookie.domain, cookie.domain) && !Web::Cookie::domain_matches(cookie.domain, old_cookie.domain))
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
|
|
||||||
// 4. The path of the newly-created cookie path-matches the path of the existing cookie.
|
// 4. The path of the newly-created cookie path-matches the path of the existing cookie.
|
||||||
|
@ -568,7 +544,7 @@ Vector<Web::Cookie::Cookie> CookieJar::get_matching_cookies(const URL::URL& url,
|
||||||
// Or:
|
// Or:
|
||||||
// The cookie's host-only-flag is false and the canonicalized host of the retrieval's URI domain-matches
|
// The cookie's host-only-flag is false and the canonicalized host of the retrieval's URI domain-matches
|
||||||
// the cookie's domain.
|
// the cookie's domain.
|
||||||
bool is_not_host_only_and_domain_matches = !cookie.host_only && domain_matches(canonicalized_domain, cookie.domain);
|
bool is_not_host_only_and_domain_matches = !cookie.host_only && Web::Cookie::domain_matches(canonicalized_domain, cookie.domain);
|
||||||
|
|
||||||
if (!is_host_only_and_has_identical_domain && !is_not_host_only_and_domain_matches)
|
if (!is_host_only_and_has_identical_domain && !is_not_host_only_and_domain_matches)
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -103,7 +103,6 @@ private:
|
||||||
AK_MAKE_NONMOVABLE(CookieJar);
|
AK_MAKE_NONMOVABLE(CookieJar);
|
||||||
|
|
||||||
static Optional<String> canonicalize_domain(const URL::URL& url);
|
static Optional<String> canonicalize_domain(const URL::URL& url);
|
||||||
static bool domain_matches(StringView string, StringView domain_string);
|
|
||||||
static bool path_matches(StringView request_path, StringView cookie_path);
|
static bool path_matches(StringView request_path, StringView cookie_path);
|
||||||
|
|
||||||
enum class MatchingCookiesSpecMode {
|
enum class MatchingCookiesSpecMode {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue