Bug 1073359 - reinstate PermissionUtils and XPIProvider which were removed in bug 1050080. r=MattN

This commit is contained in:
Mark Hammond 2014-09-26 15:54:34 +10:00
parent 3eaf85b2dd
commit 197190035c
8 changed files with 319 additions and 0 deletions

View File

@ -0,0 +1,87 @@
// 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/.
this.EXPORTED_SYMBOLS = ["PermissionsUtils"];
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
Cu.import("resource://gre/modules/Services.jsm");
let gImportedPrefBranches = new Set();
function importPrefBranch(aPrefBranch, aPermission, aAction) {
let list = Services.prefs.getChildList(aPrefBranch, {});
for (let pref of list) {
let hosts = "";
try {
hosts = Services.prefs.getCharPref(pref);
} catch (e) {}
if (!hosts)
continue;
hosts = hosts.split(",");
for (let host of hosts) {
let uri = null;
try {
uri = Services.io.newURI("http://" + host, null, null);
} catch (e) {
try {
uri = Services.io.newURI(host, null, null);
} catch (e2) {}
}
try {
Services.perms.add(uri, aPermission, aAction);
} catch (e) {}
}
Services.prefs.setCharPref(pref, "");
}
}
this.PermissionsUtils = {
/**
* Import permissions from perferences to the Permissions Manager. After being
* imported, all processed permissions will be set to an empty string.
* Perferences are only processed once during the application's
* lifetime - it's safe to call this multiple times without worrying about
* doing unnecessary work, as the preferences branch will only be processed
* the first time.
*
* @param aPrefBranch Preferences branch to import from. The preferences
* under this branch can specify whitelist (ALLOW_ACTION)
* or blacklist (DENY_ACTION) additions using perference
* names of the form:
* * <BRANCH>.whitelist.add.<ID>
* * <BRANCH>.blacklist.add.<ID>
* Where <ID> can be any valid preference name.
* The value is expected to be a comma separated list of
* host named. eg:
* * something.example.com
* * foo.exmaple.com,bar.example.com
*
* @param aPermission Permission name to be passsed to the Permissions
* Manager.
*/
importFromPrefs: function(aPrefBranch, aPermission) {
if (!aPrefBranch.endsWith("."))
aPrefBranch += ".";
// Ensure we only import this pref branch once.
if (gImportedPrefBranches.has(aPrefBranch))
return;
importPrefBranch(aPrefBranch + "whitelist.add", aPermission,
Services.perms.ALLOW_ACTION);
importPrefBranch(aPrefBranch + "blacklist.add", aPermission,
Services.perms.DENY_ACTION);
gImportedPrefBranches.add(aPrefBranch);
}
};

View File

@ -30,6 +30,7 @@ EXTRA_JS_MODULES += [
'Log.jsm',
'NewTabUtils.jsm',
'PageMenu.jsm',
'PermissionsUtils.jsm',
'PopupNotifications.jsm',
'Preferences.jsm',
'PrivateBrowsingUtils.jsm',

View File

@ -0,0 +1,67 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Tests that PerrmissionsUtils.jsm works as expected, including:
// * PermissionsUtils.importfromPrefs()
// <ROOT>.[whitelist|blacklist].add preferences are emptied when
// converted into permissions on startup.
const PREF_ROOT = "testpermissions.";
const TEST_PERM = "test-permission";
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/PermissionsUtils.jsm");
function run_test() {
test_importfromPrefs();
}
function test_importfromPrefs() {
// Create own preferences to test
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY", "");
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.EMPTY2", ",");
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST", "whitelist.example.com");
Services.prefs.setCharPref(PREF_ROOT + "whitelist.add.TEST2", "whitelist2-1.example.com,whitelist2-2.example.com,about:home");
Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.EMPTY", "");
Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST", "blacklist.example.com,");
Services.prefs.setCharPref(PREF_ROOT + "blacklist.add.TEST2", ",blacklist2-1.example.com,blacklist2-2.example.com,about:mozilla");
// Check they are unknown in the permission manager prior to importing.
let whitelisted = ["http://whitelist.example.com",
"http://whitelist2-1.example.com",
"http://whitelist2-2.example.com",
"about:home"];
let blacklisted = ["http://blacklist.example.com",
"http://blacklist2-1.example.com",
"http://blacklist2-2.example.com",
"about:mozilla"];
let unknown = whitelisted.concat(blacklisted);
for (let url of unknown) {
let uri = Services.io.newURI(url, null, null);
do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.UNKNOWN_ACTION);
}
// Import them
PermissionsUtils.importFromPrefs(PREF_ROOT, TEST_PERM);
// Get list of preferences to check
let preferences = Services.prefs.getChildList(PREF_ROOT, {});
// Check preferences were emptied
for (let pref of preferences) {
do_check_eq(Services.prefs.getCharPref(pref), "");
}
// Check they were imported into the permissions manager
for (let url of whitelisted) {
let uri = Services.io.newURI(url, null, null);
do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.ALLOW_ACTION);
}
for (let url of blacklisted) {
let uri = Services.io.newURI(url, null, null);
do_check_eq(Services.perms.testPermission(uri, TEST_PERM), Services.perms.DENY_ACTION);
}
}

View File

@ -18,6 +18,7 @@ skip-if = os == 'android'
[test_Http.js]
[test_Log.js]
[test_NewTabUtils.js]
[test_PermissionsUtils.js]
[test_Preferences.js]
[test_Promise.js]
[test_propertyListsUtils.js]

View File

@ -28,6 +28,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "ZipUtils",
"resource://gre/modules/ZipUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PermissionsUtils",
"resource://gre/modules/PermissionsUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
@ -76,6 +78,7 @@ const PREF_XPI_ENABLED = "xpinstall.enabled";
const PREF_XPI_WHITELIST_REQUIRED = "xpinstall.whitelist.required";
const PREF_XPI_DIRECT_WHITELISTED = "xpinstall.whitelist.directRequest";
const PREF_XPI_FILE_WHITELISTED = "xpinstall.whitelist.fileRequest";
const PREF_XPI_PERMISSIONS_BRANCH = "xpinstall.";
const PREF_XPI_UNPACK = "extensions.alwaysUnpack";
const PREF_INSTALL_REQUIREBUILTINCERTS = "extensions.install.requireBuiltInCerts";
const PREF_INSTALL_DISTRO_ADDONS = "extensions.installDistroAddons";
@ -3460,6 +3463,15 @@ this.XPIProvider = {
return changed;
},
/**
* Imports the xpinstall permissions from preferences into the permissions
* manager for the user to change later.
*/
importPermissions: function XPI_importPermissions() {
PermissionsUtils.importFromPrefs(PREF_XPI_PERMISSIONS_BRANCH,
XPI_PERMISSION);
},
/**
* Checks for any changes that have occurred since the last time the
* application was launched.
@ -3710,6 +3722,8 @@ this.XPIProvider = {
(aUri.schemeIs("chrome") || aUri.schemeIs("file")))
return true;
this.importPermissions();
let permission = Services.perms.testPermission(aUri, XPI_PERMISSION);
if (permission == Ci.nsIPermissionManager.DENY_ACTION)
return false;

View File

@ -0,0 +1,76 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
Components.utils.import("resource://gre/modules/NetUtil.jsm");
// Checks that permissions set in preferences are correctly imported but can
// be removed by the user.
const XPI_MIMETYPE = "application/x-xpinstall";
function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "2", "2");
Services.prefs.setCharPref("xpinstall.whitelist.add", "test1.com,test2.com");
Services.prefs.setCharPref("xpinstall.whitelist.add.36", "test3.com,www.test4.com");
Services.prefs.setCharPref("xpinstall.whitelist.add.test5", "test5.com");
Services.perms.add(NetUtil.newURI("http://www.test9.com"), "install",
AM_Ci.nsIPermissionManager.ALLOW_ACTION);
startupManager();
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://test1.com")));
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test2.com")));
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://test3.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://test4.com")));
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test4.com")));
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test5.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test6.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://test7.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test8.com")));
// This should remain unaffected
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test9.com")));
do_check_true(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test9.com")));
Services.perms.removeAll();
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://test1.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test2.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://test3.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test4.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test5.com")));
// Upgrade the application and verify that the permissions are still not there
restartManager("2");
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://test1.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test2.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://test3.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("https://www.test4.com")));
do_check_false(AddonManager.isInstallAllowed(XPI_MIMETYPE,
NetUtil.newURI("http://www.test5.com")));
}

View File

@ -0,0 +1,71 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
// Tests that xpinstall.[whitelist|blacklist].add preferences are emptied when
// converted into permissions.
const PREF_XPI_WHITELIST_PERMISSIONS = "xpinstall.whitelist.add";
const PREF_XPI_BLACKLIST_PERMISSIONS = "xpinstall.blacklist.add";
function do_check_permission_prefs(preferences) {
// Check preferences were emptied
for (let pref of preferences) {
try {
do_check_eq(Services.prefs.getCharPref(pref), "");
}
catch (e) {
// Successfully emptied
}
}
}
function clear_imported_preferences_cache() {
let scope = Components.utils.import("resource://gre/modules/PermissionsUtils.jsm", {});
scope.gImportedPrefBranches.clear();
}
function run_test() {
createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9");
// Create own preferences to test
Services.prefs.setCharPref("xpinstall.whitelist.add.EMPTY", "");
Services.prefs.setCharPref("xpinstall.whitelist.add.TEST", "whitelist.example.com");
Services.prefs.setCharPref("xpinstall.blacklist.add.EMPTY", "");
Services.prefs.setCharPref("xpinstall.blacklist.add.TEST", "blacklist.example.com");
// Get list of preferences to check
var whitelistPreferences = Services.prefs.getChildList(PREF_XPI_WHITELIST_PERMISSIONS, {});
var blacklistPreferences = Services.prefs.getChildList(PREF_XPI_BLACKLIST_PERMISSIONS, {});
var preferences = whitelistPreferences.concat(blacklistPreferences);
startupManager();
// Permissions are imported lazily - act as thought we're checking an install,
// to trigger on-deman importing of the permissions.
let url = Services.io.newURI("http://example.com/file.xpi", null, null);
AddonManager.isInstallAllowed("application/x-xpinstall", url);
do_check_permission_prefs(preferences);
// Import can also be triggerred by an observer notification by any other area
// of code, such as a permissions management UI.
// First, request to flush all permissions
clear_imported_preferences_cache();
Services.prefs.setCharPref("xpinstall.whitelist.add.TEST2", "whitelist2.example.com");
Services.obs.notifyObservers(null, "flush-pending-permissions", "install");
do_check_permission_prefs(preferences);
// Then, request to flush just install permissions
clear_imported_preferences_cache();
Services.prefs.setCharPref("xpinstall.whitelist.add.TEST3", "whitelist3.example.com");
Services.obs.notifyObservers(null, "flush-pending-permissions", "");
do_check_permission_prefs(preferences);
// And a request to flush some other permissions sholdn't flush install permissions
clear_imported_preferences_cache();
Services.prefs.setCharPref("xpinstall.whitelist.add.TEST4", "whitelist4.example.com");
Services.obs.notifyObservers(null, "flush-pending-permissions", "lolcats");
do_check_eq(Services.prefs.getCharPref("xpinstall.whitelist.add.TEST4"), "whitelist4.example.com");
}

View File

@ -219,6 +219,8 @@ requesttimeoutfactor = 2
[test_migrate_max_version.js]
[test_no_addons.js]
[test_onPropertyChanged_appDisabled.js]
[test_permissions.js]
[test_permissions_prefs.js]
[test_plugins.js]
skip-if = buildapp == "mulet"
[test_pluginchange.js]