Skip to content

Commit

Permalink
fix closure bug for prevent-xhr and add testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 16, 2022
1 parent 7fd04c9 commit 4f1d59b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ export function preventXHR(source, propsToMatch, customResponseText) {
return;
}

let shouldPrevent = false;
let response = '';
let responseText = '';
let responseUrl;
Expand All @@ -111,15 +110,15 @@ export function preventXHR(source, propsToMatch, customResponseText) {
// Log if no propsToMatch given
logMessage(source, `xhr( ${objectToString(xhrData)} )`, true);
hit(source);
} else {
shouldPrevent = matchRequestProps(source, propsToMatch, xhrData);
} else if (matchRequestProps(source, propsToMatch, xhrData)) {
thisArg.shouldBePrevented = true;
}

return Reflect.apply(target, thisArg, args);
};

const sendWrapper = (target, thisArg, args) => {
if (!shouldPrevent) {
if (!thisArg.shouldBePrevented) {
return Reflect.apply(target, thisArg, args);
}

Expand Down
52 changes: 45 additions & 7 deletions tests/scriptlets/prevent-xhr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ if (isSupported) {
xhr.send();
});

test('Args, method matched, randomize response text, rangeMin equal to rangeMax (length:100-100)', async (assert) => {
test('Args, method matched, randomize response text, rangeMin === rangeMax (length:100-100)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['method:GET', 'length:100-100'];
Expand Down Expand Up @@ -203,7 +203,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - limit range (rangeMin + rangeMax - length:8888888888888888-99999999999999999999999)', async (assert) => {
test('Empty arg, prevent all, dont randomize response - limit range (rangeMin+rangeMax-length)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length:8888888888888888-99999999999999999999999'];
Expand All @@ -224,7 +224,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - limit range (rangeMax - length:10000-600000)', async (assert) => {
test('Empty arg, prevent all, dont randomize response text - limit range (rangeMax - length)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length:10000-600000'];
Expand Down Expand Up @@ -286,7 +286,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - invalid argument (length:test-30000)', async (assert) => {
test('Empty arg, prevent all, dont randomize response - invalid argument (length:test-30000)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length:test-30000'];
Expand Down Expand Up @@ -346,7 +346,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - invalid argument (length:123-345-450)', async (assert) => {
test('Empty arg, prevent all, dont randomize response - invalid argument (length:123-345-450)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length:123-345-450'];
Expand All @@ -366,7 +366,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - invalid argument (length:123---450)', async (assert) => {
test('Empty arg, prevent all, dont randomize response - invalid argument (length:123---450)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length:123---450'];
Expand All @@ -386,7 +386,7 @@ if (isSupported) {
xhr.send();
});

test('Empty arg, prevent all, do not randomize response text - invalid argument (length::123-450)', async (assert) => {
test('Empty arg, prevent all, dont randomize response - invalid argument (length::123-450)', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;
const MATCH_DATA = ['', 'length::123-450'];
Expand Down Expand Up @@ -588,6 +588,44 @@ if (isSupported) {
};
xhr.send();
});

// https://github.com/AdguardTeam/Scriptlets/issues/261
test('Works correctly with different parallel XHR requests', async (assert) => {
const METHOD = 'GET';
const URL_TO_BLOCK = `${FETCH_OBJECTS_PATH}/test01.json`;
const URL_TO_PASS = `${FETCH_OBJECTS_PATH}/test02.json`;
const MATCH_DATA = ['test01.json'];

runScriptlet(name, MATCH_DATA);

const done1 = assert.async();
const done2 = assert.async();

const xhr1 = new XMLHttpRequest();
const xhr2 = new XMLHttpRequest();

xhr1.open(METHOD, URL_TO_PASS);
xhr2.open(METHOD, URL_TO_BLOCK);

xhr1.onload = () => {
clearGlobalProps('hit');
assert.strictEqual(xhr1.readyState, 4, 'Response done');
assert.ok(xhr1.response, 'Response data exists');
assert.strictEqual(window.hit, undefined, 'hit should not fire');
done1();
};

xhr2.onload = () => {
assert.strictEqual(xhr2.readyState, 4, 'Response done');
assert.strictEqual(typeof xhr2.responseText, 'string', 'Response text mocked');
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
clearGlobalProps('hit');
done2();
};

xhr1.send();
xhr2.send();
});
} else {
test('unsupported', (assert) => {
assert.ok(true, 'Browser does not support it');
Expand Down

0 comments on commit 4f1d59b

Please sign in to comment.