Skip to content

Commit 70ec3c0

Browse files
committed
AG-41743 Fix 'trusted-suppress-native-method' — reset 'isMatchingSuspended' when stack is not matched. #496
Squashed commit of the following: commit 6bf2247 Author: Adam Wróblewski <[email protected]> Date: Tue Apr 22 14:27:15 2025 +0200 Fix 'trusted-suppress-native-method' — reset 'isMatchingSuspended' when stack is not matched
1 parent 3e8429f commit 70ec3c0

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
2323

2424
- Improved docs for `json-prune`, `xml-prune` and `trusted-prune-inbound-object` scriptlets [#392].
2525

26+
### Fixed
27+
28+
- `trusted-suppress-native-method` scriptlet, `isMatchingSuspended` was not reset when the stack does not match,
29+
so in some cases given method was not prevented [#496].
30+
2631
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.1.7...HEAD
2732
[#392]: https://github.com/AdguardTeam/Scriptlets/issues/392
2833
[#416]: https://github.com/AdguardTeam/Scriptlets/issues/416
2934
[#477]: https://github.com/AdguardTeam/Scriptlets/issues/477
35+
[#496]: https://github.com/AdguardTeam/Scriptlets/issues/496
3036

3137
## [v2.1.7] - 2025-04-03
3238

src/scriptlets/trusted-suppress-native-method.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function trustedSuppressNativeMethod(
188188
isMatchingSuspended = true;
189189

190190
if (stack && !matchStackTrace(stack, new Error().stack || '')) {
191+
isMatchingSuspended = false;
191192
return Reflect.apply(target, thisArg, argumentsList);
192193
}
193194
const isMatching = matchMethodCall(argumentsList, signatureMatcher);

tests/scriptlets/trusted-suppress-native-method.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const natives = {
1616
'Array.isArray': Array.isArray,
1717
'Node.prototype.appendChild': Node.prototype.appendChild,
1818
'Document.prototype.querySelectorAll': Document.prototype.querySelectorAll,
19+
'Document.prototype.querySelector': Document.prototype.querySelector,
1920
};
2021

2122
const restoreNativeMethod = (path) => {
@@ -307,4 +308,23 @@ test('Match: stack trace', (assert) => {
307308

308309
assert.notOk(window.testMatching('test'), 'Call was prevented');
309310
assert.strictEqual(window.hit, 'FIRED', 'hit func executed');
311+
312+
runScriptlet(name, [
313+
'Document.prototype.querySelector',
314+
'"body"',
315+
'prevent',
316+
'testFunc',
317+
]);
318+
319+
const testFunc = () => document.querySelector('body');
320+
const result = testFunc();
321+
assert.strictEqual(result, undefined, 'testFunc should return undefined when prevented');
322+
323+
const test = () => document.querySelector('body');
324+
const result2 = test();
325+
assert.ok(result2 instanceof HTMLElement, 'test should return HTMLElement, stack does not match');
326+
327+
const testFunc1 = () => document.querySelector('body');
328+
const result3 = testFunc1();
329+
assert.strictEqual(result3, undefined, 'testFunc1 should return undefined when prevented');
310330
});

0 commit comments

Comments
 (0)