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: require operator authority to force-kill host processes by PID

[._.]/ Adam Eivy0cd1a6468abe
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.js
index 4489565fc9..9ee56818ed 100644
--- a/server/lib/hostControlRoutes.js
+++ b/server/lib/hostControlRoutes.js
@@ -228,6 +228,11 @@ export const HOST_CONTROL_ROUTES = Object.freeze([
// control of the host desktop; a peer's Basic credential must not mint one (#10923).
'POST /api/remote-desktop/sessions',
+ // Force-kills whatever host PID the caller names, not only a detected agent
+ // CLI — the same host-process control as the gated PM2 process action in
+ // routes/commands.js. CoS agent stop/kill by agent id stays open.
+ 'DELETE /api/agents/:pid',
+
// Universe Builder authoring (#10669) can dispatch tool-capable text/vision
// providers or image agents, including API-first CLI/TUI fallback. Authorize
// the whole operation before image resolution, stores, runs or queue writes.
diff --git a/server/routes/hostControlAgentProcesses.test.js b/server/routes/hostControlAgentProcesses.test.js
new file mode 100644
index 0000000000..e38fe252af
--- /dev/null
+++ b/server/routes/hostControlAgentProcesses.test.js
@@ -0,0 +1,87 @@
+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { EventEmitter } from 'node:events';
+import express from 'express';
+import { request } from '../lib/testHelper.js';
+import { errorMiddleware } from '../lib/errorHandler.js';
+import { DEV_PROXY_CLIENT_ADDRESS_HEADER } from '../../lib/portosAuthCore.js';
+import { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';
+
+// Real router and auth/host-control gates; only the process service (whose
+// killProcess sends SIGKILL to the named host PID) is replaced.
+const auth = vi.hoisted(() => ({ enabled: false }));
+const effects = vi.hoisted(() => ({
+ getRunningAgents: vi.fn(async () => []),
+ getProcessInfo: vi.fn(async (pid) => ({ pid })),
+ killProcess: vi.fn(async () => true),
+}));
+vi.mock('../lib/fileUtils.js', async (importOriginal) =>
+ makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-agent-process-authority-') }));
+afterAll(cleanupTempDataRoots);
+vi.mock('../services/auth.js', async (importOriginal) => ({
+ ...await importOriginal(), isAuthEnabled: vi.fn(async () => auth.enabled),
+}));
+vi.mock('../services/settings.js', () => ({
+ settingsEvents: new EventEmitter(), getSettings: vi.fn(async () => ({})),
+}));
+vi.mock('../services/instanceIdentity.js', () => ({ loadData: vi.fn(async () => ({ peers: [] })) }));
+vi.mock('../services/agents.js', () => effects);
+
+import { authGate, hostControlRouteGate, hostControlBodyGate } from '../services/authGate.js';
+import { createSession } from '../services/auth.js';
+import agentsRoutes from './agents.js';
+
+const appFor = (address = '192.0.2.10') => {
+ const app = express();
+ app.use((req, _res, next) => {
+ Object.defineProperty(req.socket, 'remoteAddress', { value: address });
+ next();
+ });
+ app.use(authGate, hostControlRouteGate, express.json(), hostControlBodyGate);
+ app.use('/api/agents', agentsRoutes);
+ app.use(errorMiddleware);
+ return app;
+};
+const kill = (app, path, headers = {}) => {
+ const pending = request(app).delete(path);
+ for (const [name, value] of Object.entries(headers)) pending.set(name, value);
+ return pending;
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ auth.enabled = false;
+});
+
+describe('agent process kill authority', () => {
+ it('refuses a remote password-free caller before any host process is signalled', async () => {
+ for (const path of ['/api/agents/4242', '/API/Agents/4242/']) {
+ const response = await kill(appFor(), path);
+ expect([response.status, response.body.code], path).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);
+ }
+ const proxied = await kill(appFor('127.0.0.1'), '/api/agents/4242', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '192.0.2.10' });
+ expect([proxied.status, proxied.body.code]).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);
+ expect(effects.killProcess).not.toHaveBeenCalled();
+ });
+
+ it('preserves local password-free, operator and delegated-agent kills', async () => {
+ const operator = await createSession();
+ const agent = await createSession({ label: 'agent' });
+ for (const [enabled, address, headers] of [
+ [false, '127.0.0.1', {}],
+ [true, '192.0.2.10', { Cookie: `portos_auth=${operator.token}` }],
+ [true, '127.0.0.1', { Authorization: `Bearer ${agent.token}` }],
+ ]) {
+ auth.enabled = enabled;
+ const response = await kill(appFor(address), '/api/agents/4242', headers);
+ expect(response.status, JSON.stringify(response.body)).toBe(200);
+ }
+ expect(effects.killProcess).toHaveBeenCalledTimes(3);
+ expect(effects.killProcess).toHaveBeenCalledWith(4242);
+ });
+
+ it('keeps the process list and per-process reads open to remote password-free callers', async () => {
+ expect((await request(appFor()).get('/api/agents')).status).toBe(200);
+ expect((await request(appFor()).get('/api/agents/4242')).status).toBe(200);
+ expect(effects.getProcessInfo).toHaveBeenCalledWith(4242);
+ });
+});