@whotracksme/reporting 5.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,519 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
import { isHash } from './hash-detector';
|
|
14
|
+
//import { isHash } from './hash-detector-new';
|
|
15
|
+
|
|
16
|
+
function isCharNumber(char) {
|
|
17
|
+
const code = char.charCodeAt(0);
|
|
18
|
+
return code >= 48 && code <= 57; // ASCII range for 0-9
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// precondition: isCharNumber(char) === true
|
|
22
|
+
function uncheckedCharToNumber(char) {
|
|
23
|
+
return char.charCodeAt(0) - 48; // 48 == ASCII '0'
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// https://en.wikipedia.org/wiki/International_Article_Number
|
|
27
|
+
// In the US, also known as GTIN or UPTC.
|
|
28
|
+
export function isValidEAN13(ean) {
|
|
29
|
+
if (ean.length !== 13 || ![...ean].every(isCharNumber)) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
let sum = 0;
|
|
33
|
+
for (let i = 0; i < 12; i += 1) {
|
|
34
|
+
const factor = i % 2 === 0 ? 1 : 3;
|
|
35
|
+
sum += factor * uncheckedCharToNumber(ean[i]);
|
|
36
|
+
}
|
|
37
|
+
const checksum = 10 - (sum % 10);
|
|
38
|
+
return checksum === uncheckedCharToNumber(ean[12]);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// https://en.wikipedia.org/wiki/International_Standard_Serial_Number
|
|
42
|
+
export function isValidISSN(issn) {
|
|
43
|
+
if (!/^[0-9]{4}-?[0-9]{3}[0-9xX]$/.test(issn)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
issn = issn.replace('-', '');
|
|
47
|
+
|
|
48
|
+
let checksum = 0;
|
|
49
|
+
for (let i = 0; i < 7; i++) {
|
|
50
|
+
checksum += uncheckedCharToNumber(issn[i]) * (8 - i);
|
|
51
|
+
}
|
|
52
|
+
const endsWithX = issn[7] === 'x' || issn[7] === 'X';
|
|
53
|
+
checksum += endsWithX ? 10 : uncheckedCharToNumber(issn[7]);
|
|
54
|
+
|
|
55
|
+
return checksum % 11 === 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Returns true if the given string contains any text that looks
|
|
60
|
+
* like an email address. The check is conservative, that means
|
|
61
|
+
* false positives are expected, but false negatives are not.
|
|
62
|
+
*/
|
|
63
|
+
function checkForEmail(str) {
|
|
64
|
+
return /[a-z0-9\-_@]+(@|%40|%(25)+40)[a-z0-9\-_]+\.[a-z0-9\-_]/i.test(str);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Intended to filter out potentially problematic numbers.
|
|
69
|
+
* Tries to reduce the number of false-positives by detecting certain common
|
|
70
|
+
* product IDs (EAN, ISSN), which are common in search, but don't have personal
|
|
71
|
+
* information.
|
|
72
|
+
*
|
|
73
|
+
* Otherwise, it discard queries that contain numbers longer than 7 digits.
|
|
74
|
+
* So, 123456 is still allowed, but phone numbers like (090)90-2 or 5555 3235
|
|
75
|
+
* will be dropped.
|
|
76
|
+
*
|
|
77
|
+
* Note:
|
|
78
|
+
* - the current implementation discard anything that contains full dates
|
|
79
|
+
* (e.g. "2023/05/17", "17.05.2023").
|
|
80
|
+
* (TODO: perhaps this restriction should be reconsidered to allow a search
|
|
81
|
+
* like "What happened on 24.12.1914?")
|
|
82
|
+
*/
|
|
83
|
+
function hasLongNumber(str) {
|
|
84
|
+
// allow one ISSN number
|
|
85
|
+
const issn = str.split(' ').find(isValidISSN);
|
|
86
|
+
if (issn) {
|
|
87
|
+
str = str.replace(issn, ' ');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const numbers = str
|
|
91
|
+
.replace(/[^A-Za-z0-9]/g, '')
|
|
92
|
+
.replace(/[^0-9]+/g, ' ')
|
|
93
|
+
.trim()
|
|
94
|
+
.split(' ')
|
|
95
|
+
.filter((num) => num.length > 2);
|
|
96
|
+
if (numbers.length === 1) {
|
|
97
|
+
const num = numbers[0];
|
|
98
|
+
if (num.length === 13 && str.includes(num)) {
|
|
99
|
+
const isEAN = isValidEAN13(num);
|
|
100
|
+
return !isEAN;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return numbers.some((num) => num.length > 7);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function isLogogramChar(char) {
|
|
108
|
+
const codePoint = char.codePointAt(0);
|
|
109
|
+
|
|
110
|
+
// Chinese: Range of Unicode code points for common Chinese characters
|
|
111
|
+
if (codePoint >= 0x4e00 && codePoint <= 0x9fff) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Japanese: Range of Unicode code points for Hiragana and Katakana characters
|
|
116
|
+
if (codePoint >= 0x3040 && codePoint <= 0x30ff) {
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Korean: Range of Unicode code points for Hangul syllables
|
|
121
|
+
if (codePoint >= 0xac00 && codePoint <= 0xd7af) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Thai: Range of Unicode code points for Thai characters
|
|
126
|
+
if (codePoint >= 0x0e00 && codePoint <= 0x0e7f) {
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Most languages have an alphabet where a word consist of multiple characters.
|
|
135
|
+
* But other languages (e.g. Chinese) use logograms, where a single character
|
|
136
|
+
* is equivalent to a word. Thus, heuristics need to be adjusted if they count
|
|
137
|
+
* the number of characters or words ("words" being defined as characters not
|
|
138
|
+
* separated by whitespace).
|
|
139
|
+
*
|
|
140
|
+
* Note: texts in Arabic or European languages should not trigger this check.
|
|
141
|
+
*/
|
|
142
|
+
function hasLogograms(str) {
|
|
143
|
+
return [...str].some(isLogogramChar);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function checkSuspiciousQuery(query) {
|
|
147
|
+
function accept() {
|
|
148
|
+
return {
|
|
149
|
+
accept: true,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function discard(reason) {
|
|
154
|
+
return {
|
|
155
|
+
accept: false,
|
|
156
|
+
reason,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// First, normalize white spaces
|
|
161
|
+
//
|
|
162
|
+
// Note: this code doesn't trim but preserves a leading or trailing
|
|
163
|
+
// whitespace. We could trim (and the expected differences would be minimal).
|
|
164
|
+
// Yet there is little benefit in trimming and it would lose information.
|
|
165
|
+
query = query.replace(/\s+/g, ' ');
|
|
166
|
+
|
|
167
|
+
// Remove the msg if the query is too long
|
|
168
|
+
if (query.length > 120) {
|
|
169
|
+
return discard('too long (120 character limit)');
|
|
170
|
+
}
|
|
171
|
+
if (query.length > 50 && hasLogograms(query)) {
|
|
172
|
+
return discard('too long (50 characters and logograms are present)');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const words = query.split(' ');
|
|
176
|
+
if (words.length > 9) {
|
|
177
|
+
if (words.filter((x) => x.length >= 4).length > 16) {
|
|
178
|
+
return discard('too many words');
|
|
179
|
+
}
|
|
180
|
+
if (hasLogograms(query)) {
|
|
181
|
+
return discard('too many words (smaller limit but logograms are present');
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (hasLongNumber(query)) {
|
|
186
|
+
return discard('long number detected');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Remove if it contains text that could be an email,
|
|
190
|
+
// even if the email is not well formed
|
|
191
|
+
if (checkForEmail(query)) {
|
|
192
|
+
return discard('looks like an email');
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (/[^:]+:[^@]+@/.test(query)) {
|
|
196
|
+
return discard('looks like an http password');
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
for (let i = 0; i < words.length; i += 1) {
|
|
200
|
+
if (words[i].length > 45) {
|
|
201
|
+
return discard('found long word');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Long words are common in some languages (e.g. German)
|
|
205
|
+
if (
|
|
206
|
+
words[i].length > 20 &&
|
|
207
|
+
!/^[a-zA-ZäöüéÄÖÜ][a-zäöüéß]+$/.test(words[i])
|
|
208
|
+
) {
|
|
209
|
+
return discard('found long word (smaller limit but uncommon shape)');
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return accept();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function tryParseUrl(url) {
|
|
217
|
+
try {
|
|
218
|
+
return new URL(url);
|
|
219
|
+
} catch (e) {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function checkForInternalIp(hostname) {
|
|
225
|
+
// TODO: this could be extended to detect more cases
|
|
226
|
+
return hostname === 'localhost' || hostname === '127.0.0.1';
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function looksLikeSafeUrlParameter(key, value) {
|
|
230
|
+
return value.length < 18 || /^[a-z-_]+$/.test(value);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* There should be no reason for these URLs to show up, but if they do
|
|
235
|
+
* we should never send them to the backend. Especially, "moz-extension"
|
|
236
|
+
* is problematic, as it includes an id that is unique per user and
|
|
237
|
+
* can be used to link messages.
|
|
238
|
+
*/
|
|
239
|
+
function urlLeaksExtensionId(url) {
|
|
240
|
+
return (
|
|
241
|
+
url.startsWith('moz-extension://') || url.startsWith('chrome-extension://')
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function normalizeUrlPart(urlPart) {
|
|
246
|
+
return urlPart.toLowerCase().replace(/_/g, '-');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// Note: matches URL parts (https://example.test/foo/bar/baz -> ['foo', 'bar', 'baz]).
|
|
250
|
+
// Before matching, URL match will be normalized (see "normalizeUrlPart").
|
|
251
|
+
const RISKY_URL_PATH_PARTS = new Set([
|
|
252
|
+
// login related:
|
|
253
|
+
'login',
|
|
254
|
+
'login.php',
|
|
255
|
+
'login-actions',
|
|
256
|
+
'logout',
|
|
257
|
+
'signin',
|
|
258
|
+
'recover',
|
|
259
|
+
'forgot',
|
|
260
|
+
'forgot-password',
|
|
261
|
+
'reset-credentials',
|
|
262
|
+
'authenticate',
|
|
263
|
+
'not-confirmed',
|
|
264
|
+
'reset',
|
|
265
|
+
'oauth',
|
|
266
|
+
'password',
|
|
267
|
+
|
|
268
|
+
// potential tokens
|
|
269
|
+
'token',
|
|
270
|
+
|
|
271
|
+
// could leak account:
|
|
272
|
+
'edit',
|
|
273
|
+
'checkout',
|
|
274
|
+
'account',
|
|
275
|
+
'share',
|
|
276
|
+
'sharing',
|
|
277
|
+
|
|
278
|
+
// Admin accounts
|
|
279
|
+
'admin',
|
|
280
|
+
'console',
|
|
281
|
+
|
|
282
|
+
// Wordpress
|
|
283
|
+
'wp-admin',
|
|
284
|
+
'wp-admin.php',
|
|
285
|
+
|
|
286
|
+
// Oracle WebLogic
|
|
287
|
+
'weblogic',
|
|
288
|
+
]);
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Sanity checks to protect against accidentially sending sensitive URLs.
|
|
292
|
+
*
|
|
293
|
+
* There are three possible outcomes:
|
|
294
|
+
* 1) "safe": URL can be accepted as is
|
|
295
|
+
* 2) "truncated": URL may have sensitive parts but can be truncated
|
|
296
|
+
* (use includ the hostname but remove the rest)
|
|
297
|
+
* 3) "dropped": URL is corrupted or unsafe
|
|
298
|
+
*
|
|
299
|
+
* Expections: this function should be seen as an additional layer of defence,
|
|
300
|
+
* but do not expect it to detect all situation. Instead, make sure to extract
|
|
301
|
+
* only URLs where the context is safe. Otherwise, you are expecting too
|
|
302
|
+
* much from this static classifier.
|
|
303
|
+
*
|
|
304
|
+
* When changing new rules here, it is OK to be conservative. Since
|
|
305
|
+
* classification error are expected, rather err on the side of
|
|
306
|
+
* dropping (or truncating) too much.
|
|
307
|
+
*
|
|
308
|
+
* Parameters:
|
|
309
|
+
* - strict [default=false]:
|
|
310
|
+
* Enables additional checks that will truncate more ULRs, but will lead to
|
|
311
|
+
* false-positives. Generally, the default of skipping these checks should
|
|
312
|
+
* be best for most use cases; but in situations where the URL is not critical,
|
|
313
|
+
* you can enable it for extra safety. Note that static filters will never be
|
|
314
|
+
* perfect, but quorum should be used as an additional step; quorum is effective
|
|
315
|
+
* in dropping unique identifiers like unique tokens that may slip through
|
|
316
|
+
* static rules.
|
|
317
|
+
* - tryPreservePath [default=false]:
|
|
318
|
+
* If the URL fails and gets truncated, it will normally keep only keep the
|
|
319
|
+
* the scheme and the domain. With this option, it does an extra pass to
|
|
320
|
+
* test if adding the URL path would be safe.
|
|
321
|
+
* For instance, given "https://example.test/foo/bar?arg=1&...#hash..."),
|
|
322
|
+
* it will check again with "https://example.test/foo/bar". If the latter URL
|
|
323
|
+
* passes all (strict) checks, it will be used for the truncated URL; instead of
|
|
324
|
+
* "https://example.test (PROTECTED)", the result will be
|
|
325
|
+
* "https://example.test/foo/bar (PROTECTED)".
|
|
326
|
+
*/
|
|
327
|
+
export function sanitizeUrl(url, options = {}) {
|
|
328
|
+
const { strict = false, tryPreservePath = false } = options;
|
|
329
|
+
let accept = () => ({ result: 'safe', safeUrl: url });
|
|
330
|
+
const drop = (reason) => ({ result: 'dropped', safeUrl: null, reason });
|
|
331
|
+
|
|
332
|
+
// first run some sanity check on the structure of the URL
|
|
333
|
+
const parsedUrl = tryParseUrl(url);
|
|
334
|
+
if (!parsedUrl) {
|
|
335
|
+
return drop('invalid URL');
|
|
336
|
+
}
|
|
337
|
+
if (parsedUrl.username) {
|
|
338
|
+
return drop('URL sets username');
|
|
339
|
+
}
|
|
340
|
+
if (parsedUrl.password) {
|
|
341
|
+
return drop('URL sets password');
|
|
342
|
+
}
|
|
343
|
+
if (parsedUrl.port && parsedUrl.port !== '80' && parsedUrl.port !== '443') {
|
|
344
|
+
return drop('URL has uncommon port');
|
|
345
|
+
}
|
|
346
|
+
if (parsedUrl.protocol !== 'http:' && parsedUrl.protocol !== 'https:') {
|
|
347
|
+
return drop('URL has uncommon protocol');
|
|
348
|
+
}
|
|
349
|
+
if (checkForInternalIp(parsedUrl.hostname)) {
|
|
350
|
+
return drop('URL is not public');
|
|
351
|
+
}
|
|
352
|
+
if (urlLeaksExtensionId(url)) {
|
|
353
|
+
return drop('URL leaks extension ID');
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
try {
|
|
357
|
+
// At this point, the most problematic URLs should be gone;
|
|
358
|
+
// now we can also decide to truncated by limiting it to the hostname.
|
|
359
|
+
//
|
|
360
|
+
// Often, that is a good compromise, as it still provides value
|
|
361
|
+
// but the risk that it contains sensitive information is limited.
|
|
362
|
+
// Note that even on https, the hostname will be shared in plaintext,
|
|
363
|
+
// so it is less likely that sites include secrets or personal
|
|
364
|
+
// identifiers in the hostname.
|
|
365
|
+
const truncate = (reason) => {
|
|
366
|
+
if (tryPreservePath && (parsedUrl.search || parsedUrl.hash)) {
|
|
367
|
+
// if the URL with only the URL path left is safe, than we can mask the
|
|
368
|
+
// URL less aggressively. Instead of leaving only the domain, we can
|
|
369
|
+
// keep also the URL path.
|
|
370
|
+
parsedUrl.search = '';
|
|
371
|
+
parsedUrl.hash = '';
|
|
372
|
+
const urlWithoutPath = parsedUrl.toString();
|
|
373
|
+
const { result, safeUrl } = sanitizeUrl(urlWithoutPath, {
|
|
374
|
+
...options,
|
|
375
|
+
tryPreservePath: false,
|
|
376
|
+
});
|
|
377
|
+
if (result === 'safe') {
|
|
378
|
+
return {
|
|
379
|
+
result: 'truncated',
|
|
380
|
+
safeUrl: `${safeUrl} (PROTECTED)`,
|
|
381
|
+
reason,
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const safeUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}/ (PROTECTED)`;
|
|
387
|
+
logger.debug('sanitizeUrl truncated URL:', url, '->', safeUrl);
|
|
388
|
+
return {
|
|
389
|
+
result: 'truncated',
|
|
390
|
+
safeUrl,
|
|
391
|
+
reason,
|
|
392
|
+
};
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// 50 is somewhat arbitrary, but appears to be an acceptable compromise.
|
|
396
|
+
// There are valid websites with domains longer than 50 characters
|
|
397
|
+
// (up to over 130 chars), but it is rare. Looking at relevant examples,
|
|
398
|
+
// 99 percent fell in to 50 character range. If you need to tweaking
|
|
399
|
+
// this value, it might be possible to increase it at bit.
|
|
400
|
+
if (parsedUrl.hostname.length > 50) {
|
|
401
|
+
return drop('hostname too long');
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (url.length > 800) {
|
|
405
|
+
return truncate('url too long');
|
|
406
|
+
}
|
|
407
|
+
if (parsedUrl.search.length > 150) {
|
|
408
|
+
return truncate('url search part too long');
|
|
409
|
+
}
|
|
410
|
+
if (parsedUrl.searchParams.size > 8) {
|
|
411
|
+
return truncate('too many url search parameters');
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const decodedUrl = decodeURIComponent(url);
|
|
415
|
+
if (checkForEmail(url) || checkForEmail(decodedUrl)) {
|
|
416
|
+
return truncate('potential email found');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const pathParts = parsedUrl.pathname.split('/');
|
|
420
|
+
if (pathParts.length > 8) {
|
|
421
|
+
return truncate('too many parts in the url path');
|
|
422
|
+
}
|
|
423
|
+
for (const part of pathParts) {
|
|
424
|
+
const normalizedPart = normalizeUrlPart(part);
|
|
425
|
+
if (RISKY_URL_PATH_PARTS.has(normalizedPart)) {
|
|
426
|
+
return truncate(`Found a problematic part in the URL path: ${part}`);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
if (strict && isHash(part, { threshold: 0.015 })) {
|
|
430
|
+
return truncate(
|
|
431
|
+
`Found URL path that could be an identifier: <<${part}>>`,
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const regexps = [
|
|
437
|
+
/[&?]redirect(?:-?url)?=/i,
|
|
438
|
+
/[&?#/=;](?:http|https)(?::[/]|%3A%2F)/,
|
|
439
|
+
/[/]order[/]./i,
|
|
440
|
+
/[/]auth[/]realms[/]/i,
|
|
441
|
+
/[/]protocol[/]openid-connect[/]/i,
|
|
442
|
+
/((maps|route[^r-]).*|@)\d{1,2}[^\d]-?\d{6}.+\d{1,2}[^\d]-?\d{6}/i,
|
|
443
|
+
];
|
|
444
|
+
for (const regexp of regexps) {
|
|
445
|
+
if (regexp.test(url)) {
|
|
446
|
+
return truncate(`matches ${regexp}`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
for (const [key, value] of parsedUrl.searchParams) {
|
|
451
|
+
if (value.length > 18 && !looksLikeSafeUrlParameter(key, value)) {
|
|
452
|
+
const { accept: ok, reason } = checkSuspiciousQuery(value);
|
|
453
|
+
if (!ok) {
|
|
454
|
+
return truncate(
|
|
455
|
+
`Found problematic URL parameter ${key}=${value}: ${reason}`,
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
if (strict && isHash(value, { threshold: 0.015 })) {
|
|
460
|
+
return truncate(
|
|
461
|
+
`Found URL parameter that could be an identifier ${key}=${value}`,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
if (parsedUrl.hash) {
|
|
467
|
+
parsedUrl.hash = '';
|
|
468
|
+
const safeUrl = `${parsedUrl} (PROTECTED)`;
|
|
469
|
+
logger.debug('sanitizeUrl truncated URL:', url, '->', safeUrl);
|
|
470
|
+
return {
|
|
471
|
+
result: 'truncated',
|
|
472
|
+
safeUrl,
|
|
473
|
+
reason: 'URL fragment found',
|
|
474
|
+
};
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
return accept();
|
|
478
|
+
} catch (e) {
|
|
479
|
+
logger.warn(`Unexpected error in sanitizeUrl. Skipping url=${url}`, e);
|
|
480
|
+
return drop('Unexpected error');
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Set of heuristics to prevent accidentally leaking sensitive data.
|
|
486
|
+
*
|
|
487
|
+
* It is a hard problem to classify sensititive from non-sensitive data.
|
|
488
|
+
* If you look at the risk of false positives (non-sensitive data being dropped)
|
|
489
|
+
* versus fast negatives (sensitive data being sent out), the risks are very clear.
|
|
490
|
+
*
|
|
491
|
+
* Leaking sensitive data is far more dangerous then dropping harmless messages
|
|
492
|
+
* for multiple reasons. Because of that, we should always thrive for being
|
|
493
|
+
* rather too conservative than being to open when it comes to the heuristics.
|
|
494
|
+
*
|
|
495
|
+
* In other words, because of the non-symmetric risks, it is unavoidable that
|
|
496
|
+
* you will find many harmless examples that will be rejected by the rules.
|
|
497
|
+
*/
|
|
498
|
+
export default class Sanitizer {
|
|
499
|
+
constructor(countryProvider) {
|
|
500
|
+
this.countryProvider = countryProvider;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
checkSuspiciousQuery(query) {
|
|
504
|
+
const result = checkSuspiciousQuery(query);
|
|
505
|
+
if (!result.accept) {
|
|
506
|
+
logger.debug(
|
|
507
|
+
'checkSuspiciousQuery rejected query:',
|
|
508
|
+
query,
|
|
509
|
+
', reason:',
|
|
510
|
+
result.reason,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
return result;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
getSafeCountryCode() {
|
|
517
|
+
return this.countryProvider.getSafeCountryCode();
|
|
518
|
+
}
|
|
519
|
+
}
|