Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/content/snippets.js

Issue 29886700: Issue 6969 - Implement abort-on-property-read snippet (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Update from review Created Oct. 26, 2018, 3:18 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/content/snippets.js
===================================================================
--- a/lib/content/snippets.js
+++ b/lib/content/snippets.js
@@ -486,8 +486,74 @@
{
event.preventDefault();
}
},
true);
}
exports["prevent-inline-scripts"] = preventInlineScripts;
+
+/**
+ * Generates a random alphanumeric ID consisting 6 base-36 digits
Manish Jethani 2018/10/28 21:34:06 Nit: consisting *of*
hub 2018/10/29 16:34:02 Done.
+ * from the range 100000..zzzzzz (both inclusive).
+ *
+ * @returns {string} The random ID.
+ */
+function randomId()
+{
+ // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
+ // 60466176 is 36^5
+ // 2176782336 - 60466176 = 2116316160. This ensure to always have 6
+ // chars even if Math.random() returns its minimum value 0.0
+ //
+ return Math.floor(Math.random() * 2116316160 + 60466176).toString(36);
+}
+
+function wrapPropertyAccess(object, property, descriptor)
+{
+ let currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
+ if (!currentDescriptor)
+ currentDescriptor = {};
+ if (typeof descriptor.get != "undefined")
+ currentDescriptor.get = descriptor.get;
+ if (typeof descriptor.set != "undefined")
+ currentDescriptor.set = descriptor.set;
+ Object.defineProperty(object, property, currentDescriptor);
+}
+
+/**
+ * Will patch a property on the window object to abort when read.
Manish Jethani 2018/10/28 21:34:05 Suggestion: Patches a property on the window objec
hub 2018/10/29 16:34:02 Done.
+ * It will intercept the onerror callback and block it if tagged.
Manish Jethani 2018/10/28 21:34:05 I don't think the onerror part is necessary, it re
hub 2018/10/29 16:34:02 I'll rephrase as "no error will be printed in the
+ * The idea originates from
Manish Jethani 2018/10/28 21:34:05 Since we are now using JSDoc, how about using a li
hub 2018/10/29 16:34:02 Done.
+ * https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237bfc268ecfc9d9d2b/filters/resources.txt#L1703
+ *
+ * @todo handle properties of properties.
Manish Jethani 2018/10/28 21:34:05 I searched adblockpluscore, adblockpluschrome, and
hub 2018/10/29 16:34:03 I'll remove it.
+ *
+ * @param {string} property the name of the property.
Manish Jethani 2018/10/28 21:34:05 Suggestion: @param {string} property The name o
hub 2018/10/29 16:34:02 Done.
+ */
+function abortOnPropertyRead(property)
+{
+ if (!property)
+ return;
+
+ let rid = randomId();
+
+ function abort()
+ {
+ throw new ReferenceError(rid);
+ }
+
+ let {onerror} = window;
+ window.onerror = (message, ...rest) =>
+ {
+ if (typeof message == "string" && message.includes(rid))
+ return true;
+ if (typeof onerror == "function")
+ return (() => {}).call.call(onerror, this, message, ...rest);
+ };
+
+ wrapPropertyAccess(window, property, {get: abort, set() {}});
+}
+
+exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead,
+ wrapPropertyAccess,
+ randomId);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld