Skip to content

Commit 6408555

Browse files
committed
support #@#+js() for blanket scriptlet exceptions
1 parent 9c11e76 commit 6408555

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/cosmetic_filter_cache.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ impl CosmeticFilterCache {
248248
let mut script_injections = HashMap::<&str, PermissionMask>::new();
249249
let mut exceptions = HashSet::new();
250250

251+
let mut except_all_scripts = false;
252+
251253
let hashes: Vec<&Hash> = request_entities.iter().chain(request_hostnames.iter()).collect();
252254

253255
fn populate_set(hash: &Hash, source_bin: &HostnameFilterBin<String>, dest_set: &mut HashSet<String>) {
@@ -308,9 +310,16 @@ impl CosmeticFilterCache {
308310
prune_set(hash, &self.specific_rules.unremove, &mut remove_selectors);
309311
// same logic but not using prune_set since strings are unowned, (see above)
310312
if let Some(s) = self.specific_rules.uninject_script.get(hash) {
311-
s.iter().for_each(|s| {
313+
for s in s {
314+
if s.is_empty() {
315+
except_all_scripts = true;
316+
script_injections.clear();
317+
}
318+
if except_all_scripts {
319+
continue;
320+
}
312321
script_injections.remove(s.as_str());
313-
});
322+
}
314323
}
315324

316325
prune_map(hash, &self.specific_rules.unstyle, &mut style_selectors);
@@ -657,6 +666,7 @@ mod cosmetic_cache_tests {
657666
"cosmetic.net##+js(nowebrtc.js)",
658667
"g.cosmetic.net##+js(window.open-defuser.js)",
659668
"c.g.cosmetic.net#@#+js(nowebrtc.js)",
669+
"d.g.cosmetic.net#@#+js()",
660670
]);
661671
let resources = ResourceStorage::from_resources([
662672
Resource {
@@ -698,6 +708,10 @@ mod cosmetic_cache_tests {
698708
let out = cfcache.hostname_cosmetic_resources(&resources, "c.g.cosmetic.net", false);
699709
expected.injected_script = "try {\nwindow.open-defuser.js\n} catch ( e ) { }\n".to_owned();
700710
assert_eq!(out, expected);
711+
712+
let out = cfcache.hostname_cosmetic_resources(&resources, "d.g.cosmetic.net", false);
713+
expected.injected_script = "".to_owned();
714+
assert_eq!(out, expected);
701715
}
702716

703717
#[test]

0 commit comments

Comments
 (0)