mstro-app 0.3.7 → 0.3.8
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 +4 -8
- package/bin/mstro.js +54 -15
- package/dist/server/cli/headless/stall-assessor.d.ts.map +1 -1
- package/dist/server/cli/headless/stall-assessor.js +4 -1
- package/dist/server/cli/headless/stall-assessor.js.map +1 -1
- package/dist/server/cli/headless/tool-watchdog.d.ts.map +1 -1
- package/dist/server/cli/headless/tool-watchdog.js +8 -0
- package/dist/server/cli/headless/tool-watchdog.js.map +1 -1
- package/dist/server/index.js +0 -4
- package/dist/server/index.js.map +1 -1
- package/dist/server/mcp/bouncer-integration.d.ts +2 -0
- package/dist/server/mcp/bouncer-integration.d.ts.map +1 -1
- package/dist/server/mcp/bouncer-integration.js +55 -39
- package/dist/server/mcp/bouncer-integration.js.map +1 -1
- package/dist/server/mcp/bouncer-sandbox.d.ts +60 -0
- package/dist/server/mcp/bouncer-sandbox.d.ts.map +1 -0
- package/dist/server/mcp/bouncer-sandbox.js +182 -0
- package/dist/server/mcp/bouncer-sandbox.js.map +1 -0
- package/dist/server/mcp/security-patterns.d.ts +6 -12
- package/dist/server/mcp/security-patterns.d.ts.map +1 -1
- package/dist/server/mcp/security-patterns.js +197 -10
- package/dist/server/mcp/security-patterns.js.map +1 -1
- package/dist/server/services/websocket/handler.d.ts +0 -1
- package/dist/server/services/websocket/handler.d.ts.map +1 -1
- package/dist/server/services/websocket/handler.js +7 -2
- package/dist/server/services/websocket/handler.js.map +1 -1
- package/dist/server/services/websocket/quality-handlers.d.ts +4 -0
- package/dist/server/services/websocket/quality-handlers.d.ts.map +1 -0
- package/dist/server/services/websocket/quality-handlers.js +106 -0
- package/dist/server/services/websocket/quality-handlers.js.map +1 -0
- package/dist/server/services/websocket/quality-service.d.ts +54 -0
- package/dist/server/services/websocket/quality-service.d.ts.map +1 -0
- package/dist/server/services/websocket/quality-service.js +766 -0
- package/dist/server/services/websocket/quality-service.js.map +1 -0
- package/dist/server/services/websocket/session-handlers.d.ts.map +1 -1
- package/dist/server/services/websocket/session-handlers.js +23 -0
- package/dist/server/services/websocket/session-handlers.js.map +1 -1
- package/dist/server/services/websocket/types.d.ts +2 -2
- package/dist/server/services/websocket/types.d.ts.map +1 -1
- package/package.json +2 -1
- package/server/cli/headless/stall-assessor.ts +4 -1
- package/server/cli/headless/tool-watchdog.ts +8 -0
- package/server/index.ts +0 -4
- package/server/mcp/bouncer-integration.ts +66 -44
- package/server/mcp/bouncer-sandbox.ts +214 -0
- package/server/mcp/security-patterns.ts +206 -10
- package/server/services/websocket/handler.ts +7 -2
- package/server/services/websocket/quality-handlers.ts +140 -0
- package/server/services/websocket/quality-service.ts +922 -0
- package/server/services/websocket/session-handlers.ts +26 -0
- package/server/services/websocket/types.ts +14 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Copyright (c) 2025-present Mstro, Inc. All rights reserved.
|
|
2
|
+
// Licensed under the MIT License. See LICENSE file for details.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sandbox Harness for Bouncer Testing
|
|
6
|
+
*
|
|
7
|
+
* Wraps command execution in Anthropic's sandbox-runtime (bubblewrap on Linux,
|
|
8
|
+
* sandbox-exec on macOS) to safely test what happens when the bouncer FAILS —
|
|
9
|
+
* i.e., when a malicious tool call gets through.
|
|
10
|
+
*
|
|
11
|
+
* Usage in tests:
|
|
12
|
+
* const harness = new BouncerSandboxHarness();
|
|
13
|
+
* await harness.initialize();
|
|
14
|
+
* const result = await harness.executeInSandbox('rm -rf /tmp/test-canary');
|
|
15
|
+
* expect(result.violations).toContain(...)
|
|
16
|
+
* await harness.cleanup();
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { execSync } from 'node:child_process';
|
|
20
|
+
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { tmpdir } from 'node:os';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
|
|
24
|
+
export interface SandboxExecResult {
|
|
25
|
+
/** The sandboxed command that was actually run */
|
|
26
|
+
wrappedCommand: string;
|
|
27
|
+
/** Whether sandbox-runtime is available on this platform */
|
|
28
|
+
sandboxAvailable: boolean;
|
|
29
|
+
/** Whether the sandbox contained the operation (no violations) */
|
|
30
|
+
contained: boolean;
|
|
31
|
+
/** List of violation descriptions if any escaped the sandbox */
|
|
32
|
+
violations: string[];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface CanaryCheckResult {
|
|
36
|
+
/** Whether the canary file still exists (should be true if sandbox contained the write) */
|
|
37
|
+
canaryIntact: boolean;
|
|
38
|
+
/** Whether a file was written outside the sandbox (should be false) */
|
|
39
|
+
escapeDetected: boolean;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Test harness that wraps command execution in sandbox-runtime.
|
|
44
|
+
* Provides canary files and violation tracking to verify containment.
|
|
45
|
+
*/
|
|
46
|
+
export class BouncerSandboxHarness {
|
|
47
|
+
private sandboxManager: Awaited<typeof import('@anthropic-ai/sandbox-runtime')>['SandboxManager'] | null = null;
|
|
48
|
+
private sandboxAvailable = false;
|
|
49
|
+
private tempDir: string;
|
|
50
|
+
private canaryDir: string;
|
|
51
|
+
|
|
52
|
+
constructor() {
|
|
53
|
+
this.tempDir = mkdtempSync(join(tmpdir(), 'bouncer-sandbox-'));
|
|
54
|
+
this.canaryDir = join(this.tempDir, 'canaries');
|
|
55
|
+
mkdirSync(this.canaryDir, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Initialize the sandbox. Falls back gracefully if bwrap/sandbox-exec not available.
|
|
60
|
+
*/
|
|
61
|
+
async initialize(): Promise<{ available: boolean; reason?: string }> {
|
|
62
|
+
try {
|
|
63
|
+
const { SandboxManager } = await import('@anthropic-ai/sandbox-runtime');
|
|
64
|
+
|
|
65
|
+
if (!SandboxManager.isSupportedPlatform()) {
|
|
66
|
+
return { available: false, reason: 'Platform not supported by sandbox-runtime' };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const deps = SandboxManager.checkDependencies();
|
|
70
|
+
if (deps.errors.length > 0) {
|
|
71
|
+
return {
|
|
72
|
+
available: false,
|
|
73
|
+
reason: `Missing dependencies: ${deps.errors.join(', ')}`,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
await SandboxManager.initialize({
|
|
78
|
+
network: {
|
|
79
|
+
allowedDomains: [], // Block ALL network access
|
|
80
|
+
deniedDomains: ['*'],
|
|
81
|
+
},
|
|
82
|
+
filesystem: {
|
|
83
|
+
denyRead: [
|
|
84
|
+
'/home/*/.ssh',
|
|
85
|
+
'/home/*/.aws',
|
|
86
|
+
'/home/*/.gnupg',
|
|
87
|
+
'/etc/shadow',
|
|
88
|
+
'/etc/passwd',
|
|
89
|
+
],
|
|
90
|
+
allowWrite: [this.tempDir], // Only allow writes to our temp dir
|
|
91
|
+
denyWrite: [
|
|
92
|
+
'/',
|
|
93
|
+
'/home',
|
|
94
|
+
'/etc',
|
|
95
|
+
'/usr',
|
|
96
|
+
'/var',
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
this.sandboxManager = SandboxManager;
|
|
102
|
+
this.sandboxAvailable = true;
|
|
103
|
+
return { available: true };
|
|
104
|
+
} catch (error: unknown) {
|
|
105
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
106
|
+
return { available: false, reason: `Failed to initialize sandbox: ${msg}` };
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Execute a command inside the sandbox. Returns containment results.
|
|
112
|
+
* If sandbox is not available, validates the bouncer decision only (no actual execution).
|
|
113
|
+
*/
|
|
114
|
+
async executeInSandbox(command: string): Promise<SandboxExecResult> {
|
|
115
|
+
if (!this.sandboxAvailable || !this.sandboxManager) {
|
|
116
|
+
return {
|
|
117
|
+
wrappedCommand: command,
|
|
118
|
+
sandboxAvailable: false,
|
|
119
|
+
contained: true,
|
|
120
|
+
violations: ['Sandbox not available — decision-only testing mode'],
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const violations: string[] = [];
|
|
125
|
+
try {
|
|
126
|
+
const wrappedCommand = await this.sandboxManager.wrapWithSandbox(command);
|
|
127
|
+
|
|
128
|
+
// Execute the wrapped command and capture violations
|
|
129
|
+
try {
|
|
130
|
+
execSync(wrappedCommand, {
|
|
131
|
+
timeout: 5000,
|
|
132
|
+
stdio: 'pipe',
|
|
133
|
+
cwd: this.tempDir,
|
|
134
|
+
});
|
|
135
|
+
} catch {
|
|
136
|
+
// Command failure inside sandbox is expected for malicious ops
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Check violation store
|
|
140
|
+
const stderr = this.sandboxManager.annotateStderrWithSandboxFailures(command, '');
|
|
141
|
+
if (stderr) {
|
|
142
|
+
violations.push(stderr);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
this.sandboxManager.cleanupAfterCommand();
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
wrappedCommand,
|
|
149
|
+
sandboxAvailable: true,
|
|
150
|
+
contained: violations.length === 0,
|
|
151
|
+
violations,
|
|
152
|
+
};
|
|
153
|
+
} catch (error: unknown) {
|
|
154
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
155
|
+
violations.push(`Sandbox execution error: ${msg}`);
|
|
156
|
+
return {
|
|
157
|
+
wrappedCommand: command,
|
|
158
|
+
sandboxAvailable: true,
|
|
159
|
+
contained: true, // Error means the command didn't execute
|
|
160
|
+
violations,
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Place a canary file and return a checker to verify containment.
|
|
167
|
+
* If a sandboxed command can delete or modify the canary, containment failed.
|
|
168
|
+
*/
|
|
169
|
+
placeCanary(name: string): { path: string; check: () => CanaryCheckResult } {
|
|
170
|
+
const canaryPath = join(this.canaryDir, name);
|
|
171
|
+
const escapePath = join(this.canaryDir, `${name}.escaped`);
|
|
172
|
+
writeFileSync(canaryPath, `canary-${Date.now()}`, 'utf-8');
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
path: canaryPath,
|
|
176
|
+
check: () => ({
|
|
177
|
+
canaryIntact: existsSync(canaryPath),
|
|
178
|
+
escapeDetected: existsSync(escapePath),
|
|
179
|
+
}),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get the temp directory where sandboxed commands can write.
|
|
185
|
+
*/
|
|
186
|
+
getSandboxWriteDir(): string {
|
|
187
|
+
return this.tempDir;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Whether the sandbox is actually available and initialized.
|
|
192
|
+
*/
|
|
193
|
+
isAvailable(): boolean {
|
|
194
|
+
return this.sandboxAvailable;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Clean up temp dirs and reset sandbox state.
|
|
199
|
+
*/
|
|
200
|
+
async cleanup(): Promise<void> {
|
|
201
|
+
try {
|
|
202
|
+
if (this.sandboxManager) {
|
|
203
|
+
await this.sandboxManager.reset();
|
|
204
|
+
}
|
|
205
|
+
} catch {
|
|
206
|
+
// Ignore cleanup errors
|
|
207
|
+
}
|
|
208
|
+
try {
|
|
209
|
+
rmSync(this.tempDir, { recursive: true, force: true });
|
|
210
|
+
} catch {
|
|
211
|
+
// Ignore cleanup errors
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* - The question is: "Does this operation make sense given user intent?"
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
import { resolve } from 'node:path';
|
|
18
|
+
|
|
17
19
|
export interface SecurityPattern {
|
|
18
20
|
pattern: RegExp;
|
|
19
21
|
reason?: string;
|
|
@@ -83,7 +85,16 @@ export const CRITICAL_THREATS: SecurityPattern[] = [
|
|
|
83
85
|
{
|
|
84
86
|
pattern: /chmod\s+000\s+\//i,
|
|
85
87
|
reason: 'Attempting to make system directories inaccessible'
|
|
86
|
-
}
|
|
88
|
+
},
|
|
89
|
+
// Reverse shells - never legitimate in a dev workflow
|
|
90
|
+
{
|
|
91
|
+
pattern: /\/dev\/tcp\//i,
|
|
92
|
+
reason: 'Reverse shell via /dev/tcp - classic backdoor technique'
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
pattern: /\bnc\b.*-[elp].*\b\d+\b/i,
|
|
96
|
+
reason: 'Netcat listener/reverse shell - common backdoor technique'
|
|
97
|
+
},
|
|
87
98
|
// NOTE: curl|bash is NOT here - it goes to Haiku for context review
|
|
88
99
|
// The question is "did a bad actor inject this?" not "is curl|bash dangerous?"
|
|
89
100
|
];
|
|
@@ -158,12 +169,104 @@ export const NEEDS_AI_REVIEW: SecurityPattern[] = [
|
|
|
158
169
|
reason: 'Recursive deletion - verify target matches user intent'
|
|
159
170
|
},
|
|
160
171
|
|
|
172
|
+
// Data exfiltration patterns — piping data to network tools
|
|
173
|
+
{
|
|
174
|
+
pattern: /\|\s*(nc|netcat|ncat)\b/i,
|
|
175
|
+
reason: 'Pipe to netcat - potential data exfiltration'
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
pattern: /\bscp\b.*@/i,
|
|
179
|
+
reason: 'SCP to remote host - potential data exfiltration'
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
pattern: /\|\s*curl\b/i,
|
|
183
|
+
reason: 'Pipe to curl - potential data exfiltration'
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
pattern: /curl\b.*-d\s*@/i,
|
|
187
|
+
reason: 'Curl with file upload - potential data exfiltration'
|
|
188
|
+
},
|
|
189
|
+
|
|
161
190
|
// ALL Write/Edit operations that aren't to /tmp go through context review
|
|
162
191
|
// This is the key change: we review based on context, not blanket allow/deny
|
|
163
192
|
{
|
|
164
193
|
pattern: /^(Write|Edit):\s*(?!\/tmp\/|\/var\/tmp\/)/i,
|
|
165
194
|
reason: 'File modification - verify aligns with user request'
|
|
166
195
|
},
|
|
196
|
+
|
|
197
|
+
// Reverse shells and bind shells — network-connected interactive shells
|
|
198
|
+
{
|
|
199
|
+
pattern: /\/dev\/tcp\//i,
|
|
200
|
+
reason: 'Potential reverse shell via /dev/tcp'
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
pattern: /\b(nc|netcat|ncat)\b.*-e\s/i,
|
|
204
|
+
reason: 'Netcat with -e flag - potential reverse shell'
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
pattern: /\bsocket\b.*\bconnect\b.*\b(dup2|subprocess|exec)\b/i,
|
|
208
|
+
reason: 'Programmatic reverse shell pattern (socket+connect+exec)'
|
|
209
|
+
},
|
|
210
|
+
{
|
|
211
|
+
pattern: /\bperl\b.*\bsocket\b.*\bexec\b/i,
|
|
212
|
+
reason: 'Perl reverse shell pattern'
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
// Encoded/obfuscated payloads piped to shell or eval
|
|
216
|
+
{
|
|
217
|
+
pattern: /\b(base64|base32)\b.*-d.*\|\s*(bash|sh)\b/i,
|
|
218
|
+
reason: 'Decoded payload piped to shell - obfuscated command execution'
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
pattern: /\\x[0-9a-f]{2}.*\|\s*(bash|sh)\b/i,
|
|
222
|
+
reason: 'Hex-encoded payload piped to shell'
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
pattern: /\bexec\b.*\b(base64|b64decode)\b/i,
|
|
226
|
+
reason: 'Exec with base64 decoding - obfuscated code execution'
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
pattern: /\bprintf\b.*\\x[0-9a-f].*\|\s*(bash|sh)\b/i,
|
|
230
|
+
reason: 'Printf hex payload piped to shell'
|
|
231
|
+
},
|
|
232
|
+
|
|
233
|
+
// Cloud metadata / SSRF — accessing cloud instance credentials
|
|
234
|
+
{
|
|
235
|
+
pattern: /169\.254\.169\.254/i,
|
|
236
|
+
reason: 'AWS/Azure IMDS access - potential credential theft'
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
pattern: /metadata\.google\.internal/i,
|
|
240
|
+
reason: 'GCP metadata access - potential credential theft'
|
|
241
|
+
},
|
|
242
|
+
|
|
243
|
+
// Persistence — writing to shell profiles, cron, authorized_keys via echo/append
|
|
244
|
+
{
|
|
245
|
+
pattern: />>\s*~?\/?.*\/(authorized_keys|\.bashrc|\.bash_profile|\.zshrc|\.profile)/i,
|
|
246
|
+
reason: 'Appending to sensitive file - potential persistence mechanism'
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
pattern: /\bld\.so\.preload\b/i,
|
|
250
|
+
reason: 'LD_PRELOAD injection - shared library hijacking'
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
// wget with file upload
|
|
254
|
+
{
|
|
255
|
+
pattern: /wget\b.*--post-file/i,
|
|
256
|
+
reason: 'wget file upload - potential data exfiltration'
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
// pip install from custom index (supply chain attack)
|
|
260
|
+
{
|
|
261
|
+
pattern: /pip\b.*--index-url\s+https?:\/\/(?!pypi\.org)/i,
|
|
262
|
+
reason: 'pip install from non-PyPI index - potential supply chain attack'
|
|
263
|
+
},
|
|
264
|
+
|
|
265
|
+
// MCP server manipulation
|
|
266
|
+
{
|
|
267
|
+
pattern: /\bclaude\b.*\bmcp\b.*\badd\b/i,
|
|
268
|
+
reason: 'Adding MCP server - verify source is trusted'
|
|
269
|
+
},
|
|
167
270
|
];
|
|
168
271
|
|
|
169
272
|
/**
|
|
@@ -178,11 +281,70 @@ export function matchesPattern(operation: string, patterns: SecurityPattern[]):
|
|
|
178
281
|
return null;
|
|
179
282
|
}
|
|
180
283
|
|
|
284
|
+
/**
|
|
285
|
+
* Normalize file paths in Write/Edit/Read operations to resolve .. traversal.
|
|
286
|
+
* Prevents path traversal attacks like "Write: /home/user/../../etc/passwd"
|
|
287
|
+
* from matching safe home-directory patterns.
|
|
288
|
+
*/
|
|
289
|
+
export function normalizeOperation(operation: string): string {
|
|
290
|
+
const match = operation.match(/^(Write|Edit|Read):\s*(\S+)/i);
|
|
291
|
+
if (match?.[2].includes('..')) {
|
|
292
|
+
const [, tool, rawPath] = match;
|
|
293
|
+
const normalizedPath = resolve(rawPath);
|
|
294
|
+
return `${tool}: ${normalizedPath}`;
|
|
295
|
+
}
|
|
296
|
+
return operation;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Check if a Bash command contains chain operators that could hide dangerous ops after a safe prefix. */
|
|
300
|
+
function containsChainOperators(operation: string): boolean {
|
|
301
|
+
const commandPart = operation.replace(/^Bash:\s*/i, '');
|
|
302
|
+
return /;|&&|\|\||\n/.test(commandPart);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** Check if a Bash command pipes output to known exfiltration/network tools or shells. */
|
|
306
|
+
function containsDangerousPipe(operation: string): boolean {
|
|
307
|
+
const commandPart = operation.replace(/^Bash:\s*/i, '');
|
|
308
|
+
return /\|\s*(nc|netcat|ncat|curl|wget|scp|bash|sh)\b/i.test(commandPart);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/** Check if a Bash command redirects output to sensitive paths (append or overwrite). */
|
|
312
|
+
function containsSensitiveRedirect(operation: string): boolean {
|
|
313
|
+
const commandPart = operation.replace(/^Bash:\s*/i, '');
|
|
314
|
+
return />>?\s*~?\/?.*\/(authorized_keys|\.bashrc|\.bash_profile|\.zshrc|\.profile|\.ssh\/|\.aws\/|\.gnupg\/|ld\.so\.preload|crontab|sudoers)/i.test(commandPart)
|
|
315
|
+
|| />>?\s*\/etc\//i.test(commandPart);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Check if a Bash command contains subshell or backtick expansion (not simple ${VAR}). */
|
|
319
|
+
function containsBashExpansion(operation: string): boolean {
|
|
320
|
+
const commandPart = operation.replace(/^Bash:\s*/i, '');
|
|
321
|
+
return /`[^`]+`/.test(commandPart) || /\$\([^)]+\)/.test(commandPart);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Check if a Bash command contains any form of shell expansion: ${VAR}, $(...), or backticks. */
|
|
325
|
+
function containsAnyExpansion(operation: string): boolean {
|
|
326
|
+
const cmd = operation.replace(/^Bash:\s*/i, '');
|
|
327
|
+
return /\$\{[^}]+\}/.test(cmd) || /\$\([^)]+\)/.test(cmd) || /`[^`]+`/.test(cmd);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** Check if expansion is safely used as an argument to a known-safe command prefix.
|
|
331
|
+
* e.g., "echo ${HOME}" or "cat ${FILE}" — the expansion can't change the command itself. */
|
|
332
|
+
function isSafeExpansionUse(operation: string): boolean {
|
|
333
|
+
const cmd = operation.replace(/^Bash:\s*/i, '').trim();
|
|
334
|
+
// If the expansion IS the command (first token), it's never safe
|
|
335
|
+
if (/^(\$\{|\$\(|`)/.test(cmd)) return false;
|
|
336
|
+
// Safe command prefixes where expansion as an argument is harmless
|
|
337
|
+
const safePrefix = /^(echo|printf|cat|ls|pwd|whoami|date|env|printenv|test|true|false)\s/i;
|
|
338
|
+
return safePrefix.test(cmd);
|
|
339
|
+
}
|
|
340
|
+
|
|
181
341
|
/**
|
|
182
342
|
* Determine if operation requires AI context review
|
|
183
343
|
*
|
|
184
344
|
* The philosophy here is:
|
|
185
|
-
* -
|
|
345
|
+
* - SENSITIVE_PATHS: Always require review (credentials, system configs)
|
|
346
|
+
* - SAFE_OPERATIONS: No review needed, UNLESS the bash command contains
|
|
347
|
+
* chain operators, dangerous pipes, or subshell/backtick expansion
|
|
186
348
|
* - CRITICAL_THREATS: Auto-deny, no review (catastrophic operations)
|
|
187
349
|
* - Everything else: AI reviews context to determine if it matches user intent
|
|
188
350
|
*/
|
|
@@ -197,17 +359,48 @@ const SAFE_RM_PATTERNS = [
|
|
|
197
359
|
];
|
|
198
360
|
|
|
199
361
|
export function requiresAIReview(operation: string): boolean {
|
|
200
|
-
|
|
201
|
-
|
|
362
|
+
// Normalize paths to prevent .. traversal bypass
|
|
363
|
+
const op = normalizeOperation(operation);
|
|
364
|
+
|
|
365
|
+
// Check sensitive paths BEFORE safe operations — prevents home-dir
|
|
366
|
+
// safe pattern from masking .ssh, .aws, .bashrc, etc.
|
|
367
|
+
if (matchesPattern(op, SENSITIVE_PATHS)) return true;
|
|
368
|
+
|
|
369
|
+
// Bash commands with any shell expansion (${VAR}, $(...), backticks) are
|
|
370
|
+
// opaque — the bouncer can't predict what they expand to at runtime.
|
|
371
|
+
// Route to AI review BEFORE checking CRITICAL_THREATS or SAFE_OPERATIONS,
|
|
372
|
+
// UNLESS the command is clearly safe (expansion is just an argument to a
|
|
373
|
+
// known-safe prefix like "echo ${HOME}").
|
|
374
|
+
if (/^Bash:/i.test(op) && containsAnyExpansion(op) && !isSafeExpansionUse(op)) {
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (matchesPattern(op, SAFE_OPERATIONS)) {
|
|
379
|
+
// Safe bash commands must not contain chain operators, dangerous pipes,
|
|
380
|
+
// or subshell/backtick expansion that could hide dangerous operations.
|
|
381
|
+
// A safe prefix (e.g., "git clone") with chain operators (&&, ;, ||)
|
|
382
|
+
// means the full command isn't necessarily safe — route to AI review.
|
|
383
|
+
if (/^Bash:/i.test(op) && (
|
|
384
|
+
containsChainOperators(op) ||
|
|
385
|
+
containsDangerousPipe(op) ||
|
|
386
|
+
containsBashExpansion(op) ||
|
|
387
|
+
containsSensitiveRedirect(op)
|
|
388
|
+
)) {
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (matchesPattern(op, CRITICAL_THREATS)) return false;
|
|
202
395
|
|
|
203
|
-
if (matchesPattern(
|
|
204
|
-
return !SAFE_RM_PATTERNS.some(p => p.test(
|
|
396
|
+
if (matchesPattern(op, NEEDS_AI_REVIEW)) {
|
|
397
|
+
return !SAFE_RM_PATTERNS.some(p => p.test(op));
|
|
205
398
|
}
|
|
206
399
|
|
|
207
|
-
//
|
|
208
|
-
if (/^Bash:/.test(
|
|
209
|
-
if (
|
|
210
|
-
if (/^Bash:\s*\.\//.test(
|
|
400
|
+
// Glob patterns and script execution are concerning in Bash commands
|
|
401
|
+
if (/^Bash:/.test(op)) {
|
|
402
|
+
if (/\*\*?/.test(op)) return true;
|
|
403
|
+
if (/^Bash:\s*\.\//.test(op)) return true;
|
|
211
404
|
}
|
|
212
405
|
|
|
213
406
|
return false;
|
|
@@ -262,6 +455,9 @@ export function classifyRisk(operation: string): {
|
|
|
262
455
|
{ pattern: /chmod\s+777/i, reason: 'Dangerous permissions' },
|
|
263
456
|
{ pattern: /(curl|wget).*\|.*(bash|sh)/i, reason: 'Remote code execution' },
|
|
264
457
|
{ pattern: /pkill|killall/i, reason: 'Process termination' },
|
|
458
|
+
{ pattern: /\|\s*(nc|netcat|ncat)\b/i, reason: 'Data exfiltration via netcat' },
|
|
459
|
+
{ pattern: /\bscp\b.*@/i, reason: 'Data exfiltration via SCP' },
|
|
460
|
+
{ pattern: /curl\b.*-d\s*@/i, reason: 'Data exfiltration via curl file upload' },
|
|
265
461
|
];
|
|
266
462
|
|
|
267
463
|
for (const pattern of elevatedPatterns) {
|
|
@@ -19,6 +19,7 @@ import { handleFileExplorerMessage, handleFileMessage } from './file-explorer-ha
|
|
|
19
19
|
import { FileUploadHandler } from './file-upload-handler.js';
|
|
20
20
|
import { handleGitMessage } from './git-handlers.js';
|
|
21
21
|
import type { HandlerContext, UsageReporter } from './handler-context.js';
|
|
22
|
+
import { handleQualityMessage } from './quality-handlers.js';
|
|
22
23
|
import { handleHistoryMessage, handleSessionMessage, initializeTab, resumeHistoricalSession } from './session-handlers.js';
|
|
23
24
|
import { SessionRegistry } from './session-registry.js';
|
|
24
25
|
import { generateNotificationSummary, handleGetSettings, handleUpdateSettings } from './settings-handlers.js';
|
|
@@ -220,6 +221,12 @@ export class WebSocketImproviseHandler implements HandlerContext {
|
|
|
220
221
|
return handleRemoveTab(this, ws, tabId, workingDir);
|
|
221
222
|
case 'markTabViewed':
|
|
222
223
|
return handleMarkTabViewed(this, ws, tabId, workingDir);
|
|
224
|
+
// Quality messages
|
|
225
|
+
case 'qualityDetectTools':
|
|
226
|
+
case 'qualityScan':
|
|
227
|
+
case 'qualityInstallTools':
|
|
228
|
+
case 'qualityCodeReview':
|
|
229
|
+
return handleQualityMessage(this, ws, msg, tabId, workingDir);
|
|
223
230
|
// Settings messages
|
|
224
231
|
case 'getSettings':
|
|
225
232
|
return handleGetSettings(this, ws);
|
|
@@ -291,6 +298,4 @@ export class WebSocketImproviseHandler implements HandlerContext {
|
|
|
291
298
|
this.sessions.delete(sessionId);
|
|
292
299
|
}
|
|
293
300
|
|
|
294
|
-
cleanupStaleSessions(): void {
|
|
295
|
-
}
|
|
296
301
|
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright (c) 2025-present Mstro, Inc. All rights reserved.
|
|
2
|
+
// Licensed under the MIT License. See LICENSE file for details.
|
|
3
|
+
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import type { HandlerContext } from './handler-context.js';
|
|
6
|
+
import { detectTools, installTools, runQualityScan } from './quality-service.js';
|
|
7
|
+
import type { WebSocketMessage, WSContext } from './types.js';
|
|
8
|
+
|
|
9
|
+
export function handleQualityMessage(
|
|
10
|
+
ctx: HandlerContext,
|
|
11
|
+
ws: WSContext,
|
|
12
|
+
msg: WebSocketMessage,
|
|
13
|
+
_tabId: string,
|
|
14
|
+
workingDir: string,
|
|
15
|
+
): void {
|
|
16
|
+
const handlers: Record<string, () => void> = {
|
|
17
|
+
qualityDetectTools: () => handleDetectTools(ctx, ws, msg, workingDir),
|
|
18
|
+
qualityScan: () => handleScan(ctx, ws, msg, workingDir),
|
|
19
|
+
qualityInstallTools: () => handleInstallTools(ctx, ws, msg, workingDir),
|
|
20
|
+
qualityCodeReview: () => handleCodeReview(ctx, ws, msg, workingDir),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const handler = handlers[msg.type];
|
|
24
|
+
if (!handler) return;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
handler();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const errMsg = error instanceof Error ? error.message : String(error);
|
|
30
|
+
ctx.send(ws, {
|
|
31
|
+
type: 'qualityError',
|
|
32
|
+
data: { path: msg.data?.path || workingDir, error: errMsg },
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function resolvePath(workingDir: string, dirPath?: string): string {
|
|
38
|
+
if (!dirPath || dirPath === '.' || dirPath === './') return workingDir;
|
|
39
|
+
if (dirPath.startsWith('/')) return dirPath;
|
|
40
|
+
return join(workingDir, dirPath);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function handleDetectTools(
|
|
44
|
+
ctx: HandlerContext,
|
|
45
|
+
ws: WSContext,
|
|
46
|
+
msg: WebSocketMessage,
|
|
47
|
+
workingDir: string,
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
const dirPath = resolvePath(workingDir, msg.data?.path);
|
|
50
|
+
try {
|
|
51
|
+
const { tools, ecosystem } = await detectTools(dirPath);
|
|
52
|
+
ctx.send(ws, {
|
|
53
|
+
type: 'qualityToolsDetected',
|
|
54
|
+
data: { path: msg.data?.path || '.', tools, ecosystem },
|
|
55
|
+
});
|
|
56
|
+
} catch (error) {
|
|
57
|
+
ctx.send(ws, {
|
|
58
|
+
type: 'qualityError',
|
|
59
|
+
data: { path: msg.data?.path || '.', error: error instanceof Error ? error.message : String(error) },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function handleScan(
|
|
65
|
+
ctx: HandlerContext,
|
|
66
|
+
ws: WSContext,
|
|
67
|
+
msg: WebSocketMessage,
|
|
68
|
+
workingDir: string,
|
|
69
|
+
): Promise<void> {
|
|
70
|
+
const dirPath = resolvePath(workingDir, msg.data?.path);
|
|
71
|
+
const reportPath = msg.data?.path || '.';
|
|
72
|
+
|
|
73
|
+
try {
|
|
74
|
+
const results = await runQualityScan(dirPath, (progress) => {
|
|
75
|
+
ctx.send(ws, {
|
|
76
|
+
type: 'qualityScanProgress',
|
|
77
|
+
data: { path: reportPath, progress },
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
ctx.send(ws, {
|
|
81
|
+
type: 'qualityScanResults',
|
|
82
|
+
data: { path: reportPath, results },
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
ctx.send(ws, {
|
|
86
|
+
type: 'qualityError',
|
|
87
|
+
data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function handleInstallTools(
|
|
93
|
+
ctx: HandlerContext,
|
|
94
|
+
ws: WSContext,
|
|
95
|
+
msg: WebSocketMessage,
|
|
96
|
+
workingDir: string,
|
|
97
|
+
): Promise<void> {
|
|
98
|
+
const dirPath = resolvePath(workingDir, msg.data?.path);
|
|
99
|
+
const reportPath = msg.data?.path || '.';
|
|
100
|
+
const toolNames: string[] | undefined = msg.data?.tools;
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
ctx.send(ws, {
|
|
104
|
+
type: 'qualityInstallProgress',
|
|
105
|
+
data: { path: reportPath, installing: true },
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const { tools, ecosystem } = await installTools(dirPath, toolNames);
|
|
109
|
+
|
|
110
|
+
ctx.send(ws, {
|
|
111
|
+
type: 'qualityInstallComplete',
|
|
112
|
+
data: { path: reportPath, tools, ecosystem },
|
|
113
|
+
});
|
|
114
|
+
} catch (error) {
|
|
115
|
+
ctx.send(ws, {
|
|
116
|
+
type: 'qualityError',
|
|
117
|
+
data: { path: reportPath, error: error instanceof Error ? error.message : String(error) },
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function handleCodeReview(
|
|
123
|
+
ctx: HandlerContext,
|
|
124
|
+
ws: WSContext,
|
|
125
|
+
msg: WebSocketMessage,
|
|
126
|
+
_workingDir: string,
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
const reportPath = msg.data?.path || '.';
|
|
129
|
+
|
|
130
|
+
// Code review via headless Claude will be implemented in a follow-up.
|
|
131
|
+
// For now, send an empty result to unblock the UI.
|
|
132
|
+
ctx.send(ws, {
|
|
133
|
+
type: 'qualityCodeReview',
|
|
134
|
+
data: {
|
|
135
|
+
path: reportPath,
|
|
136
|
+
findings: [],
|
|
137
|
+
summary: 'Code review requires a Claude Code session. Run from the Chat view for a detailed code review.',
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
}
|