Skip to content

Commit

Permalink
Merge pull request #136 from brave/redirect-important-pt2
Browse files Browse the repository at this point in the history
Redirect important pt2
  • Loading branch information
antonok-edm authored Sep 15, 2020
2 parents 919024d + 16f06e1 commit 77ecd45
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,13 @@ impl Blocker {
.importants
.check(request, &request_tokens, &NO_TAGS);

let redirect_filter = self.redirects.check(request, &request_tokens, &NO_TAGS);

// only check the rest of the rules if not previously matched
let filter = if important_filter.is_none() && !matched_rule {
#[cfg(feature = "metrics")]
print!("tagged\t");
self.filters_tagged.check(request, &request_tokens, &self.tags_enabled)
.or_else(|| {
#[cfg(feature = "metrics")]
print!("redirects\t");
self.redirects.check(request, &request_tokens, &NO_TAGS)
})
.or_else(|| {
#[cfg(feature = "metrics")]
print!("filters\t");
Expand Down Expand Up @@ -229,7 +226,7 @@ impl Blocker {
println!();

// only match redirects if we have them set up
let redirect: Option<String> = filter.as_ref().and_then(|f| {
let redirect: Option<String> = redirect_filter.as_ref().and_then(|f| {
// Filter redirect option is set
if let Some(redirect) = f.redirect.as_ref() {
// And we have a matching redirect resource
Expand All @@ -248,7 +245,7 @@ impl Blocker {
});

// If something has already matched before but we don't know what, still return a match
let matched = exception.is_none() && (filter.is_some() || matched_rule);
let matched = exception.is_none() && (filter.is_some() || redirect_filter.is_some() || matched_rule);
BlockerResult {
matched,
explicit_cancel: matched && filter.is_some() && filter.as_ref().map(|f| f.is_explicit_cancel()).unwrap_or_else(|| false),
Expand Down
21 changes: 21 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,25 @@ mod tests {
assert_eq!(result.generichide, expected_generichide);
});
}

#[test]
fn important_redirect() {
let mut filter_set = FilterSet::new(true);
filter_set.add_filters(&vec![
"||addthis.com^$important,3p,domain=~missingkids.com|~missingkids.org|~sainsburys.jobs|~sitecore.com|~amd.com".to_string(),
"||addthis.com/*/addthis_widget.js$script,redirect=addthis.com/addthis_widget.js".to_string(),
], FilterFormat::Standard);
let mut engine = Engine::from_filter_set(filter_set, false);

engine.add_resource(Resource {
name: "addthis.com/addthis_widget.js".to_owned(),
aliases: vec![],
kind: ResourceType::Mime(MimeType::ApplicationJavascript),
content: "window.addthis = undefined".to_string(),
});

let result = engine.check_network_urls("https://s7.addthis.com/js/250/addthis_widget.js?pub=resto", "https://www.rhmodern.com/catalog/product/product.jsp?productId=prod14970086&categoryId=cat7150028", "script");

assert!(result.redirect.is_some());
}
}

0 comments on commit 77ecd45

Please sign in to comment.