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 sessions for peer admission settings (#8225)

[._.]/ Adam Eivy5113d8b46da1
diff --git a/client/src/components/instances/PeerSyncSecretEditor.jsx b/client/src/components/instances/PeerSyncSecretEditor.jsx
index 1a83236bae..42f52398bf 100644
--- a/client/src/components/instances/PeerSyncSecretEditor.jsx
+++ b/client/src/components/instances/PeerSyncSecretEditor.jsx
@@ -32,7 +32,7 @@ export default function PeerSyncSecretEditor({ peer, onRefresh }) {
<p id={`peer-sync-secret-help-${peer.id}`} className="text-gray-400">
Enter the same randomly generated secret (32–256 characters) in this peer’s settings on both machines.
Use a different secret for each pair. Sync and category switches still control what is accepted.
- Configure from the local interface, or sign in with an instance password.
+ Sign in with an instance password to configure peer settings.
</p>
<div className="flex flex-wrap gap-3">
<button disabled={saving || secret.length < 32} onClick={() => save(secret)} className="text-port-success disabled:opacity-50">Save secret</button>
diff --git a/docs/PEER_PUSH_AUTH.md b/docs/PEER_PUSH_AUTH.md
index 703169bfa6..06f25a3d73 100644
--- a/docs/PEER_PUSH_AUTH.md
+++ b/docs/PEER_PUSH_AUTH.md
@@ -6,8 +6,9 @@ or its network address does not authorize record ingestion.
## Configure a pair
-1. Upgrade both instances. Open **Instances** from the local interface on each
- machine, or sign in with that machine's instance password for remote settings.
+1. Upgrade both instances and sign in with an operator session on each machine.
+ Peer configuration requires a session even on loopback, because a development
+ proxy can relay remote traffic over a local socket. Open **Instances**.
2. On each matching peer card, select **Set sync secret** and enter the same
randomly generated secret (32–256 characters). Use a different secret for each
pair; a password manager can generate and transfer it.
@@ -42,5 +43,7 @@ An explicit record pull uses the locally configured peer destination and verifie
that the response's source, kind, and record ID match that request. It still
requires local pairing, an enabled peer, sync consent, and category permission.
Discovery may list an unpaired peer; discovery alone cannot admit its record pushes.
-Peer configuration changes require an operator session or a direct loopback
-connection with a local browser origin while the instance password is off.
+Peer configuration changes require a verified operator session. If the instance
+password is unset, set one and sign in to configure the pair; it may be disabled
+after pairing. The peer-specific checks continue to apply while the global
+password is unset. Existing host-issued operator session tokens also work.
diff --git a/server/routes/instances.js b/server/routes/instances.js
index 5214b94d75..7648ea2422 100644
--- a/server/routes/instances.js
+++ b/server/routes/instances.js
@@ -5,10 +5,8 @@
*/
import { Router } from 'express';
-import { extractToken, verifySession, isAuthEnabled } from '../services/auth.js';
+import { extractToken, verifySession } from '../services/auth.js';
import { isCrossOrigin } from '../../lib/portosAuthCore.js';
-import { isRemoteRequest } from '../lib/requestOrigin.js';
-import { isLoopbackHostname } from '../lib/beeperOAuthOrigin.js';
import { z } from 'zod';
import * as instances from '../services/instances.js';
import { getSelf, updateSelf } from '../services/instanceIdentity.js';
@@ -25,17 +23,16 @@ import { federatedMediaPeerSettingsSchema, optionalBooleanMap, validateRequest }
const router = Router();
// Peer admission is local authority even when the instance password is off.
-// Discovery callbacks remain reachable, but cannot provision the pair secret.
+// Announcements remain discoverable. Reciprocal category changes also require
+// local authority: an anonymous callback cannot admit a discovery-only peer.
router.use(asyncHandler(async (req, _res, next) => {
if (!req.path.toLowerCase().startsWith('/peers') || ['GET', 'HEAD', 'OPTIONS'].includes(req.method)
- || ['/peers/announce', '/peers/sync-categories'].includes(req.path.toLowerCase().replace(/\/$/, ''))) return next();
- const localAddress = String(req.socket?.remoteAddress || '').replace(/^::ffff:/, '');
- const origin = req.get('origin');
- const localOrigin = !origin || (URL.canParse(origin) && isLoopbackHostname(new URL(origin).hostname));
- const local = !isRemoteRequest(req) && isLoopbackHostname(localAddress)
- && isLoopbackHostname(req.hostname) && localOrigin;
- if (!isCrossOrigin(req) && (await verifySession(extractToken(req)) || (!await isAuthEnabled() && local))) return next();
- throw new ServerError('Peer settings require an operator session or the local interface', {
+ || req.path.toLowerCase().replace(/\/$/, '') === '/peers/announce') return next();
+ // Vite and other private transports can forward a remote request over a
+ // loopback socket. Neither that socket nor supplied Origin/Host proves the
+ // operator's identity. Verify the existing session even when auth is off.
+ if (!isCrossOrigin(req) && await verifySession(extractToken(req))) return next();
+ throw new ServerError('Sign in with an operator session to change peer settings', {
status: 403, code: 'PEER_SETTINGS_OPERATOR_REQUIRED',
});
}));
diff --git a/server/routes/instances.test.js b/server/routes/instances.test.js
index e16d3478b0..baf87289a1 100644
--- a/server/routes/instances.test.js
+++ b/server/routes/instances.test.js
@@ -4,7 +4,7 @@ import { remoteRequestHandler } from '../lib/requestOrigin.js';
import { verifySession } from '../services/auth.js';
vi.mock('../services/auth.js', () => ({
extractToken: req => req.headers.authorization,
- verifySession: vi.fn().mockResolvedValue(false),
+ verifySession: vi.fn().mockResolvedValue(true),
isAuthEnabled: vi.fn().mockResolvedValue(false),
}));
import { request } from '../lib/testHelper.js';
@@ -434,6 +434,18 @@ describe('local peer admission configuration', () => {
expect(res.status).toBe(403);
expect(instances.updatePeer).not.toHaveBeenCalled();
});
+ it('rejects apparent loopback access without an operator session', async () => {
+ const res = await request(buildApp()).put('/api/instances/peers/peer-a')
+ .send({ syncSecret: 'synthetic-pair-secret-32-characters-long' });
+ expect(res.status).toBe(403);
+ expect(instances.updatePeer).not.toHaveBeenCalled();
+ });
+ it('refuses anonymous reciprocal admission of discovery peers', async () => {
+ const res = await request(remoteRequestHandler(buildApp())).post('/api/instances/peers/sync-categories')
+ .send({ instanceId: '191aaece-a492-41ee-a66d-d4661eadc132', syncCategories: { universe: true }, fullSync: true });
+ expect(res.status).toBe(403);
+ expect(instances.applyReciprocalSync).not.toHaveBeenCalled();
+ });
it('allows an authenticated operator to configure the secret remotely', async () => {
vi.mocked(verifySession).mockResolvedValue(true);
instances.updatePeer.mockResolvedValue({ id: 'peer-a', hasSyncSecret: true });
diff --git a/server/services/instances.js b/server/services/instances.js
index 6cd895ad0b..34fea530bf 100644
--- a/server/services/instances.js
+++ b/server/services/instances.js
@@ -526,7 +526,8 @@ export async function updatePeer(id, updates) {
peer.syncSecret = updates.syncSecret;
// Entering the pair secret is the local inbound-admission action.
if (peer.syncSecret && !peer.directions?.includes('inbound')) {
- peer.directions = [...(peer.directions || []), 'inbound'];
+ const directions = Array.isArray(peer.directions) && peer.directions.length ? peer.directions : ['outbound'];
+ peer.directions = [...directions, 'inbound'];
}
}
if (updates.name !== undefined) peer.name = validName(updates.name, peer.name);
diff --git a/server/services/instances.test.js b/server/services/instances.test.js
index 2d2f31a64b..9333efa6ef 100644
--- a/server/services/instances.test.js
+++ b/server/services/instances.test.js
@@ -362,6 +362,14 @@ describe('instances.js', () => {
});
});
+ it('pairing a legacy peer preserves its outbound relationship', async () => {
+ const peer = { id: 'legacy-peer', instanceId: 'peer-a', enabled: true, syncEnabled: false };
+ readJSONFile.mockResolvedValue({ self: { instanceId: 'local-instance' }, peers: [peer] });
+ await updatePeer('legacy-peer', { syncSecret: 'synthetic-pair-secret-32-characters-long' });
+ expect(peer.directions).toEqual(['outbound', 'inbound']);
+ expect(peer.syncEnabled).toBe(false);
+ });
+
describe('redactPeerForWire', () => {
it('strips credentials and local media-routing state before a peer crosses the wire', () => {
const peer = {