Skip to content

Commit

Permalink
add trusted-replace-fetch-response scriptlet AG-17043
Browse files Browse the repository at this point in the history
Merge in ADGUARD-FILTERS/scriptlets from feature/AG-17043 to release/v1.7

Squashed commit of the following:

commit d9ab3c1
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 20:17:18 2022 +0300

    fix logging in all request scriptlets

commit 8d7d5a3
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 19:56:34 2022 +0300

    improve log/matching forks for prevent-fetch, replace-fetch, replace-xhr

commit d2f0562
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 17:01:05 2022 +0300

    fix log condition

commit 545d6b3
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 15:10:31 2022 +0300

    move 'match all' logic to matchRequestProps logic

commit 88e0d03
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 13:42:16 2022 +0300

    improve forgeResponse and rename replacingRequest > forgedRequest for xhr scriptlet

commit 7de569d
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 13:13:39 2022 +0300

    remove unused helper

commit 65ee825
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 13:12:20 2022 +0300

    fix description

commit 1227b94
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Nov 1 13:06:36 2022 +0300

    fix toRegExp => toRegExp()

commit e50b102
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 18:44:08 2022 +0300

    use matchRequestProps helper in prevent-xhr & prevent-fetch

commit 53b1904
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 18:39:52 2022 +0300

    move helpers dependencies lower on the list

commit 9e76de8
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 18:27:15 2022 +0300

    add testcases

commit dc8ab9d
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 17:53:50 2022 +0300

    add testcases

commit 522ade9
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 17:20:00 2022 +0300

    add replacement logic

commit edafb05
Merge: e1ac8b5 0bc5d92
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 31 14:09:47 2022 +0300

    Merge branch 'release/v1.7' into feature/AG-17043

commit e1ac8b5
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Oct 25 14:10:02 2022 +0300

    merge parent branch & use matchRequestProps

commit b2cf83c
Merge: 039c5b0 c01dd65
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Oct 25 13:34:46 2022 +0300

    Merge branch 'release/v1.7' into feature/AG-17043

commit 039c5b0
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Tue Oct 25 13:34:01 2022 +0300

    add tests stub & fix logging logic

commit acef717
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 24 19:48:53 2022 +0300

    add replacement logic & improve description

commit f96716d
Author: Stanislav A <s.atroschenko@adguard.com>
Date:   Mon Oct 24 13:03:53 2022 +0300

    add trsuted-replace-fetch-response scriptlet
  • Loading branch information
stanislav-atr committed Nov 3, 2022
1 parent 0bc5d92 commit 788f73a
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 72 deletions.
4 changes: 4 additions & 0 deletions src/helpers/match-request-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
* @returns {boolean}
*/
export const matchRequestProps = (propsToMatch, requestData) => {
if (propsToMatch === '' || propsToMatch === '*') {
return true;
}

let isMatched;

const parsedData = parseMatchProps(propsToMatch);
Expand Down
45 changes: 17 additions & 28 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import {
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -16,6 +14,9 @@ import {
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -96,6 +97,9 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);

let strResponseBody;
if (responseBody === 'emptyObj') {
strResponseBody = '{}';
Expand All @@ -108,7 +112,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
// Skip disallowed response types
if (!(responseType === 'default' || responseType === 'opaque')) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${responseType}`);
log(`Invalid parameter: ${responseType}`);
return;
}

Expand All @@ -117,29 +121,13 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
const fetchData = getFetchData(args);
if (typeof propsToMatch === 'undefined') {
// log if no propsToMatch given
const logMessage = `log: fetch( ${objectToString(fetchData)} )`;
hit(source, logMessage);
} else if (propsToMatch === '' || propsToMatch === getWildcardSymbol()) {
// prevent all fetch calls
shouldPrevent = true;
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(fetchData, matchKey)
&& matchValue.test(fetchData[matchKey]);
});
}
log(`fetch( ${objectToString(fetchData)} )`);
hit(source);
return Reflect.apply(target, thisArg, args);
}

shouldPrevent = matchRequestProps(propsToMatch);

if (shouldPrevent) {
hit(source);
return noopPromiseResolve(strResponseBody, fetchData.url, responseType);
Expand Down Expand Up @@ -167,16 +155,17 @@ preventFetch.injections = [
hit,
getFetchData,
objectToString,
parseMatchProps,
validateParsedData,
getMatchPropsData,
noopPromiseResolve,
getWildcardSymbol,
matchRequestProps,
toRegExp,
isValidStrPattern,
escapeRegExp,
isEmptyObject,
getRequestData,
getObjectEntries,
getObjectFromEntries,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];
43 changes: 15 additions & 28 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import {
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
matchRequestProps,
// following helpers should be imported and injected
// because they are used by helpers above
toRegExp,
Expand All @@ -18,6 +16,9 @@ import {
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
} from '../helpers/index';

/* eslint-disable max-len */
Expand Down Expand Up @@ -94,6 +95,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);

let shouldPrevent = false;
let response = '';
let responseText = '';
Expand All @@ -107,27 +111,10 @@ export function preventXHR(source, propsToMatch, customResponseText) {
responseUrl = xhrData.url;
if (typeof propsToMatch === 'undefined') {
// Log if no propsToMatch given
const logMessage = `log: xhr( ${objectToString(xhrData)} )`;
hit(source, logMessage);
} else if (propsToMatch === '' || propsToMatch === getWildcardSymbol()) {
// Prevent all fetch calls
shouldPrevent = true;
log(`log: xhr( ${objectToString(xhrData)} )`);
hit(source);
} else {
const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
shouldPrevent = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
shouldPrevent = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(xhrData, matchKey)
&& matchValue.test(xhrData[matchKey]);
});
}
shouldPrevent = matchRequestProps(propsToMatch);
}

return Reflect.apply(target, thisArg, args);
Expand All @@ -151,8 +138,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
if (randomText) {
responseText = randomText;
} else {
// eslint-disable-next-line no-console
console.log(`Invalid range: ${customResponseText}`);
log(`Invalid range: ${customResponseText}`);
}
}
// Mock response object
Expand Down Expand Up @@ -205,9 +191,7 @@ preventXHR.injections = [
hit,
objectToString,
getWildcardSymbol,
parseMatchProps,
validateParsedData,
getMatchPropsData,
matchRequestProps,
getRandomIntInclusive,
getRandomStrByLength,
generateRandomResponse,
Expand All @@ -219,4 +203,7 @@ preventXHR.injections = [
getNumberFromString,
nativeIsFinite,
nativeIsNaN,
parseMatchProps,
validateParsedData,
getMatchPropsData,
];
1 change: 1 addition & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ export * from './prevent-element-src-loading';
export * from './no-topics';
export * from './trusted-replace-xhr-response';
export * from './xml-prune';
export * from './trusted-replace-fetch-response';
Loading

0 comments on commit 788f73a

Please sign in to comment.