Browse Source

fix: save JSON.* functions at start

#1440
tophf 4 years ago
parent
commit
1500eccd75

+ 1 - 1
src/injected/web/gm-values.js

@@ -6,7 +6,7 @@ export const changeHooks = createNullObj();
 
 const dataDecoders = {
   __proto__: null,
-  o: SafeJSON.parse,
+  o: jsonParse,
   n: val => +val,
   b: val => val === 'true',
 };

+ 1 - 1
src/injected/web/requests.js

@@ -31,7 +31,7 @@ function parseData(req, msg) {
   let res = req.raw;
   switch (req.opts.responseType) {
   case 'json':
-    res = SafeJSON.parse(res);
+    res = jsonParse(res);
     break;
   case 'document':
     res = new SafeDOMParser()::parseFromString(res, getContentType(msg) || 'text/html');

+ 7 - 1
src/injected/web/safe-globals-web.js

@@ -13,7 +13,6 @@ export let
   SafeError,
   SafeEventTarget,
   SafeFileReader,
-  SafeJSON,
   SafeKeyboardEvent,
   SafeMouseEvent,
   Object,
@@ -59,6 +58,8 @@ export let
   arrayIsArray,
   createObjectURL,
   funcToString,
+  jsonParse,
+  jsonStringify,
   logging,
   mathRandom,
   parseFromString, // DOMParser
@@ -82,6 +83,7 @@ export const VAULT = (() => {
   let ArrayP;
   let ElementP;
   let SafeObject;
+  let SafeJSON;
   let StringP;
   let i = -1;
   let call;
@@ -168,5 +170,9 @@ export const VAULT = (() => {
     getOwnPropertyNames(srcWindow),
     src !== srcWindow && getOwnPropertyNames(src),
   ];
+  /* Exporting the functions separately instead of exporting SafeJSON as its props may be broken
+   * by the page if it gains access to any other object from the vault e.g. a thrown SafeError. */
+  jsonParse = SafeJSON.parse;
+  jsonStringify = SafeJSON.stringify;
   return res;
 })();

+ 2 - 2
src/injected/web/util-web.js

@@ -40,14 +40,14 @@ export const jsonDump = (value, stack) => {
         const v = jsonDump(value[key], stack);
         // JSON.stringify skips keys with `undefined` or incompatible values
         if (v !== undefined) {
-          res += `${res.length > 1 ? ',' : ''}${jsonDump(key)}:${v}`;
+          res += `${res.length > 1 ? ',' : ''}${jsonStringify(key)}:${v}`;
         }
       });
       res += '}';
     }
     stack.length -= 1;
   } else if (value !== undefined) {
-    res = SafeJSON.stringify(value);
+    res = jsonStringify(value);
   }
   return res;
 };