2021-11-22 17:54:15 +01:00
|
|
|
// import { TCModel } from "@iabtcf/core";
|
2021-11-21 23:00:55 +01:00
|
|
|
import { EventEmitter } from "events";
|
2021-11-08 20:14:28 +01:00
|
|
|
import ExtendedRequest, { HAREntry } from "./extended-request";
|
2021-11-22 15:08:29 +01:00
|
|
|
|
2021-11-07 17:44:22 +01:00
|
|
|
import {
|
2021-11-07 19:15:40 +01:00
|
|
|
getshorthost,
|
2021-11-22 18:23:11 +01:00
|
|
|
isBase64,
|
2021-11-22 15:08:29 +01:00
|
|
|
isBase64JSON,
|
2021-11-07 17:44:22 +01:00
|
|
|
isJSONObject,
|
|
|
|
isURL,
|
2021-11-22 18:56:36 +01:00
|
|
|
maskString,
|
2021-11-07 17:44:22 +01:00
|
|
|
parseToObject,
|
|
|
|
} from "./util";
|
2021-11-07 17:20:58 +01:00
|
|
|
|
|
|
|
export type Sources = "cookie" | "pathname" | "queryparams" | "header";
|
|
|
|
|
2021-11-07 17:44:22 +01:00
|
|
|
export const Classifications = <const>{
|
|
|
|
id: "Sztucznie nadane ID",
|
|
|
|
history: "Część historii przeglądania",
|
2021-11-11 20:25:06 +01:00
|
|
|
location: "Informacje na temat mojego położenia",
|
2021-11-07 17:44:22 +01:00
|
|
|
};
|
|
|
|
|
2021-11-21 18:21:31 +01:00
|
|
|
const ID_PREVIEW_MAX_LENGTH = 20;
|
|
|
|
|
2021-11-07 17:20:58 +01:00
|
|
|
const id = (function* id() {
|
|
|
|
let i = 0;
|
|
|
|
while (true) {
|
|
|
|
i++;
|
|
|
|
yield i;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
2021-11-22 18:23:11 +01:00
|
|
|
export type DecodingSchema = "base64" | "raw";
|
2021-11-22 15:08:29 +01:00
|
|
|
|
2021-11-21 23:00:55 +01:00
|
|
|
export class StolenDataEntry extends EventEmitter {
|
2021-11-07 17:20:58 +01:00
|
|
|
public isIAB = false;
|
2021-11-22 17:54:15 +01:00
|
|
|
// public iab: TCModel | null = null;
|
2021-11-07 17:20:58 +01:00
|
|
|
public id: number;
|
2021-11-22 17:54:15 +01:00
|
|
|
private marked = false;
|
2021-11-07 17:44:22 +01:00
|
|
|
public classification: keyof typeof Classifications;
|
2021-11-22 18:23:11 +01:00
|
|
|
public decoding_applied: DecodingSchema = "raw";
|
|
|
|
public decodings_available: DecodingSchema[] = ["raw"];
|
2021-11-07 17:20:58 +01:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public request: ExtendedRequest,
|
|
|
|
public source: Sources,
|
|
|
|
public name: string,
|
|
|
|
public value: string
|
|
|
|
) {
|
|
|
|
// try {
|
|
|
|
// this.iab = TCString.decode(value);
|
|
|
|
// // console.log(this.iab);
|
|
|
|
// this.isIAB = true;
|
|
|
|
// } catch (e) {}
|
2021-11-21 23:00:55 +01:00
|
|
|
super();
|
2021-11-07 17:20:58 +01:00
|
|
|
this.id = id.next().value as number;
|
2021-11-07 17:44:22 +01:00
|
|
|
this.classification = this.classify();
|
2021-11-22 18:23:11 +01:00
|
|
|
if (isBase64(value)) {
|
|
|
|
this.decodings_available.push("base64");
|
2021-11-22 15:08:29 +01:00
|
|
|
}
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getPriority() {
|
|
|
|
let priority = 0;
|
|
|
|
priority += Math.min(this.value.length, 50);
|
2021-11-07 18:02:01 +01:00
|
|
|
const url = new URL(this.request.originalURL);
|
2021-11-07 17:20:58 +01:00
|
|
|
if (this.value.includes(url.host)) {
|
|
|
|
priority += 100;
|
|
|
|
}
|
|
|
|
if (this.value.includes(url.pathname)) {
|
|
|
|
priority += 100;
|
|
|
|
}
|
|
|
|
if (this.source === "cookie") {
|
|
|
|
priority += 200;
|
|
|
|
}
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
2021-11-22 17:54:15 +01:00
|
|
|
get isMarked() {
|
|
|
|
return this.marked;
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hasValue(value: string) {
|
|
|
|
return this.value === value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static parseValue(value: unknown): string | Record<string, unknown> {
|
2021-11-22 17:54:15 +01:00
|
|
|
if (isBase64JSON(value)) {
|
|
|
|
return StolenDataEntry.parseValue({ base64: JSON.parse(atob(value)) });
|
|
|
|
}
|
2021-11-22 13:28:31 +01:00
|
|
|
if (value === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
2021-11-07 17:20:58 +01:00
|
|
|
if (isJSONObject(value)) {
|
|
|
|
const object = parseToObject(value);
|
|
|
|
return object;
|
|
|
|
} else if (isURL(value)) {
|
|
|
|
const url = new URL(value);
|
2021-11-21 22:38:32 +01:00
|
|
|
let hash = url.hash;
|
|
|
|
if (hash.includes("=")) {
|
|
|
|
//facebook sometimes includes querystring-encoded data into the hash... attempt to parse it
|
|
|
|
try {
|
|
|
|
hash = Object.fromEntries(
|
|
|
|
hash
|
|
|
|
.slice(1)
|
|
|
|
.split("&")
|
|
|
|
.map((kv) => kv.split("="))
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
// failed to parse as query string
|
|
|
|
console.log(
|
|
|
|
"Failed attempt to parse hash location as query string, probably safe to ignore:",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 14:50:37 +01:00
|
|
|
const searchParams = Object.fromEntries(
|
|
|
|
((url.searchParams as unknown) as {
|
|
|
|
entries: () => Iterable<[string, string]>;
|
|
|
|
}).entries()
|
|
|
|
);
|
|
|
|
if (typeof hash !== "object" && Object.keys(searchParams).length === 0) {
|
|
|
|
return value; // just a string;
|
|
|
|
}
|
2021-11-07 17:20:58 +01:00
|
|
|
const object = {
|
2021-11-22 13:28:31 +01:00
|
|
|
[Symbol.for("originalString")]: value, // so it doesn't appear raw in the table but can be easily retrieved later
|
2021-11-07 17:20:58 +01:00
|
|
|
host: url.host,
|
|
|
|
path: url.pathname,
|
2021-11-22 14:50:37 +01:00
|
|
|
searchParams,
|
2021-11-21 22:38:32 +01:00
|
|
|
...(hash === "" ? {} : typeof hash === "string" ? { hash } : hash),
|
2021-11-07 17:20:58 +01:00
|
|
|
};
|
|
|
|
return object;
|
2021-11-22 14:50:37 +01:00
|
|
|
} else if (value === null) {
|
|
|
|
return "null";
|
2021-11-07 17:20:58 +01:00
|
|
|
} else {
|
|
|
|
return value.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-21 18:21:31 +01:00
|
|
|
getParsedValue(key_path: string): string | Record<string | symbol, unknown> {
|
2021-11-07 17:20:58 +01:00
|
|
|
let object = StolenDataEntry.parseValue(this.value);
|
|
|
|
for (const key of key_path.split(".")) {
|
|
|
|
if (key === "") continue;
|
|
|
|
object = StolenDataEntry.parseValue(object[key]);
|
|
|
|
}
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2021-11-22 17:54:15 +01:00
|
|
|
mark() {
|
|
|
|
this.marked = true;
|
2021-11-21 23:00:55 +01:00
|
|
|
this.emit("change");
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:54:15 +01:00
|
|
|
unmark() {
|
|
|
|
this.marked = false;
|
2021-11-21 23:00:55 +01:00
|
|
|
this.emit("change");
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 17:54:15 +01:00
|
|
|
toggleMark() {
|
|
|
|
if (this.marked) {
|
|
|
|
this.unmark();
|
2021-11-07 17:20:58 +01:00
|
|
|
} else {
|
2021-11-22 17:54:15 +01:00
|
|
|
this.mark();
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 17:44:22 +01:00
|
|
|
|
|
|
|
private classify(): keyof typeof Classifications {
|
2021-11-07 19:15:40 +01:00
|
|
|
let result: keyof typeof Classifications;
|
|
|
|
if (
|
|
|
|
[this.value, decodeURIComponent(this.value)].some((haystack) =>
|
|
|
|
[
|
|
|
|
this.request.origin,
|
|
|
|
this.request.originalURL,
|
|
|
|
getshorthost(this.request.origin),
|
|
|
|
].some((needle) => haystack.includes(needle))
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
result = "history";
|
2021-11-07 17:44:22 +01:00
|
|
|
} else {
|
2021-11-07 19:15:40 +01:00
|
|
|
result = "id";
|
2021-11-07 17:44:22 +01:00
|
|
|
}
|
2021-11-07 19:15:40 +01:00
|
|
|
return result;
|
2021-11-07 17:44:22 +01:00
|
|
|
}
|
2021-11-07 17:51:30 +01:00
|
|
|
|
|
|
|
isRelatedToID() {
|
|
|
|
return this.request.stolenData.some(
|
2021-11-24 01:03:32 +01:00
|
|
|
(entry) => entry.classification == "id"
|
2021-11-07 17:51:30 +01:00
|
|
|
);
|
|
|
|
}
|
2021-11-08 20:14:28 +01:00
|
|
|
|
|
|
|
matchesHAREntry(har: HAREntry): boolean {
|
|
|
|
return this.request.matchesHAREntry(har);
|
|
|
|
}
|
2021-11-21 18:21:31 +01:00
|
|
|
|
|
|
|
getValuePreview(key = ""): string {
|
|
|
|
const value = this.getParsedValue(key);
|
2021-11-22 13:28:31 +01:00
|
|
|
const str =
|
|
|
|
typeof value === "object" && value[Symbol.for("originalString")]
|
|
|
|
? (value[Symbol.for("originalString")] as string)
|
|
|
|
: value.toString();
|
2021-11-21 22:38:32 +01:00
|
|
|
if (typeof value !== "object" && this.classification == "id") {
|
2021-11-22 18:56:36 +01:00
|
|
|
return maskString(value, 1 / 3, ID_PREVIEW_MAX_LENGTH);
|
2021-11-22 13:28:31 +01:00
|
|
|
} else if (
|
|
|
|
typeof value === "object" &&
|
|
|
|
value[Symbol.for("originalString")]
|
|
|
|
) {
|
|
|
|
return value[Symbol.for("originalString")] as string;
|
2021-11-21 18:21:31 +01:00
|
|
|
} else {
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 17:20:58 +01:00
|
|
|
|
|
|
|
getUniqueKey() {
|
2021-11-22 17:54:15 +01:00
|
|
|
return this.request.shorthost + ";" + this.name + ";" + this.value;
|
2021-11-22 15:08:29 +01:00
|
|
|
}
|
2021-11-22 18:23:11 +01:00
|
|
|
|
|
|
|
exposesOrigin(): boolean {
|
|
|
|
return this.value.includes(getshorthost(this.request.origin));
|
|
|
|
}
|
2021-11-07 17:20:58 +01:00
|
|
|
}
|