tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

FxAccountsTelemetry.sys.mjs (7333B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // FxA Telemetry support. For hysterical raisins, the actual implementation
      6 // is inside "sync". We should move the core implementation somewhere that's
      7 // sanely shared (eg, services-common?), but let's wait and see where we end up
      8 // first...
      9 
     10 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
     11 
     12 const lazy = {};
     13 
     14 ChromeUtils.defineESModuleGetters(lazy, {
     15  CryptoUtils: "moz-src:///services/crypto/modules/utils.sys.mjs",
     16 
     17  // We use this observers module because we leverage its support for richer
     18  // "subject" data.
     19  Observers: "resource://services-common/observers.sys.mjs",
     20 });
     21 
     22 import {
     23  PREF_ACCOUNT_ROOT,
     24  log,
     25 } from "resource://gre/modules/FxAccountsCommon.sys.mjs";
     26 
     27 const PREF_SANITIZED_UID = PREF_ACCOUNT_ROOT + "telemetry.sanitized_uid";
     28 XPCOMUtils.defineLazyPreferenceGetter(
     29  lazy,
     30  "pref_sanitizedUid",
     31  PREF_SANITIZED_UID,
     32  ""
     33 );
     34 
     35 export class FxAccountsTelemetry {
     36  constructor(fxai) {
     37    this._fxai = fxai;
     38  }
     39 
     40  // Records an event *in the Fxa/Sync ping*.
     41  recordEvent(object, method, value, extra = undefined) {
     42    // We need to ensure the telemetry module is loaded.
     43    ChromeUtils.importESModule("resource://services-sync/telemetry.sys.mjs");
     44    // Now it will be listening for the notifications...
     45    lazy.Observers.notify("fxa:telemetry:event", {
     46      object,
     47      method,
     48      value,
     49      extra,
     50    });
     51  }
     52 
     53  generateUUID() {
     54    return Services.uuid.generateUUID().toString().slice(1, -1);
     55  }
     56 
     57  // A flow ID can be anything that's "probably" unique, so for now use a UUID.
     58  generateFlowID() {
     59    return this.generateUUID();
     60  }
     61 
     62  // FxA- and Sync-related metrics are submitted in a special-purpose "sync ping". This ping
     63  // identifies the user by a version of their FxA uid that is HMAC-ed with a server-side secret
     64  // key, in an attempt to provide a bit of anonymity.
     65 
     66  // Secret back-channel by which tokenserver client code can set the hashed UID.
     67  // This value conceptually belongs to FxA, but we currently get it from tokenserver,
     68  // so there's some light hackery to put it in the right place.
     69  _setHashedUID(hashedUID) {
     70    if (!hashedUID) {
     71      Services.prefs.clearUserPref(PREF_SANITIZED_UID);
     72    } else {
     73      Services.prefs.setStringPref(PREF_SANITIZED_UID, hashedUID);
     74    }
     75  }
     76 
     77  getSanitizedUID() {
     78    // Sadly, we can only currently obtain this value if the user has enabled sync.
     79    return lazy.pref_sanitizedUid || null;
     80  }
     81 
     82  // Sanitize the ID of a device into something suitable for including in the
     83  // ping. Returns null if no transformation is possible.
     84  sanitizeDeviceId(deviceId) {
     85    const uid = this.getSanitizedUID();
     86    if (!uid) {
     87      // Sadly, we can only currently get this if the user has enabled sync.
     88      return null;
     89    }
     90    // Combine the raw device id with the sanitized uid to create a stable
     91    // unique identifier that can't be mapped back to the user's FxA
     92    // identity without knowing the metrics HMAC key.
     93    // The result is 64 bytes long, which in retrospect is probably excessive,
     94    // but it's already shipping...
     95    return lazy.CryptoUtils.sha256(deviceId + uid);
     96  }
     97 
     98  // Record when the user opens the choose what to sync menu.
     99  async recordOpenCWTSMenu(why = null) {
    100    try {
    101      let extra = {};
    102      if (why) {
    103        extra.why = why;
    104      }
    105      Glean.syncSettings.openChooseWhatToSyncMenu.record(extra);
    106    } catch (ex) {
    107      log.error("Failed to record manage sync settings telemetry", ex);
    108      console.error("Failed to record manage sync settings telemetry", ex);
    109    }
    110  }
    111 
    112  // Record when the user saves sync settings.
    113  async recordSaveSyncSettings(settings = null) {
    114    try {
    115      let extra = {};
    116 
    117      if (settings && (settings.enabledEngines || settings.disabledEngines)) {
    118        extra.enabled_engines = settings.enabledEngines.toString();
    119        extra.disabled_engines = settings.disabledEngines.toString();
    120 
    121        Glean.syncSettings.save.record(extra);
    122      } else {
    123        Glean.syncSettings.save.record();
    124      }
    125    } catch (ex) {
    126      log.error("Failed to record save sync settings telemetry", ex);
    127      console.error("Failed to record save sync settings telemetry", ex);
    128    }
    129  }
    130 
    131  // Record the connection of FxA or one of its services.
    132  // Note that you must call this before performing the actual connection
    133  // or we may record incorrect data - for example, we will not be able to
    134  // determine whether FxA itself was connected before this call.
    135  //
    136  // Currently sends an event in the main telemetry event ping rather than the
    137  // FxA/Sync ping (although this might change in the future)
    138  //
    139  // @param services - An array of service names which should be recorded. FxA
    140  //  itself is not counted as a "service" - ie, an empty array should be passed
    141  //  if the account is connected without anything else .
    142  //
    143  // @param how - How the connection was done.
    144  async recordConnection(services, how = null) {
    145    try {
    146      let extra = {};
    147      if (how) {
    148        extra.value = how;
    149      }
    150      // Record that fxa was connected if it isn't currently - it will be soon.
    151      if (!(await this._fxai.getUserAccountData())) {
    152        extra.fxa = "true";
    153      }
    154      // Events.yaml only declares "sync" as a valid service.
    155      if (services.includes("sync")) {
    156        extra.sync = "true";
    157      }
    158      Glean.fxa.connectAccount.record(extra);
    159    } catch (ex) {
    160      log.error("Failed to record connection telemetry", ex);
    161      console.error("Failed to record connection telemetry", ex);
    162    }
    163  }
    164 
    165  // Record the disconnection of FxA or one of its services.
    166  // Note that you must call this before performing the actual disconnection
    167  // or we may record incomplete data - for example, if this is called after
    168  // disconnection, we've almost certainly lost the ability to record what
    169  // services were enabled prior to disconnection.
    170  //
    171  // Currently sends an event in the main telemetry event ping rather than the
    172  // FxA/Sync ping (although this might change in the future)
    173  //
    174  // @param service - the service being disconnected. If null, the account
    175  // itself is being disconnected, so all connected services are too.
    176  //
    177  // @param how - how the disconnection was done.
    178  async recordDisconnection(service = null, how = null) {
    179    try {
    180      let extra = {};
    181      if (how) {
    182        extra.value = how;
    183      }
    184      if (!service) {
    185        extra.fxa = "true";
    186        // We need a way to enumerate all services - but for now we just hard-code
    187        // all possibilities here.
    188        if (Services.prefs.prefHasUserValue("services.sync.username")) {
    189          extra.sync = "true";
    190        }
    191      } else if (service == "sync") {
    192        extra[service] = "true";
    193      } else {
    194        // Events.yaml only declares "sync" as a valid service.
    195        log.warn(
    196          `recordDisconnection has invalid value for service: ${service}`
    197        );
    198      }
    199      Glean.fxa.disconnectAccount.record(extra);
    200    } catch (ex) {
    201      log.error("Failed to record disconnection telemetry", ex);
    202      console.error("Failed to record disconnection telemetry", ex);
    203    }
    204  }
    205 }