-
Notifications
You must be signed in to change notification settings - Fork 828
Expand file tree
/
Copy pathreporter.js
More file actions
1101 lines (969 loc) · 43 KB
/
Copy pathreporter.js
File metadata and controls
1101 lines (969 loc) · 43 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// lib/reporter.js -- Crash/hang reporter for camofox-browser
// Files GitHub issues with paranoid anonymization. No env reads here.
// Config passed via createReporter(config) from lib/config.js.
import crypto from 'crypto';
import { monitorEventLoopDelay } from 'perf_hooks';
import { collectResourceSnapshot, classifyProxyError, browserProcessTreeRssMb, browserProcessNameRssMb } from './resources.js';
// ============================================================================
// Anonymization
// ============================================================================
const SAFE_HOSTS = new Set([
'github.com', 'api.github.com', 'npmjs.com', 'registry.npmjs.org',
'nodejs.org',
]);
const SECRET_PREFIXES = [
'ghp_', 'gho_', 'ghu_', 'ghs_', 'ghr_',
'sk-', 'sk_live_', 'sk_test_', 'pk_live_', 'pk_test_',
'AKIA', 'ASIA',
'xox', 'Bearer ', 'Basic ',
'eyJ',
];
/**
* Paranoid anonymization of arbitrary text (stack traces, error messages, etc.)
* Better to over-strip than leak. Order matters -- more specific patterns first.
*/
export function anonymize(text) {
if (!text || typeof text !== 'string') return text || '';
let s = text;
// 1. Strip known secret-prefixed tokens
for (const prefix of SECRET_PREFIXES) {
const escaped = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
s = s.replace(new RegExp(escaped + '[A-Za-z0-9_\\-\\.=+/]{8,}', 'g'), '<token>');
}
// 2. Strip Bearer/Basic auth headers
s = s.replace(/(?:Bearer|Basic)\s+[A-Za-z0-9_\-\.=+/]{8,}/gi, '<token>');
// 3. Strip proxy URLs with credentials (before email -- email regex eats user:pass@host)
s = s.replace(/(?:https?|socks[45]?):\/\/[^:]+:[^@]+@[^\s]+/gi, '<proxy-url>');
// 4. Strip email addresses
s = s.replace(/[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}/g, '<email>');
// 5. Strip full URLs (preserve scheme for context)
s = s.replace(/(https?|wss?|ftp):\/\/[^\s'",)}\]>]+/g, (match, scheme) => {
try {
const u = new URL(match);
if (SAFE_HOSTS.has(u.hostname)) return match;
} catch { /* not a valid URL, strip it */ }
return `<${scheme}-url>`;
});
// 6. Strip absolute file paths (Unix + Windows), preserve last filename
s = s.replace(
/(?:\/(?:Users|home|root|tmp|var|opt|data|app|srv|etc|mnt|run|snap|proc)\/[^\s:;,'")\]}]+|[A-Z]:\\(?:Users|Documents and Settings)\\[^\s:;,'")\]}]+)/g,
(match) => {
const parts = match.replace(/\\/g, '/').split('/');
const filename = parts[parts.length - 1] || parts[parts.length - 2] || 'unknown';
return `<path>/${filename}`;
}
);
// 7. Strip IPv4 addresses
s = s.replace(/\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g, '<ip>');
// 8. Strip IPv6 addresses
s = s.replace(/\b(?:[0-9a-fA-F]{1,4}:){2,7}[0-9a-fA-F]{1,4}\b/g, '<ipv6>');
s = s.replace(/::(?:ffff:)?(?:\d{1,3}\.){3}\d{1,3}/g, '<ipv6>');
// 9. Strip hostnames in connection errors
s = s.replace(
/(?:ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENOTFOUND|EHOSTUNREACH)\s+([a-zA-Z0-9.\-]+):(\d+)/g,
(match, host, port) => {
if (SAFE_HOSTS.has(host)) return match;
return match.replace(host, '<host>');
}
);
// 10. Strip Fly machine IDs (14-char hex), Docker container IDs (12+ hex)
s = s.replace(/\b[0-9a-f]{12,64}\b/g, '<id>');
// 11. Strip jo-* app names
s = s.replace(/\bjo-(?:machine|browser|whatsapp|discord|bot)[a-z0-9\-]*/gi, '<app>');
// 12. Strip environment variable assignments
s = s.replace(/\b[A-Z][A-Z0-9_]{3,}=[^\s]{4,}/g, '<env-var>');
// 13. Strip long alphanumeric strings (40+ chars)
s = s.replace(/[A-Za-z0-9_\-]{40,}/g, '<redacted>');
// 14. Strip base64 blobs (20+ chars with mixed case)
s = s.replace(/[A-Za-z0-9+/]{20,}={0,3}/g, (match) => {
if (/[a-z]/.test(match) && /[A-Z]/.test(match)) return '<redacted>';
return match;
});
return s;
}
/**
* Generate a stable signature for dedup. Uses error name + first meaningful
* stack frame (file:line, not column -- columns shift with minor edits).
*/
export function stackSignature(type, error) {
const name = error?.name || error?.code || 'unknown';
const message = error?.message || String(error || '');
const stack = error?.stack || '';
const frames = stack.split('\n').slice(1);
let keyFrame = '';
for (const frame of frames) {
const trimmed = frame.trim();
if (trimmed.startsWith('at ') && !trimmed.includes('node_modules') && !trimmed.includes('node:internal')) {
const fileMatch = trimmed.match(/\(([^)]+)\)/) || trimmed.match(/at\s+(.+)$/);
if (fileMatch) {
const loc = fileMatch[1];
const parts = loc.replace(/\\/g, '/').split('/');
const last = parts[parts.length - 1];
const [file, line] = last.split(':');
keyFrame = `${file}:${line || '?'}`;
break;
}
}
}
const raw = `${type}|${name}|${keyFrame || anonymize(message).slice(0, 80)}`;
return fnv1a(raw);
}
/** FNV-1a hash -> 8-char hex. Stable bucketing, not crypto. */
function fnv1a(str) {
let hash = 0x811c9dc5;
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i);
hash = (hash * 0x01000193) >>> 0;
}
return hash.toString(16).padStart(8, '0');
}
// ============================================================================
// URL anonymization (per-report salted HMAC for private domains)
// ============================================================================
// Public domains safe to show verbatim in reports.
// These are public knowledge -- showing "amazon.com" in a crash report is not PII.
// Matched by suffix. NEVER add multi-tenant hosting (herokuapp.com, vercel.app, etc.)
const PUBLIC_DOMAINS = [
// CDN & edge
'cloudflare.com', 'cloudflare-dns.com', 'cloudflareinsights.com',
'fastly.net', 'fastlylb.net',
'akamaized.net', 'akamai.net', 'cloudfront.net',
'cdn.jsdelivr.net', 'unpkg.com', 'cdnjs.com',
// Google
'google.com', 'googleapis.com', 'gstatic.com',
'googleusercontent.com', 'google-analytics.com', 'googletagmanager.com',
'googlesyndication.com', 'doubleclick.net', 'youtube.com', 'ytimg.com',
'recaptcha.net',
// Microsoft
'microsoft.com', 'msecnd.net', 'azureedge.net', 'bing.com', 'live.com',
'outlook.com', 'office.com', 'linkedin.com',
// Meta
'facebook.com', 'facebook.net', 'fbcdn.net', 'instagram.com', 'threads.net',
'whatsapp.com',
// X/Twitter
'twitter.com', 'x.com', 'twimg.com',
// GitHub
'github.com', 'githubusercontent.com', 'githubassets.com',
// Major sites (common anti-bot / frustration sources)
'amazon.com', 'amazon.co.uk', 'amazon.de', 'amazon.co.jp',
'reddit.com', 'redd.it',
'apple.com', 'icloud.com',
'netflix.com', 'spotify.com', 'discord.com', 'discord.gg',
'tiktok.com', 'pinterest.com', 'tumblr.com',
'stackoverflow.com', 'stackexchange.com',
'medium.com', 'substack.com',
'nytimes.com', 'washingtonpost.com', 'bbc.co.uk', 'bbc.com', 'cnn.com',
'ebay.com', 'etsy.com', 'walmart.com', 'target.com', 'shopify.com',
'stripe.com', 'paypal.com',
'twitch.tv', 'vimeo.com', 'dailymotion.com',
'yahoo.com', 'duckduckgo.com', 'baidu.com',
'zoom.us', 'slack.com', 'notion.so', 'figma.com',
'dropbox.com', 'box.com',
'archive.org', 'web.archive.org',
// Prediction markets & crypto (heavy anti-bot, commonly scraped)
'polymarket.com', 'kalshi.com', 'metaculus.com', 'manifold.markets',
'predictit.org', 'augur.net',
'coinbase.com', 'binance.com', 'kraken.com', 'gemini.com',
'coingecko.com', 'coinmarketcap.com',
'opensea.io', 'blur.io', 'rarible.com',
'etherscan.io', 'solscan.io', 'blockchair.com',
'uniswap.org', 'dexscreener.com', 'dextools.io',
// Data / scraping targets (aggressive anti-bot)
'zillow.com', 'realtor.com', 'redfin.com', 'trulia.com',
'indeed.com', 'glassdoor.com', 'lever.co', 'greenhouse.io',
'airbnb.com', 'booking.com', 'expedia.com', 'tripadvisor.com',
'yelp.com', 'trustpilot.com',
'craigslist.org', 'nextdoor.com',
'ticketmaster.com', 'stubhub.com', 'seatgeek.com',
// Finance / trading
'tradingview.com', 'investing.com', 'seekingalpha.com',
'finance.yahoo.com', 'bloomberg.com', 'reuters.com', 'wsj.com',
'robinhood.com', 'schwab.com', 'fidelity.com', 'etrade.com',
// AI / developer tools
'openai.com', 'anthropic.com', 'huggingface.co',
'vercel.com', 'netlify.com', 'render.com', 'fly.io',
'npmjs.com', 'pypi.org', 'crates.io', 'pkg.go.dev',
// Social / forums
'quora.com', 'hackernews.com', 'news.ycombinator.com',
'producthunt.com', 'indiehackers.com',
// Reference
'wikipedia.org', 'wikimedia.org', 'mozilla.org', 'mozilla.net',
// Anti-bot / CAPTCHA
'hcaptcha.com',
// Fonts
'typekit.net', 'fontawesome.com',
].sort((a, b) => b.length - a.length); // longest-suffix-first
// Stable key for domain hashing -- NOT a secret, just ensures consistent hashes
// across reports so we can correlate "site-a1b2c3d4 caused 12 hangs this week".
const DOMAIN_HASH_KEY = 'camofox-domain-hash-v1';
/**
* Create a URL anonymizer.
* Public domains shown verbatim. Private domains get a stable hash
* (same domain -> same hash across all reports, enabling correlation).
*/
export function createUrlAnonymizer() {
function isPublicDomain(hostname) {
for (const d of PUBLIC_DOMAINS) {
if (hostname === d || hostname.endsWith('.' + d)) return true;
}
return false;
}
function hashHost(hostname) {
return 'site-' + crypto.createHmac('sha256', DOMAIN_HASH_KEY).update(hostname).digest('hex').slice(0, 8);
}
/**
* Anonymize a URL. Preserves: scheme, public infra hostnames, path depth,
* query param count, fragment presence. Strips everything else.
*
* Examples:
* https://challenges.cloudflare.com/[path]/[path]/[path]
* https://site-a1b2c3d4:8443/[path]/[path] ?[3] #[frag]
*/
function anonymizeUrl(rawUrl) {
if (!rawUrl || typeof rawUrl !== 'string') return '[empty]';
if (rawUrl.startsWith('data:')) return '[data-uri]';
if (rawUrl.startsWith('blob:')) return '[blob-uri]';
if (rawUrl.startsWith('about:')) return rawUrl;
if (rawUrl.startsWith('javascript:')) return '[javascript-uri]';
let url;
try { url = new URL(rawUrl); } catch { return '[invalid-url]'; }
const parts = [url.protocol + '//'];
const h = url.hostname.toLowerCase();
if (/^(\d{1,3}\.){3}\d{1,3}$/.test(h) || h.includes(':')) {
parts.push(hashHost(h));
} else if (isPublicDomain(h)) {
parts.push(h);
} else {
parts.push(hashHost(h));
}
if (url.port) parts.push(':' + url.port);
const segs = url.pathname.split('/').filter(Boolean);
parts.push(segs.length > 0 ? '/' + segs.map(() => '\u2022').join('/') : '/');
const paramCount = [...url.searchParams].length;
if (paramCount > 0) parts.push(` ?[${paramCount}]`);
if (url.hash && url.hash.length > 1) parts.push(' #[frag]');
return parts.join('');
}
function anonymizeChain(urls) {
if (!Array.isArray(urls) || urls.length === 0) return '[empty-chain]';
return urls.map(u => anonymizeUrl(u)).join(' \u2192 ');
}
return { anonymizeUrl, anonymizeChain };
}
// ============================================================================
// Per-tab health tracker (count-only, no content)
// ============================================================================
// Known bot-detection providers, matched by response header fingerprints.
// Order: most specific first.
const BOT_DETECTION_SIGNATURES = [
{ header: 'cf-mitigated', value: 'challenge', provider: 'cloudflare' },
{ header: 'x-datadome', provider: 'datadome' },
{ header: 'x-px', provider: 'perimeterx' },
{ header: 'x-distil-cs', provider: 'distil' },
{ header: 'x-sucuri-id', provider: 'sucuri' },
{ header: 'server', value: 'akamaighost', provider: 'akamai' },
// cf-ray is on ALL Cloudflare responses (even 200 OK). Must be last so it
// doesn't short-circuit other providers on multi-CDN sites.
{ header: 'cf-ray', provider: 'cloudflare' },
];
/**
* Detect bot-detection provider from Playwright response headers.
* Returns { detected: bool, provider: string|null, httpStatus: number|null }
*/
export function detectBotProtection(response) {
if (!response) return { detected: false, provider: null, httpStatus: null };
const status = response.status();
let headers;
try { headers = response.headers(); } catch { return { detected: false, provider: null, httpStatus: status }; }
for (const sig of BOT_DETECTION_SIGNATURES) {
const val = headers[sig.header];
if (val !== undefined) {
if (sig.value && !val.toLowerCase().includes(sig.value)) continue;
const challenged = status === 403 || status === 429 || status === 503;
return { detected: challenged, provider: sig.provider, httpStatus: status };
}
}
return { detected: false, provider: null, httpStatus: status };
}
/**
* Create a health tracker for a tab. Attaches to Playwright page events.
* Tracks: crashes, page errors, request failures, redirect status codes,
* HTTP status histogram (4xx+), and anti-bot challenge detection.
* All count-based -- no URLs or content stored.
*/
export function createTabHealthTracker(page) {
const health = {
crashes: 0,
pageErrors: 0,
requestFailures: 0,
inflightRequests: 0,
maxRedirectDepth: 0,
redirectStatusCodes: [], // status codes in redirect chain, e.g. [301, 302, 403]
statusCounts: {}, // { 403: 5, 429: 2, ... }
botDetection: null, // { detected, provider, httpStatus } from last nav response
lastNavResponseSize: 0,
_redirectDepth: 0,
};
// Renderer crash (OOM, segfault)
page.on('crash', () => { health.crashes++; });
// Uncaught JS exceptions on the page
page.on('pageerror', () => { health.pageErrors++; });
// Failed requests (blocked, DNS failure, etc.) + decrement in-flight counter
page.on('requestfailed', () => {
health.requestFailures++;
health.inflightRequests = Math.max(0, health.inflightRequests - 1);
});
// Track in-flight requests for hang diagnostics
page.on('request', () => { health.inflightRequests++; });
page.on('requestfinished', () => { health.inflightRequests = Math.max(0, health.inflightRequests - 1); });
// HTTP status tracking (non-2xx only)
page.on('response', (resp) => {
const s = resp.status();
if (s >= 400) health.statusCounts[s] = (health.statusCounts[s] || 0) + 1;
});
// Auto-dismiss dialogs to prevent page hangs (not tracked as a metric -- noise)
page.on('dialog', async (dialog) => {
try { await dialog.dismiss(); } catch { /* page might be closed */ }
});
// Redirect depth + status code chain per navigation
page.on('request', (req) => {
if (req.isNavigationRequest()) {
if (req.redirectedFrom()) {
health._redirectDepth++;
if (health._redirectDepth > health.maxRedirectDepth) {
health.maxRedirectDepth = health._redirectDepth;
}
} else {
health._redirectDepth = 0;
health.redirectStatusCodes = [];
health.inflightRequests = 0; // reset on new navigation to prevent drift
}
}
});
// Capture redirect status codes and detect bot protection on nav responses
page.on('response', (resp) => {
try {
const req = resp.request();
if (req.isNavigationRequest()) {
health.redirectStatusCodes.push(resp.status());
health.botDetection = detectBotProtection(resp);
// Approximate response body size from content-length (no body read)
const cl = resp.headers()['content-length'];
if (cl) health.lastNavResponseSize = parseInt(cl, 10) || 0;
}
} catch { /* page closed */ }
});
/** Snapshot current health counters for inclusion in reports. */
function snapshot() {
const { _redirectDepth, ...clean } = health;
return { ...clean };
}
/**
* Get document.readyState from the page. Returns null if page is unresponsive.
* Use a tight timeout -- if the renderer is crashed, evaluate will hang.
*/
async function getReadyState() {
try {
return await Promise.race([
page.evaluate(() => document.readyState),
new Promise(resolve => setTimeout(() => resolve('unresponsive'), 1000)),
]);
} catch {
return 'unresponsive';
}
}
return { health, snapshot, getReadyState };
}
// collectResourceSnapshot and classifyProxyError live in lib/resources.js
// (isolated from network code for clean separation of concerns).
// Re-exported here for backward compatibility.
export { collectResourceSnapshot, classifyProxyError, browserProcessTreeRssMb, browserProcessNameRssMb };
// ============================================================================
// Rate limiter (sliding window, 1 hour)
// ============================================================================
class RateLimiter {
constructor(maxPerHour) {
this.maxPerHour = maxPerHour;
this.timestamps = [];
}
tryAcquire() {
const now = Date.now();
this.timestamps = this.timestamps.filter(t => t > now - 3600_000);
if (this.timestamps.length >= this.maxPerHour) return false;
this.timestamps.push(now);
return true;
}
}
// ============================================================================
// Crash relay client
// ============================================================================
// Reports are sent to a Cloudflare Worker relay. All credentials are
// environment secrets on the relay -- nothing sensitive ships in this package.
//
// Default endpoint: https://camofox-telemetry.askjo.workers.dev
// Override: CAMOFOX_CRASH_REPORT_URL=https://your-own-endpoint/report
//
// The relay source lives at workers/crash-reporter/index.ts in this repo.
// Verify: GET /source returns { commit, sha256 } to compare against the repo.
// Full source: https://github.com/jo-inc/camofox-browser/blob/main/workers/crash-reporter/index.ts
const DEFAULT_RELAY_URL = 'https://camofox-telemetry.askjo.workers.dev/report';
const FETCH_TIMEOUT_MS = 5000;
let _relayUrl = DEFAULT_RELAY_URL;
function fetchWithTimeout(url, options) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
return fetch(url, { ...options, signal: controller.signal })
.finally(() => clearTimeout(timer));
}
/**
* Send a crash report to the relay. Returns true if accepted.
* Never throws -- reporter must never crash the server.
*/
export async function sendToRelay(payload) {
try {
const resp = await fetchWithTimeout(_relayUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
return resp.ok || resp.status === 429; // rate-limited is fine, not an error
} catch {
return false;
}
}
// ============================================================================
// Issue formatting
// ============================================================================
function formatIssueBody(type, detail) {
const sections = [
'> Auto-reported by camofox-crash-reporter. All data is anonymized.',
'',
'## Environment',
`- **version:** ${detail.version || 'unknown'}`,
`- **node:** ${detail.nodeVersion || 'unknown'}`,
`- **platform:** ${detail.platform || 'unknown'}`,
`- **uptime:** ${detail.uptimeMinutes != null ? detail.uptimeMinutes + ' min' : 'unknown'}`,
];
// Resource snapshot (memory, handles, browser RSS)
const r = detail.resources;
if (r) {
sections.push('', '## Resources');
sections.push(`- **node RSS:** ${r.nodeRssMb ?? '?'} MB`);
sections.push(`- **node heap:** ${r.nodeHeapUsedMb ?? '?'} / ${r.nodeHeapTotalMb ?? '?'} MB`);
if (r.browserRssMb != null) sections.push(`- **browser RSS:** ${r.browserRssMb} MB`);
if (r.browserContexts != null) sections.push(`- **browser contexts:** ${r.browserContexts}`);
if (r.activeTabs != null) sections.push(`- **active tabs:** ${r.activeTabs}`);
if (r.openFds != null) sections.push(`- **open FDs:** ${r.openFds}`);
if (r.activeHandles != null) sections.push(`- **active handles:** ${r.activeHandles}`);
if (r.eventLoopLagMs != null) sections.push(`- **event loop lag:** ${r.eventLoopLagMs} ms`);
}
// Error info
if (detail.signal) sections.push('', `**Signal:** ${detail.signal}`);
if (detail.activeRoute) sections.push(`**Active route:** ${detail.activeRoute}`);
if (detail.message) {
sections.push('', '## Error', '```', anonymize(detail.message), '```');
}
if (detail.stack) {
sections.push('', '## Stack Trace', '```', anonymize(detail.stack), '```');
}
// Hang-specific details
if (detail.hang) {
const h = detail.hang;
sections.push('', '## Hang Details');
sections.push(`- **operation:** ${h.operation}`);
sections.push(`- **duration:** ${Math.round(h.durationMs / 1000)}s`);
if (h.lockQueueMs != null) sections.push(`- **lock queue wait:** ${Math.round(h.lockQueueMs)}ms`);
if (h.documentReadyState) sections.push(`- **document.readyState:** ${h.documentReadyState}`);
if (h.inflightRequests != null) sections.push(`- **in-flight requests:** ${h.inflightRequests}`);
}
// Anti-bot detection
if (detail.botDetection?.detected) {
const b = detail.botDetection;
sections.push('', '## Anti-Bot Detection');
sections.push(`- **provider:** ${b.provider || 'unknown'}`);
sections.push(`- **HTTP status:** ${b.httpStatus || '?'}`);
if (b.responseBodySizeKb != null) sections.push(`- **response size:** ${b.responseBodySizeKb} KB`);
if (b.redirectChainLength != null) sections.push(`- **redirect chain:** ${b.redirectChainLength} hops`);
if (b.redirectStatusCodes?.length) sections.push(`- **redirect statuses:** ${b.redirectStatusCodes.join(' -> ')}`);
}
// Proxy info (safe fields only -- no IPs, credentials, or hostnames)
if (detail.proxy) {
const p = detail.proxy;
sections.push('', '## Proxy');
sections.push(`- **configured:** ${p.configured}`);
if (p.configured) {
if (p.type) sections.push(`- **type:** ${p.type}`);
sections.push(`- **auth configured:** ${p.authConfigured ?? 'unknown'}`);
if (p.error) sections.push(`- **error:** ${p.error}`);
if (p.tlsError) sections.push(`- **TLS error:** yes`);
}
}
// Stall-specific details
if (detail.stall) {
const s = detail.stall;
sections.push('', '## Stall Details');
sections.push(`- **stall duration:** ${Math.round(s.driftMs / 1000)}s`);
if (s.classification) sections.push(`- **classification:** ${s.classification}`);
if (s.cpuElapsedS != null) sections.push(`- **CPU time during stall:** ${s.cpuElapsedS}s`);
if (s.cpuRatio != null) sections.push(`- **CPU/wall ratio:** ${s.cpuRatio}`);
if (s.sigcontInWindow != null) sections.push(`- **SIGCONT in window:** ${s.sigcontInWindow}`);
if (s.hrtimeWallDriftS != null) sections.push(`- **hrtime<->wall drift:** ${s.hrtimeWallDriftS}s`);
if (s.eventLoopDelay) {
const eld = s.eventLoopDelay;
sections.push(`- **event loop delay:** p50=${eld.p50Ms}ms p99=${eld.p99Ms}ms max=${eld.maxMs}ms`);
}
if (s.lastRoute) sections.push(`- **last route:** ${s.lastRoute}`);
if (s.activeHandles != null) sections.push(`- **active handles:** ${s.activeHandles}`);
if (s.activeRequests != null) sections.push(`- **active requests:** ${s.activeRequests}`);
if (s.heapDeltaMb != null) sections.push(`- **heap delta:** ${s.heapDeltaMb > 0 ? '+' : ''}${s.heapDeltaMb} MB`);
}
// Context (misc extra data)
if (detail.nativeMemory) {
const nm = detail.nativeMemory;
sections.push('', '## Native Memory Details');
sections.push(`- **baseline:** ${nm.baselineMb} MB`);
sections.push(`- **current:** ${nm.currentMb} MB`);
sections.push(`- **high-water:** ${nm.highWaterMb} MB`);
sections.push(`- **growth:** ${nm.growthMb} MB`);
sections.push(`- **node RSS:** ${nm.rssMb} MB`);
sections.push(`- **heap used:** ${nm.heapUsedMb} MB`);
sections.push(`- **external:** ${nm.externalMb} MB`);
if (nm.lastSeenBrowserRssMb != null) sections.push(`- **browser RSS (last seen):** ${nm.lastSeenBrowserRssMb} MB`);
else sections.push(`- **browser RSS (last seen):** not captured (browser already dead)`);
}
if (detail.context && Object.keys(detail.context).length > 0) {
sections.push('', '<details><summary>Context</summary>', '', '```json', anonymize(JSON.stringify(detail.context, null, 2)), '```', '', '</details>');
}
return sections.join('\n');
}
// ============================================================================
// Core reporter factory
// ============================================================================
/**
* Create a reporter instance.
*
* @param {object} config
* @param {boolean} config.crashReportEnabled
* @param {string} config.crashReportRepo - "owner/repo" (env override)
* @param {number} config.crashReportRateLimit - max reports per hour
* @param {object} config.crashReporterConfig - from camofox.config.json crashReporter section
* @param {string} [config.version] - package version
*/
export function createReporter(config) {
// Set relay URL (env override for self-hosted relays)
_relayUrl = config.crashReportUrl || DEFAULT_RELAY_URL;
const enabled = config.crashReportEnabled !== false;
const repo = config.crashReportRepo || 'jo-inc/camofox-browser';
const rateLimiters = {
crash: new RateLimiter(5), // 5 crashes/hr
hang: new RateLimiter(5), // 5 hangs/hr
stuck: new RateLimiter(2), // 2 stalls/hr (with active tabs only)
leak: new RateLimiter(2), // 2 leak alerts/hr
_default: new RateLimiter(config.crashReportRateLimit || 10),
};
const version = config.version || 'unknown';
let watchdogInterval = null;
let _resetNativeMemBaseline = false; // Set by resetNativeMemBaseline(), read by watchdog
let lastTick = Date.now();
const inFlight = new Set();
// Track last Express route for stall reports
let _lastRoute = null;
// No-op when disabled
if (!enabled) {
return {
reportCrash: async () => {},
reportHang: async () => {},
reportStuckLoop: async () => {},
startWatchdog: () => {},
trackRoute: () => {},
stop: () => {},
resetNativeMemBaseline: () => {},
_anonymize: anonymize,
_stackSignature: stackSignature,
};
}
/** Core: build and send a report to the relay. NEVER throws. */
async function fileReport(type, labels, detail) {
const bucket = type.startsWith('stuck:') ? 'stuck' : type.startsWith('hang:') ? 'hang' : type.startsWith('leak:') ? 'leak' : 'crash';
const limiter = rateLimiters[bucket] || rateLimiters._default;
if (!limiter.tryAcquire()) return;
const reportPromise = (async () => {
try {
const sig = stackSignature(type, detail.error || { message: detail.message, stack: detail.stack });
const safeMessage = anonymize(detail.message || detail.error?.message || type);
const title = `[${sig}] ${type}: ${safeMessage.slice(0, 120)}`;
const body = formatIssueBody(type, {
...detail,
version,
nodeVersion: typeof process !== 'undefined' ? process.version : 'unknown',
platform: typeof process !== 'undefined' ? process.platform : 'unknown',
});
const issueLabels = Array.isArray(labels) ? labels : [labels, 'auto-report'];
await sendToRelay({
type,
signature: sig,
title,
body,
labels: issueLabels,
version,
});
} catch {
// Swallow -- reporter must never crash the server
}
})();
inFlight.add(reportPromise);
reportPromise.finally(() => inFlight.delete(reportPromise));
}
/**
* Track the last Express route for stall diagnostics.
* Call from middleware: reporter.trackRoute(req.method + ' ' + req.route?.path)
*/
function trackRoute(route) {
_lastRoute = route || null;
}
async function reportCrash(error, opts = {}) {
const err = error instanceof Error ? error : new Error(String(error));
const uptimeMinutes = typeof process !== 'undefined'
? Math.round(process.uptime() / 60) : undefined;
const resources = collectResourceSnapshot(opts.resourceOpts || {});
await fileReport(
opts.signal ? `signal:${opts.signal}` : (err.name || 'crash'),
['crash', 'auto-report'],
{
error: err,
message: err.message,
stack: err.stack,
signal: opts.signal || null,
activeRoute: _lastRoute,
uptimeMinutes,
resources,
proxy: opts.proxy || null,
context: opts.context,
},
);
}
async function reportHang(operation, durationMs, opts = {}) {
const uptimeMinutes = typeof process !== 'undefined'
? Math.round(process.uptime() / 60) : undefined;
const resources = collectResourceSnapshot(opts.resourceOpts || {});
// Build lean context (journal only, no redundant fields)
const context = { ...opts.context };
if (context.journal) {
context.journal = context.journal.map(j => typeof j === 'string' ? j : j);
}
// Remove fields that now have dedicated sections
delete context.operation;
delete context.durationMs;
// Anti-bot detection from health snapshot
const healthSnap = opts.healthSnapshot;
const botDetection = healthSnap?.botDetection?.detected ? {
...healthSnap.botDetection,
responseBodySizeKb: healthSnap.lastNavResponseSize
? Math.round(healthSnap.lastNavResponseSize / 1024) : null,
redirectChainLength: healthSnap.redirectStatusCodes?.length || null,
redirectStatusCodes: healthSnap.redirectStatusCodes?.length
? healthSnap.redirectStatusCodes : null,
} : null;
// Get document.readyState if healthTracker provided
let documentReadyState = null;
if (opts.healthTracker?.getReadyState) {
documentReadyState = await opts.healthTracker.getReadyState();
}
const labels = ['hang', 'auto-report'];
if (botDetection?.detected) labels.push('bot-detection');
await fileReport(
`hang:${operation}`,
labels,
{
message: `Operation "${operation}" hung for ${Math.round(durationMs / 1000)}s`,
stack: opts.error?.stack,
activeRoute: _lastRoute,
uptimeMinutes,
resources,
hang: {
operation,
durationMs,
lockQueueMs: opts.lockQueueMs ?? null,
documentReadyState,
inflightRequests: healthSnap?.inflightRequests ?? null,
},
botDetection,
proxy: opts.proxy || null,
context,
},
);
}
async function reportStuckLoop(durationMs, opts = {}) {
const uptimeMinutes = typeof process !== 'undefined'
? Math.round(process.uptime() / 60) : undefined;
const resources = collectResourceSnapshot(opts.resourceOpts || {});
await fileReport(
'stuck:tab-lock',
['stuck', 'auto-report'],
{
message: `Tab lock held for ${Math.round(durationMs / 1000)}s (tab destroyed)`,
uptimeMinutes,
resources,
context: { durationMs, ...opts.context },
},
);
}
function startWatchdog(thresholdMs = 5000, getContext) {
if (watchdogInterval) return;
const checkMs = 1000;
lastTick = Date.now();
let lastCpuUsage = process.cpuUsage();
let lastHrtime = process.hrtime.bigint();
let lastHeapUsed = process.memoryUsage().heapUsed;
// --- Native memory leak tracking ---
// Track RSS minus JS heap over time to detect native/external memory leaks.
// Sample every 30s, alert if native memory stays >400MB above baseline for
// 3 consecutive checks (~90s). This avoids false positives from:
// - Browser initialization spikes (first 2 min)
// - One-time allocations that stabilize
// - Post-session RSS that hasn't been reclaimed by the OS yet
// - Self-healing restart (kills browser at 200MB growth when sessions=0)
// The memory pressure restart in server.js fires at 200MB when idle.
// We only report at 400MB to catch cases where self-healing FAILED.
let nativeMemBaseline = null; // RSS - heapUsed at first measurement
let nativeMemHighWater = 0;
let lastNativeMemCheck = 0;
const NATIVE_MEM_CHECK_INTERVAL_MS = 30_000;
const NATIVE_MEM_LEAK_THRESHOLD_MB = 400; // alert only when growth exceeds self-healing threshold
const NATIVE_MEM_MIN_UPTIME_S = 120; // don't measure until process has been up 2 min
const NATIVE_MEM_CONSECUTIVE_REQUIRED = 3; // require 3 consecutive checks above threshold
const NATIVE_MEM_GRACE_CHECKS = 2; // skip 2 checks after baseline reset (let memory settle)
let nativeMemAlertFired = false;
let nativeMemConsecutiveAbove = 0; // consecutive checks above threshold
let nativeMemGraceRemaining = 0; // checks to skip after baseline reset
let lastSeenBrowserRssMb = null; // captured during growth checks while browser is alive
// SIGCONT detection -- macOS sends SIGCONT on wake from sleep/suspend
let lastSigcont = 0;
try { process.on('SIGCONT', () => { lastSigcont = Date.now(); }); } catch { /* unavailable */ }
// Event loop delay histogram (perf_hooks) -- correlating evidence
let elHistogram = null;
try {
elHistogram = monitorEventLoopDelay({ resolution: 20 });
elHistogram.enable();
} catch { /* unavailable */ }
// Suppress false positives from OS sleep/suspend (laptop lid close, VM pause).
// Stalls > 120s are almost certainly not event-loop bugs.
const MAX_REPORTABLE_DRIFT_MS = 60_000;
let suppressTicksRemaining = 0;
const SUPPRESS_TICKS_AFTER_WAKE = 5;
lastTick = Date.now();
lastHeapUsed = process.memoryUsage().heapUsed;
watchdogInterval = setInterval(() => {
const now = Date.now();
const drift = now - lastTick - checkMs;
const cpuDelta = process.cpuUsage(lastCpuUsage);
const hrtimeNow = process.hrtime.bigint();
const hrtimeDeltaMs = Number(hrtimeNow - lastHrtime) / 1e6;
lastTick = now;
lastCpuUsage = process.cpuUsage();
lastHrtime = hrtimeNow;
// After a long sleep/suspend, suppress the next few ticks (post-wake jitter)
if (drift > MAX_REPORTABLE_DRIFT_MS) {
suppressTicksRemaining = SUPPRESS_TICKS_AFTER_WAKE;
lastHeapUsed = process.memoryUsage().heapUsed;
return;
}
if (suppressTicksRemaining > 0) {
suppressTicksRemaining--;
lastHeapUsed = process.memoryUsage().heapUsed;
return;
}
// --- Native memory leak detection (runs every ~30s) ---
if (now - lastNativeMemCheck >= NATIVE_MEM_CHECK_INTERVAL_MS) {
lastNativeMemCheck = now;
try {
// Skip until process has been up long enough for browser to initialize.
// Browser launch causes a 100-300MB RSS spike that isn't a leak.
if (process.uptime() >= NATIVE_MEM_MIN_UPTIME_S) {
// Check if baseline should be reset (e.g. after browser close)
if (_resetNativeMemBaseline) {
nativeMemBaseline = null;
nativeMemHighWater = 0;
nativeMemAlertFired = false;
nativeMemConsecutiveAbove = 0;
nativeMemGraceRemaining = NATIVE_MEM_GRACE_CHECKS;
_resetNativeMemBaseline = false;
}
// Grace period after reset -- let memory settle before re-baselining
if (nativeMemGraceRemaining > 0) {
nativeMemGraceRemaining--;
} else {
const mem = process.memoryUsage();
const nativeMemMb = Math.round((mem.rss - mem.heapUsed) / 1048576);
if (nativeMemBaseline === null) {
nativeMemBaseline = nativeMemMb;
}
nativeMemHighWater = Math.max(nativeMemHighWater, nativeMemMb);
const growth = nativeMemMb - nativeMemBaseline;
if (growth > NATIVE_MEM_LEAK_THRESHOLD_MB && !nativeMemAlertFired) {
// Require sustained growth -- one-time spikes aren't leaks.
// Must exceed threshold on 3 consecutive checks (~90s).
nativeMemConsecutiveAbove++;
// Capture browser RSS NOW while it may still be alive.
// By report time the browser is often killed by memory pressure restart,
// making browserRssMb null. This preserves the last-seen value.
try {
if (getContext) {
const ctx = getContext();
if (ctx.resourceOpts?.browserPid) {
const snap = collectResourceSnapshot(ctx.resourceOpts);
if (snap.browserRssMb != null) lastSeenBrowserRssMb = snap.browserRssMb;
}
}
} catch { /* swallow */ }
if (nativeMemConsecutiveAbove >= NATIVE_MEM_CONSECUTIVE_REQUIRED) {
nativeMemAlertFired = true;
let extra = {};
try { if (getContext) extra = getContext(); } catch { /* swallow */ }
const resources = collectResourceSnapshot(extra.resourceOpts || {});
delete extra.resourceOpts;
// Skip report if sessions=0 — memory pressure restart handles idle leaks.
// Only report when sessions are active (restart CAN'T fire) or restart failed.
const sessionCount = resources.browserContexts ?? 0;
if (sessionCount === 0 && resources.browserRssMb == null) {
// Browser already dead, restart mechanism handled it. Don't spam.
// But if growth is extreme (>600MB), report anyway — restart may have failed.
if (growth < 600) {
// Self-healing. Skip report.
return;
}
}
fileReport('leak:native-memory', ['auto-report', 'memory-leak'], {
message: `Native memory grew by ${growth}MB (baseline: ${nativeMemBaseline}MB, current: ${nativeMemMb}MB, high-water: ${nativeMemHighWater}MB)`,
uptimeMinutes: Math.round(process.uptime() / 60),
resources,
nativeMemory: {
baselineMb: nativeMemBaseline,
currentMb: nativeMemMb,
highWaterMb: nativeMemHighWater,
growthMb: growth,
rssMb: Math.round(mem.rss / 1048576),
heapUsedMb: Math.round(mem.heapUsed / 1048576),
externalMb: Math.round(mem.external / 1048576),
lastSeenBrowserRssMb,
},
context: extra,
});
}
} else {
// Reset consecutive counter if memory dropped back below threshold
nativeMemConsecutiveAbove = 0;
}
}
}
} catch { /* swallow */ }
}
if (drift > thresholdMs) {
// CPU time consumed during the stall interval (user + system, in seconds)
const cpuElapsedS = (cpuDelta.user + cpuDelta.system) / 1e6;
const wallElapsedS = drift / 1000;
const cpuRatio = wallElapsedS > 0 ? cpuElapsedS / wallElapsedS : 0;
// SIGCONT within the stall window = OS sleep/resume
const sigcontInWindow = lastSigcont > 0 && (now - lastSigcont) < drift + 2000;
// hrtime vs wall clock drift (macOS: hrtime doesn't advance during sleep)
const hrtimeWallDriftS = Math.abs((drift - (hrtimeDeltaMs - checkMs))) / 1000;
// Classify: sleep vs real stall
let classification;
if (cpuRatio < 0.01 && sigcontInWindow) classification = 'sleep';
else if (cpuRatio < 0.001) classification = 'likely_sleep';
else if (cpuRatio < 0.01) classification = 'likely_sleep';
else if (cpuRatio > 0.1) classification = 'real_stall';
else classification = 'ambiguous';
// Don't file reports for sleep/suspend-resume stalls
if (classification === 'sleep' || classification === 'likely_sleep') {
lastHeapUsed = process.memoryUsage().heapUsed;