Verified Commit 47c03bfc authored by Pier Angelo Vendrame's avatar Pier Angelo Vendrame 🎃
Browse files

fixup! Bug 3455: Add DomainIsolator, for isolating circuit by domain.

Manage NEWNYM here.
parent 9018b2cd
Loading
Loading
Loading
Loading
+23 −1
Original line number Diff line number Diff line
@@ -18,6 +18,12 @@ XPCOMUtils.defineLazyServiceGetters(this, {
  ],
});

ChromeUtils.defineModuleGetter(
  this,
  "TorProtocolService",
  "resource://gre/modules/TorProtocolService.jsm"
);

const logger = new ConsoleAPI({
  prefix: "TorDomainIsolator",
  maxLogLevel: "warn",
@@ -31,6 +37,9 @@ const CATCHALL_DOMAIN = "--unknown--";
// disabled.
const NON_TOR_PROXY_PREF = "extensions.torbutton.use_nontor_proxy";

// The topic of new identity, to observe to cleanup all the nonces.
const NEW_IDENTITY_TOPIC = "new-identity-requested";

class TorDomainIsolatorImpl {
  // A mutable map that records what nonce we are using for each domain.
  #noncesForDomains = new Map();
@@ -58,6 +67,7 @@ class TorDomainIsolatorImpl {
    this.#setupProxyFilter();

    Services.prefs.addObserver(NON_TOR_PROXY_PREF, this);
    Services.obs.addObserver(this, NEW_IDENTITY_TOPIC);
  }

  /**
@@ -65,6 +75,7 @@ class TorDomainIsolatorImpl {
   */
  uninit() {
    Services.prefs.removeObserver(NON_TOR_PROXY_PREF, this);
    Services.obs.removeObserver(this, NEW_IDENTITY_TOPIC);
  }

  enable() {
@@ -152,13 +163,24 @@ class TorDomainIsolatorImpl {
    this.#catchallDirtySince = 0;
  }

  observe(subject, topic, data) {
  async observe(subject, topic, data) {
    if (topic === "nsPref:changed" && data === NON_TOR_PROXY_PREF) {
      if (Services.prefs.getBoolPref(NON_TOR_PROXY_PREF)) {
        this.disable();
      } else {
        this.enable();
      }
    } else if (topic === NEW_IDENTITY_TOPIC) {
      logger.info(
        "New identity has been requested, clearing isolation tokens."
      );
      this.clearIsolation();
      try {
        await TorProtocolService.newnym();
      } catch (e) {
        logger.error("Could not send the newnym command", e);
        // TODO: What UX to use here? See tor-browser#41708
      }
    }
  }