Skip to content

Commit

Permalink
Merge branch 'release/v1.7' into fix/AG-14514
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 25, 2022
2 parents 1a0d9c5 + c01dd65 commit 53cfe6a
Show file tree
Hide file tree
Showing 22 changed files with 1,817 additions and 14 deletions.
30 changes: 30 additions & 0 deletions src/helpers/prepare-cookie.js → src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,33 @@ export const prepareCookie = (name, value) => {

return cookieData;
};

/**
* Parses cookie string into object
* @param {string} cookieString string that conforms to document.cookie format
* @returns {Object} key:value object that corresponds with incoming cookies keys and values
*/
export const parseCookieString = (cookieString) => {
const COOKIE_DELIMITER = '=';
const COOKIE_PAIRS_DELIMITER = ';';

// Get raw cookies
const cookieChunks = cookieString.split(COOKIE_PAIRS_DELIMITER);
const cookieData = {};

cookieChunks.forEach((singleCookie) => {
let cookieKey;
let cookieValue;
const delimiterIndex = singleCookie.indexOf(COOKIE_DELIMITER);
if (delimiterIndex === -1) {
cookieKey = singleCookie.trim();
} else {
cookieKey = singleCookie.slice(0, delimiterIndex).trim();
cookieValue = singleCookie.slice(delimiterIndex + 1);
}
// Save cookie key=value data with null instead of empty ('') values
cookieData[cookieKey] = cookieValue || null;
});

return cookieData;
};
20 changes: 17 additions & 3 deletions src/helpers/get-descriptor-addon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { randomId } from './random-id';
/**
* Prevent infinite loops when trapping props that could be used by scriptlet's own helpers
* Example: window.RegExp, that is used by matchStackTrace > toRegExp
*
* https://github.com/AdguardTeam/Scriptlets/issues/251
* https://github.com/AdguardTeam/Scriptlets/issues/226
* https://github.com/AdguardTeam/Scriptlets/issues/232
*
Expand All @@ -12,9 +14,21 @@ export function getDescriptorAddon() {
isAbortingSuspended: false,
isolateCallback(cb, ...args) {
this.isAbortingSuspended = true;
const result = cb(...args);
this.isAbortingSuspended = false;
return result;
// try...catch is required in case if there are more than one inline scripts
// which should be aborted.
// so after the first successful abortion, `cb(...args);` will throw error,
// and we should not stop on that and continue to abort other scripts
try {
const result = cb(...args);
this.isAbortingSuspended = false;
return result;
} catch {
this.isAbortingSuspended = false;
const rid = randomId();
// It's necessary to throw error
// otherwise script will be not aborted
throw new ReferenceError(rid);
}
},
};
}
5 changes: 3 additions & 2 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export * from './observer';
export * from './match-stack';
export * from './open-shadow-dom-utils';
export * from './array-utils';
export * from './prepare-cookie';
export * from './cookie-utils';
export * from './number-utils';
export * from './adjust-set-utils';
export * from './fetch-utils';
export * from './request-utils';
export * from './object-utils';
export * from './prevent-window-open-utils';
export * from './add-event-listener-utils';
Expand All @@ -26,3 +26,4 @@ export * from './regexp-utils';
export * from './random-response';
export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './match-request-props';
35 changes: 35 additions & 0 deletions src/helpers/match-request-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
getMatchPropsData,
validateParsedData,
parseMatchProps,
} from './request-utils';

/**
* Checks if given propsToMatch string matches with given request data
* This is used by prevent-xhr, prevent-fetch, trusted-replace-xhr-response
* and trusted-replace-fetch-response scriptlets
* @param {string} propsToMatch
* @param {Object} requestData object with standard properties of fetch/xhr like url, method etc
* @returns {boolean}
*/
export const matchRequestProps = (propsToMatch, requestData) => {
let isMatched;

const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
isMatched = false;
} else {
const matchData = getMatchPropsData(parsedData);
// prevent only if all props match
isMatched = Object.keys(matchData)
.every((matchKey) => {
const matchValue = matchData[matchKey];
return Object.prototype.hasOwnProperty.call(requestData, matchKey)
&& matchValue.test(requestData[matchKey]);
});
}

return isMatched;
};
19 changes: 19 additions & 0 deletions src/helpers/fetch-utils.js → src/helpers/request-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ export const getFetchData = (args) => {
return fetchPropsObj;
};

/**
* Collect xhr.open arguments to object
* @param {string} method
* @param {string} url
* @param {string} async
* @param {string} user
* @param {string} password
* @returns {Object}
*/
export const getXhrData = (method, url, async, user, password) => {
return {
method,
url,
async,
user,
password,
};
};

/**
* Parse propsToMatch input string into object;
* used for prevent-fetch and prevent-xhr
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const startsWith = (str, prefix) => {
export const endsWith = (str, ending) => {
// if str === '', (str && false) will return ''
// that's why it has to be !!str
return !!str && str.indexOf(ending) === str.length - ending.length;
return !!str && str.lastIndexOf(ending) === str.length - ending.length;
};

export const substringAfter = (str, separator) => {
Expand Down
4 changes: 3 additions & 1 deletion src/redirects/googlesyndication-adsbygoogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ export function GoogleSyndicationAdsByGoogle(source) {
for (const key of Object.keys(arg)) {
if (typeof arg[key] === 'function') {
try {
arg[key].call();
// https://github.com/AdguardTeam/Scriptlets/issues/252
// argument "{}" is needed to fix issue with undefined argument
arg[key].call(this, {});
} catch {
/* empty */
}
Expand Down
3 changes: 2 additions & 1 deletion src/redirects/pardot-1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
* @redirect pardot-1.0
*
* @description
* Mocks the pd.js file of Salesforce
* Mocks the pd.js file of Salesforce.
* https://pi.pardot.com/pd.js
* https://developer.salesforce.com/docs/marketing/pardot/overview
*
* **Example**
* ```
* ||pi.pardot.com/pd.js$script,redirect=pardot
Expand Down
2 changes: 1 addition & 1 deletion src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
* - value — range on numbers, for example `100-300`, limited to 500000 characters
*
* > Usage with no arguments will log XMLHttpRequest objects to browser console;
* which is useful for debugging but permitted for production filter lists.
* which is useful for debugging but not allowed for production filter lists.
*
* **Examples**
* 1. Log all XMLHttpRequests
Expand Down
3 changes: 3 additions & 0 deletions src/scriptlets/scriptlets-list.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* This file must export all scriptlets which should be accessible
*/
export * from './trusted-click-element';
export * from './abort-on-property-read';
export * from './abort-on-property-write';
export * from './prevent-setTimeout';
Expand Down Expand Up @@ -47,3 +48,5 @@ export * from './close-window';
export * from './prevent-refresh';
export * from './prevent-element-src-loading';
export * from './no-topics';
export * from './trusted-replace-xhr-response';
export * from './xml-prune';
Loading

0 comments on commit 53cfe6a

Please sign in to comment.