Bug 1433271 - Don't fail policy validation if an array inside a object is missing. r=Mossop
authorFelipe Gomes <felipc@gmail.com>
Wed, 07 Feb 2018 20:37:55 -0200
changeset 402876 8130dd2083937aeb982f35bde2816013351fad3a
parent 402875 0b156d8a695ffc557fad0c49c47840aeeed5c316
child 402877 bbcf611ca8b93467a5fcee60be2589d5f6f9af88
push id33405
push usershindli@mozilla.com
push dateThu, 08 Feb 2018 10:04:47 +0000
treeherdermozilla-central@0ac953fcddf1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMossop
bugs1433271
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 1433271 - Don't fail policy validation if an array inside a object is missing. r=Mossop A top-level array (i.e., if a policy directly requires an array) won't be affected by this problem, because if the array is missing, that means that that policy is not present, so it will be ignored. However, this can affect an array that is expected inside another object, for more complex policy types (like the popups permission which accepts both an 'allow' and a 'block' array of URLs. In the future, we should implement the 'required' property as defined by the JSON-Schema standard, but there's not a strong use case for it yet, so let's do the simple solution for now MozReview-Commit-ID: 4MTBTsPYlX8
browser/components/enterprisepolicies/PoliciesValidator.jsm
browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
--- a/browser/components/enterprisepolicies/PoliciesValidator.jsm
+++ b/browser/components/enterprisepolicies/PoliciesValidator.jsm
@@ -42,16 +42,24 @@ function validateAndParseParamRecursive(
     case "number":
     case "integer":
     case "string":
     case "URL":
     case "origin":
       return validateAndParseSimpleParam(param, properties.type);
 
     case "array":
+      if (param === undefined) {
+        // Accept a missing array as valid. Policies that use
+        // arrays should still check before iterating through
+        // the array, unless it is marked as "required" in the
+        // schema (but "required" is not implemented yet).
+        return [true, undefined];
+      }
+
       if (!Array.isArray(param)) {
         log.error("Array expected but not received");
         return [false, null];
       }
 
       let parsedArray = [];
       for (let item of param) {
         log.debug(`in array, checking @${item}@ for type ${properties.items.type}`);
--- a/browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
+++ b/browser/components/enterprisepolicies/tests/browser/browser_policies_validate_and_parse_API.js
@@ -230,8 +230,39 @@ add_task(async function test_array_of_ob
   ok(typeof(parsed[0]) == "object" && typeof(parsed[1]) == "object", "Correct objects inside array");
 
   is(parsed[0].url.spec, "https://www.example.com/bookmark1", "Correct URL for bookmark 1");
   is(parsed[1].url.spec, "https://www.example.com/bookmark2", "Correct URL for bookmark 2");
 
   is(parsed[0].title, "Foo", "Correct title for bookmark 1");
   is(parsed[1].title, "Bar", "Correct title for bookmark 2");
 });
+
+add_task(async function test_missing_arrays_inside_objects() {
+  let schema = {
+    type: "object",
+    properties: {
+      allow: {
+        type: "array",
+        items: {
+          type: "boolean"
+        }
+      },
+      block: {
+        type: "array",
+        items: {
+          type: "boolean"
+        }
+      }
+
+    }
+  };
+
+  let valid, parsed;
+  [valid, parsed] = PoliciesValidator.validateAndParseParameters({
+    allow: [true, true, true]
+  }, schema);
+
+  ok(valid, "Object is valid");
+  is(parsed.allow.length, 3, "Allow array is correct.");
+  is(parsed.block, undefined, "Block array is undefined, as expected.");
+});
+