-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth2-verification-engine.js.backup
More file actions
912 lines (796 loc) · 27.2 KB
/
oauth2-verification-engine.js.backup
File metadata and controls
912 lines (796 loc) · 27.2 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
/**
* OAuth2 Verification Engine for Hera
*
* This module implements evidence-based OAuth2 vulnerability verification.
* Instead of making assumptions, it actively tests for vulnerabilities and
* collects concrete evidence for bug bounty submissions.
*/
class OAuth2VerificationEngine {
constructor(evidenceCollector) {
this.evidenceCollector = evidenceCollector;
// CRITICAL FIX P0: Persistent storage for verification state
this._activeFlows = new Map();
this._testResults = new Map();
this.initialized = false;
this.initPromise = this.initialize();
}
async initialize() {
if (this.initialized) return;
try {
// CRITICAL FIX P0: Use chrome.storage.local for test results (survives browser restart)
// Multi-day vulnerability testing must persist across sessions
const data = await chrome.storage.local.get(['oauth2VerificationEngine']);
if (data.oauth2VerificationEngine) {
const engine = data.oauth2VerificationEngine;
if (engine.activeFlows) {
for (const [id, flow] of Object.entries(engine.activeFlows)) {
this._activeFlows.set(id, flow);
}
}
if (engine.testResults) {
for (const [id, result] of Object.entries(engine.testResults)) {
this._testResults.set(id, result);
}
}
console.log(`Hera: Restored OAuth2 verification (${this._activeFlows.size} flows, ${this._testResults.size} results)`);
}
this.initialized = true;
} catch (error) {
console.error('Hera: Failed to initialize OAuth2VerificationEngine:', error);
this.initialized = true;
}
}
async _syncToStorage() {
try {
await this.initPromise;
const engine = {
activeFlows: Object.fromEntries(this._activeFlows.entries()),
testResults: Object.fromEntries(this._testResults.entries())
};
// CRITICAL FIX P0: Use chrome.storage.local (survives browser restart)
await chrome.storage.local.set({ oauth2VerificationEngine: engine });
} catch (error) {
console.error('Hera: Failed to sync OAuth2VerificationEngine:', error);
}
}
_debouncedSync() {
if (this._syncTimeout) clearTimeout(this._syncTimeout);
this._syncTimeout = setTimeout(() => {
this._syncToStorage().catch(err => console.error('OAuth2 verification sync failed:', err));
}, 200);
}
// Getters for backward compatibility
get activeFlows() {
return this._activeFlows;
}
get testResults() {
return this._testResults;
}
/**
* Verify CSRF protection in OAuth2 authorization flow
* @param {string} authorizationUrl - The OAuth2 authorization URL to test
* @returns {Object} Evidence package with test results
*/
async verifyCSRFProtection(authorizationUrl) {
const evidence = {
originalRequest: authorizationUrl,
stateParameter: this.extractStateParameter(authorizationUrl),
testResults: [],
timestamp: Date.now(),
flowId: this.generateFlowId()
};
try {
// Test 1: Check if state parameter exists in original request
if (!evidence.stateParameter) {
// Test 2: Attempt request without state parameter
const noStateResult = await this.testWithoutState(authorizationUrl);
evidence.testResults.push({
test: "csrf_no_state",
result: noStateResult.success ? "VULNERABLE" : "PROTECTED",
evidence: noStateResult,
severity: noStateResult.success ? "HIGH" : "SECURE"
});
} else {
// Test 3: Verify state parameter entropy
const entropyTest = this.analyzeStateEntropy(evidence.stateParameter);
evidence.testResults.push({
test: "state_entropy",
result: entropyTest.sufficient ? "SECURE" : "WEAK",
evidence: entropyTest,
severity: entropyTest.sufficient ? "SECURE" : "MEDIUM"
});
// Test 4: Attempt state replay attack
const replayTest = await this.testStateReplay(authorizationUrl);
evidence.testResults.push({
test: "state_replay",
result: replayTest.vulnerable ? "VULNERABLE" : "PROTECTED",
evidence: replayTest,
severity: replayTest.vulnerable ? "HIGH" : "SECURE"
});
// Test 5: Attempt state prediction
const predictionTest = await this.testStatePrediction(authorizationUrl);
evidence.testResults.push({
test: "state_prediction",
result: predictionTest.vulnerable ? "VULNERABLE" : "PROTECTED",
evidence: predictionTest,
severity: predictionTest.vulnerable ? "HIGH" : "SECURE"
});
}
// Store results for correlation
this.testResults.set(evidence.flowId, evidence);
// CRITICAL FIX P0: Persist to storage.session
this._debouncedSync();
return evidence;
} catch (error) {
evidence.testResults.push({
test: "error",
result: "ERROR",
evidence: { error: error.message },
severity: "UNKNOWN"
});
return evidence;
}
}
/**
* Extract state parameter from OAuth2 URL
* @param {string} url - OAuth2 authorization URL
* @returns {string|null} State parameter value
*/
extractStateParameter(url) {
try {
const urlObj = new URL(url);
return urlObj.searchParams.get('state');
} catch (error) {
return null;
}
}
/**
* Test if OAuth2 flow works without state parameter
* @param {string} authorizationUrl - Original authorization URL
* @returns {Object} Test results with evidence
*/
async testWithoutState(authorizationUrl) {
try {
const urlObj = new URL(authorizationUrl);
// Remove state parameter
urlObj.searchParams.delete('state');
const testUrl = urlObj.toString();
// Note: In a real implementation, we would make an actual request
// For now, we simulate the test based on URL analysis
const result = {
testUrl: testUrl,
originalUrl: authorizationUrl,
stateRemoved: true,
success: false, // Will be determined by actual response
evidence: {
test_performed: "Attempted authorization without state parameter",
expected_behavior: "Request should be rejected",
methodology: "Removed state parameter from authorization URL"
}
};
// In production, this would make an actual HTTP request
// and check if the authorization succeeds
result.success = await this.simulateRequestWithoutState(testUrl);
return result;
} catch (error) {
return {
success: false,
error: error.message,
evidence: { test_failed: true, reason: error.message }
};
}
}
/**
* Analyze entropy of state parameter
* @param {string} stateValue - State parameter value
* @returns {Object} Entropy analysis results
*/
analyzeStateEntropy(stateValue) {
if (!stateValue) {
return {
sufficient: false,
reason: "no_state_parameter",
evidence: { state_value: null }
};
}
// Calculate Shannon entropy
const entropy = this.calculateEntropy(stateValue);
const length = stateValue.length;
// Analysis criteria
const minLength = 16;
const minEntropy = 3.5; // bits per character
const isBase64 = /^[A-Za-z0-9+/=]+$/.test(stateValue);
const isHex = /^[0-9a-fA-F]+$/.test(stateValue);
const hasRepeatingPatterns = this.hasRepeatingPatterns(stateValue);
const analysis = {
value: stateValue,
length: length,
entropy: entropy,
entropyPerChar: entropy / length,
isBase64: isBase64,
isHex: isHex,
hasRepeatingPatterns: hasRepeatingPatterns,
sufficient: length >= minLength && (entropy / length) >= minEntropy && !hasRepeatingPatterns
};
return {
sufficient: analysis.sufficient,
analysis: analysis,
evidence: {
state_value: stateValue,
calculated_entropy: entropy,
length: length,
entropy_per_char: entropy / length,
meets_length_requirement: length >= minLength,
meets_entropy_requirement: (entropy / length) >= minEntropy,
no_repeating_patterns: !hasRepeatingPatterns
}
};
}
/**
* Calculate Shannon entropy of a string
* @param {string} str - String to analyze
* @returns {number} Shannon entropy
*/
calculateEntropy(str) {
if (!str || str.length === 0) return 0;
const freq = {};
for (const char of str) {
freq[char] = (freq[char] || 0) + 1;
}
let entropy = 0;
const len = str.length;
for (const char in freq) {
const p = freq[char] / len;
entropy -= p * Math.log2(p);
}
return entropy;
}
/**
* Check for repeating patterns in string
* @param {string} str - String to analyze
* @returns {boolean} True if repeating patterns found
*/
hasRepeatingPatterns(str) {
// Check for simple repeating patterns
if (/(.)\1{3,}/.test(str)) return true; // Same character repeated 4+ times
if (str === str[0].repeat(str.length)) return true; // All same character
// Check for repeating substrings
for (let len = 2; len <= str.length / 2; len++) {
const pattern = str.substring(0, len);
if (str === pattern.repeat(str.length / len)) {
return true;
}
}
return false;
}
/**
* Test state replay vulnerability
* @param {string} authorizationUrl - Original authorization URL
* @returns {Object} Replay test results
*/
async testStateReplay(authorizationUrl) {
try {
const state = this.extractStateParameter(authorizationUrl);
// Simulate replay attack by reusing the same state value
const replayResult = {
originalState: state,
replayAttempted: true,
vulnerable: false, // Will be determined by actual test
evidence: {
test_description: "Attempted to reuse state parameter in new authorization",
state_value: state,
expected_behavior: "Second use of state should be rejected"
}
};
// In production, this would make multiple requests with the same state
replayResult.vulnerable = await this.simulateStateReplay(authorizationUrl, state);
return replayResult;
} catch (error) {
return {
vulnerable: false,
error: error.message,
evidence: { test_failed: true, reason: error.message }
};
}
}
/**
* Test state prediction vulnerability
* @param {string} authorizationUrl - Original authorization URL
* @returns {Object} Prediction test results
*/
async testStatePrediction(authorizationUrl) {
try {
const state = this.extractStateParameter(authorizationUrl);
// Analyze if state is predictable
const predictionAnalysis = {
originalState: state,
predictable: false,
evidence: {
test_description: "Analyzed state parameter for predictability",
state_value: state,
patterns_detected: []
}
};
// Check for timestamp-based states
if (this.isTimestampBased(state)) {
predictionAnalysis.predictable = true;
predictionAnalysis.evidence.patterns_detected.push("timestamp_based");
}
// Check for incremental states
if (this.isIncremental(state)) {
predictionAnalysis.predictable = true;
predictionAnalysis.evidence.patterns_detected.push("incremental");
}
// Check for weak random generation
if (this.isWeakRandom(state)) {
predictionAnalysis.predictable = true;
predictionAnalysis.evidence.patterns_detected.push("weak_random");
}
return {
vulnerable: predictionAnalysis.predictable,
analysis: predictionAnalysis,
evidence: predictionAnalysis.evidence
};
} catch (error) {
return {
vulnerable: false,
error: error.message,
evidence: { test_failed: true, reason: error.message }
};
}
}
/**
* Verify PKCE implementation
* @param {string} authorizationUrl - OAuth2 authorization URL
* @returns {Object} PKCE verification results
*/
async verifyPKCE(authorizationUrl) {
const evidence = {
originalRequest: authorizationUrl,
codeChallenge: this.extractCodeChallenge(authorizationUrl),
codeChallengeMethod: this.extractCodeChallengeMethod(authorizationUrl),
testResults: [],
timestamp: Date.now()
};
// Test 1: Check if PKCE is implemented
if (!evidence.codeChallenge) {
evidence.testResults.push({
test: "pkce_missing",
result: "VULNERABLE",
evidence: {
description: "No code_challenge parameter found",
recommendation: "Implement PKCE for public clients"
},
severity: "HIGH"
});
} else {
// Test 2: Verify code challenge method
const methodTest = this.analyzeCodeChallengeMethod(evidence.codeChallengeMethod);
evidence.testResults.push({
test: "pkce_method",
result: methodTest.secure ? "SECURE" : "WEAK",
evidence: methodTest,
severity: methodTest.secure ? "SECURE" : "MEDIUM"
});
// Test 3: Analyze code challenge entropy
const challengeTest = this.analyzeChallengeEntropy(evidence.codeChallenge);
evidence.testResults.push({
test: "pkce_entropy",
result: challengeTest.sufficient ? "SECURE" : "WEAK",
evidence: challengeTest,
severity: challengeTest.sufficient ? "SECURE" : "MEDIUM"
});
}
return evidence;
}
/**
* Extract code_challenge parameter
* @param {string} url - OAuth2 authorization URL
* @returns {string|null} Code challenge value
*/
extractCodeChallenge(url) {
try {
const urlObj = new URL(url);
return urlObj.searchParams.get('code_challenge');
} catch (error) {
return null;
}
}
/**
* Extract code_challenge_method parameter
* @param {string} url - OAuth2 authorization URL
* @returns {string|null} Code challenge method
*/
extractCodeChallengeMethod(url) {
try {
const urlObj = new URL(url);
return urlObj.searchParams.get('code_challenge_method');
} catch (error) {
return null;
}
}
/**
* Analyze code challenge method security
* @param {string} method - Code challenge method
* @returns {Object} Method analysis
*/
analyzeCodeChallengeMethod(method) {
if (!method || method === 'plain') {
return {
secure: false,
method: method || 'none',
reason: 'Plain text method is insecure',
recommendation: 'Use S256 method instead'
};
}
if (method === 'S256') {
return {
secure: true,
method: method,
reason: 'SHA256 method is secure'
};
}
return {
secure: false,
method: method,
reason: 'Unknown or insecure method',
recommendation: 'Use S256 method'
};
}
/**
* Analyze code challenge entropy
* @param {string} challenge - Code challenge value
* @returns {Object} Challenge analysis
*/
analyzeChallengeEntropy(challenge) {
if (!challenge) {
return {
sufficient: false,
reason: 'No challenge provided'
};
}
const entropy = this.calculateEntropy(challenge);
const length = challenge.length;
const minEntropy = 128; // bits total
const actualEntropy = entropy * length;
return {
sufficient: actualEntropy >= minEntropy,
challenge: challenge,
length: length,
entropy: entropy,
totalEntropy: actualEntropy,
minimumRequired: minEntropy
};
}
// Helper methods for testing (these would make actual HTTP requests in production)
async simulateRequestWithoutState(testUrl) {
// In production, this would make an actual HTTP request
// For now, return false (secure) as default
return false;
}
async simulateStateReplay(authorizationUrl, state) {
// In production, this would test actual state reuse
return false;
}
isTimestampBased(state) {
// Check if state appears to be timestamp-based
if (state.length < 10) return false;
// Try to decode as base64 and check for timestamp patterns
try {
const decoded = atob(state);
const timestamp = parseInt(decoded);
const now = Date.now();
return timestamp > 1000000000 && timestamp < now * 2; // Reasonable timestamp range
} catch {
return false;
}
}
isIncremental(state) {
// Check if state appears to be incremental (simple numeric)
return /^\d+$/.test(state) && state.length < 20;
}
isWeakRandom(state) {
// Check for weak randomness indicators
const entropy = this.calculateEntropy(state);
return entropy < 3.0; // Low entropy indicates weak randomness
}
generateFlowId() {
return `oauth2_flow_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
/**
* Generate a comprehensive vulnerability report
* @param {Object} evidence - Test evidence
* @returns {Object} Bug bounty ready report
*/
generateVulnerabilityReport(evidence) {
const vulnerabilities = evidence.testResults.filter(test =>
test.result === 'VULNERABLE' && test.severity === 'HIGH'
);
if (vulnerabilities.length === 0) {
return null; // No high-severity vulnerabilities found
}
const report = {
title: "OAuth2 Security Vulnerabilities",
severity: "HIGH",
confidence: "CONFIRMED",
target: evidence.originalRequest,
summary: this.generateSummary(vulnerabilities),
vulnerabilities: vulnerabilities.map(vuln => ({
type: vuln.test,
description: this.getVulnerabilityDescription(vuln.test),
evidence: vuln.evidence,
impact: this.getImpact(vuln.test),
reproduction: this.getReproductionSteps(vuln.test, vuln.evidence)
})),
recommendations: this.getRecommendations(vulnerabilities),
evidence_package: {
flow_id: evidence.flowId,
timestamp: evidence.timestamp,
complete_test_results: evidence.testResults
}
};
return report;
}
generateSummary(vulnerabilities) {
const types = vulnerabilities.map(v => v.test);
if (types.includes('csrf_no_state')) {
return "OAuth2 authorization endpoint lacks CSRF protection, allowing attackers to forge authorization requests.";
}
if (types.includes('state_replay')) {
return "OAuth2 state parameter can be reused, enabling CSRF attacks.";
}
return "Multiple OAuth2 security vulnerabilities detected.";
}
getVulnerabilityDescription(testType) {
const descriptions = {
csrf_no_state: "The OAuth2 authorization endpoint accepts requests without a state parameter, making it vulnerable to CSRF attacks.",
state_replay: "The OAuth2 state parameter can be reused multiple times, defeating CSRF protection.",
state_prediction: "The OAuth2 state parameter is predictable, allowing attackers to forge valid states."
};
return descriptions[testType] || "OAuth2 security vulnerability detected.";
}
getImpact(testType) {
const impacts = {
csrf_no_state: "Attackers can trick users into authorizing malicious applications, leading to account takeover.",
state_replay: "Attackers can reuse captured state values to bypass CSRF protection.",
state_prediction: "Attackers can predict valid state values to forge authorization requests."
};
return impacts[testType] || "Security vulnerability that may lead to unauthorized access.";
}
getReproductionSteps(testType, evidence) {
if (testType === 'csrf_no_state') {
return [
"1. Create a malicious OAuth2 application",
"2. Craft authorization URL without state parameter",
"3. Social engineer victim to click malicious link",
"4. Observe successful authorization without CSRF protection",
"5. Attacker receives authorization code for victim's account"
];
}
return ["Detailed reproduction steps included in evidence package"];
}
getRecommendations(vulnerabilities) {
const recommendations = [];
const types = vulnerabilities.map(v => v.test);
if (types.includes('csrf_no_state')) {
recommendations.push("Implement mandatory state parameter validation");
recommendations.push("Generate cryptographically random state values");
recommendations.push("Validate state parameter on callback");
}
if (types.includes('state_replay')) {
recommendations.push("Implement one-time use state parameter validation");
recommendations.push("Store and invalidate used state values");
}
return recommendations;
}
}
class HSTSVerificationEngine {
constructor(evidenceCollector) {
this.evidenceCollector = evidenceCollector;
this.testResults = new Map();
}
/**
* Verify HSTS implementation with active testing
* @param {string} httpsUrl - HTTPS URL to test
* @returns {Object} Complete HSTS verification evidence
*/
async verifyHSTSImplementation(httpsUrl) {
const evidence = {
targetUrl: httpsUrl,
tests: {},
timestamp: Date.now(),
testId: this.generateTestId()
};
try {
// Test 1: Check HTTPS response headers
const httpsResponse = await this.makeRequest(httpsUrl);
evidence.tests.httpsHeaderCheck = {
hstsHeader: this.extractHSTSHeader(httpsResponse.headers),
otherSecurityHeaders: this.extractSecurityHeaders(httpsResponse.headers),
evidence: httpsResponse.headers,
status: httpsResponse.status
};
// Test 2: Attempt HTTP downgrade
const httpUrl = httpsUrl.replace('https://', 'http://');
try {
const httpResponse = await this.makeRequest(httpUrl);
evidence.tests.httpDowngradeTest = {
httpAccessible: true,
redirectsToHttps: httpResponse.status === 301 || httpResponse.status === 302,
locationHeader: this.getLocationHeader(httpResponse.headers),
evidence: httpResponse,
vulnerability: !httpResponse.redirectsToHttps && !evidence.tests.httpsHeaderCheck.hstsHeader
};
} catch (error) {
evidence.tests.httpDowngradeTest = {
httpAccessible: false,
error: error.message,
evidence: { connection_refused: true }
};
}
// Test 3: Browser HSTS behavior simulation
const hstsTest = await this.testHSTSBehavior(httpsUrl);
evidence.tests.hstsBehaviorTest = hstsTest;
return this.assessHSTSRisk(evidence);
} catch (error) {
evidence.tests.error = {
message: error.message,
failed: true
};
return evidence;
}
}
/**
* Make HTTP request (simulated for now)
* @param {string} url - URL to request
* @returns {Object} Response object
*/
async makeRequest(url) {
// In production, this would make an actual HTTP request
// For now, simulate based on URL analysis
const urlObj = new URL(url);
const isHttps = urlObj.protocol === 'https:';
if (isHttps) {
// Simulate HTTPS response
return {
status: 200,
headers: this.simulateHttpsHeaders(urlObj.hostname),
url: url
};
} else {
// Simulate HTTP response
return {
status: 301,
headers: [
{ name: 'location', value: url.replace('http://', 'https://') }
],
url: url
};
}
}
/**
* Simulate HTTPS headers for testing
* @param {string} hostname - Target hostname
* @returns {Array} Simulated headers
*/
simulateHttpsHeaders(hostname) {
const baseHeaders = [
{ name: 'content-type', value: 'text/html' },
{ name: 'cache-control', value: 'no-cache' }
];
// Simulate different HSTS policies for different domains
if (hostname.includes('bank') || hostname.includes('secure')) {
baseHeaders.push({
name: 'strict-transport-security',
value: 'max-age=31536000; includeSubDomains; preload'
});
} else if (hostname.includes('example') || hostname.includes('test')) {
// No HSTS header for test domains
} else {
// Weak HSTS for others
baseHeaders.push({
name: 'strict-transport-security',
value: 'max-age=3600'
});
}
return baseHeaders;
}
extractHSTSHeader(headers) {
const hstsHeader = headers.find(h =>
h.name.toLowerCase() === 'strict-transport-security'
);
if (!hstsHeader) {
return null;
}
const value = hstsHeader.value;
const maxAgeMatch = value.match(/max-age=(\d+)/i);
const includeSubDomains = /includeSubDomains/i.test(value);
const preload = /preload/i.test(value);
return {
value: value,
maxAge: maxAgeMatch ? parseInt(maxAgeMatch[1]) : 0,
includeSubDomains: includeSubDomains,
preload: preload,
analysis: {
strongPolicy: maxAgeMatch && parseInt(maxAgeMatch[1]) >= 31536000,
includesSubdomains: includeSubDomains,
preloadReady: preload
}
};
}
extractSecurityHeaders(headers) {
const securityHeaders = [
'content-security-policy',
'x-frame-options',
'x-content-type-options',
'referrer-policy'
];
const found = [];
for (const headerName of securityHeaders) {
const header = headers.find(h => h.name.toLowerCase() === headerName);
if (header) {
found.push({ name: header.name, value: header.value });
}
}
return found;
}
getLocationHeader(headers) {
const locationHeader = headers.find(h =>
h.name.toLowerCase() === 'location'
);
return locationHeader ? locationHeader.value : null;
}
async testHSTSBehavior(httpsUrl) {
// Simulate browser HSTS behavior testing
const urlObj = new URL(httpsUrl);
return {
test_description: "Browser HSTS behavior simulation",
domain: urlObj.hostname,
would_upgrade_http: false, // Would be true if HSTS policy exists
hsts_cached: false,
evidence: {
test_methodology: "Simulated browser HSTS cache behavior",
expected_behavior: "HTTP requests should be upgraded to HTTPS"
}
};
}
assessHSTSRisk(evidence) {
const assessment = {
evidence: evidence,
vulnerabilities: [],
riskLevel: 'LOW',
recommendations: []
};
// Check for missing HSTS
if (!evidence.tests.httpsHeaderCheck.hstsHeader) {
assessment.vulnerabilities.push({
type: 'MISSING_HSTS',
severity: 'MEDIUM',
description: 'No HSTS header present',
evidence: evidence.tests.httpsHeaderCheck.evidence
});
}
// Check for HTTP accessibility without redirect
if (evidence.tests.httpDowngradeTest.httpAccessible &&
!evidence.tests.httpDowngradeTest.redirectsToHttps) {
assessment.vulnerabilities.push({
type: 'HTTP_DOWNGRADE_POSSIBLE',
severity: 'HIGH',
description: 'HTTP version accessible without redirect',
evidence: evidence.tests.httpDowngradeTest.evidence
});
}
// Determine overall risk level
const highSeverity = assessment.vulnerabilities.filter(v => v.severity === 'HIGH');
if (highSeverity.length > 0) {
assessment.riskLevel = 'HIGH';
} else if (assessment.vulnerabilities.length > 0) {
assessment.riskLevel = 'MEDIUM';
}
return assessment;
}
generateTestId() {
return `hsts_test_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
}
export { OAuth2VerificationEngine, HSTSVerificationEngine };