Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HSTS (preload-only) #6490

Merged
merged 24 commits into from Jul 22, 2015
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
aa19a9a
Preload an HSTS domain list from chromium
samfoo Jun 19, 2015
d2f3555
Implement mutable HSTS list
samfoo Jun 22, 2015
72d4433
Do not allow IP address in HSTS list
samfoo Jun 22, 2015
855a948
Do not change the port when loading HSTS domain
samfoo Jun 22, 2015
cb9b0c2
Add max-age to HSTS entries
samfoo Jun 22, 2015
8d39fb6
Shift checking for IP address host for HSTS entry to constructor
samfoo Jun 22, 2015
15c90a5
Expire HSTS entries that have exceeded their max-age
samfoo Jun 23, 2015
690ac63
Rename/refactor
samfoo Jun 23, 2015
ff1777e
Evict HSTS entries when a max-age of 0 is seen
samfoo Jun 23, 2015
f284181
Abstract out ResourceManager messaging from impl
samfoo Jun 24, 2015
795454f
Adds control message for HSTS headers
samfoo Jun 24, 2015
8a401d5
Re-parse URL to not have inconsistent state
samfoo Jun 25, 2015
865fb2e
Resolve tidy issues
samfoo Jun 26, 2015
a068a80
Don't unnecessarily clone strings
samfoo Jul 8, 2015
8086034
Commit HSTS preload list to source control
samfoo Jul 8, 2015
29a34db
Resolves code review comments
samfoo Jul 18, 2015
02bd5cd
Resolves remaining code review issues
samfoo Jul 18, 2015
826f56b
Moves HSTS code to it's own module
samfoo Jul 18, 2015
f2148f0
Moves the HSTS replacement code to http_loader
samfoo Jul 18, 2015
11f5be6
Responds to more code review feedback
samfoo Jul 18, 2015
82cafc4
Passes an Arc<Mutex<HSTSList>> to threads instead of cloning
samfoo Jul 19, 2015
bae9791
Moves HSTS includeSubdomains enum to net_traits
samfoo Jul 19, 2015
5014da4
Only secure URL's that aren't already to HTTPS.
samfoo Jul 19, 2015
118122d
Uses the approach suggested by @SimonSapin for changing Url scheme
samfoo Jul 19, 2015
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Responds to more code review feedback

* Use regex from resource task
* Don't have an option of an HSTS list, default to empty
  • Loading branch information
samfoo committed Jul 21, 2015
commit 11f5be6d854634f5bbc8c4bb9d5a0d2fdd15cbb3
@@ -2,20 +2,15 @@
* 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 regex::Regex;
use rustc_serialize::json::{decode};
use time;
use url::Url;
use resource_task::{IPV4_REGEX, IPV6_REGEX};

use std::str::{from_utf8};

use util::resource_files::read_resource_file;

static IPV4_REGEX: Regex = regex!(
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
);
static IPV6_REGEX: Regex = regex!(r"^([a-fA-F0-9]{0,4}[:]?){1,8}(/\d{1,3})?$");

#[derive(RustcDecodable, RustcEncodable, Clone)]
pub struct HSTSEntry {
pub host: String,
@@ -69,6 +64,12 @@ pub struct HSTSList {
}

impl HSTSList {
pub fn new() -> HSTSList {
HSTSList {
entries: vec![]
}
}

pub fn new_from_preload(preload_content: &str) -> Option<HSTSList> {
decode(preload_content).ok()
}
@@ -36,7 +36,7 @@ use std::boxed::FnBox;

pub fn factory(cookies_chan: Sender<ControlMsg>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: Option<HSTSList>)
hsts_list: HSTSList)
-> Box<FnBox(LoadData, LoadConsumer, Arc<MIMEClassifier>) + Send> {
box move |load_data, senders, classifier| {
spawn_named("http_loader".to_owned(),
@@ -72,10 +72,10 @@ fn read_block<R: Read>(reader: &mut R) -> Result<ReadResult, ()> {
}
}

fn request_must_be_secured(hsts_list: Option<&HSTSList>, url: &Url) -> bool {
match (hsts_list.as_ref(), url.domain()) {
(Some(ref l), Some(ref h)) => {
l.is_host_secure(h)
fn request_must_be_secured(hsts_list: &HSTSList, url: &Url) -> bool {
match url.domain() {
Some(ref h) => {
hsts_list.is_host_secure(h)
},
_ => false
}
@@ -86,7 +86,7 @@ fn load(mut load_data: LoadData,
classifier: Arc<MIMEClassifier>,
cookies_chan: Sender<ControlMsg>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: Option<HSTSList>) {
hsts_list: HSTSList) {
// FIXME: At the time of writing this FIXME, servo didn't have any central
// location for configuration. If you're reading this and such a
// repository DOES exist, please update this constant to use it.
@@ -117,7 +117,7 @@ fn load(mut load_data: LoadData,
loop {
iters = iters + 1;

if request_must_be_secured(hsts_list.as_ref(), &url) {
if request_must_be_secured(&hsts_list, &url) {
info!("{} is in the strict transport security list, requesting secure host", url);
url = secure_url(&url);
}
@@ -19,10 +19,7 @@ use util::opts;
use util::task::spawn_named;
use url::Url;

use hsts::HSTSList;
use hsts::HSTSEntry;
use hsts::Subdomains;
use hsts::preload_hsts_domains;
use hsts::{HSTSList, HSTSEntry, Subdomains, preload_hsts_domains};

use devtools_traits::{DevtoolsControlMsg};
use hyper::header::{ContentType, Header, SetCookie, UserAgent};
@@ -39,10 +36,10 @@ use std::sync::Arc;
use std::sync::mpsc::{channel, Receiver, Sender};

static mut HOST_TABLE: Option<*mut HashMap<String, String>> = None;
static IPV4_REGEX: Regex = regex!(
pub static IPV4_REGEX: Regex = regex!(
r"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
);
static IPV6_REGEX: Regex = regex!(r"^([a-fA-F0-9]{0,4}[:]?){1,8}(/\d{1,3})?$");
pub static IPV6_REGEX: Regex = regex!(r"^([a-fA-F0-9]{0,4}[:]?){1,8}(/\d{1,3})?$");

pub fn global_init() {
//TODO: handle bad file path
@@ -165,7 +162,10 @@ pub fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata) -> Result
/// Create a ResourceTask
pub fn new_resource_task(user_agent: Option<String>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>) -> ResourceTask {
let hsts_preload = preload_hsts_domains();
let hsts_preload = match preload_hsts_domains() {
Some(list) => list,
None => HSTSList::new()
};

let (setup_chan, setup_port) = channel();
let setup_chan_clone = setup_chan.clone();
@@ -260,13 +260,13 @@ pub struct ResourceManager {
resource_task: Sender<ControlMsg>,
mime_classifier: Arc<MIMEClassifier>,
devtools_chan: Option<Sender<DevtoolsControlMsg>>,
hsts_list: Option<HSTSList>
hsts_list: HSTSList
}

impl ResourceManager {
pub fn new(user_agent: Option<String>,
resource_task: Sender<ControlMsg>,
hsts_list: Option<HSTSList>,
hsts_list: HSTSList,
devtools_channel: Option<Sender<DevtoolsControlMsg>>) -> ResourceManager {
ResourceManager {
user_agent: user_agent,
@@ -293,16 +293,11 @@ impl ResourceManager {
}

pub fn add_hsts_entry(&mut self, entry: HSTSEntry) {
if let Some(list) = self.hsts_list.as_mut() {
list.push(entry)
}
self.hsts_list.push(entry);
}

pub fn is_host_sts(&self, host: &str) -> bool {
match self.hsts_list.as_ref() {
Some(list) => list.is_host_secure(host),
None => false
}
self.hsts_list.is_host_secure(host)
}

fn load(&mut self, mut load_data: LoadData, consumer: LoadConsumer) {
@@ -7,7 +7,6 @@ use net::hsts::HSTSEntry;
use net::hsts::Subdomains;
use net::hsts::secure_url;
use net::resource_task::ResourceManager;
use net_traits::LoadData;
use std::sync::mpsc::channel;
use url::Url;
use time;
@@ -19,7 +18,7 @@ fn test_add_hsts_entry_to_resource_manager_adds_an_hsts_entry() {
};

let (tx, _) = channel();
let mut manager = ResourceManager::new(None, tx, Some(list), None);
let mut manager = ResourceManager::new(None, tx, list, None);

let entry = HSTSEntry::new(
"mozilla.org".to_string(), Subdomains::NotIncluded, None
@@ -134,6 +133,18 @@ fn test_push_entry_to_hsts_list_should_not_create_duplicate_entry() {

#[test]
fn test_push_multiple_entrie_to_hsts_list_should_add_them_all() {
let mut list = HSTSList {
entries: Vec::new()
};

assert!(!list.is_host_secure("mozilla.org"));
assert!(!list.is_host_secure("bugzilla.org"));

list.push(HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap());
list.push(HSTSEntry::new("bugzilla.org".to_string(), Subdomains::Included, None).unwrap());

assert!(list.is_host_secure("mozilla.org"));
assert!(list.is_host_secure("bugzilla.org"));
}

#[test]
@@ -143,13 +154,10 @@ fn test_push_entry_to_hsts_list_should_add_an_entry() {
};

assert!(!list.is_host_secure("mozilla.org"));
assert!(!list.is_host_secure("bugzilla.org"));

list.push(HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap());
list.push(HSTSEntry::new("bugzilla.org".to_string(), Subdomains::Included, None).unwrap());

assert!(list.is_host_secure("mozilla.org"));
assert!(list.is_host_secure("bugzilla.org"));
}

#[test]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.