@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,548 @@
|
|
|
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 { requireParam, lazyInitAsync } from './utils';
|
|
14
|
+
import { randomBetween } from './random';
|
|
15
|
+
import { BadJobError } from './errors';
|
|
16
|
+
import { anonymousHttpGet } from './http';
|
|
17
|
+
import parseHtml from './html-parser';
|
|
18
|
+
import { analyzePageStructure } from './page-structure';
|
|
19
|
+
import { sanitizeUrl } from './sanitizer';
|
|
20
|
+
import { removeSearchHash } from './url-cleaner';
|
|
21
|
+
|
|
22
|
+
const SECOND = 1000;
|
|
23
|
+
|
|
24
|
+
const ALLOW_HTML_AND_TEXT = [
|
|
25
|
+
'text/html',
|
|
26
|
+
'text/plain',
|
|
27
|
+
'application/xhtml+xml', // legacy format
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* This checks if the extracted title of a website is safe to share.
|
|
32
|
+
* To decide, it compares it with the title on the doublefetch HTML.
|
|
33
|
+
*
|
|
34
|
+
* Note that this function is not symmetric: swapping the order of
|
|
35
|
+
* the parameters can change the results. Why? It should be valid
|
|
36
|
+
* assumption that the title *after* doublefetch is public information,
|
|
37
|
+
* while the opposite cannot not be said for the title *before*
|
|
38
|
+
* doublefetch.
|
|
39
|
+
*/
|
|
40
|
+
export function titlesMatchAfterDoublefetch({
|
|
41
|
+
before: originalBefore,
|
|
42
|
+
after: originalAfter,
|
|
43
|
+
}) {
|
|
44
|
+
if (!originalBefore) {
|
|
45
|
+
logger.warn(
|
|
46
|
+
'Pages without "before-doublefetch" title should have been dropped already',
|
|
47
|
+
);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (!originalAfter) {
|
|
51
|
+
logger.warn(
|
|
52
|
+
'Pages without "after-doublefetch" title should have been dropped already',
|
|
53
|
+
);
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (originalBefore === originalAfter) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const normalize = (x) => x.toLowerCase().replace(/\s/g, '');
|
|
62
|
+
const before = normalize(originalBefore);
|
|
63
|
+
const after = normalize(originalAfter);
|
|
64
|
+
if (before === after) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (before.length >= 6 && after.includes(before)) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// sometimes encodings are broken after doublefetch (e.g. "Sánchez" -> "S�nchez")
|
|
73
|
+
if (after.includes('�')) {
|
|
74
|
+
const nonPrintableAscii = /[^\x20-\x7f]/g;
|
|
75
|
+
const brokenBefore = before.replace(nonPrintableAscii, '�');
|
|
76
|
+
const brokenAfter = after.replace(nonPrintableAscii, '�');
|
|
77
|
+
const start = brokenAfter.indexOf(brokenBefore);
|
|
78
|
+
if (start >= 0) {
|
|
79
|
+
let brokenChars = 0;
|
|
80
|
+
for (let i = 0; i < before.length; i += 1) {
|
|
81
|
+
const beforeChar = before.codePointAt(i);
|
|
82
|
+
const afterChar = after.codePointAt(start + i);
|
|
83
|
+
if (beforeChar === afterChar) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Tolerate it if a non-printable ASCII character has been replaced by '�'.
|
|
88
|
+
if ((beforeChar < 0x20 || beforeChar > 0x7f) && afterChar === 65533) {
|
|
89
|
+
brokenChars += 1;
|
|
90
|
+
} else {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Note: this is a bit abitrary, but the idea is to detect
|
|
96
|
+
// if significant parts of the string got replaced
|
|
97
|
+
if (brokenChars < 0.2 * after.length && !after.includes('���')) {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The activity should be number between 0 and 1. To avoid suprises like
|
|
108
|
+
* leaking details such as clock resolution or floating point rounding,
|
|
109
|
+
* apply a bit of noise, truncate after a few digits, and represent it
|
|
110
|
+
* as a string.
|
|
111
|
+
*/
|
|
112
|
+
export function sanitizeActivity(activity) {
|
|
113
|
+
if (!Number.isFinite(activity) || activity < 0) {
|
|
114
|
+
logger.warn(
|
|
115
|
+
'Corrupted "activity" value detected:',
|
|
116
|
+
activity,
|
|
117
|
+
'Replacing by 0',
|
|
118
|
+
);
|
|
119
|
+
return '0';
|
|
120
|
+
}
|
|
121
|
+
if (activity >= 1) {
|
|
122
|
+
return '1';
|
|
123
|
+
}
|
|
124
|
+
if (activity <= 0) {
|
|
125
|
+
return '0';
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const noisyActivity = activity * randomBetween(0.9, 1.1);
|
|
129
|
+
if (noisyActivity >= 1) {
|
|
130
|
+
return '1';
|
|
131
|
+
}
|
|
132
|
+
if (noisyActivity <= 0) {
|
|
133
|
+
return '0';
|
|
134
|
+
}
|
|
135
|
+
return noisyActivity.toFixed(4);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
class CachedPageFetcher {
|
|
139
|
+
constructor() {
|
|
140
|
+
this.urlToAsyncPageStructure = new Map();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async doublefetchUrl(url) {
|
|
144
|
+
let psProvider = this.urlToAsyncPageStructure.get(url);
|
|
145
|
+
if (!psProvider) {
|
|
146
|
+
psProvider = lazyInitAsync(() => this._doublefetchUrl(url));
|
|
147
|
+
this.urlToAsyncPageStructure.set(url, psProvider);
|
|
148
|
+
}
|
|
149
|
+
return psProvider();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async _doublefetchUrl(url) {
|
|
153
|
+
const shouldFollowRedirect = (targetUrl) => {
|
|
154
|
+
const from = new URL(url);
|
|
155
|
+
const to = new URL(targetUrl);
|
|
156
|
+
return from.origin === to.origin && from.pathname === to.pathname;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
let html;
|
|
160
|
+
try {
|
|
161
|
+
html = await anonymousHttpGet(url, {
|
|
162
|
+
redirect: 'follow',
|
|
163
|
+
shouldFollowRedirect,
|
|
164
|
+
treat429AsPermanentError: true,
|
|
165
|
+
downloadLimit: this.downloadLimit,
|
|
166
|
+
allowedContentTypes: ALLOW_HTML_AND_TEXT,
|
|
167
|
+
});
|
|
168
|
+
} catch (e) {
|
|
169
|
+
if (e.isPermanentError) {
|
|
170
|
+
return { rejectReason: `unable to fetch page (${e})` };
|
|
171
|
+
}
|
|
172
|
+
logger.debug(
|
|
173
|
+
'Failed to double-fetch',
|
|
174
|
+
url,
|
|
175
|
+
'(potentially it will work after a retry). Error message:',
|
|
176
|
+
e.message,
|
|
177
|
+
);
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
if (html.length === 0) {
|
|
181
|
+
return { rejectReason: 'got an empty document' };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const doc = await parseHtml(html);
|
|
185
|
+
const pageStructure = await analyzePageStructure(doc);
|
|
186
|
+
return { pageStructure };
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export default class DoublefetchPageHandler {
|
|
191
|
+
constructor({
|
|
192
|
+
jobScheduler,
|
|
193
|
+
sanitizer,
|
|
194
|
+
newPageApprover,
|
|
195
|
+
|
|
196
|
+
// by default, use a separate fetcher cache per job
|
|
197
|
+
pageFetcherProvider = () => new CachedPageFetcher(),
|
|
198
|
+
}) {
|
|
199
|
+
this.sanitizer = requireParam(sanitizer);
|
|
200
|
+
this.newPageApprover = requireParam(newPageApprover);
|
|
201
|
+
this.pageFetcherProvider = requireParam(pageFetcherProvider);
|
|
202
|
+
this.downloadLimit = 2 * 1024 * 1024; // 2 MB
|
|
203
|
+
|
|
204
|
+
const config = {
|
|
205
|
+
priority: -1000,
|
|
206
|
+
cooldownInMs: 3 * SECOND,
|
|
207
|
+
maxJobsTotal: 200,
|
|
208
|
+
};
|
|
209
|
+
jobScheduler.registerHandler(
|
|
210
|
+
'doublefetch-page',
|
|
211
|
+
async (job) => {
|
|
212
|
+
const { page } = job.args;
|
|
213
|
+
const { ok, safePage, details } = await this.runJob(page);
|
|
214
|
+
if (!ok) {
|
|
215
|
+
logger.debug('Page has been rejected during doublefetch:', {
|
|
216
|
+
page,
|
|
217
|
+
details,
|
|
218
|
+
});
|
|
219
|
+
return [];
|
|
220
|
+
}
|
|
221
|
+
return [{ type: 'page-quorum-check', args: { safePage } }];
|
|
222
|
+
},
|
|
223
|
+
config,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async runJob(page) {
|
|
228
|
+
const { url, preDoublefetch } = page;
|
|
229
|
+
if (!url) {
|
|
230
|
+
throw new BadJobError('url missing');
|
|
231
|
+
}
|
|
232
|
+
if (!preDoublefetch) {
|
|
233
|
+
throw new BadJobError('preDoublefetch missing');
|
|
234
|
+
}
|
|
235
|
+
const canonicalUrl = preDoublefetch?.meta?.canonicalUrl;
|
|
236
|
+
const canonicalUrlDiffers = canonicalUrl && canonicalUrl !== url;
|
|
237
|
+
|
|
238
|
+
const pageFetcher = this.pageFetcherProvider();
|
|
239
|
+
const log = logger.debug.bind(logger.debug, `[doublefetch=${url}]`);
|
|
240
|
+
const logWarn = logger.warn.bind(logger.warn, `[doublefetch=${url}]`);
|
|
241
|
+
|
|
242
|
+
try {
|
|
243
|
+
const checkUrl = async (urlToTest, { isCanonicalUrl }) => {
|
|
244
|
+
if (await this.newPageApprover.mightBeMarkedAsPrivate(urlToTest)) {
|
|
245
|
+
return { ok: false, details: 'marked as private in bloom filter' };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const { pageStructure, rejectReason } =
|
|
249
|
+
await pageFetcher.doublefetchUrl(urlToTest);
|
|
250
|
+
if (!pageStructure) {
|
|
251
|
+
return { ok: false, details: rejectReason };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const canonicalUrl2 = pageStructure?.meta?.canonicalUrl;
|
|
255
|
+
if (canonicalUrl2) {
|
|
256
|
+
if (!isCanonicalUrl && urlToTest === canonicalUrl2) {
|
|
257
|
+
log(
|
|
258
|
+
'Found a matching canonical URL in doublefetch result for URL:',
|
|
259
|
+
urlToTest,
|
|
260
|
+
);
|
|
261
|
+
isCanonicalUrl = true;
|
|
262
|
+
} else if (urlToTest === url && canonicalUrl2 !== canonicalUrl) {
|
|
263
|
+
log(
|
|
264
|
+
'Updated canonical URL from',
|
|
265
|
+
canonicalUrl,
|
|
266
|
+
'to',
|
|
267
|
+
canonicalUrl2,
|
|
268
|
+
);
|
|
269
|
+
try {
|
|
270
|
+
const result = await checkUrl(canonicalUrl2, {
|
|
271
|
+
isCanonicalUrl: true,
|
|
272
|
+
});
|
|
273
|
+
if (result.ok) {
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
} catch (e) {
|
|
277
|
+
if (e.isPermanentError) {
|
|
278
|
+
logWarn(
|
|
279
|
+
'Failed to process (corrected) canonical URL',
|
|
280
|
+
canonicalUrl2,
|
|
281
|
+
'(falling back to original URL now)',
|
|
282
|
+
e,
|
|
283
|
+
);
|
|
284
|
+
await this._tryMarkAsPrivate(canonicalUrl2);
|
|
285
|
+
} else {
|
|
286
|
+
throw e;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const { accept, details } = this._structureMatches({
|
|
293
|
+
before: preDoublefetch,
|
|
294
|
+
after: pageStructure,
|
|
295
|
+
page,
|
|
296
|
+
log,
|
|
297
|
+
});
|
|
298
|
+
if (!accept) {
|
|
299
|
+
return { ok: false, details };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const safePage = this._sanitizePage({
|
|
303
|
+
page,
|
|
304
|
+
doublefetchUrl: urlToTest,
|
|
305
|
+
isCanonicalUrl,
|
|
306
|
+
log,
|
|
307
|
+
logWarn,
|
|
308
|
+
});
|
|
309
|
+
if (!safePage) {
|
|
310
|
+
return { ok: false, details: 'rejected by page sanitizer' };
|
|
311
|
+
}
|
|
312
|
+
return { ok: true, safePage };
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
let safePage;
|
|
316
|
+
if (canonicalUrlDiffers) {
|
|
317
|
+
try {
|
|
318
|
+
const result = await checkUrl(canonicalUrl, { isCanonicalUrl: true });
|
|
319
|
+
if (result.ok) {
|
|
320
|
+
safePage = result.safePage;
|
|
321
|
+
}
|
|
322
|
+
} catch (e) {
|
|
323
|
+
if (e.isPermanentError) {
|
|
324
|
+
logWarn(
|
|
325
|
+
'Failed to process canonical URL',
|
|
326
|
+
canonicalUrl,
|
|
327
|
+
'(falling back to original URL now)',
|
|
328
|
+
e,
|
|
329
|
+
);
|
|
330
|
+
await this._tryMarkAsPrivate(canonicalUrl);
|
|
331
|
+
} else {
|
|
332
|
+
throw e;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (!safePage) {
|
|
338
|
+
const isCanonicalUrl = url === canonicalUrl;
|
|
339
|
+
const result = await checkUrl(url, { isCanonicalUrl });
|
|
340
|
+
if (result.ok) {
|
|
341
|
+
safePage = result.safePage;
|
|
342
|
+
} else {
|
|
343
|
+
await this._tryMarkAsPrivate(url);
|
|
344
|
+
return { ok: false, details: result.details };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return { ok: true, safePage };
|
|
349
|
+
} catch (e) {
|
|
350
|
+
log('Exception', e);
|
|
351
|
+
throw e;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* This comparables the structure the page before and after doublefetch:
|
|
357
|
+
* - before: the real page that the user saw (including dynamic content)
|
|
358
|
+
* - after: parsing the HTML from doublefetch
|
|
359
|
+
*
|
|
360
|
+
* For dynamic pages, it is expected that the structure will not be
|
|
361
|
+
* identical. In some sense, we are comparing apples and oranges; still,
|
|
362
|
+
* it is a useful heuristic to filter out pages that leak private
|
|
363
|
+
* information (mostly, pages that require a login).
|
|
364
|
+
*
|
|
365
|
+
* To get an the intuition, suppose an email websites uses the page title
|
|
366
|
+
* to display the email subject. The doublefetch HTTP request should not
|
|
367
|
+
* include credentials or session tokens (required to access the email).
|
|
368
|
+
* Thus, the title should no longer match, and the message will be dropped.
|
|
369
|
+
*/
|
|
370
|
+
_structureMatches({ before, after, page, log }) {
|
|
371
|
+
const accept = () => ({ accept: true });
|
|
372
|
+
const discard = (reason) => ({ accept: false, details: reason });
|
|
373
|
+
|
|
374
|
+
if (before.noindex !== false) {
|
|
375
|
+
logger.warn(
|
|
376
|
+
'noindex pages should have been filtered out already:',
|
|
377
|
+
before,
|
|
378
|
+
);
|
|
379
|
+
return discard('noindex must be false (before)');
|
|
380
|
+
}
|
|
381
|
+
if (after.noindex !== false) {
|
|
382
|
+
return discard('noindex must be false (after)');
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (!page.title) {
|
|
386
|
+
return discard('page title missing');
|
|
387
|
+
}
|
|
388
|
+
if (!before.title) {
|
|
389
|
+
return discard('title missing (before)');
|
|
390
|
+
}
|
|
391
|
+
if (!after.title) {
|
|
392
|
+
return discard('title missing (after)');
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (page.url !== before.url) {
|
|
396
|
+
// Before giving up, try a relaxed match where the URL hash of the original URL
|
|
397
|
+
// is ignored. Note that for the page itself, the hash should have been already
|
|
398
|
+
// removed in a previous step.
|
|
399
|
+
// Note: this is relevant to handle URLs like "https://pytorch.org/docs/stable/torch.html#tensors"
|
|
400
|
+
const safeToIgnoreHash =
|
|
401
|
+
page.pageLoadMethod === 'full-page-load' ||
|
|
402
|
+
(page.title === before.title && before.title === after.title);
|
|
403
|
+
if (safeToIgnoreHash && page.url === removeSearchHash(before.url)) {
|
|
404
|
+
log(
|
|
405
|
+
'The page url',
|
|
406
|
+
page.url,
|
|
407
|
+
'does not exactly match the original URL',
|
|
408
|
+
before.url,
|
|
409
|
+
'However, it is identical when ignoring the hash in the original URL.',
|
|
410
|
+
'Based on the other information, it looks safe to continue.',
|
|
411
|
+
);
|
|
412
|
+
} else {
|
|
413
|
+
return discard(
|
|
414
|
+
`inconsistency detected: page url does not match url (before): page.url=<<${page.url}>> !== before.url=<<${before.url}>>`,
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
if (
|
|
420
|
+
!titlesMatchAfterDoublefetch({
|
|
421
|
+
before: page.title,
|
|
422
|
+
after: after.title,
|
|
423
|
+
})
|
|
424
|
+
) {
|
|
425
|
+
return discard(
|
|
426
|
+
`titles do not match the page title: <<${page.title}>> ==> <<${after.title}>>`,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
if (
|
|
430
|
+
page.pageLoadMethod !== 'history-navigation' &&
|
|
431
|
+
!titlesMatchAfterDoublefetch({
|
|
432
|
+
before: before.title,
|
|
433
|
+
after: after.title,
|
|
434
|
+
})
|
|
435
|
+
) {
|
|
436
|
+
return discard(
|
|
437
|
+
`titles do not match the meta title: <<${before.title}>> ==> <<${after.title}>>`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return accept();
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
_sanitizePage({ page, doublefetchUrl, isCanonicalUrl, log, logWarn }) {
|
|
445
|
+
const safePage = {
|
|
446
|
+
url: doublefetchUrl,
|
|
447
|
+
title: page.title,
|
|
448
|
+
requestedIndex: page.preDoublefetch.requestedIndex,
|
|
449
|
+
lang: {
|
|
450
|
+
html: page.preDoublefetch?.meta?.language || null,
|
|
451
|
+
detect: page.lang || null,
|
|
452
|
+
},
|
|
453
|
+
aggregator: {
|
|
454
|
+
activity: sanitizeActivity(page.aggregator.activity),
|
|
455
|
+
},
|
|
456
|
+
};
|
|
457
|
+
if (safePage.url !== page.url) {
|
|
458
|
+
log('url changed:', page.url, '->', safePage.url);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (page.search) {
|
|
462
|
+
const { query, category, depth } = page.search;
|
|
463
|
+
safePage.search = {
|
|
464
|
+
query,
|
|
465
|
+
category,
|
|
466
|
+
depth,
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
const { accept, reason } = this.sanitizer.checkSuspiciousQuery(query);
|
|
470
|
+
if (!accept) {
|
|
471
|
+
log('Omitting search query from page:', { query, reason });
|
|
472
|
+
safePage.search.query = null;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
// Run static checks on the URL. Depending on the context, it is more likely
|
|
477
|
+
// that the URL is safe if it has a canonical URL, has been indexed by a search
|
|
478
|
+
// engine, or contains meta information that signal that the page wants to be
|
|
479
|
+
// indexed. Depending on how confident these indicators are, we can either skip
|
|
480
|
+
// the checks or fallback to a more conservative version - even if the latter
|
|
481
|
+
// will lead to safe URLs being dropped.
|
|
482
|
+
const isIndexed = safePage.search && safePage.search.depth === 1;
|
|
483
|
+
if (isCanonicalUrl && isIndexed && safePage.requestedIndex) {
|
|
484
|
+
log(
|
|
485
|
+
'There are strong indicators that the page is public. Skipping static checks for the URL:',
|
|
486
|
+
safePage.url,
|
|
487
|
+
);
|
|
488
|
+
} else if (isCanonicalUrl || isIndexed || safePage.requestedIndex) {
|
|
489
|
+
const { result, reason } = sanitizeUrl(safePage.url);
|
|
490
|
+
if (result !== 'safe') {
|
|
491
|
+
logWarn(
|
|
492
|
+
'The page appears to be public, but it will be dropped, since it failed standard static checks for the URL',
|
|
493
|
+
safePage.url,
|
|
494
|
+
':',
|
|
495
|
+
reason,
|
|
496
|
+
);
|
|
497
|
+
return null;
|
|
498
|
+
}
|
|
499
|
+
} else {
|
|
500
|
+
const { result, reason } = sanitizeUrl(safePage.url, { strict: true });
|
|
501
|
+
if (result !== 'safe') {
|
|
502
|
+
log(
|
|
503
|
+
'Failed strict static checks for the URL',
|
|
504
|
+
safePage.url,
|
|
505
|
+
':',
|
|
506
|
+
reason,
|
|
507
|
+
);
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// At this point, we should no longer trust the canonical URL in "meta".
|
|
513
|
+
// The canonical URL that we got after doublefetch is more reliable
|
|
514
|
+
// since it has logic to deal with single-page applications where the
|
|
515
|
+
// meta attributes become outdated.
|
|
516
|
+
safePage.canonicalUrl = isCanonicalUrl ? doublefetchUrl : null;
|
|
517
|
+
|
|
518
|
+
const cache = new Map();
|
|
519
|
+
const maskUrl = (url) => {
|
|
520
|
+
let entry = cache.get(url);
|
|
521
|
+
if (!entry) {
|
|
522
|
+
entry = sanitizeUrl(url, { strict: true });
|
|
523
|
+
cache.set(url, entry);
|
|
524
|
+
}
|
|
525
|
+
return entry.safeUrl || null;
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
if (page.redirects && page.redirects.length > 0) {
|
|
529
|
+
safePage.redirects = page.redirects.map(({ from, to, statusCode }) => ({
|
|
530
|
+
from: maskUrl(from),
|
|
531
|
+
to: to === '...' ? to : maskUrl(to),
|
|
532
|
+
statusCode,
|
|
533
|
+
}));
|
|
534
|
+
}
|
|
535
|
+
if (page.ref) {
|
|
536
|
+
safePage.ref = maskUrl(page.ref);
|
|
537
|
+
}
|
|
538
|
+
return safePage;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
async _tryMarkAsPrivate(url) {
|
|
542
|
+
try {
|
|
543
|
+
await this.newPageApprover.markAsPrivate(url);
|
|
544
|
+
} catch (e) {
|
|
545
|
+
logger.error('Failed to mark url', url, 'as private', e);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|