Skip to content

Commit

Permalink
adding hasOwnProperty helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
BatMiles committed Aug 30, 2024
1 parent 70239b4 commit 8d40d3c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
25 changes: 19 additions & 6 deletions src/cookie-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { join } from 'path';
import { Page } from 'puppeteer';
import { getDomain, getHostname } from 'tldts';
import { Cookie } from 'tough-cookie';
import { getScriptUrl } from './utils';
import { getScriptUrl, hasOwnProperty } from './utils';

const parseCookie = (cookieStr, fpUrl) => {
const cookie = Cookie.parse(cookieStr);
try {
Expand Down Expand Up @@ -73,7 +74,7 @@ export const getHTTPCookies = (events, url): any[] => {
m.data
.filter(c => c)
.map(d => ({
domain: d.hasOwnProperty('domain') ? d.domain : getHostname(url),
domain: hasOwnProperty(d, 'domain') ? d.domain : getHostname(url),
name: d.key,
path: d.path,
script: getScriptUrl(m),
Expand All @@ -96,10 +97,21 @@ export const getJsCookies = (events, url) => {
)
.map(d => {
const data = parseCookie(d.data.value, url);
const hasOwnDomain = d.hasOwnProperty('domain') && d.domain !== null && d.domain !== undefined;
const hasOwnName = data && data.hasOwnProperty('key') && data.key !== null && data.key !== undefined;
const hasOwnPath = data && data.hasOwnProperty('path') && data.path !== null && data.path !== undefined;
const hasOwnValue = data && data.hasOwnProperty('value') && data.value !== null && data.value !== undefined;
const hasOwnDomain = hasOwnProperty(d, 'domain') &&
d.domain !== null &&
d.domain !== undefined;
const hasOwnName = data &&
hasOwnProperty(data, 'key') &&
data.key !== null &&
data.key !== undefined;
const hasOwnPath = data &&
hasOwnProperty(data, 'path') &&
data.path !== null &&
data.path !== undefined;
const hasOwnValue = data &&
hasOwnProperty(data, 'value') &&
data.value !== null &&
data.value !== undefined;
const script = getScriptUrl(d);

return {
Expand All @@ -112,6 +124,7 @@ export const getJsCookies = (events, url) => {
};
});
};

export const matchCookiesToEvents = (cookies, events, url) => {
const jsCookies = getJsCookies(events, url);
const httpCookie = getHTTPCookies(events, url);
Expand Down
21 changes: 13 additions & 8 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ import {
JsInstrumentEvent,
KeyLoggingEvent,
SessionRecordingEvent,
TrackingRequestEvent
TrackingRequestEvent,
} from './types';
import { getScriptUrl, groupBy, loadJSONSafely } from './utils';
import {
getScriptUrl,
groupBy,
loadJSONSafely,
hasOwnProperty,
} from './utils';

export const generateReport = (reportType, messages, dataDir, url) => {
const eventData = getEventData(reportType, messages);
Expand Down Expand Up @@ -42,6 +47,7 @@ export const generateReport = (reportType, messages, dataDir, url) => {
const filterByEvent = (messages, typePattern) => {
return messages.filter(m => m.message.type.includes(typePattern) && !m.message.type.includes('Error'));
};

const getEventData = (reportType, messages): BlacklightEvent[] => {
let filtered = [];
switch (reportType) {
Expand Down Expand Up @@ -78,6 +84,7 @@ const getEventData = (reportType, messages): BlacklightEvent[] => {
}
return filtered.map(m => m.message);
};

const reportSessionRecorders = (eventData: BlacklightEvent[]) => {
const report = {};
eventData.forEach((event: SessionRecordingEvent) => {
Expand Down Expand Up @@ -118,9 +125,8 @@ const reportEventListeners = (eventData: BlacklightEvent[]) => {
return acc;
}

if (acc.hasOwnProperty(data.event_group)) {
// console.log(data.event_group, script, cur.symbol);
if (acc[data.event_group].hasOwnProperty(script)) {
if (hasOwnProperty(acc, data.event_group)) {
if (hasOwnProperty(acc[data.event_group], script)) {
acc[data.event_group][script].add(data.name);
} else {
acc[data.event_group][script] = new Set([data.name]);
Expand Down Expand Up @@ -184,9 +190,8 @@ const reportFingerprintableAPIs = (eventData: BlacklightEvent[]) => {
return acc;
}

if (acc.hasOwnProperty(cur.api_group)) {
// console.log(cur.api_group, script, cur.symbol);
if (acc[cur.api_group].hasOwnProperty(script)) {
if (hasOwnProperty(acc, cur.api_group)) {
if (hasOwnProperty(acc[cur.api_group], script)) {
acc[cur.api_group][script].add(cur.symbol);
} else {
acc[cur.api_group][script] = new Set([cur.symbol]);
Expand Down
22 changes: 16 additions & 6 deletions src/pptr-utils/get-links.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LinkObject } from '../types';
import { hasOwnProperty } from '../utils';

export const getLinks = async (page): Promise<LinkObject[]> => {
return page.evaluate(() => {
Expand All @@ -11,8 +12,12 @@ export const getLinks = async (page): Promise<LinkObject[]> => {
inner_text: a.innerText
};
})
.filter(link => {
return link.href.startsWith('http') && !link.href.endsWith('.pdf') && !link.href.endsWith('.zip');
.filter((link:LinkObject) => {
return (
link.href.startsWith('http') &&
!link.href.endsWith('.pdf') &&
!link.href.endsWith('.zip')
);
});
} catch (error) {
return [];
Expand All @@ -22,10 +27,15 @@ export const getLinks = async (page): Promise<LinkObject[]> => {

// https://dev.to/vuevixens/removing-duplicates-in-an-array-of-objects-in-js-with-sets-3fep
export const dedupLinks = (links_with_duplicates: LinkObject[]) => {
const links = Array.from(new Set(links_with_duplicates.filter(f => f && f.hasOwnProperty('href')).map(link => link.href))).map(href => {
return links_with_duplicates.find(link => link.href === href);
});

const sanitizedLinks = links_with_duplicates.filter(f => f && hasOwnProperty(f, 'href'));
const dedupedLinkArray = Array.from(new Set(sanitizedLinks));
// I don't think the bellow modification actually does anything,
// but I'm gonna write tests for this function before pulling the plug
const links = dedupedLinkArray
.map((link:LinkObject) => link.href)
.map(href => {
return links_with_duplicates.find(link => link.href === href);
});
return links;
};

Expand Down
11 changes: 8 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { join } from 'path';
import { getDomain } from 'tldts';
import { BlacklightEvent } from './types';


export const hasOwnProperty = (object:object, property:string) => {
return Object.prototype.hasOwnProperty.call(object, property);
};

const deleteFolderRecursive = path => {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(file => {
Expand Down Expand Up @@ -77,7 +82,7 @@ export const getScriptUrl = (item: BlacklightEvent) => {
const { stack } = item;

for (let i = 0; i < stack.length; i++) {
if (Object.prototype.hasOwnProperty.call(stack[i], 'fileName')) {
if (hasOwnProperty(stack[i], 'fileName')) {
return stack[i].fileName;
} else {
if (i === stack.length - 1) {
Expand All @@ -101,7 +106,7 @@ export const getStackType = (stack, firstPartyDomain) => {
let hasFirstParty = false;
let hasThirdParty = false;
stack.forEach(s => {
if (Object.prototype.hasOwnProperty.call(s, 'fileName')) {
if (hasOwnProperty(s, 'fileName')) {
const scriptDomain = getDomain(s.fileName);
if (scriptDomain === firstPartyDomain) {
hasFirstParty = true;
Expand All @@ -128,4 +133,4 @@ export const getHashedValues = (algorithm, object) => {
acc[cur[0]] = algorithm === 'base64' ? Buffer.from(cur[1]).toString('base64') : getStringHash(algorithm, cur[1]);
return acc;
}, {});
};
};

0 comments on commit 8d40d3c

Please sign in to comment.