From 964fcbf6d911c39e1ab91572e45ce0a7b63b297b Mon Sep 17 00:00:00 2001 From: Cosmin Sabou Date: Wed, 13 Sep 2023 23:05:19 +0300 Subject: [PATCH] Backed out changeset 8771fccb94fa (bug 1853013) for causing security related mochitest failures. CLOSED TREE --- .../{AboutCompat.sys.mjs => AboutCompat.jsm} | 11 ++- .../extensions/webcompat/about-compat/aboutPage.js | 6 +- .../about-compat/aboutPageProcessScript.js | 4 +- browser/extensions/webcompat/components.conf | 2 +- browser/extensions/webcompat/data/shims.js | 1 + browser/extensions/webcompat/data/ua_overrides.js | 2 +- browser/extensions/webcompat/lib/injections.js | 92 +++------------------- browser/extensions/webcompat/lib/shims.js | 50 ++---------- browser/extensions/webcompat/manifest.json | 3 +- browser/extensions/webcompat/moz.build | 2 +- 10 files changed, 40 insertions(+), 133 deletions(-) rename browser/extensions/webcompat/about-compat/{AboutCompat.sys.mjs => AboutCompat.jsm} (85%) diff --git a/browser/extensions/webcompat/about-compat/AboutCompat.sys.mjs b/browser/extensions/webcompat/about-compat/AboutCompat.jsm similarity index 85% rename from browser/extensions/webcompat/about-compat/AboutCompat.sys.mjs rename to browser/extensions/webcompat/about-compat/AboutCompat.jsm index bedcdd668d51..7c844726f8b7 100644 --- a/browser/extensions/webcompat/about-compat/AboutCompat.sys.mjs +++ b/browser/extensions/webcompat/about-compat/AboutCompat.jsm @@ -2,14 +2,21 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +"use strict"; + +var EXPORTED_SYMBOLS = ["AboutCompat"]; + +const Services = + globalThis.Services || + ChromeUtils.import("resource://gre/modules/Services.jsm").Services; + const addonID = "webcompat@mozilla.org"; const addonPageRelativeURL = "/about-compat/aboutCompat.html"; -export function AboutCompat() { +function AboutCompat() { this.chromeURL = WebExtensionPolicy.getByID(addonID).getURL(addonPageRelativeURL); } - AboutCompat.prototype = { QueryInterface: ChromeUtils.generateQI(["nsIAboutModule"]), getURIFlags() { diff --git a/browser/extensions/webcompat/about-compat/aboutPage.js b/browser/extensions/webcompat/about-compat/aboutPage.js index 0df6d1e0f295..0f2e7c4ad465 100644 --- a/browser/extensions/webcompat/about-compat/aboutPage.js +++ b/browser/extensions/webcompat/about-compat/aboutPage.js @@ -4,7 +4,11 @@ "use strict"; -/* global ExtensionAPI, XPCOMUtils, Services */ +/* global ExtensionAPI, XPCOMUtils */ + +const Services = + globalThis.Services || + ChromeUtils.import("resource://gre/modules/Services.jsm").Services; XPCOMUtils.defineLazyServiceGetter( this, diff --git a/browser/extensions/webcompat/about-compat/aboutPageProcessScript.js b/browser/extensions/webcompat/about-compat/aboutPageProcessScript.js index 24d44e53fa0d..13cb4fd0bf53 100644 --- a/browser/extensions/webcompat/about-compat/aboutPageProcessScript.js +++ b/browser/extensions/webcompat/about-compat/aboutPageProcessScript.js @@ -19,8 +19,8 @@ if (!Cm.isCIDRegistered(classID)) { ); const factory = ComponentUtils.generateSingletonFactory(function () { - const { AboutCompat } = ChromeUtils.importESModule( - "resource://webcompat/AboutCompat.sys.mjs" + const { AboutCompat } = ChromeUtils.import( + "resource://webcompat/AboutCompat.jsm" ); return new AboutCompat(); }); diff --git a/browser/extensions/webcompat/components.conf b/browser/extensions/webcompat/components.conf index 0ad4e5fef1ba..ca5a6c3dbdfd 100644 --- a/browser/extensions/webcompat/components.conf +++ b/browser/extensions/webcompat/components.conf @@ -11,7 +11,7 @@ Classes = [ { 'cid': '{97bf9550-2a7b-11e9-b56e-0800200c9a66}', 'contract_ids': ['@mozilla.org/network/protocol/about;1?what=compat'], - 'esModule': 'resource://webcompat/AboutCompat.sys.mjs', + 'jsm': 'resource://webcompat/AboutCompat.jsm', 'constructor': 'AboutCompat', }, ] diff --git a/browser/extensions/webcompat/data/shims.js b/browser/extensions/webcompat/data/shims.js index 76cb780bcc6a..707cefb7adbf 100644 --- a/browser/extensions/webcompat/data/shims.js +++ b/browser/extensions/webcompat/data/shims.js @@ -589,6 +589,7 @@ const AVAILABLE_SHIMS = [ ], contentScripts: [ { + cookieStoreId: "firefox-private", js: "firebase.js", runAt: "document_start", matches: [ diff --git a/browser/extensions/webcompat/data/ua_overrides.js b/browser/extensions/webcompat/data/ua_overrides.js index 452fd4aa0d6a..cb3115b16e0e 100644 --- a/browser/extensions/webcompat/data/ua_overrides.js +++ b/browser/extensions/webcompat/data/ua_overrides.js @@ -486,7 +486,7 @@ const AVAILABLE_UA_OVERRIDES = [ domain: "granbluefantasy.jp", bug: "1722954", config: { - matches: ["*://*.granbluefantasy.jp/*", "*://*.gbf.game.mbga.jp/*"], + matches: ["*://*.granbluefantasy.jp/*"], uaTransformer: originalUA => { return originalUA + " iPhone OS 12_0 like Mac OS X"; }, diff --git a/browser/extensions/webcompat/lib/injections.js b/browser/extensions/webcompat/lib/injections.js index bd53bcb5cef6..8760f551c7d8 100644 --- a/browser/extensions/webcompat/lib/injections.js +++ b/browser/extensions/webcompat/lib/injections.js @@ -13,7 +13,7 @@ class Injections { this._injectionsEnabled = true; this._availableInjections = availableInjections; - this._activeInjections = new Set(); + this._activeInjections = new Map(); this._customFunctions = customFunctions; } @@ -48,33 +48,6 @@ class Injections { return this._injectionsEnabled; } - async getPromiseRegisteredScriptIds(scriptIds) { - let registeredScriptIds = []; - - // Try to avoid re-registering scripts already registered - // (e.g. if the webcompat background page is restarted - // after an extension process crash, after having registered - // the content scripts already once), but do not prevent - // to try registering them again if the getRegisteredContentScripts - // method returns an unexpected rejection. - try { - const registeredScripts = - await browser.scripting.getRegisteredContentScripts({ - // By default only look for script ids that belongs to Injections - // (and ignore the ones that may belong to Shims). - ids: scriptIds ?? this._availableInjections.map(inj => inj.id), - }); - registeredScriptIds = registeredScripts.map(script => script.id); - } catch (ex) { - console.error( - "Retrieve WebCompat GoFaster registered content scripts failed: ", - ex - ); - } - - return registeredScriptIds; - } - async registerContentScripts() { const platformInfo = await browser.runtime.getPlatformInfo(); const platformMatches = [ @@ -82,13 +55,10 @@ class Injections { platformInfo.os, platformInfo.os == "android" ? "android" : "desktop", ]; - - let registeredScriptIds = await this.getPromiseRegisteredScriptIds(); - for (const injection of this._availableInjections) { if (platformMatches.includes(injection.platform)) { injection.availableOnPlatform = true; - await this.enableInjection(injection, registeredScriptIds); + await this.enableInjection(injection); } } @@ -100,39 +70,17 @@ class Injections { }); } - buildContentScriptRegistrations(contentScripts) { + assignContentScriptDefaults(contentScripts) { let finalConfig = Object.assign({}, contentScripts); - // Don't persist the content scripts across browser restarts - // (at least not yet, we would need to apply some more changes - // to adjust webcompat for accounting for the scripts to be - // already registered). - // - // NOTE: scripting API has been introduced in Gecko 102, - // prior to Gecko 105 persistAcrossSessions option was required - // and only accepted false persistAcrossSessions, after Gecko 105 - // is optional and defaults to true. - finalConfig.persistAcrossSessions = false; - if (!finalConfig.runAt) { finalConfig.runAt = "document_start"; } - // Convert js/css from contentScripts.register API method - // format to scripting.registerContentScripts API method - // format. - if (Array.isArray(finalConfig.js)) { - finalConfig.js = finalConfig.js.map(e => e.file); - } - - if (Array.isArray(finalConfig.css)) { - finalConfig.css = finalConfig.css.map(e => e.file); - } - return finalConfig; } - async enableInjection(injection, registeredScriptIds) { + async enableInjection(injection) { if (injection.active) { return undefined; } @@ -141,7 +89,7 @@ class Injections { return this.enableCustomInjection(injection); } - return this.enableContentScripts(injection, registeredScriptIds); + return this.enableContentScripts(injection); } enableCustomInjection(injection) { @@ -155,31 +103,16 @@ class Injections { } } - async enableContentScripts(injection, registeredScriptIds) { - let injectProps; + async enableContentScripts(injection) { try { - const { id } = injection; - // enableContentScripts receives a registeredScriptIds already - // pre-computed once from registerContentScripts to register all - // the injection, whereas it does not expect to receive one when - // it is called from the AboutCompatBroker to re-enable one specific - // injection. - let activeScriptIds = Array.isArray(registeredScriptIds) - ? registeredScriptIds - : await this.getPromiseRegisteredScriptIds([id]); - injectProps = this.buildContentScriptRegistrations( - injection.contentScripts + const handle = await browser.contentScripts.register( + this.assignContentScriptDefaults(injection.contentScripts) ); - injectProps.id = id; - if (!activeScriptIds.includes(id)) { - await browser.scripting.registerContentScripts([injectProps]); - } - this._activeInjections.add(id); + this._activeInjections.set(injection, handle); injection.active = true; } catch (ex) { console.error( "Registering WebCompat GoFaster content scripts failed: ", - { injection, injectProps }, ex ); } @@ -222,10 +155,9 @@ class Injections { } async disableContentScripts(injection) { - if (this._activeInjections.has(injection.id)) { - await browser.scripting.unregisterContentScripts({ ids: [injection.id] }); - this._activeInjections.delete(injection); - } + const contentScript = this._activeInjections.get(injection); + await contentScript.unregister(); + this._activeInjections.delete(injection); injection.active = false; } } diff --git a/browser/extensions/webcompat/lib/shims.js b/browser/extensions/webcompat/lib/shims.js index 3822d734daa9..ee33627c5783 100644 --- a/browser/extensions/webcompat/lib/shims.js +++ b/browser/extensions/webcompat/lib/shims.js @@ -80,10 +80,10 @@ class Shim { this.contentScripts = contentScripts || []; for (const script of this.contentScripts) { if (typeof script.css === "string") { - script.css = [`/shims/${script.css}`]; + script.css = [{ file: `/shims/${script.css}` }]; } if (typeof script.js === "string") { - script.js = [`/shims/${script.js}`]; + script.js = [{ file: `/shims/${script.js}` }]; } } @@ -244,47 +244,10 @@ class Shim { !this._contentScriptRegistrations.length ) { const matches = []; - let idx = 0; for (const options of this.contentScripts) { matches.push(options.matches); - // Some shims includes more than one script (e.g. Blogger one contains - // a content script to be run on document_start and one to be run - // on document_end. - options.id = `shim-${this.id}-${idx++}`; - options.persistAcrossSessions = false; - // Having to call getRegisteredContentScripts each time we are going to - // register a Shim content script is suboptimal, but avoiding that - // may require a bit more changes (e.g. rework both Injections, Shim and Shims - // classes to more easily register all content scripts with a single - // call to the scripting API methods when the background script page is loading - // and one per injection or shim being enabled from the AboutCompatBroker). - // In the short term we call getRegisteredContentScripts and restrict it to - // the script id we are about to register. - let isAlreadyRegistered = false; - try { - const registeredScripts = - await browser.scripting.getRegisteredContentScripts({ - ids: [options.id], - }); - isAlreadyRegistered = !!registeredScripts.length; - } catch (ex) { - console.error( - "Retrieve WebCompat GoFaster registered content scripts failed: ", - ex - ); - } - try { - if (!isAlreadyRegistered) { - await browser.scripting.registerContentScripts([options]); - } - this._contentScriptRegistrations.push(options.id); - } catch (ex) { - console.error( - "Registering WebCompat Shim content scripts failed: ", - options, - ex - ); - } + const reg = await browser.contentScripts.register(options); + this._contentScriptRegistrations.push(reg); } const urls = Array.from(new Set(matches.flat())); debug("Enabling content scripts for these URLs:", urls); @@ -292,8 +255,9 @@ class Shim { } async _unregisterContentScripts() { - const ids = this._contentScriptRegistrations; - await browser.scripting.unregisterContentScripts({ ids }); + for (const registration of this._contentScriptRegistrations) { + registration.unregister(); + } this._contentScriptRegistrations = []; } diff --git a/browser/extensions/webcompat/manifest.json b/browser/extensions/webcompat/manifest.json index fc5d0846c7b5..30a1fe7ebdba 100644 --- a/browser/extensions/webcompat/manifest.json +++ b/browser/extensions/webcompat/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Web Compatibility Interventions", "description": "Urgent post-release fixes for web compatibility.", - "version": "118.2.0", + "version": "118.1.0", "browser_specific_settings": { "gecko": { "id": "webcompat@mozilla.org", @@ -65,7 +65,6 @@ "permissions": [ "mozillaAddons", - "scripting", "tabs", "webNavigation", "webRequest", diff --git a/browser/extensions/webcompat/moz.build b/browser/extensions/webcompat/moz.build index 8a5b0b38e462..5b950dd1b0b9 100644 --- a/browser/extensions/webcompat/moz.build +++ b/browser/extensions/webcompat/moz.build @@ -16,7 +16,7 @@ FINAL_TARGET_FILES.features["webcompat@mozilla.org"]["about-compat"] += [ "about-compat/aboutCompat.css", "about-compat/aboutCompat.html", "about-compat/aboutCompat.js", - "about-compat/AboutCompat.sys.mjs", + "about-compat/AboutCompat.jsm", "about-compat/aboutPage.js", "about-compat/aboutPage.json", "about-compat/aboutPageProcessScript.js", -- 2.11.4.GIT