summaryrefslogtreecommitdiffstats
path: root/toolkit/components/telemetry/app/ClientID.jsm
blob: 43d95ab9b8c9deac64b023b9d9d170ea58b58330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* This Source Code Form is subject to the terms of the Mozilla Public
 * 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 = ["ClientID"];

const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { Log } = ChromeUtils.import("resource://gre/modules/Log.jsm");
const { AppConstants } = ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);
if (AppConstants.MOZ_GLEAN) {
  Cu.importGlobalProperties(["Glean"]);
}

const LOGGER_NAME = "Toolkit.Telemetry";
const LOGGER_PREFIX = "ClientID::";
// Must match ID in TelemetryUtils
const CANARY_CLIENT_ID = "c0ffeec0-ffee-c0ff-eec0-ffeec0ffeec0";

ChromeUtils.defineModuleGetter(
  this,
  "CommonUtils",
  "resource://services-common/utils.js"
);

XPCOMUtils.defineLazyGetter(this, "CryptoHash", () => {
  return Components.Constructor(
    "@mozilla.org/security/hash;1",
    "nsICryptoHash",
    "initWithString"
  );
});

XPCOMUtils.defineLazyGetter(this, "gDatareportingPath", () => {
  return PathUtils.join(
    Services.dirsvc.get("ProfD", Ci.nsIFile).path,
    "datareporting"
  );
});

XPCOMUtils.defineLazyGetter(this, "gStateFilePath", () => {
  return PathUtils.join(gDatareportingPath, "state.json");
});

const PREF_CACHED_CLIENTID = "toolkit.telemetry.cachedClientID";

const SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID =
  "deletion.request.ecosystem_client_id";

/**
 * Checks if client ID has a valid format.
 *
 * @param {String} id A string containing the client ID.
 * @return {Boolean} True when the client ID has valid format, or False
 * otherwise.
 */
function isValidClientID(id) {
  const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
  return UUID_REGEX.test(id);
}

var ClientID = Object.freeze({
  /**
   * This returns a promise resolving to the the stable client ID we use for
   * data reporting (FHR & Telemetry).
   *
   * WARNING: This functionality is duplicated for Android (see GeckoProfile.getClientId
   * for more). There are Java tests (TestGeckoProfile) to ensure the functionality is
   * consistent and Gecko tests to come (bug 1249156). However, THIS IS NOT FOOLPROOF.
   * Be careful when changing this code and, in particular, the underlying file format.
   *
   * @return {Promise<string>} The stable client ID.
   */
  getClientID() {
    return ClientIDImpl.getClientID();
  },

  /**
   * Returns a promise resolving to the ecosystem client ID, used in ecosystem
   * pings as a stable identifier for this profile.
   *
   * @return {Promise<string>} The ecosystem client ID.
   */
  getEcosystemClientID() {
    return ClientIDImpl.getEcosystemClientID();
  },

  /**
   * This returns true if the client ID prior to the last client ID reset was a canary client ID.
   * Android only. Always returns null on Desktop.
   */
  wasCanaryClientID() {
    if (AppConstants.platform == "android") {
      return ClientIDImpl.wasCanaryClientID();
    }

    return null;
  },

  /**
   * Get the client id synchronously without hitting the disk.
   * This returns:
   *  - the current on-disk client id if it was already loaded
   *  - the client id that we cached into preferences (if any)
   *  - null otherwise
   */
  getCachedClientID() {
    return ClientIDImpl.getCachedClientID();
  },

  /**
   * Gets the in-memory cached ecosystem client ID if it was already loaded;
   * `null` otherwise.
   */
  getCachedEcosystemClientID() {
    return ClientIDImpl.getCachedEcosystemClientID();
  },

  async getClientIdHash() {
    return ClientIDImpl.getClientIdHash();
  },

  /**
   * Sets the main and ecosystem client IDs to the canary (known) client ID,
   * writing them to disk and updating the cached versions.
   *
   * Use `removeClientIDs` followed by `get{Ecosystem}ClientID` to clear the
   * existing IDs and generate new, random ones if required.
   *
   * @return {Promise<void>}
   */
  setCanaryClientIDs() {
    return ClientIDImpl.setCanaryClientIDs();
  },

  /**
   * Sets the ecosystem client IDs to a new random value while leaving other IDs
   * unchanged, writing the result to disk and updating the cached identifier.
   * This can be used when a user signs out, to avoid linking telemetry between
   * different accounts.
   *
   * Use `removeClientIDs` followed by `get{Ecosystem}ClientID` to reset *all* the
   * identifiers rather than just the ecosystem client id.
   *
   * @return {Promise<void>} Resolves when the change has been saved to disk.
   */
  resetEcosystemClientID() {
    return ClientIDImpl.resetEcosystemClientID();
  },

  /**
   * Clears the main and ecosystem client IDs asynchronously, removing them
   * from disk. Use `getClientID()` and `getEcosystemClientID()` to generate
   * fresh IDs after calling this method.
   *
   * Should only be used if a reset is explicitly requested by the user.
   *
   * @return {Promise<void>}
   */
  removeClientIDs() {
    return ClientIDImpl.removeClientIDs();
  },

  /**
   * Only used for testing. Invalidates the cached client IDs so that they're
   * read again from file, but doesn't remove the existing IDs from disk.
   */
  _reset() {
    return ClientIDImpl._reset();
  },
});

var ClientIDImpl = {
  _clientID: null,
  _clientIDHash: null,
  _ecosystemClientID: null,
  _loadClientIdsTask: null,
  _saveClientIdsTask: null,
  _removeClientIdsTask: null,
  _logger: null,
  _wasCanary: null,

  _loadClientIDs() {
    if (this._loadClientIdsTask) {
      return this._loadClientIdsTask;
    }

    this._loadClientIdsTask = this._doLoadClientIDs();
    let clear = () => (this._loadClientIdsTask = null);
    this._loadClientIdsTask.then(clear, clear);
    return this._loadClientIdsTask;
  },

  /**
   * Load the client IDs (Telemetry Client ID and Ecosystem Client ID) from the
   * DataReporting Service state file. If either ID is missing, we generate a
   * new one.
   */
  async _doLoadClientIDs() {
    this._log.trace(`_doLoadClientIDs`);
    // If there's a removal in progress, let's wait for it
    await this._removeClientIdsTask;

    // Try to load the client id from the DRS state file.
    let hasCurrentClientID = false;
    let hasCurrentEcosystemClientID = false;
    try {
      let state = await CommonUtils.readJSON(gStateFilePath);
      if (AppConstants.platform == "android" && state && "wasCanary" in state) {
        this._wasCanary = state.wasCanary;
      }
      if (state) {
        hasCurrentClientID = this.updateClientID(state.clientID);
        hasCurrentEcosystemClientID = this.updateEcosystemClientID(
          state.ecosystemClientID
        );
        if (hasCurrentClientID && hasCurrentEcosystemClientID) {
          this._log.trace(`_doLoadClientIDs: Client IDs loaded from state.`);
          return {
            clientID: this._clientID,
            ecosystemClientID: this._ecosystemClientID,
          };
        }
      }
    } catch (e) {
      // fall through to next option
    }

    // We're missing one or both IDs from the DRS state file, generate new ones.
    if (!hasCurrentClientID) {
      this.updateClientID(CommonUtils.generateUUID());
    }
    if (!hasCurrentEcosystemClientID) {
      this.updateEcosystemClientID(CommonUtils.generateUUID());
    }
    this._saveClientIdsTask = this._saveClientIDs();

    // Wait on persisting the id. Otherwise failure to save the ID would result in
    // the client creating and subsequently sending multiple IDs to the server.
    // This would appear as multiple clients submitting similar data, which would
    // result in orphaning.
    await this._saveClientIdsTask;

    this._log.trace("_doLoadClientIDs: New client IDs loaded and persisted.");
    return {
      clientID: this._clientID,
      ecosystemClientID: this._ecosystemClientID,
    };
  },

  /**
   * Save the client ID to the client ID file.
   *
   * @return {Promise} A promise resolved when the client ID is saved to disk.
   */
  async _saveClientIDs() {
    this._log.trace(`_saveClientIDs`);
    let obj = {
      clientID: this._clientID,
      ecosystemClientID: this._ecosystemClientID,
    };
    // We detected a canary client ID when resetting, storing this as a flag
    if (AppConstants.platform == "android" && this._wasCanary) {
      obj.wasCanary = true;
    }
    try {
      await IOUtils.makeDirectory(gDatareportingPath);
    } catch (ex) {
      if (ex.name != "NotAllowedError") {
        throw ex;
      }
    }
    await CommonUtils.writeJSON(obj, gStateFilePath);
    this._saveClientIdsTask = null;
  },

  /**
   * This returns a promise resolving to the the stable client ID we use for
   * data reporting (FHR & Telemetry).
   *
   * @return {Promise<string>} The stable client ID.
   */
  async getClientID() {
    if (!this._clientID) {
      let { clientID } = await this._loadClientIDs();
      return clientID;
    }

    return Promise.resolve(this._clientID);
  },

  async getEcosystemClientID() {
    if (!this._ecosystemClientID) {
      let { ecosystemClientID } = await this._loadClientIDs();
      return ecosystemClientID;
    }

    return Promise.resolve(this._ecosystemClientID);
  },

  /**
   * This returns true if the client ID prior to the last client ID reset was a canary client ID.
   * Android only. Always returns null on Desktop.
   */
  wasCanaryClientID() {
    return this._wasCanary;
  },

  /**
   * Get the client id synchronously without hitting the disk.
   * This returns:
   *  - the current on-disk client id if it was already loaded
   *  - the client id that we cached into preferences (if any)
   *  - null otherwise
   */
  getCachedClientID() {
    if (this._clientID) {
      // Already loaded the client id from disk.
      return this._clientID;
    }

    // If the client id cache contains a value of the wrong type,
    // reset the pref. We need to do this before |getStringPref| since
    // it will just return |null| in that case and we won't be able
    // to distinguish between the missing pref and wrong type cases.
    if (
      Services.prefs.prefHasUserValue(PREF_CACHED_CLIENTID) &&
      Services.prefs.getPrefType(PREF_CACHED_CLIENTID) !=
        Ci.nsIPrefBranch.PREF_STRING
    ) {
      this._log.error(
        "getCachedClientID - invalid client id type in preferences, resetting"
      );
      Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
    }

    // Not yet loaded, return the cached client id if we have one.
    let id = Services.prefs.getStringPref(PREF_CACHED_CLIENTID, null);
    if (id === null) {
      return null;
    }
    if (!isValidClientID(id)) {
      this._log.error(
        "getCachedClientID - invalid client id in preferences, resetting",
        id
      );
      Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);
      return null;
    }
    return id;
  },

  getCachedEcosystemClientID() {
    return this._ecosystemClientID;
  },

  async getClientIdHash() {
    if (!this._clientIDHash) {
      let byteArr = new TextEncoder().encode(await this.getClientID());
      let hash = new CryptoHash("sha256");
      hash.update(byteArr, byteArr.length);
      this._clientIDHash = CommonUtils.bytesAsHex(hash.finish(false));
    }
    return this._clientIDHash;
  },

  /*
   * Resets the provider. This is for testing only.
   */
  async _reset() {
    await this._loadClientIdsTask;
    await this._saveClientIdsTask;
    this._clientID = null;
    this._clientIDHash = null;
    this._ecosystemClientID = null;
  },

  async setCanaryClientIDs() {
    this._log.trace("setCanaryClientIDs");
    this.updateClientID(CANARY_CLIENT_ID);
    this.updateEcosystemClientID(CANARY_CLIENT_ID);

    this._saveClientIdsTask = this._saveClientIDs();
    await this._saveClientIdsTask;
    return this._clientID;
  },

  async resetEcosystemClientID() {
    this._log.trace("resetEcosystemClientID");
    this.updateEcosystemClientID(CommonUtils.generateUUID());
    this._saveClientIdsTask = this._saveClientIDs();
    await this._saveClientIdsTask;
    return this._ecosystemClientID;
  },

  async _doRemoveClientIDs() {
    this._log.trace("_doRemoveClientIDs");

    // Reset the cached main and ecosystem client IDs.
    this._clientID = null;
    this._clientIDHash = null;
    this._ecosystemClientID = null;

    // Clear the client id from the preference cache.
    Services.prefs.clearUserPref(PREF_CACHED_CLIENTID);

    // Clear the old ecosystem client ID from the deletion request scalar store.
    Services.telemetry.scalarSet(
      SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID,
      ""
    );

    // If there is a save in progress, wait for it to complete.
    await this._saveClientIdsTask;

    // Remove the client id from disk
    await IOUtils.remove(gStateFilePath);
  },

  async removeClientIDs() {
    this._log.trace("removeClientIDs");
    let oldClientId = this._clientID;

    // Wait for the removal.
    // Asynchronous calls to getClientID will also be blocked on this.
    this._removeClientIdsTask = this._doRemoveClientIDs();
    let clear = () => (this._removeClientIdsTask = null);
    this._removeClientIdsTask.then(clear, clear);

    await this._removeClientIdsTask;

    // On Android we detect resets after a canary client ID.
    if (AppConstants.platform == "android") {
      this._wasCanary = oldClientId == CANARY_CLIENT_ID;
    }
  },

  /**
   * Sets the client id to the given value and updates the value cached in
   * preferences only if the given id is a valid.
   *
   * @param {String} id A string containing the client ID.
   * @return {Boolean} True when the client ID has valid format, or False
   * otherwise.
   */
  updateClientID(id) {
    if (!isValidClientID(id)) {
      this._log.error("updateClientID - invalid client ID", id);
      return false;
    }

    this._clientID = id;

    if (AppConstants.MOZ_GLEAN) {
      Glean.fogValidation.legacyTelemetryClientId.set(this._clientID);
    }

    this._clientIDHash = null;
    Services.prefs.setStringPref(PREF_CACHED_CLIENTID, this._clientID);
    return true;
  },

  updateEcosystemClientID(id) {
    if (!isValidClientID(id)) {
      this._log.error(
        "updateEcosystemClientID - invalid ecosystem client ID",
        id
      );
      return false;
    }
    this._ecosystemClientID = id;
    Services.telemetry.scalarSet(
      SCALAR_DELETION_REQUEST_ECOSYSTEM_CLIENT_ID,
      id
    );
    return true;
  },

  /**
   * A helper for getting access to telemetry logger.
   */
  get _log() {
    if (!this._logger) {
      this._logger = Log.repository.getLoggerWithMessagePrefix(
        LOGGER_NAME,
        LOGGER_PREFIX
      );
    }

    return this._logger;
  },
};