-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbackground.js
More file actions
1220 lines (1045 loc) · 41.6 KB
/
background.js
File metadata and controls
1220 lines (1045 loc) · 41.6 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
/**
* debugHunter v2.0.6 - Background Service Worker
* Multi-factor detection with configurable comparison strategies
* - Added redirect detection to filter false positives on paths
* - Added natural variance measurement to filter false positives on dynamic sites
* - Require variance check for all detections without debug indicators
*/
import { stringSimilarity } from './similarity.js';
// ============================================================================
// DETECTION PATTERNS
// ============================================================================
const debugParams = {
high: [
{ key: "_debug", value: "1" },
{ key: "debug", value: "1" },
{ key: "debug", value: "true" },
{ key: "debug_mode", value: "1" },
{ key: "XDEBUG_SESSION_START", value: "phpstorm" },
{ key: "XDEBUG_SESSION", value: "1" },
{ key: "debugbar", value: "1" },
{ key: "profiler", value: "1" },
{ key: "trace", value: "1" },
{ key: "verbose", value: "1" },
{ key: "show_errors", value: "1" },
{ key: "display_errors", value: "1" },
{ key: "dev_mode", value: "1" },
{ key: "phpinfo", value: "1" },
{ key: "error_reporting", value: "E_ALL" },
],
medium: [
{ key: "env", value: "dev" },
{ key: "env", value: "staging" },
{ key: "env", value: "pre" },
{ key: "env", value: "sandbox" },
{ key: "environment", value: "dev" },
{ key: "staging", value: "1" },
{ key: "beta", value: "1" },
{ key: "internal", value: "1" },
{ key: "test", value: "1" },
{ key: "admin", value: "1" },
]
};
const customHeaders = [
{ key: "X-Debug", value: "1" },
{ key: "X-Forwarded-Host", value: "localhost" },
{ key: "X-Forwarded-For", value: "127.0.0.1" },
{ key: "X-Original-URL", value: "/admin" },
{ key: "X-Env", value: "dev" },
{ key: "Env", value: "pre" },
{ key: "Env", value: "dev" },
];
// Sensitive paths organized by priority (critical first, checked with HEAD before GET)
const sensitivePaths = {
// Critical - secrets, credentials (always check these)
critical: [
"/.env",
"/.git/config",
"/config.json",
"/.env.local",
"/.env.production",
"/.env.development",
"/credentials.json",
"/auth.json",
"/secrets.json",
"/database.yml",
"/wp-config.php.bak",
"/wp-config.php.old",
"/.aws/credentials",
"/backup.sql",
"/dump.sql",
"/.htpasswd",
"/actuator/env",
"/actuator/heapdump",
],
// High - debug endpoints, source code
high: [
"/.git/HEAD",
"/.git/logs/HEAD",
"/.svn/entries",
"/phpinfo.php",
"/info.php",
"/graphiql",
"/__debug__",
"/debug",
"/server-status",
"/elmah.axd",
"/trace.axd",
"/rails/info/properties",
"/package.json",
"/composer.json",
],
// Medium - documentation, configs
medium: [
"/swagger-ui.html",
"/swagger.json",
"/api-docs",
"/openapi.json",
"/web.config",
"/.htaccess",
"/Dockerfile",
"/docker-compose.yml",
],
};
// Default patterns to filter dynamic content
const DEFAULT_DYNAMIC_PATTERNS = [
/\b\d{10,13}\b/g, // Unix timestamps
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/g, // ISO dates
/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/gi, // UUIDs
/csrf[_-]?token["']?\s*[:=]\s*["']?[^"'\s]+/gi,
/nonce["']?\s*[:=]\s*["']?[^"'\s]+/gi,
/_token["']?\s*[:=]\s*["']?[^"'\s]+/gi,
/session[_-]?id["']?\s*[:=]\s*["']?[^"'\s]+/gi,
/PHPSESSID=[^;\s]+/gi,
/JSESSIONID=[^;\s]+/gi,
/\?v=\d+/g,
/\?_=\d+/g,
/data-request-id="[^"]+"/g,
/data-nonce="[^"]+"/g,
];
// Soft 404 indicators
const soft404Indicators = [
"404", "not found", "page not found", "no encontrado",
"doesn't exist", "does not exist", "cannot be found",
"error 404", "oops", "sorry, we couldn't find"
];
// Debug indicators for content validation
const debugIndicators = {
critical: [
/DB_PASSWORD\s*[=:]\s*['"]?[^'"}\s]+/i,
/API_KEY\s*[=:]\s*['"]?[a-zA-Z0-9_-]{20,}/i,
/SECRET_KEY\s*[=:]\s*['"]?[^'"}\s]+/i,
/AWS_SECRET_ACCESS_KEY/i,
/-----BEGIN (RSA |DSA |EC )?PRIVATE KEY-----/,
/password\s*[=:]\s*['"]?[^'"}\s]{4,}/i,
/mysql:\/\/[^:]+:[^@]+@/i,
/postgres:\/\/[^:]+:[^@]+@/i,
/mongodb(\+srv)?:\/\/[^:]+:[^@]+@/i,
],
high: [
/stack\s*trace/i,
/Traceback \(most recent call last\)/,
/at [A-Za-z0-9_$]+\.[A-Za-z0-9_$]+\([^)]*:\d+\)/,
/Fatal error:/i,
/Parse error:/i,
/java\.lang\.\w+Exception/,
/xdebug/i,
/debugbar/i,
/\[core\]\s*\n\s*repositoryformatversion/i,
],
medium: [
/PHP\/\d+\.\d+/,
/Apache\/\d+\.\d+/,
/nginx\/\d+\.\d+/,
/\/home\/[a-z]+\//i,
/\/var\/www\//i,
/debug\s*[=:]\s*true/i,
],
low: [
/Warning:/i,
/Notice:/i,
/Deprecated:/i,
]
};
// Response headers that indicate debug mode
const debugHeaders = [
'x-debug', 'x-debug-token', 'x-debug-link',
'x-debugbar', 'x-powered-by', 'server',
'x-aspnet-version', 'x-aspnetmvc-version'
];
// ============================================================================
// SETTINGS
// ============================================================================
async function getSettings() {
const result = await chrome.storage.sync.get([
'enabled',
'detectionMode',
'requireDebugIndicators',
'detectStatusChanges',
'detectHeaderChanges',
'filterDynamicContent',
'similarityThreshold',
'minLengthDiff',
'checkInterval',
'dynamicPatterns',
'baseDelay',
'maxConcurrent',
'whitelist'
]);
return {
enabled: result.enabled !== false, // Enabled by default
detectionMode: result.detectionMode || 'smart',
requireDebugIndicators: result.requireDebugIndicators !== false,
detectStatusChanges: result.detectStatusChanges !== false,
detectHeaderChanges: result.detectHeaderChanges !== false,
filterDynamicContent: result.filterDynamicContent !== false,
similarityThreshold: result.similarityThreshold || 0.90,
minLengthDiff: result.minLengthDiff || 200,
checkInterval: (result.checkInterval || 480) * 60 * 1000,
dynamicPatterns: result.dynamicPatterns || [],
baseDelay: result.baseDelay || 300,
maxConcurrent: parseInt(result.maxConcurrent) || 3,
whitelist: result.whitelist || []
};
}
// ============================================================================
// RATE LIMITING
// ============================================================================
const rateLimitState = new Map();
async function rateLimitedFetch(url, options = {}) {
const settings = await getSettings();
const domain = new URL(url).hostname;
if (!rateLimitState.has(domain)) {
rateLimitState.set(domain, { delay: settings.baseDelay, lastRequest: 0 });
}
const state = rateLimitState.get(domain);
const now = Date.now();
const timeSince = now - state.lastRequest;
if (timeSince < state.delay) {
await new Promise(r => setTimeout(r, state.delay - timeSince));
}
state.lastRequest = Date.now();
try {
const response = await fetch(url, { ...options, signal: AbortSignal.timeout(15000) });
if ([429, 503, 502].includes(response.status)) {
state.delay = Math.min(state.delay * 2, 10000);
const retryAfter = response.headers.get('Retry-After');
if (retryAfter) {
await new Promise(r => setTimeout(r, parseInt(retryAfter) * 1000));
}
throw new Error(`Rate limited: ${response.status}`);
}
// Recover delay on success
state.delay = Math.max(state.delay * 0.9, settings.baseDelay);
return response;
} catch (error) {
if (error.name === 'TimeoutError') {
state.delay = Math.min(state.delay * 1.5, 10000);
}
throw error;
}
}
// ============================================================================
// CONTENT FILTERING & COMPARISON
// ============================================================================
function filterDynamicContent(text, customPatterns = []) {
let filtered = text;
// Apply default patterns
for (const pattern of DEFAULT_DYNAMIC_PATTERNS) {
filtered = filtered.replace(pattern, '[DYNAMIC]');
}
// Apply custom patterns
for (const patternStr of customPatterns) {
try {
const pattern = new RegExp(patternStr, 'gi');
filtered = filtered.replace(pattern, '[CUSTOM_DYNAMIC]');
} catch (e) {
// Invalid regex, skip
}
}
return filtered;
}
function containsDebugIndicators(text) {
for (const level of ['critical', 'high', 'medium', 'low']) {
for (const pattern of debugIndicators[level]) {
if (pattern.test(text)) {
return { found: true, level };
}
}
}
return { found: false, level: null };
}
function getLevelPriority(level) {
const priorities = { critical: 4, high: 3, medium: 2, low: 1 };
return priorities[level] || 0;
}
function extractInterestingHeaders(response) {
const found = {};
for (const header of debugHeaders) {
const value = response.headers.get(header);
if (value) {
found[header] = value;
}
}
return found;
}
function compareHeaders(original, modified) {
const changes = [];
// Check for new headers in modified response
for (const [key, value] of Object.entries(modified)) {
if (!original[key]) {
changes.push({ type: 'added', header: key, value });
} else if (original[key] !== value) {
changes.push({ type: 'changed', header: key, from: original[key], to: value });
}
}
return changes;
}
// ============================================================================
// MULTI-FACTOR COMPARISON
// ============================================================================
async function analyzeResponseDifference(originalResponse, modifiedResponse, originalText, modifiedText, settings, naturalVariance = null) {
const result = {
isDifferent: false,
confidence: 0,
reasons: [],
severity: 'low',
debugIndicators: null,
headerChanges: [],
requiresVarianceCheck: false, // Flag to trigger control request verification
};
// 1. Status code change detection
if (settings.detectStatusChanges && originalResponse.status !== modifiedResponse.status) {
result.reasons.push(`Status changed: ${originalResponse.status} → ${modifiedResponse.status}`);
result.confidence += 30;
// Special cases
if (originalResponse.status === 403 && modifiedResponse.status === 200) {
result.confidence += 40; // Bypass!
result.severity = 'critical';
} else if (modifiedResponse.status >= 500) {
result.confidence += 20; // Server error triggered
result.severity = 'high';
}
}
// 2. Header changes
if (settings.detectHeaderChanges) {
const originalHeaders = extractInterestingHeaders(originalResponse);
const modifiedHeaders = extractInterestingHeaders(modifiedResponse);
result.headerChanges = compareHeaders(originalHeaders, modifiedHeaders);
if (result.headerChanges.length > 0) {
result.reasons.push(`Header changes detected: ${result.headerChanges.length}`);
result.confidence += 15 * result.headerChanges.length;
}
}
// 3. Content length difference (variance-aware)
const lengthDiff = Math.abs(modifiedText.length - originalText.length);
// If we know the site's natural variance, only count if difference EXCEEDS natural variance
const isLengthWithinVariance = naturalVariance && lengthDiff <= naturalVariance.lengthDiff * 1.2;
if (!isLengthWithinVariance && lengthDiff >= settings.minLengthDiff) {
result.reasons.push(`Content length diff: ${lengthDiff} bytes`);
result.confidence += Math.min(lengthDiff / 100, 25);
}
// 4. Debug indicator detection - only count if NEW (not present in original)
const debugCheckModified = containsDebugIndicators(modifiedText);
const debugCheckOriginal = containsDebugIndicators(originalText);
// Only consider debug indicators that are NEW (caused by the param/header)
// If the same level of indicator exists in original, it's not caused by our test
const debugCheck = {
found: debugCheckModified.found && (!debugCheckOriginal.found ||
getLevelPriority(debugCheckModified.level) > getLevelPriority(debugCheckOriginal.level)),
level: debugCheckModified.level,
};
if (debugCheck.found) {
result.debugIndicators = debugCheck;
result.reasons.push(`Debug indicators found: ${debugCheck.level}`);
result.confidence += debugCheck.level === 'critical' ? 50 : debugCheck.level === 'high' ? 35 : 20;
if (debugCheck.level === 'critical') result.severity = 'critical';
else if (debugCheck.level === 'high' && result.severity !== 'critical') result.severity = 'high';
else if (debugCheck.level === 'medium' && !['critical', 'high'].includes(result.severity)) result.severity = 'medium';
}
// 5. Similarity check (after filtering dynamic content, variance-aware)
let originalFiltered = originalText;
let modifiedFiltered = modifiedText;
if (settings.filterDynamicContent) {
originalFiltered = filterDynamicContent(originalText, settings.dynamicPatterns);
modifiedFiltered = filterDynamicContent(modifiedText, settings.dynamicPatterns);
}
const similarity = stringSimilarity.compareTwoStrings(originalFiltered, modifiedFiltered);
// If we know the site's natural variance, only count if similarity is WORSE than natural variance
// E.g., if site naturally has 92% similarity between requests, only flag if this request is < 90%
const isSimilarityWithinVariance = naturalVariance && similarity >= naturalVariance.similarity - 0.02;
if (!isSimilarityWithinVariance && similarity < settings.similarityThreshold) {
result.reasons.push(`Similarity: ${(similarity * 100).toFixed(1)}%`);
result.confidence += (1 - similarity) * 30;
}
// If we have confidence but NO debug indicators, always verify with variance check
// This prevents false positives on dynamic sites (login pages, news sites, etc.)
if (!naturalVariance && !debugCheck.found && result.confidence > 0) {
result.requiresVarianceCheck = true;
}
// Only critical/high/medium indicators count as significant (low like "Warning:" can appear in normal pages)
const hasSignificantDebugIndicators = debugCheck.found && ['critical', 'high', 'medium'].includes(debugCheck.level);
// Determine if response is different based on mode
switch (settings.detectionMode) {
case 'aggressive':
// Any signal counts
result.isDifferent = result.confidence >= 15;
break;
case 'conservative':
// Needs debug indicators AND other signals
result.isDifferent = result.confidence >= 50 && debugCheck.found;
break;
case 'keywords-only':
// Only debug indicators matter
result.isDifferent = debugCheck.found;
break;
case 'smart':
default:
// Smart mode: require clear evidence to avoid false positives on dynamic sites
const hasStatusBypass = originalResponse.status === 403 && modifiedResponse.status === 200;
const hasServerError = modifiedResponse.status >= 500;
// Check if content is significantly different (not just dynamic variation)
const isSignificantlyDifferent = similarity < 0.70;
// Debug indicators in modified response (even if also in original)
const hasAnyDebugIndicators = debugCheckModified.found && ['critical', 'high', 'medium'].includes(debugCheckModified.level);
if (hasStatusBypass || hasServerError) {
// Clear signal - status change is strong evidence
result.isDifferent = true;
} else if (hasSignificantDebugIndicators) {
// NEW debug indicators found - report
result.isDifferent = result.confidence >= 40;
} else if (hasAnyDebugIndicators && isSignificantlyDifferent) {
// Debug indicators exist AND content is very different - likely more debug info triggered
result.isDifferent = true;
} else {
// No clear evidence - don't report to avoid FPs on dynamic sites
result.isDifferent = false;
}
break;
}
return result;
}
// ============================================================================
// STORAGE
// ============================================================================
async function getFindings() {
const result = await chrome.storage.local.get(['findings']);
return result.findings || { params: [], headers: [], paths: [] };
}
async function saveFindings(findings) {
await chrome.storage.local.set({ findings });
updateBadge(findings);
}
async function addFinding(type, data) {
const findings = await getFindings();
const exists = findings[type].some(f => {
if (type === 'params') return f.url === data.url;
if (type === 'headers') return f.url === data.url && f.header === data.header;
return f.path === data.path;
});
if (!exists) {
findings[type].push({ ...data, timestamp: Date.now() });
findings[type].sort((a, b) => {
const scores = { critical: 4, high: 3, medium: 2, low: 1 };
return (scores[b.severity] || 0) - (scores[a.severity] || 0);
});
await saveFindings(findings);
const colors = { critical: '#e74c3c', high: '#e67e22', medium: '#f1c40f', low: '#3498db' };
console.log(`%c[debugHunter] [${data.severity?.toUpperCase()}] ${type}: ${data.url || data.path}`,
`background: ${colors[data.severity] || '#27ae60'}; color: white; padding: 2px 6px; border-radius: 3px`);
}
}
async function removeFinding(type, identifier) {
const findings = await getFindings();
findings[type] = findings[type].filter(f => {
if (type === 'params') return f.url !== identifier;
if (type === 'headers') return `${f.url}|${f.header}` !== identifier;
return f.path !== identifier;
});
await saveFindings(findings);
}
async function clearFindings(type = null) {
if (type) {
const findings = await getFindings();
findings[type] = [];
await saveFindings(findings);
} else {
await saveFindings({ params: [], headers: [], paths: [] });
}
}
function updateBadge(findings) {
const total = findings.params.length + findings.headers.length + findings.paths.length;
const all = [...findings.params, ...findings.headers, ...findings.paths];
const hasCritical = all.some(f => f.severity === 'critical');
const hasHigh = all.some(f => f.severity === 'high');
let color = '#3498db';
if (hasCritical) color = '#e74c3c';
else if (hasHigh) color = '#e67e22';
chrome.action.setBadgeText({ text: total > 0 ? total.toString() : '' });
chrome.action.setBadgeBackgroundColor({ color });
}
// ============================================================================
// SCAN STATUS
// ============================================================================
let scanStatus = { active: false, domain: '' };
function updateScanStatus(status) {
scanStatus = { ...scanStatus, ...status };
try { chrome.runtime.sendMessage({ action: 'scanStatus', status: scanStatus }); } catch (e) {}
}
// ============================================================================
// URL VALIDATION
// ============================================================================
async function shouldScanUrl(url) {
if (url.startsWith('chrome://') || url.startsWith('chrome-extension://') ||
url.startsWith('about:') || url.startsWith('file://')) {
return false;
}
const settings = await getSettings();
const { hostname } = new URL(url);
// Check whitelist
for (const pattern of settings.whitelist) {
const parts = pattern.split('.').reverse();
const hostParts = hostname.split('.').reverse();
let match = true;
for (let i = 0; i < parts.length && match; i++) {
if (parts[i] !== '*' && parts[i] !== hostParts[i]) match = false;
}
if (match) return false;
}
// Check interval
const result = await chrome.storage.local.get(['checkedUrls']);
const checked = result.checkedUrls || {};
const lastChecked = checked[url];
if (lastChecked && (Date.now() - lastChecked < settings.checkInterval)) {
return false;
}
checked[url] = Date.now();
await chrome.storage.local.set({ checkedUrls: checked });
return true;
}
function isSoft404(text) {
const lower = text.toLowerCase();
return soft404Indicators.some(i => lower.includes(i.toLowerCase()));
}
function truncateForStorage(text, max = 5000) {
return text.length <= max ? text : text.substring(0, max) + '\n... [truncated]';
}
// ============================================================================
// BASELINE CACHE FOR PARAMS/HEADERS (avoid duplicate requests)
// ============================================================================
const urlBaselineCache = new Map();
async function getUrlBaseline(url) {
if (urlBaselineCache.has(url)) {
const cached = urlBaselineCache.get(url);
if (Date.now() - cached.timestamp < 60000) { // 1 min cache
return cached;
}
}
try {
const response = await rateLimitedFetch(url);
const text = await response.text();
const cached = {
response: {
status: response.status,
headers: Object.fromEntries([...response.headers.entries()]),
},
text,
timestamp: Date.now(),
};
// Mock response object for analysis
cached.mockResponse = {
status: cached.response.status,
headers: { get: (key) => cached.response.headers[key.toLowerCase()] },
};
urlBaselineCache.set(url, cached);
return cached;
} catch (e) {
return null;
}
}
// ============================================================================
// NATURAL VARIANCE MEASUREMENT (for dynamic sites)
// ============================================================================
const varianceCache = new Map();
async function measureNaturalVariance(url, baselineText, settings, useRandomParam = false) {
// Cache key includes whether we're measuring with params
const cacheKey = useRandomParam ? `${url}#withParam` : url;
// Check cache first (valid for 2 minutes)
if (varianceCache.has(cacheKey)) {
const cached = varianceCache.get(cacheKey);
if (Date.now() - cached.timestamp < 120000) {
return cached.variance;
}
}
try {
// For params, measure variance by adding a random param to see how the site responds
// This catches sites that return different content when ANY query param is present
let controlUrl = url;
if (useRandomParam) {
const randomParam = `_rnd${Math.random().toString(36).substring(7)}`;
const urlObj = new URL(url);
urlObj.searchParams.set(randomParam, '1');
controlUrl = urlObj.href;
}
const controlResponse = await rateLimitedFetch(controlUrl);
const controlText = await controlResponse.text();
// Filter dynamic content before comparison
let baselineFiltered = baselineText;
let controlFiltered = controlText;
if (settings.filterDynamicContent) {
baselineFiltered = filterDynamicContent(baselineText, settings.dynamicPatterns);
controlFiltered = filterDynamicContent(controlText, settings.dynamicPatterns);
}
// Calculate natural variance between baseline and control
const naturalSimilarity = stringSimilarity.compareTwoStrings(baselineFiltered, controlFiltered);
const naturalLengthDiff = Math.abs(controlText.length - baselineText.length);
const variance = {
similarity: naturalSimilarity,
lengthDiff: naturalLengthDiff,
// Site is "highly dynamic" if requests differ significantly
isHighlyDynamic: naturalSimilarity < 0.95,
};
varianceCache.set(cacheKey, { variance, timestamp: Date.now() });
return variance;
} catch (e) {
return null;
}
}
// ============================================================================
// PARAMETER CHECKING (uses cached baseline)
// ============================================================================
function appendParam(url, param) {
const u = new URL(url);
u.searchParams.set(param.key, param.value);
return u.href;
}
async function checkParams(url, baseline = null) {
const settings = await getSettings();
try {
// Use provided baseline or fetch new one
if (!baseline) {
baseline = await getUrlBaseline(url);
}
if (!baseline) return;
// Check high-confidence params first
const sortedParams = [
...debugParams.high.map(p => ({ ...p, confidence: 'high' })),
...debugParams.medium.map(p => ({ ...p, confidence: 'medium' })),
];
// Track if we've measured variance for this URL (lazy - only when needed)
let measuredVariance = null;
for (const param of sortedParams) {
const modifiedUrl = appendParam(url, param);
try {
const modifiedResponse = await rateLimitedFetch(modifiedUrl);
const modifiedText = await modifiedResponse.text();
// First analysis without variance
let analysis = await analyzeResponseDifference(
baseline.mockResponse, modifiedResponse,
baseline.text, modifiedText,
settings,
measuredVariance
);
// If flagged but needs variance verification (no debug indicators found)
if (analysis.isDifferent && analysis.requiresVarianceCheck && !measuredVariance) {
// Measure variance with a random param to see how site responds to ANY query param
// This catches sites that return different content when params are present (vs absent)
measuredVariance = await measureNaturalVariance(url, baseline.text, settings, true);
if (measuredVariance) {
// Re-analyze with variance knowledge - always re-check, not just for highly dynamic sites
analysis = await analyzeResponseDifference(
baseline.mockResponse, modifiedResponse,
baseline.text, modifiedText,
settings,
measuredVariance
);
}
}
if (analysis.isDifferent) {
await addFinding('params', {
url: modifiedUrl,
baseUrl: url,
param: `${param.key}=${param.value}`,
confidence: param.confidence,
severity: analysis.severity,
reasons: analysis.reasons,
originalResponse: truncateForStorage(baseline.text),
modifiedResponse: truncateForStorage(modifiedText),
});
}
} catch (e) {
// Skip this param on error
}
}
} catch (error) {
console.error(`[debugHunter] Params check failed: ${error.message}`);
}
}
// ============================================================================
// HEADER CHECKING (uses cached baseline)
// ============================================================================
async function checkHeaders(url, baseline = null) {
const settings = await getSettings();
try {
// Use provided baseline or fetch new one
if (!baseline) {
baseline = await getUrlBaseline(url);
}
if (!baseline) return;
// Track if we've measured variance for this URL (lazy - only when needed)
let measuredVariance = null;
for (const header of customHeaders) {
try {
const headers = new Headers();
headers.set(header.key, header.value);
const modifiedResponse = await rateLimitedFetch(url, { headers });
const modifiedText = await modifiedResponse.text();
// First analysis without variance
let analysis = await analyzeResponseDifference(
baseline.mockResponse, modifiedResponse,
baseline.text, modifiedText,
settings,
measuredVariance
);
// If flagged but needs variance verification (no debug indicators found)
if (analysis.isDifferent && analysis.requiresVarianceCheck && !measuredVariance) {
// Measure natural variance with a control request
measuredVariance = await measureNaturalVariance(url, baseline.text, settings);
if (measuredVariance) {
// Re-analyze with variance knowledge - always re-check, not just for highly dynamic sites
analysis = await analyzeResponseDifference(
baseline.mockResponse, modifiedResponse,
baseline.text, modifiedText,
settings,
measuredVariance
);
}
}
if (analysis.isDifferent) {
await addFinding('headers', {
url,
header: `${header.key}: ${header.value}`,
severity: analysis.severity,
reasons: analysis.reasons,
originalResponse: truncateForStorage(baseline.text),
modifiedResponse: truncateForStorage(modifiedText),
});
}
} catch (e) {
// Skip this header on error
}
}
} catch (error) {
console.error(`[debugHunter] Headers check failed: ${error.message}`);
}
}
// ============================================================================
// PATH CHECKING (Optimized with HEAD requests + domain caching)
// ============================================================================
// Cache for domain baselines and soft-404 fingerprints
const domainCache = new Map();
// Normalize redirect URL for comparison (resolves relative URLs, removes trailing slashes)
function normalizeRedirectUrl(location, baseUrl) {
try {
const resolved = new URL(location, baseUrl);
// Return pathname without trailing slash for consistent comparison
return resolved.pathname.replace(/\/$/, '') || '/';
} catch (e) {
return location;
}
}
// Check if a redirect is just URL normalization (trailing slash, case change)
function isNormalizationRedirect(originalPath, redirectPath) {
const normalizedOriginal = originalPath.replace(/\/$/, '').toLowerCase();
const normalizedRedirect = redirectPath.replace(/\/$/, '').toLowerCase();
return normalizedOriginal === normalizedRedirect;
}
async function getDomainBaseline(baseUrl) {
if (domainCache.has(baseUrl)) {
const cached = domainCache.get(baseUrl);
if (Date.now() - cached.timestamp < 300000) { // 5 min cache
return cached;
}
}
try {
// Get baseline response
const baseResponse = await rateLimitedFetch(baseUrl);
const baseText = await baseResponse.text();
// Get soft-404 fingerprint and catch-all redirect (request a random non-existent path)
const randomPath = `/${Math.random().toString(36).substring(7)}-${Date.now()}`;
let soft404Fingerprint = null;
let soft404Length = 0;
let catchAllRedirect = null;
try {
// Use redirect: 'manual' to detect catch-all redirects
const soft404Response = await rateLimitedFetch(baseUrl + randomPath, { redirect: 'manual' });
// Check if the random path redirects somewhere (catch-all redirect pattern)
if (soft404Response.status >= 300 && soft404Response.status < 400) {
const location = soft404Response.headers.get('location');
if (location) {
// Normalize the redirect URL for comparison
catchAllRedirect = normalizeRedirectUrl(location, baseUrl);
}
}
// For fingerprinting, follow the redirect to get actual content
const finalResponse = await rateLimitedFetch(baseUrl + randomPath);
const soft404Text = await finalResponse.text();
soft404Length = soft404Text.length;
// Create a fingerprint based on content structure, not exact content
soft404Fingerprint = {
status: finalResponse.status,
length: soft404Text.length,
hasTitle: /<title>/i.test(soft404Text),
isSoft404: isSoft404(soft404Text),
};
} catch (e) {
// Couldn't get soft-404, that's fine
}
const cached = {
baseText,
baseLength: baseText.length,
soft404Fingerprint,
soft404Length,
catchAllRedirect,
timestamp: Date.now(),
};
domainCache.set(baseUrl, cached);
return cached;
} catch (e) {
return null;
}
}
function matchesSoft404(response, text, fingerprint) {
if (!fingerprint) return false;
// If it returned the same status as our random 404 probe (non-200)
if (fingerprint.status === response.status && fingerprint.status !== 200) {
return true;
}
const lengthDiff = Math.abs(text.length - fingerprint.length);
const lengthRatio = lengthDiff / fingerprint.length;
// If content length is nearly identical (within 3%), very likely the same page
// This catches soft-404s that return 200 without "404" text
if (lengthRatio < 0.03) {
return true;
}
// If content length is similar (within 10%) AND has soft-404 indicators
if (lengthRatio < 0.1 && fingerprint.isSoft404) {
return true;
}
return false;
}
async function checkPathWithHead(baseUrl, path, settings, catchAllRedirect = null) {
const testUrl = baseUrl + path;
try {
// First, try HEAD request with redirect: manual to detect catch-all redirects
const headResponse = await rateLimitedFetch(testUrl, { method: 'HEAD', redirect: 'manual' });
// Check for redirects (3xx status codes)
if (headResponse.status >= 300 && headResponse.status < 400) {
const location = headResponse.headers.get('location');
if (location && catchAllRedirect) {
const redirectPath = normalizeRedirectUrl(location, baseUrl);
// Only skip if it redirects to the SAME place as the random path probe
if (redirectPath === catchAllRedirect && !isNormalizationRedirect(path, redirectPath)) {
return null; // Catch-all redirect - false positive
}
}
// For other redirects, continue and follow them (could be legit /admin -> /admin/login)
}
// Only proceed if status indicates potential content (or redirect that we'll follow)
if (headResponse.status === 200 || headResponse.status === 403 ||
(headResponse.status >= 300 && headResponse.status < 400)) {
const contentLength = parseInt(headResponse.headers.get('content-length') || '0');
// Skip if too small (likely empty or error) - but only for 200 responses
if (headResponse.status === 200 && contentLength > 0 && contentLength < 30) return null;
// Now do full GET to analyze content (this will follow redirects)