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: Allow multiple properties as arguments. Created Sept. 21, 2018, 8:03 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
@@ -431,8 +431,56 @@
object + "";
if (typeof dir == "function")
dir.call(this, object);
};
}
exports["dir-string"] = makeInjector(dirString);
+
+function randomId()
+{
+ // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
Manish Jethani 2018/09/24 11:08:35 Since we're not zero-padding the return value, doe
hub 2018/09/25 04:28:52 Modifying the formula so that if Math.random() ret
Manish Jethani 2018/09/25 12:25:22 The updated version looks alright to me, it always
hub 2018/09/25 15:40:43 6 is arbitrary. Long enough to be unique, but stil
+ return Math.floor(Math.random() * 2176782336).toString(36);
+}
+
+/**
+ * Will patch a property on the window object to abort when read.
+ * It will intercept the onerror callback and block it if tagged.
+ *
+ * @todo handle properties of properties.
+ *
+ * @param {string} prop the name of the property.
+ */
+function abortOnPropertyRead(...props)
Manish Jethani 2018/09/24 11:08:35 The problem with accepting a variable number of ar
hub 2018/09/25 04:28:52 I didn't have uBO compatibility in mind. I just al
Manish Jethani 2018/09/25 12:25:22 The thing is that the moment ABP starts using the
hub 2018/09/25 15:40:43 fair enough. will fix it.
Manish Jethani 2018/10/22 14:21:01 Happy to proceed whenever you're ready.
hub 2018/10/22 14:52:49 This was fixed in Patch Set 5
Manish Jethani 2018/10/22 20:21:54 Sorry, I missed this. I'll look at this patch tomo
+{
+ if (props.length == 0)
+ return;
+
+ let magic = randomId();
+
+ let abort = function()
+ {
+ throw new ReferenceError(magic);
+ };
+
+ let {onerror} = window;
+ window.onerror = (msg, ...args) =>
Manish Jethani 2018/09/24 11:08:35 Let's rename this to `message` and `...rest`. Abou
hub 2018/09/25 04:28:51 Done.
+ {
+ if (typeof msg === "string" && msg.indexOf(magic) != -1)
Manish Jethani 2018/09/24 11:08:35 We could use `String.includes` instead of `String.
Manish Jethani 2018/09/24 11:08:35 Strict equality is unnecessary here.
hub 2018/09/25 04:28:51 Done.
hub 2018/09/25 04:28:52 Done.
+ return true;
+ if (onerror)
Manish Jethani 2018/09/24 11:08:35 Just to be safe, we should check `typeof onerror =
hub 2018/09/25 04:28:51 Done.
+ return onerror(msg, ...args);
Manish Jethani 2018/09/24 11:08:35 Let's just also set the `this` for this call, so `
hub 2018/09/25 04:28:52 Done.
+ };
+
+ let patch = function(o, p)
+ {
+ // simple property
+ let d = Object.getOwnPropertyDescriptor(o, p);
+ if (!d || d.get !== abort)
Manish Jethani 2018/09/24 11:08:35 Strict inequality is unnecessary here.
hub 2018/09/25 04:28:51 Done.
+ Object.defineProperty(o, p, {get: abort, set() {}});
+ };
+ for (let prop of props)
+ patch(window, prop);
+}
+
+exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, 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