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: guard workId against path traversal in every Writers Room service module
Adam Eivy79624d93e0fb
diff --git a/server/services/writersRoom/_shared.js b/server/services/writersRoom/_shared.jsindex c2922f304c..05c4f37b82 100644--- a/server/services/writersRoom/_shared.js+++ b/server/services/writersRoom/_shared.js@@ -1,9 +1,21 @@// Tiny helpers shared by every Writers Room service module (local, evaluator,-// characters). All three need the same iso-stamp + ServerError shorthands;-// duplicating them turned into byte-for-byte drift risk.+// characters, settings). All four need the same iso-stamp + ServerError+// shorthands plus the work-id shape check; duplicating them turned into+// byte-for-byte drift risk.import { ServerError } from '../../lib/errorHandler.js';export const nowIso = () => new Date().toISOString();export const badRequest = (message) => new ServerError(message, { status: 400, code: 'VALIDATION_ERROR' });export const notFound = (what) => new ServerError(`${what} not found`, { status: 404, code: 'NOT_FOUND' });++// Work ids are minted as `wr-work-<uuid>`. Anything else may be an attempted+// path traversal via the on-disk `data/writers-room/works/<workId>/` layout+// — every service that interpolates workId into a filesystem path must+// guard with this regex first.+export const WORK_ID_RE = /^wr-work-[0-9a-f-]+$/i;+export const assertValidWorkId = (workId) => {+ if (typeof workId !== 'string' || !WORK_ID_RE.test(workId)) {+ throw badRequest('Invalid work id');+ }+};diff --git a/server/services/writersRoom/characters.js b/server/services/writersRoom/characters.jsindex 0d8fa875d0..652a0fd62a 100644--- a/server/services/writersRoom/characters.js+++ b/server/services/writersRoom/characters.js@@ -15,12 +15,19 @@import { join } from 'path';import { randomUUID } from 'crypto';import { PATHS, atomicWrite, ensureDir, readJSONFile } from '../../lib/fileUtils.js';-import { nowIso, badRequest, notFound } from './_shared.js';+import { nowIso, badRequest, notFound, assertValidWorkId } from './_shared.js';const CHAR_ID_RE = /^wr-char-[0-9a-f-]+$/i;const root = () => join(PATHS.data, 'writers-room');-const charsFile = (workId) => join(root(), 'works', workId, 'characters.json');+const charsFile = (workId) => {+ // Defense-in-depth: the route layer validates the URL parameter, but this+ // module is also imported directly (e.g. by mergeExtractedCharacters from+ // the analysis pipeline). Refusing path-traversal-shaped ids here makes+ // workId-as-filesystem-path safe regardless of caller.+ assertValidWorkId(workId);+ return join(root(), 'works', workId, 'characters.json');+};const EDITABLE_FIELDS = ['name', 'aliases', 'role', 'physicalDescription', 'personality', 'background', 'notes'];@@ -61,6 +68,7 @@ async function loadFile(workId) {}async function saveFile(workId, state) {+ assertValidWorkId(workId);await ensureDir(join(root(), 'works', workId));await atomicWrite(charsFile(workId), { ...state, updatedAt: nowIso() });}diff --git a/server/services/writersRoom/characters.test.js b/server/services/writersRoom/characters.test.jsindex 018dde5df3..724f5cce17 100644--- a/server/services/writersRoom/characters.test.js+++ b/server/services/writersRoom/characters.test.js@@ -42,6 +42,15 @@ describe('writers room — characters CRUD', () => {expect(await listCharacters(id)).toEqual([]);});+ it('rejects path-traversal-shaped work ids on every read/write helper', async () => {+ // Every public helper interpolates workId into an on-disk path; a+ // crafted id like '../../etc' must be refused with a 400 before any+ // filesystem access. This protects callers that bypass the route layer.+ await expect(listCharacters('../../etc')).rejects.toThrow(/work id/i);+ await expect(createCharacter('../../etc', { name: 'X' })).rejects.toThrow(/work id/i);+ await expect(mergeExtractedCharacters('../../etc', [{ name: 'X' }])).rejects.toThrow(/work id/i);+ });+it('rejects creating without a name', async () => {const id = await newWork();await expect(createCharacter(id, { name: ' ' })).rejects.toThrow(/name required/i);diff --git a/server/services/writersRoom/evaluator.js b/server/services/writersRoom/evaluator.jsindex 22529f0344..2e2925b648 100644--- a/server/services/writersRoom/evaluator.js+++ b/server/services/writersRoom/evaluator.js@@ -16,7 +16,7 @@ import { ANALYSIS_KINDS } from '../../lib/writersRoomPresets.js';import { getWorkWithBody } from './local.js';import { listCharacters, mergeExtractedCharacters } from './characters.js';import { listSettings, mergeExtractedSettings } from './settings.js';-import { nowIso, badRequest, notFound } from './_shared.js';+import { nowIso, badRequest, notFound, assertValidWorkId } from './_shared.js';export { ANALYSIS_KINDS };@@ -34,7 +34,13 @@ const isValidAnalysisId = (id) => typeof id === 'string' && ANALYSIS_KINDS.incluconst LEGACY_ANALYSIS_ID_RE = /^wr-analysis-[0-9a-f-]+$/i;const root = () => join(PATHS.data, 'writers-room');-const analysisDir = (workId) => join(root(), 'works', workId, 'analysis');+const analysisDir = (workId) => {+ // Defense-in-depth: refuse path-traversal-shaped workIds before+ // interpolating them into the on-disk path. Mirrors the guard in+ // characters.js / settings.js.+ assertValidWorkId(workId);+ return join(root(), 'works', workId, 'analysis');+};const analysisPath = (workId, id) => join(analysisDir(workId), `${id}.json`);// ---------- LLM invocation ----------diff --git a/server/services/writersRoom/local.js b/server/services/writersRoom/local.jsindex 53d4885c56..2c0e64e2c4 100644--- a/server/services/writersRoom/local.js+++ b/server/services/writersRoom/local.js@@ -20,7 +20,7 @@ import { readFile, rm, readdir } from 'fs/promises';import { PATHS, atomicWrite, ensureDir, readJSONFile, safeJSONParse } from '../../lib/fileUtils.js';import { ServerError } from '../../lib/errorHandler.js';import { WORK_KINDS, WORK_STATUSES } from '../../lib/writersRoomPresets.js';-import { nowIso, badRequest, notFound } from './_shared.js';+import { nowIso, badRequest, notFound, WORK_ID_RE, assertValidWorkId } from './_shared.js';// Paths are resolved lazily so tests can swap PATHS.data via vi.mock without// the module-load snapshot freezing them at import time.@@ -29,11 +29,10 @@ const foldersFile = () => join(root(), 'folders.json');const exercisesFile = () => join(root(), 'exercises.json');const worksDir = () => join(root(), 'works');-const WORK_ID_RE = /^wr-work-[0-9a-f-]+$/i;const DRAFT_ID_RE = /^wr-draft-[0-9a-f-]+$/i;function workDir(workId) {- if (!WORK_ID_RE.test(workId)) throw badRequest('Invalid work id');+ assertValidWorkId(workId);return join(worksDir(), workId);}diff --git a/server/services/writersRoom/settings.js b/server/services/writersRoom/settings.jsindex 6134add867..05f471322a 100644--- a/server/services/writersRoom/settings.js+++ b/server/services/writersRoom/settings.js@@ -13,12 +13,18 @@import { join } from 'path';import { randomUUID } from 'crypto';import { PATHS, atomicWrite, ensureDir, readJSONFile } from '../../lib/fileUtils.js';-import { nowIso, badRequest, notFound } from './_shared.js';+import { nowIso, badRequest, notFound, assertValidWorkId } from './_shared.js';const SETTING_ID_RE = /^wr-setting-[0-9a-f-]+$/i;const root = () => join(PATHS.data, 'writers-room');-const settingsFile = (workId) => join(root(), 'works', workId, 'settings.json');+const settingsFile = (workId) => {+ // Defense-in-depth: refuse path-traversal-shaped workIds before+ // interpolating them into the on-disk path. Routes already validate, but+ // this module is also called directly from analysis-merge code paths.+ assertValidWorkId(workId);+ return join(root(), 'works', workId, 'settings.json');+};const EDITABLE_FIELDS = ['name', 'slugline', 'description', 'palette', 'era', 'weather', 'recurringDetails', 'notes'];@@ -69,6 +75,7 @@ async function loadFile(workId) {}async function saveFile(workId, state) {+ assertValidWorkId(workId);await ensureDir(join(root(), 'works', workId));await atomicWrite(settingsFile(workId), { ...state, updatedAt: nowIso() });}diff --git a/server/services/writersRoom/settings.test.js b/server/services/writersRoom/settings.test.jsindex 23bf0ba6bc..83910312ad 100644--- a/server/services/writersRoom/settings.test.js+++ b/server/services/writersRoom/settings.test.js@@ -64,6 +64,15 @@ describe('writers room — settings CRUD', () => {expect(await listSettings(id)).toEqual([]);});+ it('rejects path-traversal-shaped work ids on every read/write helper', async () => {+ // Mirrors the characters.js guard. Every helper that interpolates+ // workId into the on-disk path must refuse a traversal-shaped id+ // with a 400 before any I/O happens.+ await expect(listSettings('../../etc')).rejects.toThrow(/work id/i);+ await expect(createSetting('../../etc', { slugline: 'X' })).rejects.toThrow(/work id/i);+ await expect(mergeExtractedSettings('../../etc', [{ slugline: 'X' }])).rejects.toThrow(/work id/i);+ });+it('rejects creating a setting with neither slugline nor name', async () => {const id = await newWork();await expect(createSetting(id, {})).rejects.toThrow(/slugline or a name/i);