Skip to content

Commit cc7c86f

Browse files
trflynn89awesomekling
authored andcommitted
Browser: Remove expired cookies from the CookieJar
The spec doesn't have any exact steps here, it just notes: The user agent MUST evict all expired cookies from the cookie store if, at any time, an expired cookie exists in the cookie store. Here, we implement "at any time" as "when a cookie is retrieved or stored".
1 parent 329e625 commit cc7c86f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Userland/Applications/Browser/CookieJar.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ struct ParsedCookie {
4545
bool http_only_attribute_present { false };
4646
};
4747

48-
String CookieJar::get_cookie(const URL& url) const
48+
String CookieJar::get_cookie(const URL& url)
4949
{
50+
purge_expired_cookies();
51+
5052
auto domain = canonicalize_domain(url);
5153
if (!domain.has_value())
5254
return {};
@@ -76,6 +78,7 @@ void CookieJar::set_cookie(const URL& url, const String& cookie_string)
7678
return;
7779

7880
store_cookie(parsed_cookie.value(), url, move(domain.value()));
81+
purge_expired_cookies();
7982
}
8083

8184
void CookieJar::dump_cookies() const
@@ -549,4 +552,18 @@ void CookieJar::store_cookie(ParsedCookie& parsed_cookie, const URL& url, String
549552
m_cookies.set(key, move(cookie));
550553
}
551554

555+
void CookieJar::purge_expired_cookies()
556+
{
557+
time_t now = Core::DateTime::now().timestamp();
558+
Vector<CookieStorageKey> keys_to_evict;
559+
560+
for (const auto& cookie : m_cookies) {
561+
if (cookie.value.expiry_time.timestamp() < now)
562+
keys_to_evict.append(cookie.key);
563+
}
564+
565+
for (const auto& key : keys_to_evict)
566+
m_cookies.remove(key);
567+
}
568+
552569
}

Userland/Applications/Browser/CookieJar.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct CookieStorageKey {
6060

6161
class CookieJar {
6262
public:
63-
String get_cookie(const URL& url) const;
63+
String get_cookie(const URL& url);
6464
void set_cookie(const URL& url, const String& cookie);
6565
void dump_cookies() const;
6666

@@ -80,6 +80,7 @@ class CookieJar {
8080
static bool domain_matches(const String& string, const String& domain_string);
8181

8282
void store_cookie(ParsedCookie& parsed_cookie, const URL& url, String canonicalized_domain);
83+
void purge_expired_cookies();
8384

8485
HashMap<CookieStorageKey, Cookie> m_cookies;
8586
};

0 commit comments

Comments
 (0)