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
fix(security): apply per-peer sharing settings to brain, memory and catalog pulls
Adam Eivy26e83f71eb92
diff --git a/docs/PEER_PUSH_AUTH.md b/docs/PEER_PUSH_AUTH.mdindex 599c48fb38..d1e767d23d 100644--- a/docs/PEER_PUSH_AUTH.md+++ b/docs/PEER_PUSH_AUTH.md@@ -84,7 +84,11 @@ does not admit.| GET | `/data/{images,image-refs,videos,music,audio,writers-room/works}/*` | Asset pulls |GET includes HEAD. Paths with `.`/`..`/empty segments or encoded separators-are refused. The generic `GET /api/instances/peers/:id/query` proxy reaches only+are refused. Brain, memory, catalog and `/api/sync/*` reads also check this+install's sharing settings for the caller (`authorizePeerPull`): a peer whose+category is off is warned about, and refused with `403 PEER_PULL_FORBIDDEN`+when `federation.strictPullAuthorization` is on (the digital twin, MeatSpace+and character snapshots are always refused). The generic `GET /api/instances/peers/:id/query` proxy reaches onlythese paths on a paired peer.`POST /api/instances/peers/pair-secret` is a separate Basic-auth bootstrap, notdiff --git a/server/routes/brainSync.js b/server/routes/brainSync.jsindex 42ca3a830e..dbe60b4608 100644--- a/server/routes/brainSync.js+++ b/server/routes/brainSync.js@@ -22,6 +22,14 @@ import * as brainParity from '../services/brainParity.js';const router = Router();+// Peers pull these (PEER_API_SURFACE). Gate them on the per-peer `brain`+// category like the /api/sync snapshot reads (#5663); a checksum or manifest+// fingerprints the same records, so it must not become the weaker door.+const authorizeBrainPull = async (req, route) => {+ const { authorizePeerPull } = await import('../services/sharing/peerPullAuthorization.js');+ return authorizePeerPull(req, { syncCategory: 'brain', route });+};+/*** POST /api/brain/bridge-sync* Sync all brain data to CoS memory system (generates embeddings).@@ -54,6 +62,7 @@ router.get('/embeddings/status', asyncHandler(async (_req, res) => {*/router.get('/sync', asyncHandler(async (req, res) => {const { since, limit } = validateRequest(brainSyncQuerySchema, req.query);+ await authorizeBrainPull(req, 'brain sync');const result = await brainSyncLog.getChangesSince(since, limit);res.json(result);}));@@ -75,6 +84,7 @@ router.post('/sync', asyncHandler(async (req, res) => {* full snapshot below. Lightweight (one hash) so it's cheap to poll each cycle.*/router.get('/reconcile/checksum', asyncHandler(async (req, res) => {+ await authorizeBrainPull(req, 'brain checksum');const checksum = await brainReconcile.getBrainChecksum();res.json({ checksum });}));@@ -86,6 +96,7 @@ router.get('/reconcile/checksum', asyncHandler(async (req, res) => {* the receiver applies it idempotently via applyRemoteRecord.*/router.get('/reconcile/snapshot', asyncHandler(async (req, res) => {+ await authorizeBrainPull(req, 'brain snapshot');const snapshot = await brainReconcile.getBrainSnapshot();res.json(snapshot);}));@@ -98,7 +109,8 @@ router.get('/reconcile/snapshot', asyncHandler(async (req, res) => {* pulls brain content it wouldn't otherwise receive. Far cheaper than* /reconcile/snapshot, which older peers fall back to.*/-router.get('/reconcile/manifest', asyncHandler(async (_req, res) => {+router.get('/reconcile/manifest', asyncHandler(async (req, res) => {+ await authorizeBrainPull(req, 'brain manifest');res.json(await brainParity.buildBrainManifest());}));diff --git a/server/routes/brainSync.test.js b/server/routes/brainSync.test.jsindex a379f6393d..9a9f944813 100644--- a/server/routes/brainSync.test.js+++ b/server/routes/brainSync.test.js@@ -24,9 +24,24 @@ vi.mock('../services/brainParity.js', () => ({getBrainParityReports: vi.fn(),runBrainParityCheck: vi.fn(),}));+// The real peer-pull gate runs; only the peer registry and settings it reads+// are stubbed, as in routes/dataSync.test.js.+const peers = [];+vi.mock('../services/instances.js', async () => ({+ ...(await vi.importActual('../services/instances.js')),+ getPeers: async () => peers,+}));+let settings = {};+vi.mock('../services/settings.js', async () => ({+ ...(await vi.importActual('../services/settings.js')),+ getSettings: async () => settings,+}));import * as brainParity from '../services/brainParity.js';import * as brainSync from '../services/brainSync.js';+import * as brainSyncLog from '../services/brainSyncLog.js';+import * as brainReconcile from '../services/brainReconcile.js';+import { PEER_INSTANCE_ID_HEADER, __resetPullWarnThrottleForTests } from '../services/sharing/peerPullAuthorization.js';import brainSyncRoutes from './brainSync.js';const buildApp = () => {@@ -39,6 +54,59 @@ const buildApp = () => {beforeEach(() => {vi.clearAllMocks();+ peers.length = 0;+ settings = {};+ __resetPullWarnThrottleForTests();+});++describe('brain pulls honor the per-peer brain category', () => {+ const PEER_ID = 'peer-a-instance-id';+ const peer = (syncCategories) => ({+ instanceId: PEER_ID, name: 'Example Peer', enabled: true, syncEnabled: true,+ directions: ['outbound', 'inbound'], syncCategories,+ });+ const reads = [+ ['/api/brain/sync', () => brainSyncLog.getChangesSince],+ ['/api/brain/reconcile/checksum', () => brainReconcile.getBrainChecksum],+ ['/api/brain/reconcile/snapshot', () => brainReconcile.getBrainSnapshot],+ ['/api/brain/reconcile/manifest', () => brainParity.buildBrainManifest],+ ];+ beforeEach(() => {+ brainSyncLog.getChangesSince.mockResolvedValue({ changes: [], maxSeq: 0, hasMore: false });+ brainReconcile.getBrainChecksum.mockResolvedValue('abc');+ brainReconcile.getBrainSnapshot.mockResolvedValue({ records: {} });+ brainParity.buildBrainManifest.mockResolvedValue({ types: {} });+ });++ it.each(reads)('strict mode refuses %s to a peer paired without brain sync', async (path, read) => {+ settings = { federation: { strictPullAuthorization: true } };+ peers.push(peer({ brain: false, catalog: true }));++ const res = await request(buildApp()).get(path).set(PEER_INSTANCE_ID_HEADER, PEER_ID);++ expect(res.status).toBe(403);+ expect(res.body.code).toBe('PEER_PULL_FORBIDDEN');+ expect(read()).not.toHaveBeenCalled();+ });++ it.each(reads)('serves %s to a peer the user shares brain with', async (path, read) => {+ settings = { federation: { strictPullAuthorization: true } };+ peers.push(peer({ brain: true }));++ const res = await request(buildApp()).get(path).set(PEER_INSTANCE_ID_HEADER, PEER_ID);++ expect(res.status).toBe(200);+ expect(read()).toHaveBeenCalledOnce();+ });++ it('keeps the warn-first ramp: without strict mode a denied pull is still served', async () => {+ peers.push(peer({ brain: false }));++ const res = await request(buildApp()).get('/api/brain/reconcile/snapshot').set(PEER_INSTANCE_ID_HEADER, PEER_ID);++ expect(res.status).toBe(200);+ expect(brainReconcile.getBrainSnapshot).toHaveBeenCalledOnce();+ });});describe('POST /api/brain/sync', () => {diff --git a/server/routes/catalog.js b/server/routes/catalog.jsindex 3ca2d3b082..26025c215f 100644--- a/server/routes/catalog.js+++ b/server/routes/catalog.js@@ -809,6 +809,9 @@ router.get('/export', asyncHandler(async (req, res) => {}));router.get('/sync', asyncHandler(async (req, res) => {+ // Peers pull this (PEER_API_SURFACE): honor the per-peer `catalog` category.+ const { authorizePeerPull } = await import('../services/sharing/peerPullAuthorization.js');+ await authorizePeerPull(req, { syncCategory: 'catalog', route: 'catalog sync' });// The four `sync_sequence` columns are independent — accept either// `?since=N` (uniform; only meaningful on the first pull where everyone's// at 0) or `?since[scraps]=A&since[ingredients]=B&...` for subsequentdiff --git a/server/routes/catalogSyncPull.test.js b/server/routes/catalogSyncPull.test.jsnew file mode 100644index 0000000000..0a7385236d--- /dev/null+++ b/server/routes/catalogSyncPull.test.js@@ -0,0 +1,63 @@+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 { cleanupTempDataRoots, lazyTempDataRoot, makePathsProxy } from '../lib/mockPathsDataRoot.js';++// GET /api/catalog/sync is a peer pull (PEER_API_SURFACE). The real peer-pull+// gate runs; only the peer registry, settings and the sync read are stubbed.+vi.mock('../lib/fileUtils.js', async (importOriginal) =>+ makePathsProxy(await importOriginal(), { dataRoot: () => lazyTempDataRoot('portos-catalog-sync-pull-') }));+afterAll(cleanupTempDataRoots);+const peers = [];+vi.mock('../services/instances.js', async () => ({+ ...(await vi.importActual('../services/instances.js')),+ getPeers: async () => peers,+}));+let settings = {};+vi.mock('../services/settings.js', async () => ({+ ...(await vi.importActual('../services/settings.js')),+ getSettings: async () => settings,+}));+vi.mock('../services/catalogSync.js', () => ({+ getChangesSince: vi.fn(async () => ({ changes: {}, maxSequences: {}, hasMore: false })),+ applyRemoteChanges: vi.fn(),+}));++import * as catalogSync from '../services/catalogSync.js';+import { PEER_INSTANCE_ID_HEADER } from '../services/sharing/peerPullAuthorization.js';+import catalogRoutes from './catalog.js';++const PEER_ID = 'peer-a-instance-id';+const buildApp = () => {+ const app = express();+ app.use('/api/catalog', catalogRoutes);+ app.use(errorMiddleware);+ return app;+};++describe('GET /api/catalog/sync peer pull authorization', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ settings = { federation: { strictPullAuthorization: true } };+ peers.length = 0;+ });++ it('refuses a peer the user does not share the catalog with', async () => {+ peers.push({ instanceId: PEER_ID, enabled: true, syncEnabled: true, directions: ['outbound', 'inbound'], syncCategories: { brain: true } });++ const res = await request(buildApp()).get('/api/catalog/sync?since=0').set(PEER_INSTANCE_ID_HEADER, PEER_ID);++ expect(res.status).toBe(403);+ expect(catalogSync.getChangesSince).not.toHaveBeenCalled();+ });++ it('serves a peer with the catalog category on', async () => {+ peers.push({ instanceId: PEER_ID, enabled: true, syncEnabled: true, directions: ['outbound', 'inbound'], syncCategories: { catalog: true } });++ const res = await request(buildApp()).get('/api/catalog/sync?since=0').set(PEER_INSTANCE_ID_HEADER, PEER_ID);++ expect(res.status).toBe(200);+ expect(catalogSync.getChangesSince).toHaveBeenCalledOnce();+ });+});diff --git a/server/routes/memory.js b/server/routes/memory.jsindex 75fc97f89c..9746760ced 100644--- a/server/routes/memory.js+++ b/server/routes/memory.js@@ -85,6 +85,9 @@ router.get('/sync', asyncHandler(async (req, res) => {throw new ServerError('Sync requires PostgreSQL backend', { status: 400 });}const { since, limit, schemaVersion } = validateRequest(memorySyncQuerySchema, req.query);+ // Peers pull this (PEER_API_SURFACE): honor the per-peer `memory` category.+ const { authorizePeerPull } = await import('../services/sharing/peerPullAuthorization.js');+ await authorizePeerPull(req, { syncCategory: 'memory', route: 'memory sync' });const result = schemaVersion === undefined? await memorySync.getChangesSince(since, limit): await memorySync.getChangesSince(since, limit, schemaVersion);diff --git a/server/routes/memory.test.js b/server/routes/memory.test.jsindex bc4cf0cbef..c4370952df 100644--- a/server/routes/memory.test.js+++ b/server/routes/memory.test.js@@ -52,6 +52,20 @@ vi.mock('../services/memorySync.js', () => ({applyRemoteChanges: vi.fn()}));+// The real peer-pull gate runs; only the peer registry and settings it reads+// are stubbed, as in routes/dataSync.test.js.+const peers = [];+vi.mock('../services/instances.js', async () => ({+ ...(await vi.importActual('../services/instances.js')),+ getPeers: async () => peers,+}));+let settings = {};+vi.mock('../services/settings.js', async () => ({+ ...(await vi.importActual('../services/settings.js')),+ getSettings: async () => settings,+}));++import { PEER_INSTANCE_ID_HEADER } from '../services/sharing/peerPullAuthorization.js';import { ensureBackend, getMemories, getTimeline, archiveMemory, purgeMemory, applyDecay, linkMemories } from '../services/memoryBackend.js';import { checkHealth } from '../lib/db.js';import * as memorySync from '../services/memorySync.js';@@ -75,6 +89,8 @@ describe('Memory Routes', () => {app.use(express.json());app.use('/api/memory', memoryRoutes);vi.clearAllMocks();+ settings = {};+ peers.length = 0;});describe('DELETE /api/memory/:id', () => {@@ -203,6 +219,20 @@ describe('Memory Routes', () => {expect(memorySync.getChangesSince).toHaveBeenCalledWith('10', 50, 3);});+ it('strict mode refuses a peer the user does not share memory with', async () => {+ ensureBackend.mockResolvedValue('postgres');+ settings = { federation: { strictPullAuthorization: true } };+ peers.splice(0, peers.length, {+ instanceId: 'peer-a-instance-id', enabled: true, syncEnabled: true,+ directions: ['outbound', 'inbound'], syncCategories: { brain: true, memory: false },+ });++ const response = await request(app).get('/api/memory/sync').set(PEER_INSTANCE_ID_HEADER, 'peer-a-instance-id');++ expect(response.status).toBe(403);+ expect(memorySync.getChangesSince).not.toHaveBeenCalled();+ });+it('passes a schema-version conflict through at the HTTP boundary', async () => {ensureBackend.mockResolvedValue('postgres');memorySync.getChangesSince.mockRejectedValue(Object.assign(new Error('schema version is ahead'), { status: 409 }));