Skip to content

Fix 'trusted-suppress-native-method' — reset 'isMatchingSuspended' when stack is not matched #496

Open
@AdamWr

Description

@AdamWr

Steps to reproduce:

  1. Add this rule:
example.org#%#//scriptlet('trusted-suppress-native-method', 'Document.prototype.querySelector', '"h1"', 'prevent', 'testFunc')
  1. Go to - https://example.org/
  2. In browser console run:
Code:
const testFunc = () => {
    // console.log('testFunc');

    const h1 = document.querySelector('h1');
    if (h1 === undefined) {
        console.log('h1 is undefined, prevented');
        return;
    }
    console.log(h1);
};
testFunc();

const test = () => {
    // console.log('test');

    const testQuerySelector = document.querySelector('querySelector');
};
test();

const testFunc1 = () => {
    // console.log('testFunc1');

    const h1 = document.querySelector('h1');
    if (h1 === undefined) {
        console.log('h1 is undefined, prevented');
        return;
    }
    console.log(h1);
};
testFunc1();

It should print h1 is undefined, prevented 2 times in console, but it looks like that isMatchingSuspended is not reset when stack is not matched:

if (stack && !matchStackTrace(stack, new Error().stack || '')) {
return Reflect.apply(target, thisArg, argumentsList);
}

so second time is not prevented and element <h1>Example Domain</h1> is logged, but it should not be (it works fine when test() is commented out).

Adding isMatchingSuspended = false; before return Reflect.apply(target, thisArg, argumentsList); should fixes it.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions