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: validate CoS script names (#4879)

Adam Eivyde463ab799fe
diff --git a/server/services/cos.js b/server/services/cos.js
index a12d51b1f9..14bc239453 100644
--- a/server/services/cos.js
+++ b/server/services/cos.js
@@ -15,7 +15,7 @@
* - cosHealthMonitor.js — daemon health checks (PM2/memory, auto-restart)
*/
-import { readFile, writeFile, readdir } from 'fs/promises';
+import { readFile, readdir } from 'fs/promises';
import { existsSync } from 'fs';
import { join } from 'path';
import { getActiveProvider } from './providers.js';
@@ -28,7 +28,8 @@ import { getPerformanceSummary, checkAndRehabilitateSkippedTasks, getLearningIns
import { schedule as scheduleEvent, cancel as cancelEvent } from './eventScheduler.js';
import { generateProactiveTasks as generateMissionTasks } from './missions.js';
import { recordJobExecution } from './autonomousJobs.js';
-import { atomicWrite, safeJSONParse, sleep } from '../lib/fileUtils.js';
+import { safeJSONParse, sleep, isTopLevelEntryName } from '../lib/fileUtils.js';
+import { ServerError } from '../lib/errorHandler.js';
import { addNotification, NOTIFICATION_TYPES } from './notifications.js';
import { getUserTimezone, todayInTimezone } from '../lib/timezone.js';
import { normalizeDomainAutonomy, getDomainMode } from '../lib/domainAutonomy.js';
@@ -750,25 +751,6 @@ async function resetOrphanedTasks({ bootRecovery = false } = {}) {
}
}
-/**
- * Save a generated script
- */
-export async function saveScript(name, content, metadata = {}) {
- await ensureDirectories();
- const scriptPath = join(SCRIPTS_DIR, `${name}.sh`);
- await writeFile(scriptPath, content, { mode: 0o755 });
-
- // Save metadata
- const metaPath = join(SCRIPTS_DIR, `${name}.json`);
- await atomicWrite(metaPath, {
- name,
- createdAt: new Date().toISOString(),
- ...metadata
- });
-
- return { path: scriptPath, name };
-}
-
/**
* List generated scripts
*/
@@ -782,6 +764,9 @@ export async function listScripts() {
* Get script content
*/
export async function getScript(name) {
+ if (!isTopLevelEntryName(name) || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(name)) {
+ throw new ServerError('Invalid script name', { status: 400, code: 'VALIDATION_ERROR' });
+ }
const scriptPath = join(SCRIPTS_DIR, `${name}.sh`);
const metaPath = join(SCRIPTS_DIR, `${name}.json`);
diff --git a/server/services/cosScripts.test.js b/server/services/cosScripts.test.js
new file mode 100644
index 0000000000..90c246a6e7
--- /dev/null
+++ b/server/services/cosScripts.test.js
@@ -0,0 +1,38 @@
+import { afterAll, describe, expect, it, vi } from 'vitest';
+import { mkdir, writeFile } from 'fs/promises';
+import { join } from 'path';
+import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';
+
+const { makeProxy, cleanup } = mockPathsDataRoot({ prefix: 'portos-cos-scripts-' });
+
+vi.mock('../lib/fileUtils.js', async () => makeProxy(await vi.importActual('../lib/fileUtils.js')));
+
+const { SCRIPTS_DIR } = await import('./cosState.js');
+const { getScript } = await import('./cos.js');
+
+afterAll(cleanup);
+
+describe('getScript', () => {
+ it('returns a script and its metadata for a valid script name', async () => {
+ await mkdir(SCRIPTS_DIR, { recursive: true });
+ await writeFile(join(SCRIPTS_DIR, 'deploy.sh'), '#!/bin/sh\necho deploy\n');
+ await writeFile(join(SCRIPTS_DIR, 'deploy.json'), '{"label":"Deploy"}');
+
+ await expect(getScript('deploy')).resolves.toEqual({
+ name: 'deploy',
+ content: '#!/bin/sh\necho deploy\n',
+ metadata: { label: 'Deploy' },
+ });
+ });
+
+ it.each(['../settings', 'nested/script', 'nested\\script', '%2Fsettings', '.hidden', 'script name'])(
+ 'rejects hostile or malformed script name %j',
+ async (name) => {
+ await expect(getScript(name)).rejects.toMatchObject({
+ message: 'Invalid script name',
+ status: 400,
+ code: 'VALIDATION_ERROR',
+ });
+ },
+ );
+});