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: redact operator command output and scope its socket stream

[._.]/ Adam Eivy00660a736b1f
diff --git a/client/src/pages/RunnerPage.jsx b/client/src/pages/RunnerPage.jsx
index e0eb3dc8b5..05334eb533 100644
--- a/client/src/pages/RunnerPage.jsx
+++ b/client/src/pages/RunnerPage.jsx
@@ -5,6 +5,7 @@ import toast from '../components/ui/Toast';
import FilePickerButton from '../components/ui/FilePickerButton';
import * as api from '../services/api';
import socket from '../services/socket';
+import { useSocketSubscription } from '../hooks/useSocketSubscription';
import { processScreenshotUploads } from '../services/apiMedia';
import { filterSelectableModels } from '../utils/providers';
import ProviderModelSelector from '../components/ProviderModelSelector';
@@ -112,6 +113,9 @@ export function RunnerPage() {
};
}, [runId]);
+ // Command output is only broadcast to subscribed sockets (#12019).
+ useSocketSubscription('command');
+
// Subscribe to command output
useEffect(() => {
if (!commandId) return;
diff --git a/client/src/socketSubscriptionConventions.test.js b/client/src/socketSubscriptionConventions.test.js
index fd00b84a3c..c5f3f1ebe5 100644
--- a/client/src/socketSubscriptionConventions.test.js
+++ b/client/src/socketSubscriptionConventions.test.js
@@ -45,7 +45,7 @@ import { trackedSourceFiles } from './test/trackedFiles.js';
const CLIENT_ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
// The namespaces `registerSubscriber` wires up in server/services/socket.js.
-const SUBSCRIBER_NAMESPACES = ['cos', 'errors', 'notifications', 'agents', 'instances', 'loops', 'beeper'];
+const SUBSCRIBER_NAMESPACES = ['cos', 'errors', 'notifications', 'agents', 'instances', 'loops', 'beeper', 'command'];
const SUBSCRIBE_CALL = new RegExp(
`emit\\(\\s*['"](?:${SUBSCRIBER_NAMESPACES.join('|')}):subscribe['"]`
diff --git a/server/lib/commandSecurity.js b/server/lib/commandSecurity.js
index 6942875c66..ae68bc66f5 100644
--- a/server/lib/commandSecurity.js
+++ b/server/lib/commandSecurity.js
@@ -354,17 +354,27 @@ export function validateUnattendedCommand(command) {
return check;
}
-// Patterns matching sensitive env var values in command output
-const SENSITIVE_ENV_PATTERN = /("(?:[a-z0-9]+_)*(?:KEY|SECRET|TOKEN|PASSWORD|PASSPHRASE|MACAROON|CERT|CREDENTIAL|AUTH)(?:_[a-z0-9]+)*":\s*)"[^"]+"/gi;
+// Sensitive key names: a segment of the key, separated by `_`, `.` or `-`, so
+// `API_TOKEN` and `github.token` match while `author` does not.
+const SENSITIVE_KEY_WORDS = 'KEY|SECRET|TOKEN|PASSWORD|PASSPHRASE|MACAROON|CERT|CREDENTIAL|AUTH';
+// JSON key/value pairs, e.g. `"SECRET_KEY": "value"` in `pm2 jlist` env blocks.
+const SENSITIVE_ENV_PATTERN = new RegExp(`("(?:[a-z0-9]+_)*(?:${SENSITIVE_KEY_WORDS})(?:_[a-z0-9]+)*":\\s*)"[^"]+"`, 'gi');
+// Assignment lines, e.g. `API_TOKEN=value` from a dotenv read or
+// `github.token=value` from `git config --list` (#12019). Anchored to the
+// line (horizontal whitespace only) so a value is only ever the remainder of
+// its own line.
+const SENSITIVE_ASSIGNMENT_PATTERN = new RegExp(`^([ \\t]*(?:export[ \\t]+)?(?:[a-z0-9]+[_.-])*(?:${SENSITIVE_KEY_WORDS})(?:[_.-][a-z0-9]+)*[ \\t]*=)(.+)$`, 'gim');
/**
- * Redact sensitive env var values from command output before persisting.
- * Only redacts JSON key/value patterns (e.g. "SECRET_KEY": "value"). Shell-level
- * leaks (env expansion, command substitution) are not covered — acceptable for
- * PortOS's single-user, private-network deployment where the operator is the
- * only user and shell output is not exposed to external consumers.
+ * Redact sensitive values from command output before it is persisted,
+ * broadcast, or returned. Covers JSON key/value pairs and `KEY=value`
+ * assignment lines whose key names a secret. Shell-level leaks (env
+ * expansion, command substitution) are not covered — the allowlist and the
+ * metacharacter filter keep those out of the spawn in the first place.
*/
export function redactOutput(output) {
if (!output) return output;
- return output.replace(SENSITIVE_ENV_PATTERN, '$1"[REDACTED]"');
+ return output
+ .replace(SENSITIVE_ENV_PATTERN, '$1"[REDACTED]"')
+ .replace(SENSITIVE_ASSIGNMENT_PATTERN, '$1[REDACTED]');
}
diff --git a/server/lib/commandSecurity.test.js b/server/lib/commandSecurity.test.js
index b1f3ee63d5..dc75d0dc7b 100644
--- a/server/lib/commandSecurity.test.js
+++ b/server/lib/commandSecurity.test.js
@@ -221,6 +221,25 @@ describe('commandSecurity', () => {
expect(redactOutput(input)).toBe(input)
})
+ // Operator command output is dotenv / `git config --list` shaped as often
+ // as JSON (#12019): the key must name a secret as its own segment.
+ it.each([
+ ['a dotenv line', 'API_TOKEN=abc123\nPORT=3000', 'API_TOKEN=[REDACTED]\nPORT=3000'],
+ ['an exported shell assignment', 'export GITHUB_TOKEN=ghp_example', 'export GITHUB_TOKEN=[REDACTED]'],
+ ['a git config key', 'github.token=example\nuser.name=Example', 'github.token=[REDACTED]\nuser.name=Example'],
+ ['a kebab-case key', 'my-secret-key=value', 'my-secret-key=[REDACTED]'],
+ ])('redacts %s', (_label, input, expected) => {
+ expect(redactOutput(input)).toBe(expected)
+ })
+
+ it.each([
+ ['a key that only contains a secret word', 'author=Example Author'],
+ ['an empty assignment', 'API_TOKEN='],
+ ['a value on a different line from the key', 'API_TOKEN\n=value'],
+ ])('leaves %s alone', (_label, input) => {
+ expect(redactOutput(input)).toBe(input)
+ })
+
it('should redact multiple sensitive values', () => {
const input = '{"API_KEY": "key1", "SECRET_TOKEN": "tok2", "name": "app"}'
const result = redactOutput(input)
diff --git a/server/routes/commands.js b/server/routes/commands.js
index 8841589570..4c635753fa 100644
--- a/server/routes/commands.js
+++ b/server/routes/commands.js
@@ -4,6 +4,7 @@ import { existsSync, statSync, realpathSync } from 'fs';
import { resolve } from 'path';
import * as commands from '../services/commands.js';
import { requireHostControl } from '../services/authGate.js';
+import { broadcastToCommandSubscribers } from '../services/socket.js';
import * as pm2Service from '../services/pm2.js';
import { asyncHandler, ServerError } from '../lib/errorHandler.js';
import { validateRequest, commandExecuteBodySchema } from '../lib/validation.js';
@@ -42,21 +43,20 @@ router.post('/execute', requireHostControl, asyncHandler(async (req, res) => {
}
}
- const io = req.app.get('io');
-
// executeCommand reports validation failures through onComplete before it
// returns null. Declare the id first so that synchronous callback cannot
// access it in its temporal dead zone; there is no command-scoped socket
- // event to emit when no command was started.
+ // event to emit when no command was started. Output goes only to sockets
+ // subscribed to command output, never to every connected socket (#12019).
let commandId;
commandId = commands.executeCommand(
command,
workspacePath,
(data, stream) => {
- if (commandId) io?.emit(`command:${commandId}:data`, { data, stream });
+ if (commandId) broadcastToCommandSubscribers(`command:${commandId}:data`, { data, stream });
},
(result) => {
- if (commandId) io?.emit(`command:${commandId}:complete`, result);
+ if (commandId) broadcastToCommandSubscribers(`command:${commandId}:complete`, result);
}
);
diff --git a/server/routes/commands.test.js b/server/routes/commands.test.js
index 8e4132aaff..457bf3548a 100644
--- a/server/routes/commands.test.js
+++ b/server/routes/commands.test.js
@@ -22,6 +22,12 @@ vi.mock('../services/history.js', () => ({
logAction: vi.fn(),
}));
+// Command output reaches only subscribed sockets (#12019); the route never
+// touches the global io instance.
+vi.mock('../services/socket.js', () => ({
+ broadcastToCommandSubscribers: vi.fn(),
+}));
+
vi.mock('../services/pm2.js', () => ({
listProcesses: vi.fn().mockResolvedValue([]),
listProcessesStrict: vi.fn().mockResolvedValue([]),
@@ -66,6 +72,8 @@ vi.mock('../services/settings.js', () => ({
}));
import { authGate } from '../services/authGate.js';
+import { broadcastToCommandSubscribers } from '../services/socket.js';
+import { logAction } from '../services/history.js';
import { isAuthEnabled } from '../services/auth.js';
import { existsSync, realpathSync, statSync } from 'fs';
import { isWithinAllowedRoots } from '../lib/workspaceRoots.js';
@@ -347,7 +355,7 @@ describe('commands routes', () => {
it('returns FORBIDDEN for a command outside the operator allowlist', async () => {
allowWorkspace();
- const { app, io } = createApp();
+ const { app } = createApp();
const response = await request(app)
.post('/api/commands/execute')
@@ -356,7 +364,7 @@ describe('commands routes', () => {
expect(response.status).toBe(403);
expect(response.body.code).toBe('FORBIDDEN');
expect(spawnMock).not.toHaveBeenCalled();
- expect(io.emit).not.toHaveBeenCalledWith(
+ expect(broadcastToCommandSubscribers).not.toHaveBeenCalledWith(
expect.stringMatching(/^command:/),
expect.anything(),
);
@@ -384,26 +392,60 @@ describe('commands routes', () => {
child.emit('close', 0);
const eventPrefix = `command:${response.body.commandId}`;
- expect(io.emit).toHaveBeenCalledWith(`${eventPrefix}:data`, {
+ expect(broadcastToCommandSubscribers).toHaveBeenCalledWith(`${eventPrefix}:data`, {
data: 'workspace output',
stream: 'stdout',
});
- expect(io.emit).toHaveBeenCalledWith(`${eventPrefix}:data`, {
+ expect(broadcastToCommandSubscribers).toHaveBeenCalledWith(`${eventPrefix}:data`, {
data: 'workspace warning',
stream: 'stderr',
});
- expect(io.emit).toHaveBeenCalledWith(`${eventPrefix}:complete`, expect.objectContaining({
+ expect(broadcastToCommandSubscribers).toHaveBeenCalledWith(`${eventPrefix}:complete`, expect.objectContaining({
success: true,
exitCode: 0,
output: 'workspace outputworkspace warning',
}));
+ // Scoped to command subscribers — never the global io broadcast (#12019).
+ expect(io.emit).not.toHaveBeenCalled();
+ });
+
+ it('redacts secrets in every copy of the output: the streamed chunk, the completion frame, and the history row (#12019)', async () => {
+ allowWorkspace();
+ const child = createChildProcess();
+ spawnMock.mockReturnValue(child);
+ const { app } = createApp();
+
+ const response = await request(app)
+ .post('/api/commands/execute')
+ .send({ command: 'git config --list', workspacePath: WORKSPACE_PATH });
+ expect(response.status).toBe(202);
+
+ child.stdout.emit('data', Buffer.from('user.name=Example\ngithub.token=ghp_example_value\n'));
+ // A value split across two chunks is still redacted once the buffer is whole.
+ child.stdout.emit('data', Buffer.from('API_TO'));
+ child.stdout.emit('data', Buffer.from('KEN=split-example\n'));
+ child.emit('close', 0);
+
+ const eventPrefix = `command:${response.body.commandId}`;
+ const frames = broadcastToCommandSubscribers.mock.calls
+ .filter(([event]) => event === `${eventPrefix}:data`)
+ .map(([, payload]) => payload.data);
+ expect(frames[0]).toBe('user.name=Example\ngithub.token=[REDACTED]\n');
+ expect(frames.join('')).not.toContain('ghp_example_value');
+
+ const [, completion] = broadcastToCommandSubscribers.mock.calls.find(([event]) => event === `${eventPrefix}:complete`);
+ expect(completion.output).toBe('user.name=Example\ngithub.token=[REDACTED]\nAPI_TOKEN=[REDACTED]\n');
+
+ const [, , , historyRow] = logAction.mock.calls.find(([type, , , details]) => type === 'command' && details.exitCode === 0);
+ expect(historyRow.output).toBe('user.name=Example\ngithub.token=[REDACTED]\nAPI_TOKEN=[REDACTED]\n');
+ expect(JSON.stringify(logAction.mock.calls)).not.toMatch(/ghp_example_value|split-example/);
});
it('invokes onComplete exactly once with error details when child process fails to spawn, even if close emits after error', async () => {
allowWorkspace();
const child = createChildProcess();
spawnMock.mockReturnValue(child);
- const { app, io } = createApp();
+ const { app } = createApp();
const { logAction } = await import('../services/history.js');
const response = await request(app)
@@ -422,14 +464,14 @@ describe('commands routes', () => {
child.emit('close', null);
// Verify onComplete was called exactly once with the error details
- expect(io.emit).toHaveBeenCalledWith(`${eventPrefix}:complete`, {
+ expect(broadcastToCommandSubscribers).toHaveBeenCalledWith(`${eventPrefix}:complete`, {
success: false,
error: 'ENOENT: no such file or directory',
exitCode: 1,
});
// Count completion events to ensure only one was emitted
- const completionCalls = io.emit.mock.calls.filter(
+ const completionCalls = broadcastToCommandSubscribers.mock.calls.filter(
call => call[0] === `${eventPrefix}:complete`
);
expect(completionCalls).toHaveLength(1);
diff --git a/server/services/commands.js b/server/services/commands.js
index 19d066ce5b..2ed2acb683 100644
--- a/server/services/commands.js
+++ b/server/services/commands.js
@@ -1,6 +1,6 @@
import { spawn } from '../lib/childProcess.js';
import { logAction } from './history.js';
-import { ALLOWED_COMMANDS, validateCommand } from '../lib/commandSecurity.js';
+import { ALLOWED_COMMANDS, redactOutput, validateCommand } from '../lib/commandSecurity.js';
import { safeChildProcessEnv } from '../lib/processEnv.js';
import { withSpawnCwdEnv } from '../lib/spawnCwd.js';
@@ -52,16 +52,20 @@ export function executeCommand(command, workspacePath, onData, onComplete) {
activeCommands.set(commandId, child);
+ // Every copy of the output leaves this function redacted (#12019): each
+ // streamed chunk on its own, and the accumulated buffer once more at close
+ // so a secret split across two chunks is still caught in the persisted and
+ // completion copies.
child.stdout.on('data', (data) => {
const text = data.toString();
output += text;
- onData?.(text, 'stdout');
+ onData?.(redactOutput(text), 'stdout');
});
child.stderr.on('data', (data) => {
const text = data.toString();
output += text;
- onData?.(text, 'stderr');
+ onData?.(redactOutput(text), 'stderr');
});
child.on('close', (code) => {
@@ -70,20 +74,21 @@ export function executeCommand(command, workspacePath, onData, onComplete) {
activeCommands.delete(commandId);
const runtime = Date.now() - startTime;
const success = code === 0;
+ const redacted = redactOutput(output);
logAction('command', null, command.substring(0, 50), {
command,
workspacePath,
exitCode: code,
runtime,
- output: output.substring(0, 10000) // Truncate to prevent huge history
+ output: redacted.substring(0, 10000) // Truncate to prevent huge history
}, success, success ? null : `Exit code ${code}`);
onComplete?.({
success,
exitCode: code,
runtime,
- output
+ output: redacted
});
});
diff --git a/server/services/peerSocketRelayAuth.test.js b/server/services/peerSocketRelayAuth.test.js
index a72d58134d..39bb44c381 100644
--- a/server/services/peerSocketRelayAuth.test.js
+++ b/server/services/peerSocketRelayAuth.test.js
@@ -199,6 +199,27 @@ describe('peer socket relay stays connected through cos:subscribe on a password-
expect(client.connected).toBe(false);
});
+ it('a peer-token-authenticated socket is disconnected by command:subscribe, so it never joins the command-output audience (#12019)', async () => {
+ await setInstancePassword();
+ instanceRegistry.data.peers = [{ id: 'peer-record', name: 'Example Peer', instanceId: PEER_ID, enabled: true, syncSecret: PAIR_SECRET }];
+ const { derivePeerAuthToken, PEER_AUTH_HEADER, PEER_INSTANCE_HEADER } = await stage('import peerHttpClient.js', import('../lib/peerHttpClient.js'));
+
+ await startServer();
+ const client = connectClient({
+ [PEER_INSTANCE_HEADER]: PEER_ID,
+ [PEER_AUTH_HEADER]: derivePeerAuthToken(PAIR_SECRET, PEER_ID),
+ });
+
+ await waitFor(client, 'connect');
+ const subscribedAck = vi.fn();
+ client.on('command:subscribed', subscribedAck);
+ const disconnected = waitFor(client, 'disconnect');
+ client.emit('command:subscribe');
+ await disconnected;
+ expect(client.connected).toBe(false);
+ expect(subscribedAck).not.toHaveBeenCalled();
+ });
+
it('a Basic-authenticated socket also survives cos:subscribe but is disconnected by a shell:* event', async () => {
await setInstancePassword();
diff --git a/server/services/socket.js b/server/services/socket.js
index 5dfc573485..5071f69ace 100644
--- a/server/services/socket.js
+++ b/server/services/socket.js
@@ -87,6 +87,10 @@ const codeAnimationSubscribers = new Set();
// global emit.
const beeperSubscribers = new Set();
const fableLoomSubscribers = new Set();
+// Operator command-runner output (#12019). Never a global emit: the stream
+// carries host command output, and a peer relay socket (restricted to
+// `cos:subscribe`) must not receive it.
+const commandSubscribers = new Set();
// Store io instance for broadcasting
let ioInstance = null;
@@ -99,12 +103,17 @@ export function getIo() {
return ioInstance;
}
-const ALL_SUBSCRIBER_SETS = [cosSubscribers, errorSubscribers, notificationSubscribers, agentSubscribers, instanceSubscribers, loopSubscribers, codeAnimationSubscribers, beeperSubscribers, fableLoomSubscribers];
+const ALL_SUBSCRIBER_SETS = [cosSubscribers, errorSubscribers, notificationSubscribers, agentSubscribers, instanceSubscribers, loopSubscribers, codeAnimationSubscribers, beeperSubscribers, fableLoomSubscribers, commandSubscribers];
export function emitCodeAnimationChanged(id) {
broadcastToSet(codeAnimationSubscribers, 'code-animation:changed', { id });
}
+/** Emit a `command:<id>:*` frame to sockets that sent `command:subscribe`. */
+export function broadcastToCommandSubscribers(event, data) {
+ broadcastToSet(commandSubscribers, event, data);
+}
+
function broadcastToSet(set, event, data) {
const disconnected = [];
for (const s of set) {
@@ -214,6 +223,7 @@ function registerSubscriptionHandlers(socket, _io) {
registerSubscriber(socket, 'code-animation', codeAnimationSubscribers);
registerSubscriber(socket, 'beeper', beeperSubscribers);
registerSubscriber(socket, 'fableloom', fableLoomSubscribers);
+ registerSubscriber(socket, 'command', commandSubscribers);
}
function registerErrorHandlers(socket, io) {
diff --git a/server/services/socket.test.js b/server/services/socket.test.js
index f64e92affc..dbc764f844 100644
--- a/server/services/socket.test.js
+++ b/server/services/socket.test.js
@@ -101,7 +101,7 @@ vi.mock('./appUpdater.js', () => ({ updateApp: vi.fn() }));
vi.mock('./appDeployer.js', () => ({ hasDeployScript: vi.fn(), deployApp: vi.fn(), runDeployFlow: vi.fn() }));
vi.mock('../sockets/voice.js', () => ({ registerVoiceHandlers: vi.fn() }));
-import { initSocket } from './socket.js';
+import { broadcastToCommandSubscribers, initSocket } from './socket.js';
import { spawnPm2 } from './pm2.js';
import { getAppById, notifyAppsChanged } from './apps.js';
import { resolvePm2HomeForProcess } from './appProcessStatus.js';
@@ -267,6 +267,34 @@ describe('socket.js — initSocket', () => {
expect(io.emitted).toEqual([['meatspace:death-clock:changed', {}]]);
});
+ it('delivers operator command output only to command subscribers, never to every socket (#12019)', () => {
+ const subscriber = makeSocket('command-subscriber');
+ const cosOnly = makeSocket('cos-only-relay');
+ const bystander = makeSocket('bystander');
+ io.connect(subscriber);
+ io.connect(cosOnly);
+ io.connect(bystander);
+ createdSockets.push(subscriber, cosOnly, bystander);
+ subscriber.handlers['command:subscribe']();
+ // A peer relay is limited to cos:subscribe by PEER_RELAY_ALLOWED_EVENTS.
+ cosOnly.handlers['cos:subscribe']();
+ io.emitted.length = 0;
+
+ broadcastToCommandSubscribers('command:example-id:data', { data: 'TOKEN=[REDACTED]', stream: 'stdout' });
+ broadcastToCommandSubscribers('command:example-id:complete', { success: true, exitCode: 0 });
+
+ expect(subscriber.emitted).toContainEqual(['command:example-id:data', { data: 'TOKEN=[REDACTED]', stream: 'stdout' }]);
+ expect(subscriber.emitted).toContainEqual(['command:example-id:complete', { success: true, exitCode: 0 }]);
+ expect(cosOnly.emitted.some(([event]) => event.startsWith('command:'))).toBe(false);
+ expect(bystander.emitted.some(([event]) => event.startsWith('command:'))).toBe(false);
+ expect(io.emitted).toEqual([]);
+
+ subscriber.handlers['command:unsubscribe']();
+ subscriber.emitted.length = 0;
+ broadcastToCommandSubscribers('command:example-id:data', { data: 'after', stream: 'stdout' });
+ expect(subscriber.emitted.some(([event]) => event.startsWith('command:'))).toBe(false);
+ });
+
it('forwards dashboard invalidations without source records and gates CoS events on subscription', () => {
const subscriber = makeSocket('dashboard-subscriber');
const other = makeSocket('dashboard-other');