Skip to content

Commit

Permalink
Move function to helper
Browse files Browse the repository at this point in the history
Fix eslint
Add comment about location hash
Use upper snake case in constants
Add comment to regexp pattern
Add description to syntax
  • Loading branch information
AdamWr committed Nov 15, 2022
1 parent 0ab3d04 commit 3782873
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 49 deletions.
67 changes: 67 additions & 0 deletions src/helpers/check-if-inline-or-injected-script-in-stack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { startsWith } from './string-utils';

export const isInlineScript = (stackMatch) => stackMatch.indexOf('inlineScript') > -1;
export const isInjectedScript = (stackMatch) => stackMatch.indexOf('injectedScript') > -1;

/**
* Determines if type of script is inline or injected
* and when it's one of them then return true, otherwise false
* https://github.com/AdguardTeam/Scriptlets/issues/201
* @param {string|undefined} stackMatch - input stack value to match
* @param {string} stackTrace - script error stack trace
* @returns {boolean}
*/
export const isInlineOrInjectedScript = (stackMatch, stackTrace) => {
const INJECTED_SCRIPT = 'injectedScript';
const INJECTED_SCRIPT_MARKER = '<anonymous>';
let documentURL = window.location.href;
const pos = documentURL.indexOf('#');
// Remove URL hash
// in Chrome, URL in stackTrace doesn't contain hash
// so, it's necessary to remove it, otherwise location.href
// will not match with location from stackTrace
if (pos !== -1) {
documentURL = documentURL.slice(0, pos);
}
const stackSteps = stackTrace.split('\n').slice(2).map((line) => line.trim());
const stackLines = stackSteps.map((line) => {
let stack;
// Get stack trace URL
// in Firefox stack trace looks like this: advanceTaskQueue@http://127.0.0.1:8080/scriptlets/tests/dist/qunit.js:1834:20
// in Chrome like this: at Assert.throws (http://127.0.0.1:8080/scriptlets/tests/dist/qunit.js:3178:16)
// so, first group "(.*?@)" is required for Firefox, second group contains URL
const getStackTraceURL = /(.*?@)?(\S+)(:\d+):\d+\)?$/.exec(line);
if (getStackTraceURL) {
let stackURL = getStackTraceURL[2];
if (startsWith(stackURL, '(')) {
stackURL = stackURL.slice(1);
}
if (startsWith(stackURL, INJECTED_SCRIPT_MARKER)) {
stackURL = INJECTED_SCRIPT;
let stackFunction = getStackTraceURL[1] !== undefined
? getStackTraceURL[1].slice(0, -1)
: line.slice(0, getStackTraceURL.index).trim();
if (startsWith(stackFunction, 'at')) {
stackFunction = stackFunction.slice(2).trim();
}
stack = `${stackFunction} ${stackURL}`.trim();
} else {
stack = stackURL;
}
} else {
stack = line;
}
return stack;
});
if (stackLines) {
for (let index = 0; index < stackLines.length; index += 1) {
if (isInlineScript(stackMatch) && documentURL === stackLines[index]) {
return true;
}
if (isInjectedScript(stackMatch) && startsWith(stackLines[index], INJECTED_SCRIPT)) {
return true;
}
}
}
return false;
};
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './noop';
export * from './hit';
export * from './observer';
export * from './match-stack';
export * from './check-if-inline-or-injected-script-in-stack';
export * from './open-shadow-dom-utils';
export * from './array-utils';
export * from './cookie-utils';
Expand Down
52 changes: 5 additions & 47 deletions src/helpers/match-stack.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toRegExp, startsWith } from './string-utils';
import { toRegExp } from './string-utils';
import { isInlineScript, isInjectedScript, isInlineOrInjectedScript } from './check-if-inline-or-injected-script-in-stack';
import { getNativeRegexpTest } from './regexp-utils';

/**
Expand All @@ -13,52 +14,9 @@ export const matchStackTrace = (stackMatch, stackTrace) => {
return true;
}

const isInlineScript = stackMatch.indexOf('inlineScript') > -1;
const isInjectedScript = stackMatch.indexOf('injectedScript') > -1;
if (isInlineScript || isInjectedScript) {
const injectedScript = 'injectedScript';
const injectedScriptMatch = '<anonymous>';
let documentURL = window.location.href;
const pos = documentURL.indexOf('#');
if (pos !== -1) {
documentURL = documentURL.slice(0, pos);
}
const stackSteps = stackTrace.split('\n').slice(2).map((line) => line.trim());
const stackLines = stackSteps.map((line) => {
let stack;
const reg = /(.*?@)?(\S+)(:\d+):\d+\)?$/.exec(line);
if (reg) {
let stackURL = reg[2];
if (startsWith(stackURL, '(')) {
stackURL = stackURL.slice(1);
}
if (startsWith(stackURL, injectedScriptMatch)) {
stackURL = injectedScript;
let stackFunction = reg[1] !== undefined
? reg[1].slice(0, -1)
: line.slice(0, reg.index).trim();
if (startsWith(stackFunction, 'at')) {
stackFunction = stackFunction.slice(2).trim();
}
stack = `${stackFunction} ${stackURL}`.trim();
} else {
stack = stackURL;
}
} else {
stack = line;
}
return stack;
});
if (stackLines) {
for (let index = 0; index < stackLines.length; index += 1) {
if (isInlineScript && documentURL === stackLines[index]) {
return true;
}
if (isInjectedScript && startsWith(stackLines[index], injectedScript)) {
return true;
}
}
}
if ((isInlineScript(stackMatch) || isInjectedScript(stackMatch))
&& isInlineOrInjectedScript(stackMatch, stackTrace)) {
return true;
}

const stackRegexp = toRegExp(stackMatch);
Expand Down
9 changes: 9 additions & 0 deletions src/scriptlets/abort-on-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
isValidStrPattern,
matchStackTrace,
getDescriptorAddon,
isInlineScript,
isInjectedScript,
isInlineOrInjectedScript,
// following helpers are needed for helpers above
escapeRegExp,
toRegExp,
Expand All @@ -32,6 +35,9 @@ import {
*
* - `property` - required, path to a property. The property must be attached to window.
* - `stack` - required, string that must match the current function call stack trace.
* - values to abort inline or injected script, accordingly:
* - `inlineScript`
* - `injectedScript`
*
* **Examples**
* ```
Expand Down Expand Up @@ -149,4 +155,7 @@ abortOnStackTrace.injections = [
isEmptyObject,
getNativeRegexpTest,
startsWith,
isInlineScript,
isInjectedScript,
isInlineOrInjectedScript,
];
4 changes: 2 additions & 2 deletions src/scriptlets/log-on-stack-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export function logOnStacktrace(source, property) {
const regFirefox = /(.*?@)(\S+)(:\d+):\d+\)?$/;
if (line.match(reg)) {
funcName = line.split(' ').slice(0, -1).join(' ');
/* eslint-disable-next-line prefer-destructuring, no-useless-escape */
/* eslint-disable-next-line prefer-destructuring */
funcFullPath = line.match(reg)[1];
} else if (line.match(regFirefox)) {
funcName = line.split('@').slice(0, -1).join(' ');
/* eslint-disable-next-line prefer-destructuring, no-useless-escape */
/* eslint-disable-next-line prefer-destructuring */
funcFullPath = line.match(regFirefox)[2];
} else {
// For when func name is not available
Expand Down

0 comments on commit 3782873

Please sign in to comment.