-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote-agent.js
More file actions
427 lines (364 loc) · 14.7 KB
/
Copy pathremote-agent.js
File metadata and controls
427 lines (364 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// ==========================================
// Remote Agent — execute real work via WhatsApp/Telegram/Teams
// ==========================================
// Receives task messages, uses AI to plan steps,
// executes file operations & terminal commands,
// and reports results back on the messaging platform.
import fs from 'fs';
import path from 'path';
import { execFileSync } from 'child_process';
import config from './config.js';
import { chatWithFailover } from './failover.js';
import { auditLog } from './audit-log.js';
import { getHardenedSystemPrompt } from './jailbreak-defense.js';
// ---- Configuration ----
const WORKSPACE = path.resolve(config.remoteAgent.workspace || '.');
const MAX_EXEC_TIMEOUT_MS = 30_000; // 30 seconds per command
const MAX_OUTPUT_LENGTH = 3000; // truncate command output for WhatsApp readability
const MAX_STEPS = 10; // max actions per task
// Commands that are never allowed
const BLOCKED_COMMANDS = [
'format', 'diskpart', 'shutdown', 'reboot', 'poweroff',
'rm -rf /', 'del /f /s /q c:', 'mkfs',
'dd if=', 'curl | bash', 'wget | bash',
':(){:|:&};:', 'fork bomb',
'npm publish', 'npm unpublish', 'git push --force',
'DROP DATABASE', 'DROP TABLE', 'TRUNCATE',
];
// Shell metacharacters that indicate injection attempts
const SHELL_METACHAR_RE = /[;&|`$(){}]|&&|\|\||\$\(|<\(|>\(/;
// Allowed first-word commands (whitelist)
const ALLOWED_EXECUTABLES = new Set([
'node', 'npm', 'npx', 'git', 'ls', 'dir', 'cat', 'head', 'tail',
'echo', 'grep', 'find', 'pwd', 'cd', 'mkdir', 'cp', 'mv',
'python', 'python3', 'pip', 'pip3',
'tsc', 'eslint', 'prettier', 'jest', 'vitest', 'mocha',
'docker', 'docker-compose',
'curl', 'wget', // allowed standalone (not piped)
]);
// File paths that are never writable
const PROTECTED_PATHS = [
'.env', '.git', 'node_modules', 'auth_info',
'package-lock.json',
];
// ---- Task State ----
// Pending confirmations: sender -> { task, steps, timestamp }
const pendingConfirmations = new Map();
// ---- System Prompt for Task Planning ----
const TASK_PLANNER_PROMPT = getHardenedSystemPrompt(`You are ${config.agent.name}, a remote coding agent. The user sends you tasks via WhatsApp/Telegram/Teams and you execute them on their machine.
You MUST respond with a JSON plan. Do NOT include any text outside the JSON block.
Respond with a JSON array of steps:
[
{"action": "read", "path": "relative/path/to/file"},
{"action": "write", "path": "relative/path/to/file", "content": "full file content"},
{"action": "edit", "path": "relative/path/to/file", "find": "exact text to find", "replace": "replacement text"},
{"action": "run", "command": "shell command to execute"},
{"action": "mkdir", "path": "relative/path/to/dir"},
{"action": "delete", "path": "relative/path/to/file"},
{"action": "list", "path": "relative/path/to/dir"}
]
Rules:
- Paths are relative to the workspace root: ${WORKSPACE}
- NEVER modify .env, .git, node_modules, or auth_info
- NEVER run destructive system commands (format, shutdown, rm -rf /, etc.)
- Keep commands short and safe
- For edits, provide exact "find" text that uniquely identifies the location
- For writes, provide the COMPLETE file content
- Maximum ${MAX_STEPS} steps per task
- If the task is unclear, return: [{"action": "ask", "question": "your clarifying question"}]
Current workspace: ${WORKSPACE}
Current date: ${new Date().toISOString().split('T')[0]}`);
// ---- Security Checks ----
function isRemoteAgentAllowed(sender) {
const allowlist = config.remoteAgent.allowlist;
if (!allowlist || allowlist.length === 0) return false; // DENY by default
return allowlist.includes(sender);
}
function isCommandBlocked(command) {
const lower = command.toLowerCase();
// Blacklist check
if (BLOCKED_COMMANDS.some(blocked => lower.includes(blocked.toLowerCase()))) return true;
// Shell metacharacter injection check
if (SHELL_METACHAR_RE.test(command)) return true;
// Whitelist check: first word must be an allowed executable
const firstWord = command.trim().split(/\s+/)[0].toLowerCase();
if (!ALLOWED_EXECUTABLES.has(firstWord)) return true;
return false;
}
function isPathProtected(filePath) {
const normalized = filePath.replace(/\\/g, '/');
return PROTECTED_PATHS.some(p => normalized.includes(p));
}
function isPathInWorkspace(filePath) {
const resolved = path.resolve(WORKSPACE, filePath);
return resolved.startsWith(WORKSPACE);
}
function validateStep(step) {
if (step.action === 'run' && isCommandBlocked(step.command)) {
return { valid: false, reason: `Blocked command: ${step.command}` };
}
if (step.path) {
if (isPathProtected(step.path)) {
return { valid: false, reason: `Protected path: ${step.path}` };
}
if (!isPathInWorkspace(step.path)) {
return { valid: false, reason: `Path outside workspace: ${step.path}` };
}
}
if (step.action === 'delete') {
return { valid: true, requiresConfirmation: true };
}
if (step.action === 'run') {
return { valid: true, requiresConfirmation: true };
}
return { valid: true };
}
// ---- Step Executors ----
function executeRead(step) {
const full = path.resolve(WORKSPACE, step.path);
if (!fs.existsSync(full)) return { success: false, output: `File not found: ${step.path}` };
const content = fs.readFileSync(full, 'utf8');
const truncated = content.length > MAX_OUTPUT_LENGTH
? content.slice(0, MAX_OUTPUT_LENGTH) + `\n... (${content.length} chars total, truncated)`
: content;
return { success: true, output: truncated };
}
function executeWrite(step) {
const full = path.resolve(WORKSPACE, step.path);
const dir = path.dirname(full);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(full, step.content, 'utf8');
return { success: true, output: `Wrote ${step.content.length} chars to ${step.path}` };
}
function executeEdit(step) {
const full = path.resolve(WORKSPACE, step.path);
if (!fs.existsSync(full)) return { success: false, output: `File not found: ${step.path}` };
let content = fs.readFileSync(full, 'utf8');
if (!content.includes(step.find)) {
return { success: false, output: `Could not find text to replace in ${step.path}` };
}
content = content.replace(step.find, step.replace);
fs.writeFileSync(full, content, 'utf8');
return { success: true, output: `Edited ${step.path}` };
}
function executeRun(step) {
try {
// Split command into executable + args (no shell interpretation)
const parts = step.command.trim().split(/\s+/);
const cmd = parts[0];
const args = parts.slice(1);
const output = execFileSync(cmd, args, {
cwd: WORKSPACE,
timeout: MAX_EXEC_TIMEOUT_MS,
encoding: 'utf8',
maxBuffer: 1024 * 1024, // 1MB
stdio: ['pipe', 'pipe', 'pipe'],
});
const truncated = output.length > MAX_OUTPUT_LENGTH
? output.slice(0, MAX_OUTPUT_LENGTH) + '\n... (truncated)'
: output;
return { success: true, output: truncated || '(no output)' };
} catch (err) {
const stderr = err.stderr?.slice(0, MAX_OUTPUT_LENGTH) || err.message;
return { success: false, output: `Command failed: ${stderr}` };
}
}
function executeMkdir(step) {
const full = path.resolve(WORKSPACE, step.path);
fs.mkdirSync(full, { recursive: true });
return { success: true, output: `Created directory: ${step.path}` };
}
function executeDelete(step) {
const full = path.resolve(WORKSPACE, step.path);
if (!fs.existsSync(full)) return { success: false, output: `Not found: ${step.path}` };
const stat = fs.statSync(full);
if (stat.isDirectory()) {
fs.rmSync(full, { recursive: true });
} else {
fs.unlinkSync(full);
}
return { success: true, output: `Deleted: ${step.path}` };
}
function executeList(step) {
const full = path.resolve(WORKSPACE, step.path || '.');
if (!fs.existsSync(full)) return { success: false, output: `Not found: ${step.path}` };
const items = fs.readdirSync(full, { withFileTypes: true });
const listing = items.map(i => `${i.isDirectory() ? '📁' : '📄'} ${i.name}`).join('\n');
return { success: true, output: listing || '(empty directory)' };
}
const EXECUTORS = {
read: executeRead,
write: executeWrite,
edit: executeEdit,
run: executeRun,
mkdir: executeMkdir,
delete: executeDelete,
list: executeList,
};
// ---- Main Handler ----
/**
* Check if a message is a remote agent task.
* Tasks start with "!do " or "!task ".
*/
export function isRemoteTask(text) {
if (!config.remoteAgent.enabled) return false;
const lower = text.toLowerCase().trim();
return lower.startsWith('!do ') || lower.startsWith('!task ');
}
/**
* Handle a remote agent task from a messaging platform.
* Returns the response string to send back.
*/
export async function handleRemoteTask(sender, text, client) {
// Security: only allowlisted senders
if (!isRemoteAgentAllowed(sender)) {
auditLog('BLOCK', 'remote-agent-denied', { sender });
return '⛔ Remote agent access denied. Your number is not in REMOTE_AGENT_ALLOWLIST.';
}
// Check for confirmation response
if (pendingConfirmations.has(sender)) {
const lower = text.toLowerCase().trim();
if (lower === '!do yes' || lower === '!task yes' || lower === 'yes' || lower === 'y') {
return await executeConfirmedTask(sender);
} else {
pendingConfirmations.delete(sender);
return '❌ Task cancelled.';
}
}
// Extract the task description
const task = text.replace(/^!(do|task)\s+/i, '').trim();
if (!task) return 'Usage: !do <describe what you want me to do>';
auditLog('INFO', 'remote-task-received', { sender, task: task.slice(0, 200) });
// Get workspace context for AI
let workspaceContext = '';
try {
const items = fs.readdirSync(WORKSPACE, { withFileTypes: true });
workspaceContext = '\n\nWorkspace files:\n' + items
.slice(0, 50)
.map(i => `${i.isDirectory() ? '📁' : '📄'} ${i.name}`)
.join('\n');
} catch { /* ignore */ }
// Ask AI to plan the steps
const planMessages = [
{ role: 'system', content: TASK_PLANNER_PROMPT },
{ role: 'user', content: `Task: ${task}${workspaceContext}` },
];
await client.sendMessage(sender, '🔧 Planning task...');
let planResponse;
try {
planResponse = await chatWithFailover(planMessages);
} catch (err) {
return `❌ Failed to plan task: ${err.message}`;
}
// Parse the plan
let steps;
try {
// Extract JSON from response (may be wrapped in ```json blocks)
const jsonMatch = planResponse.match(/```(?:json)?\s*([\s\S]*?)```/);
const raw = jsonMatch ? jsonMatch[1].trim() : planResponse.trim();
steps = JSON.parse(raw);
if (!Array.isArray(steps)) steps = [steps];
} catch {
return `❌ AI returned an invalid plan. Try rephrasing your task.\n\nRaw response:\n${planResponse.slice(0, 500)}`;
}
// Handle clarifying questions
if (steps.length === 1 && steps[0].action === 'ask') {
return `❓ ${steps[0].question}`;
}
// Limit steps
if (steps.length > MAX_STEPS) {
return `❌ Task too complex (${steps.length} steps, max ${MAX_STEPS}). Break it into smaller tasks.`;
}
// Validate all steps
let needsConfirmation = false;
const stepSummary = [];
for (let i = 0; i < steps.length; i++) {
const step = steps[i];
const validation = validateStep(step);
if (!validation.valid) {
auditLog('BLOCK', 'remote-step-blocked', { sender, step: step.action, reason: validation.reason });
return `⛔ Step ${i + 1} blocked: ${validation.reason}`;
}
if (validation.requiresConfirmation) needsConfirmation = true;
// Build summary
switch (step.action) {
case 'read': stepSummary.push(`${i + 1}. 📖 Read ${step.path}`); break;
case 'write': stepSummary.push(`${i + 1}. ✏️ Write ${step.path} (${step.content?.length || 0} chars)`); break;
case 'edit': stepSummary.push(`${i + 1}. 🔧 Edit ${step.path}`); break;
case 'run': stepSummary.push(`${i + 1}. ▶️ Run: ${step.command}`); break;
case 'mkdir': stepSummary.push(`${i + 1}. 📁 Create dir: ${step.path}`); break;
case 'delete': stepSummary.push(`${i + 1}. 🗑️ Delete: ${step.path}`); break;
case 'list': stepSummary.push(`${i + 1}. 📋 List: ${step.path || '.'}`); break;
default: stepSummary.push(`${i + 1}. ❓ Unknown: ${step.action}`);
}
}
// If destructive actions, ask for confirmation
if (needsConfirmation) {
pendingConfirmations.set(sender, {
steps,
task,
timestamp: Date.now(),
});
// Auto-expire after 5 minutes
setTimeout(() => pendingConfirmations.delete(sender), 5 * 60 * 1000);
return `⚠️ This task includes commands or deletions that need your approval.\n\n*Plan:*\n${stepSummary.join('\n')}\n\nReply *yes* to proceed or anything else to cancel.`;
}
// Execute immediately (safe actions only)
return await executeSteps(sender, steps, stepSummary);
}
async function executeConfirmedTask(sender) {
const pending = pendingConfirmations.get(sender);
pendingConfirmations.delete(sender);
if (!pending) return '❌ No pending task found.';
// Check if expired (5 min)
if (Date.now() - pending.timestamp > 5 * 60 * 1000) {
return '❌ Task expired. Please submit it again.';
}
const stepSummary = pending.steps.map((step, i) => {
switch (step.action) {
case 'run': return `${i + 1}. ▶️ Run: ${step.command}`;
case 'delete': return `${i + 1}. 🗑️ Delete: ${step.path}`;
default: return `${i + 1}. ${step.action}: ${step.path || step.command || ''}`;
}
});
return await executeSteps(sender, pending.steps, stepSummary);
}
async function executeSteps(sender, steps, stepSummary) {
auditLog('INFO', 'remote-task-executing', { sender, stepCount: steps.length });
const results = [];
let allSuccess = true;
for (let i = 0; i < steps.length; i++) {
const step = steps[i];
const executor = EXECUTORS[step.action];
if (!executor) {
results.push(`${i + 1}. ❌ Unknown action: ${step.action}`);
allSuccess = false;
break;
}
try {
const result = executor(step);
const icon = result.success ? '✅' : '❌';
results.push(`${i + 1}. ${icon} ${result.output}`);
if (!result.success) {
allSuccess = false;
break; // Stop on first failure
}
} catch (err) {
results.push(`${i + 1}. ❌ Error: ${err.message}`);
allSuccess = false;
break;
}
}
const status = allSuccess ? '✅ Task completed!' : '⚠️ Task partially completed (stopped on error)';
auditLog('INFO', 'remote-task-done', { sender, success: allSuccess });
return `${status}\n\n${results.join('\n\n')}`;
}
// Clean up expired confirmations every 10 minutes
setInterval(() => {
const now = Date.now();
for (const [sender, pending] of pendingConfirmations) {
if (now - pending.timestamp > 5 * 60 * 1000) {
pendingConfirmations.delete(sender);
}
}
}, 10 * 60 * 1000).unref();