From 435fd5953622c83a66b5a186d86b52450835fc62 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 02:57:28 +0200 Subject: [PATCH 1/7] Fix invalid ruleset scope for custom rulesets --- chromium/background-scripts/rules.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index 4b4cc5805bfd..7028214f291a 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -16,7 +16,11 @@ const trivial_cookie_rule_c = /.+/; /* A map of all scope RegExp objects */ const scopes = new Map(); -/* Returns the scope object from the map for the given scope string */ +/** + * Returns the scope object from the map for the given scope string + * @param {string} scope + * @returns {RegExp} + */ function getScope(scope) { if (!scopes.has(scope)) { scopes.set(scope, new RegExp(scope)); @@ -398,7 +402,7 @@ RuleSets.prototype = { loadStoredUserRules: function() { return this.getStoredUserRules() .then(userRules => { - this.addFromJson(userRules, getScope()); + this.addFromJson(userRules, ''); util.log(util.INFO, `loaded ${userRules.length} stored user rules`); }); }, @@ -409,7 +413,7 @@ RuleSets.prototype = { * @param cb: Callback to call after success/fail * */ addNewRuleAndStore: async function(params) { - if (this.addUserRule(params, getScope())) { + if (this.addUserRule(params, '')) { // If we successfully added the user rule, save it in the storage // api so it's automatically applied when the extension is // reloaded. From 6b6b2ada5252acafcb14d17940eb8c366683ad29 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 03:41:19 +0200 Subject: [PATCH 2/7] Update rules.js --- chromium/background-scripts/rules.js | 40 +++++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index 7028214f291a..2d1d7653f3a8 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -17,8 +17,8 @@ const trivial_cookie_rule_c = /.+/; const scopes = new Map(); /** - * Returns the scope object from the map for the given scope string - * @param {string} scope + * Compile literal ruleset scope into RegExp + * @param {string} scope literal ruleset scope * @returns {RegExp} */ function getScope(scope) { @@ -79,9 +79,10 @@ function CookieRule(host, cookiename) { /** *A collection of rules - * @param set_name The name of this set - * @param default_state activity state - * @param note Note will be displayed in popup + * @param {string} set_name The name of this set + * @param {boolean} default_state activity state + * @param {RegExp} scope compiled RegExp ruleset scope + * @param {string} note Note will be displayed in popup * @constructor */ function RuleSet(set_name, default_state, scope, note) { @@ -180,7 +181,6 @@ RuleSet.prototype = { /** * Initialize Rule Sets - * @param ruleActiveStates default state for rules * @constructor */ function RuleSets() { @@ -193,7 +193,10 @@ function RuleSets() { // A cache for cookie hostnames. this.cookieHostCache = new Map(); - // A hash of rule name -> active status (true/false). + /** + * A hash of rule name -> active status (true/false). + * @type {Object} + */ this.ruleActiveStates = {}; // The key to retrieve user rules from the storage api @@ -224,6 +227,8 @@ RuleSets.prototype = { /** * Convert XML to JS and load rulesets + * @param {Document} ruleXml + * @param {string} scope */ addFromXml: function(ruleXml, scope) { const rulesets_xml = ruleXml.getElementsByTagName("ruleset"); @@ -236,19 +241,22 @@ RuleSets.prototype = { this.addFromJson(rulesets, scope); }, + /** + * + * @param {*} ruleJson + * @param {string} scope + */ addFromJson: function(ruleJson, scope) { - const scope_obj = getScope(scope); - if (this.wasm_rs) { this.wasm_rs.add_all_from_js_array( ruleJson, settings.enableMixedRulesets, this.ruleActiveStates, - scope); + getScope(scope)); } else { for (let ruleset of ruleJson) { try { - this.parseOneJsonRuleset(ruleset, scope_obj); + this.parseOneJsonRuleset(ruleset, scope); } catch(e) { util.log(util.WARN, 'Error processing ruleset:' + e); } @@ -256,6 +264,11 @@ RuleSets.prototype = { } }, + /** + * Parse one JSON format ruleset element + * @param {*} ruletag + * @param {string} scope + */ parseOneJsonRuleset: function(ruletag, scope) { var default_state = true; var note = ""; @@ -279,7 +292,7 @@ RuleSets.prototype = { note += "Platform(s): " + platform + "\n"; } - var rule_set = new RuleSet(ruletag["name"], default_state, scope, note.trim()); + var rule_set = new RuleSet(ruletag["name"], default_state, getScope(scope), note.trim()); // Read user prefs if (rule_set.name in this.ruleActiveStates) { @@ -324,6 +337,7 @@ RuleSets.prototype = { /** * Load a user rule * @param params + * @param {string} scope * @returns {boolean} */ addUserRule : function(params, scope) { @@ -470,7 +484,7 @@ RuleSets.prototype = { }, loadCustomRuleset: function(ruleset_string) { - this.addFromXml((new DOMParser()).parseFromString(ruleset_string, 'text/xml')); + this.addFromXml((new DOMParser()).parseFromString(ruleset_string, 'text/xml'), ''); }, setRuleActiveState: async function(ruleset_name, active) { From 7085c01d7d6ac56420727e2b1a907a1653b7f955 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 04:27:06 +0200 Subject: [PATCH 3/7] Update rules.js --- chromium/background-scripts/rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index 2d1d7653f3a8..c84f2018f8ab 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -17,7 +17,7 @@ const trivial_cookie_rule_c = /.+/; const scopes = new Map(); /** - * Compile literal ruleset scope into RegExp + * Returns the scope object from the map for the given scope string. * @param {string} scope literal ruleset scope * @returns {RegExp} */ From ad89943d7e984ae6045cd094b11fb73ae8115410 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 05:34:51 +0200 Subject: [PATCH 4/7] Update update.js add default scope --- chromium/background-scripts/update.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chromium/background-scripts/update.js b/chromium/background-scripts/update.js index e59793c2d3b5..3750e77841ac 100644 --- a/chromium/background-scripts/update.js +++ b/chromium/background-scripts/update.js @@ -215,7 +215,7 @@ async function applyStoredRulesets(rulesets_obj) { } if(!replaces) { - rulesets_obj.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json')); + rulesets_obj.addFromJson(util.loadExtensionFile('rules/default.rulesets', 'json'), ''); } } From 977d647184f13ffe88c5b57a3104200904bdab89 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 07:44:56 +0200 Subject: [PATCH 5/7] Update rules.js --- chromium/background-scripts/rules.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index c84f2018f8ab..b40601fa1865 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -242,9 +242,8 @@ RuleSets.prototype = { }, /** - * - * @param {*} ruleJson - * @param {string} scope + * @param {*} ruleJson + * @param {string} scope */ addFromJson: function(ruleJson, scope) { if (this.wasm_rs) { From f87434f126961c62116737936fee964e196db546 Mon Sep 17 00:00:00 2001 From: Chan Chak Shing Date: Tue, 13 Apr 2021 22:54:50 +0200 Subject: [PATCH 6/7] Pass string scope to the wasm rust bindings --- chromium/background-scripts/rules.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index b40601fa1865..f0f33ea19cec 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -251,7 +251,7 @@ RuleSets.prototype = { ruleJson, settings.enableMixedRulesets, this.ruleActiveStates, - getScope(scope)); + scope); } else { for (let ruleset of ruleJson) { try { From 1c20e82def114bcf966a0c9021532b26af30a291 Mon Sep 17 00:00:00 2001 From: cschanaj Date: Tue, 13 Apr 2021 23:05:10 +0200 Subject: [PATCH 7/7] Call getScope only in the Ruleset constructor --- chromium/background-scripts/rules.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chromium/background-scripts/rules.js b/chromium/background-scripts/rules.js index f0f33ea19cec..fb2d8cad2011 100644 --- a/chromium/background-scripts/rules.js +++ b/chromium/background-scripts/rules.js @@ -18,7 +18,7 @@ const scopes = new Map(); /** * Returns the scope object from the map for the given scope string. - * @param {string} scope literal ruleset scope + * @param {string} scope ruleset scope string * @returns {RegExp} */ function getScope(scope) { @@ -81,7 +81,7 @@ function CookieRule(host, cookiename) { *A collection of rules * @param {string} set_name The name of this set * @param {boolean} default_state activity state - * @param {RegExp} scope compiled RegExp ruleset scope + * @param {string} scope ruleset scope string * @param {string} note Note will be displayed in popup * @constructor */ @@ -92,7 +92,7 @@ function RuleSet(set_name, default_state, scope, note) { this.cookierules = null; this.active = default_state; this.default_state = default_state; - this.scope = scope; + this.scope = getScope(scope); this.note = note; } @@ -291,7 +291,7 @@ RuleSets.prototype = { note += "Platform(s): " + platform + "\n"; } - var rule_set = new RuleSet(ruletag["name"], default_state, getScope(scope), note.trim()); + var rule_set = new RuleSet(ruletag["name"], default_state, scope, note.trim()); // Read user prefs if (rule_set.name in this.ruleActiveStates) { @@ -582,7 +582,7 @@ RuleSets.prototype = { if (this.wasm_rs) { let pa = this.wasm_rs.potentially_applicable(host); results = new Set([...pa].map(ruleset => { - let rs = new RuleSet(ruleset.name, ruleset.default_state, getScope(ruleset.scope), ruleset.note); + let rs = new RuleSet(ruleset.name, ruleset.default_state, ruleset.scope, ruleset.note); if (ruleset.cookierules) { let cookierules = ruleset.cookierules.map(cookierule => {