Skip to content

Redirect important pt2 #136

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

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}