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 for live camera and microphone access (#11272)

Adam Eivy5dfbf0a68829
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.js
index 8d0fcffa86..960fad2854 100644
--- a/server/lib/hostControlRoutes.js
+++ b/server/lib/hostControlRoutes.js
@@ -11,7 +11,8 @@
*
* A route belongs here when it runs a process, git, npm, gh or a CoS agent on
* the host, or when it sets what one will run (a repo path, a start command, a
- * task prompt, a shell job). Read-only GETs stay open. Mutating routes in the
+ * task prompt, a shell job), or accesses live camera/microphone streams.
+ * Ordinary read-only GETs stay open. Mutating routes in the
* same families that are deliberately left OUT:
* - apps: delete/archive/unarchive, scope-adherence — they
* change only PortOS's own records or read files; nothing runs.
@@ -158,6 +159,12 @@ import { LLM_DRILL_TYPES } from './postDrillTypes.js';
import { escapeRegExp } from './textUtils.js';
export const HOST_CONTROL_ROUTES = Object.freeze([
+ // Capture activation and existing live feeds require operator authority (#11272).
+ // HEAD inherits the GET policy because Express invokes the same handler.
+ 'POST /api/media/start',
+ 'GET /api/media/video',
+ 'GET /api/media/audio',
+
// Rigging and retargeting spawn Blender/Python workers on the host for up to
// 20 minutes each (#10922).
'POST /api/rigging/models/:id',
@@ -996,7 +1003,7 @@ export const changedVoiceConfigHostControlPaths = (paths, body, current) => (
/** The HOST_CONTROL_ROUTES entry that `method path` (an Express `req.method` / `req.path`) matches, or null. */
export const hostControlRouteFor = (method, path) => {
if (typeof method !== 'string' || typeof path !== 'string') return null;
- const verb = method.toUpperCase();
+ const verb = method.toUpperCase() === 'HEAD' ? 'GET' : method.toUpperCase();
return COMPILED_ROUTES.find((entry) => entry.method === verb && entry.pattern.test(path))?.route ?? null;
};
diff --git a/server/routes/hostControlMediaCapture.test.js b/server/routes/hostControlMediaCapture.test.js
new file mode 100644
index 0000000000..2ef71c2e0a
--- /dev/null
+++ b/server/routes/hostControlMediaCapture.test.js
@@ -0,0 +1,141 @@
+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import { EventEmitter } from 'node:events';
+import { PassThrough, Readable } from 'node:stream';
+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';
+
+vi.mock('../lib/fileUtils.js', async (importOriginal) =>
+ makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-capture-authority-') }));
+afterAll(cleanupTempDataRoots);
+const auth = vi.hoisted(() => ({ enabled: false }));
+vi.mock('../services/auth.js', async (importOriginal) => ({
+ ...await importOriginal(),
+ isAuthEnabled: vi.fn(async () => auth.enabled),
+ verifyPassword: vi.fn(async () => true),
+}));
+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/mediaService.js', () => ({ default: {
+ listDevices: vi.fn(async () => []),
+ isVideoStreaming: vi.fn(() => true), isAudioStreaming: vi.fn(() => true),
+ startVideoStream: vi.fn(), startAudioStream: vi.fn(), stopAll: vi.fn(),
+ getVideoStream: vi.fn(), getAudioStream: vi.fn(),
+} }));
+
+import { authGate, hostControlRouteGate } from '../services/authGate.js';
+import { createSession } from '../services/auth.js';
+import mediaService from '../services/mediaService.js';
+import mediaRoutes from './media.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());
+ app.use('/api/media', mediaRoutes);
+ app.use(errorMiddleware);
+ return app;
+};
+const operations = [
+ ['post', '/api/media/start'],
+ ['get', '/api/media/video'], ['head', '/api/media/video'],
+ ['get', '/api/media/audio'], ['head', '/api/media/audio'],
+];
+const call = (app, [method, path], headers = {}) => {
+ const pending = request(app)[method](path);
+ for (const [name, value] of Object.entries(headers)) pending.set(name, value);
+ return method === 'post' ? pending.send({}) : pending;
+};
+const expectNoCaptureEffects = () => {
+ for (const effect of [mediaService.startVideoStream, mediaService.startAudioStream,
+ mediaService.getVideoStream, mediaService.getAudioStream]) expect(effect).not.toHaveBeenCalled();
+};
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ auth.enabled = false;
+});
+
+describe('capture and live-stream operator authority (#11272)', () => {
+ it('refuses remote and dev-proxied callers before device access or subscription to existing feeds', async () => {
+ const video = new PassThrough();
+ const audio = new PassThrough();
+ mediaService.getVideoStream.mockReturnValue(video);
+ mediaService.getAudioStream.mockReturnValue(audio);
+ for (const [address, headers] of [
+ ['192.0.2.10', {}],
+ ['127.0.0.1', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '192.0.2.10' }],
+ ['192.0.2.10', { [DEV_PROXY_CLIENT_ADDRESS_HEADER]: '127.0.0.1' }],
+ ]) {
+ for (const operation of operations) {
+ const response = await call(appFor(address), operation, headers);
+ expect(response.status, operation.join(' ')).toBe(403);
+ if (operation[0] !== 'head') expect(response.body.code).toBe('HOST_CONTROL_FORBIDDEN');
+ }
+ }
+ expectNoCaptureEffects();
+ for (const source of [video, audio]) {
+ expect(source.listenerCount('data')).toBe(0);
+ expect(source.listenerCount('end')).toBe(0);
+ }
+ });
+
+ it('protects case and trailing-slash aliases, including implicit HEAD', async () => {
+ for (const [method, path] of operations) {
+ expect((await call(appFor(), [method, path.toUpperCase() + '//'])).status).toBe(403);
+ }
+ expectNoCaptureEffects();
+ });
+
+ it('rejects anonymous and legacy Basic callers when a password is enabled', async () => {
+ auth.enabled = true;
+ for (const [headers, status] of [
+ [{}, 401],
+ [{ Authorization: 'Basic ' + Buffer.from(':example-password').toString('base64') }, 403],
+ ]) {
+ for (const operation of operations) expect((await call(appFor(), operation, headers)).status).toBe(status);
+ }
+ expectNoCaptureEffects();
+ });
+
+ it('preserves local capture and real human/agent sessions through the mounted streaming handlers', async () => {
+ const human = 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=${human.token}` }],
+ [true, '192.0.2.10', { Authorization: `Bearer ${agent.token}` }],
+ ]) {
+ auth.enabled = enabled;
+ for (const operation of operations) {
+ mediaService.getVideoStream.mockReturnValue(Readable.from([Buffer.from([0xff, 0xd8, 0xff, 0xd9])]));
+ mediaService.getAudioStream.mockReturnValue(Readable.from([Buffer.from('synthetic audio')]));
+ const response = await call(appFor(address), operation, headers);
+ expect(response.status, operation.join(' ')).toBe(200);
+ if (operation[0] === 'get') {
+ expect(response.headers['content-type']).toMatch(operation[1].endsWith('video') ? /^multipart/ : /^audio/);
+ expect(response.text.length).toBeGreaterThan(0);
+ }
+ }
+ }
+ expect(mediaService.startVideoStream).toHaveBeenCalledTimes(3);
+ expect(mediaService.startAudioStream).toHaveBeenCalledTimes(3);
+ expect(mediaService.getVideoStream).toHaveBeenCalledTimes(6);
+ expect(mediaService.getAudioStream).toHaveBeenCalledTimes(6);
+ });
+
+ it('keeps status, device discovery and stop behavior available', async () => {
+ const app = appFor();
+ for (const path of ['/api/media/status', '/api/media/devices']) expect((await request(app).get(path)).status).toBe(200);
+ expect((await request(app).post('/api/media/stop').send({})).status).toBe(200);
+ expect(mediaService.stopAll).toHaveBeenCalledOnce();
+ expectNoCaptureEffects();
+ });
+});