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
address review: cache resilience, selector injection, WebSocket leak, validation errors
Adam Eivy2c2280f6724d
diff --git a/server/routes/messages.js b/server/routes/messages.jsindex aac3dc01f6..bd430b5088 100644--- a/server/routes/messages.js+++ b/server/routes/messages.js@@ -1,6 +1,7 @@import express from 'express';import { z } from 'zod';import { asyncHandler } from '../lib/errorHandler.js';+import { validateRequest } from '../lib/validation.js';import * as messageAccounts from '../services/messageAccounts.js';import * as messageSync from '../services/messageSync.js';import * as messageDrafts from '../services/messageDrafts.js';@@ -71,7 +72,7 @@ router.get('/accounts', asyncHandler(async (req, res) => {}));router.post('/accounts', asyncHandler(async (req, res) => {- const data = createAccountSchema.parse(req.body);+ const data = validateRequest(createAccountSchema, req.body);const account = await messageAccounts.createAccount(data);req.app.get('io')?.emit('messages:changed', {});res.status(201).json(account);@@ -81,7 +82,7 @@ router.put('/accounts/:id', asyncHandler(async (req, res) => {if (!z.string().uuid().safeParse(req.params.id).success) {return res.status(400).json({ error: 'Invalid account ID format' });}- const updates = updateAccountSchema.parse(req.body);+ const updates = validateRequest(updateAccountSchema, req.body);const account = await messageAccounts.updateAccount(req.params.id, updates);if (!account) return res.status(404).json({ error: 'Account not found' });req.app.get('io')?.emit('messages:changed', {});@@ -149,7 +150,7 @@ router.get('/drafts', asyncHandler(async (req, res) => {}));router.post('/drafts', asyncHandler(async (req, res) => {- const data = createDraftSchema.parse(req.body);+ const data = validateRequest(createDraftSchema, req.body);const account = await messageAccounts.getAccount(data.accountId);if (!account) return res.status(404).json({ error: 'Account not found' });if (!data.sendVia) {@@ -161,7 +162,7 @@ router.post('/drafts', asyncHandler(async (req, res) => {}));router.post('/drafts/generate', asyncHandler(async (req, res) => {- const data = generateDraftSchema.parse(req.body);+ const data = validateRequest(generateDraftSchema, req.body);const account = await messageAccounts.getAccount(data.accountId);if (!account) return res.status(404).json({ error: 'Account not found' });// AI draft generation - stub for now@@ -183,7 +184,7 @@ router.put('/drafts/:id', asyncHandler(async (req, res) => {if (!z.string().uuid().safeParse(req.params.id).success) {return res.status(400).json({ error: 'Invalid draft ID format' });}- const updates = updateDraftSchema.parse(req.body);+ const updates = validateRequest(updateDraftSchema, req.body);const draft = await messageDrafts.updateDraft(req.params.id, updates);if (!draft) return res.status(404).json({ error: 'Draft not found' });res.json(draft);@@ -259,7 +260,7 @@ router.put('/selectors/:provider', asyncHandler(async (req, res) => {if (!ALLOWED_PROVIDERS.includes(req.params.provider)) {return res.status(400).json({ error: 'Invalid provider' });}- const { selectors } = updateSelectorsSchema.parse(req.body);+ const { selectors } = validateRequest(updateSelectorsSchema, req.body);const updated = await updateSelectors(req.params.provider, selectors);res.json(updated);}));diff --git a/server/routes/messages.test.js b/server/routes/messages.test.jsindex bd6c32af55..c4d2ddbbdf 100644--- a/server/routes/messages.test.js+++ b/server/routes/messages.test.js@@ -111,7 +111,7 @@ describe('Messages Routes', () => {.post('/api/messages/accounts').send({ type: 'gmail' });- expect(response.status).toBe(500); // Zod throws, no error middleware in test+ expect(response.status).toBe(400);});it('should return 400 for invalid type', async () => {@@ -119,7 +119,7 @@ describe('Messages Routes', () => {.post('/api/messages/accounts').send({ name: 'Test', type: 'yahoo' });- expect(response.status).toBe(500); // Zod throws, no error middleware in test+ expect(response.status).toBe(400);});});@@ -352,8 +352,8 @@ describe('Messages Routes', () => {.post('/api/messages/drafts').send({ accountId: INVALID_UUID });- // Zod validation failure for uuid- expect(response.status).toBe(500);+ // validateRequest returns 400 for invalid input+ expect(response.status).toBe(400);});});diff --git a/server/services/messagePlaywrightSync.js b/server/services/messagePlaywrightSync.jsindex 829bdf04c7..6b95e981b8 100644--- a/server/services/messagePlaywrightSync.js+++ b/server/services/messagePlaywrightSync.js@@ -87,7 +87,7 @@ async function evaluateOnPage(page, expression) {}});- ws.on('error', () => { clearTimeout(timer); resolve(null); });+ ws.on('error', () => { clearTimeout(timer); ws.close(); resolve(null); });});}@@ -179,7 +179,7 @@ function buildExtractionScript(type, sels) {const listSel = sels.messageRow || "[role='listitem']";return `(function() {- const rows = document.querySelectorAll('${listSel.replace(/'/g, "\\'")}');+ const rows = document.querySelectorAll(${JSON.stringify(listSel)});return Array.from(rows).slice(0, 50).map(row => {const text = row.innerText || '';const lines = text.split('\\n').map(l => l.trim()).filter(Boolean);@@ -198,7 +198,7 @@ function buildExtractionScript(type, sels) {const msgSel = sels.messageItem || "[role='listitem']";return `(function() {- const items = document.querySelectorAll('${msgSel.replace(/'/g, "\\'")}');+ const items = document.querySelectorAll(${JSON.stringify(msgSel)});return Array.from(items).slice(0, 50).map(item => {const text = item.innerText || '';const lines = text.split('\\n').map(l => l.trim()).filter(Boolean);@@ -239,7 +239,7 @@ export async function testSelectors(provider) {for (const [name, selector] of Object.entries(sels)) {const count = await evaluateOnPage(page,- `document.querySelectorAll('${selector.replace(/'/g, "\\'")}').length`+ `document.querySelectorAll(${JSON.stringify(selector)}).length`);results[name] = { selector, matches: count ?? 0 };}diff --git a/server/services/messageSync.js b/server/services/messageSync.jsindex 209faabc29..4428d5af99 100644--- a/server/services/messageSync.js+++ b/server/services/messageSync.js@@ -53,6 +53,7 @@ export async function getMessages(options = {}) {for (const file of files) {if (!file.endsWith('.json')) continue;const fileAccountId = file.replace('.json', '');+ if (!UUID_RE.test(fileAccountId)) continue;const cache = await loadCache(fileAccountId);allMessages.push(...cache.messages.map(m => ({ ...m, accountId: m.accountId || fileAccountId })));}