Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
security: fix command injection vulnerabilities
Adam Eivy7ea02efdc2b6
diff --git a/server/routes/apps.js b/server/routes/apps.jsindex 21d02c8df1..0ea4902246 100644--- a/server/routes/apps.js+++ b/server/routes/apps.js@@ -235,6 +235,29 @@ router.get('/:id/logs', asyncHandler(async (req, res, next) => {res.json({ processName, lines, logs });}));+// Allowlist of safe editor commands+// Security: Only allow known-safe editor commands to prevent arbitrary code execution+const ALLOWED_EDITORS = new Set([+ 'code', // VS Code+ 'cursor', // Cursor+ 'zed', // Zed+ 'subl', // Sublime Text+ 'atom', // Atom+ 'vim', // Vim+ 'nvim', // Neovim+ 'nano', // Nano+ 'emacs', // Emacs+ 'idea', // IntelliJ IDEA+ 'pycharm', // PyCharm+ 'webstorm', // WebStorm+ 'phpstorm', // PhpStorm+ 'rubymine', // RubyMine+ 'goland', // GoLand+ 'clion', // CLion+ 'rider', // Rider+ 'studio' // Android Studio+]);+// POST /api/apps/:id/open-editor - Open app in editorrouter.post('/:id/open-editor', asyncHandler(async (req, res, next) => {const app = await appsService.getAppById(req.params.id);@@ -250,11 +273,33 @@ router.post('/:id/open-editor', asyncHandler(async (req, res, next) => {const editorCommand = app.editorCommand || 'code .';const [cmd, ...args] = editorCommand.split(/\s+/);+ // Security: Validate that the editor command is in our allowlist+ // This prevents arbitrary command execution via malicious editorCommand values+ if (!ALLOWED_EDITORS.has(cmd)) {+ throw new ServerError(`Editor '${cmd}' is not in the allowed editors list`, {+ status: 400,+ code: 'INVALID_EDITOR',+ context: { allowedEditors: Array.from(ALLOWED_EDITORS) }+ });+ }++ // Security: Validate args don't contain shell metacharacters+ const DANGEROUS_CHARS = /[;|&`$(){}[\]<>\\!#*?~]/;+ for (const arg of args) {+ if (DANGEROUS_CHARS.test(arg)) {+ throw new ServerError('Editor arguments contain disallowed characters', {+ status: 400,+ code: 'INVALID_EDITOR_ARGS'+ });+ }+ }+// Spawn the editor process detached so it doesn't blockconst child = spawn(cmd, args, {cwd: app.repoPath,detached: true,- stdio: 'ignore'+ stdio: 'ignore',+ shell: false // Security: Ensure no shell interpretation});child.unref();diff --git a/server/routes/logs.js b/server/routes/logs.jsindex ae8f61861c..da3756018b 100644--- a/server/routes/logs.js+++ b/server/routes/logs.js@@ -6,6 +6,22 @@ import { asyncHandler, ServerError } from '../lib/errorHandler.js';const router = Router();+/**+ * Validate PM2 process name to prevent command injection+ * PM2 names can contain alphanumeric, hyphens, underscores, and dots+ */+function validateProcessName(name) {+ if (typeof name !== 'string' || !name) {+ return null;+ }+ // Only allow safe characters for PM2 process names+ // Reject any shell metacharacters+ if (!/^[a-zA-Z0-9._-]+$/.test(name)) {+ return null;+ }+ return name;+}+// GET /api/logs/processes - List all PM2 processes for log selectionrouter.get('/processes', asyncHandler(async (req, res) => {const processes = await pm2Service.listProcesses().catch(() => []);@@ -18,11 +34,17 @@ router.get('/:processName', asyncHandler(async (req, res) => {const lines = parseInt(req.query.lines) || 100;const follow = req.query.follow === 'true';+ // Security: Validate process name to prevent command injection+ const safeProcessName = validateProcessName(processName);+ if (!safeProcessName) {+ throw new ServerError('Invalid process name', { status: 400, code: 'INVALID_PROCESS_NAME' });+ }+if (!follow) {// Static log fetch- const logs = await pm2Service.getLogs(processName, lines)+ const logs = await pm2Service.getLogs(safeProcessName, lines).catch(err => `Error: ${err.message}`);- return res.json({ processName, lines, logs });+ return res.json({ processName: safeProcessName, lines, logs });}// SSE streaming@@ -32,10 +54,11 @@ router.get('/:processName', asyncHandler(async (req, res) => {res.setHeader('X-Accel-Buffering', 'no');// Send initial connection event- res.write(`event: connected\ndata: ${JSON.stringify({ processName, timestamp: Date.now() })}\n\n`);+ res.write(`event: connected\ndata: ${JSON.stringify({ processName: safeProcessName, timestamp: Date.now() })}\n\n`);// Spawn pm2 logs with --raw flag for clean output- const logProcess = spawn('pm2', ['logs', processName, '--raw', '--lines', String(lines)], {+ // Security: safeProcessName is validated above to only contain safe characters+ const logProcess = spawn('pm2', ['logs', safeProcessName, '--raw', '--lines', String(lines)], {shell: false});diff --git a/server/services/agents.js b/server/services/agents.jsindex a11ea9c1fd..ed9f045319 100644--- a/server/services/agents.js+++ b/server/services/agents.js@@ -90,13 +90,37 @@ async function findProcesses(pattern) {return [];}+/**+ * Validate pattern to prevent command injection+ * Only allows alphanumeric characters, hyphens, and underscores+ */+function validatePattern(pattern) {+ if (typeof pattern !== 'string' || !pattern) {+ return null;+ }+ // Only allow safe characters for process name matching+ // Reject any shell metacharacters+ if (!/^[a-zA-Z0-9_-]+$/.test(pattern)) {+ return null;+ }+ return pattern;+}+/*** Find processes on Unix-like systems (macOS, Linux)*/async function findUnixProcesses(pattern) {+ // Security: Validate pattern to prevent command injection+ const safePattern = validatePattern(pattern);+ if (!safePattern) {+ console.warn(`⚠️ Invalid process pattern rejected: ${pattern}`);+ return [];+ }+// ps command to get process info// -e: all processes, -o: output format, -ww: unlimited width (no truncation)- const cmd = `ps -ww -eo pid,ppid,%cpu,%mem,etime,command | grep -i "${pattern}" | grep -v grep`;+ // Security: Pattern is validated above to only contain safe characters+ const cmd = `ps -ww -eo pid,ppid,%cpu,%mem,etime,command | grep -i "${safePattern}" | grep -v grep`;const result = await execAsync(cmd, { maxBuffer: 10 * 1024 * 1024 }).catch(() => ({ stdout: '' }));@@ -139,7 +163,15 @@ async function findUnixProcesses(pattern) {* Find processes on Windows*/async function findWindowsProcesses(pattern) {- const cmd = `wmic process where "name like '%${pattern}%'" get ProcessId,ParentProcessId,PercentProcessorTime,WorkingSetSize,CreationDate,CommandLine /format:csv`;+ // Security: Validate pattern to prevent command injection+ const safePattern = validatePattern(pattern);+ if (!safePattern) {+ console.warn(`⚠️ Invalid process pattern rejected: ${pattern}`);+ return [];+ }++ // Security: Pattern is validated above to only contain safe characters+ const cmd = `wmic process where "name like '%${safePattern}%'" get ProcessId,ParentProcessId,PercentProcessorTime,WorkingSetSize,CreationDate,CommandLine /format:csv`;const result = await execAsync(cmd).catch(() => ({ stdout: '' }));