|
| 1 | +import { |
| 2 | + hit, |
| 3 | + logMessage, |
| 4 | + toRegExp, |
| 5 | + parseMatchArg, |
| 6 | +} from '../helpers'; |
| 7 | +import { type Source } from './scriptlets'; |
| 8 | + |
| 9 | +/** |
| 10 | + * @scriptlet prevent-innerHTML |
| 11 | + * |
| 12 | + * @description |
| 13 | + * Conditionally prevents assignment to `innerHTML` property |
| 14 | + * and can replace the value returned by the getter. |
| 15 | + * |
| 16 | + * Related UBO scriptlet: |
| 17 | + * https://github.com/gorhill/uBlock/wiki/Resources-Library#prevent-innerhtmljs- |
| 18 | + * |
| 19 | + * ### Syntax |
| 20 | + * |
| 21 | + * ```text |
| 22 | + * example.org#%#//scriptlet('prevent-innerHTML'[, selector[, pattern[, replacement]]]) |
| 23 | + * ``` |
| 24 | + * |
| 25 | + * - `selector` — optional, CSS selector to match element. If not specified, matches all elements. |
| 26 | + * - `pattern` — optional, string or regular expression to match against the assigned/returned value. |
| 27 | + * Prepend with `!` to invert the match. If not specified, matches all values. |
| 28 | + * - `replacement` — optional, replacement value to return from getter when pattern matches. |
| 29 | + * If not specified, the getter returns the original value unchanged (setter-only mode). |
| 30 | + * If specified, enables getter manipulation mode. Possible values: |
| 31 | + * - empty string — `''`, |
| 32 | + * - custom text. |
| 33 | + * |
| 34 | + * ### Examples |
| 35 | + * |
| 36 | + * 1. Prevent any `innerHTML` assignment |
| 37 | + * |
| 38 | + * ```adblock |
| 39 | + * example.org#%#//scriptlet('prevent-innerHTML') |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * 1. Prevent `innerHTML` assignment on elements matching selector |
| 43 | + * |
| 44 | + * ```adblock |
| 45 | + * example.org#%#//scriptlet('prevent-innerHTML', '#ads') |
| 46 | + * ``` |
| 47 | + * |
| 48 | + * 1. Prevent `innerHTML` assignment when the value contains "ad" |
| 49 | + * |
| 50 | + * ```adblock |
| 51 | + * example.org#%#//scriptlet('prevent-innerHTML', '', 'ad') |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * 1. Prevent `innerHTML` assignment on specific element when value matches regex |
| 55 | + * |
| 56 | + * ```adblock |
| 57 | + * example.org#%#//scriptlet('prevent-innerHTML', 'div.ads', '/banner|sponsor/') |
| 58 | + * ``` |
| 59 | + * |
| 60 | + * 1. Prevent `innerHTML` assignment when value does NOT match pattern (inverted match) |
| 61 | + * |
| 62 | + * ```adblock |
| 63 | + * example.org#%#//scriptlet('prevent-innerHTML', '', '!allowed-content') |
| 64 | + * ``` |
| 65 | + * |
| 66 | + * 1. Replace innerHTML getter value with empty string when it contains "delete window" |
| 67 | + * |
| 68 | + * ```adblock |
| 69 | + * example.org#%#//scriptlet('prevent-innerHTML', '', 'delete window', '') |
| 70 | + * ``` |
| 71 | + * |
| 72 | + * 1. Replace innerHTML getter value with custom text when pattern matches |
| 73 | + * |
| 74 | + * ```adblock |
| 75 | + * example.org#%#//scriptlet('prevent-innerHTML', 'div.code', '/evil-script/', 'safe-replacement') |
| 76 | + * ``` |
| 77 | + * |
| 78 | + * @added v2.2.14. |
| 79 | + */ |
| 80 | +export function preventInnerHTML(source: Source, selector = '', pattern = '', replacement?: string): void { |
| 81 | + const { isInvertedMatch, matchRegexp } = parseMatchArg(pattern); |
| 82 | + |
| 83 | + const nativeDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'innerHTML'); |
| 84 | + if (nativeDescriptor === undefined) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Determines whether the innerHTML assignment should be prevented. |
| 90 | + * |
| 91 | + * @param element Element being assigned to. |
| 92 | + * @param value Value being assigned. |
| 93 | + * |
| 94 | + * @returns True if assignment should be prevented. |
| 95 | + */ |
| 96 | + const shouldPrevent = (element: Element, value: string): boolean => { |
| 97 | + if (selector !== '') { |
| 98 | + if (typeof element.matches !== 'function') { |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + try { |
| 103 | + if (element.matches(selector) === false) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } catch (e) { |
| 107 | + // Invalid selector, do not prevent |
| 108 | + logMessage(source, `prevent-innerHTML: invalid selector "${selector}"`, true); |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const patternMatches = matchRegexp.test(String(value)); |
| 114 | + |
| 115 | + return isInvertedMatch ? !patternMatches : patternMatches; |
| 116 | + }; |
| 117 | + |
| 118 | + Object.defineProperty(Element.prototype, 'innerHTML', { |
| 119 | + configurable: true, |
| 120 | + enumerable: true, |
| 121 | + get() { |
| 122 | + const value = nativeDescriptor.get |
| 123 | + ? nativeDescriptor.get.call(this) |
| 124 | + : nativeDescriptor.value; |
| 125 | + |
| 126 | + // If replacement is specified, check if we should replace the getter value |
| 127 | + if (replacement !== undefined && shouldPrevent(this, value)) { |
| 128 | + hit(source); |
| 129 | + logMessage(source, 'Replaced innerHTML getter value'); |
| 130 | + return replacement; |
| 131 | + } |
| 132 | + |
| 133 | + return value; |
| 134 | + }, |
| 135 | + set(value: string) { |
| 136 | + if (shouldPrevent(this, value)) { |
| 137 | + hit(source); |
| 138 | + logMessage(source, 'Prevented innerHTML assignment'); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (nativeDescriptor.set) { |
| 143 | + nativeDescriptor.set.call(this, value); |
| 144 | + } |
| 145 | + }, |
| 146 | + }); |
| 147 | +} |
| 148 | + |
| 149 | +export const preventInnerHTMLNames = [ |
| 150 | + 'prevent-innerHTML', |
| 151 | + // aliases are needed for matching the related scriptlet converted into our syntax |
| 152 | + 'prevent-innerHTML.js', |
| 153 | + 'ubo-prevent-innerHTML.js', |
| 154 | + 'ubo-prevent-innerHTML', |
| 155 | +]; |
| 156 | + |
| 157 | +// eslint-disable-next-line prefer-destructuring |
| 158 | +preventInnerHTML.primaryName = preventInnerHTMLNames[0]; |
| 159 | + |
| 160 | +preventInnerHTML.injections = [ |
| 161 | + hit, |
| 162 | + logMessage, |
| 163 | + toRegExp, |
| 164 | + parseMatchArg, |
| 165 | +]; |
0 commit comments