Skip to content

Commit

Permalink
Add http/browser session
Browse files Browse the repository at this point in the history
  • Loading branch information
Jyyjy committed Feb 13, 2024
1 parent 456b2f3 commit 94cdcdc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 8 deletions.
29 changes: 24 additions & 5 deletions src/UserALEWebExtension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const defaultConfig = {
};

var urlWhitelist;
var tabToHttpSession = {};
var browserSessionId = null;

function updateConfig(config) {
urlWhitelist = new RegExp(config.pluginConfig.urlWhitelist);
Expand All @@ -63,27 +65,44 @@ function filterUrl(log) {
return false;
}

// Sets http session id of tab logs to that of the target tab
function injectSessions(log) {
let id = log.details.id;
if(id in tabToHttpSession) {
log.httpSession = tabToHttpSession[id];
} else {
log.httpSession = null
}
log.browserSessionId = browserSessionId;
return log;
}

browser.storage.local.get(defaultConfig, (res) => {
// Apply url filter to logs generated by the background page.
userale.addCallbacks({filterUrl});
userale.addCallbacks({filterUrl, injectSessions});
updateConfig(res);
browserSessionId = JSON.parse(window.sessionStorage.getItem('userAleHttpSessionId'));
});

browser.runtime.onMessage.addListener(function (message, sender, sendResponse) {
switch (message.type) {
// Handles logs rerouted from content and option scripts.
case MessageTypes.ADD_LOG:
let log = message.payload;
if("tab" in sender && "id" in sender.tab) {
log["tabId"] = sender.tab.id;
}
log.browserSessionId = browserSessionId;
// Apply url filter to logs generated outside the background page.
log = filterUrl(log);
if(log) {
userale.log(log);
}
break;

case MessageTypes.HTTP_SESSION:
if("tab" in sender && "id" in sender.tab) {
tabToHttpSession[sender.tab.id] = message.payload;
}
break;

case MessageTypes.CONFIG_CHANGE:
updateConfig(message.payload);
break;
Expand All @@ -101,7 +120,7 @@ function packageTabLog(tabId, data, type) {
}

function packageDetailedTabLog(tab, data, type) {
Object.assign(data, {'tabEvent': type});
Object.assign(data, {type});
userale.packageCustomLog(data, ()=>{return tab}, true);
}

Expand Down
4 changes: 4 additions & 0 deletions src/UserALEWebExtension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import { rerouteLog, browser } from './globals.js';
browser.storage.local.get("useraleConfig", (res) => {
userale.options(res.useraleConfig);
userale.addCallbacks({rerouteLog});

// Send httpSession to background scirpt to inject into tab events.
let payload = JSON.parse(window.sessionStorage.getItem('userAleHttpSessionId'));
browser.runtime.sendMessage({type: MessageTypes.HTTP_SESSION, payload});
});

browser.runtime.onMessage.addListener(function (message) {
Expand Down
1 change: 1 addition & 0 deletions src/UserALEWebExtension/messageTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ const prefix = 'USERALE_';

export const CONFIG_CHANGE = prefix + 'CONFIG_CHANGE';
export const ADD_LOG = prefix + 'ADD_LOG';
export const HTTP_SESSION = prefix + 'HTTP_SESSION';
24 changes: 23 additions & 1 deletion src/getInitialSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

let sessionId = null;
let httpSessionId = null;

/**
* Extracts the initial configuration settings from the
Expand All @@ -29,6 +30,10 @@ export function getInitialSettings() {
sessionId = getSessionId('userAleSessionId', 'session_' + String(Date.now()));
}

if (httpSessionId === null) {
httpSessionId = getSessionId('userAleHttpSessionId', generateHttpSessionId());
}

const script = document.currentScript || (function () {
const scripts = document.getElementsByTagName('script');
return scripts[scripts.length - 1];
Expand All @@ -49,6 +54,8 @@ export function getInitialSettings() {
settings.userFromParams = get('data-user-from-params') || null;
settings.time = timeStampScale(document.createEvent('CustomEvent'));
settings.sessionID = get('data-session') || sessionId;
settings.httpSessionId = httpSessionId;
settings.browserSessionId = null;
settings.authHeader = get('data-auth') || null;
settings.custIndex = get('data-index') || null;
settings.headers = get('data-headers') || null;
Expand All @@ -70,7 +77,6 @@ export function getSessionId(sessionKey, value) {
return JSON.parse(window.sessionStorage.getItem(sessionKey));
}


/**
* Creates a function to normalize the timestamp of the provided event.
* @param {Object} e An event containing a timeStamp property.
Expand Down Expand Up @@ -108,3 +114,19 @@ export function timeStampScale(e) {

return tsScaler;
}

/**
* Creates a cryptographiclly random string to represent this http session.
* @return {String} A random 32 digit hex string
*/
function generateHttpSessionId() {
// 32 digit hex -> 128 bits of info -> 2^64 ~= 10^19 sessions needed for 50% chance of collison
let len = 32;
var arr = new Uint8Array(len / 2);
window.crypto.getRandomValues(arr);
return Array.from(arr,
(dec) => {
return dec.toString(16).padStart(2, "0");
}
).join('');
}
10 changes: 8 additions & 2 deletions src/packageLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ export function packageLog(e, detailFcn) {
'toolName' : config.toolName,
'useraleVersion': config.useraleVersion,
'sessionID': config.sessionID,
'httpSessionId': config.httpSessionId,
'browserSessionId': config.browserSessionId,
};

if ((typeof filterHandler === 'function') && !filterHandler(log)) {
Expand Down Expand Up @@ -198,7 +200,9 @@ export function packageCustomLog(customLog, detailFcn, userAction) {
'toolVersion' : config.version,
'toolName' : config.toolName,
'useraleVersion': config.useraleVersion,
'sessionID': config.sessionID
'sessionID': config.sessionID,
'httpSessionId': config.httpSessionId,
'browserSessionId': config.browserSessionId,
};

let log = Object.assign(metaData, customLog);
Expand Down Expand Up @@ -282,7 +286,9 @@ export function packageIntervalLog(e) {
'toolVersion': config.version,
'toolName': config.toolName,
'useraleVersion': config.useraleVersion,
'sessionID': config.sessionID
'sessionID': config.sessionID,
'httpSessionId': config.httpSessionId,
'browserSessionId': config.browserSessionId,
};

if (typeof filterHandler === 'function' && !filterHandler(intervalLog)) {
Expand Down
2 changes: 2 additions & 0 deletions test/main_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('Userale API', () => {
'logCountThreshold',
'userId',
'sessionID',
'httpSessionId',
'browserSessionId',
'version',
'logDetails',
'resolution',
Expand Down

0 comments on commit 94cdcdc

Please sign in to comment.