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 review — safer handler dispatch, allSettled memory fetch, symlink-aware vault containment, sync-throw catch

Adam Eivy3db259097481
diff --git a/server/routes/commands.js b/server/routes/commands.js
index 45c0d58e4d..b6c6aec08b 100644
--- a/server/routes/commands.js
+++ b/server/routes/commands.js
@@ -55,10 +55,18 @@ router.post('/execute', asyncHandler(async (req, res) => {
if (!existsSync(resolvedPath)) {
throw new ServerError('workspacePath does not exist', { status: 400, code: 'INVALID_PATH' });
}
- if (!statSync(resolvedPath).isDirectory()) {
- throw new ServerError('workspacePath is not a directory', { status: 400, code: 'INVALID_PATH' });
+ // statSync/realpathSync can throw on permission/symlink edge cases — convert
+ // those into clean 400s rather than leaking as 500 via centralized middleware.
+ let realPath;
+ try {
+ if (!statSync(resolvedPath).isDirectory()) {
+ throw new ServerError('workspacePath is not a directory', { status: 400, code: 'INVALID_PATH' });
+ }
+ realPath = realpathSync(resolvedPath);
+ } catch (err) {
+ if (err instanceof ServerError) throw err;
+ throw new ServerError('workspacePath is not accessible', { status: 400, code: 'INVALID_PATH' });
}
- const realPath = realpathSync(resolvedPath);
const isAllowed = ALLOWED_WORKSPACE_ROOTS.some(root => isWithinRoot(realPath, root));
if (!isAllowed) {
throw new ServerError('workspacePath is outside allowed directories', { status: 400, code: 'INVALID_PATH' });
diff --git a/server/services/cosRunnerClient.js b/server/services/cosRunnerClient.js
index e166709c1b..294981f5f6 100644
--- a/server/services/cosRunnerClient.js
+++ b/server/services/cosRunnerClient.js
@@ -29,7 +29,19 @@ export function initCosRunnerConnection() {
const dispatch = (event, data) => {
const handlers = eventHandlers.get(event);
- if (handlers) handlers.forEach(h => h(data));
+ if (!handlers) return;
+ for (const h of handlers) {
+ // Guard against sync throws and async rejections so a single bad handler
+ // can't crash the process via unhandledRejection.
+ try {
+ const ret = h(data);
+ if (ret && typeof ret.then === 'function') {
+ ret.catch(err => console.error(`🔌 CoS runner handler for ${event} rejected: ${err.message}`));
+ }
+ } catch (err) {
+ console.error(`🔌 CoS runner handler for ${event} threw: ${err.message}`);
+ }
+ }
};
socket.on('connect', () => {
diff --git a/server/services/memoryRetriever.js b/server/services/memoryRetriever.js
index 4a84fc4347..d7546868ed 100644
--- a/server/services/memoryRetriever.js
+++ b/server/services/memoryRetriever.js
@@ -44,10 +44,12 @@ export async function getRelevantMemories(task, options = {}) {
});
}
- const fetchedMems = await Promise.all(searchResults.memories.map(r => getMemory(r.id)));
+ // allSettled: a single failed getMemory() shouldn't abort the whole retrieval.
+ const fetchedMems = await Promise.allSettled(searchResults.memories.map(r => getMemory(r.id)));
for (let i = 0; i < searchResults.memories.length; i++) {
const result = searchResults.memories[i];
- const mem = fetchedMems[i];
+ const settled = fetchedMems[i];
+ const mem = settled.status === 'fulfilled' ? settled.value : null;
if (mem) {
const tokens = estimateTokens(mem.content);
if (tokenCount + tokens <= maxTokens) {
diff --git a/server/services/obsidian.js b/server/services/obsidian.js
index 0acc2f4fdb..af1bcf0195 100644
--- a/server/services/obsidian.js
+++ b/server/services/obsidian.js
@@ -7,8 +7,8 @@
*/
import { readFile, writeFile, readdir, stat, unlink } from 'fs/promises';
-import { existsSync } from 'fs';
-import { join, relative, basename, dirname, extname } from 'path';
+import { existsSync, realpathSync } from 'fs';
+import { join, relative, resolve, basename, dirname, extname, isAbsolute } from 'path';
import { v4 as uuidv4 } from '../lib/uuid.js';
import { ensureDir, readJSONFile, PATHS } from '../lib/fileUtils.js';
@@ -88,6 +88,35 @@ export async function updateVault(id, updates) {
return vault;
}
+// Resolve `notePath` against `vault.path` and confirm the result is still
+// contained within the vault after symlinks are followed. Returns the
+// containable full path or null if the path escapes (including via symlinks,
+// prefix-match tricks like `/vault-evil`, or absolute/UNC inputs). When the
+// target doesn't exist yet (createNote), realpath the parent directory so a
+// new file path can still be validated.
+function resolveVaultPath(vault, notePath) {
+ const rootResolved = resolve(vault.path);
+ const rootReal = (() => {
+ try { return realpathSync(rootResolved); } catch { return rootResolved; }
+ })();
+ const fullPath = resolve(join(vault.path, notePath));
+ const real = (() => {
+ try { return realpathSync(fullPath); } catch {
+ // Non-existent target: realpath the parent (which MUST already exist
+ // for the path to be meaningful) and re-append the basename.
+ try {
+ const parentReal = realpathSync(dirname(fullPath));
+ return join(parentReal, basename(fullPath));
+ } catch {
+ return fullPath;
+ }
+ }
+ })();
+ const rel = relative(rootReal, real);
+ if (rel === '' || (!rel.startsWith('..') && !isAbsolute(rel))) return real;
+ return null;
+}
+
export async function getVaultById(id) {
const vaults = await getVaults();
return vaults.find(v => v.id === id) || null;
@@ -176,8 +205,8 @@ export async function getNote(vaultId, notePath, { includeBacklinks = true } = {
const vault = await getVaultById(vaultId);
if (!vault) return { error: 'VAULT_NOT_FOUND' };
- const fullPath = join(vault.path, notePath);
- if (!fullPath.startsWith(vault.path)) {
+ const fullPath = resolveVaultPath(vault, notePath);
+ if (!fullPath) {
return { error: 'INVALID_PATH', message: 'Path traversal not allowed' };
}
if (!existsSync(fullPath)) return { error: 'NOTE_NOT_FOUND' };
@@ -208,8 +237,8 @@ export async function updateNote(vaultId, notePath, content) {
const vault = await getVaultById(vaultId);
if (!vault) return { error: 'VAULT_NOT_FOUND' };
- const fullPath = join(vault.path, notePath);
- if (!fullPath.startsWith(vault.path)) return { error: 'INVALID_PATH' };
+ const fullPath = resolveVaultPath(vault, notePath);
+ if (!fullPath) return { error: 'INVALID_PATH' };
if (!existsSync(fullPath)) return { error: 'NOTE_NOT_FOUND' };
await writeFile(fullPath, content, 'utf-8');
@@ -223,13 +252,15 @@ export async function createNote(vaultId, notePath, content = '') {
if (!notePath.endsWith('.md')) notePath += '.md';
- const fullPath = join(vault.path, notePath);
- if (!fullPath.startsWith(vault.path)) return { error: 'INVALID_PATH' };
+ // For a new file the target doesn't exist yet — resolveVaultPath realpaths
+ // the parent directory (which must exist) to still catch symlink escapes.
+ await ensureDir(dirname(join(vault.path, notePath)));
+ const fullPath = resolveVaultPath(vault, notePath);
+ if (!fullPath) return { error: 'INVALID_PATH' };
if (existsSync(fullPath)) {
return { error: 'NOTE_EXISTS', message: 'A note with this name already exists' };
}
- await ensureDir(dirname(fullPath));
await writeFile(fullPath, content, 'utf-8');
console.log(`📓 Created note: ${notePath} in vault ${vault.name}`);
return await getNote(vaultId, notePath, { includeBacklinks: false });
@@ -239,8 +270,8 @@ export async function deleteNote(vaultId, notePath) {
const vault = await getVaultById(vaultId);
if (!vault) return { error: 'VAULT_NOT_FOUND' };
- const fullPath = join(vault.path, notePath);
- if (!fullPath.startsWith(vault.path)) return { error: 'INVALID_PATH' };
+ const fullPath = resolveVaultPath(vault, notePath);
+ if (!fullPath) return { error: 'INVALID_PATH' };
if (!existsSync(fullPath)) return { error: 'NOTE_NOT_FOUND' };
await unlink(fullPath);