From 00241e84bc896d53b9bdbc4916cd738a16f23679 Mon Sep 17 00:00:00 2001 From: shanehandley <1322294+shanehandley@users.noreply.github.com> Date: Mon, 31 Jul 2023 18:53:53 +1000 Subject: [PATCH] Remove customised implementation of hsts headers. (#30046) This looks to have originally been implemented due to missing functionality in the headers crate, which has since been added and released. --- components/net/hsts.rs | 102 ++++------------------------------------- 1 file changed, 9 insertions(+), 93 deletions(-) diff --git a/components/net/hsts.rs b/components/net/hsts.rs index dfaab4f2b60..de78e342ee8 100644 --- a/components/net/hsts.rs +++ b/components/net/hsts.rs @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use embedder_traits::resources::{self, Resource}; -use headers::{Header, HeaderMapExt, HeaderName, HeaderValue}; +use headers::{HeaderMapExt, StrictTransportSecurity}; use http::HeaderMap; use net_traits::pub_domains::reg_suffix; use net_traits::IncludeSubdomains; @@ -187,18 +187,20 @@ impl HstsList { if let Some(header) = headers.typed_get::() { if let Some(host) = url.domain() { - let include_subdomains = if header.include_subdomains { + let include_subdomains = if header.include_subdomains() { IncludeSubdomains::Included } else { IncludeSubdomains::NotIncluded }; - if let Some(entry) = - HstsEntry::new(host.to_owned(), include_subdomains, Some(header.max_age)) - { + if let Some(entry) = HstsEntry::new( + host.to_owned(), + include_subdomains, + Some(header.max_age().as_secs()), + ) { info!("adding host {} to the strict transport security list", host); - info!("- max-age {}", header.max_age); - if header.include_subdomains { + info!("- max-age {}", header.max_age().as_secs()); + if header.include_subdomains() { info!("- includeSubdomains"); } @@ -208,89 +210,3 @@ impl HstsList { } } } - -// TODO: Remove this with the next update of the `headers` crate -// https://github.com/hyperium/headers/issues/61 -#[derive(Clone, Debug, PartialEq)] -struct StrictTransportSecurity { - include_subdomains: bool, - max_age: u64, -} - -enum Directive { - MaxAge(u64), - IncludeSubdomains, - Unknown, -} - -// taken from https://github.com/hyperium/headers -impl Header for StrictTransportSecurity { - fn name() -> &'static HeaderName { - &http::header::STRICT_TRANSPORT_SECURITY - } - - fn decode<'i, I: Iterator>( - values: &mut I, - ) -> Result { - values - .just_one() - .and_then(|v| v.to_str().ok()) - .map(|s| { - s.split(';') - .map(str::trim) - .map(|sub| { - if sub.eq_ignore_ascii_case("includeSubDomains") { - Some(Directive::IncludeSubdomains) - } else { - let mut sub = sub.splitn(2, '='); - match (sub.next(), sub.next()) { - (Some(left), Some(right)) - if left.trim().eq_ignore_ascii_case("max-age") => - { - right - .trim() - .trim_matches('"') - .parse() - .ok() - .map(Directive::MaxAge) - }, - _ => Some(Directive::Unknown), - } - } - }) - .fold(Some((None, None)), |res, dir| match (res, dir) { - (Some((None, sub)), Some(Directive::MaxAge(age))) => Some((Some(age), sub)), - (Some((age, None)), Some(Directive::IncludeSubdomains)) => { - Some((age, Some(()))) - }, - (Some((Some(_), _)), Some(Directive::MaxAge(_))) | - (Some((_, Some(_))), Some(Directive::IncludeSubdomains)) | - (_, None) => None, - (res, _) => res, - }) - .and_then(|res| match res { - (Some(age), sub) => Some(StrictTransportSecurity { - max_age: age, - include_subdomains: sub.is_some(), - }), - _ => None, - }) - .ok_or_else(headers::Error::invalid) - }) - .unwrap_or_else(|| Err(headers::Error::invalid())) - } - - fn encode>(&self, _values: &mut E) {} -} - -trait IterExt: Iterator { - fn just_one(&mut self) -> Option { - let one = self.next()?; - match self.next() { - Some(_) => None, - None => Some(one), - } - } -} - -impl IterExt for T {}