Skip to content

Commit

Permalink
AG-27143 Fix issue with setting values to wrong properties in set-con…
Browse files Browse the repository at this point in the history
…stant scriptlet. #373

Squashed commit of the following:

commit b4fb774
Merge: 751872d 0605d92
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Oct 27 15:41:18 2023 +0200

    Merge branch 'master' into fix/AG-27143

commit 751872d
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Oct 27 07:42:15 2023 +0200

    Update tests
    Remove global properties after tests

commit d1a58f9
Author: Adam Wróblewski <adam@adguard.com>
Date:   Thu Oct 26 21:28:05 2023 +0200

    Fix issue with setting values to wrong properties in set-constant scriptlet
  • Loading branch information
AdamWr committed Oct 30, 2023
1 parent 0605d92 commit 38df83c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
<!-- TODO: add @added tag to the files with specific version -->
<!-- during new scriptlets or redirects releasing -->

## [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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/scriptlets/set-constant.js
Expand Up @@ -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;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/scriptlets/set-constant.test.js
Expand Up @@ -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');
});
}

0 comments on commit 38df83c

Please sign in to comment.