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
1 changed files with 8 additions and 2 deletions

View File

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