From 38df83c56aef4cacdc0a5e705eacf9ef217d4a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wr=C3=B3blewski?= Date: Mon, 30 Oct 2023 19:55:06 +0300 Subject: [PATCH] AG-27143 Fix issue with setting values to wrong properties in set-constant scriptlet. #373 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Squashed commit of the following: commit b4fb7749b2905518f717bb45dfb2ddce79e10c19 Merge: 751872df 0605d92e Author: Adam Wróblewski Date: Fri Oct 27 15:41:18 2023 +0200 Merge branch 'master' into fix/AG-27143 commit 751872dfaeca1f6a0e38a6fa44bf3c4a5c82bf7e Author: Adam Wróblewski Date: Fri Oct 27 07:42:15 2023 +0200 Update tests Remove global properties after tests commit d1a58f9f139b0ed4d6444903dc8eda05b583cd39 Author: Adam Wróblewski Date: Thu Oct 26 21:28:05 2023 +0200 Fix issue with setting values to wrong properties in set-constant scriptlet --- CHANGELOG.md | 8 +++++ src/scriptlets/set-constant.js | 5 +++- tests/scriptlets/set-constant.test.js | 43 +++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acd24492..07759590 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 +## [Unreleased] + +### Fixed + +- issue with setting values to wrong properties in `set-constant` scriptlet + [#373](https://github.com/AdguardTeam/Scriptlets/issues/373) + ## [v1.9.83] - 2023-10-13 ### Added @@ -266,6 +273,7 @@ prevent inline `onerror` and match `link` tag [#276](https://github.com/AdguardT - `metrika-yandex-tag` [#254](https://github.com/AdguardTeam/Scriptlets/issues/254) - `googlesyndication-adsbygoogle` [#252](https://github.com/AdguardTeam/Scriptlets/issues/252) +[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.83...HEAD [v1.9.83]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.72...v1.9.83 [v1.9.72]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.70...v1.9.72 [v1.9.70]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.62...v1.9.70 diff --git a/src/scriptlets/set-constant.js b/src/scriptlets/set-constant.js index b308eab1..482cef01 100644 --- a/src/scriptlets/set-constant.js +++ b/src/scriptlets/set-constant.js @@ -271,7 +271,10 @@ export function setConstant(source, property, value, stack = '', valueWrapper = return false; } - base[prop] = constantValue; + if (base[prop]) { + base[prop] = constantValue; + } + if (origDescriptor.set instanceof Function) { prevSetter = origDescriptor.set; } diff --git a/tests/scriptlets/set-constant.test.js b/tests/scriptlets/set-constant.test.js index 744f4c39..b886e3fe 100644 --- a/tests/scriptlets/set-constant.test.js +++ b/tests/scriptlets/set-constant.test.js @@ -752,4 +752,47 @@ if (!isSupported) { // eslint-disable-next-line max-len assert.strictEqual(window.ads.videoAd.loadModule(), true, 'ads.videoAd.loadModule was set to function which returns true'); }); + + test('Test for reassignment 1', (assert) => { + runScriptletFromTag('zxcv.test.bar.qw', 'trueFunc'); + const funcOne = () => 1; + window.zxcv = {}; + // Reassign + window.zxcv = {}; + window.zxcv.mnb = true; + window.zxcv.test = { + bar: { + qw: funcOne, + }, + }; + const result = window.zxcv.test.bar.qw(); + assert.strictEqual(window.zxcv.mnb, true, 'redefined correctly'); + assert.strictEqual(result, true, 'redefined correctly by scriptlet'); + clearGlobalProps('zxcv'); + }); + + test('Test for reassignment 2', (assert) => { + runScriptletFromTag('WO.adblock.useAdblocker', 'false'); + + window.WO = window.WO || {}; + window.WO.strings = window.WO.strings || {}; + // Reassign + window.WO = window.WO || {}; + window.WO.strings = window.WO.strings || {}; + window.WO.test = 1; + window.WO.adblock = { + useAdblocker: true, + }; + assert.strictEqual(window.WO.test, 1, 'WO.test redefined correctly'); + assert.strictEqual(window.WO.adblock.useAdblocker, false, 'WO.adblock.useAdblocker set to false by scriptlet'); + clearGlobalProps('WO'); + }); + + test('Should not set', (assert) => { + runScriptletFromTag('something.start.stop', 'false'); + + window.something = window.something || {}; + assert.strictEqual(window.something.start, undefined, 'something.start was not set'); + clearGlobalProps('something'); + }); }