@sparkleideas/shared 3.0.0-alpha.7
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/README.md +323 -0
- package/__tests__/hooks/bash-safety.test.ts +289 -0
- package/__tests__/hooks/file-organization.test.ts +335 -0
- package/__tests__/hooks/git-commit.test.ts +336 -0
- package/__tests__/hooks/index.ts +23 -0
- package/__tests__/hooks/session-hooks.test.ts +357 -0
- package/__tests__/hooks/task-hooks.test.ts +193 -0
- package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
- package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
- package/docs/EVENTS_README.md +352 -0
- package/package.json +39 -0
- package/src/core/config/defaults.ts +207 -0
- package/src/core/config/index.ts +15 -0
- package/src/core/config/loader.ts +271 -0
- package/src/core/config/schema.ts +188 -0
- package/src/core/config/validator.ts +209 -0
- package/src/core/event-bus.ts +236 -0
- package/src/core/index.ts +22 -0
- package/src/core/interfaces/agent.interface.ts +251 -0
- package/src/core/interfaces/coordinator.interface.ts +363 -0
- package/src/core/interfaces/event.interface.ts +267 -0
- package/src/core/interfaces/index.ts +19 -0
- package/src/core/interfaces/memory.interface.ts +332 -0
- package/src/core/interfaces/task.interface.ts +223 -0
- package/src/core/orchestrator/event-coordinator.ts +122 -0
- package/src/core/orchestrator/health-monitor.ts +214 -0
- package/src/core/orchestrator/index.ts +89 -0
- package/src/core/orchestrator/lifecycle-manager.ts +263 -0
- package/src/core/orchestrator/session-manager.ts +279 -0
- package/src/core/orchestrator/task-manager.ts +317 -0
- package/src/events/domain-events.ts +584 -0
- package/src/events/event-store.test.ts +387 -0
- package/src/events/event-store.ts +588 -0
- package/src/events/example-usage.ts +293 -0
- package/src/events/index.ts +90 -0
- package/src/events/projections.ts +561 -0
- package/src/events/state-reconstructor.ts +349 -0
- package/src/events.ts +367 -0
- package/src/hooks/INTEGRATION.md +658 -0
- package/src/hooks/README.md +532 -0
- package/src/hooks/example-usage.ts +499 -0
- package/src/hooks/executor.ts +379 -0
- package/src/hooks/hooks.test.ts +421 -0
- package/src/hooks/index.ts +131 -0
- package/src/hooks/registry.ts +333 -0
- package/src/hooks/safety/bash-safety.ts +604 -0
- package/src/hooks/safety/file-organization.ts +473 -0
- package/src/hooks/safety/git-commit.ts +623 -0
- package/src/hooks/safety/index.ts +46 -0
- package/src/hooks/session-hooks.ts +559 -0
- package/src/hooks/task-hooks.ts +513 -0
- package/src/hooks/types.ts +357 -0
- package/src/hooks/verify-exports.test.ts +125 -0
- package/src/index.ts +195 -0
- package/src/mcp/connection-pool.ts +438 -0
- package/src/mcp/index.ts +183 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/session-manager.ts +428 -0
- package/src/mcp/tool-registry.ts +566 -0
- package/src/mcp/transport/http.ts +557 -0
- package/src/mcp/transport/index.ts +294 -0
- package/src/mcp/transport/stdio.ts +324 -0
- package/src/mcp/transport/websocket.ts +484 -0
- package/src/mcp/types.ts +565 -0
- package/src/plugin-interface.ts +663 -0
- package/src/plugin-loader.ts +638 -0
- package/src/plugin-registry.ts +604 -0
- package/src/plugins/index.ts +34 -0
- package/src/plugins/official/hive-mind-plugin.ts +330 -0
- package/src/plugins/official/index.ts +24 -0
- package/src/plugins/official/maestro-plugin.ts +508 -0
- package/src/plugins/types.ts +108 -0
- package/src/resilience/bulkhead.ts +277 -0
- package/src/resilience/circuit-breaker.ts +326 -0
- package/src/resilience/index.ts +26 -0
- package/src/resilience/rate-limiter.ts +420 -0
- package/src/resilience/retry.ts +224 -0
- package/src/security/index.ts +39 -0
- package/src/security/input-validation.ts +265 -0
- package/src/security/secure-random.ts +159 -0
- package/src/services/index.ts +16 -0
- package/src/services/v3-progress.service.ts +505 -0
- package/src/types/agent.types.ts +144 -0
- package/src/types/index.ts +22 -0
- package/src/types/mcp.types.ts +300 -0
- package/src/types/memory.types.ts +263 -0
- package/src/types/swarm.types.ts +255 -0
- package/src/types/task.types.ts +205 -0
- package/src/types.ts +367 -0
- package/src/utils/secure-logger.d.ts +69 -0
- package/src/utils/secure-logger.d.ts.map +1 -0
- package/src/utils/secure-logger.js +208 -0
- package/src/utils/secure-logger.js.map +1 -0
- package/src/utils/secure-logger.ts +257 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Bash Safety Hook
|
|
3
|
+
*
|
|
4
|
+
* TypeScript conversion of V2 bash-hook.sh.
|
|
5
|
+
* Provides command safety analysis, dangerous command detection,
|
|
6
|
+
* secret detection, and safe alternatives.
|
|
7
|
+
*
|
|
8
|
+
* @module v3/shared/hooks/safety/bash-safety
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
HookEvent,
|
|
13
|
+
HookContext,
|
|
14
|
+
HookResult,
|
|
15
|
+
HookPriority,
|
|
16
|
+
CommandInfo,
|
|
17
|
+
} from '../types.js';
|
|
18
|
+
import { HookRegistry } from '../registry.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Bash safety hook result
|
|
22
|
+
*/
|
|
23
|
+
export interface BashSafetyResult extends HookResult {
|
|
24
|
+
/** Risk level assessment */
|
|
25
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
26
|
+
/** Whether the command should be blocked */
|
|
27
|
+
blocked: boolean;
|
|
28
|
+
/** Reason for blocking (if blocked) */
|
|
29
|
+
blockReason?: string;
|
|
30
|
+
/** Modified command (if applicable) */
|
|
31
|
+
modifiedCommand?: string;
|
|
32
|
+
/** Detected risks */
|
|
33
|
+
risks: CommandRisk[];
|
|
34
|
+
/** Safe alternatives */
|
|
35
|
+
safeAlternatives?: string[];
|
|
36
|
+
/** Warnings (non-blocking) */
|
|
37
|
+
warnings?: string[];
|
|
38
|
+
/** Missing dependencies detected */
|
|
39
|
+
missingDependencies?: string[];
|
|
40
|
+
/** Redacted command (secrets removed) */
|
|
41
|
+
redactedCommand?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Command risk definition
|
|
46
|
+
*/
|
|
47
|
+
export interface CommandRisk {
|
|
48
|
+
/** Risk type */
|
|
49
|
+
type: 'dangerous' | 'destructive' | 'secret' | 'privilege' | 'network' | 'resource';
|
|
50
|
+
/** Risk severity */
|
|
51
|
+
severity: 'low' | 'medium' | 'high' | 'critical';
|
|
52
|
+
/** Risk description */
|
|
53
|
+
description: string;
|
|
54
|
+
/** Pattern that matched */
|
|
55
|
+
pattern?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Dangerous command patterns
|
|
60
|
+
*/
|
|
61
|
+
const DANGEROUS_PATTERNS: Array<{
|
|
62
|
+
pattern: RegExp;
|
|
63
|
+
type: CommandRisk['type'];
|
|
64
|
+
severity: CommandRisk['severity'];
|
|
65
|
+
description: string;
|
|
66
|
+
block: boolean;
|
|
67
|
+
}> = [
|
|
68
|
+
// Critical - Always block
|
|
69
|
+
{
|
|
70
|
+
pattern: /rm\s+(-[rRf]+\s+)*\//,
|
|
71
|
+
type: 'destructive',
|
|
72
|
+
severity: 'critical',
|
|
73
|
+
description: 'Recursive deletion from root directory',
|
|
74
|
+
block: true,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
pattern: /rm\s+-rf\s+\/\*/,
|
|
78
|
+
type: 'destructive',
|
|
79
|
+
severity: 'critical',
|
|
80
|
+
description: 'Recursive deletion of all root files',
|
|
81
|
+
block: true,
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
pattern: /dd\s+if=.*of=\/dev\/(sd|hd|nvme)/,
|
|
85
|
+
type: 'destructive',
|
|
86
|
+
severity: 'critical',
|
|
87
|
+
description: 'Direct disk write that can destroy data',
|
|
88
|
+
block: true,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
pattern: /mkfs\./,
|
|
92
|
+
type: 'destructive',
|
|
93
|
+
severity: 'critical',
|
|
94
|
+
description: 'Filesystem formatting command',
|
|
95
|
+
block: true,
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
pattern: />\s*\/dev\/sd[a-z]/,
|
|
99
|
+
type: 'destructive',
|
|
100
|
+
severity: 'critical',
|
|
101
|
+
description: 'Direct write to disk device',
|
|
102
|
+
block: true,
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
// Fork bomb patterns - various formats (with flexible spacing)
|
|
106
|
+
pattern: /:\s*\(\s*\)\s*\{\s*:\s*\|\s*:\s*&\s*\}\s*;\s*:|bomb\s*\(\)|while\s+true.*fork/,
|
|
107
|
+
type: 'resource',
|
|
108
|
+
severity: 'critical',
|
|
109
|
+
description: 'Fork bomb detected',
|
|
110
|
+
block: true,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
pattern: /chmod\s+(-R\s+)?777\s+\//,
|
|
114
|
+
type: 'privilege',
|
|
115
|
+
severity: 'critical',
|
|
116
|
+
description: 'Setting dangerous permissions on root',
|
|
117
|
+
block: true,
|
|
118
|
+
},
|
|
119
|
+
|
|
120
|
+
// High - Block but offer alternatives
|
|
121
|
+
{
|
|
122
|
+
pattern: /rm\s+-rf\s+\*/,
|
|
123
|
+
type: 'destructive',
|
|
124
|
+
severity: 'high',
|
|
125
|
+
description: 'Recursive deletion of all files in directory',
|
|
126
|
+
block: true,
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
pattern: /rm\s+-rf\s+\.\//,
|
|
130
|
+
type: 'destructive',
|
|
131
|
+
severity: 'high',
|
|
132
|
+
description: 'Recursive deletion of current directory',
|
|
133
|
+
block: true,
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
pattern: /rm\s+-rf\s+~/,
|
|
137
|
+
type: 'destructive',
|
|
138
|
+
severity: 'high',
|
|
139
|
+
description: 'Recursive deletion of home directory',
|
|
140
|
+
block: true,
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
pattern: /curl.*\|\s*(bash|sh|zsh)/,
|
|
144
|
+
type: 'dangerous',
|
|
145
|
+
severity: 'high',
|
|
146
|
+
description: 'Piping remote content directly to shell',
|
|
147
|
+
block: true,
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
pattern: /wget.*-O-\s*\|\s*(bash|sh|zsh)/,
|
|
151
|
+
type: 'dangerous',
|
|
152
|
+
severity: 'high',
|
|
153
|
+
description: 'Piping remote content directly to shell',
|
|
154
|
+
block: true,
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
pattern: /eval\s+.*\$\(/,
|
|
158
|
+
type: 'dangerous',
|
|
159
|
+
severity: 'high',
|
|
160
|
+
description: 'Dynamic code execution with command substitution',
|
|
161
|
+
block: true,
|
|
162
|
+
},
|
|
163
|
+
|
|
164
|
+
// Medium - Warn
|
|
165
|
+
{
|
|
166
|
+
pattern: /rm\s+(?!.*-i)/,
|
|
167
|
+
type: 'destructive',
|
|
168
|
+
severity: 'medium',
|
|
169
|
+
description: 'Remove command without interactive flag',
|
|
170
|
+
block: false,
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
pattern: /sudo\s+rm/,
|
|
174
|
+
type: 'privilege',
|
|
175
|
+
severity: 'medium',
|
|
176
|
+
description: 'Privileged file deletion',
|
|
177
|
+
block: false,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
pattern: /sudo\s+chmod/,
|
|
181
|
+
type: 'privilege',
|
|
182
|
+
severity: 'medium',
|
|
183
|
+
description: 'Privileged permission change',
|
|
184
|
+
block: false,
|
|
185
|
+
},
|
|
186
|
+
{
|
|
187
|
+
pattern: /git\s+push\s+.*--force/,
|
|
188
|
+
type: 'destructive',
|
|
189
|
+
severity: 'medium',
|
|
190
|
+
description: 'Force push can overwrite remote history',
|
|
191
|
+
block: false,
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
pattern: /git\s+reset\s+--hard/,
|
|
195
|
+
type: 'destructive',
|
|
196
|
+
severity: 'medium',
|
|
197
|
+
description: 'Hard reset discards uncommitted changes',
|
|
198
|
+
block: false,
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
pattern: /DROP\s+(DATABASE|TABLE)/i,
|
|
202
|
+
type: 'destructive',
|
|
203
|
+
severity: 'high',
|
|
204
|
+
description: 'Database/table deletion command',
|
|
205
|
+
block: false,
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
pattern: /TRUNCATE\s+TABLE/i,
|
|
209
|
+
type: 'destructive',
|
|
210
|
+
severity: 'medium',
|
|
211
|
+
description: 'Table truncation command',
|
|
212
|
+
block: false,
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
// Low - Informational
|
|
216
|
+
{
|
|
217
|
+
pattern: /kill\s+-9/,
|
|
218
|
+
type: 'dangerous',
|
|
219
|
+
severity: 'low',
|
|
220
|
+
description: 'Force kill signal prevents graceful shutdown',
|
|
221
|
+
block: false,
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
pattern: /killall/,
|
|
225
|
+
type: 'dangerous',
|
|
226
|
+
severity: 'low',
|
|
227
|
+
description: 'Kills all processes by name',
|
|
228
|
+
block: false,
|
|
229
|
+
},
|
|
230
|
+
];
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Secret patterns to detect and redact
|
|
234
|
+
*/
|
|
235
|
+
const SECRET_PATTERNS: Array<{
|
|
236
|
+
pattern: RegExp;
|
|
237
|
+
name: string;
|
|
238
|
+
redactGroup?: number;
|
|
239
|
+
}> = [
|
|
240
|
+
{ pattern: /(password|passwd|pwd)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'password', redactGroup: 2 },
|
|
241
|
+
{ pattern: /(api[_-]?key)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'API key', redactGroup: 2 },
|
|
242
|
+
{ pattern: /(secret[_-]?key)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'secret key', redactGroup: 2 },
|
|
243
|
+
{ pattern: /(access[_-]?token)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'access token', redactGroup: 2 },
|
|
244
|
+
{ pattern: /(auth[_-]?token)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'auth token', redactGroup: 2 },
|
|
245
|
+
{ pattern: /(bearer)\s+([a-zA-Z0-9._-]+)/i, name: 'bearer token', redactGroup: 2 },
|
|
246
|
+
{ pattern: /(private[_-]?key)\s*[=:]\s*['"]?([^\s'"]+)/i, name: 'private key', redactGroup: 2 },
|
|
247
|
+
{ pattern: /(\bsk-[a-zA-Z0-9]{20,})/i, name: 'OpenAI API key' },
|
|
248
|
+
{ pattern: /(\bghp_[a-zA-Z0-9]{36,})/i, name: 'GitHub token' },
|
|
249
|
+
{ pattern: /(\bnpm_[a-zA-Z0-9]{36,})/i, name: 'npm token' },
|
|
250
|
+
{ pattern: /(AKIA[0-9A-Z]{16})/i, name: 'AWS access key' },
|
|
251
|
+
];
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Common dependencies to check
|
|
255
|
+
*/
|
|
256
|
+
const DEPENDENCY_CHECKS: Array<{
|
|
257
|
+
command: RegExp;
|
|
258
|
+
dependency: string;
|
|
259
|
+
}> = [
|
|
260
|
+
{ command: /\bjq\b/, dependency: 'jq' },
|
|
261
|
+
{ command: /\byq\b/, dependency: 'yq' },
|
|
262
|
+
{ command: /\bawk\b/, dependency: 'awk' },
|
|
263
|
+
{ command: /\bsed\b/, dependency: 'sed' },
|
|
264
|
+
{ command: /\bcurl\b/, dependency: 'curl' },
|
|
265
|
+
{ command: /\bwget\b/, dependency: 'wget' },
|
|
266
|
+
{ command: /\bgit\b/, dependency: 'git' },
|
|
267
|
+
{ command: /\bdocker\b/, dependency: 'docker' },
|
|
268
|
+
{ command: /\bkubectl\b/, dependency: 'kubectl' },
|
|
269
|
+
{ command: /\bpython3?\b/, dependency: 'python' },
|
|
270
|
+
{ command: /\bnode\b/, dependency: 'node' },
|
|
271
|
+
{ command: /\bnpm\b/, dependency: 'npm' },
|
|
272
|
+
{ command: /\byarn\b/, dependency: 'yarn' },
|
|
273
|
+
{ command: /\bpnpm\b/, dependency: 'pnpm' },
|
|
274
|
+
];
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Safe alternatives for dangerous commands (with patterns for matching)
|
|
278
|
+
*/
|
|
279
|
+
const SAFE_ALTERNATIVES: Array<{
|
|
280
|
+
pattern: RegExp;
|
|
281
|
+
alternatives: string[];
|
|
282
|
+
}> = [
|
|
283
|
+
{
|
|
284
|
+
pattern: /rm\s+-rf\s+\*/,
|
|
285
|
+
alternatives: [
|
|
286
|
+
'rm -ri * (interactive mode)',
|
|
287
|
+
'find . -maxdepth 1 -type f -delete (only files)',
|
|
288
|
+
'git clean -fd (for git repositories)',
|
|
289
|
+
],
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
pattern: /rm\s+-rf/,
|
|
293
|
+
alternatives: [
|
|
294
|
+
'rm -ri (interactive mode)',
|
|
295
|
+
'trash-cli (move to trash instead)',
|
|
296
|
+
'mv to backup directory first',
|
|
297
|
+
],
|
|
298
|
+
},
|
|
299
|
+
{
|
|
300
|
+
pattern: /kill\s+-9/,
|
|
301
|
+
alternatives: [
|
|
302
|
+
'kill (graceful termination first)',
|
|
303
|
+
'kill -15 (SIGTERM)',
|
|
304
|
+
'systemctl stop (for services)',
|
|
305
|
+
],
|
|
306
|
+
},
|
|
307
|
+
{
|
|
308
|
+
pattern: /curl.*\|\s*(bash|sh|zsh)/,
|
|
309
|
+
alternatives: [
|
|
310
|
+
'Download script first, review, then execute',
|
|
311
|
+
'Use package managers when available',
|
|
312
|
+
'Verify script hash before execution',
|
|
313
|
+
],
|
|
314
|
+
},
|
|
315
|
+
{
|
|
316
|
+
pattern: /wget.*\|\s*(bash|sh|zsh)/,
|
|
317
|
+
alternatives: [
|
|
318
|
+
'Download script first, review, then execute',
|
|
319
|
+
'Use package managers when available',
|
|
320
|
+
'Verify script hash before execution',
|
|
321
|
+
],
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
pattern: /git\s+push.*--force/,
|
|
325
|
+
alternatives: [
|
|
326
|
+
'git push --force-with-lease (safer)',
|
|
327
|
+
'Create backup branch first',
|
|
328
|
+
'git push --force-if-includes',
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
{
|
|
332
|
+
pattern: /git\s+reset\s+--hard/,
|
|
333
|
+
alternatives: [
|
|
334
|
+
'git stash (save changes first)',
|
|
335
|
+
'git reset --soft (keep changes staged)',
|
|
336
|
+
'Create backup branch first',
|
|
337
|
+
],
|
|
338
|
+
},
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Bash Safety Hook Manager
|
|
343
|
+
*/
|
|
344
|
+
export class BashSafetyHook {
|
|
345
|
+
private registry: HookRegistry;
|
|
346
|
+
private blockedCommands: Set<string> = new Set();
|
|
347
|
+
private availableDependencies: Set<string> = new Set();
|
|
348
|
+
|
|
349
|
+
constructor(registry: HookRegistry) {
|
|
350
|
+
this.registry = registry;
|
|
351
|
+
this.registerHooks();
|
|
352
|
+
this.detectAvailableDependencies();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Register bash safety hooks
|
|
357
|
+
*/
|
|
358
|
+
private registerHooks(): void {
|
|
359
|
+
this.registry.register(
|
|
360
|
+
HookEvent.PreCommand,
|
|
361
|
+
this.analyzeCommand.bind(this),
|
|
362
|
+
HookPriority.Critical,
|
|
363
|
+
{ name: 'bash-safety:pre-command' }
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Detect available dependencies
|
|
369
|
+
*/
|
|
370
|
+
private async detectAvailableDependencies(): Promise<void> {
|
|
371
|
+
// In a real implementation, this would check which commands are available
|
|
372
|
+
// For now, assume common ones are available
|
|
373
|
+
const commonDeps = ['git', 'node', 'npm', 'curl', 'sed', 'awk'];
|
|
374
|
+
commonDeps.forEach(dep => this.availableDependencies.add(dep));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Analyze a command for safety
|
|
379
|
+
*/
|
|
380
|
+
async analyzeCommand(context: HookContext): Promise<BashSafetyResult> {
|
|
381
|
+
const commandInfo = context.command;
|
|
382
|
+
if (!commandInfo) {
|
|
383
|
+
return this.createResult('low', false, []);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
const command = commandInfo.command;
|
|
387
|
+
const risks: CommandRisk[] = [];
|
|
388
|
+
const warnings: string[] = [];
|
|
389
|
+
let blocked = false;
|
|
390
|
+
let blockReason: string | undefined;
|
|
391
|
+
let modifiedCommand: string | undefined;
|
|
392
|
+
let safeAlternatives: string[] | undefined;
|
|
393
|
+
|
|
394
|
+
// Check for dangerous patterns
|
|
395
|
+
for (const pattern of DANGEROUS_PATTERNS) {
|
|
396
|
+
if (pattern.pattern.test(command)) {
|
|
397
|
+
risks.push({
|
|
398
|
+
type: pattern.type,
|
|
399
|
+
severity: pattern.severity,
|
|
400
|
+
description: pattern.description,
|
|
401
|
+
pattern: pattern.pattern.toString(),
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
if (pattern.block) {
|
|
405
|
+
blocked = true;
|
|
406
|
+
blockReason = pattern.description;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Find safe alternatives using pattern matching
|
|
410
|
+
for (const { pattern: altPattern, alternatives } of SAFE_ALTERNATIVES) {
|
|
411
|
+
if (altPattern.test(command)) {
|
|
412
|
+
safeAlternatives = alternatives;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Check for secrets
|
|
420
|
+
const { secrets, redactedCommand } = this.detectSecrets(command);
|
|
421
|
+
for (const secret of secrets) {
|
|
422
|
+
risks.push({
|
|
423
|
+
type: 'secret',
|
|
424
|
+
severity: 'high',
|
|
425
|
+
description: `Potential ${secret.name} detected in command`,
|
|
426
|
+
});
|
|
427
|
+
warnings.push(`Detected potential secret: ${secret.name}`);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// Check for missing dependencies
|
|
431
|
+
const missingDependencies = this.checkDependencies(command);
|
|
432
|
+
|
|
433
|
+
// Add -i flag to rm commands if not present
|
|
434
|
+
if (/\brm\s+/.test(command) && !/-i\b/.test(command) && !blocked) {
|
|
435
|
+
modifiedCommand = command.replace(/\brm\s+/, 'rm -i ');
|
|
436
|
+
warnings.push('Added -i flag for interactive confirmation');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// Calculate overall risk level
|
|
440
|
+
const riskLevel = this.calculateRiskLevel(risks);
|
|
441
|
+
|
|
442
|
+
// Determine if we should proceed
|
|
443
|
+
const shouldProceed = !blocked;
|
|
444
|
+
|
|
445
|
+
return {
|
|
446
|
+
success: true,
|
|
447
|
+
riskLevel,
|
|
448
|
+
blocked,
|
|
449
|
+
blockReason,
|
|
450
|
+
modifiedCommand,
|
|
451
|
+
risks,
|
|
452
|
+
safeAlternatives,
|
|
453
|
+
warnings: warnings.length > 0 ? warnings : undefined,
|
|
454
|
+
missingDependencies: missingDependencies.length > 0 ? missingDependencies : undefined,
|
|
455
|
+
redactedCommand: secrets.length > 0 ? redactedCommand : undefined,
|
|
456
|
+
abort: blocked,
|
|
457
|
+
data: blocked ? undefined : {
|
|
458
|
+
command: {
|
|
459
|
+
...commandInfo,
|
|
460
|
+
command: modifiedCommand || command,
|
|
461
|
+
isDestructive: risks.some(r => r.type === 'destructive'),
|
|
462
|
+
},
|
|
463
|
+
},
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Detect secrets in command
|
|
469
|
+
*/
|
|
470
|
+
private detectSecrets(command: string): {
|
|
471
|
+
secrets: Array<{ name: string; position: number }>;
|
|
472
|
+
redactedCommand: string;
|
|
473
|
+
} {
|
|
474
|
+
const secrets: Array<{ name: string; position: number }> = [];
|
|
475
|
+
let redactedCommand = command;
|
|
476
|
+
|
|
477
|
+
for (const { pattern, name, redactGroup } of SECRET_PATTERNS) {
|
|
478
|
+
const match = pattern.exec(command);
|
|
479
|
+
if (match) {
|
|
480
|
+
secrets.push({ name, position: match.index });
|
|
481
|
+
|
|
482
|
+
// Redact the secret value
|
|
483
|
+
if (redactGroup && match[redactGroup]) {
|
|
484
|
+
redactedCommand = redactedCommand.replace(
|
|
485
|
+
match[redactGroup],
|
|
486
|
+
'[REDACTED]'
|
|
487
|
+
);
|
|
488
|
+
} else {
|
|
489
|
+
redactedCommand = redactedCommand.replace(
|
|
490
|
+
match[0],
|
|
491
|
+
`[REDACTED_${name.toUpperCase().replace(/\s/g, '_')}]`
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return { secrets, redactedCommand };
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Check for missing dependencies
|
|
502
|
+
*/
|
|
503
|
+
private checkDependencies(command: string): string[] {
|
|
504
|
+
const missing: string[] = [];
|
|
505
|
+
|
|
506
|
+
for (const { command: pattern, dependency } of DEPENDENCY_CHECKS) {
|
|
507
|
+
if (pattern.test(command) && !this.availableDependencies.has(dependency)) {
|
|
508
|
+
missing.push(dependency);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
return missing;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Calculate overall risk level
|
|
517
|
+
*/
|
|
518
|
+
private calculateRiskLevel(risks: CommandRisk[]): BashSafetyResult['riskLevel'] {
|
|
519
|
+
if (risks.length === 0) {
|
|
520
|
+
return 'low';
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
const severities = risks.map(r => r.severity);
|
|
524
|
+
|
|
525
|
+
if (severities.includes('critical')) {
|
|
526
|
+
return 'critical';
|
|
527
|
+
}
|
|
528
|
+
if (severities.includes('high')) {
|
|
529
|
+
return 'high';
|
|
530
|
+
}
|
|
531
|
+
if (severities.includes('medium')) {
|
|
532
|
+
return 'medium';
|
|
533
|
+
}
|
|
534
|
+
return 'low';
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Create a result object
|
|
539
|
+
*/
|
|
540
|
+
private createResult(
|
|
541
|
+
riskLevel: BashSafetyResult['riskLevel'],
|
|
542
|
+
blocked: boolean,
|
|
543
|
+
risks: CommandRisk[]
|
|
544
|
+
): BashSafetyResult {
|
|
545
|
+
return {
|
|
546
|
+
success: true,
|
|
547
|
+
riskLevel,
|
|
548
|
+
blocked,
|
|
549
|
+
risks,
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Manually analyze a command
|
|
555
|
+
*/
|
|
556
|
+
async analyze(command: string): Promise<BashSafetyResult> {
|
|
557
|
+
const context: HookContext = {
|
|
558
|
+
event: HookEvent.PreCommand,
|
|
559
|
+
timestamp: new Date(),
|
|
560
|
+
command: { command },
|
|
561
|
+
};
|
|
562
|
+
|
|
563
|
+
return this.analyzeCommand(context);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Add a custom dangerous pattern
|
|
568
|
+
*/
|
|
569
|
+
addDangerousPattern(
|
|
570
|
+
pattern: RegExp,
|
|
571
|
+
type: CommandRisk['type'],
|
|
572
|
+
severity: CommandRisk['severity'],
|
|
573
|
+
description: string,
|
|
574
|
+
block = true
|
|
575
|
+
): void {
|
|
576
|
+
DANGEROUS_PATTERNS.push({ pattern, type, severity, description, block });
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* Mark a dependency as available
|
|
581
|
+
*/
|
|
582
|
+
markDependencyAvailable(dependency: string): void {
|
|
583
|
+
this.availableDependencies.add(dependency);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Check if a command would be blocked
|
|
588
|
+
*/
|
|
589
|
+
wouldBlock(command: string): boolean {
|
|
590
|
+
for (const pattern of DANGEROUS_PATTERNS) {
|
|
591
|
+
if (pattern.block && pattern.pattern.test(command)) {
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return false;
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/**
|
|
600
|
+
* Create bash safety hook
|
|
601
|
+
*/
|
|
602
|
+
export function createBashSafetyHook(registry: HookRegistry): BashSafetyHook {
|
|
603
|
+
return new BashSafetyHook(registry);
|
|
604
|
+
}
|