Policies.jsm 13.6 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
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
9
10
11
XPCOMUtils.defineLazyServiceGetter(this, "gXulStore",
                                   "@mozilla.org/xul/xulstore;1",
                                   "nsIXULStore");
12

13
14
15
16
XPCOMUtils.defineLazyModuleGetters(this, {
  BookmarksPolicies: "resource:///modules/policies/BookmarksPolicies.jsm",
});

17
const PREF_LOGLEVEL           = "browser.policies.loglevel";
18
const BROWSER_DOCUMENT_URL    = "chrome://browser/content/browser.xul";
19
20

XPCOMUtils.defineLazyGetter(this, "log", () => {
21
  let { ConsoleAPI } = ChromeUtils.import("resource://gre/modules/Console.jsm", {});
22
23
24
25
26
27
28
29
30
  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,
  });
});

31
var EXPORTED_SYMBOLS = ["Policies"];
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
/*
 * ============================
 * = 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.
 */
59
var Policies = {
60
61
62
63
64
65
66
67
  "BlockAboutAddons": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:addons", true);
      }
    }
  },

68
  "BlockAboutConfig": {
69
    onBeforeUIStartup(manager, param) {
70
      if (param) {
71
        manager.disallowFeature("about:config", true);
72
        setAndLockPref("devtools.chrome.enabled", false);
73
74
75
      }
    }
  },
76

77
78
79
80
81
82
83
84
  "BlockAboutProfiles": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:profiles", true);
      }
    }
  },

85
86
87
88
89
90
91
92
  "BlockAboutSupport": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("about:support", true);
      }
    }
  },

93
  "BlockSetDesktopBackground": {
94
    onBeforeUIStartup(manager, param) {
95
      if (param) {
96
        manager.disallowFeature("setDesktopBackground", true);
97
98
99
100
      }
    }
  },

101
102
103
104
105
106
  "Bookmarks": {
    onAllWindowsRestored(manager, param) {
      BookmarksPolicies.processBookmarks(param);
    }
  },

107
  "Cookies": {
108
    onBeforeUIStartup(manager, param) {
109
      addAllowDenyPermissions("cookie", param.Allow, param.Block);
110
111
112
113
114
115
116
117
118

      if (param.Block) {
        const hosts = param.Block.map(uri => uri.host).sort().join("\n");
        runOncePerModification("clearCookiesForBlockedHosts", hosts, () => {
          for (let blocked of param.Block) {
            Services.cookies.removeCookiesWithOriginAttributes("{}", blocked.host);
          }
        });
      }
119
120
121
    }
  },

122
  "CreateMasterPassword": {
123
    onBeforeUIStartup(manager, param) {
124
125
      if (!param) {
        manager.disallowFeature("createMasterPassword");
126
127
128
129
      }
    }
  },

130
131
132
133
  "DisableAppUpdate": {
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("appUpdate");
134
135
136
137
      }
    }
  },

138
139
140
141
142
143
144
145
  "DisableBuiltinPDFViewer": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("PDF.js");
      }
    }
  },

146
147
148
149
150
151
152
153
154
155
156
157
158
159
  "DisableDeveloperTools": {
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("devtools.policy.disabled", true);
        setAndLockPref("devtools.chrome.enabled", false);

        manager.disallowFeature("devtools");
        manager.disallowFeature("about:devtools");
        manager.disallowFeature("about:debugging");
        manager.disallowFeature("about:devtools-toolbox");
      }
    }
  },

160
161
162
163
164
165
166
167
  "DisableFeedbackCommands": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("feedbackCommands");
      }
    }
  },

168
169
170
171
172
173
174
175
  "DisableFirefoxAccounts": {
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("identity.fxaccounts.enabled", false);
      }
    }
  },

176
177
  "DisableFirefoxScreenshots": {
    onBeforeAddons(manager, param) {
178
      if (param) {
179
180
181
182
183
        setAndLockPref("extensions.screenshots.disabled", true);
      }
    }
  },

184
185
  "DisableFirefoxStudies": {
    onBeforeAddons(manager, param) {
186
      if (param) {
187
188
189
190
191
        manager.disallowFeature("Shield");
      }
    }
  },

192
193
  "DisableFormHistory": {
    onBeforeUIStartup(manager, param) {
194
      if (param) {
195
196
197
198
199
        setAndLockPref("browser.formfill.enable", false);
      }
    }
  },

200
201
202
203
204
205
206
207
  "DisablePocket": {
    onBeforeAddons(manager, param) {
      if (param) {
        setAndLockPref("extensions.pocket.enabled", false);
      }
    }
  },

208
209
210
211
212
213
214
215
216
217
  "DisablePrivateBrowsing": {
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("privatebrowsing");
        manager.disallowFeature("about:privatebrowsing", true);
        setAndLockPref("browser.privatebrowsing.autostart", false);
      }
    }
  },

218
219
220
221
222
223
224
225
  "DisableSafeMode": {
    onBeforeUIStartup(manager, param) {
      if (param) {
        manager.disallowFeature("safeMode");
      }
    }
  },

226
227
228
229
230
231
232
233
  "DisableSysAddonUpdate": {
    onBeforeAddons(manager, param) {
      if (param) {
        manager.disallowFeature("SysAddonUpdate");
      }
    }
  },

234
  "DisplayBookmarksToolbar": {
235
    onBeforeUIStartup(manager, param) {
236
237
238
239
      if (param) {
        // 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.
240
        runOnce("displayBookmarksToolbar", () => {
241
          gXulStore.setValue(BROWSER_DOCUMENT_URL, "PersonalToolbar", "collapsed", "false");
242
        });
243
      }
244
    }
245
246
  },

247
  "DisplayMenuBar": {
248
    onBeforeUIStartup(manager, param) {
249
250
251
252
      if (param) {
        // 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
        // menu bar, do not show it again.
253
        runOnce("displayMenuBar", () => {
254
          gXulStore.setValue(BROWSER_DOCUMENT_URL, "toolbar-menubar", "autohide", "false");
255
        });
256
      }
257
258
259
    }
  },

260
  "DontCheckDefaultBrowser": {
261
    onBeforeUIStartup(manager, param) {
262
      setAndLockPref("browser.shell.checkDefaultBrowser", false);
263
264
265
    }
  },

266
267
268
269
270
271
272
273
274
275
276
277
  "EnableTrackingProtection": {
    onBeforeUIStartup(manager, param) {
      if (param.Locked) {
        setAndLockPref("privacy.trackingprotection.enabled", param.Value);
        setAndLockPref("privacy.trackingprotection.pbmode.enabled", param.Value);
      } else {
        setDefaultPref("privacy.trackingprotection.enabled", param.Value);
        setDefaultPref("privacy.trackingprotection.pbmode.enabled", param.Value);
      }
    }
  },

278
  "FlashPlugin": {
279
    onBeforeUIStartup(manager, param) {
280
      addAllowDenyPermissions("plugin:flash", param.Allow, param.Block);
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
  "Homepage": {
    onBeforeUIStartup(manager, param) {
      // |homepages| will be a string containing a pipe-separated ('|') list of
      // URLs because that is what the "Home page" section of about:preferences
      // (and therefore what the pref |browser.startup.homepage|) accepts.
      let homepages = param.URL.spec;
      if (param.Additional && param.Additional.length > 0) {
        homepages += "|" + param.Additional.map(url => url.spec).join("|");
      }
      if (param.Locked) {
        setAndLockPref("browser.startup.homepage", homepages);
        setAndLockPref("browser.startup.page", 1);
        setAndLockPref("pref.browser.homepage.disable_button.current_page", true);
        setAndLockPref("pref.browser.homepage.disable_button.bookmark_page", true);
        setAndLockPref("pref.browser.homepage.disable_button.restore_default", true);
      } else {
        runOncePerModification("setHomepage", homepages, () => {
          Services.prefs.setStringPref("browser.startup.homepage", homepages);
          Services.prefs.setIntPref("browser.startup.page", 1);
        });
      }
    }
  },

308
  "InstallAddons": {
309
    onBeforeUIStartup(manager, param) {
310
      addAllowDenyPermissions("install", param.Allow, null);
311
312
313
    }
  },

314
315
316
317
318
319
320
321
  "NoDefaultBookmarks": {
    onProfileAfterChange(manager, param) {
      if (param) {
        manager.disallowFeature("defaultBookmarks");
      }
    }
  },

322
  "Popups": {
323
    onBeforeUIStartup(manager, param) {
324
      addAllowDenyPermissions("popup", param.Allow, null);
325
326
327
328
329
330
    }
  },

  "RememberPasswords": {
    onBeforeUIStartup(manager, param) {
      setAndLockPref("signon.rememberSignons", param);
331
332
    }
  },
333
};
334
335
336
337
338
339
340
341
342

/*
 * ====================
 * = HELPER FUNCTIONS =
 * ====================
 *
 * The functions below are helpers to be used by several policies.
 */

343
344
345
346
347
348
349
350
351
352
353
354
355
/**
 * setAndLockPref
 *
 * Sets the _default_ value of a pref, and locks it (meaning that
 * the default value will always be returned, independent from what
 * is stored as the user value).
 * The value is only changed in memory, and not stored to disk.
 *
 * @param {string} prefName
 *        The pref to be changed
 * @param {boolean,number,string} prefValue
 *        The value to set and lock
 */
356
357
358
359
360
function setAndLockPref(prefName, prefValue) {
  if (Services.prefs.prefIsLocked(prefName)) {
    Services.prefs.unlockPref(prefName);
  }

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  setDefaultPref(prefName, prefValue);

  Services.prefs.lockPref(prefName);
}

/**
 * setDefaultPref
 *
 * Sets the _default_ value of a pref.
 * The value is only changed in memory, and not stored to disk.
 *
 * @param {string} prefName
 *        The pref to be changed
 * @param {boolean,number,string} prefValue
 *        The value to set
 */
function setDefaultPref(prefName, prefValue) {
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  let defaults = Services.prefs.getDefaultBranch("");

  switch (typeof(prefValue)) {
    case "boolean":
      defaults.setBoolPref(prefName, prefValue);
      break;

    case "number":
      if (!Number.isInteger(prefValue)) {
        throw new Error(`Non-integer value for ${prefName}`);
      }

      defaults.setIntPref(prefName, prefValue);
      break;

    case "string":
      defaults.setStringPref(prefName, prefValue);
      break;
  }
}
398

399
400
401
402
403
404
405
406
407
408
409
410
411
/**
 * addAllowDenyPermissions
 *
 * Helper function to call the permissions manager (Services.perms.add)
 * for two arrays of URLs.
 *
 * @param {string} permissionName
 *        The name of the permission to change
 * @param {array} allowList
 *        The list of URLs to be set as ALLOW_ACTION for the chosen permission.
 * @param {array} blockList
 *        The list of URLs to be set as DENY_ACTION for the chosen permission.
 */
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
function addAllowDenyPermissions(permissionName, allowList, blockList) {
  allowList = allowList || [];
  blockList = blockList || [];

  for (let origin of allowList) {
    Services.perms.add(origin,
                       permissionName,
                       Ci.nsIPermissionManager.ALLOW_ACTION,
                       Ci.nsIPermissionManager.EXPIRE_POLICY);
  }

  for (let origin of blockList) {
    Services.perms.add(origin,
                       permissionName,
                       Ci.nsIPermissionManager.DENY_ACTION,
                       Ci.nsIPermissionManager.EXPIRE_POLICY);
  }
}
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

/**
 * runOnce
 *
 * Helper function to run a callback only once per policy.
 *
 * @param {string} actionName
 *        A given name which will be used to track if this callback has run.
 * @param {Functon} callback
 *        The callback to run only once.
 */
function runOnce(actionName, callback) {
  let prefName = `browser.policies.runonce.${actionName}`;
  if (Services.prefs.getBoolPref(prefName, false)) {
    log.debug(`Not running action ${actionName} again because it has already run.`);
    return;
  }
  Services.prefs.setBoolPref(prefName, true);
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
  callback();
}

/**
 * runOncePerModification
 *
 * Helper function similar to runOnce. The difference is that runOnce runs the
 * callback once when the policy is set, then never again.
 * runOncePerModification runs the callback once each time the policy value
 * changes from its previous value.
 *
 * @param {string} actionName
 *        A given name which will be used to track if this callback has run.
 *        This string will be part of a pref name.
 * @param {string} policyValue
 *        The current value of the policy. This will be compared to previous
 *        values given to this function to determine if the policy value has
 *        changed. Regardless of the data type of the policy, this must be a
 *        string.
 * @param {Function} callback
 *        The callback to be run when the pref value changes
 */
function runOncePerModification(actionName, policyValue, callback) {
  let prefName = `browser.policies.runOncePerModification.${actionName}`;
  let oldPolicyValue = Services.prefs.getStringPref(prefName, undefined);
  if (policyValue === oldPolicyValue) {
    log.debug(`Not running action ${actionName} again because the policy's value is unchanged`);
    return;
  }
  Services.prefs.setStringPref(prefName, policyValue);
  callback();
479
}