From 6c0927c2897c172f580b1f0d4c4b91aae5a4a601 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 15 Apr 2016 18:07:55 -0400 Subject: [PATCH 1/3] Add relevant sepc link for 'apache bug' mimesniff. --- components/net/resource_thread.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index e7b1b58e9db..7b8a2da7435 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -113,6 +113,7 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat start_sending_opt(start_chan, metadata) } +/// https://mimesniff.spec.whatwg.org/#supplied-mime-type-detection-algorithm fn apache_bug_predicate(last_raw_content_type: &[u8]) -> ApacheBugFlag { if last_raw_content_type == b"text/plain" || last_raw_content_type == b"text/plain; charset=ISO-8859-1" From 540e5ce6adf5109300a527865b63eab118dfedad Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 15 Apr 2016 18:10:33 -0400 Subject: [PATCH 2/3] Use constructor pattern for `ApacheFlag` enum. --- components/net/mime_classifier.rs | 14 ++++++++++++++ components/net/resource_thread.rs | 14 +------------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/components/net/mime_classifier.rs b/components/net/mime_classifier.rs index 3b97566f018..e9bc4ed4f4f 100644 --- a/components/net/mime_classifier.rs +++ b/components/net/mime_classifier.rs @@ -28,6 +28,20 @@ pub enum ApacheBugFlag { OFF } +impl ApacheBugFlag { + /// https://mimesniff.spec.whatwg.org/#supplied-mime-type-detection-algorithm + pub fn from_content_type(last_raw_content_type: &[u8]) -> ApacheBugFlag { + if last_raw_content_type == b"text/plain" + || last_raw_content_type == b"text/plain; charset=ISO-8859-1" + || last_raw_content_type == b"text/plain; charset=iso-8859-1" + || last_raw_content_type == b"text/plain; charset=UTF-8" { + ApacheBugFlag::ON + } else { + ApacheBugFlag::OFF + } + } +} + #[derive(PartialEq)] pub enum NoSniffFlag { ON, diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index 7b8a2da7435..c218c793ebb 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -86,7 +86,7 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat if let Some(ref raw_content_type) = headers.get_raw("content-type") { if raw_content_type.len() > 0 { let last_raw_content_type = &raw_content_type[raw_content_type.len() - 1]; - check_for_apache_bug = apache_bug_predicate(last_raw_content_type) + check_for_apache_bug = ApacheBugFlag::from_content_type(last_raw_content_type) } } if let Some(ref raw_content_type_options) = headers.get_raw("X-content-type-options") { @@ -113,18 +113,6 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat start_sending_opt(start_chan, metadata) } -/// https://mimesniff.spec.whatwg.org/#supplied-mime-type-detection-algorithm -fn apache_bug_predicate(last_raw_content_type: &[u8]) -> ApacheBugFlag { - if last_raw_content_type == b"text/plain" - || last_raw_content_type == b"text/plain; charset=ISO-8859-1" - || last_raw_content_type == b"text/plain; charset=iso-8859-1" - || last_raw_content_type == b"text/plain; charset=UTF-8" { - ApacheBugFlag::ON - } else { - ApacheBugFlag::OFF - } -} - /// For use by loaders in responding to a Load message. fn start_sending_opt(start_chan: LoadConsumer, metadata: Metadata) -> Result { match start_chan { From da34d469cfe6cafeb9b1b1fc05fa429f88c890b9 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 15 Apr 2016 18:14:16 -0400 Subject: [PATCH 3/3] Cleanup retrieval of last slice item. --- components/net/resource_thread.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/components/net/resource_thread.rs b/components/net/resource_thread.rs index c218c793ebb..38ad17e9a00 100644 --- a/components/net/resource_thread.rs +++ b/components/net/resource_thread.rs @@ -83,11 +83,8 @@ pub fn start_sending_sniffed_opt(start_chan: LoadConsumer, mut metadata: Metadat let mut check_for_apache_bug = ApacheBugFlag::OFF; if let Some(ref headers) = metadata.headers { - if let Some(ref raw_content_type) = headers.get_raw("content-type") { - if raw_content_type.len() > 0 { - let last_raw_content_type = &raw_content_type[raw_content_type.len() - 1]; - check_for_apache_bug = ApacheBugFlag::from_content_type(last_raw_content_type) - } + if let Some(ref content_type) = headers.get_raw("content-type").and_then(|c| c.last()) { + check_for_apache_bug = ApacheBugFlag::from_content_type(content_type) } if let Some(ref raw_content_type_options) = headers.get_raw("X-content-type-options") { if raw_content_type_options.iter().any(|ref opt| *opt == b"nosniff") {