AdguardTeam / Scriptlets

AdGuard scriptlets library
GNU General Public License v3.0
148 stars 29 forks source link

Fix 'set-constant' — setProxyTrap() #403

Closed AdamWr closed 6 months ago

AdamWr commented 7 months ago

Currently it doesn't work correctly for falsy values.

Steps to reproduce:

  1. Add this rule:
    example.org#%#//scriptlet('set-constant', 'foo.bar.test', 'true', '', '', 'true')
  2. Go to - https://example.org/
  3. In browser console run:
    Code:
foo = {
  bar: {
    abc: {}
  }
};
console.log('Original foo.bar - foo.bar.test:', foo.bar.test);
foo.bar = {
  abc: {},
};
console.log('Rewritten foo.bar - foo.bar.test:', foo.bar.test);

or

Code: ```js foo = { bar: { abc: {} } }; console.log('Original foo.bar - foo.bar.test:', foo.bar.test); foo.bar = { abc: {}, test: false, }; console.log('Rewritten foo.bar - foo.bar.test:', foo.bar.test); ```

foo.bar.test should returns true.

Probably currentObj && should be removed from: https://github.com/AdguardTeam/Scriptlets/blob/2f91f88c788222db147667c56aba74ebdc6e0c96/src/scriptlets/set-constant.js#L320