-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstolen-data-entry.ts
256 lines (231 loc) · 7.78 KB
/
stolen-data-entry.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import ExtendedRequest, { HAREntry } from './extended-request';
import { SaferEmitter } from './safer-emitter';
import {
getshorthost,
isBase64,
isBase64JSON,
isJSONObject,
isURL,
maskString,
parseToObject,
safeDecodeURIComponent,
} from './util';
export type Sources = 'cookie' | 'pathname' | 'queryparams' | 'header' | 'request_body';
export const Classifications = <const>{
id: 'Identyfikator internetowy',
history: 'Część historii przeglądania',
location: 'Informacje na temat mojego położenia',
};
const ID_PREVIEW_MAX_LENGTH = 20;
const MIN_COOKIE_LENGTH_FOR_AUTO_MARK = 15;
const id = (function* id() {
let i = 0;
while (true) {
i++;
yield i;
}
})();
export type DecodingSchema = 'base64' | 'raw';
export class StolenDataEntry extends SaferEmitter {
public isIAB = false;
public id: number;
private marked = false;
public classification: keyof typeof Classifications;
public decoding_applied: DecodingSchema = 'raw';
public decodings_available: DecodingSchema[] = ['raw'];
constructor(
public request: ExtendedRequest,
public source: Sources,
public name: string,
public value: string
) {
super();
this.id = id.next().value as number;
this.classification = this.classify();
if (isBase64(value)) {
this.decodings_available.push('base64');
}
}
getPriority() {
let priority = 0;
priority += Math.min(this.value.length, 50);
if (this.value.includes(this.request.originalHost)) {
priority += 100;
}
if (this.request.originalPathname && this.value.includes(this.request.originalPathname)) {
priority += 100;
}
if (this.source === 'cookie') {
priority += 200;
}
return priority;
}
get isMarked() {
return this.marked;
}
hasValue(value: string) {
return this.value === value;
}
static parseValue(value: unknown): string | Record<string, unknown> {
if (isBase64JSON(value)) {
return StolenDataEntry.parseValue({
base64: JSON.parse(atob(value)),
});
}
if (value === undefined) {
return '';
}
if (isJSONObject(value)) {
const object = parseToObject(value);
return object;
} else if (isURL(value)) {
const url = new URL(value);
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
);
}
}
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;
}
const object = {
[Symbol.for('originalString')]: value, // so it doesn't appear raw in the table but can be easily retrieved later
host: url.host,
path: url.pathname,
searchParams,
...(hash === '' ? {} : typeof hash === 'string' ? { hash } : hash),
};
return object;
} else if (value === null) {
return 'null';
} else {
return (value as any).toString();
}
}
getParsedValue(key_path: string): string | Record<string | symbol, unknown> {
let object = StolenDataEntry.parseValue(this.value);
for (const key of key_path.split('.')) {
if (key === '') continue;
if (typeof key === 'string') {
throw new Error('something went wrong when parsing ' + key_path);
}
object = StolenDataEntry.parseValue(object[key]);
}
return object;
}
mark() {
const had_been_marked_before = this.marked;
this.marked = true;
if (!had_been_marked_before) {
this.emit('change');
}
}
unmark() {
const had_been_marked_before = this.marked;
this.marked = false;
if (had_been_marked_before) {
this.emit('change', this.request.origin);
}
}
toggleMark() {
if (this.marked) {
this.unmark();
} else {
this.mark();
}
}
private classify(): keyof typeof Classifications {
let result: keyof typeof Classifications;
if (this.exposesOrigin()) {
result = 'history';
} else {
result = 'id';
}
return result;
}
isRelatedToID() {
return this.request.stolenData.some((entry) => entry.classification == 'id');
}
matchesHAREntry(har: HAREntry): boolean {
return this.request.matchesHAREntry(har);
}
getValuePreview(key = ''): string {
const value = this.getParsedValue(key);
const str =
typeof value === 'object' && value[Symbol.for('originalString')]
? (value[Symbol.for('originalString')] as string)
: value.toString();
if (typeof value !== 'object' && this.classification == 'id') {
return maskString(value, 1 / 3, ID_PREVIEW_MAX_LENGTH);
} else if (typeof value === 'object' && value[Symbol.for('originalString')]) {
return value[Symbol.for('originalString')] as string;
} else {
return str;
}
}
getUniqueKey() {
return this.request.shorthost + ';' + this.name + ';' + this.value;
}
exposesOrigin(): boolean {
return this.exposesHost() || this.exposesPath();
}
autoMark() {
if (
this.classification == 'history' ||
((this.source === 'cookie' ||
this.name.toLowerCase().includes('id') ||
this.name.toLowerCase().includes('cookie') ||
this.name.toLowerCase().includes('ga') ||
this.name.toLowerCase().includes('ses') ||
this.name.toLowerCase().includes('fb')) &&
this.value.length > MIN_COOKIE_LENGTH_FOR_AUTO_MARK)
) {
if (
(this.request.shorthost.includes('google') ||
this.request.shorthost.includes('youtube')) &&
this.name == 'CONSENT'
) {
// this cookie contains "YES" and might distract the person looking at it into thinking i gave consent on the reported site
return;
}
this.mark();
}
}
exposesPath() {
const pathname = this.request.originalPathname;
if (pathname === null) {
return false;
}
return (
this.request.originalPathname !== '/' &&
[this.value, safeDecodeURIComponent(this.value)].some((haystack) =>
haystack.includes(pathname)
)
);
}
exposesHost() {
return [this.value, safeDecodeURIComponent(this.value)].some((haystack) =>
haystack.includes(getshorthost(this.request.origin))
);
}
}