From e9508889a764dda8a2fb2f07180b6623ca4949cb Mon Sep 17 00:00:00 2001 From: hackademix Date: Tue, 29 Oct 2024 12:56:26 +0100 Subject: [PATCH] [tor-browser#43258] Remove content script crypto.subtle dependency. --- src/bg/LifeCycle.js | 46 ++++++++++++++++++++++++++++-------------- src/content/content.js | 11 +++------- src/manifest.json | 1 - 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/bg/LifeCycle.js b/src/bg/LifeCycle.js index d9eeb16..c0972d9 100644 --- a/src/bg/LifeCycle.js +++ b/src/bg/LifeCycle.js @@ -18,6 +18,9 @@ * this program. If not, see . */ +// depends on /nscl/common/sha256.js +// depends on /nscl/common/uuid.js + "use strict"; var LifeCycle = (() => { @@ -108,30 +111,45 @@ var LifeCycle = (() => { unrestrictedTabs: [...ns.unrestrictedTabs] })); - let attr; try { + // random attribute name for DOM storage + const attr = await sha256(data.concat(uuid())); + await new Promise((resolve, reject) => { - let l = async (tabId, changeInfo) => { - if (!!attr || tabId !== tab.id) return; - debug("Survival tab updating", changeInfo); - if (changeInfo.status !== "complete") return; + + let stored = false; + const storeInTab = async (tabId, tabInfo) => { + if (stored) { + browser.tabs.onUpdated.removeListener(storeInTab); + return; + } + if (tabId !== tab.id) { + return; + } + debug("Survival tab updating", tabInfo); + if (tabInfo.status !== "complete") { + return; + } try { - attr = await Messages.send("store", {url, data: toBase64(new Uint8Array(cypherText))}, {tabId, frameId: 0}); + stored = await Messages.send("store", { + url, + data: toBase64(new Uint8Array(cypherText)), + attr, + }, + {tabId, frameId: 0} + ); resolve(); - debug("Survival tab updated"); + debug(`Survival tab updated, stored: ${stored}`); } catch (e) { if (!Messages.isMissingEndpoint(e)) { error(e, "Survival tab failed"); reject(e); } // otherwise we keep waiting for further updates from the tab until content script is ready to answer - return false; }; - return true; }; - - l(tabId, tab).then(r => { - if (!r) browser.tabs.onUpdated.addListener(l); + storeInTab(tabId, tab).then(() => { + if (!stored) browser.tabs.onUpdated.addListener(storeInTab); }); }); @@ -218,8 +236,6 @@ var LifeCycle = (() => { return { async onInstalled(details) { - browser.runtime.onInstalled.removeListener(this.onInstalled); - if (!UA.isMozilla) { // Chromium does not inject content scripts at startup automatically for already loaded pages, // let's hack it manually. @@ -318,9 +334,9 @@ var LifeCycle = (() => { if (Ver.is(previousVersion, "<=", "11.4.1rc3")) { // show theme switcher on update unless user has already chosen between Vintage Blue and Modern Red (async () => { + await ns.initializing; let isVintage = await Themes.isVintage(); if (typeof isVintage === "boolean") return; - await ns.init; ns.openOptionsPage({tab: 2, focus: "#opt-vintageTheme", hilite: "#sect-themes"}); })(); } diff --git a/src/content/content.js b/src/content/content.js index 0d610be..5b0a6d9 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -90,19 +90,14 @@ Messages.addHandler({ return list; }, async store(event) { - if (document.URL !== event.url) return; - const {data} = event; - const attr = await sha256(data.concat(Math.random())); + if (document.URL !== event.url) return false; + const {data, attr} = event; document.documentElement.dataset[attr] = data; - return attr; + return true; }, retrieve(event) { if (document.URL !== event.url) return; let {attr, preserve} = event; - if (!attr) { - // legacy, < 11.0.39rc8 - return document.documentElement.lastChild.textContent; - } let data = document.documentElement.dataset[attr]; if (!preserve) delete document.documentElement.dataset[attr]; return data; diff --git a/src/manifest.json b/src/manifest.json index 6c655a1..6aff451 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -86,7 +86,6 @@ "all_frames": true, "js": [ "/nscl/lib/browser-polyfill.js", - "/nscl/common/sha256.js", "/nscl/common/UA.js", "/nscl/common/uuid.js", "/nscl/common/log.js",