Policies.jsm 78.1 KB
Newer Older
1
2
3
4
5
6
/* 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";

7
8
9
10
11
12
13
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
const { AppConstants } = ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);
14
15
16

XPCOMUtils.defineLazyServiceGetters(this, {
  gCertDB: ["@mozilla.org/security/x509certdb;1", "nsIX509CertDB"],
17
18
19
20
21
22
23
24
25
  gExternalProtocolService: [
    "@mozilla.org/uriloader/external-protocol-service;1",
    "nsIExternalProtocolService",
  ],
  gHandlerService: [
    "@mozilla.org/uriloader/handler-service;1",
    "nsIHandlerService",
  ],
  gMIMEService: ["@mozilla.org/mime;1", "nsIMIMEService"],
26
27
  gXulStore: ["@mozilla.org/xul/xulstore;1", "nsIXULStore"],
});
28
29

XPCOMUtils.defineLazyModuleGetters(this, {
30
  AddonManager: "resource://gre/modules/AddonManager.jsm",
31
  BookmarksPolicies: "resource:///modules/policies/BookmarksPolicies.jsm",
32
  CustomizableUI: "resource:///modules/CustomizableUI.jsm",
33
  FileUtils: "resource://gre/modules/FileUtils.jsm",
34
35
  ProxyPolicies: "resource:///modules/policies/ProxyPolicies.jsm",
  WebsiteFilter: "resource:///modules/policies/WebsiteFilter.jsm",
36
37
});

38
39
XPCOMUtils.defineLazyGlobalGetters(this, ["File", "FileReader"]);

40
41
const PREF_LOGLEVEL = "browser.policies.loglevel";
const BROWSER_DOCUMENT_URL = AppConstants.BROWSER_CHROME_URL;
42
43
44
45
46
47
const ABOUT_CONTRACT = "@mozilla.org/network/protocol/about;1?what=";

let env = Cc["@mozilla.org/process/environment;1"].getService(
  Ci.nsIEnvironment
);
const isXpcshell = env.exists("XPCSHELL_TEST_PROFILE_DIR");
48
49

XPCOMUtils.defineLazyGetter(this, "log", () => {
50
  let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm");
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
  return new ConsoleAPI({
    prefix: "Policies.jsm",
    // tip: set maxLogLevel to "debug" and use log.debug() to create detailed
    // messages during development. See LOG_LEVELS in Console.jsm for details.
    maxLogLevel: "error",
    maxLogLevelPref: PREF_LOGLEVEL,
  });
});

var EXPORTED_SYMBOLS = ["Policies"];

/*
 * ============================
 * = POLICIES IMPLEMENTATIONS =
 * ============================
 *
 * The Policies object below is where the implementation for each policy
 * happens. An object for each policy should be defined, containing
 * callback functions that will be called by the engine.
 *
 * See the _callbacks object in EnterprisePolicies.js for the list of
 * possible callbacks and an explanation of each.
 *
 * Each callback will be called with two parameters:
 * - manager
 *   This is the EnterprisePoliciesManager singleton object from
 *   EnterprisePolicies.js
 *
 * - param
 *   The parameter defined for this policy in policies-schema.json.
 *   It will be different for each policy. It could be a boolean,
 *   a string, an array or a complex object. All parameters have
 *   been validated according to the schema, and no unknown
 *   properties will be present on them.
 *
 * The callbacks will be bound to their parent policy object.
 */
var Policies = {
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
  // Used for cleaning up policies.
  // Use the same timing that you used for setting up the policy.
  _cleanup: {
    onBeforeAddons(manager) {
      if (Cu.isInAutomation || isXpcshell) {
        console.log("_cleanup from onBeforeAddons");
        clearBlockedAboutPages();
      }
    },
    onProfileAfterChange(manager) {
      if (Cu.isInAutomation || isXpcshell) {
        console.log("_cleanup from onProfileAfterChange");
      }
    },
    onBeforeUIStartup(manager) {
      if (Cu.isInAutomation || isXpcshell) {
        console.log("_cleanup from onBeforeUIStartup");
      }
    },
    onAllWindowsRestored(manager) {
      if (Cu.isInAutomation || isXpcshell) {
        console.log("_cleanup from onAllWindowsRestored");
      }
    },
  },

115
116
117
118
119
120
  "3rdparty": {
    onBeforeAddons(manager, param) {
      manager.setExtensionPolicies(param.Extensions);
    },
  },

121
122
123
124
125
126
127
128
129
130
131
  AllowedDomainsForApps: {
    onBeforeAddons(manager, param) {
      Services.obs.addObserver(function(subject, topic, data) {
        let channel = subject.QueryInterface(Ci.nsIHttpChannel);
        if (channel.URI.host.endsWith(".google.com")) {
          channel.setRequestHeader("X-GoogApps-Allowed-Domains", param, true);
        }
      }, "http-on-modify-request");
    },
  },

132
133
134
135
136
137
138
139
140
  AppAutoUpdate: {
    onBeforeUIStartup(manager, param) {
      // Logic feels a bit reversed here, but it's correct. If AppAutoUpdate is
      // true, we disallow turning off auto updating, and visa versa.
      if (param) {
        manager.disallowFeature("app-auto-updates-off");
      } else {
        manager.disallowFeature("app-auto-updates-on");
      }
141
142
143
    },
  },

144
145
146
147
148
  AppUpdateURL: {
    // No implementation needed here. UpdateService.jsm will check for this
    // policy directly when determining the update URL.
  },

149
  Authentication: {
150
    onBeforeAddons(manager, param) {
151
152
153
154
155
      let locked = true;
      if ("Locked" in param) {
        locked = param.Locked;
      }

156
      if ("SPNEGO" in param) {
157
        setDefaultPref(
158
          "network.negotiate-auth.trusted-uris",
159
160
          param.SPNEGO.join(", "),
          locked
161
        );
162
163
      }
      if ("Delegated" in param) {
164
        setDefaultPref(
165
          "network.negotiate-auth.delegation-uris",
166
167
          param.Delegated.join(", "),
          locked
168
        );
169
170
      }
      if ("NTLM" in param) {
171
        setDefaultPref(
172
          "network.automatic-ntlm-auth.trusted-uris",
173
174
          param.NTLM.join(", "),
          locked
175
        );
176
      }
177
      if ("AllowNonFQDN" in param) {
178
179
        if ("NTLM" in param.AllowNonFQDN) {
          setDefaultPref(
180
            "network.automatic-ntlm-auth.allow-non-fqdn",
181
182
            param.AllowNonFQDN.NTLM,
            locked
183
          );
184
        }
185
186
        if ("SPNEGO" in param.AllowNonFQDN) {
          setDefaultPref(
187
            "network.negotiate-auth.allow-non-fqdn",
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
            param.AllowNonFQDN.SPNEGO,
            locked
          );
        }
      }
      if ("AllowProxies" in param) {
        if ("NTLM" in param.AllowProxies) {
          setDefaultPref(
            "network.automatic-ntlm-auth.allow-proxies",
            param.AllowProxies.NTLM,
            locked
          );
        }
        if ("SPNEGO" in param.AllowProxies) {
          setDefaultPref(
            "network.negotiate-auth.allow-proxies",
            param.AllowProxies.SPNEGO,
            locked
206
          );
207
208
        }
      }
209
210
211
212
213
214
215
      if ("PrivateBrowsing" in param) {
        setDefaultPref(
          "network.auth.private-browsing-sso",
          param.PrivateBrowsing,
          locked
        );
      }
216
    },
217
218
  },

219
220
221
222
223
224
225
226
227
228
229
  AutoLaunchProtocolsFromOrigins: {
    onBeforeAddons(manager, param) {
      for (let info of param) {
        addAllowDenyPermissions(
          `open-protocol-handler^${info.protocol}`,
          info.allowed_origins
        );
      }
    },
  },

230
231
232
233
234
235
236
237
238
239
  BackgroundAppUpdate: {
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("app-background-update-off");
      } else {
        manager.disallowFeature("app-background-update-on");
      }
    },
  },

240
  BlockAboutAddons: {
241
242
    onBeforeUIStartup(manager, param) {
      if (param) {
243
        blockAboutPage(manager, "about:addons", true);
244
      }
245
    },
246
247
  },

248
  BlockAboutConfig: {
249
250
    onBeforeUIStartup(manager, param) {
      if (param) {
251
        blockAboutPage(manager, "about:config");
252
253
        setAndLockPref("devtools.chrome.enabled", false);
      }
254
    },
255
256
  },

257
  BlockAboutProfiles: {
258
259
    onBeforeUIStartup(manager, param) {
      if (param) {
260
        blockAboutPage(manager, "about:profiles");
261
      }
262
    },
263
264
  },

265
  BlockAboutSupport: {
266
267
    onBeforeUIStartup(manager, param) {
      if (param) {
268
        blockAboutPage(manager, "about:support");
269
      }
270
    },
271
272
  },

273
  Bookmarks: {
274
275
    onAllWindowsRestored(manager, param) {
      BookmarksPolicies.processBookmarks(param);
276
    },
277
278
  },

279
  CaptivePortal: {
280
281
282
283
284
    onBeforeAddons(manager, param) {
      setAndLockPref("network.captive-portal-service.enabled", param);
    },
  },

285
  Certificates: {
286
287
    onBeforeAddons(manager, param) {
      if ("ImportEnterpriseRoots" in param) {
288
289
290
291
        setAndLockPref(
          "security.enterprise_roots.enabled",
          param.ImportEnterpriseRoots
        );
292
      }
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
      if ("Install" in param) {
        (async () => {
          let dirs = [];
          let platform = AppConstants.platform;
          if (platform == "win") {
            dirs = [
              // Ugly, but there is no official way to get %USERNAME\AppData\Roaming\Mozilla.
              Services.dirsvc.get("XREUSysExt", Ci.nsIFile).parent,
              // Even more ugly, but there is no official way to get %USERNAME\AppData\Local\Mozilla.
              Services.dirsvc.get("DefProfLRt", Ci.nsIFile).parent.parent,
            ];
          } else if (platform == "macosx" || platform == "linux") {
            dirs = [
              // These two keys are named wrong. They return the Mozilla directory.
              Services.dirsvc.get("XREUserNativeManifests", Ci.nsIFile),
              Services.dirsvc.get("XRESysNativeManifests", Ci.nsIFile),
            ];
          }
          dirs.unshift(Services.dirsvc.get("XREAppDist", Ci.nsIFile));
          for (let certfilename of param.Install) {
            let certfile;
            try {
315
316
317
              certfile = Cc["@mozilla.org/file/local;1"].createInstance(
                Ci.nsIFile
              );
318
319
320
321
              certfile.initWithPath(certfilename);
            } catch (e) {
              for (let dir of dirs) {
                certfile = dir.clone();
322
323
324
                certfile.append(
                  platform == "linux" ? "certificates" : "Certificates"
                );
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
                certfile.append(certfilename);
                if (certfile.exists()) {
                  break;
                }
              }
            }
            let file;
            try {
              file = await File.createFromNsIFile(certfile);
            } catch (e) {
              log.error(`Unable to find certificate - ${certfilename}`);
              continue;
            }
            let reader = new FileReader();
            reader.onloadend = function() {
              if (reader.readyState != reader.DONE) {
                log.error(`Unable to read certificate - ${certfile.path}`);
                return;
              }
344
              let certFile = reader.result;
345
346
347
348
              let certFileArray = [];
              for (let i = 0; i < certFile.length; i++) {
                certFileArray.push(certFile.charCodeAt(i));
              }
349
              let cert;
350
              try {
351
                cert = gCertDB.constructX509(certFileArray);
352
              } catch (e) {
353
354
355
                log.debug(
                  `constructX509 failed with error '${e}' - trying constructX509FromBase64.`
                );
356
357
358
359
                try {
                  // It might be PEM instead of DER.
                  cert = gCertDB.constructX509FromBase64(pemToBase64(certFile));
                } catch (ex) {
360
                  log.error(`Unable to add certificate - ${certfile.path}`, ex);
361
362
363
                }
              }
              if (cert) {
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
                if (
                  gCertDB.isCertTrusted(
                    cert,
                    Ci.nsIX509Cert.CA_CERT,
                    Ci.nsIX509CertDB.TRUSTED_SSL
                  )
                ) {
                  // Certificate is already installed.
                  return;
                }
                try {
                  gCertDB.addCert(certFile, "CT,CT,");
                } catch (e) {
                  // It might be PEM instead of DER.
                  gCertDB.addCertFromBase64(pemToBase64(certFile), "CT,CT,");
                }
380
381
382
383
384
385
386
              }
            };
            reader.readAsBinaryString(file);
          }
        })();
      }
    },
387
388
  },

389
  Cookies: {
390
391
392
    onBeforeUIStartup(manager, param) {
      addAllowDenyPermissions("cookie", param.Allow, param.Block);

393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
      if (param.AllowSession) {
        for (let origin of param.AllowSession) {
          try {
            Services.perms.addFromPrincipal(
              Services.scriptSecurityManager.createContentPrincipalFromOrigin(
                origin
              ),
              "cookie",
              Ci.nsICookiePermission.ACCESS_SESSION,
              Ci.nsIPermissionManager.EXPIRE_POLICY
            );
          } catch (ex) {
            log.error(
              `Unable to add cookie session permission - ${origin.href}`
            );
          }
        }
      }

412
      if (param.Block) {
413
414
415
        const hosts = param.Block.map(url => url.hostname)
          .sort()
          .join("\n");
416
417
        runOncePerModification("clearCookiesForBlockedHosts", hosts, () => {
          for (let blocked of param.Block) {
418
419
420
421
            Services.cookies.removeCookiesWithOriginAttributes(
              "{}",
              blocked.hostname
            );
422
423
424
          }
        });
      }
425
426

      if (param.ExpireAtSessionEnd !== undefined || param.Locked) {
427
        let newLifetimePolicy = Ci.nsICookieService.ACCEPT_NORMALLY;
428
        if (param.ExpireAtSessionEnd) {
429
          newLifetimePolicy = Ci.nsICookieService.ACCEPT_SESSION;
430
431
        }

432
433
434
435
436
        setDefaultPref(
          "network.cookie.lifetimePolicy",
          newLifetimePolicy,
          param.Locked
        );
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
499

      // New Cookie Behavior option takes precendence
      let defaultPref = Services.prefs.getDefaultBranch("");
      let newCookieBehavior = defaultPref.getIntPref(
        "network.cookie.cookieBehavior"
      );
      let newCookieBehaviorPB = defaultPref.getIntPref(
        "network.cookie.cookieBehavior.pbmode"
      );
      if ("Behavior" in param || "BehaviorPrivateBrowsing" in param) {
        let behaviors = {
          accept: Ci.nsICookieService.BEHAVIOR_ACCEPT,
          "reject-foreign": Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN,
          reject: Ci.nsICookieService.BEHAVIOR_REJECT,
          "limit-foreign": Ci.nsICookieService.BEHAVIOR_LIMIT_FOREIGN,
          "reject-tracker": Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER,
          "reject-tracker-and-partition-foreign":
            Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN,
        };
        if ("Behavior" in param) {
          newCookieBehavior = behaviors[param.Behavior];
        }
        if ("BehaviorPrivateBrowsing" in param) {
          newCookieBehaviorPB = behaviors[param.BehaviorPrivateBrowsing];
        }
      } else {
        // Default, AcceptThirdParty, and RejectTracker are being
        // deprecated in favor of Behavior. They will continue
        // to be supported, though.
        if (
          param.Default !== undefined ||
          param.AcceptThirdParty !== undefined ||
          param.RejectTracker !== undefined ||
          param.Locked
        ) {
          newCookieBehavior = Ci.nsICookieService.BEHAVIOR_ACCEPT;
          if (param.Default !== undefined && !param.Default) {
            newCookieBehavior = Ci.nsICookieService.BEHAVIOR_REJECT;
          } else if (param.AcceptThirdParty) {
            if (param.AcceptThirdParty == "never") {
              newCookieBehavior = Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN;
            } else if (param.AcceptThirdParty == "from-visited") {
              newCookieBehavior = Ci.nsICookieService.BEHAVIOR_LIMIT_FOREIGN;
            }
          } else if (param.RejectTracker) {
            newCookieBehavior = Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER;
          }
        }
        // With the old cookie policy, we made private browsing the same.
        newCookieBehaviorPB = newCookieBehavior;
      }
      // We set the values no matter what just in case the policy was only used to lock.
      setDefaultPref(
        "network.cookie.cookieBehavior",
        newCookieBehavior,
        param.Locked
      );
      setDefaultPref(
        "network.cookie.cookieBehavior.pbmode",
        newCookieBehaviorPB,
        param.Locked
      );
500
501
502
    },
  },

503
  DefaultDownloadDirectory: {
504
505
506
507
508
509
510
    onBeforeAddons(manager, param) {
      setDefaultPref("browser.download.dir", replacePathVariables(param));
      // If a custom download directory is being used, just lock folder list to 2.
      setAndLockPref("browser.download.folderList", 2);
    },
  },

511
  DisableAppUpdate: {
512
513
514
515
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("appUpdate");
      }
516
    },
517
518
  },

519
  DisableBuiltinPDFViewer: {
520
    onBeforeAddons(manager, param) {
521
      if (param) {
522
        setAndLockPref("pdfjs.disabled", true);
523
      }
524
    },
525
526
  },

527
528
529
  DisabledCiphers: {
    onBeforeAddons(manager, param) {
      if ("TLS_DHE_RSA_WITH_AES_128_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
530
531
532
533
        setAndLockPref(
          "security.ssl3.dhe_rsa_aes_128_sha",
          !param.TLS_DHE_RSA_WITH_AES_128_CBC_SHA
        );
534
535
      }
      if ("TLS_DHE_RSA_WITH_AES_256_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
536
537
538
539
        setAndLockPref(
          "security.ssl3.dhe_rsa_aes_256_sha",
          !param.TLS_DHE_RSA_WITH_AES_256_CBC_SHA
        );
540
541
      }
      if ("TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
542
543
544
545
        setAndLockPref(
          "security.ssl3.ecdhe_rsa_aes_128_sha",
          !param.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
        );
546
547
      }
      if ("TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
548
549
550
551
        setAndLockPref(
          "security.ssl3.ecdhe_rsa_aes_256_sha",
          !param.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
        );
552
553
      }
      if ("TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
554
555
556
557
        setAndLockPref(
          "security.ssl3.ecdhe_rsa_aes_128_gcm_sha256",
          !param.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
        );
558
559
      }
      if ("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
560
561
562
563
        setAndLockPref(
          "security.ssl3.ecdhe_ecdsa_aes_128_gcm_sha256",
          !param.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
        );
564
565
      }
      if ("TLS_RSA_WITH_AES_128_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
566
567
568
569
        setAndLockPref(
          "security.ssl3.rsa_aes_128_sha",
          !param.TLS_RSA_WITH_AES_128_CBC_SHA
        );
570
571
      }
      if ("TLS_RSA_WITH_AES_256_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
572
573
574
575
        setAndLockPref(
          "security.ssl3.rsa_aes_256_sha",
          !param.TLS_RSA_WITH_AES_256_CBC_SHA
        );
576
577
      }
      if ("TLS_RSA_WITH_3DES_EDE_CBC_SHA" in param) {
Carsten Schoenert's avatar
Carsten Schoenert committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
        setAndLockPref(
          "security.ssl3.rsa_des_ede3_sha",
          !param.TLS_RSA_WITH_3DES_EDE_CBC_SHA
        );
      }
      if ("TLS_RSA_WITH_AES_128_GCM_SHA256" in param) {
        setAndLockPref(
          "security.ssl3.rsa_aes_128_gcm_sha256",
          !param.TLS_RSA_WITH_AES_128_GCM_SHA256
        );
      }
      if ("TLS_RSA_WITH_AES_256_GCM_SHA384" in param) {
        setAndLockPref(
          "security.ssl3.rsa_aes_256_gcm_sha384",
          !param.TLS_RSA_WITH_AES_256_GCM_SHA384
        );
594
595
596
597
598
599
600
601
602
603
      }
    },
  },

  DisableDefaultBrowserAgent: {
    // The implementation of this policy is in the default browser agent itself
    // (/toolkit/mozapps/defaultagent); we need an entry for it here so that it
    // shows up in about:policies as a real policy and not as an error.
  },

604
  DisableDeveloperTools: {
605
606
607
608
609
610
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("devtools.policy.disabled", true);
        setAndLockPref("devtools.chrome.enabled", false);

        manager.disallowFeature("devtools");
611
612
613
        blockAboutPage(manager, "about:devtools");
        blockAboutPage(manager, "about:debugging");
        blockAboutPage(manager, "about:devtools-toolbox");
614
        blockAboutPage(manager, "about:profiling");
615
      }
616
    },
617
618
  },

619
  DisableFeedbackCommands: {
620
621
622
623
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("feedbackCommands");
      }
624
    },
625
626
  },

627
  DisableFirefoxAccounts: {
628
629
630
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("identity.fxaccounts.enabled", false);
631
        setAndLockPref("browser.aboutwelcome.enabled", false);
632
      }
633
    },
634
635
  },

636
  DisableFirefoxScreenshots: {
637
638
639
640
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("extensions.screenshots.disabled", true);
      }
641
    },
642
643
  },

644
  DisableFirefoxStudies: {
645
646
647
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("Shield");
648
649
650
651
652
653
654
655
        setAndLockPref(
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.addons",
          false
        );
        setAndLockPref(
          "browser.newtabpage.activity-stream.asrouter.userprefs.cfr.features",
          false
        );
656
      }
657
    },
658
659
  },

660
  DisableForgetButton: {
661
662
663
664
    onProfileAfterChange(manager, param) {
      if (param) {
        setAndLockPref("privacy.panicButton.enabled", false);
      }
665
    },
666
667
  },

668
  DisableFormHistory: {
669
670
671
672
    onBeforeUIStartup(manager, param) {
      if (param) {
        setAndLockPref("browser.formfill.enable", false);
      }
673
    },
674
675
  },

676
  DisableMasterPasswordCreation: {
677
678
679
680
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("createMasterPassword");
      }
681
    },
682
683
  },

684
685
686
687
688
689
690
691
  DisablePasswordReveal: {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("passwordReveal");
      }
    },
  },

692
  DisablePocket: {
693
694
695
696
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("extensions.pocket.enabled", false);
      }
697
    },
698
699
  },

700
  DisablePrivateBrowsing: {
701
702
703
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("privatebrowsing");
704
        blockAboutPage(manager, "about:privatebrowsing", true);
705
706
        setAndLockPref("browser.privatebrowsing.autostart", false);
      }
707
    },
708
709
  },

710
  DisableProfileImport: {
711
712
713
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("profileImport");
714
715
716
717
        setAndLockPref(
          "browser.newtabpage.activity-stream.migrationExpired",
          true
        );
718
      }
719
    },
720
721
  },

722
  DisableProfileRefresh: {
723
724
725
726
727
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("profileRefresh");
        setAndLockPref("browser.disableResetPrompt", true);
      }
728
    },
729
730
  },

731
  DisableSafeMode: {
732
733
734
735
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("safeMode");
      }
736
    },
737
738
  },

739
  DisableSecurityBypass: {
740
741
    onBeforeUIStartup(manager, param) {
      if ("InvalidCertificate" in param) {
742
743
744
745
        setAndLockPref(
          "security.certerror.hideAddException",
          param.InvalidCertificate
        );
746
747
748
      }

      if ("SafeBrowsing" in param) {
749
750
751
752
        setAndLockPref(
          "browser.safebrowsing.allowOverride",
          !param.SafeBrowsing
        );
753
      }
754
    },
755
756
  },

757
  DisableSetDesktopBackground: {
758
759
    onBeforeUIStartup(manager, param) {
      if (param) {
760
        manager.disallowFeature("setDesktopBackground");
761
      }
762
    },
763
764
  },

765
  DisableSystemAddonUpdate: {
766
767
768
769
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("SysAddonUpdate");
      }
770
    },
771
772
  },

773
  DisableTelemetry: {
774
    onBeforeAddons(manager, param) {
775
      if (param) {
776
777
        setAndLockPref("datareporting.healthreport.uploadEnabled", false);
        setAndLockPref("datareporting.policy.dataSubmissionEnabled", false);
778
        setAndLockPref("toolkit.telemetry.archive.enabled", false);
779
        blockAboutPage(manager, "about:telemetry");
780
      }
781
    },
782
783
  },

784
  DisplayBookmarksToolbar: {
785
786
787
788
789
790
    onBeforeUIStartup(manager, param) {
      let value = (!param).toString();
      // This policy is meant to change the default behavior, not to force it.
      // If this policy was alreay applied and the user chose to re-hide the
      // bookmarks toolbar, do not show it again.
      runOncePerModification("displayBookmarksToolbar", value, () => {
791
792
793
794
795
796
797
798
799
800
        // Set the preference to keep the bookmarks bar open and also
        // declaratively open the bookmarks toolbar. Otherwise, default
        // to showing it on the New Tab Page.
        let visibilityPref = "browser.toolbars.bookmarks.visibility";
        let bookmarksFeaturePref = "browser.toolbars.bookmarks.2h2020";
        let visibility = param ? "always" : "never";
        if (Services.prefs.getBoolPref(bookmarksFeaturePref, false)) {
          visibility = param ? "always" : "newtab";
        }
        Services.prefs.setCharPref(visibilityPref, visibility);
801
802
803
804
805
806
        gXulStore.setValue(
          BROWSER_DOCUMENT_URL,
          "PersonalToolbar",
          "collapsed",
          value
        );
807
      });
808
    },
809
810
  },

811
  DisplayMenuBar: {
812
    onBeforeUIStartup(manager, param) {
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
      let value;
      if (
        typeof param === "boolean" ||
        param == "default-on" ||
        param == "default-off"
      ) {
        switch (param) {
          case "default-on":
            value = "false";
            break;
          case "default-off":
            value = "true";
            break;
          default:
            value = (!param).toString();
            break;
        }
        // This policy is meant to change the default behavior, not to force it.
        // If this policy was already applied and the user chose to re-hide the
        // menu bar, do not show it again.
        runOncePerModification("displayMenuBar", value, () => {
          gXulStore.setValue(
            BROWSER_DOCUMENT_URL,
            "toolbar-menubar",
            "autohide",
            value
          );
        });
      } else {
        switch (param) {
          case "always":
            value = "false";
            break;
          case "never":
            // Make sure Alt key doesn't show the menubar
            setAndLockPref("ui.key.menuAccessKeyFocuses", false);
            value = "true";
            break;
        }
852
853
854
855
856
857
        gXulStore.setValue(
          BROWSER_DOCUMENT_URL,
          "toolbar-menubar",
          "autohide",
          value
        );
858
859
        manager.disallowFeature("hideShowMenuBar");
      }
860
    },
861
862
  },

863
  DNSOverHTTPS: {
864
    onBeforeAddons(manager, param) {
865
866
867
868
      let locked = false;
      if ("Locked" in param) {
        locked = param.Locked;
      }
869
870
      if ("Enabled" in param) {
        let mode = param.Enabled ? 2 : 5;
871
        setDefaultPref("network.trr.mode", mode, locked);
872
      }
873
874
875
876
877
878
879
880
881
      if ("ProviderURL" in param) {
        setDefaultPref("network.trr.uri", param.ProviderURL.href, locked);
      }
      if ("ExcludedDomains" in param) {
        setDefaultPref(
          "network.trr.excluded-domains",
          param.ExcludedDomains.join(","),
          locked
        );
882
883
884
885
      }
    },
  },

886
  DontCheckDefaultBrowser: {
887
    onBeforeUIStartup(manager, param) {
888
      setAndLockPref("browser.shell.checkDefaultBrowser", !param);
889
    },
890
891
  },

892
  DownloadDirectory: {
893
894
895
896
    onBeforeAddons(manager, param) {
      setAndLockPref("browser.download.dir", replacePathVariables(param));
      // If a custom download directory is being used, just lock folder list to 2.
      setAndLockPref("browser.download.folderList", 2);
897
898
899
      // Per Chrome spec, user can't choose to download every time
      // if this is set.
      setAndLockPref("browser.download.useDownloadDir", true);
900
901
902
    },
  },

903
  EnableTrackingProtection: {
904
905
    onBeforeUIStartup(manager, param) {
      if (param.Value) {
906
907
908
909
910
911
912
913
914
915
        setDefaultPref(
          "privacy.trackingprotection.enabled",
          true,
          param.Locked
        );
        setDefaultPref(
          "privacy.trackingprotection.pbmode.enabled",
          true,
          param.Locked
        );
916
917
918
919
      } else {
        setAndLockPref("privacy.trackingprotection.enabled", false);
        setAndLockPref("privacy.trackingprotection.pbmode.enabled", false);
      }
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
      if ("Cryptomining" in param) {
        setDefaultPref(
          "privacy.trackingprotection.cryptomining.enabled",
          param.Cryptomining,
          param.Locked
        );
      }
      if ("Fingerprinting" in param) {
        setDefaultPref(
          "privacy.trackingprotection.fingerprinting.enabled",
          param.Fingerprinting,
          param.Locked
        );
      }
      if ("Exceptions" in param) {
        addAllowDenyPermissions("trackingprotection", param.Exceptions);
      }
937
    },
938
939
  },

940
941
942
943
944
945
946
947
948
949
950
951
  EncryptedMediaExtensions: {
    onBeforeAddons(manager, param) {
      let locked = false;
      if ("Locked" in param) {
        locked = param.Locked;
      }
      if ("Enabled" in param) {
        setDefaultPref("media.eme.enabled", param.Enabled, locked);
      }
    },
  },

952
  Extensions: {
953
    onBeforeUIStartup(manager, param) {
954
955
      let uninstallingPromise = Promise.resolve();
      if ("Uninstall" in param) {
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
        uninstallingPromise = runOncePerModification(
          "extensionsUninstall",
          JSON.stringify(param.Uninstall),
          async () => {
            // If we're uninstalling add-ons, re-run the extensionsInstall runOnce even if it hasn't
            // changed, which will allow add-ons to be updated.
            Services.prefs.clearUserPref(
              "browser.policies.runOncePerModification.extensionsInstall"
            );
            let addons = await AddonManager.getAddonsByIDs(param.Uninstall);
            for (let addon of addons) {
              if (addon) {
                try {
                  await addon.uninstall();
                } catch (e) {
                  // This can fail for add-ons that can't be uninstalled.
                  log.debug(`Add-on ID (${addon.id}) couldn't be uninstalled.`);
                }
974
975
976
              }
            }
          }
977
        );
978
      }
979
      if ("Install" in param) {
980
981
982
983
984
985
986
        runOncePerModification(
          "extensionsInstall",
          JSON.stringify(param.Install),
          async () => {
            await uninstallingPromise;
            for (let location of param.Install) {
              let uri;
987
              try {
988
989
990
                // We need to try as a file first because
                // Windows paths are valid URIs.
                // This is done for legacy support (old API)
991
992
                let xpiFile = new FileUtils.File(location);
                uri = Services.io.newFileURI(xpiFile);
993
994
              } catch (e) {
                uri = Services.io.newURI(location);
995
              }
996
              installAddonFromURL(uri.spec);
997
            }
998
          }
999
        );
1000
      }
For faster browsing, not all history is shown. View entire blame