summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/xpcshell/test_QuarantinedDomains_AMRemoteSettings.js
blob: 9bca9d17b13ee8798270f21ce7803eae98ecf6c9 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Globals imported from head_telemetry.js
/* globals setupTelemetryForTests, resetTelemetryData */

const { QuarantinedDomains } = ChromeUtils.importESModule(
  "resource://gre/modules/ExtensionPermissions.sys.mjs"
);

ChromeUtils.defineESModuleGetters(this, {
  computeSha1HashAsString: "resource://gre/modules/addons/crypto-utils.sys.mjs",
});

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "42", "42");

const QUARANTINE_LIST_PREF = "extensions.quarantinedDomains.list";

function assertQuarantinedListPref(expectedPrefValue) {
  Assert.equal(
    Services.prefs.getPrefType(QUARANTINE_LIST_PREF),
    Services.prefs.PREF_STRING,
    `Expect ${QUARANTINE_LIST_PREF} preference type to be string`
  );

  Assert.equal(
    Services.prefs.getStringPref(QUARANTINE_LIST_PREF),
    expectedPrefValue,
    `Got the expected value set on ${QUARANTINE_LIST_PREF}`
  );
}

function assertQuarantinedListTelemetry(expectedTelemetryHash) {
  Assert.deepEqual(
    {
      listhash: Glean.extensionsQuarantinedDomains.listhash.testGetValue(),
      remotehash: Glean.extensionsQuarantinedDomains.remotehash.testGetValue(),
    },
    expectedTelemetryHash,
    "Got the expected computed domains list probes recorded by the Glean metrics"
  );

  const scalars = Services.telemetry.getSnapshotForScalars().parent;
  Assert.deepEqual(
    {
      listhash: scalars?.["extensions.quarantinedDomains.listhash"],
      remotehash: scalars?.["extensions.quarantinedDomains.remotehash"],
    },
    expectedTelemetryHash,
    "Got the expected metrics mirrored into the unified telemetry scalars"
  );
}

async function testQuarantinedDomainsFromRemoteSettings() {
  // Same as MAX_PREF_LENGTH as defined in Preferences.cpp,
  // see https://searchfox.org/mozilla-central/rev/06510249/modules/libpref/Preferences.cpp#162
  const MAX_PREF_LENGTH = 1 * 1024 * 1024;
  const quarantinedDomainsSets = {
    testSet1: "example.com,example.org",
    testSet2: "someothersite.org,testset2.org",
  };

  // Make sure there isn't initially any pre-existing telemetry data.
  resetTelemetryData();

  await setAndEmitFakeRemoteSettingsData([
    {
      id: "quarantinedDomains-01-testSet-toolong",
      // We expect this entry to throw when trying to set a string pref
      // that doesn't fit in the string prefs size limits.
      quarantinedDomains: {
        [QUARANTINE_LIST_PREF]: "x".repeat(MAX_PREF_LENGTH + 1),
      },
      installTriggerDeprecation: null,
    },
    {
      id: "quarantinedDomains-02-testSet1",
      quarantinedDomains: {
        [QUARANTINE_LIST_PREF]: quarantinedDomainsSets.testSet1,
      },
      installTriggerDeprecation: null,
    },
    {
      // We expect this pref to override the pref set based on the
      // previous entry.
      id: "quarantinedDomains-03-testSet2",
      quarantinedDomains: {
        [QUARANTINE_LIST_PREF]: quarantinedDomainsSets.testSet2,
      },
      installTriggerDeprecation: null,
    },
    {
      // Expect this entry to leave the domains list pref unchanged.
      id: "quarantinedDomains-04-null",
      quarantinedDomains: null,
      installTriggerDeprecation: null,
    },
  ]);

  Assert.equal(
    Services.prefs.getPrefType(QUARANTINE_LIST_PREF),
    Services.prefs.PREF_STRING,
    `Expect ${QUARANTINE_LIST_PREF} preference type to be string`
  );
  // The entry too big to fix in the pref value should throw but not preventing
  // the other entries from being processed.
  // The Last collection entry setting the pref wins, and so we expect
  // the pref to be set to the domains listed in the collection
  // entry with id "quarantinedDomains-testSet2".
  assertQuarantinedListPref(quarantinedDomainsSets.testSet2);
  assertQuarantinedListTelemetry({
    listhash: computeSha1HashAsString(quarantinedDomainsSets.testSet2),
    remotehash: computeSha1HashAsString(quarantinedDomainsSets.testSet2),
  });

  // Confirm that the updated quarantined domains list is now reflected
  // by the results returned by WebExtensionPolicy.isQuarantinedURI.
  // NOTE: Additional test coverage over the quarantined domains behaviors
  // are part of a separate xpcshell test
  // (see toolkit/components/extensions/test/xpcshell/test_QuarantinedDomains.js).
  for (const domain of quarantinedDomainsSets.testSet2.split(",")) {
    let uri = Services.io.newURI(`https://${domain}/`);
    ok(
      WebExtensionPolicy.isQuarantinedURI(uri),
      `Expect ${domain} to be quarantined`
    );
  }

  for (const domain of quarantinedDomainsSets.testSet1.split(",")) {
    let uri = Services.io.newURI(`https://${domain}/`);
    ok(
      !WebExtensionPolicy.isQuarantinedURI(uri),
      `Expect ${domain} to not be quarantined`
    );
  }

  const NEW_PREF_VALUE = "newdomain1.org,newdomain2.org";
  await setAndEmitFakeRemoteSettingsData([
    {
      // This entry doesn't includes an installTriggerDeprecation property
      // (and then we verify that the pref is still set as expected).
      id: "quarantinedDomains-withoutInstallTriggerDeprecation",
      quarantinedDomains: {
        [QUARANTINE_LIST_PREF]: NEW_PREF_VALUE,
      },
    },
  ]);
  assertQuarantinedListPref(NEW_PREF_VALUE);
  assertQuarantinedListTelemetry({
    listhash: computeSha1HashAsString(NEW_PREF_VALUE),
    remotehash: computeSha1HashAsString(NEW_PREF_VALUE),
  });

  await setAndEmitFakeRemoteSettingsData([
    {
      // This entry includes an unexpected property
      // (and then we verify that the pref is still set as expected).
      id: "quarantinedDomains-withoutInstallTriggerDeprecation",
      quarantinedDomains: {
        [QUARANTINE_LIST_PREF]: quarantinedDomainsSets.testSet1,
      },
      someUnexpectedProperty: "some unexpected value",
    },
  ]);
  assertQuarantinedListPref(quarantinedDomainsSets.testSet1);
  assertQuarantinedListTelemetry({
    listhash: computeSha1HashAsString(quarantinedDomainsSets.testSet1),
    remotehash: computeSha1HashAsString(quarantinedDomainsSets.testSet1),
  });

  info(
    "Tamper with the domains list pref value, verify the remotesettings value is set back after restart"
  );
  const MANUALLY_CHANGED_PREF_VALUE =
    quarantinedDomainsSets.testSet1 + ",test123.example.org";
  Services.prefs.setStringPref(
    QUARANTINE_LIST_PREF,
    MANUALLY_CHANGED_PREF_VALUE
  );
  // At this point we expect the value of the hash recorded in telemetry to differ
  // between the listhash and remotehash glean metrics.
  assertQuarantinedListTelemetry({
    listhash: computeSha1HashAsString(MANUALLY_CHANGED_PREF_VALUE),
    remotehash: computeSha1HashAsString(quarantinedDomainsSets.testSet1),
  });

  // Then, we expect the remotehash and listhash to match each other again
  // after the browser restart and the pref value to be back to the last
  // value got from RemoteSettings.
  info("Mock browser restart");
  // Clear telemetry data that was collected so far.
  resetTelemetryData();
  const promisePrefChanged = TestUtils.waitForPrefChange(QUARANTINE_LIST_PREF);
  await AddonTestUtils.promiseRestartManager();
  info(
    `Wait for expected change notified for the ${QUARANTINE_LIST_PREF} pref`
  );
  await promisePrefChanged;

  assertQuarantinedListPref(quarantinedDomainsSets.testSet1);
  assertQuarantinedListTelemetry({
    listhash: computeSha1HashAsString(quarantinedDomainsSets.testSet1),
    remotehash: computeSha1HashAsString(quarantinedDomainsSets.testSet1),
  });
}

add_setup(async () => {
  setupTelemetryForTests();
  await AddonTestUtils.promiseStartupManager();

  Assert.ok(
    QuarantinedDomains._initialized,
    "QuarantinedDomains is initialized"
  );
});

add_task(testQuarantinedDomainsFromRemoteSettings);