Skip to main content

net/
cookie_storage.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Implementation of cookie storage as specified in
6//! <http://tools.ietf.org/html/rfc6265>
7
8use std::cmp::Ordering;
9use std::collections::HashMap;
10use std::collections::hash_map::Entry;
11use std::net::IpAddr;
12use std::time::SystemTime;
13
14use cookie::Cookie;
15use hyper_serde::Serde;
16use itertools::Itertools;
17use log::{info, warn};
18use malloc_size_of_derive::MallocSizeOf;
19use net_traits::pub_domains::reg_suffix;
20use net_traits::{
21    CookieAsyncResponse, CookieChange, CookieChangeType, CookieData, CookieSource, SiteDescriptor,
22};
23use serde::{Deserialize, Serialize};
24use servo_base::generic_channel::GenericCallback;
25use servo_base::id::CookieStoreId;
26use servo_url::ServoUrl;
27
28use crate::cookie::ServoCookie;
29
30#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
31pub struct CookieStorage {
32    version: u32,
33    cookies_map: HashMap<String, Vec<ServoCookie>>,
34    max_per_host: usize,
35    #[serde(skip)]
36    #[ignore_malloc_size_of = "Channels"]
37    listeners: HashMap<CookieStoreId, (ServoUrl, GenericCallback<CookieAsyncResponse>)>,
38}
39
40struct ServoCookieChange {
41    cookie: ServoCookie,
42    type_: CookieChangeType,
43}
44
45#[derive(Debug)]
46pub enum RemoveCookieError {
47    Overlapping,
48    NonHTTP,
49}
50
51impl CookieStorage {
52    pub fn new(max_cookies: usize) -> CookieStorage {
53        CookieStorage {
54            version: 1,
55            cookies_map: HashMap::new(),
56            max_per_host: max_cookies,
57            listeners: HashMap::new(),
58        }
59    }
60
61    pub fn add_listener(
62        &mut self,
63        store_id: CookieStoreId,
64        url: ServoUrl,
65        callback: GenericCallback<CookieAsyncResponse>,
66    ) {
67        self.listeners.insert(store_id, (url, callback));
68    }
69
70    pub fn remove_listener(&mut self, store_id: CookieStoreId) {
71        self.listeners.remove(&store_id);
72    }
73
74    pub fn send_response(&self, store_id: CookieStoreId, data: CookieData) {
75        let Some((_, callback)) = self.listeners.get(&store_id) else {
76            warn!("Async cookie request made for store id that is non-existent {store_id:?}");
77            return;
78        };
79        if callback.send(CookieAsyncResponse { data }).is_err() {
80            warn!("Unable to send cookie response to script thread");
81        }
82    }
83
84    /// <https://cookiestore.spec.whatwg.org/#process-cookie-changes>
85    fn process_cookie_changes(&self, changes: &[ServoCookieChange]) {
86        // 1. For every Window window, run the following steps:
87        for (url, callback) in self.listeners.values() {
88            // 1.1. Let url be window’s relevant settings object’s creation URL.
89            // 1.2. Let changes be the observable changes for url.
90            let changes = observable_changes(url, changes);
91
92            // 1.3. If changes is empty, then continue.
93            if changes.is_empty() {
94                continue;
95            }
96
97            // 1.4. Queue a global task on the DOM manipulation task source given window to fire a
98            // change event named "change" with changes at window’s CookieStore.
99            //
100            // This is implemented in CookieListener::handle.
101            if callback
102                .send(CookieAsyncResponse {
103                    data: CookieData::Change(changes),
104                })
105                .is_err()
106            {
107                warn!("Unable to send cookie change to script thread");
108            }
109        }
110
111        // TODO: 2. For every service worker registration registration, fire a functional event
112        // named "cookiechange" for the changes matching its cookie change subscription list.
113    }
114
115    // http://tools.ietf.org/html/rfc6265#section-5.3
116    pub fn remove(
117        &mut self,
118        cookie: &ServoCookie,
119        url: &ServoUrl,
120        source: CookieSource,
121    ) -> Result<Option<ServoCookie>, RemoveCookieError> {
122        let domain = reg_host(cookie.cookie.domain().as_ref().unwrap_or(&""));
123        let cookies = self.cookies_map.entry(domain).or_default();
124
125        // https://www.ietf.org/id/draft-ietf-httpbis-cookie-alone-01.txt Step 2
126        if !cookie.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
127            let new_domain = cookie.cookie.domain().as_ref().unwrap().to_owned();
128            let new_path = cookie.cookie.path().as_ref().unwrap().to_owned();
129
130            let any_overlapping = cookies.iter().any(|c| {
131                let existing_domain = c.cookie.domain().as_ref().unwrap().to_owned();
132                let existing_path = c.cookie.path().as_ref().unwrap().to_owned();
133
134                c.cookie.name() == cookie.cookie.name() &&
135                    c.cookie.secure().unwrap_or(false) &&
136                    (ServoCookie::domain_match(new_domain, existing_domain) ||
137                        ServoCookie::domain_match(existing_domain, new_domain)) &&
138                    ServoCookie::path_match(new_path, existing_path)
139            });
140
141            if any_overlapping {
142                return Err(RemoveCookieError::Overlapping);
143            }
144        }
145
146        // Step 11.1
147        let position = cookies.iter().position(|c| {
148            c.cookie.domain() == cookie.cookie.domain() &&
149                c.cookie.path() == cookie.cookie.path() &&
150                c.cookie.name() == cookie.cookie.name()
151        });
152
153        if let Some(ind) = position {
154            // Step 11.4
155            let c = cookies.remove(ind);
156
157            // http://tools.ietf.org/html/rfc6265#section-5.3 step 11.2
158            if c.cookie.http_only().unwrap_or(false) && source == CookieSource::NonHTTP {
159                // Undo the removal.
160                cookies.push(c);
161                Err(RemoveCookieError::NonHTTP)
162            } else {
163                Ok(Some(c))
164            }
165        } else {
166            Ok(None)
167        }
168    }
169
170    pub fn delete_cookies_for_sites(&mut self, sites: &Vec<String>) {
171        // Note: We assume the number of sites is smaller than the number of
172        // entries in the cookies map. If this assumption stops holding in
173        // practice, this implementation can be revised to use `retain`
174        // together with a temporary `HashSet` of sites.
175        for site in sites {
176            // TODO: We currently mark cookies as expired instead of removing
177            // them immediately (same behavior as in the functions below).
178            // This is safe because higher-level cookie accessors always call
179            // `remove_expired_cookies_for_url` / `remove_all_expired_cookies`.
180            // Consider whether we should instead delete the entries directly.
181            if let Some(cookies) = self.cookies_map.get_mut(site) {
182                for cookie in cookies.iter_mut() {
183                    cookie.set_expiry_time_in_past();
184                }
185            }
186        }
187    }
188
189    pub fn clear_session_cookies(&mut self) {
190        self.cookies_map
191            .values_mut()
192            .flat_map(|cookies| cookies.iter_mut())
193            .filter(|cookie| !cookie.persistent)
194            .for_each(|cookie| cookie.set_expiry_time_in_past());
195    }
196
197    pub fn clear_storage(&mut self, url: Option<&ServoUrl>) {
198        if let Some(url) = url {
199            let domain = reg_host(url.host_str().unwrap_or(""));
200            if let Some(cookies) = self.cookies_map.get_mut(&domain) {
201                for cookie in cookies.iter_mut() {
202                    cookie.set_expiry_time_in_past();
203                }
204            }
205        } else {
206            self.cookies_map.clear();
207        }
208    }
209
210    pub fn delete_cookie_with_name(&mut self, url: &ServoUrl, name: String) {
211        let domain = reg_host(url.host_str().unwrap_or(""));
212        if let Some(cookies) = self.cookies_map.get_mut(&domain) {
213            for cookie in cookies.iter_mut() {
214                if cookie.cookie.name() == name {
215                    cookie.set_expiry_time_in_past();
216                }
217            }
218        }
219    }
220
221    // http://tools.ietf.org/html/rfc6265#section-5.3
222    pub fn push(&mut self, mut cookie: ServoCookie, url: &ServoUrl, source: CookieSource) {
223        // https://www.ietf.org/id/draft-ietf-httpbis-cookie-alone-01.txt Step 1
224        if cookie.cookie.secure().unwrap_or(false) && !url.is_secure_scheme() {
225            return;
226        }
227
228        let Ok(old_cookie) = self.remove(&cookie, url, source) else {
229            // This new cookie is not allowed to overwrite an existing one.
230            return;
231        };
232
233        // Step 11
234        if let Some(old_cookie) = &old_cookie {
235            // Step 11.3
236            cookie.creation_time = old_cookie.creation_time;
237        }
238
239        // An already-expired cookie is never stored. Setting one is how cookies are deleted, and the
240        // existing cookie (if any) has already been removed above.
241        if is_cookie_expired(&cookie) {
242            // https://cookiestore.spec.whatwg.org/#cookie-change
243            // > A newly-created cookie which is immediately evicted is considered deleted.
244            // NOTE: We do not process a change if the immediately evicted cookie does not overwrite
245            // an existing cookie, as that cookie would not be observable by the browser. This
246            // mirrors the behaviour of other browsers and WPT expectations.
247            if old_cookie.is_some() {
248                self.process_cookie_changes(&[ServoCookieChange {
249                    cookie,
250                    type_: CookieChangeType::Deleted,
251                }]);
252            }
253            return;
254        }
255
256        // Step 12
257        let mut changes = vec![];
258        let domain = reg_host(cookie.cookie.domain().as_ref().unwrap_or(&""));
259        let cookies = self.cookies_map.entry(domain).or_default();
260
261        if cookies.len() == self.max_per_host {
262            let old_len = cookies.len();
263            changes.extend(evict_expired_cookies(cookies));
264
265            // https://www.ietf.org/id/draft-ietf-httpbis-cookie-alone-01.txt
266            if cookies.len() == old_len {
267                let Some(evicted) =
268                    evict_one_cookie(cookie.cookie.secure().unwrap_or(false), cookies)
269                else {
270                    return;
271                };
272
273                // https://cookiestore.spec.whatwg.org/#cookie-change
274                // > A cookie which is otherwise evicted or removed is considered deleted
275                changes.push(ServoCookieChange {
276                    cookie: evicted,
277                    type_: CookieChangeType::Deleted,
278                });
279            }
280        }
281
282        // https://cookiestore.spec.whatwg.org/#cookie-change
283        // > A newly-created cookie which is not immediately evicted is considered changed.
284        // NOTE: We do not process a change if the new cookie is identical to the existing one, as
285        // that cookie would not be observable by the browser. This mirrors the behaviour of other
286        // browsers and WPT expectations.
287        let is_identical = old_cookie.is_some_and(|old_cookie| {
288            old_cookie.cookie == cookie.cookie &&
289                old_cookie.cookie.same_site() == cookie.cookie.same_site() &&
290                old_cookie.expiry_time == cookie.expiry_time
291        });
292        if !is_identical {
293            changes.push(ServoCookieChange {
294                cookie: cookie.clone(),
295                type_: CookieChangeType::Changed,
296            });
297        }
298
299        cookies.push(cookie);
300
301        // https://cookiestore.spec.whatwg.org/#ref-for-process-cookie-changes
302        // > When any of the following conditions occur for a cookie store, perform the steps to
303        // > process cookie changes.
304        // > - A newly-created cookie is inserted into the cookie store.
305        // > - A user agent evicts expired cookies from the cookie store.
306        // > - A user agent removes excess cookies from the cookie store.
307        self.process_cookie_changes(&changes);
308    }
309
310    pub fn cookie_comparator(a: &ServoCookie, b: &ServoCookie) -> Ordering {
311        let a_path_len = a.cookie.path().as_ref().map_or(0, |p| p.len());
312        let b_path_len = b.cookie.path().as_ref().map_or(0, |p| p.len());
313        match a_path_len.cmp(&b_path_len) {
314            Ordering::Equal => a.creation_time.cmp(&b.creation_time),
315            // Ensure that longer paths are sorted earlier than shorter paths
316            Ordering::Greater => Ordering::Less,
317            Ordering::Less => Ordering::Greater,
318        }
319    }
320
321    pub fn remove_expired_cookies_for_url(&mut self, url: &ServoUrl) {
322        let domain = reg_host(url.host_str().unwrap_or(""));
323        if let Entry::Occupied(mut entry) = self.cookies_map.entry(domain) {
324            let cookies = entry.get_mut();
325            let expired = evict_expired_cookies(cookies);
326            if cookies.is_empty() {
327                entry.remove_entry();
328            }
329
330            // https://cookiestore.spec.whatwg.org/#ref-for-process-cookie-changes
331            // > When any of the following conditions occur for a cookie store, perform the steps to
332            // > process cookie changes.
333            // > - A user agent evicts expired cookies from the cookie store.
334            self.process_cookie_changes(&expired);
335        }
336    }
337
338    pub fn remove_all_expired_cookies(&mut self) {
339        let expired: Vec<_> = self
340            .cookies_map
341            .values_mut()
342            .flat_map(evict_expired_cookies)
343            .collect();
344        self.cookies_map.retain(|_, cookies| !cookies.is_empty());
345
346        // https://cookiestore.spec.whatwg.org/#ref-for-process-cookie-changes
347        // > When any of the following conditions occur for a cookie store, perform the steps to
348        // > process cookie changes.
349        // > - A user agent evicts expired cookies from the cookie store.
350        self.process_cookie_changes(&expired);
351    }
352
353    // http://tools.ietf.org/html/rfc6265#section-5.4
354    pub fn cookies_for_url(&mut self, url: &ServoUrl, source: CookieSource) -> Option<String> {
355        // Let cookie-list be the set of cookies from the cookie store
356        let cookie_list = self.cookies_data_for_url(url, source);
357
358        let reducer = |acc: String, cookie: Cookie<'static>| -> String {
359            // Serialize the cookie-list into a cookie-string by processing each cookie in the cookie-list in order:
360            // If the cookies' name is not empty, output the cookie's name followed by the %x3D ("=") character.
361            // If the cookies' value is not empty, output the cookie's value.
362            // If there is an unprocessed cookie in the cookie-list, output the characters %x3B and %x20 ("; ").
363            // Security: the above steps allow for "nameless" cookies which have proved to be a security footgun
364            // especially with the new cookie name prefix proposals
365            (match acc.len() {
366                0 => acc,
367                _ => acc + "; ",
368            }) + cookie.name() +
369                "=" +
370                cookie.value()
371        };
372
373        // Serialize the cookie-list into a cookie-string by processing each cookie in the cookie-list in order
374        let result = cookie_list.fold(String::new(), reducer);
375
376        info!(" === COOKIES SENT: {}", result);
377        match result.len() {
378            0 => None,
379            _ => Some(result),
380        }
381    }
382
383    /// <https://cookiestore.spec.whatwg.org/#query-cookies>
384    pub fn query_cookies(&mut self, url: &ServoUrl, name: Option<String>) -> Vec<Cookie<'static>> {
385        // 1. Retrieve cookie-list given request-uri and "non-HTTP" source
386        let cookie_list = self.cookies_data_for_url(url, CookieSource::NonHTTP);
387
388        // 3. For each cookie in cookie-list, run these steps:
389        // 3.2. If name is given, then run these steps:
390        if let Some(name) = name {
391            // Let cookieName be the result of running UTF-8 decode without BOM on cookie’s name.
392            // If cookieName does not equal name, then continue.
393            cookie_list.filter(|cookie| cookie.name() == name).collect()
394        } else {
395            cookie_list.collect()
396        }
397
398        // Note: we do not convert the list into CookieListItem's here, we do that in script to not not have to define
399        // the binding types in net.
400
401        // Return list
402    }
403
404    pub fn cookies_data_for_url<'a>(
405        &'a mut self,
406        url: &'a ServoUrl,
407        source: CookieSource,
408    ) -> impl Iterator<Item = cookie::Cookie<'static>> + 'a {
409        let domain = reg_host(url.host_str().unwrap_or(""));
410        let cookies = self.cookies_map.entry(domain).or_default();
411
412        cookies
413            .iter_mut()
414            .filter(move |c| c.appropriate_for_url(url, source))
415            .sorted_by(|a: &&mut ServoCookie, b: &&mut ServoCookie| {
416                // The user agent SHOULD sort the cookie-list
417                CookieStorage::cookie_comparator(a, b)
418            })
419            .map(|c| {
420                // Update the last-access-time of each cookie in the cookie-list to the current date and time
421                c.touch();
422                c.cookie.clone()
423            })
424    }
425
426    pub fn cookie_site_descriptors(&self) -> Vec<SiteDescriptor> {
427        self.cookies_map
428            .keys()
429            .cloned()
430            .map(SiteDescriptor::new)
431            .collect()
432    }
433}
434
435/// <https://cookiestore.spec.whatwg.org/#observable-changes>
436fn observable_changes(url: &ServoUrl, changes: &[ServoCookieChange]) -> Vec<CookieChange> {
437    // > The observable changes for url are the set of cookie changes to cookies in a cookie store
438    // > which meet the requirements in step 1 of Cookies § Retrieval Algorithm’s steps to compute
439    // > the "cookie-string from a given cookie store" with url as request-uri, for a "non-HTTP" API.
440    changes
441        .iter()
442        .filter(|change| {
443            change
444                .cookie
445                .appropriate_for_url(url, CookieSource::NonHTTP)
446        })
447        .map(|change| CookieChange {
448            cookie: Serde(change.cookie.cookie.clone()),
449            type_: change.type_.clone(),
450        })
451        .collect()
452}
453
454fn reg_host(url: &str) -> String {
455    let host_for_ip_parse = url
456        .strip_prefix('[')
457        .and_then(|url| url.strip_suffix(']'))
458        .unwrap_or(url);
459    if let Ok(address) = host_for_ip_parse.parse::<IpAddr>() {
460        debug_assert!(address.to_string() == address.to_string().to_lowercase());
461        return address.to_string();
462    }
463
464    reg_suffix(url).to_lowercase()
465}
466
467fn is_cookie_expired(cookie: &ServoCookie) -> bool {
468    matches!(cookie.expiry_time, Some(date_time) if date_time <= SystemTime::now())
469}
470
471fn evict_one_cookie(is_secure_cookie: bool, cookies: &mut Vec<ServoCookie>) -> Option<ServoCookie> {
472    // Remove non-secure cookie with oldest access time
473    if let Some((index, _)) = get_oldest_accessed(false, cookies) {
474        return Some(cookies.remove(index));
475    }
476
477    // All secure cookies were found
478    if !is_secure_cookie {
479        return None;
480    }
481    let (index, _) = get_oldest_accessed(true, cookies)?;
482    Some(cookies.remove(index))
483}
484
485fn evict_expired_cookies(cookies: &mut Vec<ServoCookie>) -> Vec<ServoCookieChange> {
486    cookies
487        .extract_if(.., |c| is_cookie_expired(c))
488        // https://cookiestore.spec.whatwg.org/#cookie-change
489        // > A cookie which is otherwise evicted or removed is considered deleted.
490        .map(|cookie| ServoCookieChange {
491            cookie,
492            type_: CookieChangeType::Deleted,
493        })
494        .collect()
495}
496
497fn get_oldest_accessed(
498    is_secure_cookie: bool,
499    cookies: &mut [ServoCookie],
500) -> Option<(usize, SystemTime)> {
501    let mut oldest_accessed = None;
502    for (i, c) in cookies.iter().enumerate() {
503        if (c.cookie.secure().unwrap_or(false) == is_secure_cookie) &&
504            oldest_accessed
505                .as_ref()
506                .is_none_or(|(_, current_oldest_time)| c.last_access < *current_oldest_time)
507        {
508            oldest_accessed = Some((i, c.last_access));
509        }
510    }
511    oldest_accessed
512}