From faa3d8724b9d45bc15f03b3fe46058daf565cd21 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 17 Apr 2016 12:25:17 -0400 Subject: [PATCH] Refactor Servo HSTS file loading, hard-fail if can't load. Use constructor pattern instead of separate utility function. Instead of allowing the Servo HSTS file loading to silently fail, we should expect that file to always exist and be formatted correctly. --- components/net/hsts.rs | 13 +++++++------ components/net/resource_thread.rs | 8 ++------ tests/unit/net/hsts.rs | 4 ++-- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/components/net/hsts.rs b/components/net/hsts.rs index 946bb19c294..70c301a2ed8 100644 --- a/components/net/hsts.rs +++ b/components/net/hsts.rs @@ -70,6 +70,13 @@ impl HSTSList { .and_then(|c| decode(c).ok()) } + pub fn from_servo_preload() -> HSTSList { + let file_bytes = read_resource_file("hsts_preload.json") + .expect("Could not find Servo HSTS preload file"); + HSTSList::from_preload(&file_bytes) + .expect("Servo HSTS preload file is invalid") + } + pub fn is_host_secure(&self, host: &str) -> bool { // TODO - Should this be faster than O(n)? The HSTS list is only a few // hundred or maybe thousand entries... @@ -114,12 +121,6 @@ impl HSTSList { } } -pub fn preload_hsts_domains() -> Option { - read_resource_file("hsts_preload.json") - .ok() - .and_then(|bytes| HSTSList::from_preload(&bytes)) -} - pub fn secure_url(url: &Url) -> Url { if &*url.scheme == "http" { let mut secure_url = url.clone(); diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index e7b1b58e9db..30125e2f983 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -10,7 +10,7 @@ use cookie_storage::CookieStorage; use data_loader; use devtools_traits::{DevtoolsControlMsg}; use file_loader; -use hsts::{HSTSList, preload_hsts_domains}; +use hsts::HSTSList; use http_loader::{self, Connector, create_http_connector, HttpState}; use hyper::client::pool::Pool; use hyper::header::{ContentType, Header, SetCookie}; @@ -148,11 +148,7 @@ fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata) -> Result>) -> ResourceThread { - let hsts_preload = match preload_hsts_domains() { - Some(list) => list, - None => HSTSList::new() - }; - + let hsts_preload = HSTSList::from_servo_preload(); let (setup_chan, setup_port) = ipc::channel().unwrap(); let setup_chan_clone = setup_chan.clone(); spawn_named("ResourceManager".to_owned(), move || { diff --git a/tests/unit/net/hsts.rs b/tests/unit/net/hsts.rs index 95eeaffcecb..76768972f2f 100644 --- a/tests/unit/net/hsts.rs +++ b/tests/unit/net/hsts.rs @@ -2,8 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use net::hsts::secure_url; use net::hsts::{HSTSList, HSTSEntry}; -use net::hsts::{secure_url, preload_hsts_domains}; use net_traits::IncludeSubdomains; use time; use url::Url; @@ -250,7 +250,7 @@ fn test_hsts_list_with_expired_entry_is_not_is_host_secure() { #[test] fn test_preload_hsts_domains_well_formed() { - let hsts_list = preload_hsts_domains().unwrap(); + let hsts_list = HSTSList::from_servo_preload(); assert!(!hsts_list.entries.is_empty()); }