From 6355a17187f70c288ff8354656835bdc0a1e13eb Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 20 Nov 2024 09:04:52 -0500 Subject: [PATCH] [mv3] Fix flaw breaking scriptlets injection in optimal/basic mode Not all matching scriptlets were injected on a given site in Optimal or Complete mode when default mode was set to Basic or less. A high profile manifestation of this bug was that Youtube ads were not being blocked when using Optimal on Youtube while default mode was Basic. --- platform/mv3/extension/js/scripting-manager.js | 4 ++-- platform/mv3/extension/js/utils.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/platform/mv3/extension/js/scripting-manager.js b/platform/mv3/extension/js/scripting-manager.js index 814343ebe..23a6444ea 100644 --- a/platform/mv3/extension/js/scripting-manager.js +++ b/platform/mv3/extension/js/scripting-manager.js @@ -450,8 +450,8 @@ function registerScriptlet(context, scriptletDetails) { targetHostnames = permissionGrantedHostnames; } else { targetHostnames = ut.intersectHostnameIters( - permissionGrantedHostnames, - scriptletHostnames + scriptletHostnames, + permissionGrantedHostnames ); } } diff --git a/platform/mv3/extension/js/utils.js b/platform/mv3/extension/js/utils.js index b32104f94..7bf6e1350 100644 --- a/platform/mv3/extension/js/utils.js +++ b/platform/mv3/extension/js/utils.js @@ -47,6 +47,13 @@ const isDescendantHostname = (hna, hnb) => { return hna.charCodeAt(hna.length - hnb.length - 1) === 0x2E /* '.' */; }; +/** + * Returns whether a hostname is part of a collection, or is descendant of an + * item in the collection. + * @param hna - the hostname representing the needle. + * @param iterb - an iterable representing the haystack of hostnames. + */ + const isDescendantHostnameOfIter = (hna, iterb) => { const setb = iterb instanceof Set ? iterb : new Set(iterb); if ( setb.has('all-urls') || setb.has('*') ) { return true; } @@ -60,6 +67,13 @@ const isDescendantHostnameOfIter = (hna, iterb) => { return false; }; +/** + * Returns all hostnames in the first collection which are equal or descendant + * of hostnames in the second collection. + * @param itera - an iterable which hostnames must be filtered out. + * @param iterb - an iterable which hostnames must be matched. + */ + const intersectHostnameIters = (itera, iterb) => { const setb = iterb instanceof Set ? iterb : new Set(iterb); if ( setb.has('all-urls') || setb.has('*') ) { return Array.from(itera); }