Skip to content

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

fix: address second round of PR review feedback

Adam Eivy3ce29b2ec228
diff --git a/server/cos-runner/index.js b/server/cos-runner/index.js
index e0c8b749b1..4de7ecccc6 100644
--- a/server/cos-runner/index.js
+++ b/server/cos-runner/index.js
@@ -24,7 +24,7 @@ const STATE_FILE = join(ROOT_DIR, 'data/cos/runner-state.json');
const AGENTS_DIR = join(ROOT_DIR, 'data/cos/agents');
const PORT = process.env.PORT || 5558;
-const HOST = process.env.HOST || '0.0.0.0';
+const HOST = process.env.HOST || '127.0.0.1';
const RUNS_DIR = join(ROOT_DIR, 'data/runs');
// Active agent processes (in memory)
diff --git a/server/routes/screenshots.js b/server/routes/screenshots.js
index c817c7f1ad..aa5fc601b8 100644
--- a/server/routes/screenshots.js
+++ b/server/routes/screenshots.js
@@ -12,6 +12,31 @@ const SCREENSHOTS_DIR = join(__dirname, '../../data/screenshots');
const router = Router();
+// Max file size: 10MB
+const MAX_FILE_SIZE = 10 * 1024 * 1024;
+
+// Validate image magic bytes
+function isValidImage(buffer) {
+ // PNG: 89 50 4E 47
+ if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4E && buffer[3] === 0x47) {
+ return 'png';
+ }
+ // JPEG: FF D8 FF
+ if (buffer[0] === 0xFF && buffer[1] === 0xD8 && buffer[2] === 0xFF) {
+ return 'jpg';
+ }
+ // GIF: 47 49 46 38
+ if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38) {
+ return 'gif';
+ }
+ // WebP: 52 49 46 46 ... 57 45 42 50
+ if (buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46 &&
+ buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50) {
+ return 'webp';
+ }
+ return null;
+}
+
// POST /api/screenshots - Upload a screenshot (base64)
router.post('/', asyncHandler(async (req, res) => {
const { data, filename, mimeType } = req.body;
@@ -20,18 +45,28 @@ router.post('/', asyncHandler(async (req, res) => {
throw new ServerError('data is required (base64)', { status: 400, code: 'VALIDATION_ERROR' });
}
+ // Decode base64 and validate size
+ const buffer = Buffer.from(data, 'base64');
+ if (buffer.length > MAX_FILE_SIZE) {
+ throw new ServerError(`File exceeds maximum size of ${MAX_FILE_SIZE / 1024 / 1024}MB`, { status: 400, code: 'FILE_TOO_LARGE' });
+ }
+
+ // Validate actual file content (magic bytes)
+ const detectedType = isValidImage(buffer);
+ if (!detectedType) {
+ throw new ServerError('Invalid image file - only PNG, JPEG, GIF, and WebP are supported', { status: 400, code: 'INVALID_FILE_TYPE' });
+ }
+
// Ensure screenshots directory exists
if (!existsSync(SCREENSHOTS_DIR)) {
await mkdir(SCREENSHOTS_DIR, { recursive: true });
}
const id = uuidv4();
- const ext = mimeType?.includes('png') ? 'png' : 'jpg';
+ const ext = detectedType; // Use detected type, not client-provided
const fname = filename || `screenshot-${id}.${ext}`;
const filepath = join(SCREENSHOTS_DIR, fname);
- // Decode base64 and write file
- const buffer = Buffer.from(data, 'base64');
await writeFile(filepath, buffer);
console.log(`📸 Screenshot saved: ${fname} (${buffer.length} bytes)`);
diff --git a/server/services/runner.js b/server/services/runner.js
index 95492e3434..d15c234980 100644
--- a/server/services/runner.js
+++ b/server/services/runner.js
@@ -12,7 +12,8 @@ import {
isRunActiveInRunner,
stopRunViaRunner,
initCosRunnerConnection,
- onCosRunnerEvent
+ onCosRunnerEvent,
+ getActiveRunsFromRunner
} from './cosRunnerClient.js';
const __filename = fileURLToPath(import.meta.url);
@@ -20,6 +21,17 @@ const __dirname = dirname(__filename);
const DATA_DIR = join(__dirname, '../../data');
const RUNS_DIR = join(DATA_DIR, 'runs');
+/**
+ * Safe JSON parse with fallback to empty object
+ */
+function safeJsonParse(str, fallback = {}) {
+ try {
+ return JSON.parse(str);
+ } catch {
+ return fallback;
+ }
+}
+
// Track active runs for cancellation (only used for API runs, CLI runs are in cos-runner)
const activeRuns = new Map();
@@ -56,7 +68,7 @@ function initRunnerEvents() {
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
const outputPath = join(RUNS_DIR, runId, 'output.txt');
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
const output = await readFile(outputPath, 'utf-8').catch(() => '');
metadata.endTime = new Date().toISOString();
@@ -100,7 +112,7 @@ function initRunnerEvents() {
const callbacks = pendingRunCallbacks.get(runId);
if (callbacks) {
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.success = false;
@@ -346,7 +358,7 @@ export async function executeCliRun(runId, provider, prompt, workspacePath, onDa
// Fall back to error - cos-runner must be running
console.error(`❌ CoS runner not available for run ${runId}`);
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.success = false;
metadata.error = 'CoS runner service not available';
@@ -380,7 +392,7 @@ export async function executeCliRun(runId, provider, prompt, workspacePath, onDa
pendingRunCallbacks.delete(runId);
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.success = false;
metadata.error = err.message;
@@ -426,7 +438,7 @@ export async function executeApiRun(runId, provider, model, prompt, workspacePat
if (!response.ok) {
activeRuns.delete(runId);
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.duration = Date.now() - startTime;
metadata.success = false;
@@ -502,7 +514,7 @@ export async function executeApiRun(runId, provider, model, prompt, workspacePat
await writeFile(outputPath, output);
activeRuns.delete(runId);
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.duration = Date.now() - startTime;
metadata.exitCode = 0;
@@ -521,7 +533,7 @@ export async function executeApiRun(runId, provider, model, prompt, workspacePat
processStream().catch(async (err) => {
activeRuns.delete(runId);
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
metadata.endTime = new Date().toISOString();
metadata.duration = Date.now() - startTime;
metadata.success = false;
@@ -573,7 +585,7 @@ export async function getRun(runId) {
return null;
}
- const metadata = JSON.parse(await readFile(join(runDir, 'metadata.json'), 'utf-8'));
+ const metadata = safeJsonParse(await readFile(join(runDir, 'metadata.json'), 'utf-8').catch(() => '{}'));
return metadata;
}
@@ -608,8 +620,8 @@ export async function listRuns(limit = 50, offset = 0, source = 'all') {
for (const runId of runIds) {
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
if (existsSync(metadataPath)) {
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8'));
- runs.push(metadata);
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ if (metadata.id) runs.push(metadata);
}
}
@@ -650,7 +662,7 @@ export async function deleteFailedRuns() {
for (const runId of runIds) {
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
if (existsSync(metadataPath)) {
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
if (metadata.success === false) {
await rm(join(RUNS_DIR, runId), { recursive: true });
deletedCount++;
@@ -700,7 +712,7 @@ async function syncActiveRuns() {
const metadataPath = join(RUNS_DIR, runId, 'metadata.json');
if (existsSync(outputPath) && existsSync(metadataPath)) {
- const metadata = JSON.parse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
+ const metadata = safeJsonParse(await readFile(metadataPath, 'utf-8').catch(() => '{}'));
// If endTime is set, the run completed - we missed the event
if (metadata.endTime) {