Bug 1440578 - Policy: "block" under "cookies" should clear already-stored cookies. r=felipc, r=jdm
☠☠ backed out by b18dbffb418d ☠ ☠
authorYUKI Piro Hiroshi <yuki@clear-code.com>
Mon, 05 Mar 2018 19:36:00 -0500
changeset 407061 5521f4a9012d
parent 407060 18336fd064bd
child 407109 b7f24d48cd55
push id100576
push userryanvm@gmail.com
push dateThu, 08 Mar 2018 03:34:16 +0000
treeherdermozilla-inbound@5521f4a9012d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfelipc, jdm
bugs1440578
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1440578 - Policy: "block" under "cookies" should clear already-stored cookies. r=felipc, r=jdm
browser/components/enterprisepolicies/Policies.jsm
browser/components/enterprisepolicies/tests/browser/browser.ini
browser/components/enterprisepolicies/tests/browser/browser_policy_blocked_cookies.js
--- a/browser/components/enterprisepolicies/Policies.jsm
+++ b/browser/components/enterprisepolicies/Policies.jsm
@@ -102,16 +102,25 @@ var Policies = {
     onAllWindowsRestored(manager, param) {
       BookmarksPolicies.processBookmarks(param);
     }
   },
 
   "Cookies": {
     onBeforeUIStartup(manager, param) {
       addAllowDenyPermissions("cookie", param.Allow, param.Block);
+
+      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);
+          }
+        });
+      }
     }
   },
 
   "CreateMasterPassword": {
     onBeforeUIStartup(manager, param) {
       if (!param) {
         manager.disallowFeature("createMasterPassword");
       }
--- a/browser/components/enterprisepolicies/tests/browser/browser.ini
+++ b/browser/components/enterprisepolicies/tests/browser/browser.ini
@@ -16,16 +16,17 @@ support-files =
 [browser_policies_sorted_alphabetically.js]
 [browser_policies_validate_and_parse_API.js]
 [browser_policy_app_update.js]
 [browser_policy_block_about_addons.js]
 [browser_policy_block_about_config.js]
 [browser_policy_block_about_profiles.js]
 [browser_policy_block_about_support.js]
 [browser_policy_block_set_desktop_background.js]
+[browser_policy_blocked_cookies.js]
 [browser_policy_bookmarks.js]
 [browser_policy_default_browser_check.js]
 [browser_policy_disable_formhistory.js]
 [browser_policy_disable_fxaccounts.js]
 [browser_policy_disable_fxscreenshots.js]
 [browser_policy_disable_masterpassword.js]
 [browser_policy_disable_pocket.js]
 [browser_policy_disable_privatebrowsing.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policy_blocked_cookies.js
@@ -0,0 +1,62 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+"use strict";
+
+
+add_task(async function setup() {
+  const expiry = Date.now() + 24 * 60 * 60;
+  Services.cookies.add("example.com", "/", "secure", "true", true, false, false, expiry, {});
+  Services.cookies.add("example.com", "/", "insecure", "true", false, false, false, expiry, {});
+  Services.cookies.add("example.org", "/", "secure", "true", true, false, false, expiry, {});
+  Services.cookies.add("example.org", "/", "insecure", "true", false, false, false, expiry, {});
+  Services.cookies.add("example.net", "/", "secure", "true", true, false, false, expiry, {});
+  await setupPolicyEngineWithJson({
+    "policies": {
+      "Cookies": {
+        "Block": [
+          "http://example.com",
+          "https://example.org:8080"
+        ]
+      }
+    }
+  });
+});
+
+function retrieve_all_cookies(host) {
+  const values = [];
+  const cookies = Services.cookies.getCookiesFromHost(host, {});
+  while (cookies.hasMoreElements()) {
+    const cookie = cookies.getNext().QueryInterface(Ci.nsICookie);
+    values.push({
+      host: cookie.host,
+      name: cookie.name,
+      path: cookie.path
+    });
+  }
+  return values;
+}
+
+add_task(async function test_cookies_for_blocked_sites_cleared() {
+  const cookies = {
+    hostname: retrieve_all_cookies("example.com"),
+    origin: retrieve_all_cookies("example.org"),
+    keep: retrieve_all_cookies("example.net")
+  };
+  const expected = {
+    hostname: [],
+    origin: [],
+    keep: [
+      {host: "example.net",
+       name: "secure",
+       path: "/"}
+    ]
+  };
+  is(JSON.stringify(cookies), JSON.stringify(expected),
+     "All stored cookies for blocked hosts should be cleared");
+});
+
+add_task(function teardown() {
+  for (let host of ["example.com", "example.org", "example.net"]) {
+    Services.cookies.removeCookiesWithOriginAttributes("{}", host);
+  }
+});
\ No newline at end of file