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

Moves HSTS includeSubdomains enum to net_traits

  • Loading branch information
samfoo committed Jul 21, 2015
commit bae979137ad54c4c4b50dfce7771401285456053
@@ -5,6 +5,7 @@
use rustc_serialize::json::{decode};
use time;
use url::Url;
use net_traits::IncludeSubdomains;
use resource_task::{IPV4_REGEX, IPV6_REGEX};

use std::str::{from_utf8};
@@ -19,20 +20,14 @@ pub struct HSTSEntry {
pub timestamp: Option<u64>
}

#[derive(PartialEq, Copy, Clone)]
pub enum Subdomains {
Included,
NotIncluded
}

impl HSTSEntry {
pub fn new(host: String, subdomains: Subdomains, max_age: Option<u64>) -> Option<HSTSEntry> {
pub fn new(host: String, subdomains: IncludeSubdomains, max_age: Option<u64>) -> Option<HSTSEntry> {
if IPV4_REGEX.is_match(&host) || IPV6_REGEX.is_match(&host) {
None
} else {
Some(HSTSEntry {
host: host,
include_subdomains: (subdomains == Subdomains::Included),
include_subdomains: (subdomains == IncludeSubdomains::Included),
max_age: max_age,
timestamp: Some(time::get_time().sec as u64)
})
@@ -19,7 +19,7 @@ use util::opts;
use util::task::spawn_named;
use url::Url;

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

use devtools_traits::{DevtoolsControlMsg};
use hyper::header::{ContentType, Header, SetCookie, UserAgent};
@@ -236,13 +236,7 @@ impl ResourceChannelManager {
consumer.send(self.resource_manager.cookie_storage.cookies_for_url(&url, source)).unwrap();
}
ControlMsg::SetHSTSEntryForHost(host, include_subdomains, max_age) => {
let subdomains = if include_subdomains {
Subdomains::Included
} else {
Subdomains::NotIncluded
};

if let Some(entry) = HSTSEntry::new(host, subdomains, max_age) {
if let Some(entry) = HSTSEntry::new(host, include_subdomains, max_age) {
self.resource_manager.add_hsts_entry(entry)
}
}
@@ -119,6 +119,12 @@ pub enum LoadConsumer {
/// Handle to a resource task
pub type ResourceTask = Sender<ControlMsg>;

#[derive(PartialEq, Copy, Clone)]
pub enum IncludeSubdomains {
Included,
NotIncluded
}

pub enum ControlMsg {
/// Request the data associated with a particular URL
Load(LoadData, LoadConsumer),
@@ -127,7 +133,7 @@ pub enum ControlMsg {
/// Retrieve the stored cookies for a given URL
GetCookiesForUrl(Url, Sender<Option<String>>, CookieSource),
/// Store a domain's STS information
SetHSTSEntryForHost(String, bool, Option<u64>),
SetHSTSEntryForHost(String, IncludeSubdomains, Option<u64>),
Exit
}

Some generated files are not rendered by default. Learn more.

@@ -4,7 +4,7 @@

use net::hsts::HSTSList;
use net::hsts::HSTSEntry;
use net::hsts::Subdomains;
use net_traits::IncludeSubdomains;
use net::hsts::secure_url;
use net::resource_task::ResourceManager;
use std::sync::mpsc::channel;
@@ -21,7 +21,7 @@ fn test_add_hsts_entry_to_resource_manager_adds_an_hsts_entry() {
let mut manager = ResourceManager::new(None, tx, list, None);

let entry = HSTSEntry::new(
"mozilla.org".to_string(), Subdomains::NotIncluded, None
"mozilla.org".to_string(), IncludeSubdomains::NotIncluded, None
);

assert!(!manager.is_host_sts("mozilla.org"));
@@ -70,7 +70,7 @@ fn test_hsts_entry_is_expired_when_it_has_reached_its_max_age() {
#[test]
fn test_hsts_entry_cant_be_created_with_ipv6_address_as_host() {
let entry = HSTSEntry::new(
"2001:0db8:0000:0000:0000:ff00:0042:8329".to_string(), Subdomains::NotIncluded, None
"2001:0db8:0000:0000:0000:ff00:0042:8329".to_string(), IncludeSubdomains::NotIncluded, None
);

assert!(entry.is_none(), "able to create HSTSEntry with IPv6 host");
@@ -79,7 +79,7 @@ fn test_hsts_entry_cant_be_created_with_ipv6_address_as_host() {
#[test]
fn test_hsts_entry_cant_be_created_with_ipv4_address_as_host() {
let entry = HSTSEntry::new(
"4.4.4.4".to_string(), Subdomains::NotIncluded, None
"4.4.4.4".to_string(), IncludeSubdomains::NotIncluded, None
);

assert!(entry.is_none(), "able to create HSTSEntry with IPv4 host");
@@ -88,45 +88,53 @@ fn test_hsts_entry_cant_be_created_with_ipv4_address_as_host() {
#[test]
fn test_push_entry_with_0_max_age_evicts_entry_from_list() {
let mut list = HSTSList {
entries: vec!(HSTSEntry::new("mozilla.org".to_string(), Subdomains::NotIncluded, Some(500000u64)).unwrap())
entries: vec!(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, Some(500000u64)).unwrap())
};

list.push(HSTSEntry::new("mozilla.org".to_string(), Subdomains::NotIncluded, Some(0)).unwrap());
list.push(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, Some(0)).unwrap());

assert!(list.is_host_secure("mozilla.org") == false)
}

#[test]
fn test_push_entry_to_hsts_list_should_not_add_subdomains_whose_superdomain_is_already_matched() {
let mut list = HSTSList {
entries: vec!(HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap())
entries: vec!(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap())
};

list.push(HSTSEntry::new("servo.mozilla.org".to_string(), Subdomains::NotIncluded, None).unwrap());
list.push(HSTSEntry::new("servo.mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, None).unwrap());

assert!(list.entries.len() == 1)
}

#[test]
fn test_push_entry_to_hsts_list_should_update_existing_domain_entrys_include_subdomains() {
let mut list = HSTSList {
entries: vec!(HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap())
entries: vec!(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap())
};

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

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

assert!(!list.is_host_secure("servo.mozilla.org"))
}

#[test]
fn test_push_entry_to_hsts_list_should_not_create_duplicate_entry() {
let mut list = HSTSList {
entries: vec!(HSTSEntry::new("mozilla.org".to_string(), Subdomains::NotIncluded, None).unwrap())
entries: vec!(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, None).unwrap())
};

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

assert!(list.entries.len() == 1)
}
@@ -140,8 +148,10 @@ fn test_push_multiple_entrie_to_hsts_list_should_add_them_all() {
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());
list.push(HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap());
list.push(HSTSEntry::new("bugzilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap());

assert!(list.is_host_secure("mozilla.org"));
assert!(list.is_host_secure("bugzilla.org"));
@@ -155,7 +165,8 @@ fn test_push_entry_to_hsts_list_should_add_an_entry() {

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

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

assert!(list.is_host_secure("mozilla.org"));
}
@@ -199,7 +210,8 @@ fn test_hsts_list_with_no_entries_does_not_is_host_secure() {
#[test]
fn test_hsts_list_with_exact_domain_entry_is_is_host_secure() {
let hsts_list = HSTSList {
entries: vec![HSTSEntry::new("mozilla.org".to_string(), Subdomains::NotIncluded, None).unwrap()]
entries: vec![HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, None).unwrap()]
};

assert!(hsts_list.is_host_secure("mozilla.org"));
@@ -208,7 +220,8 @@ fn test_hsts_list_with_exact_domain_entry_is_is_host_secure() {
#[test]
fn test_hsts_list_with_subdomain_when_include_subdomains_is_true_is_is_host_secure() {
let hsts_list = HSTSList {
entries: vec![HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap()]
entries: vec![HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap()]
};

assert!(hsts_list.is_host_secure("servo.mozilla.org"));
@@ -217,7 +230,8 @@ fn test_hsts_list_with_subdomain_when_include_subdomains_is_true_is_is_host_secu
#[test]
fn test_hsts_list_with_subdomain_when_include_subdomains_is_false_is_not_is_host_secure() {
let hsts_list = HSTSList {
entries: vec![HSTSEntry::new("mozilla.org".to_string(), Subdomains::NotIncluded, None).unwrap()]
entries: vec![HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::NotIncluded, None).unwrap()]
};

assert!(!hsts_list.is_host_secure("servo.mozilla.org"));
@@ -226,7 +240,8 @@ fn test_hsts_list_with_subdomain_when_include_subdomains_is_false_is_not_is_host
#[test]
fn test_hsts_list_with_subdomain_when_host_is_not_a_subdomain_is_not_is_host_secure() {
let hsts_list = HSTSList {
entries: vec![HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap()]
entries: vec![HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap()]
};

assert!(!hsts_list.is_host_secure("servo-mozilla.org"));
@@ -235,7 +250,8 @@ fn test_hsts_list_with_subdomain_when_host_is_not_a_subdomain_is_not_is_host_sec
#[test]
fn test_hsts_list_with_subdomain_when_host_is_exact_match_is_is_host_secure() {
let hsts_list = HSTSList {
entries: vec![HSTSEntry::new("mozilla.org".to_string(), Subdomains::Included, None).unwrap()]
entries: vec![HSTSEntry::new("mozilla.org".to_string(),
IncludeSubdomains::Included, None).unwrap()]
};

assert!(hsts_list.is_host_secure("mozilla.org"));
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.