2021-10-03 16:33:23 +02:00
|
|
|
import { EventEmitter } from "events";
|
|
|
|
import ExtendedRequest from "./extended-request";
|
2021-11-22 17:54:15 +01:00
|
|
|
import { Sources, StolenDataEntry } from "./stolen-data-entry";
|
2021-10-04 18:51:51 +02:00
|
|
|
|
2021-11-22 12:03:55 +01:00
|
|
|
import { allSubhosts, isSameURL, reduceConcat, unique } from "./util";
|
|
|
|
|
|
|
|
const source_priority: Array<Sources> = [
|
|
|
|
"cookie",
|
|
|
|
"pathname",
|
|
|
|
"queryparams",
|
|
|
|
"header",
|
|
|
|
];
|
2021-10-03 20:13:36 +02:00
|
|
|
|
2021-10-03 16:33:23 +02:00
|
|
|
export class RequestCluster extends EventEmitter {
|
|
|
|
public requests: ExtendedRequest[] = [];
|
|
|
|
constructor(public id: string) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
add(request: ExtendedRequest) {
|
|
|
|
this.requests.push(request);
|
|
|
|
this.emit("change");
|
|
|
|
}
|
|
|
|
|
|
|
|
hasCookies() {
|
|
|
|
for (const request of this.requests) {
|
|
|
|
if (request.hasCookie()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-22 17:54:15 +01:00
|
|
|
getRepresentativeStolenData(
|
|
|
|
filter: {
|
|
|
|
minValueLength: number;
|
|
|
|
cookiesOnly: boolean;
|
|
|
|
cookiesOrOriginOnly: boolean;
|
|
|
|
} = { minValueLength: 0, cookiesOnly: false, cookiesOrOriginOnly: false }
|
|
|
|
): StolenDataEntry[] {
|
2021-10-04 18:51:51 +02:00
|
|
|
return this.requests
|
2021-11-07 11:18:53 +01:00
|
|
|
.map((request) => request.stolenData)
|
2021-11-22 17:54:15 +01:00
|
|
|
|
2021-10-03 16:33:23 +02:00
|
|
|
.reduce((a, b) => a.concat(b), [])
|
2021-10-04 18:51:51 +02:00
|
|
|
.filter((entry) => {
|
|
|
|
return entry.value.length >= filter.minValueLength;
|
|
|
|
})
|
2021-11-06 20:02:02 +01:00
|
|
|
.filter((entry) => !filter.cookiesOnly || entry.source === "cookie")
|
2021-11-07 19:27:03 +01:00
|
|
|
.filter(
|
|
|
|
(entry) =>
|
|
|
|
!filter.cookiesOrOriginOnly ||
|
|
|
|
entry.source === "cookie" ||
|
|
|
|
entry.classification === "history"
|
|
|
|
)
|
2021-11-22 17:54:15 +01:00
|
|
|
.sort((entry1, entry2) => {
|
|
|
|
if (entry1.value > entry2.value) {
|
|
|
|
return -1;
|
|
|
|
} else if (entry1.value < entry2.value) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
const indexA = source_priority.indexOf(entry1.source);
|
|
|
|
const indexB = source_priority.indexOf(entry2.source);
|
|
|
|
if (indexA < indexB) {
|
|
|
|
return -1;
|
|
|
|
} else if (indexA > indexB) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return entry1.value.length > entry2.value.length ? -1 : 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((_, index, array) => {
|
|
|
|
// removing value duplicates
|
2021-10-04 18:51:51 +02:00
|
|
|
if (index == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (
|
2021-11-22 17:54:15 +01:00
|
|
|
array[index].getValuePreview() ===
|
|
|
|
array[index - 1].getValuePreview() ||
|
|
|
|
(array[index].classification === "history" &&
|
|
|
|
array[index - 1].classification === "history") || // if they're both history, then the first one is the longest
|
|
|
|
isSameURL(array[index].value, array[index - 1].value)
|
2021-10-04 18:51:51 +02:00
|
|
|
) {
|
2021-11-22 17:54:15 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2021-10-04 18:51:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-11-06 22:32:40 +01:00
|
|
|
})
|
2021-11-22 17:54:15 +01:00
|
|
|
.sort((entry1, entry2) => {
|
|
|
|
if (entry1.name < entry2.name) {
|
|
|
|
return -1;
|
|
|
|
} else if (entry1.name > entry2.name) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (entry1.value.length > entry2.value.length) {
|
|
|
|
return 1;
|
2021-11-06 22:32:40 +01:00
|
|
|
} else {
|
2021-11-22 17:54:15 +01:00
|
|
|
return -1;
|
2021-11-06 22:32:40 +01:00
|
|
|
}
|
2021-11-22 17:54:15 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter((_, index, array) => {
|
|
|
|
// removing name duplicates, keeping only the first - which is the longest. Some data loss may occur.
|
|
|
|
if (index == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (array[index].name === array[index - 1].name) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
})
|
2021-11-06 22:32:40 +01:00
|
|
|
.sort((entry1, entry2) =>
|
|
|
|
entry1.getPriority() > entry2.getPriority() ? -1 : 1
|
|
|
|
);
|
2021-10-03 16:33:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sortCompare(a: RequestCluster, b: RequestCluster) {
|
|
|
|
if (a.hasCookies() == b.hasCookies()) {
|
|
|
|
if (a.id < b.id) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (a.hasCookies()) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 15:45:26 +01:00
|
|
|
|
|
|
|
getMarkedRequests() {
|
|
|
|
return this.requests.filter((request) => request.hasMark());
|
|
|
|
}
|
2021-11-07 17:18:17 +01:00
|
|
|
|
|
|
|
getFullHosts() {
|
|
|
|
return unique(
|
|
|
|
this.requests
|
|
|
|
.map((request) => allSubhosts(request.getHost()))
|
|
|
|
.reduce((a, b) => a.concat(b), [])
|
|
|
|
);
|
|
|
|
}
|
2021-11-07 19:03:00 +01:00
|
|
|
|
|
|
|
hasMarks() {
|
|
|
|
return this.requests.some((request) => request.hasMark());
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:21:31 +01:00
|
|
|
getMarkedEntries(): StolenDataEntry[] {
|
2021-11-07 19:03:00 +01:00
|
|
|
return this.requests
|
|
|
|
.map((request) => request.getMarkedEntries())
|
|
|
|
.reduce(reduceConcat, []);
|
|
|
|
}
|
2021-11-07 19:27:03 +01:00
|
|
|
|
|
|
|
exposesOrigin() {
|
|
|
|
return this.requests.some((request) => request.exposesOrigin());
|
|
|
|
}
|
2021-10-03 16:33:23 +02:00
|
|
|
}
|