Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope. r=kmag
authorLuca Greco <lgreco@mozilla.com>
Mon, 28 Nov 2016 20:11:06 +0100
changeset 325155 f463014033520214bc213683fcb086ee3848cd30
parent 325154 30afe7e8fb7115df44796764ea85d784797e135a
child 325156 a8e20bf36959426f0a6eb4b5df29ef3dd85ad4bd
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerskmag
bugs1320181
milestone53.0a1
Bug 1320181 - Storage sanitizer should return an empty object belonging to the correct scope. r=kmag MozReview-Commit-ID: Bx95Cgx0EuH
toolkit/components/extensions/ExtensionStorage.jsm
--- a/toolkit/components/extensions/ExtensionStorage.jsm
+++ b/toolkit/components/extensions/ExtensionStorage.jsm
@@ -13,17 +13,30 @@ const Cr = Components.results;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "OS",
                                   "resource://gre/modules/osfile.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown",
                                   "resource://gre/modules/AsyncShutdown.jsm");
 
-function jsonReplacer(key, value) {
+/**
+ * Helper function used to sanitize the objects that have to be saved in the ExtensionStorage.
+ *
+ * @param {BaseContext} context
+ *   The current extension context.
+ * @param {string} key
+ *   The key of the current JSON property.
+ * @param {any} value
+ *   The value of the current JSON property.
+ *
+ * @returns {any}
+ *   The sanitized value of the property.
+ */
+function jsonReplacer(context, key, value) {
   switch (typeof(value)) {
     // Serialize primitive types as-is.
     case "string":
     case "number":
     case "boolean":
       return value;
 
     case "object":
@@ -44,17 +57,17 @@ function jsonReplacer(key, value) {
           return String(value);
       }
       break;
   }
 
   if (!key) {
     // If this is the root object, and we can't serialize it, serialize
     // the value to an empty object.
-    return {};
+    return new context.cloneScope.Object();
   }
 
   // Everything else, omit entirely.
   return undefined;
 }
 
 this.ExtensionStorage = {
   cache: new Map(),
@@ -67,17 +80,17 @@ this.ExtensionStorage = {
    * @param {value} value
    *        The value to sanitize.
    * @param {Context} context
    *        The extension context in which to sanitize the value
    * @returns {value}
    *        The sanitized value.
    */
   sanitize(value, context) {
-    let json = context.jsonStringify(value, jsonReplacer);
+    let json = context.jsonStringify(value, jsonReplacer.bind(null, context));
     return JSON.parse(json);
   },
 
   getExtensionDir(extensionId) {
     return OS.Path.join(this.extensionDir, extensionId);
   },
 
   getStorageFile(extensionId) {