|
| 1 | +import { |
| 2 | + hit, |
| 3 | + toRegExp, |
| 4 | + logMessage, |
| 5 | + noopFunc, |
| 6 | +} from '../helpers'; |
| 7 | +import { type Source } from './scriptlets'; |
| 8 | + |
| 9 | +/* eslint-disable max-len */ |
| 10 | +/** |
| 11 | + * @scriptlet prevent-constructor |
| 12 | + * |
| 13 | + * @description |
| 14 | + * Prevents a constructor call if the constructor name |
| 15 | + * and optionally the first argument match the specified criteria. |
| 16 | + * This scriptlet is useful for blocking constructors like `Promise` or `MutationObserver` |
| 17 | + * that can be used to circumvent existing scriptlets like `prevent-addEventListener`. |
| 18 | + * |
| 19 | + * ### Syntax |
| 20 | + * |
| 21 | + * ```text |
| 22 | + * example.org#%#//scriptlet('prevent-constructor', constructorName[, argumentSearch]) |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * - `constructorName` — required, string, the name of the constructor to prevent, |
| 26 | + * e.g., "Promise", "MutationObserver". |
| 27 | + * Must be a property of the global `window` object. |
| 28 | + * - `argumentsMatch` — optional, string or regular expression, |
| 29 | + * or JSON array of patterns matching arguments passed to the constructor. |
| 30 | + * Defaults to match all constructors if not specified. |
| 31 | + * Possible values: |
| 32 | + * - string — matches the first argument only; |
| 33 | + * - JSON array — matches arguments positionally, should be wrapped in `[]`; |
| 34 | + * use `"*"` to skip positions, e.g., if only second argument should be matched. |
| 35 | + * Invalid regular expression or JSON will cause exit and rule will not work. |
| 36 | + * |
| 37 | + * ### Examples |
| 38 | + * |
| 39 | + * 1. Prevent all `MutationObserver` constructor calls |
| 40 | + * |
| 41 | + * ```adblock |
| 42 | + * example.org#%#//scriptlet('prevent-constructor', 'MutationObserver') |
| 43 | + * ``` |
| 44 | + * |
| 45 | + * 1. Prevent `Promise` constructor calls where the first argument contains `adblock` |
| 46 | + * |
| 47 | + * ```adblock |
| 48 | + * example.org#%#//scriptlet('prevent-constructor', 'Promise', 'adblock') |
| 49 | + * ``` |
| 50 | + * |
| 51 | + * 1. Prevent `MutationObserver` calls where the first argument matches a regexp |
| 52 | + * |
| 53 | + * ```adblock |
| 54 | + * example.org#%#//scriptlet('prevent-constructor', 'MutationObserver', '/detect.*ad/') |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * 1. Prevent `MutationObserver` calls where the second argument contains `attributes`, |
| 58 | + * and matching of the first argument is skipped |
| 59 | + * |
| 60 | + * ```adblock |
| 61 | + * example.org#%#//scriptlet('prevent-constructor', 'MutationObserver', '["*", "attributes"]') |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * @added unknown |
| 65 | + */ |
| 66 | +/* eslint-enable max-len */ |
| 67 | +export function preventConstructor( |
| 68 | + source: Source, |
| 69 | + constructorName: string, |
| 70 | + argumentsMatch?: string, |
| 71 | +) { |
| 72 | + if (!constructorName) { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const nativeConstructor = (window as any)[constructorName]; |
| 77 | + |
| 78 | + if (typeof nativeConstructor !== 'function') { |
| 79 | + logMessage(source, `"${constructorName}" is not a function`); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks if the argumentsMatch string represents a valid array of patterns |
| 85 | + * needed to match constructor arguments respectively. |
| 86 | + * |
| 87 | + * @param input The argumentsMatch string to parse. |
| 88 | + * |
| 89 | + * @returns Array of patterns or null if parsing fails. |
| 90 | + */ |
| 91 | + const parseArgumentsMatchAsArray = (input: string | undefined): string[] | null => { |
| 92 | + if (!input) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + if ( |
| 97 | + input.trim().startsWith('[') |
| 98 | + && input.trim().endsWith(']') |
| 99 | + ) { |
| 100 | + try { |
| 101 | + const parsed = JSON.parse(input); |
| 102 | + |
| 103 | + if (Array.isArray(parsed)) { |
| 104 | + return parsed.map((p: any) => String(p)); |
| 105 | + } |
| 106 | + |
| 107 | + logMessage(source, 'Invalid argumentsMatch: not an array'); |
| 108 | + |
| 109 | + return null; |
| 110 | + } catch (e) { |
| 111 | + logMessage(source, `Invalid JSON in argumentsMatch: ${input}`); |
| 112 | + |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Plain string - will be used for first argument matching |
| 118 | + return null; |
| 119 | + }; |
| 120 | + |
| 121 | + const arrayArgPatterns = parseArgumentsMatchAsArray(argumentsMatch); |
| 122 | + |
| 123 | + /** |
| 124 | + * This flag allows to prevent infinite loops when trapping constructors |
| 125 | + * that are used by scriptlet's own code, e.g., RegExp used by toRegExp). |
| 126 | + */ |
| 127 | + let isMatchingSuspended = false; |
| 128 | + |
| 129 | + const handlerWrapper = (target: any, args: any[], newTarget: any) => { |
| 130 | + // If matching is suspended, pass through to the original constructor |
| 131 | + // to avoid infinite recursion |
| 132 | + if (isMatchingSuspended) { |
| 133 | + return Reflect.construct(target, args, newTarget); |
| 134 | + } |
| 135 | + |
| 136 | + // Prevent matching to avoid infinite recursion |
| 137 | + isMatchingSuspended = true; |
| 138 | + |
| 139 | + let shouldPrevent = false; |
| 140 | + |
| 141 | + if (!argumentsMatch) { |
| 142 | + // No argument search specified - match all |
| 143 | + shouldPrevent = true; |
| 144 | + } else if (arrayArgPatterns !== null) { |
| 145 | + // Array syntax — match arguments positionally. |
| 146 | + // Assume it is matched until proven otherwise |
| 147 | + shouldPrevent = true; |
| 148 | + |
| 149 | + for (let i = 0; i < arrayArgPatterns.length; i += 1) { |
| 150 | + const pattern = arrayArgPatterns[i]; |
| 151 | + |
| 152 | + // Some arguments may be skipped, e.g. ['*', 'callback'] |
| 153 | + if (pattern === '*') { |
| 154 | + continue; |
| 155 | + } |
| 156 | + |
| 157 | + // Check if argument exists at this position |
| 158 | + if (i >= args.length) { |
| 159 | + // Pattern expects an argument that does not exist - do not match |
| 160 | + // eslint-disable-next-line max-len |
| 161 | + const msg = `Pattern expects argument at position ${i}, but constructor called with ${args.length} arguments`; |
| 162 | + logMessage(source, msg); |
| 163 | + shouldPrevent = false; |
| 164 | + break; |
| 165 | + } |
| 166 | + |
| 167 | + const arg = args[i]; |
| 168 | + let argStr: string; |
| 169 | + |
| 170 | + if (typeof arg === 'function') { |
| 171 | + argStr = arg.toString(); |
| 172 | + } else if (typeof arg === 'object' && arg !== null) { |
| 173 | + try { |
| 174 | + argStr = JSON.stringify(arg); |
| 175 | + } catch (e) { |
| 176 | + argStr = String(arg); |
| 177 | + } |
| 178 | + } else { |
| 179 | + argStr = String(arg); |
| 180 | + } |
| 181 | + |
| 182 | + const patternRegexp = toRegExp(pattern); |
| 183 | + |
| 184 | + if (!patternRegexp.test(argStr)) { |
| 185 | + shouldPrevent = false; |
| 186 | + break; |
| 187 | + } |
| 188 | + } |
| 189 | + } else { |
| 190 | + // if argumentsMatch is set and is not an array, it should be a plain string, |
| 191 | + // so only the first argument should be matched |
| 192 | + const firstArg = args[0]; |
| 193 | + let firstArgStr: string; |
| 194 | + |
| 195 | + if (typeof firstArg === 'function') { |
| 196 | + firstArgStr = firstArg.toString(); |
| 197 | + } else if (typeof firstArg === 'object' && firstArg !== null) { |
| 198 | + try { |
| 199 | + firstArgStr = JSON.stringify(firstArg); |
| 200 | + } catch (e) { |
| 201 | + firstArgStr = String(firstArg); |
| 202 | + } |
| 203 | + } else { |
| 204 | + firstArgStr = String(firstArg); |
| 205 | + } |
| 206 | + |
| 207 | + const argumentsMatchRegexp = toRegExp(argumentsMatch); |
| 208 | + shouldPrevent = argumentsMatchRegexp.test(firstArgStr); |
| 209 | + } |
| 210 | + |
| 211 | + if (!shouldPrevent) { |
| 212 | + isMatchingSuspended = false; |
| 213 | + return Reflect.construct(target, args, newTarget); |
| 214 | + } |
| 215 | + |
| 216 | + hit(source); |
| 217 | + // Construct with noop callback to prevent original code execution |
| 218 | + // while maintaining proper instanceof checks |
| 219 | + try { |
| 220 | + const result = Reflect.construct(target, [noopFunc], newTarget); |
| 221 | + isMatchingSuspended = false; |
| 222 | + return result; |
| 223 | + } catch (e) { |
| 224 | + // If construction fails, return an empty object with proper prototype |
| 225 | + isMatchingSuspended = false; |
| 226 | + return Object.create(target.prototype || null); |
| 227 | + } |
| 228 | + }; |
| 229 | + |
| 230 | + const constructorHandler: ProxyHandler<typeof nativeConstructor> = { |
| 231 | + construct: handlerWrapper, |
| 232 | + get(target, prop, receiver) { |
| 233 | + if (prop === 'toString') { |
| 234 | + return Function.prototype.toString.bind(target); |
| 235 | + } |
| 236 | + return Reflect.get(target, prop, receiver); |
| 237 | + }, |
| 238 | + }; |
| 239 | + |
| 240 | + (window as any)[constructorName] = new Proxy(nativeConstructor, constructorHandler); |
| 241 | +} |
| 242 | + |
| 243 | +export const preventConstructorNames = [ |
| 244 | + 'prevent-constructor', |
| 245 | +]; |
| 246 | + |
| 247 | +// eslint-disable-next-line prefer-destructuring |
| 248 | +preventConstructor.primaryName = preventConstructorNames[0]; |
| 249 | + |
| 250 | +preventConstructor.injections = [ |
| 251 | + hit, |
| 252 | + toRegExp, |
| 253 | + logMessage, |
| 254 | + noopFunc, |
| 255 | +]; |
0 commit comments