1use std::collections::HashMap;
6use std::net::{Ipv4Addr, Ipv6Addr};
7use std::num::NonZeroU64;
8use std::sync::LazyLock;
9use std::time::Duration;
10
11use embedder_traits::resources::{self, Resource, ResourceBytes};
12use fst::{Map, MapBuilder};
13use headers::{HeaderMapExt, StrictTransportSecurity};
14use http::HeaderMap;
15use log::{debug, error, info};
16use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
17use malloc_size_of_derive::MallocSizeOf;
18use net_traits::IncludeSubdomains;
19use net_traits::pub_domains::reg_suffix;
20use serde::{Deserialize, Serialize};
21use servo_config::pref;
22use servo_url::{Host, ServoUrl};
23use time::UtcDateTime;
24
25#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
26pub struct HstsEntry {
27 pub host: String,
28 pub include_subdomains: bool,
29 pub expires_at: Option<NonZeroU64>,
31}
32
33fn unix_timestamp_to_nonzerou64(timestamp: i64) -> NonZeroU64 {
35 if timestamp <= 0 {
36 NonZeroU64::new(1).unwrap()
37 } else {
38 NonZeroU64::new(timestamp.try_into().unwrap()).unwrap()
39 }
40}
41
42impl HstsEntry {
43 pub fn new(
44 host: String,
45 subdomains: IncludeSubdomains,
46 max_age: Option<Duration>,
47 ) -> Option<HstsEntry> {
48 let expires_at = max_age.map(|duration| {
49 unix_timestamp_to_nonzerou64((UtcDateTime::now() + duration).unix_timestamp())
50 });
51 if host.parse::<Ipv4Addr>().is_ok() || host.parse::<Ipv6Addr>().is_ok() {
52 None
53 } else {
54 Some(HstsEntry {
55 host,
56 include_subdomains: (subdomains == IncludeSubdomains::Included),
57 expires_at,
58 })
59 }
60 }
61
62 pub fn is_expired(&self) -> bool {
63 match self.expires_at {
64 Some(timestamp) => {
65 unix_timestamp_to_nonzerou64(UtcDateTime::now().unix_timestamp()) >= timestamp
66 },
67 _ => false,
68 }
69 }
70
71 fn matches_domain(&self, host: &str) -> bool {
72 self.host == host
73 }
74
75 fn matches_subdomain(&self, host: &str) -> bool {
76 host.ends_with(&format!(".{}", self.host))
77 }
78}
79
80#[derive(Clone, Debug, Default, Deserialize, MallocSizeOf, Serialize)]
81pub struct HstsList {
82 pub entries_map: HashMap<String, Vec<HstsEntry>>,
84}
85
86#[derive(Debug)]
93pub struct HstsPreloadList(fst::Map<ResourceBytes>);
94
95impl HstsPreloadList {
96 fn get(&self, domain: &str) -> Option<u64> {
97 self.0.get(domain)
98 }
99
100 #[expect(clippy::len_without_is_empty)]
101 pub fn len(&self) -> usize {
102 self.0.len()
103 }
104}
105
106impl MallocSizeOf for HstsPreloadList {
107 fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
108 self.0.as_fst().as_inner().size_of(ops)
109 }
110}
111
112static PRELOAD_LIST_ENTRIES: LazyLock<HstsPreloadList> =
113 LazyLock::new(HstsPreloadList::from_servo_preload);
114
115pub fn hsts_preload_size_of(ops: &mut MallocSizeOfOps) -> usize {
116 PRELOAD_LIST_ENTRIES.size_of(ops)
117}
118
119impl HstsPreloadList {
120 pub fn from_preload(preload_content: ResourceBytes) -> Option<HstsPreloadList> {
122 Map::new(preload_content).map(HstsPreloadList).ok()
123 }
124
125 pub fn from_servo_preload() -> HstsPreloadList {
126 debug!("Intializing HSTS Preload list");
127 let map_bytes = resources::read_bytes(Resource::HstsPreloadList);
128
129 HstsPreloadList::from_preload(map_bytes).unwrap_or_else(|| {
130 error!("HSTS preload file is invalid. Setting HSTS list to default values");
131 let list = MapBuilder::memory()
132 .into_map()
133 .map_data(ResourceBytes::from_vec)
134 .expect("Should always be valid");
135
136 HstsPreloadList(list)
137 })
138 }
139
140 pub fn is_host_secure(&self, host: &str) -> bool {
141 let base_domain = reg_suffix(host);
142 let parts = host[..host.len() - base_domain.len()].rsplit_terminator('.');
143 let mut domain_to_test = base_domain.to_owned();
144
145 if self.get(&domain_to_test).is_some_and(|id| {
146 id % 2 == 1 || domain_to_test == host
148 }) {
149 return true;
150 }
151
152 for part in parts {
154 domain_to_test = format!("{}.{}", part, domain_to_test);
155 if self.get(&domain_to_test).is_some_and(|id| {
156 id % 2 == 1 || domain_to_test == host
158 }) {
159 return true;
160 }
161 }
162 false
163 }
164}
165
166impl HstsList {
167 pub fn is_host_secure(&self, host: &str) -> bool {
168 if PRELOAD_LIST_ENTRIES.is_host_secure(host) {
169 info!("{host} is in the preload list");
170 return true;
171 }
172
173 let base_domain = reg_suffix(host);
174 self.entries_map.get(base_domain).is_some_and(|entries| {
175 entries.iter().filter(|e| !e.is_expired()).any(|e| {
176 if e.include_subdomains {
177 e.matches_subdomain(host) || e.matches_domain(host)
178 } else {
179 e.matches_domain(host)
180 }
181 })
182 })
183 }
184
185 fn has_domain(&self, host: &str, base_domain: &str) -> bool {
186 self.entries_map
187 .get(base_domain)
188 .is_some_and(|entries| entries.iter().any(|e| e.matches_domain(host)))
189 }
190
191 fn has_subdomain(&self, host: &str, base_domain: &str) -> bool {
192 self.entries_map.get(base_domain).is_some_and(|entries| {
193 entries
194 .iter()
195 .any(|e| e.include_subdomains && e.matches_subdomain(host))
196 })
197 }
198
199 pub fn push(&mut self, entry: HstsEntry) {
200 let base_domain = reg_suffix(&entry.host);
201 let have_domain = self.has_domain(&entry.host, base_domain);
202 let have_subdomain = self.has_subdomain(&entry.host, base_domain);
203
204 let entries = self.entries_map.entry(base_domain.to_owned()).or_default();
205 if !have_domain && !have_subdomain {
206 entries.push(entry);
207 } else if !have_subdomain {
208 for e in entries.iter_mut() {
209 if e.matches_domain(&entry.host) {
210 e.include_subdomains = entry.include_subdomains;
211 e.expires_at = entry.expires_at;
212 }
213 }
214 }
215 entries.retain(|e| !e.is_expired());
216 }
217
218 pub fn apply_hsts_rules(&self, url: &mut ServoUrl) {
220 if url.scheme() != "http" && url.scheme() != "ws" {
221 return;
222 }
223
224 let upgrade_scheme = if pref!(network_enforce_tls_enabled) {
225 if (!pref!(network_enforce_tls_localhost) &&
226 match url.host() {
227 Some(Host::Domain(domain)) => {
228 domain.ends_with(".localhost") || domain == "localhost"
229 },
230 Some(Host::Ipv4(ipv4)) => ipv4.is_loopback(),
231 Some(Host::Ipv6(ipv6)) => ipv6.is_loopback(),
232 _ => false,
233 }) ||
234 (!pref!(network_enforce_tls_onion) &&
235 url.domain()
236 .is_some_and(|domain| domain.ends_with(".onion")))
237 {
238 url.domain()
239 .is_some_and(|domain| self.is_host_secure(domain))
240 } else {
241 true
242 }
243 } else {
244 url.domain()
245 .is_some_and(|domain| self.is_host_secure(domain))
246 };
247
248 if upgrade_scheme {
249 let upgraded_scheme = match url.scheme() {
250 "ws" => "wss",
251 _ => "https",
252 };
253 url.as_mut_url().set_scheme(upgraded_scheme).unwrap();
254 }
255 }
256
257 pub fn update_hsts_list_from_response(&mut self, url: &ServoUrl, headers: &HeaderMap) {
258 if url.scheme() != "https" && url.scheme() != "wss" {
259 return;
260 }
261
262 if let Some(header) = headers.typed_get::<StrictTransportSecurity>() &&
263 let Some(host) = url.domain()
264 {
265 let include_subdomains = if header.include_subdomains() {
266 IncludeSubdomains::Included
267 } else {
268 IncludeSubdomains::NotIncluded
269 };
270
271 if let Some(entry) =
272 HstsEntry::new(host.to_owned(), include_subdomains, Some(header.max_age()))
273 {
274 info!("adding host {} to the strict transport security list", host);
275 info!("- max-age {}", header.max_age().as_secs());
276 if header.include_subdomains() {
277 info!("- includeSubdomains");
278 }
279
280 self.push(entry);
281 }
282 }
283 }
284}