Skip to content

Commit 7fc93da

Browse files
committed
AG-34947 Fix 'trusted-replace-node-text' — output literal quotes for escaped quotes. #440
Squashed commit of the following: commit 94b028d Author: Slava Leleka <v.leleka@adguard.com> Date: Tue May 13 15:08:42 2025 +0300 Use a different quotes commit a2d2dfb Author: Adam Wróblewski <adam@adguard.com> Date: Tue May 13 12:39:56 2025 +0200 Fix escaping quotes in `trusted-replace-node-text` scriptlet
1 parent c6b1575 commit 7fc93da

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
2626

2727
### Fixed
2828

29+
- Escaping quotes in `trusted-replace-node-text` scriptlet [#440].
2930
- `trusted-suppress-native-method` scriptlet, `isMatchingSuspended` was not reset when the stack does not match,
3031
so in some cases given method was not prevented [#496].
3132

3233
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.1.7...HEAD
3334
[#392]: https://github.com/AdguardTeam/Scriptlets/issues/392
3435
[#416]: https://github.com/AdguardTeam/Scriptlets/issues/416
36+
[#440]: https://github.com/AdguardTeam/Scriptlets/issues/440
3537
[#477]: https://github.com/AdguardTeam/Scriptlets/issues/477
3638
[#496]: https://github.com/AdguardTeam/Scriptlets/issues/496
3739
[#497]: https://github.com/AdguardTeam/Scriptlets/issues/497

src/helpers/node-text-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ export const replaceNodeText = (
111111
): void => {
112112
const { textContent } = node;
113113
if (textContent) {
114-
let modifiedText = textContent.replace(pattern, replacement);
114+
// Remove quotes' escapes for cases where scriptlet rule argument has own escaped quotes
115+
// https://github.com/AdguardTeam/Scriptlets/issues/440
116+
let modifiedText = textContent.replace(pattern, replacement)
117+
.replace(/\\'/g, "'")
118+
.replace(/\\"/g, '"');
115119

116120
// For websites that use Trusted Types
117121
// https://w3c.github.io/webappsec-trusted-types/dist/spec/

tests/scriptlets/trusted-replace-node-text.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ test('simple case', (assert) => {
7272
}, 1);
7373
});
7474

75+
test('simple case - check if quotes are correctly escaped', (assert) => {
76+
const done = assert.async();
77+
78+
const nodeName = 'div';
79+
const textMatch = 'alert';
80+
const pattern = '/alert\\(\\\'test\\\'\\)/';
81+
const replacement = 'alert(\\\'replaced\\\')';
82+
const text = "alert('test')";
83+
const expectedText = "alert('replaced')";
84+
85+
const nodeBefore = addNode('div', text);
86+
const safeNodeBefore = addNode('a', text);
87+
88+
runScriptlet(name, [nodeName, textMatch, pattern, replacement]);
89+
90+
const nodeAfter = addNode('div', text);
91+
const safeNodeAfter = addNode('span', text);
92+
setTimeout(() => {
93+
assert.strictEqual(nodeAfter.textContent, expectedText, 'text content should be modified');
94+
assert.strictEqual(nodeBefore.textContent, expectedText, 'text content should be modified');
95+
96+
assert.strictEqual(safeNodeAfter.textContent, text, 'non-matched node should not be affected');
97+
assert.strictEqual(safeNodeBefore.textContent, text, 'non-matched node should not be affected');
98+
99+
assert.strictEqual(window.hit, 'FIRED', 'hit function should fire');
100+
done();
101+
}, 1);
102+
});
103+
75104
test('using matchers as regexes', (assert) => {
76105
const done = assert.async();
77106

0 commit comments

Comments
 (0)