@probelabs/probe 0.6.0-rc178 → 0.6.0-rc186
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/build/agent/ProbeAgent.js +17 -1
- package/build/agent/bashCommandUtils.js +63 -0
- package/build/agent/bashExecutor.js +34 -19
- package/build/agent/bashPermissions.js +74 -13
- package/build/agent/index.js +185 -37
- package/cjs/agent/ProbeAgent.cjs +2798 -1843
- package/cjs/index.cjs +2798 -1843
- package/package.json +1 -1
- package/src/agent/ProbeAgent.js +17 -1
- package/src/agent/bashCommandUtils.js +63 -0
- package/src/agent/bashExecutor.js +34 -19
- package/src/agent/bashPermissions.js +74 -13
package/package.json
CHANGED
package/src/agent/ProbeAgent.js
CHANGED
|
@@ -2501,10 +2501,26 @@ When troubleshooting:
|
|
|
2501
2501
|
// Execute native tool
|
|
2502
2502
|
try {
|
|
2503
2503
|
// Add sessionId and workingDirectory to params for tool execution
|
|
2504
|
+
// Validate and resolve workingDirectory
|
|
2505
|
+
let resolvedWorkingDirectory = (this.allowedFolders && this.allowedFolders[0]) || process.cwd();
|
|
2506
|
+
if (params.workingDirectory) {
|
|
2507
|
+
const requestedDir = resolve(params.workingDirectory);
|
|
2508
|
+
// Check if the requested directory is within allowed folders
|
|
2509
|
+
const isWithinAllowed = !this.allowedFolders || this.allowedFolders.length === 0 ||
|
|
2510
|
+
this.allowedFolders.some(folder => {
|
|
2511
|
+
const resolvedFolder = resolve(folder);
|
|
2512
|
+
return requestedDir === resolvedFolder || requestedDir.startsWith(resolvedFolder + sep);
|
|
2513
|
+
});
|
|
2514
|
+
if (isWithinAllowed) {
|
|
2515
|
+
resolvedWorkingDirectory = requestedDir;
|
|
2516
|
+
} else if (this.debug) {
|
|
2517
|
+
console.error(`[DEBUG] Rejected workingDirectory "${params.workingDirectory}" - not within allowed folders`);
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2504
2520
|
const toolParams = {
|
|
2505
2521
|
...params,
|
|
2506
2522
|
sessionId: this.sessionId,
|
|
2507
|
-
workingDirectory:
|
|
2523
|
+
workingDirectory: resolvedWorkingDirectory
|
|
2508
2524
|
};
|
|
2509
2525
|
|
|
2510
2526
|
// Log tool execution in debug mode
|
|
@@ -159,6 +159,69 @@ export function isComplexCommand(command) {
|
|
|
159
159
|
return result.isComplex;
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Check if a pattern is a complex pattern (contains shell operators)
|
|
164
|
+
* Complex patterns are used to match full command strings including operators
|
|
165
|
+
* @param {string} pattern - Pattern to check
|
|
166
|
+
* @returns {boolean} True if pattern contains shell operators
|
|
167
|
+
*/
|
|
168
|
+
export function isComplexPattern(pattern) {
|
|
169
|
+
if (!pattern || typeof pattern !== 'string') return false;
|
|
170
|
+
|
|
171
|
+
// Check for operators in the pattern (aligned with complexPatterns in parseSimpleCommand)
|
|
172
|
+
const operatorPatterns = [
|
|
173
|
+
/\|/, // Pipes
|
|
174
|
+
/&&/, // Logical AND
|
|
175
|
+
/\|\|/, // Logical OR
|
|
176
|
+
/;/, // Command separator
|
|
177
|
+
/&$/, // Background execution
|
|
178
|
+
/\$\(/, // Command substitution $()
|
|
179
|
+
/`/, // Command substitution ``
|
|
180
|
+
/>/, // Redirection >
|
|
181
|
+
/</, // Redirection <
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
return operatorPatterns.some(p => p.test(pattern));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Convert a glob-style pattern to regex for matching
|
|
189
|
+
* Supports * as wildcard (matches any characters except operators)
|
|
190
|
+
* @param {string} pattern - Glob pattern
|
|
191
|
+
* @returns {RegExp} Compiled regex
|
|
192
|
+
*/
|
|
193
|
+
function globToRegex(pattern) {
|
|
194
|
+
// Escape regex special characters except *
|
|
195
|
+
let escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
|
|
196
|
+
// Convert * to .*? (non-greedy match)
|
|
197
|
+
escaped = escaped.replace(/\*/g, '.*?');
|
|
198
|
+
// Make it match the full string
|
|
199
|
+
return new RegExp('^' + escaped + '$', 'i');
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Match a command string against a complex pattern
|
|
204
|
+
* Complex patterns use glob-style wildcards (*) for matching
|
|
205
|
+
* @param {string} command - Full command string
|
|
206
|
+
* @param {string} pattern - Complex pattern with wildcards
|
|
207
|
+
* @returns {boolean} True if command matches the pattern
|
|
208
|
+
*/
|
|
209
|
+
export function matchesComplexPattern(command, pattern) {
|
|
210
|
+
if (!command || !pattern) return false;
|
|
211
|
+
|
|
212
|
+
// Normalize whitespace
|
|
213
|
+
const normalizedCommand = command.trim().replace(/\s+/g, ' ');
|
|
214
|
+
const normalizedPattern = pattern.trim().replace(/\s+/g, ' ');
|
|
215
|
+
|
|
216
|
+
try {
|
|
217
|
+
const regex = globToRegex(normalizedPattern);
|
|
218
|
+
return regex.test(normalizedCommand);
|
|
219
|
+
} catch (e) {
|
|
220
|
+
// If regex fails, fall back to exact match
|
|
221
|
+
return normalizedCommand === normalizedPattern;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
162
225
|
/**
|
|
163
226
|
* Legacy compatibility function - parses command for permission checking
|
|
164
227
|
* @param {string} command - Command to parse
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { spawn } from 'child_process';
|
|
7
7
|
import { resolve, join } from 'path';
|
|
8
8
|
import { existsSync } from 'fs';
|
|
9
|
-
import { parseCommandForExecution } from './bashCommandUtils.js';
|
|
9
|
+
import { parseCommandForExecution, isComplexCommand } from './bashCommandUtils.js';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Execute a bash command with security controls
|
|
@@ -63,31 +63,46 @@ export async function executeBashCommand(command, options = {}) {
|
|
|
63
63
|
...env
|
|
64
64
|
};
|
|
65
65
|
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
const args = parseCommandForExecution(command);
|
|
69
|
-
if (!args || args.length === 0) {
|
|
70
|
-
resolve({
|
|
71
|
-
success: false,
|
|
72
|
-
error: 'Failed to parse command',
|
|
73
|
-
stdout: '',
|
|
74
|
-
stderr: '',
|
|
75
|
-
exitCode: 1,
|
|
76
|
-
command,
|
|
77
|
-
workingDirectory: cwd,
|
|
78
|
-
duration: Date.now() - startTime
|
|
79
|
-
});
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
66
|
+
// Check if this is a complex command (contains pipes, operators, etc.)
|
|
67
|
+
const isComplex = isComplexCommand(command);
|
|
82
68
|
|
|
83
|
-
|
|
69
|
+
let cmd, cmdArgs, useShell;
|
|
70
|
+
|
|
71
|
+
if (isComplex) {
|
|
72
|
+
// For complex commands, use sh -c to execute through shell
|
|
73
|
+
// This is only reached if the permission checker allowed the complex command
|
|
74
|
+
cmd = 'sh';
|
|
75
|
+
cmdArgs = ['-c', command];
|
|
76
|
+
useShell = false; // We explicitly use sh -c, not spawn's shell option
|
|
77
|
+
if (debug) {
|
|
78
|
+
console.log(`[BashExecutor] Complex command - using sh -c`);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
// Parse simple command for direct execution
|
|
82
|
+
const args = parseCommandForExecution(command);
|
|
83
|
+
if (!args || args.length === 0) {
|
|
84
|
+
resolve({
|
|
85
|
+
success: false,
|
|
86
|
+
error: 'Failed to parse command',
|
|
87
|
+
stdout: '',
|
|
88
|
+
stderr: '',
|
|
89
|
+
exitCode: 1,
|
|
90
|
+
command,
|
|
91
|
+
workingDirectory: cwd,
|
|
92
|
+
duration: Date.now() - startTime
|
|
93
|
+
});
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
[cmd, ...cmdArgs] = args;
|
|
97
|
+
useShell = false;
|
|
98
|
+
}
|
|
84
99
|
|
|
85
100
|
// Spawn the process
|
|
86
101
|
const child = spawn(cmd, cmdArgs, {
|
|
87
102
|
cwd,
|
|
88
103
|
env: processEnv,
|
|
89
104
|
stdio: ['ignore', 'pipe', 'pipe'], // stdin ignored, capture stdout/stderr
|
|
90
|
-
shell:
|
|
105
|
+
shell: useShell, // false for security
|
|
91
106
|
windowsHide: true
|
|
92
107
|
});
|
|
93
108
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { DEFAULT_ALLOW_PATTERNS, DEFAULT_DENY_PATTERNS } from './bashDefaults.js';
|
|
7
|
-
import { parseCommand, isComplexCommand } from './bashCommandUtils.js';
|
|
7
|
+
import { parseCommand, isComplexCommand, isComplexPattern, matchesComplexPattern } from './bashCommandUtils.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Check if a pattern matches a parsed command
|
|
@@ -125,7 +125,7 @@ export class BashPermissionChecker {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
/**
|
|
128
|
-
* Check if a simple command is allowed (
|
|
128
|
+
* Check if a simple command is allowed (complex commands allowed if they match patterns)
|
|
129
129
|
* @param {string} command - Command to check
|
|
130
130
|
* @returns {Object} Permission result
|
|
131
131
|
*/
|
|
@@ -138,19 +138,17 @@ export class BashPermissionChecker {
|
|
|
138
138
|
};
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
//
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
isComplex: true
|
|
148
|
-
};
|
|
141
|
+
// Check if this is a complex command
|
|
142
|
+
const commandIsComplex = isComplexCommand(command);
|
|
143
|
+
|
|
144
|
+
if (commandIsComplex) {
|
|
145
|
+
// For complex commands, check against complex patterns in allow/deny lists
|
|
146
|
+
return this._checkComplexCommand(command);
|
|
149
147
|
}
|
|
150
148
|
|
|
151
149
|
// Parse the simple command
|
|
152
150
|
const parsed = parseCommand(command);
|
|
153
|
-
|
|
151
|
+
|
|
154
152
|
if (parsed.error) {
|
|
155
153
|
return {
|
|
156
154
|
allowed: false,
|
|
@@ -203,14 +201,77 @@ export class BashPermissionChecker {
|
|
|
203
201
|
parsed: parsed,
|
|
204
202
|
isComplex: false
|
|
205
203
|
};
|
|
206
|
-
|
|
204
|
+
|
|
207
205
|
if (this.debug) {
|
|
208
206
|
console.log(`[BashPermissions] ALLOWED - command passed all checks`);
|
|
209
207
|
}
|
|
210
|
-
|
|
208
|
+
|
|
211
209
|
return result;
|
|
212
210
|
}
|
|
213
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Check a complex command against complex patterns in allow/deny lists
|
|
214
|
+
* @private
|
|
215
|
+
* @param {string} command - Complex command to check
|
|
216
|
+
* @returns {Object} Permission result
|
|
217
|
+
*/
|
|
218
|
+
_checkComplexCommand(command) {
|
|
219
|
+
if (this.debug) {
|
|
220
|
+
console.log(`[BashPermissions] Checking complex command: "${command}"`);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Get complex patterns from allow and deny lists
|
|
224
|
+
const complexAllowPatterns = this.allowPatterns.filter(p => isComplexPattern(p));
|
|
225
|
+
const complexDenyPatterns = this.denyPatterns.filter(p => isComplexPattern(p));
|
|
226
|
+
|
|
227
|
+
if (this.debug) {
|
|
228
|
+
console.log(`[BashPermissions] Complex allow patterns: ${complexAllowPatterns.length}`);
|
|
229
|
+
console.log(`[BashPermissions] Complex deny patterns: ${complexDenyPatterns.length}`);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Check deny patterns first (deny takes precedence)
|
|
233
|
+
for (const pattern of complexDenyPatterns) {
|
|
234
|
+
if (matchesComplexPattern(command, pattern)) {
|
|
235
|
+
if (this.debug) {
|
|
236
|
+
console.log(`[BashPermissions] DENIED - matches complex deny pattern: ${pattern}`);
|
|
237
|
+
}
|
|
238
|
+
return {
|
|
239
|
+
allowed: false,
|
|
240
|
+
reason: `Command matches deny pattern: ${pattern}`,
|
|
241
|
+
command: command,
|
|
242
|
+
isComplex: true,
|
|
243
|
+
matchedPatterns: [pattern]
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Check allow patterns
|
|
249
|
+
for (const pattern of complexAllowPatterns) {
|
|
250
|
+
if (matchesComplexPattern(command, pattern)) {
|
|
251
|
+
if (this.debug) {
|
|
252
|
+
console.log(`[BashPermissions] ALLOWED - matches complex allow pattern: ${pattern}`);
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
allowed: true,
|
|
256
|
+
command: command,
|
|
257
|
+
isComplex: true,
|
|
258
|
+
matchedPattern: pattern
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// No matching complex pattern found - reject complex command
|
|
264
|
+
if (this.debug) {
|
|
265
|
+
console.log(`[BashPermissions] DENIED - no matching complex pattern found`);
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
allowed: false,
|
|
269
|
+
reason: 'Complex shell commands require explicit allow patterns (e.g., "cd * && git *")',
|
|
270
|
+
command: command,
|
|
271
|
+
isComplex: true
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
214
275
|
/**
|
|
215
276
|
* Get configuration summary
|
|
216
277
|
* @returns {Object} Configuration info
|