Bug 1307577 - Switch to `createCodebasePrincipal` in `PushRecord`. r=mt
authorKit Cambridge <kit@yakshaving.ninja>
Thu, 20 Oct 2016 10:29:58 -0700
changeset 428116 8f1273f17e6f0665d1ae0e8fff595bec69194b62
parent 428115 6d552b35b4793b033eb745eefb4ea32607c62501
child 428117 fc577423948f36ef86955ebac4a8e468ddd3a8ab
push id33225
push userbmo:dmu@mozilla.com
push dateFri, 21 Oct 2016 15:19:23 +0000
reviewersmt
bugs1307577
milestone52.0a1
Bug 1307577 - Switch to `createCodebasePrincipal` in `PushRecord`. r=mt `createCodebasePrincipalFromOrigin` mangles the origin after extracting the suffix. The proper long-term fix is to serialize and store the original principal, instead of reconstructing a new codebase principal from the scope and origin suffix. MozReview-Commit-ID: bS2lRx3n7j
dom/push/PushRecord.jsm
dom/push/test/xpcshell/test_record.js
--- a/dom/push/PushRecord.jsm
+++ b/dom/push/PushRecord.jsm
@@ -291,22 +291,22 @@ var principals = new WeakMap();
 Object.defineProperties(PushRecord.prototype, {
   principal: {
     get() {
       if (this.systemRecord) {
         return Services.scriptSecurityManager.getSystemPrincipal();
       }
       let principal = principals.get(this);
       if (!principal) {
-        let url = this.scope;
-        if (this.originAttributes) {
-          // Allow tests to omit origin attributes.
-          url += this.originAttributes;
-        }
-        principal = Services.scriptSecurityManager.createCodebasePrincipalFromOrigin(url);
+        let uri = Services.io.newURI(this.scope, null, null);
+        // Allow tests to omit origin attributes.
+        let originSuffix = this.originAttributes || "";
+        let originAttributes =
+        principal = Services.scriptSecurityManager.createCodebasePrincipal(uri,
+          ChromeUtils.createOriginAttributesFromOrigin(originSuffix));
         principals.set(this, principal);
       }
       return principal;
     },
     configurable: true,
   },
 
   uri: {
--- a/dom/push/test/xpcshell/test_record.js
+++ b/dom/push/test/xpcshell/test_record.js
@@ -44,10 +44,50 @@ add_task(function* test_systemRecord_upd
   systemRecord.updateQuota(Date.now() - 3 * MS_IN_ONE_DAY);
   equal(systemRecord.quota, Infinity,
     'System subscriptions should ignore quota updates');
   systemRecord.updateQuota(-1);
   equal(systemRecord.quota, Infinity,
     'System subscriptions should ignore the last visit time');
   systemRecord.reduceQuota();
   equal(systemRecord.quota, Infinity,
-    'SYstem subscriptions should ignore quota reductions');
+    'System subscriptions should ignore quota reductions');
 });
+
+function testPermissionCheck(props) {
+  let record = new PushRecord(props);
+  equal(record.uri.spec, props.scope,
+    `Record URI should match scope URL for ${JSON.stringify(props)}`);
+  if (props.originAttributes) {
+    let originSuffix = ChromeUtils.originAttributesToSuffix(
+      record.principal.originAttributes);
+    equal(originSuffix, props.originAttributes,
+      `Origin suffixes should match for ${JSON.stringify(props)}`);
+  }
+  ok(!record.hasPermission(), `Record ${
+    JSON.stringify(props)} should not have permission yet`);
+  let permURI = Services.io.newURI(props.scope, null, null);
+  Services.perms.add(permURI, 'desktop-notification',
+                     Ci.nsIPermissionManager.ALLOW_ACTION);
+  try {
+    ok(record.hasPermission(), `Record ${
+      JSON.stringify(props)} should have permission`);
+  } finally {
+    Services.perms.remove(permURI, 'desktop-notification');
+  }
+}
+
+add_task(function* test_principal_permissions() {
+  let testProps = [{
+    scope: 'https://example.com/',
+  }, {
+    scope: 'https://example.com/',
+    originAttributes: '^userContextId=1',
+  }, {
+    scope: 'https://блог.фанфрог.рф/',
+  }, {
+    scope: 'https://блог.фанфрог.рф/',
+    originAttributes: '^userContextId=1',
+  }];
+  for (let props of testProps) {
+    testPermissionCheck(props);
+  }
+});