Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(534)

Unified Diff: ios/web/web_state/js/resources/common.js

Issue 2807213003: Move stringify, form, navigation and scroll methods out of core.js. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/web/web_state/js/resources/common.js
diff --git a/ios/web/web_state/js/resources/common.js b/ios/web/web_state/js/resources/common.js
index 56adb250ff7019be9cc1ec763fcc2b014c187381..d2f826e7bb0f943babf236878b27374cd8800cf1 100644
--- a/ios/web/web_state/js/resources/common.js
+++ b/ios/web/web_state/js/resources/common.js
@@ -26,6 +26,7 @@ __gCrWeb['common'] = __gCrWeb.common;
/* Beginning of anonymous object. */
(function() {
+
/**
* JSON safe object to protect against custom implementation of Object.toJSON
* in host pages.
@@ -46,6 +47,31 @@ __gCrWeb['common'] = __gCrWeb.common;
__gCrWeb.common.JSONStringify = JSON.stringify;
/**
+ * Returns a string that is formatted according to the JSON syntax rules.
+ * This is equivalent to the built-in JSON.stringify() function, but is
+ * less likely to be overridden by the website itself. This public function
+ * should not be used if spoofing it would create a security vulnerability.
Eugene But (OOO till 7-30) 2017/04/10 21:42:07 How about s/if spoofing it would create a security
danyao 2017/04/10 22:12:59 Did you perhaps mean {@code JSONStringify}? Chang
Eugene But (OOO till 7-30) 2017/04/11 00:12:44 {@code JSONStringify} lg. I just was not sure what
danyao 2017/04/11 15:10:39 Updated. There may be something weird going on wit
+ * The |__gCrWeb| object itself does not use it; it uses its private
+ * counterpart instead.
+ * Prevents websites from changing stringify's behavior by adding the
+ * method toJSON() by temporarily removing it.
+ */
+ __gCrWeb['stringify'] = function(value) {
+ if (value === null)
+ return 'null';
+ if (value === undefined)
+ return 'undefined';
+ if (typeof(value.toJSON) == 'function') {
+ var originalToJSON = value.toJSON;
+ value.toJSON = undefined;
+ var stringifiedValue = __gCrWeb.common.JSONStringify(value);
+ value.toJSON = originalToJSON;
+ return stringifiedValue;
+ }
+ return __gCrWeb.common.JSONStringify(value);
+ };
+
+ /**
* Prefix used in references to form elements that have no 'id' or 'name'
*/
__gCrWeb.common.kNamelessFormIDPrefix = 'gChrome~';
@@ -689,4 +715,5 @@ __gCrWeb['common'] = __gCrWeb.common;
}
return false;
};
+
}()); // End of anonymous object

Powered by Google App Engine
This is Rietveld 408576698