Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with prevent-fetch and prevent-xhr
Browse files Browse the repository at this point in the history
AdamWr committed Jun 28, 2023
1 parent 97e7764 commit b0a4bc3
Showing 5 changed files with 74 additions and 12 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,12 @@ 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 `prevent-fetch` and `prevent-xhr` scriptlets [#334](https://github.com/AdguardTeam/Scriptlets/issues/334)

## <a name="v1.9.37"></a> [v1.9.37] - 2023-06-06

### Added
@@ -180,7 +186,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.37...HEAD
[v1.9.37]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.7...v1.9.37
[v1.9.7]: https://github.com/AdguardTeam/Scriptlets/compare/v1.9.1...v1.9.7
[v1.9.1]: https://github.com/AdguardTeam/Scriptlets/compare/v1.8.2...v1.9.1
25 changes: 17 additions & 8 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import {
objectToString,
matchRequestProps,
logMessage,
noopPromiseResolve,
modifyResponse,
// following helpers should be imported and injected
// because they are used by helpers above
@@ -156,14 +157,21 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re

if (shouldPrevent) {
hit(source);
const origResponse = await Reflect.apply(target, thisArg, args);
return modifyResponse(
origResponse,
{
body: strResponseBody,
type: responseType,
},
);
try {
const origResponse = await Reflect.apply(target, thisArg, args);
if (!origResponse.ok) {
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
}
return modifyResponse(
origResponse,
{
body: strResponseBody,
type: responseType,
},
);
} catch (ex) {
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
}
}

return Reflect.apply(target, thisArg, args);
@@ -190,6 +198,7 @@ preventFetch.injections = [
objectToString,
matchRequestProps,
logMessage,
noopPromiseResolve,
modifyResponse,
toRegExp,
isValidStrPattern,
6 changes: 3 additions & 3 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
@@ -185,19 +185,19 @@ export function preventXHR(source, propsToMatch, customResponseText) {
readyState,
responseURL,
responseXML,
status,
statusText,
} = forgedRequest;

// Mock response object
Object.defineProperties(thisArg, {
// original values
readyState: { value: readyState, writable: false },
status: { value: status, writable: false },
statusText: { value: statusText, writable: false },
responseURL: { value: responseURL, writable: false },
// If request is blocked by extension, then responseURL is an empty string
responseURL: { value: responseURL || xhrData.url, writable: false },
responseXML: { value: responseXML, writable: false },
// modified values
status: { value: 200, writable: false },
response: { value: modifiedResponse, writable: false },
responseText: { value: modifiedResponseText, writable: false },
});
21 changes: 21 additions & 0 deletions tests/scriptlets/prevent-fetch.test.js
Original file line number Diff line number Diff line change
@@ -139,6 +139,27 @@ if (!isSupported) {
done();
});

test('prevent not existing fetch call', async (assert) => {
// blocked_request.json doesn't exist,
// it's required for test for blocked requests
const BLOCKED_REQUEST = `${FETCH_OBJECTS_PATH}/blocked_request.json`;
const inputRequest = new Request(BLOCKED_REQUEST);

runScriptlet(name, ['blocked_request']);
const done = assert.async(1);

const response = await fetch(inputRequest);
const parsedData = await response.json();

if (!response.ok) {
assert.strictEqual(response.ok, true, 'Request blocked');
done();
}
assert.ok(isEmptyObject(parsedData), 'Response is mocked');
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
done();
});

test('simple fetch - match single pair prop', async (assert) => {
const INPUT_JSON_PATH = `${FETCH_OBJECTS_PATH}/test03.json`;
const options = {
26 changes: 26 additions & 0 deletions tests/scriptlets/prevent-xhr.test.js
Original file line number Diff line number Diff line change
@@ -508,6 +508,32 @@ if (isSupported) {
xhr.send();
});

test('Args matched, prevent blocked request', async (assert) => {
// blocked_request.json doesn't exist,
// it's required for test for blocked requests
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/blocked_request.json`;
const MATCH_DATA = [`blocked_request method:${METHOD}`];

runScriptlet(name, MATCH_DATA);

const done = assert.async();

const reqListener = (e) => {
assert.strictEqual(e.target.status, 200, 'Status mocked');
assert.ok(e.target.responseURL.includes('/blocked_request.json'), 'Origianl URL mocked');
assert.strictEqual(e.target.readyState, 4, 'Response done');
assert.strictEqual(e.target.response, '', 'Response data mocked');
assert.strictEqual(window.hit, 'FIRED', 'hit function fired');
done();
};

const xhr = new XMLHttpRequest();
xhr.open(METHOD, URL);
xhr.addEventListener('load', reqListener);
xhr.send();
});

test('Args, match, listeners after .send work', async (assert) => {
const METHOD = 'GET';
const URL = `${FETCH_OBJECTS_PATH}/test01.json`;

0 comments on commit b0a4bc3

Please sign in to comment.