Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vringar committed Sep 10, 2023
1 parent a3702cd commit bdf7960
Show file tree
Hide file tree
Showing 6 changed files with 1,026 additions and 820 deletions.
2 changes: 1 addition & 1 deletion Extension/src/background/javascript-instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class JavascriptInstrument {
matchAboutBlank: true,
});
}
const entryScript = (this.legacy) ? "/content.js" : "/stealth.js";
const entryScript = this.legacy ? "/content.js" : "/stealth.js";
return browser.contentScripts.register({
js: [{ file: entryScript }],
matches: ["<all_urls>"],
Expand Down
20 changes: 12 additions & 8 deletions Extension/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main() {
navigation_instrument: true,
cookie_instrument: true,
js_instrument: true,
stealth_js_instrument:false,
stealth_js_instrument: false,
cleaned_js_instrument_settings: [
{
object: `window.CanvasRenderingContext2D.prototype`,
Expand Down Expand Up @@ -72,16 +72,20 @@ async function main() {
const cookieInstrument = new CookieInstrument(loggingDB);
cookieInstrument.run(config.browser_id);
}
if (config['stealth_js_instrument']) {
if (config.stealth_js_instrument) {
loggingDB.logDebug("Stealth JavaScript Instrumentation enabled");
let stealthJSInstrument = new JavascriptInstrument(loggingDB, false);
stealthJSInstrument.run(config['browser_id']);
const stealthJSInstrument = new JavascriptInstrument(loggingDB, false);
stealthJSInstrument.run(config.browser_id);
await stealthJSInstrument.registerContentScript();
} if (config['js_instrument']) {
}
if (config.js_instrument) {
loggingDB.logDebug("Javascript instrumentation enabled");
let jsInstrument = new JavascriptInstrument(loggingDB, true);
jsInstrument.run(config['browser_id']);
await jsInstrument.registerContentScript(config['testing'], config['cleaned_js_instrument_settings']);
const jsInstrument = new JavascriptInstrument(loggingDB, true);
jsInstrument.run(config.browser_id);
await jsInstrument.registerContentScript(
config.testing,
config.cleaned_js_instrument_settings,
);
}

if (config.http_instrument) {
Expand Down
96 changes: 49 additions & 47 deletions Extension/src/stealth/error.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/*
* Functionality to generate error objects
*/
function generateErrorObject(err, context){
function generateErrorObject(err, context) {
// TODO: Pass context
context = (context !== undefined) ? context : window;
const cleaned = cleanErrorStack(err.stack)
context = context !== undefined ? context : window;
const cleaned = cleanErrorStack(err.stack);
const stack = splitStack(cleaned);
const lineInfo = getLineInfo(stack);
const fileName = getFileName(stack);
let fakeError;
try{
let fakeError: { lineNumber: any; columnNumber: any };
try {
// fake type, message, filename, column and line
const propertyName = "stack";
// const propertyName = "stack";
fakeError = new context.wrappedJSObject[err.name](err.message, fileName);
fakeError.lineNumber = lineInfo.lineNumber;
fakeError.columnNumber = lineInfo.columnNumber;
}catch(error){
} catch (error) {
console.log("ERROR creation failed. Error was:" + error);
}
return fakeError;
Expand All @@ -26,10 +26,10 @@ function generateErrorObject(err, context){
*/
function cleanErrorStack(stack) {
const extensionID = browser.runtime.getURL("");
const lines = (typeof(stack) !== "string") ? stack : splitStack(stack);
lines.forEach(line =>{
if (line.includes(extensionID)){
stack = stack.replace(line+"\n","");
const lines = typeof stack !== "string" ? stack : splitStack(stack);
lines.forEach((line) => {
if (line.includes(extensionID)) {
stack = stack.replace(line + "\n", "");
}
});
return stack;
Expand All @@ -40,9 +40,9 @@ function cleanErrorStack(stack) {
*/
function getBeginOfScriptCalls(stack) {
const extensionID = browser.runtime.getURL("");
const lines = (typeof(stack) !== "string") ? stack : splitStack(stack);
for (let i=0; i<lines.length; i++){
if (!lines[i].includes(extensionID)){
const lines = typeof stack !== "string" ? stack : splitStack(stack);
for (let i = 0; i < lines.length; i++) {
if (!lines[i].includes(extensionID)) {
return i;
}
}
Expand All @@ -52,8 +52,10 @@ function getBeginOfScriptCalls(stack) {
/*
* Get the stack as array
*/
function splitStack(stack){
return stack.split('\n').map(function (line) { return line.trim(); });
function splitStack(stack) {
return stack.split("\n").map(function (line) {
return line.trim();
});
}

/*
Expand All @@ -64,15 +66,17 @@ function getLineInfo(stack) {
const firstLine = stack[0];
const matches = [...firstLine.matchAll(":")];
const column = firstLine.slice(
matches[matches.length-1].index + 1,
firstLine.length);
matches[matches.length - 1].index + 1,
firstLine.length,
);
const line = firstLine.slice(
matches[matches.length-2].index + 1,
matches[matches.length-1].index);
matches[matches.length - 2].index + 1,
matches[matches.length - 1].index,
);
return {
"lineNumber": line,
"columnNumber": column
}
lineNumber: line,
columnNumber: column,
};
}

/*
Expand All @@ -84,35 +88,34 @@ function getFileName(stack) {
const matches_at = [...firstLine.matchAll("@")];
const matches_colon = [...firstLine.matchAll(":")];
return firstLine.slice(
matches_at[matches_at.length-1].index + 1,
matches_colon[matches_colon.length-2].index
matches_at[matches_at.length - 1].index + 1,
matches_colon[matches_colon.length - 2].index,
);
}

function getOriginFromStackTrace(err, includeStack){
console.log(err.stack);
// function getOriginFromStackTrace(err, includeStack){
// console.log(err.stack);

const stack = splitStack(err.stack);
const lineInfo = getLineInfo(stack);
const fileName = getFileName(stack);

const callSite = stack[1];
const callSiteParts = callSite.split("@");
const funcName = callSiteParts[0] || "";
const items = rsplit(callSiteParts[1], ":", 2);
const scriptFileName = items[items.length - 3] || "";
// const stack = splitStack(err.stack);
// const lineInfo = getLineInfo(stack);
// const fileName = getFileName(stack);

const callContext = {
scriptUrl,
scriptLine: lineInfo.lineNumber,
scriptCol: lineInfo.columnNumber,
funcName,
scriptLocEval,
callStack: includeStack ? trace.slice(3).join("\n").trim() : "",
};
// const callSite = stack[1];
// const callSiteParts = callSite.split("@");
// const funcName = callSiteParts[0] || "";
// const items = rsplit(callSiteParts[1], ":", 2);
// const scriptFileName = items[items.length - 3] || "";

}
// const callContext = {
// scriptUrl,
// scriptLine: lineInfo.lineNumber,
// scriptCol: lineInfo.columnNumber,
// funcName,
// scriptLocEval,
// callStack: includeStack ? trace.slice(3).join("\n").trim() : "",
// };

// }

// Helper to get originating script urls
// Legacy code
Expand All @@ -128,5 +131,4 @@ function getStackTrace() {
return stack;
}


export { generateErrorObject, getBeginOfScriptCalls, getStackTrace };
export { generateErrorObject, getBeginOfScriptCalls, getStackTrace };
Loading

0 comments on commit bdf7960

Please sign in to comment.