Skip to content

Commit

Permalink
only apply removeparam to document/subdocument/xhr by default
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Jun 5, 2024
1 parent 859e7de commit da3d470
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 30 deletions.
56 changes: 36 additions & 20 deletions src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ mod blocker_tests {
"*$removeparam=fbclid",
"/script.js$redirect-rule=noopjs",
"^block^$important",
"$removeparam=testCase,~image",
"$removeparam=testCase,~xhr",
];

let (network_filters, _) = parse_filters(&filters, true, Default::default());
Expand All @@ -1630,73 +1630,89 @@ mod blocker_tests {

resources.add_resource(Resource::simple("noopjs", crate::resources::MimeType::ApplicationJavascript, "(() => {})()")).unwrap();

let result = blocker.check(&Request::new("https://example.com?q=1&test=2#blue", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q=1&test=2#blue", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q=1#blue".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?test=2&q=1#blue", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?test=2&q=1#blue", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q=1#blue".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?test=2#blue", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?test=2#blue", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com#blue".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q=1#blue", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q=1#blue", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q=1&test=2", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q=1&test=2", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q=1".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?test=2&q=1", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?test=2&q=1", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q=1".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?test=2", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?test=2", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q=1", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?test=2", "https://antonok.com", "image").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q=fbclid", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q=1", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?fbclid=10938&q=1&test=2", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q=fbclid", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?fbclid=10938&q=1&test=2", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q=1".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://test.com?fbclid=10938&q=1&test=2", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://test.com?fbclid=10938&q=1&test=2", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://test.com?q=1&test=2".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q1=1&q2=2&q3=3&test=2&q4=4&q5=5&fbclid=39", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q1=1&q2=2&q3=3&test=2&q4=4&q5=5&fbclid=39", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q1=1&q2=2&q3=3&q4=4&q5=5".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?q1=1&q1=2&test=2&test=3", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?q1=1&q1=2&test=2&test=3", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?q1=1&q1=2".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com/script.js?test=2#blue", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com/script.js?test=2#blue", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com/script.js#blue".into()));
assert_eq!(result.redirect, Some("data:application/javascript;base64,KCgpID0+IHt9KSgp".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com/block/script.js?test=2", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com/block/script.js?test=2", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert_eq!(result.redirect, Some("data:application/javascript;base64,KCgpID0+IHt9KSgp".into()));
assert!(result.matched);

let result = blocker.check(&Request::new("https://example.com/Path/?Test=ABC&testcase=AbC&testCase=aBc", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com/Path/?Test=ABC&testcase=AbC&testCase=aBc", "https://antonok.com", "xhr").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com/Path/?Test=ABC&testcase=AbC&testCase=aBc", "https://antonok.com", "image").unwrap(), &resources);
assert_eq!(result.rewritten_url, None);
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com/Path/?Test=ABC&testcase=AbC&testCase=aBc", "https://antonok.com", "subdocument").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com/Path/?Test=ABC&testcase=AbC".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com/Path/?Test=ABC&testcase=AbC&testCase=aBc", "https://antonok.com", "document").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com/Path/?Test=ABC&testcase=AbC".into()));
assert!(!result.matched);

let result = blocker.check(&Request::new("https://example.com?Test=ABC?123&test=3#&test=4#b", "https://antonok.com", "script").unwrap(), &resources);
let result = blocker.check(&Request::new("https://example.com?Test=ABC?123&test=3#&test=4#b", "https://antonok.com", "document").unwrap(), &resources);
assert_eq!(result.rewritten_url, Some("https://example.com?Test=ABC?123#&test=4#b".into()));
assert!(!result.matched);

Expand Down Expand Up @@ -1774,7 +1790,7 @@ mod blocker_tests {
let resources = ResourceStorage::default();

for (original, expected) in testcases.into_iter() {
let result = blocker.check(&Request::new(original, "https://example.net", "script").unwrap(), &resources);
let result = blocker.check(&Request::new(original, "https://example.net", "xhr").unwrap(), &resources);
let expected = if original == expected {
None
} else {
Expand All @@ -1799,7 +1815,7 @@ fn test_removeparam_same_tokens() {

let blocker = Blocker::new(network_filters, &blocker_options);

let result = blocker.check(&Request::new("https://example.com?example1_=1&example1-=2", "https://example.com", "script").unwrap(), &Default::default());
let result = blocker.check(&Request::new("https://example.com?example1_=1&example1-=2", "https://example.com", "xhr").unwrap(), &Default::default());
assert_eq!(result.rewritten_url, Some("https://example.com".into()));
assert!(!result.matched);
}
Expand Down
24 changes: 14 additions & 10 deletions src/filters/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,22 @@ impl NetworkFilter {

// If any negated "network" types were set, then implicitly enable all network types.
// The negated types will be applied later.
if (cpt_mask_negative & NetworkFilterMask::FROM_NETWORK_TYPES) != NetworkFilterMask::NONE {
//
// This doesn't apply to removeparam filters.
if !mask.contains(NetworkFilterMask::IS_REMOVEPARAM)
&& (cpt_mask_negative & NetworkFilterMask::FROM_NETWORK_TYPES) != NetworkFilterMask::NONE {
mask |= NetworkFilterMask::FROM_NETWORK_TYPES;
}
// If no positive types were set, then the filter should apply to all network types.
if (cpt_mask_positive & NetworkFilterMask::FROM_ALL_TYPES).is_empty() {
mask |= NetworkFilterMask::FROM_NETWORK_TYPES;
// Removeparam is again a special case.
if mask.contains(NetworkFilterMask::IS_REMOVEPARAM) {
mask |= NetworkFilterMask::FROM_DOCUMENT
| NetworkFilterMask::FROM_SUBDOCUMENT
| NetworkFilterMask::FROM_XMLHTTPREQUEST;
} else {
mask |= NetworkFilterMask::FROM_NETWORK_TYPES;
}
}

match parsed.pattern.left_anchor {
Expand Down Expand Up @@ -833,13 +843,6 @@ impl NetworkFilter {
return Err(NetworkFilterError::RemoveparamWithException);
}

// `removeparam` rules apply to all request types by default, including document.
if mask.contains(NetworkFilterMask::IS_REMOVEPARAM)
&& (cpt_mask_positive & NetworkFilterMask::FROM_ALL_TYPES).is_empty()
{
mask |= NetworkFilterMask::FROM_ALL_TYPES;
}

// uBlock Origin would block main document `https://example.com` requests with all of the
// following filters:
// - ||example.com
Expand All @@ -854,7 +857,8 @@ impl NetworkFilter {
(cpt_mask_negative & NetworkFilterMask::FROM_ALL_TYPES).is_empty() &&
mask.contains(NetworkFilterMask::IS_HOSTNAME_ANCHOR) &&
mask.contains(NetworkFilterMask::IS_RIGHT_ANCHOR) &&
!end_url_anchor {
!end_url_anchor &&
!mask.contains(NetworkFilterMask::IS_REMOVEPARAM) {
mask |= NetworkFilterMask::FROM_ALL_TYPES;
}
// Finally, apply any explicitly negated request types
Expand Down

0 comments on commit da3d470

Please sign in to comment.