Skip to main content

net_traits/
pub_domains.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 public domain matching.
6//!
7//! The list is a file located on the `resources` folder and loaded once on first need.
8//!
9//! The list can be updated with `./mach update-pub-domains` from this source:
10//! <https://publicsuffix.org/list/>
11//!
12//! This implementation is not strictly following the specification of the list. Wildcards are not
13//! restricted to appear only in the leftmost position, but the current list has no such cases so
14//! we don't need to make the code more complex for it. The `mach` update command makes sure that
15//! those cases are not present.
16
17use std::iter::FromIterator;
18use std::sync::LazyLock;
19
20use embedder_traits::resources::{self, Resource};
21use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
22use malloc_size_of_derive::MallocSizeOf;
23use rustc_hash::FxHashSet;
24use serde::{Deserialize, Serialize};
25use servo_url::{Host, ImmutableOrigin, ServoUrl};
26
27// We can use FxHash here.
28// The list is given by publicsuffix.org so an attack is highly unlikely
29#[derive(Clone, Debug, Default, MallocSizeOf)]
30pub struct PubDomainRules {
31    rules: FxHashSet<String>,
32    wildcards: FxHashSet<String>,
33    exceptions: FxHashSet<String>,
34}
35
36static PUB_DOMAINS: LazyLock<PubDomainRules> = LazyLock::new(load_pub_domains);
37
38pub fn public_suffix_list_size_of(ops: &mut MallocSizeOfOps) -> usize {
39    PUB_DOMAINS.size_of(ops)
40}
41
42impl<'a> FromIterator<&'a str> for PubDomainRules {
43    fn from_iter<T>(iter: T) -> Self
44    where
45        T: IntoIterator<Item = &'a str>,
46    {
47        let mut result = PubDomainRules::default();
48        for item in iter {
49            if let Some(stripped) = item.strip_prefix('!') {
50                result.exceptions.insert(String::from(stripped));
51            } else if let Some(stripped) = item.strip_prefix("*.") {
52                result.wildcards.insert(String::from(stripped));
53            } else {
54                result.rules.insert(String::from(item));
55            }
56        }
57        result
58    }
59}
60
61impl PubDomainRules {
62    pub fn parse(content: &str) -> PubDomainRules {
63        content
64            .lines()
65            .map(str::trim)
66            .filter(|s| !s.is_empty())
67            .filter(|s| !s.starts_with("//"))
68            .collect()
69    }
70    fn suffix_pair<'a>(&self, domain: &'a str) -> (&'a str, &'a str) {
71        let domain = domain.trim_start_matches('.');
72        // <https://url.spec.whatwg.org/#host-public-suffix>
73        // A trailing dot does not take part in the public suffix list lookup, but it is
74        // kept in the result, so that "example.com." and "example.com" stay distinct.
75        let trailing_dot = usize::from(domain.ends_with('.'));
76        let (public, registrable) =
77            self.suffix_pair_without_trailing_dot(&domain[..domain.len() - trailing_dot]);
78        (
79            &domain[domain.len() - trailing_dot - public.len()..],
80            &domain[domain.len() - trailing_dot - registrable.len()..],
81        )
82    }
83    fn suffix_pair_without_trailing_dot<'a>(&self, domain: &'a str) -> (&'a str, &'a str) {
84        let mut suffix = domain;
85        let mut prev_suffix = domain;
86        for (index, _) in domain.match_indices('.') {
87            let next_suffix = &domain[index + 1..];
88            if self.exceptions.contains(suffix) {
89                return (next_suffix, suffix);
90            }
91            if self.wildcards.contains(next_suffix) || self.rules.contains(suffix) {
92                return (suffix, prev_suffix);
93            }
94            prev_suffix = suffix;
95            suffix = next_suffix;
96        }
97        (suffix, prev_suffix)
98    }
99    pub fn public_suffix<'a>(&self, domain: &'a str) -> &'a str {
100        let (public, _) = self.suffix_pair(domain);
101        public
102    }
103    pub fn registrable_suffix<'a>(&self, domain: &'a str) -> &'a str {
104        let (_, registrable) = self.suffix_pair(domain);
105        registrable
106    }
107    pub fn is_public_suffix(&self, domain: &str) -> bool {
108        // Speeded-up version of
109        // domain != "" &&
110        // self.public_suffix(domain) == domain.
111        let domain = domain.trim_start_matches('.');
112        let domain = domain.strip_suffix('.').unwrap_or(domain);
113        match domain.find('.') {
114            None => !domain.is_empty(),
115            Some(index) => {
116                !self.exceptions.contains(domain) && self.wildcards.contains(&domain[index + 1..]) ||
117                    self.rules.contains(domain)
118            },
119        }
120    }
121    pub fn is_registrable_suffix(&self, domain: &str) -> bool {
122        // Speeded-up version of
123        // self.public_suffix(domain) != domain &&
124        // self.registrable_suffix(domain) == domain.
125        let domain = domain.trim_start_matches('.');
126        let domain = domain.strip_suffix('.').unwrap_or(domain);
127        match domain.find('.') {
128            None => false,
129            Some(index) => {
130                self.exceptions.contains(domain) ||
131                    !self.wildcards.contains(&domain[index + 1..]) &&
132                        !self.rules.contains(domain) &&
133                        self.is_public_suffix(&domain[index + 1..])
134            },
135        }
136    }
137}
138
139fn load_pub_domains() -> PubDomainRules {
140    let bytes = resources::read_bytes(Resource::DomainList);
141    PubDomainRules::parse(str::from_utf8(bytes.as_ref()).expect("Could not read pub domain rules"))
142}
143
144pub fn pub_suffix(domain: &str) -> &str {
145    PUB_DOMAINS.public_suffix(domain)
146}
147
148pub fn reg_suffix(domain: &str) -> &str {
149    PUB_DOMAINS.registrable_suffix(domain)
150}
151
152pub fn is_pub_domain(domain: &str) -> bool {
153    PUB_DOMAINS.is_public_suffix(domain)
154}
155
156pub fn is_reg_domain(domain: &str) -> bool {
157    PUB_DOMAINS.is_registrable_suffix(domain)
158}
159
160/// <https://html.spec.whatwg.org/multipage/#same-site>
161pub fn is_same_site(site_a: &ImmutableOrigin, site_b: &ImmutableOrigin) -> bool {
162    // First steps are for
163    // https://html.spec.whatwg.org/multipage/#concept-site-same-site
164    //
165    // Step 1. If A and B are the same opaque origin, then return true.
166    if !site_a.is_tuple() && !site_b.is_tuple() && site_a == site_b {
167        return true;
168    }
169
170    // Step 2. If A or B is an opaque origin, then return false.
171    let ImmutableOrigin::Tuple(scheme_a, host_a, _) = site_a else {
172        return false;
173    };
174    let ImmutableOrigin::Tuple(scheme_b, host_b, _) = site_b else {
175        return false;
176    };
177
178    // Step 3. If A's and B's scheme values are different, then return false.
179    if scheme_a != scheme_b {
180        return false;
181    }
182
183    // Step 4. If A's and B's host values are not equal, then return false.
184    // Includes the steps of https://html.spec.whatwg.org/multipage/#obtain-a-site
185    if let (Host::Domain(domain_a), Host::Domain(domain_b)) = (host_a, host_b) {
186        if reg_suffix(domain_a) != reg_suffix(domain_b) {
187            return false;
188        }
189    } else if host_a != host_b {
190        return false;
191    }
192
193    // Step 5. Return true.
194    true
195}
196
197/// The registered domain name (aka eTLD+1) for a URL.
198/// Returns None if the URL has no host name.
199/// Returns the registered suffix for the host name if it is a domain.
200/// Leaves the host name alone if it is an IP address.
201pub fn registered_domain_name(url: &ServoUrl) -> Option<Host> {
202    match url.origin() {
203        ImmutableOrigin::Tuple(_, Host::Domain(domain), _) => {
204            Some(Host::Domain(String::from(reg_suffix(&domain))))
205        },
206        ImmutableOrigin::Tuple(_, ip, _) => Some(ip),
207        ImmutableOrigin::Opaque(_) => None,
208    }
209}
210
211/// A [site](https://html.spec.whatwg.org/multipage/#site).
212#[derive(Clone, Debug, Deserialize, Eq, Hash, MallocSizeOf, PartialEq, Serialize)]
213pub enum Site {
214    Opaque(ImmutableOrigin),
215    Tuple { scheme: String, host: Host },
216}
217
218impl Site {
219    /// <https://html.spec.whatwg.org/multipage/#obtain-a-site>
220    pub fn from_origin(origin: &ImmutableOrigin) -> Self {
221        match origin {
222            // Step 1. If origin is an opaque origin, then return origin.
223            ImmutableOrigin::Opaque(_) => Site::Opaque(origin.clone()),
224            // Step 3. Return (origin's scheme, origin's host's registrable domain).
225            ImmutableOrigin::Tuple(scheme, Host::Domain(domain), _) => Site::Tuple {
226                scheme: scheme.clone(),
227                host: Host::Domain(reg_suffix(domain).to_owned()),
228            },
229            // Step 2. If origin's host's registrable domain is null, then return (origin's scheme, origin's host).
230            ImmutableOrigin::Tuple(scheme, host, _) => Site::Tuple {
231                scheme: scheme.clone(),
232                host: host.clone(),
233            },
234        }
235    }
236}