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: eslint fixes Created Sept. 20, 2018, 10:26 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
@@ -408,8 +408,55 @@
mutation.target.appendChild(node);
}
}
}
});
}
exports.readd = readd;
+
+function randomId()
+{
+ // 2176782336 is 36^6 which mean 6 chars [a-z0-9]
+ 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(prop)
+{
+ if (!prop)
+ return;
+
+ let magic = randomId();
+
+ let abort = function()
+ {
+ throw new ReferenceError(magic);
+ };
+
+ let {onerror} = window.onerror;
+ window.onerror = (msg, ...args) =>
+ {
+ if (typeof msg === "string" && msg.indexOf(magic) != -1)
+ return true;
+ if (onerror)
+ return onerror(msg, ...args);
+ };
+
+ // patching function
+ (function(o, p)
+ {
+ // simple property
+ let d = Object.getOwnPropertyDescriptor(o, p);
+ if (!d || d.get !== abort)
+ Object.defineProperty(o, p, {get: abort, set() {}});
+ })(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