micro-models-agent 0.7.9 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/commands.js +173 -0
- package/dist/cli/completer.js +168 -0
- package/dist/cli/index.js +2 -0
- package/dist/cli/main.js +95 -0
- package/dist/cli/repl.js +762 -0
- package/dist/cli/security-commands.js +166 -0
- package/dist/cli/setup.js +214 -0
- package/dist/config/config.js +123 -0
- package/dist/config/defaults.js +91 -0
- package/dist/config/experts.js +15 -0
- package/dist/config/index.js +3 -0
- package/dist/config/security.js +187 -0
- package/dist/config/types.js +1 -0
- package/dist/core/agent.js +626 -0
- package/dist/core/bootstrap.js +307 -0
- package/dist/core/index.js +2 -0
- package/dist/core/prompt-builder.js +55 -0
- package/dist/core/types.js +1 -0
- package/dist/i18n/en.json +405 -0
- package/dist/i18n/index.js +43 -0
- package/dist/i18n/ru.json +405 -0
- package/dist/index.js +22 -0
- package/dist/llm/index.js +4 -0
- package/dist/llm/model-loader.js +78 -0
- package/dist/llm/openai-compat.js +277 -0
- package/dist/llm/orchestrator.js +194 -0
- package/dist/llm/provider.js +2 -0
- package/dist/llm/response.js +39 -0
- package/dist/llm/token-counter.js +37 -0
- package/dist/llm/types.js +1 -0
- package/dist/logger/app-logger.js +76 -0
- package/dist/logger/index.js +1 -0
- package/dist/migration/backup.js +45 -0
- package/dist/migration/detect.js +50 -0
- package/dist/migration/index.js +2 -0
- package/dist/modules/browser/actions.js +46 -0
- package/dist/modules/browser/cookie-store.js +24 -0
- package/dist/modules/browser/index.js +5 -0
- package/dist/modules/browser/module.js +28 -0
- package/dist/modules/browser/session.js +287 -0
- package/dist/modules/browser/snapshot.js +114 -0
- package/dist/modules/browser/types.js +9 -0
- package/dist/modules/context/history.js +15 -0
- package/dist/modules/context/index.js +1 -0
- package/dist/modules/context/manager.js +179 -0
- package/dist/modules/execution/auditor.js +72 -0
- package/dist/modules/execution/index.js +6 -0
- package/dist/modules/execution/module.js +334 -0
- package/dist/modules/execution/moe-executor.js +196 -0
- package/dist/modules/execution/plan-validator.js +153 -0
- package/dist/modules/execution/planner.js +35 -0
- package/dist/modules/execution/stuck-detector.js +113 -0
- package/dist/modules/execution/tracker.js +53 -0
- package/dist/modules/execution/types.js +1 -0
- package/dist/modules/execution/verifier.js +149 -0
- package/dist/modules/hallucination/confidence.js +47 -0
- package/dist/modules/hallucination/consistency.js +32 -0
- package/dist/modules/hallucination/detector.js +41 -0
- package/dist/modules/hallucination/factual.js +128 -0
- package/dist/modules/hallucination/index.js +4 -0
- package/dist/modules/index.js +5 -0
- package/dist/modules/indexer/cache.js +38 -0
- package/dist/modules/indexer/index.js +3 -0
- package/dist/modules/indexer/module.js +192 -0
- package/dist/modules/indexer/walker.js +101 -0
- package/dist/modules/mcp/client.js +393 -0
- package/dist/modules/mcp/index.js +3 -0
- package/dist/modules/mcp/module.js +146 -0
- package/dist/modules/mcp/registry.js +15 -0
- package/dist/modules/memory/index.js +1 -0
- package/dist/modules/memory/search.js +26 -0
- package/dist/modules/memory/store.js +38 -0
- package/dist/modules/pipelines/engine.js +60 -0
- package/dist/modules/pipelines/index.js +3 -0
- package/dist/modules/pipelines/parser.js +53 -0
- package/dist/modules/pipelines/template.js +14 -0
- package/dist/modules/plugins/builtin/lint-on-write.js +121 -0
- package/dist/modules/plugins/builtin/notify.js +8 -0
- package/dist/modules/plugins/index.js +1 -0
- package/dist/modules/plugins/loader.js +28 -0
- package/dist/modules/plugins/manager.js +161 -0
- package/dist/modules/plugins/types.js +1 -0
- package/dist/modules/registry.js +45 -0
- package/dist/modules/security/audit-log.js +108 -0
- package/dist/modules/security/audit-notifier.js +292 -0
- package/dist/modules/security/command-validator.js +91 -0
- package/dist/modules/security/content-scanner.js +52 -0
- package/dist/modules/security/data-sanitizer.js +97 -0
- package/dist/modules/security/encryption.js +218 -0
- package/dist/modules/security/index.js +14 -0
- package/dist/modules/security/network-validator.js +79 -0
- package/dist/modules/security/path-validator.js +155 -0
- package/dist/modules/security/rate-limiter.js +119 -0
- package/dist/modules/security/security-policies.js +393 -0
- package/dist/modules/security/session-encryption.js +193 -0
- package/dist/modules/security/session-isolation.js +95 -0
- package/dist/modules/session/index.js +3 -0
- package/dist/modules/session/manager.js +167 -0
- package/dist/modules/session/module.js +28 -0
- package/dist/modules/session/store.js +174 -0
- package/dist/modules/session/types.js +1 -0
- package/dist/modules/skills/index.js +3 -0
- package/dist/modules/skills/loader.js +72 -0
- package/dist/modules/skills/matcher.js +27 -0
- package/dist/modules/skills/module.js +180 -0
- package/dist/modules/types.js +1 -0
- package/dist/modules/updater/checker.js +32 -0
- package/dist/modules/updater/index.js +1 -0
- package/dist/modules/user-profile/compressor.js +16 -0
- package/dist/modules/user-profile/index.js +1 -0
- package/dist/modules/user-profile/profile.js +68 -0
- package/dist/tools/approve.js +32 -0
- package/dist/tools/bash.js +77 -0
- package/dist/tools/browser.js +97 -0
- package/dist/tools/create-dir.js +57 -0
- package/dist/tools/delete-file.js +64 -0
- package/dist/tools/edit-file.js +78 -0
- package/dist/tools/executor.js +83 -0
- package/dist/tools/file-info.js +46 -0
- package/dist/tools/filter-tools.js +10 -0
- package/dist/tools/glob-tool.js +19 -0
- package/dist/tools/grep-tool.js +51 -0
- package/dist/tools/index.js +44 -0
- package/dist/tools/list-dir.js +40 -0
- package/dist/tools/load-skill.js +48 -0
- package/dist/tools/mcp-call.js +68 -0
- package/dist/tools/move-file.js +84 -0
- package/dist/tools/pipeline-run.js +39 -0
- package/dist/tools/question.js +142 -0
- package/dist/tools/read-file.js +65 -0
- package/dist/tools/registry.js +36 -0
- package/dist/tools/scope-check.js +30 -0
- package/dist/tools/search-history.js +64 -0
- package/dist/tools/subagent.js +130 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/user-input.js +123 -0
- package/dist/tools/web-browse.js +51 -0
- package/dist/tools/web-fetch.js +62 -0
- package/dist/tools/web-search.js +59 -0
- package/dist/tools/write-file.js +80 -0
- package/dist/ui/box.js +81 -0
- package/dist/ui/colors.js +4 -0
- package/dist/ui/diff.js +185 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/md-formatter.js +212 -0
- package/dist/ui/output.js +13 -0
- package/dist/ui/renderer.js +141 -0
- package/dist/ui/spinner.js +70 -0
- package/dist/ui/table.js +144 -0
- package/package.json +1 -1
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { writeFileSync, appendFileSync, existsSync, mkdirSync } from 'fs';
|
|
2
|
+
import { join, dirname } from 'path';
|
|
3
|
+
import { homedir } from 'os';
|
|
4
|
+
/**
|
|
5
|
+
* Severity weights for comparison
|
|
6
|
+
*/
|
|
7
|
+
const SEVERITY_WEIGHTS = {
|
|
8
|
+
low: 1,
|
|
9
|
+
medium: 2,
|
|
10
|
+
high: 3,
|
|
11
|
+
critical: 4,
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Default audit notifier configuration
|
|
15
|
+
*/
|
|
16
|
+
export const DEFAULT_AUDIT_NOTIFIER_CONFIG = {
|
|
17
|
+
enabled: false,
|
|
18
|
+
filePath: join(homedir(), '.mma', 'logs', 'audit-notifications.jsonl'),
|
|
19
|
+
webhookTimeout: 5000,
|
|
20
|
+
minSeverity: 'medium',
|
|
21
|
+
eventTypes: [
|
|
22
|
+
'security_block',
|
|
23
|
+
'bash_command',
|
|
24
|
+
'file_operation',
|
|
25
|
+
'network_request',
|
|
26
|
+
'authentication',
|
|
27
|
+
'configuration_change',
|
|
28
|
+
],
|
|
29
|
+
maxRetries: 3,
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Audit notifier for sending notifications about security events
|
|
33
|
+
*/
|
|
34
|
+
export class AuditNotifier {
|
|
35
|
+
config;
|
|
36
|
+
retryQueue = [];
|
|
37
|
+
isProcessing = false;
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.config = { ...DEFAULT_AUDIT_NOTIFIER_CONFIG, ...config };
|
|
40
|
+
this.ensureLogDirectory();
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Check if notifier is enabled
|
|
44
|
+
*/
|
|
45
|
+
isEnabled() {
|
|
46
|
+
return this.config.enabled;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Enable the notifier
|
|
50
|
+
*/
|
|
51
|
+
enable() {
|
|
52
|
+
this.config.enabled = true;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Disable the notifier
|
|
56
|
+
*/
|
|
57
|
+
disable() {
|
|
58
|
+
this.config.enabled = false;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Update configuration
|
|
62
|
+
*/
|
|
63
|
+
updateConfig(config) {
|
|
64
|
+
this.config = { ...this.config, ...config };
|
|
65
|
+
this.ensureLogDirectory();
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Ensure the log directory exists
|
|
69
|
+
*/
|
|
70
|
+
ensureLogDirectory() {
|
|
71
|
+
if (this.config.filePath) {
|
|
72
|
+
const dir = dirname(this.config.filePath);
|
|
73
|
+
mkdirSync(dir, { recursive: true });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if an event type should be notified
|
|
78
|
+
*/
|
|
79
|
+
shouldNotify(eventType) {
|
|
80
|
+
if (!this.config.enabled)
|
|
81
|
+
return false;
|
|
82
|
+
return this.config.eventTypes?.includes(eventType) ?? false;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Check if a severity level should be notified
|
|
86
|
+
*/
|
|
87
|
+
shouldNotifySeverity(severity) {
|
|
88
|
+
const minSeverity = this.config.minSeverity ?? 'medium';
|
|
89
|
+
const minWeight = SEVERITY_WEIGHTS[minSeverity];
|
|
90
|
+
const eventWeight = SEVERITY_WEIGHTS[severity];
|
|
91
|
+
return eventWeight >= minWeight;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Send a notification for an audit event
|
|
95
|
+
*/
|
|
96
|
+
async notify(entry) {
|
|
97
|
+
if (!this.config.enabled)
|
|
98
|
+
return null;
|
|
99
|
+
const eventType = this.mapEventType(entry.action);
|
|
100
|
+
if (!eventType || !this.shouldNotify(eventType))
|
|
101
|
+
return null;
|
|
102
|
+
const severity = this.determineSeverity(entry);
|
|
103
|
+
if (!this.shouldNotifySeverity(severity))
|
|
104
|
+
return null;
|
|
105
|
+
const notification = {
|
|
106
|
+
timestamp: new Date().toISOString(),
|
|
107
|
+
eventType,
|
|
108
|
+
severity,
|
|
109
|
+
message: entry.details || entry.reason || entry.action,
|
|
110
|
+
details: entry.details ? { details: entry.details } : undefined,
|
|
111
|
+
sessionId: entry.sessionId,
|
|
112
|
+
};
|
|
113
|
+
// Write to file
|
|
114
|
+
this.writeToFile(notification);
|
|
115
|
+
// Send to webhook if configured
|
|
116
|
+
if (this.config.webhookUrl) {
|
|
117
|
+
await this.sendToWebhook(notification);
|
|
118
|
+
}
|
|
119
|
+
return notification;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Map audit log entry action to notification event type
|
|
123
|
+
*/
|
|
124
|
+
mapEventType(action) {
|
|
125
|
+
const typeMap = {
|
|
126
|
+
'tool_call': 'tool_call',
|
|
127
|
+
'bash_command': 'bash_command',
|
|
128
|
+
'file_write': 'file_operation',
|
|
129
|
+
'file_delete': 'file_operation',
|
|
130
|
+
'network_request': 'network_request',
|
|
131
|
+
'security_block': 'security_block',
|
|
132
|
+
};
|
|
133
|
+
return typeMap[action] || null;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Determine severity from audit log entry
|
|
137
|
+
*/
|
|
138
|
+
determineSeverity(entry) {
|
|
139
|
+
// Default severity based on action type
|
|
140
|
+
const typeSeverity = {
|
|
141
|
+
'security_block': 'high',
|
|
142
|
+
'bash_command': 'high',
|
|
143
|
+
'file_delete': 'medium',
|
|
144
|
+
'file_write': 'medium',
|
|
145
|
+
'network_request': 'medium',
|
|
146
|
+
'tool_call': 'low',
|
|
147
|
+
'session_start': 'low',
|
|
148
|
+
'session_end': 'low',
|
|
149
|
+
};
|
|
150
|
+
return typeSeverity[entry.action] || 'low';
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Write notification to file
|
|
154
|
+
*/
|
|
155
|
+
writeToFile(notification) {
|
|
156
|
+
if (!this.config.filePath)
|
|
157
|
+
return;
|
|
158
|
+
try {
|
|
159
|
+
const line = JSON.stringify(notification);
|
|
160
|
+
appendFileSync(this.config.filePath, line + '\n', 'utf8');
|
|
161
|
+
}
|
|
162
|
+
catch (error) {
|
|
163
|
+
console.error('[AuditNotifier] Failed to write to file:', error);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Send notification to webhook
|
|
168
|
+
*/
|
|
169
|
+
async sendToWebhook(notification) {
|
|
170
|
+
if (!this.config.webhookUrl)
|
|
171
|
+
return;
|
|
172
|
+
try {
|
|
173
|
+
const controller = new AbortController();
|
|
174
|
+
const timeoutId = setTimeout(() => controller.abort(), this.config.webhookTimeout);
|
|
175
|
+
const response = await fetch(this.config.webhookUrl, {
|
|
176
|
+
method: 'POST',
|
|
177
|
+
headers: { 'Content-Type': 'application/json' },
|
|
178
|
+
body: JSON.stringify(notification),
|
|
179
|
+
signal: controller.signal,
|
|
180
|
+
});
|
|
181
|
+
clearTimeout(timeoutId);
|
|
182
|
+
if (!response.ok) {
|
|
183
|
+
throw new Error(`Webhook returned ${response.status}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
catch (error) {
|
|
187
|
+
console.error('[AuditNotifier] Webhook failed, adding to retry queue:', error);
|
|
188
|
+
this.addToRetryQueue(notification);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Add notification to retry queue
|
|
193
|
+
*/
|
|
194
|
+
addToRetryQueue(notification) {
|
|
195
|
+
this.retryQueue.push({ entry: notification, retries: 0 });
|
|
196
|
+
if (!this.isProcessing) {
|
|
197
|
+
this.processRetryQueue();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Process the retry queue
|
|
202
|
+
*/
|
|
203
|
+
async processRetryQueue() {
|
|
204
|
+
if (this.retryQueue.length === 0 || this.isProcessing)
|
|
205
|
+
return;
|
|
206
|
+
this.isProcessing = true;
|
|
207
|
+
while (this.retryQueue.length > 0) {
|
|
208
|
+
const item = this.retryQueue[0];
|
|
209
|
+
if (item.retries >= (this.config.maxRetries ?? 3)) {
|
|
210
|
+
console.error('[AuditNotifier] Max retries exceeded for notification:', item.entry);
|
|
211
|
+
this.retryQueue.shift();
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
await this.sendToWebhook(item.entry);
|
|
216
|
+
this.retryQueue.shift();
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
item.retries++;
|
|
220
|
+
// Exponential backoff
|
|
221
|
+
const delay = Math.pow(2, item.retries) * 1000;
|
|
222
|
+
await new Promise(resolve => setTimeout(resolve, delay));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
this.isProcessing = false;
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Read notifications from file
|
|
229
|
+
*/
|
|
230
|
+
readNotifications(limit = 100) {
|
|
231
|
+
if (!this.config.filePath || !existsSync(this.config.filePath)) {
|
|
232
|
+
return [];
|
|
233
|
+
}
|
|
234
|
+
try {
|
|
235
|
+
const content = readFileSync(this.config.filePath, 'utf8');
|
|
236
|
+
const lines = content.split('\n').filter(Boolean);
|
|
237
|
+
return lines.slice(-limit).map(line => JSON.parse(line));
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Clear all notifications
|
|
245
|
+
*/
|
|
246
|
+
clearNotifications() {
|
|
247
|
+
if (this.config.filePath) {
|
|
248
|
+
writeFileSync(this.config.filePath, '', 'utf8');
|
|
249
|
+
}
|
|
250
|
+
this.retryQueue = [];
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Get notification statistics
|
|
254
|
+
*/
|
|
255
|
+
getStats() {
|
|
256
|
+
const notifications = this.readNotifications();
|
|
257
|
+
const bySeverity = { low: 0, medium: 0, high: 0, critical: 0 };
|
|
258
|
+
const byType = {
|
|
259
|
+
tool_call: 0,
|
|
260
|
+
tool_result: 0,
|
|
261
|
+
bash_command: 0,
|
|
262
|
+
file_operation: 0,
|
|
263
|
+
network_request: 0,
|
|
264
|
+
security_block: 0,
|
|
265
|
+
authentication: 0,
|
|
266
|
+
configuration_change: 0,
|
|
267
|
+
session_start: 0,
|
|
268
|
+
session_end: 0,
|
|
269
|
+
};
|
|
270
|
+
for (const n of notifications) {
|
|
271
|
+
bySeverity[n.severity] = (bySeverity[n.severity] || 0) + 1;
|
|
272
|
+
byType[n.eventType] = (byType[n.eventType] || 0) + 1;
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
total: notifications.length,
|
|
276
|
+
bySeverity,
|
|
277
|
+
byType,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Global audit notifier instance
|
|
283
|
+
*/
|
|
284
|
+
export const globalAuditNotifier = new AuditNotifier();
|
|
285
|
+
/**
|
|
286
|
+
* Create an audit notifier with custom configuration
|
|
287
|
+
*/
|
|
288
|
+
export function createAuditNotifier(config) {
|
|
289
|
+
return new AuditNotifier(config);
|
|
290
|
+
}
|
|
291
|
+
// Import readFileSync for readNotifications
|
|
292
|
+
import { readFileSync } from 'fs';
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if a command is allowed based on security configuration
|
|
3
|
+
*/
|
|
4
|
+
export function isCommandAllowed(command, securityConfig) {
|
|
5
|
+
// If no security config, allow everything (backward compatibility)
|
|
6
|
+
if (!securityConfig) {
|
|
7
|
+
return { allowed: true, sanitizedCommand: command };
|
|
8
|
+
}
|
|
9
|
+
const trimmedCommand = command.trim();
|
|
10
|
+
if (!trimmedCommand) {
|
|
11
|
+
return { allowed: false, reason: "Empty command" };
|
|
12
|
+
}
|
|
13
|
+
// Extract base command (first word)
|
|
14
|
+
const baseCommand = trimmedCommand.split(/\s+/)[0];
|
|
15
|
+
// Check whitelist first (if non-empty, only whitelisted commands are allowed)
|
|
16
|
+
if (securityConfig.whitelist.length > 0) {
|
|
17
|
+
if (!securityConfig.whitelist.includes(baseCommand)) {
|
|
18
|
+
return {
|
|
19
|
+
allowed: false,
|
|
20
|
+
reason: `Command "${baseCommand}" is not in the whitelist`,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// Check blacklist
|
|
25
|
+
if (securityConfig.blacklist.includes(baseCommand)) {
|
|
26
|
+
return {
|
|
27
|
+
allowed: false,
|
|
28
|
+
reason: `Command "${baseCommand}" is blacklisted`,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// Check for dangerous operators
|
|
32
|
+
if (securityConfig.blockDangerousFlags) {
|
|
33
|
+
for (const op of securityConfig.dangerousOperators || []) {
|
|
34
|
+
if (command.includes(op)) {
|
|
35
|
+
return {
|
|
36
|
+
allowed: false,
|
|
37
|
+
reason: `Operator "${op}" is not allowed`,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
// Check for dangerous flags
|
|
43
|
+
if (securityConfig.blockDangerousFlags) {
|
|
44
|
+
for (const flag of securityConfig.dangerousFlags || []) {
|
|
45
|
+
// Check for flag as whole word or with equals
|
|
46
|
+
const flagPattern = new RegExp(`(?:^|\\s)${flag}(?:\\s|$|=)`);
|
|
47
|
+
if (flagPattern.test(command)) {
|
|
48
|
+
return {
|
|
49
|
+
allowed: false,
|
|
50
|
+
reason: `Dangerous flag "${flag}" is not allowed`,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
// Command is allowed
|
|
56
|
+
return { allowed: true, sanitizedCommand: command };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sanitize command for logging (remove sensitive information)
|
|
60
|
+
* Uses the same patterns as sanitizeLogMessage for consistency
|
|
61
|
+
*/
|
|
62
|
+
export function sanitizeCommandForLog(command) {
|
|
63
|
+
// Import patterns from data-sanitizer to maintain consistency
|
|
64
|
+
const patterns = [
|
|
65
|
+
/(api[_-]?key|apikey|api_key)\s*[=:\s]\s*["']?([a-zA-Z0-9_-]{10,})["']?/gi,
|
|
66
|
+
/(token|access[_-]?token|auth[_-]?token)\s*[=:\s]\s*["']?([a-zA-Z0-9_-]{10,})["']?/gi,
|
|
67
|
+
/(password|passwd|pwd|pass)\s*[=:\s]\s*["']?([^\s"'&]+)["']?/gi,
|
|
68
|
+
/(secret|private[_-]?key|secret[_-]?key)\s*[=:\s]\s*["']?([^\s"'&]+)["']?/gi,
|
|
69
|
+
/\bsk-[a-zA-Z0-9_-]{20,}\b/gi,
|
|
70
|
+
/\bctx7sk-[a-zA-Z0-9_-]{10,}\b/gi,
|
|
71
|
+
/\bghp_[a-zA-Z0-9]{30,}\b/gi,
|
|
72
|
+
/\bgithub_pat_[a-zA-Z0-9]{20,}\b/gi,
|
|
73
|
+
/\bAKIA[0-9A-Z]{15,}\b/gi,
|
|
74
|
+
/Bearer\s+[a-zA-Z0-9_-]{20,}/gi,
|
|
75
|
+
/\b([a-zA-Z0-9_-]{30,})\b/g,
|
|
76
|
+
];
|
|
77
|
+
let sanitized = command;
|
|
78
|
+
for (const pattern of patterns) {
|
|
79
|
+
if (pattern.toString().includes('($1')) {
|
|
80
|
+
// For patterns with capture groups, use the group in replacement
|
|
81
|
+
sanitized = sanitized.replace(pattern, (match, p1) => {
|
|
82
|
+
return `${p1}=[REDACTED]`;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// For simple patterns, just replace with [REDACTED]
|
|
87
|
+
sanitized = sanitized.replace(pattern, '[REDACTED]');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return sanitized;
|
|
91
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default dangerous patterns to scan for in file content
|
|
3
|
+
*/
|
|
4
|
+
const DEFAULT_DANGEROUS_PATTERNS = [
|
|
5
|
+
/eval\(/gi,
|
|
6
|
+
/new Function\(/gi,
|
|
7
|
+
/child_process\.exec\(/gi,
|
|
8
|
+
/child_process\.spawn\(/gi,
|
|
9
|
+
/require\('child_process'\)/gi,
|
|
10
|
+
/require\('http'\).createServer/gi,
|
|
11
|
+
/require\('net'\).createServer/gi,
|
|
12
|
+
/require\('fs'\).writeFileSync/gi,
|
|
13
|
+
/require\('fs'\).unlinkSync/gi,
|
|
14
|
+
/process\.exit\(/gi,
|
|
15
|
+
/setInterval\(/gi,
|
|
16
|
+
/setTimeout\(/gi,
|
|
17
|
+
/while\(true\)/gi,
|
|
18
|
+
/for\(;;\)/gi,
|
|
19
|
+
/rm -rf/gi,
|
|
20
|
+
/dd if=/gi,
|
|
21
|
+
/chmod 777/gi,
|
|
22
|
+
/wget.*\|.*sh/gi,
|
|
23
|
+
/curl.*\|.*sh/gi,
|
|
24
|
+
/\$\{/gi,
|
|
25
|
+
/`[^`]+`/gi,
|
|
26
|
+
];
|
|
27
|
+
/**
|
|
28
|
+
* Scan file content for dangerous patterns
|
|
29
|
+
*/
|
|
30
|
+
export function scanContent(content, filePath, config) {
|
|
31
|
+
// If scanning is disabled, allow everything
|
|
32
|
+
if (!config?.enabled) {
|
|
33
|
+
return { allowed: true };
|
|
34
|
+
}
|
|
35
|
+
// Use default patterns if none provided
|
|
36
|
+
const patterns = config.dangerousPatterns?.length ? config.dangerousPatterns : DEFAULT_DANGEROUS_PATTERNS;
|
|
37
|
+
const matches = [];
|
|
38
|
+
for (const pattern of patterns) {
|
|
39
|
+
const match = content.match(pattern);
|
|
40
|
+
if (match) {
|
|
41
|
+
matches.push(...match);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (matches.length > 0) {
|
|
45
|
+
return {
|
|
46
|
+
allowed: false,
|
|
47
|
+
reason: `Content contains dangerous patterns: ${matches.slice(0, 3).join(", ")}`,
|
|
48
|
+
matches,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return { allowed: true };
|
|
52
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Patterns for sensitive data that should be redacted from logs
|
|
3
|
+
*/
|
|
4
|
+
const SENSITIVE_PATTERNS = [
|
|
5
|
+
// API keys and tokens in key=value format
|
|
6
|
+
{
|
|
7
|
+
pattern: /(api[_-]?key|apikey|api_key)\s*[=:\s]\s*["']?([a-zA-Z0-9_-]{10,})["']?/gi,
|
|
8
|
+
replacement: "$1=[REDACTED]",
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
pattern: /(token|access[_-]?token|auth[_-]?token)\s*[=:\s]\s*["']?([a-zA-Z0-9_-]{10,})["']?/gi,
|
|
12
|
+
replacement: "$1=[REDACTED]",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
pattern: /(password|passwd|pwd|pass)\s*[=:\s]\s*["']?([^\s"'&]+)["']?/gi,
|
|
16
|
+
replacement: "$1=[REDACTED]",
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
pattern: /(secret|private[_-]?key|secret[_-]?key)\s*[=:\s]\s*["']?([^\s"'&]+)["']?/gi,
|
|
20
|
+
replacement: "$1=[REDACTED]",
|
|
21
|
+
},
|
|
22
|
+
// Standalone API keys (not in key=value format)
|
|
23
|
+
{
|
|
24
|
+
pattern: /\bsk-[a-zA-Z0-9_-]{20,}\b/gi,
|
|
25
|
+
replacement: "sk-[REDACTED]",
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
pattern: /\bctx7sk-[a-zA-Z0-9_-]{10,}\b/gi,
|
|
29
|
+
replacement: "ctx7sk-[REDACTED]",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
pattern: /\bghp_[a-zA-Z0-9]{30,}\b/gi,
|
|
33
|
+
replacement: "ghp_[REDACTED]",
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
pattern: /\bgithub_pat_[a-zA-Z0-9]{20,}\b/gi,
|
|
37
|
+
replacement: "github_pat_[REDACTED]",
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
pattern: /\bAKIA[0-9A-Z]{15,}\b/gi,
|
|
41
|
+
replacement: "AKIA[REDACTED]",
|
|
42
|
+
},
|
|
43
|
+
// Bearer tokens in headers
|
|
44
|
+
{
|
|
45
|
+
pattern: /Bearer\s+[a-zA-Z0-9_-]{20,}/gi,
|
|
46
|
+
replacement: "Bearer [REDACTED]",
|
|
47
|
+
},
|
|
48
|
+
// Generic high-entropy strings that look like secrets (30+ chars, mixed case and numbers)
|
|
49
|
+
{
|
|
50
|
+
pattern: /\b([a-zA-Z0-9_-]{30,})\b/g,
|
|
51
|
+
replacement: "[REDACTED]",
|
|
52
|
+
},
|
|
53
|
+
];
|
|
54
|
+
/**
|
|
55
|
+
* Sanitize a log message by removing sensitive information
|
|
56
|
+
*/
|
|
57
|
+
export function sanitizeLogMessage(message) {
|
|
58
|
+
let sanitized = message;
|
|
59
|
+
for (const { pattern, replacement } of SENSITIVE_PATTERNS) {
|
|
60
|
+
sanitized = sanitized.replace(pattern, replacement);
|
|
61
|
+
}
|
|
62
|
+
return sanitized;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Sanitize a command string by removing sensitive information
|
|
66
|
+
*/
|
|
67
|
+
export function sanitizeCommand(command) {
|
|
68
|
+
return sanitizeLogMessage(command);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Sanitize a URL by removing sensitive query parameters
|
|
72
|
+
*/
|
|
73
|
+
export function sanitizeUrl(url) {
|
|
74
|
+
try {
|
|
75
|
+
const urlObj = new URL(url);
|
|
76
|
+
const sensitiveKeys = [
|
|
77
|
+
"api_key",
|
|
78
|
+
"token",
|
|
79
|
+
"password",
|
|
80
|
+
"secret",
|
|
81
|
+
"access_token",
|
|
82
|
+
"auth",
|
|
83
|
+
"key",
|
|
84
|
+
];
|
|
85
|
+
const sanitizedParams = new URLSearchParams(urlObj.search);
|
|
86
|
+
for (const key of sensitiveKeys) {
|
|
87
|
+
if (sanitizedParams.has(key)) {
|
|
88
|
+
sanitizedParams.set(key, "[REDACTED]");
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
urlObj.search = sanitizedParams.toString();
|
|
92
|
+
return urlObj.toString();
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return sanitizeLogMessage(url);
|
|
96
|
+
}
|
|
97
|
+
}
|