Process content blocker rules in the HTTP loader.

This commit is contained in:
Josh Matthews 2016-04-17 16:45:33 -04:00 committed by Anthony Ramine
parent 50fea8554e
commit 074fc4a5e1
5 changed files with 105 additions and 4 deletions

View file

@ -4,7 +4,8 @@
use brotli::Decompressor;
use connector::Connector;
use content_blocker_parser::RuleList;
use content_blocker_parser::{LoadType, Reaction, Request as CBRequest, ResourceType};
use content_blocker_parser::{RuleList, process_rules_for_request};
use cookie;
use cookie_storage::CookieStorage;
use devtools_traits::{ChromeToDevtoolsControlMsg, DevtoolsControlMsg, HttpRequest as DevtoolsHttpRequest};
@ -329,6 +330,7 @@ pub enum LoadErrorType {
Cancelled,
Connection { reason: String },
ConnectionAborted { reason: String },
ContentBlocked,
// Preflight fetch inconsistent with main fetch
CorsPreflightFetchInconsistent,
Decoding { reason: String },
@ -351,6 +353,7 @@ impl Error for LoadErrorType {
LoadErrorType::Cancelled => "load cancelled",
LoadErrorType::Connection { ref reason } => reason,
LoadErrorType::ConnectionAborted { ref reason } => reason,
LoadErrorType::ContentBlocked => "content blocked",
LoadErrorType::CorsPreflightFetchInconsistent => "preflight fetch inconsistent with main fetch",
LoadErrorType::Decoding { ref reason } => reason,
LoadErrorType::InvalidRedirect { ref reason } => reason,
@ -592,7 +595,8 @@ pub fn modify_request_headers(headers: &mut Headers,
user_agent: &str,
cookie_jar: &Arc<RwLock<CookieStorage>>,
auth_cache: &Arc<RwLock<AuthCache>>,
load_data: &LoadData) {
load_data: &LoadData,
block_cookies: bool) {
// Ensure that the host header is set from the original url
let host = Host {
hostname: url.host_str().unwrap().to_owned(),
@ -622,7 +626,9 @@ pub fn modify_request_headers(headers: &mut Headers,
// https://fetch.spec.whatwg.org/#concept-http-network-or-cache-fetch step 11
if load_data.credentials_flag {
set_request_cookies(url.clone(), headers, cookie_jar);
if !block_cookies {
set_request_cookies(url.clone(), headers, cookie_jar);
}
// https://fetch.spec.whatwg.org/#http-network-or-cache-fetch step 12
set_auth_header(headers, url, auth_cache);
@ -854,6 +860,24 @@ pub fn load<A, B>(load_data: &LoadData,
return Err(LoadError::new(doc_url, LoadErrorType::Cancelled));
}
let mut block_cookies = false;
if let Some(ref rules) = *http_state.blocked_content {
let actions = process_rules_for_request(rules, &CBRequest {
url: &doc_url,
resource_type: to_resource_type(&load_data.context),
load_type: LoadType::FirstParty, //FIXME need request origin
});
for action in actions {
match action {
Reaction::Block => {
return Err(LoadError::new(doc_url, LoadErrorType::ContentBlocked));
},
Reaction::BlockCookies => block_cookies = true,
Reaction::HideMatchingElements(_) => (),
}
}
}
info!("requesting {}", doc_url);
// Avoid automatically preserving request headers when redirects occur.
@ -872,7 +896,7 @@ pub fn load<A, B>(load_data: &LoadData,
modify_request_headers(&mut request_headers, &doc_url,
&user_agent, &http_state.cookie_jar,
&http_state.auth_cache, &load_data);
&http_state.auth_cache, &load_data, block_cookies);
//if there is a new auth header then set the request headers with it
if let Some(ref auth_header) = new_auth_header {
@ -1034,3 +1058,17 @@ fn is_cert_verify_error(error: &OpensslError) -> bool {
}
}
}
fn to_resource_type(context: &LoadContext) -> ResourceType {
match *context {
LoadContext::Browsing => ResourceType::Document,
LoadContext::Image => ResourceType::Image,
LoadContext::AudioVideo => ResourceType::Media,
LoadContext::Plugin => ResourceType::Raw,
LoadContext::Style => ResourceType::StyleSheet,
LoadContext::Script => ResourceType::Script,
LoadContext::Font => ResourceType::Font,
LoadContext::TextTrack => ResourceType::Media,
LoadContext::CacheManifest => ResourceType::Raw,
}
}