1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-05 11:37:01 +02:00

Use safe versions of Math.floor/Math.random in scriptlets

Related issue:
https://github.com/uBlockOrigin/uBOL-home/issues/78
This commit is contained in:
Raymond Hill 2023-10-09 09:45:26 -04:00
parent ad75b1bdba
commit 780383faef
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -48,6 +48,8 @@ function safeSelf() {
const self = globalThis;
const safe = {
'Error': self.Error,
'Math_floor': Math.floor,
'Math_random': Math.random,
'Object_defineProperty': Object.defineProperty.bind(Object),
'RegExp': self.RegExp,
'RegExp_test': self.RegExp.prototype.test,
@ -133,11 +135,15 @@ function safeSelf() {
builtinScriptlets.push({
name: 'get-exception-token.fn',
fn: getExceptionToken,
dependencies: [
'safe-self.fn',
],
});
function getExceptionToken() {
const safe = safeSelf();
const token =
String.fromCharCode(Date.now() % 26 + 97) +
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
safe.Math_floor(safe.Math_random() * 982451653 + 982451653).toString(36);
const oe = self.onerror;
self.onerror = function(msg, ...args) {
if ( typeof msg === 'string' && msg.includes(token) ) { return true; }
@ -2207,7 +2213,7 @@ function noXhrIf(
const warOrigin = scriptletGlobals.get('warOrigin');
const generateRandomString = len => {
let s = '';
do { s += Math.random().toString(36).slice(2); }
do { s += safe.Math_random().toString(36).slice(2); }
while ( s.length < 10 );
return s.slice(0, len);
};