Skip to content

Commit 34f0eaf

Browse files
committed
Pull request 834: AG-56516 fix trusted-replace-argument replace: mode for non-string arguments
Squashed commit of the following: commit d94ac08 Author: Maxim Topciu <mtopciu@adguard.com> Date: Mon Jul 20 07:29:48 2026 +0000 Pull request 835: AG-56671 add piano-analytics.js redirect to compatibility table Squashed commit of the following: commit 1ad1735 Author: Maxim Topciu <mtopciu@adguard.com> Date: Sat Jul 18 06:53:53 2026 +0300 AG-56671 add piano-analytics.js redirect to compatibility table commit 7aa25db Author: Maxim Topciu <mtopciu@adguard.com> Date: Fri Jul 17 23:37:42 2026 +0300 AG-56516 fix trusted-replace-argument replace: mode for non-string arguments
1 parent 7581f60 commit 34f0eaf

5 files changed

Lines changed: 90 additions & 12 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
3131

3232
- UBO aliases for `prevent-bab` scriptlet and redirect.
3333

34+
### Fixed
35+
36+
- `trusted-replace-argument` — when `replace:` is used and the argument is not a string,
37+
the argument is no longer blindly overwritten with the replacement value; the replacement
38+
is applied to its string form and the original value and type are kept when the pattern
39+
does not match [#570].
40+
3441
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.4.3...HEAD
3542
[#417]: https://github.com/AdguardTeam/Scriptlets/issues/417
3643
[#566]: https://github.com/AdguardTeam/Scriptlets/issues/566
44+
[#570]: https://github.com/AdguardTeam/Scriptlets/issues/570
3745

3846
## [2.4.3] - 2026-06-24
3947

‎scripts/compatibility-table.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,9 @@
654654
},
655655
{
656656
"ubo": "adthrive_abd.js"
657+
},
658+
{
659+
"ubo": "piano-analytics.js"
657660
}
658661
]
659662
}

‎src/scriptlets/trusted-replace-argument.ts‎

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ import { type Source } from './scriptlets';
4848
* - `argumentValue` – required, string value to set for the argument.
4949
* If it starts with `replace:`, it is treated as a replacement pattern in the format `replace:/regex/replacement/`.
5050
* To replace all occurrences of a pattern, the replacement string must include the global flag `g`, like this: `replace:/foo/bar/g`, otherwise only the first occurrence will be replaced.
51+
* If the matched argument is not a string, it is converted to a string before applying the replacement,
52+
* and the argument is modified only when the replacement changes the resulting value;
53+
* otherwise the original value and its type are kept.
5154
* If it starts with `json:`, it is treated as a JSON string to parse and set for the argument. For example, `json:{"key": "value"}` will set the argument to an object `{ key: 'value' }`.
5255
* If it does not start with `replace:` or `json:`, it is treated as a constant value to set for the argument, or as one of the following predefined constants:
5356
* - `undefined`
@@ -325,6 +328,29 @@ export function trustedReplaceArgument(
325328
return true;
326329
};
327330

331+
/**
332+
* Replaces the argument at the target index in the provided arguments list.
333+
*
334+
* In `replace:` mode the argument is converted to a string and the regex replacement
335+
* is applied; the result is assigned only if it differs from the converted value,
336+
* so arguments that do not match the regex keep their original value and type
337+
* (e.g. numbers). Otherwise the argument is set to the constant value.
338+
*
339+
* @param argumentsList - The arguments list to modify in place.
340+
*/
341+
const replaceTargetArgument = (argumentsList: unknown[]) => {
342+
const argumentToReplace = argumentsList[Number(argumentIndex)];
343+
if (shouldReplaceArgument) {
344+
const argumentString = String(argumentToReplace);
345+
const replacedArgument = argumentString.replace(replaceRegexValue, constantValue as string);
346+
if (replacedArgument !== argumentString) {
347+
argumentsList[Number(argumentIndex)] = replacedArgument;
348+
}
349+
} else {
350+
argumentsList[Number(argumentIndex)] = constantValue;
351+
}
352+
};
353+
328354
let isMatchingSuspended = false;
329355

330356
const applyWrapper = (target: Function, thisArg: any, argumentsList: unknown[]) => {
@@ -355,12 +381,7 @@ export function trustedReplaceArgument(
355381
return Reflect.apply(target, thisArg, argumentsList);
356382
}
357383

358-
if (typeof argumentToReplace === 'string' && shouldReplaceArgument) {
359-
argumentsList[Number(argumentIndex)] = argumentToReplace
360-
.replace(replaceRegexValue, constantValue as string);
361-
} else {
362-
argumentsList[Number(argumentIndex)] = constantValue;
363-
}
384+
replaceTargetArgument(argumentsList);
364385

365386
// Log the modified arguments after replacement
366387
if (verbose === 'true') {
@@ -408,12 +429,7 @@ export function trustedReplaceArgument(
408429
return Reflect.construct(target, argumentsList, newTarget);
409430
}
410431

411-
if (typeof argumentToReplace === 'string' && shouldReplaceArgument) {
412-
argumentsList[Number(argumentIndex)] = argumentToReplace
413-
.replace(replaceRegexValue, constantValue as string);
414-
} else {
415-
argumentsList[Number(argumentIndex)] = constantValue;
416-
}
432+
replaceTargetArgument(argumentsList);
417433

418434
// Log the modified arguments after replacement
419435
if (verbose === 'true') {

‎tests/scriptlets/trusted-replace-argument.test.js‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const nativeDocumentQuerySelectorAll = window.document.querySelectorAll;
1313
const nativeMutationObserver = window.MutationObserver;
1414
const nativeObjectDefineProperty = window.Object.defineProperty;
1515
const nativeStringReplace = String.prototype.replace;
16+
const nativeSetAttribute = window.Element.prototype.setAttribute;
17+
const nativeJSONStringify = window.JSON.stringify;
1618

1719
const beforeEach = () => {
1820
window.__debug = () => {
@@ -31,6 +33,8 @@ const afterEach = () => {
3133
window.MutationObserver = nativeMutationObserver;
3234
window.Object.defineProperty = nativeObjectDefineProperty;
3335
window.String.prototype.replace = nativeStringReplace;
36+
window.Element.prototype.setAttribute = nativeSetAttribute;
37+
window.JSON.stringify = nativeJSONStringify;
3438
};
3539

3640
module(name, { beforeEach, afterEach });
@@ -290,3 +294,49 @@ test('Replace argument in String.prototype.replace to "test" if pattern matches'
290294
);
291295
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
292296
});
297+
298+
test('Non-string argument stays intact when "replace:" pattern does not match', (assert) => {
299+
runScriptlet(name, ['Element.prototype.setAttribute', '1', 'replace:/whatever/test/g']);
300+
301+
const div = document.createElement('div');
302+
div.setAttribute('data-attr', 1234);
303+
304+
assert.strictEqual(div.getAttribute('data-attr'), '1234', 'Numeric attribute value should stay intact');
305+
assert.strictEqual(
306+
Element.prototype.setAttribute.toString(),
307+
nativeSetAttribute.toString(),
308+
'setAttribute.toString() returns the original value',
309+
);
310+
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
311+
});
312+
313+
test('Non-string argument keeps its type when "replace:" pattern does not match', (assert) => {
314+
runScriptlet(name, ['JSON.stringify', '0', 'replace:/whatever/test/g']);
315+
316+
const result = JSON.stringify(1234);
317+
318+
// Would be '"test"' if the number was blindly replaced with the replacement string,
319+
// and '"1234"' if the number was coerced to a string despite the non-matching regex
320+
assert.strictEqual(result, '1234', 'Number argument should keep its value and type');
321+
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
322+
});
323+
324+
test('Non-string argument is replaced when "replace:" pattern matches its string form', (assert) => {
325+
runScriptlet(name, ['Element.prototype.setAttribute', '1', 'replace:/23/98/']);
326+
327+
const div = document.createElement('div');
328+
div.setAttribute('data-attr', 1234);
329+
330+
assert.strictEqual(div.getAttribute('data-attr'), '1984', 'Matching part of the numeric value is replaced');
331+
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
332+
});
333+
334+
test('Non-string constructor argument stays intact when "replace:" pattern does not match', (assert) => {
335+
runScriptlet(name, ['Array', '1', 'replace:/whatever/test/g']);
336+
337+
// eslint-disable-next-line no-array-constructor
338+
const result = new Array(1, 2, 3);
339+
340+
assert.deepEqual(result, [1, 2, 3], 'Constructor arguments should stay intact');
341+
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
342+
});

‎wiki/compatibility-table.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,4 @@
198198
| | sensors-analytics.js | |
199199
| | nitropay_ads.js | |
200200
| | adthrive_abd.js | |
201+
| | piano-analytics.js | |

0 commit comments

Comments
 (0)