|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + uBlock Origin - a comprehensive, efficient content blocker |
| 4 | + Copyright (C) 2019-present Raymond Hill |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see {http://www.gnu.org/licenses/}. |
| 18 | +
|
| 19 | + Home: https://github.com/gorhill/uBlock |
| 20 | +
|
| 21 | +*/ |
| 22 | + |
| 23 | +import { parseReplaceFn } from './parse-replace.js'; |
| 24 | +import { proxyApplyFn } from './proxy-apply.js'; |
| 25 | +import { registerScriptlet } from './base.js'; |
| 26 | +import { safeSelf } from './safe-self.js'; |
| 27 | +import { validateConstantFn } from './set-constant.js'; |
| 28 | + |
| 29 | +/** |
| 30 | + * @scriptlet trusted-replace-argument.js |
| 31 | + * |
| 32 | + * @description |
| 33 | + * Replace an argument passed to a method. Requires a trusted source. |
| 34 | + * |
| 35 | + * @param propChain |
| 36 | + * The property chain to the function which argument must be replaced when |
| 37 | + * called. |
| 38 | + * |
| 39 | + * @param argposRaw |
| 40 | + * The zero-based position of the argument in the argument list. Use a negative |
| 41 | + * number for a position relative to the last argument. |
| 42 | + * |
| 43 | + * @param argraw |
| 44 | + * The replacement value, validated using the same heuristic as with the |
| 45 | + * `set-constant.js` scriptlet. |
| 46 | + * If the replacement value matches `json:...`, the value will be the |
| 47 | + * json-parsed string after `json:`. |
| 48 | + * If the replacement value matches `repl:/.../.../`, the target argument will |
| 49 | + * be replaced according the regex-replacement directive following `repl:` |
| 50 | + * |
| 51 | + * @param [, condition, pattern] |
| 52 | + * Optional. The replacement will occur only when pattern matches the target |
| 53 | + * argument. |
| 54 | + * |
| 55 | + * */ |
| 56 | + |
| 57 | +export function trustedReplaceArgument( |
| 58 | + propChain = '', |
| 59 | + argposRaw = '', |
| 60 | + argraw = '' |
| 61 | +) { |
| 62 | + if ( propChain === '' ) { return; } |
| 63 | + const safe = safeSelf(); |
| 64 | + const logPrefix = safe.makeLogPrefix('trusted-replace-argument', propChain, argposRaw, argraw); |
| 65 | + const argoffset = parseInt(argposRaw, 10) || 0; |
| 66 | + const extraArgs = safe.getExtraArgs(Array.from(arguments), 3); |
| 67 | + const replacer = argraw.startsWith('repl:/') && |
| 68 | + parseReplaceFn(argraw.slice(5)) || undefined; |
| 69 | + const value = replacer === undefined && |
| 70 | + validateConstantFn(true, argraw, extraArgs) || undefined; |
| 71 | + const reCondition = extraArgs.condition |
| 72 | + ? safe.patternToRegex(extraArgs.condition) |
| 73 | + : /^/; |
| 74 | + proxyApplyFn(propChain, function(context) { |
| 75 | + const { callArgs } = context; |
| 76 | + if ( argposRaw === '' ) { |
| 77 | + safe.uboLog(logPrefix, `Arguments:\n${callArgs.join('\n')}`); |
| 78 | + return context.reflect(); |
| 79 | + } |
| 80 | + const argpos = argoffset >= 0 ? argoffset : callArgs.length - argoffset; |
| 81 | + if ( argpos < 0 || argpos >= callArgs.length ) { |
| 82 | + return context.reflect(); |
| 83 | + } |
| 84 | + const argBefore = callArgs[argpos]; |
| 85 | + if ( safe.RegExp_test.call(reCondition, argBefore) === false ) { |
| 86 | + return context.reflect(); |
| 87 | + } |
| 88 | + const argAfter = replacer && typeof argBefore === 'string' |
| 89 | + ? argBefore.replace(replacer.re, replacer.replacement) |
| 90 | + : value; |
| 91 | + callArgs[argpos] = argAfter; |
| 92 | + safe.uboLog(logPrefix, `Replaced argument:\nBefore: ${JSON.stringify(argBefore)}\nAfter: ${argAfter}`); |
| 93 | + return context.reflect(); |
| 94 | + }); |
| 95 | +} |
| 96 | +registerScriptlet(trustedReplaceArgument, { |
| 97 | + name: 'trusted-replace-argument.js', |
| 98 | + requiresTrust: true, |
| 99 | + dependencies: [ |
| 100 | + parseReplaceFn, |
| 101 | + proxyApplyFn, |
| 102 | + safeSelf, |
| 103 | + validateConstantFn, |
| 104 | + ], |
| 105 | +}); |
0 commit comments