Index: lib/content/snippets.js |
=================================================================== |
--- a/lib/content/snippets.js |
+++ b/lib/content/snippets.js |
@@ -486,8 +486,79 @@ |
{ |
event.preventDefault(); |
} |
}, |
true); |
} |
exports["prevent-inline-scripts"] = preventInlineScripts; |
+ |
+/** |
+ * Generates an alphanumeric ID consisting of 5 randomly 6 base-36 digits |
+ * from the range 100000..zzzzzz (both inclusive) concatenated. |
+ * |
+ * @returns {string} The random ID. |
+ */ |
+function randomId() |
Manish Jethani
2018/10/25 22:22:52
Do you like this version of the function?
I don't
Manish Jethani
2018/10/25 22:59:57
Just to clarify, I'm sorry about leading you down
hub
2018/10/26 15:19:05
Done.
|
+{ |
+ let id = ""; |
+ // |
+ // 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 |
+ // |
+ for (let i = 0; i < 5; i++) |
+ id += Math.floor(Math.random() * 2116316160 + 60466176).toString(36); |
+ |
+ return id; |
+} |
+ |
+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. |
+ * It will intercept the onerror callback and block it if tagged. |
+ * The idea originates from |
+ * https://github.com/uBlockOrigin/uAssets/blob/80b195436f8f8d78ba713237bfc268ecfc9d9d2b/filters/resources.txt#L1703 |
+ * |
+ * @todo handle properties of properties. |
+ * |
+ * @param {string} property the name of the property. |
+ */ |
+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 onerror.call(this, message, ...rest); |
Manish Jethani
2018/10/25 22:22:52
OK, this one I already explained why there's no gu
hub
2018/10/26 15:19:06
Done.
|
+ }; |
+ |
+ wrapPropertyAccess(window, property, {get: abort, set() {}}); |
+} |
+ |
+exports["abort-on-property-read"] = makeInjector(abortOnPropertyRead, |
+ wrapPropertyAccess, |
+ randomId); |