This is an automated email from the git hooks/post-receive script.
pierov pushed a commit to branch tor-browser-91.10.0esr-11.5-1 in repository tor-browser.
The following commit(s) were added to refs/heads/tor-browser-91.10.0esr-11.5-1 by this push: new 9a5d862212abb fixup! Bug 40458: Implement .tor.onion aliases 9a5d862212abb is described below
commit 9a5d862212abbade74b97e4e3dbe87e09ed9ac67 Author: Pier Angelo Vendrame pierov@torproject.org AuthorDate: Thu Jun 23 17:10:07 2022 +0200
fixup! Bug 40458: Implement .tor.onion aliases
Bug 41036: Add a preference to disable Onion Aliases --- .../components/onionservices/OnionAliasStore.jsm | 92 ++++++++++++++++------ modules/libpref/init/StaticPrefList.yaml | 5 ++ netwerk/dns/OnionAliasService.cpp | 5 +- 3 files changed, 79 insertions(+), 23 deletions(-)
diff --git a/browser/components/onionservices/OnionAliasStore.jsm b/browser/components/onionservices/OnionAliasStore.jsm index 059bca8f5a2bf..d5849e4b94289 100644 --- a/browser/components/onionservices/OnionAliasStore.jsm +++ b/browser/components/onionservices/OnionAliasStore.jsm @@ -40,6 +40,8 @@ const SECURE_DROP = { currentTimestamp: 0, };
+const kPrefOnionAliasEnabled = "browser.urlbar.onionRewrites.enabled"; + // Logger adapted from CustomizableUI.jsm const kPrefOnionAliasDebug = "browser.onionalias.debug"; XPCOMUtils.defineLazyPreferenceGetter( @@ -299,29 +301,19 @@ class _OnionAliasStore {
async init() { await this._loadSettings(); - const dt = Date.now() - this._lastCheck; - let force = false; - for (const ch of this._channels.values()) { - if (ch.enabled && !ch.currentTimestamp) { - // Edited while being offline or some other error happened - force = true; - break; - } - } - if (dt > _OnionAliasStore.RULESET_CHECK_INTERVAL || force) { - log.debug( - `Mappings are stale (${dt}), or force check requested (${force}), checking them immediately` - ); - await this._periodicRulesetCheck(); - } else { - this._scheduleCheck(_OnionAliasStore.RULESET_CHECK_INTERVAL - dt); + if (this.enabled) { + await this._startUpdates(); } + Services.prefs.addObserver(kPrefOnionAliasEnabled, this); }
uninit() { this._clear(); - clearTimeout(this._rulesetTimeout); + if (this._rulesetTimeout) { + clearTimeout(this._rulesetTimeout); + } this._rulesetTimeout = null; + Services.prefs.removeObserver(kPrefOnionAliasEnabled, this); }
async getChannels() { @@ -362,13 +354,16 @@ class _OnionAliasStore { this._applyMappings(); this._saveSettings(); this._notifyChanges(); - if (enabled && !channel.currentTimestamp) { + if (this.enabled && enabled && !channel.currentTimestamp) { this.updateChannel(name); } } }
async updateChannel(name) { + if (!this.enabled) { + throw Error("Onion Aliases are disabled"); + } const channel = this._channels.get(name); if (channel === null) { throw Error("Channel not found"); @@ -469,6 +464,10 @@ class _OnionAliasStore { }
async _periodicRulesetCheck() { + if (!this.enabled) { + log.debug("Onion Aliases are disabled, not updating rulesets."); + return; + } log.debug("Begin scheduled ruleset update"); this._lastCheck = Date.now(); let anyUpdated = false; @@ -495,12 +494,46 @@ class _OnionAliasStore { this._scheduleCheck(_OnionAliasStore.RULESET_CHECK_INTERVAL); }
+ async _startUpdates() { + // This is a "private" function, so we expect the callers to verify wheter + // onion aliases are enabled. + // Callees will also do, so we avoid an additional check here. + const dt = Date.now() - this._lastCheck; + let force = false; + for (const ch of this._channels.values()) { + if (ch.enabled && !ch.currentTimestamp) { + // Edited while being offline or some other error happened + force = true; + break; + } + } + if (dt > _OnionAliasStore.RULESET_CHECK_INTERVAL || force) { + log.debug( + `Mappings are stale (${dt}), or force check requested (${force}), checking them immediately` + ); + await this._periodicRulesetCheck(); + } else { + this._scheduleCheck(_OnionAliasStore.RULESET_CHECK_INTERVAL - dt); + } + } + _scheduleCheck(dt) { + if (this._rulesetTimeout) { + log.warn("The previous update timeout was not null"); + clearTimeout(this._rulesetTimeout); + } + if (!this.enabled) { + log.warn( + "Ignoring the scheduling of a new check because the Onion Alias feature is currently disabled." + ); + this._rulesetTimeout = null; + return; + } log.debug(`Scheduling ruleset update in ${dt}`); - this._rulesetTimeout = setTimeout( - this._periodicRulesetCheck.bind(this), - dt - ); + this._rulesetTimeout = setTimeout(() => { + this._rulesetTimeout = null; + this._periodicRulesetCheck(); + }, dt); }
_notifyChanges() { @@ -509,6 +542,21 @@ class _OnionAliasStore { OnionAliasStoreTopics.ChannelsChanged ); } + + get enabled() { + return Services.prefs.getBoolPref(kPrefOnionAliasEnabled, true); + } + + observe(aSubject, aTopic, aData) { + if (aTopic === "nsPref:changed") { + if (this.enabled) { + this._startUpdates(); + } else if (this._rulesetTimeout) { + clearTimeout(this._rulesetTimeout); + this._rulesetTimeout = null; + } + } + } }
const OnionAliasStore = new _OnionAliasStore(); diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index b3e93c5443969..243585deecbf7 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -1338,6 +1338,11 @@ value: true mirror: always
+- name: browser.urlbar.onionRewrites.enabled + type: RelaxedAtomicBool + value: true + mirror: always + - name: browser.viewport.desktopWidth type: RelaxedAtomicInt32 value: 980 diff --git a/netwerk/dns/OnionAliasService.cpp b/netwerk/dns/OnionAliasService.cpp index 5140f4fe0d833..a23bf93cee8b0 100644 --- a/netwerk/dns/OnionAliasService.cpp +++ b/netwerk/dns/OnionAliasService.cpp @@ -1,5 +1,7 @@ #include "torproject/OnionAliasService.h"
+#include "mozilla/StaticPrefs_browser.h" + #include "nsUnicharUtils.h"
/** @@ -74,7 +76,8 @@ NS_IMETHODIMP OnionAliasService::GetOnionAlias(const nsACString& aShortHostname, nsACString& aLongHostname) { aLongHostname = aShortHostname; - if (StringEndsWith(aShortHostname, ".tor.onion"_ns)) { + if (mozilla::StaticPrefs::browser_urlbar_onionRewrites_enabled() && + StringEndsWith(aShortHostname, ".tor.onion"_ns)) { nsAutoCString* alias = nullptr; // We want to keep the string stored in the map alive at least until we // finish to copy it to the output parameter.
tbb-commits@lists.torproject.org