-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-modular-reference.js
More file actions
163 lines (135 loc) · 4.94 KB
/
background-modular-reference.js
File metadata and controls
163 lines (135 loc) · 4.94 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
// Hera Background Service Worker - Modular Architecture
// Main coordinator that orchestrates all modules
// Import core modules
import { SecurityValidation } from './modules/security-validation.js';
import { storageManager } from './modules/storage-manager.js';
import { memoryManager } from './modules/memory-manager.js';
import { RequestProcessor } from './modules/request-processor.js';
import { MessageHandler } from './modules/message-handler.js';
// Import analysis engines
import { HeraAuthProtocolDetector } from './hera-auth-detector.js';
import { HeraSecretScanner } from './hera-secret-scanner.js';
import { HeraMaliciousExtensionDetector } from './hera-extension-security.js';
import { HeraAuthSecurityAnalyzer } from './hera-auth-security-analyzer.js';
import { HeraPortAuthAnalyzer } from './hera-port-auth-analyzer.js';
import { EvidenceCollector } from './evidence-collector.js';
import { AlertManager } from './alert-manager.js';
// --- Initialization ---
// Initialize evidence collection and analysis engines
const evidenceCollector = new EvidenceCollector();
const alertManager = new AlertManager();
const heraAuthDetector = new HeraAuthProtocolDetector(evidenceCollector);
const heraSecretScanner = new HeraSecretScanner();
const heraExtensionDetector = new HeraMaliciousExtensionDetector();
const heraAuthSecurityAnalyzer = new HeraAuthSecurityAnalyzer();
const heraPortAuthAnalyzer = new HeraPortAuthAnalyzer();
// Add wrapper methods for HeraAuthProtocolDetector
heraAuthDetector.isAuthRequest = function(url, options) {
const authPatterns = [
'/oauth', '/authorize', '/token', '/login', '/signin', '/auth',
'/api/auth', '/session', '/connect', '/saml', '/oidc', '/scim'
];
const urlLower = url.toLowerCase();
return authPatterns.some(pattern => urlLower.includes(pattern));
};
heraAuthDetector.analyze = function(url, method, headers, body) {
return this.analyzeRequest({
url: url,
method: method,
headers: headers,
body: body
});
};
// Initialize processors
const requestProcessor = new RequestProcessor(
heraAuthDetector,
heraSecretScanner,
heraPortAuthAnalyzer,
alertManager
);
const messageHandler = new MessageHandler(requestProcessor, null);
// --- Alarm Setup (Periodic Tasks) ---
// Setup periodic cleanup and monitoring
chrome.alarms.create('cleanupAuthRequests', { periodInMinutes: 2 });
chrome.alarms.create('checkStorageQuota', { periodInMinutes: 10 });
chrome.alarms.onAlarm.addListener((alarm) => {
if (alarm.name === 'cleanupAuthRequests') {
memoryManager.cleanupStaleRequests();
alertManager.cleanupAlertHistory();
} else if (alarm.name === 'checkStorageQuota') {
storageManager.checkStorageQuota();
}
});
// --- WebRequest Listeners ---
// 1. Capture request details
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
requestProcessor.handleBeforeRequest(details);
},
{ urls: ["<all_urls>"] },
["requestBody"]
);
// 2. Capture request headers and perform initial analysis
chrome.webRequest.onBeforeSendHeaders.addListener(
(details) => {
requestProcessor.handleBeforeSendHeaders(details);
},
{ urls: ["<all_urls>"] },
["requestHeaders"]
);
// 3. Capture response headers and status
chrome.webRequest.onHeadersReceived.addListener(
(details) => {
requestProcessor.handleHeadersReceived(details);
},
{ urls: ["<all_urls>"] },
["responseHeaders"]
);
// --- Message Listener ---
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
messageHandler.handleMessage(message, sender, sendResponse);
return true; // Keep channel open for async responses
});
// --- Extension Lifecycle ---
chrome.runtime.onInstalled.addListener(() => {
console.log('Hera extension installed/updated (modular architecture).');
storageManager.updateBadge();
});
chrome.runtime.onStartup.addListener(() => {
console.log('Hera starting up (modular architecture)...');
storageManager.updateBadge();
});
// --- Utility Functions for Alert Display ---
function showAuthSecurityAlert(finding, url) {
try {
const enrichedFinding = {
...finding,
url: url,
evidence: finding.evidence || {}
};
alertManager.processFinding(enrichedFinding);
} catch (error) {
console.error('Failed to show auth security alert:', error);
}
}
function showExtensionSecurityAlert(finding) {
try {
const enrichedFinding = {
...finding,
severity: 'CRITICAL',
url: 'chrome://extensions/',
evidence: {
verification: finding.details?.extensionId
? `chrome://extensions/?id=${finding.details.extensionId}`
: null
}
};
alertManager.processFinding(enrichedFinding);
} catch (error) {
console.error('Failed to show extension security alert:', error);
}
}
// Export for debugger manager (if needed)
globalThis.showAuthSecurityAlert = showAuthSecurityAlert;
globalThis.showExtensionSecurityAlert = showExtensionSecurityAlert;
console.log('✅ Hera modular background service worker initialized');