Skip to content

Commit

Permalink
support #@#+js() for blanket scriptlet exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Oct 18, 2023
1 parent 9c11e76 commit 6408555
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/cosmetic_filter_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ impl CosmeticFilterCache {
let mut script_injections = HashMap::<&str, PermissionMask>::new();
let mut exceptions = HashSet::new();

let mut except_all_scripts = false;

let hashes: Vec<&Hash> = request_entities.iter().chain(request_hostnames.iter()).collect();

fn populate_set(hash: &Hash, source_bin: &HostnameFilterBin<String>, dest_set: &mut HashSet<String>) {
Expand Down Expand Up @@ -308,9 +310,16 @@ impl CosmeticFilterCache {
prune_set(hash, &self.specific_rules.unremove, &mut remove_selectors);
// same logic but not using prune_set since strings are unowned, (see above)
if let Some(s) = self.specific_rules.uninject_script.get(hash) {
s.iter().for_each(|s| {
for s in s {
if s.is_empty() {
except_all_scripts = true;
script_injections.clear();
}
if except_all_scripts {
continue;
}
script_injections.remove(s.as_str());
});
}
}

prune_map(hash, &self.specific_rules.unstyle, &mut style_selectors);
Expand Down Expand Up @@ -657,6 +666,7 @@ mod cosmetic_cache_tests {
"cosmetic.net##+js(nowebrtc.js)",
"g.cosmetic.net##+js(window.open-defuser.js)",
"c.g.cosmetic.net#@#+js(nowebrtc.js)",
"d.g.cosmetic.net#@#+js()",
]);
let resources = ResourceStorage::from_resources([
Resource {
Expand Down Expand Up @@ -698,6 +708,10 @@ mod cosmetic_cache_tests {
let out = cfcache.hostname_cosmetic_resources(&resources, "c.g.cosmetic.net", false);
expected.injected_script = "try {\nwindow.open-defuser.js\n} catch ( e ) { }\n".to_owned();
assert_eq!(out, expected);

let out = cfcache.hostname_cosmetic_resources(&resources, "d.g.cosmetic.net", false);
expected.injected_script = "".to_owned();
assert_eq!(out, expected);
}

#[test]
Expand Down

0 comments on commit 6408555

Please sign in to comment.