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
Require operator authority for automatic repository clone via Brain links and capture (#11037)
Adam Eivy2fe20ce75c4f
diff --git a/server/lib/hostControlRoutes.js b/server/lib/hostControlRoutes.jsindex be82142741..48befc335f 100644--- a/server/lib/hostControlRoutes.js+++ b/server/lib/hostControlRoutes.js@@ -733,8 +733,13 @@ export const HOST_CONTROL_ROUTES = Object.freeze(['POST /api/update/sync-fork',// Brain links (#8742): git clone/pull in the linked repo, opening a host- // app, or queuing a CoS malware-scan/repo-study agent. `links`/`buckets`- // CRUD and `scan-report` only read or record; nothing runs.+ // app, or queuing a CoS malware-scan/repo-study agent. `buckets` CRUD,+ // `scan-report` and link CRUD only read or record; nothing runs. The one+ // exception is a repository URL that auto-clones: POST /api/brain/links+ // (unless `autoClone: false`) and a bare-repo-URL POST /api/brain/capture+ // start a clone and may arm post-clone scan/study agents, so those two are+ // gated per request in their handlers (`assertHostControl`, #11037) rather+ // than listed here — a plain bookmark stays open.'POST /api/brain/links/:id/clone','POST /api/brain/links/:id/pull','POST /api/brain/links/:id/open-folder',diff --git a/server/routes/brainCapture.js b/server/routes/brainCapture.jsindex a8e634f329..fc4baa06a1 100644--- a/server/routes/brainCapture.js+++ b/server/routes/brainCapture.js@@ -7,6 +7,9 @@import { Router } from 'express';import * as brainService from '../services/brain.js';import { recordUserAction } from '../services/userActions.js';+import { assertHostControl } from '../services/authGate.js';+import { parseBareUrl } from '../lib/bareUrl.js';+import { deriveRepoLinkFields } from '../lib/repoLinkFields.js';import { asyncHandler, ServerError } from '../lib/errorHandler.js';import { validateRequest, brainInboxRetryBodySchema } from '../lib/validation.js';import {@@ -22,10 +25,15 @@ const router = Router();/*** POST /api/brain/capture- * Capture a thought, classify it, and store it+ * Capture a thought, classify it, and store it. A capture that is nothing but+ * a repository URL is filed as a link and auto-cloned (with any `repoIntake`+ * agents queued after), so it needs the same operator authority as creating+ * that link directly (#11037).*/router.post('/capture', asyncHandler(async (req, res) => {const { text, providerOverride, modelOverride, creative, repoIntake, note } = validateRequest(captureInputSchema, req.body);+ const bareUrl = parseBareUrl(text);+ if (bareUrl && deriveRepoLinkFields(bareUrl).isRepo) assertHostControl(req);const captureOptions = { creative, repoIntake };if (note !== undefined) captureOptions.note = note;const result = await brainService.captureThought(text, providerOverride, modelOverride, captureOptions);diff --git a/server/routes/brainLinks.js b/server/routes/brainLinks.jsindex 489c10ce06..a8070b30f0 100644--- a/server/routes/brainLinks.js+++ b/server/routes/brainLinks.js@@ -23,6 +23,7 @@ import {} from '../lib/brainValidation.js';import * as repoCloner from '../services/repoCloner.js';import { deriveRepoLinkFields } from '../lib/repoLinkFields.js';+import { assertHostControl } from '../services/authGate.js';import { queueMalwareScan, restudyRepoLink } from '../services/repoIntake.js';import { getScanReport } from '../services/malwareScanReports.js';@@ -115,10 +116,14 @@ router.get('/links/:id', asyncHandler(async (req, res) => {/*** POST /api/brain/links- * Create a new link (quick-add with URL)+ * Create a new link (quick-add with URL). A plain bookmark, or a repository+ * with `autoClone: false`, is a record; a repository that will auto-clone runs+ * git on the host and can queue post-clone CoS agents (`repoIntake`), so that+ * form needs operator authority (#11037).*/router.post('/links', asyncHandler(async (req, res) => {const { url, ...options } = validateRequest(linkInputSchema, req.body);+ if (deriveRepoLinkFields(url).isRepo && options.autoClone !== false) assertHostControl(req);// Check if URL already existsconst existing = await brainService.getLinkByUrl(url);diff --git a/server/routes/hostControlBrainRepoIntake.test.js b/server/routes/hostControlBrainRepoIntake.test.jsnew file mode 100644index 0000000000..0f880c626b--- /dev/null+++ b/server/routes/hostControlBrainRepoIntake.test.js@@ -0,0 +1,197 @@+import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest';+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';++// Session minting writes auth-sessions.json; keep it out of the real data/ tree.+vi.mock('../lib/fileUtils.js', async (importOriginal) =>+ makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-brain-repo-intake-') }));+afterAll(cleanupTempDataRoots);++// Automatic repository intake (#11037). Real Brain links + capture routers and+// the real Brain service sit behind the real auth + host-control gates; only+// storage (in memory), the git clone (a deferred promise) and the post-clone+// intake queue are doubled, so the assertions watch the three effects that+// must not happen for an unauthorized caller: a record, a clone, an agent.+const auth = vi.hoisted(() => ({ enabled: false }));+const store = vi.hoisted(() => ({ links: new Map(), inbox: [] }));+const clone = vi.hoisted(() => ({ cloneRepo: vi.fn(), runRepoIntake: vi.fn(async () => ({})) }));++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: { on: vi.fn(), off: vi.fn(), emit: vi.fn() },+ getSettings: vi.fn(async () => ({})),+}));+vi.mock('../services/instanceIdentity.js', () => ({+ loadData: vi.fn(async () => ({ peers: [] })),+ getInstanceId: vi.fn(async () => 'example-instance'),+ ensureInstanceId: vi.fn(async () => 'example-instance'),+ UNKNOWN_INSTANCE_ID: 'unknown',+}));+vi.mock('../services/userActions.js', () => ({ recordUserAction: vi.fn(async () => {}) }));+vi.mock('../services/repoCloner.js', () => ({+ cloneRepo: clone.cloneRepo,+ pullRepo: vi.fn(),+ reapStaleCloneStaging: vi.fn(async () => 0),+}));+vi.mock('../services/repoIntake.js', () => ({+ runRepoIntake: clone.runRepoIntake,+ queueMalwareScan: vi.fn(),+ restudyRepoLink: vi.fn(),+}));+vi.mock('../services/malwareScanReports.js', () => ({ getScanReport: vi.fn() }));+vi.mock('../lib/openFolder.js', () => ({ openFolderInSystemExplorer: vi.fn() }));+vi.mock('../services/brainStorage.js', async (importOriginal) => ({+ ...await importOriginal(),+ getLinkByUrl: vi.fn(async (url) => [...store.links.values()].find((link) => link.url === url) ?? null),+ getLinkById: vi.fn(async (id) => store.links.get(id) ?? null),+ createLink: vi.fn(async (data) => {+ const link = { id: `link-${store.links.size + 1}`, ...data };+ store.links.set(link.id, link);+ return link;+ }),+ updateLink: vi.fn(async (id, patch) => {+ if (!store.links.has(id)) return null;+ const link = { ...store.links.get(id), ...patch };+ store.links.set(id, link);+ return link;+ }),+ createInboxLog: vi.fn(async (entry) => {+ const log = { id: `inbox-${store.inbox.length + 1}`, ...entry };+ store.inbox.push(log);+ return log;+ }),+}));++import { authGate, hostControlRouteGate } from '../services/authGate.js';+import { createSession } from '../services/auth.js';+import brainRoutes from './brain.js';++const REPO_URL = 'https://github.com/example-org/example-repo';+const intake = { malwareScan: true, learn: true, providerId: 'example-cli', model: 'example-model', effort: 'high' };+const operations = {+ 'link create': ['/api/brain/links', { url: REPO_URL, repoIntake: intake }],+ 'bare-URL capture': ['/api/brain/capture', { text: REPO_URL, repoIntake: intake }],+};++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/brain', brainRoutes);+ app.use(errorMiddleware);+ return app;+};+const post = (app, [path, body], headers = {}) => {+ const pending = request(app).post(path);+ for (const [name, value] of Object.entries(headers)) pending.set(name, value);+ return pending.send(body);+};+// Let the un-awaited clone chain settle, so a deferred side effect would show.+const settle = () => new Promise((resolve) => setTimeout(resolve, 20));++beforeEach(() => {+ vi.clearAllMocks();+ auth.enabled = false;+ store.links.clear();+ store.inbox.length = 0;+ clone.cloneRepo.mockResolvedValue({ localPath: '/repos/example-org/example-repo' });+});++describe('automatic repository intake operator authority (#11037)', () => {+ it.each(Object.entries(operations))('refuses a remote password-free %s before any record, clone or agent', async (_name, operation) => {+ 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' }],+ ]) {+ const response = await post(appFor(address), operation, headers);+ expect([response.status, response.body.code]).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);+ }+ await settle();+ expect(store.links.size).toBe(0);+ expect(store.inbox).toHaveLength(0);+ expect(clone.cloneRepo).not.toHaveBeenCalled();+ expect(clone.runRepoIntake).not.toHaveBeenCalled();+ });++ it('never lets a refused request leave a record that queues an agent when a clone later completes', async () => {+ let finishClone;+ clone.cloneRepo.mockReturnValue(new Promise((resolve) => { finishClone = resolve; }));++ // An authorized request is in flight; the refused one beside it must add nothing.+ expect((await post(appFor('127.0.0.1'), operations['link create'])).status).toBe(201);+ expect((await post(appFor('192.0.2.10'), ['/api/brain/links', { url: `${REPO_URL}-two`, repoIntake: intake }])).status).toBe(403);+ finishClone({ localPath: '/repos/example-org/example-repo' });+ await settle();++ expect(store.links.size).toBe(1);+ expect(clone.cloneRepo).toHaveBeenCalledTimes(1);+ expect(clone.runRepoIntake).toHaveBeenCalledTimes(1);+ });++ it('requires an operator session with a password and denies legacy Basic', async () => {+ auth.enabled = true;+ for (const operation of Object.values(operations)) {+ expect((await post(appFor(), operation)).status).toBe(401);+ const basic = { Authorization: 'Basic ' + Buffer.from(':example-password').toString('base64') };+ const response = await post(appFor(), operation, basic);+ expect([response.status, response.body.code]).toEqual([403, 'HOST_CONTROL_FORBIDDEN']);+ }+ await settle();+ expect(store.links.size).toBe(0);+ expect(clone.cloneRepo).not.toHaveBeenCalled();+ expect(clone.runRepoIntake).not.toHaveBeenCalled();+ });++ it('keeps clone and post-clone intake working for local callers, operator sessions and delegated agent sessions', async () => {+ const operator = await createSession();+ const agent = await createSession({ label: 'agent' });+ let n = 0;+ for (const [enabled, address, headers] of [+ [false, '127.0.0.1', {}],+ [true, '192.0.2.10', { Authorization: `Bearer ${operator.token}` }],+ [true, '192.0.2.10', { Authorization: `Bearer ${agent.token}` }],+ ]) {+ auth.enabled = enabled;+ for (const [path, body] of Object.values(operations)) {+ n += 1;+ const unique = { ...body, ...(body.url ? { url: `${REPO_URL}-${n}` } : { text: `${REPO_URL}-${n}` }) };+ const response = await post(appFor(address), [path, unique], headers);+ expect(response.status, `${path}: ${JSON.stringify(response.body)}`).toBeLessThan(300);+ }+ }+ await settle();+ expect(clone.cloneRepo).toHaveBeenCalledTimes(n);+ expect(clone.runRepoIntake).toHaveBeenCalledTimes(n);+ // The provider/model/effort pins ride along on the persisted record.+ expect(clone.runRepoIntake.mock.calls[0][1]).toEqual(expect.objectContaining(intake));+ });++ it('keeps plain bookmarks and autoClone:false repository records open to a remote caller, data-only', async () => {+ for (const body of [+ { url: 'https://example.com/article' },+ { url: REPO_URL, autoClone: false, repoIntake: intake },+ ]) {+ const response = await post(appFor(), ['/api/brain/links', body]);+ expect(response.status, JSON.stringify(response.body)).toBe(201);+ expect(response.body.cloneStatus).toBe('none');+ expect(response.body.repoIntake).toBeUndefined();+ }+ const capture = await post(appFor(), ['/api/brain/capture', { text: 'https://example.com/article-two' }]);+ expect(capture.status).toBe(200);+ await settle();+ expect(store.links.size).toBe(3);+ expect(clone.cloneRepo).not.toHaveBeenCalled();+ expect(clone.runRepoIntake).not.toHaveBeenCalled();+ });+});diff --git a/server/services/authGate.js b/server/services/authGate.jsindex 870d96fc76..b1cae21597 100644--- a/server/services/authGate.js+++ b/server/services/authGate.js@@ -252,13 +252,25 @@ export const socketHasCurrentHostControl = async (socket) => {return socketHasHostControl(socket);};+const hostControlRefusal = (req) => {+ logSecurityEvent('host-control.refused', { refused: true, ip: req.ip, method: req.method, path: req.originalUrl?.split('?')[0] ?? req.path, auth: req.portosAuthContext?.method ?? 'none' });+ return new ServerError(HOST_CONTROL_FORBIDDEN_MESSAGE, {+ status: 403, code: 'HOST_CONTROL_FORBIDDEN',+ });+};+// Mount after authGate: missing context fails closed.export const requireHostControl = (req, res, next) => {if (requestHasHostControl(req)) return next();- logSecurityEvent('host-control.refused', { refused: true, ip: req.ip, method: req.method, path: req.originalUrl?.split('?')[0] ?? req.path, auth: req.portosAuthContext?.method ?? 'none' });- sendErrorResponse(res, new ServerError(HOST_CONTROL_FORBIDDEN_MESSAGE, {- status: 403, code: 'HOST_CONTROL_FORBIDDEN',- }));+ sendErrorResponse(res, hostControlRefusal(req));+};++// The in-handler twin of requireHostControl, for a route that is open for+// plain records and needs operator authority only when the validated request+// would start host work (e.g. a repository link that auto-clones). Throws the+// same 403 so the route's error middleware answers it.+export const assertHostControl = (req) => {+ if (!requestHasHostControl(req)) throw hostControlRefusal(req);};// Applies requireHostControl to every route in the audited