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: sharpen the snapshot pull gate's denial reasons and master-switch handling (#5663)
Adam Eivy2102afb853dd
diff --git a/server/services/sharing/peerPullAuthorization.js b/server/services/sharing/peerPullAuthorization.jsindex 3a404a53f0..a70f12d73c 100644--- a/server/services/sharing/peerPullAuthorization.js+++ b/server/services/sharing/peerPullAuthorization.js@@ -36,7 +36,7 @@* covers shipping an identity record to a host we cannot name.*/import { ServerError } from '../../lib/errorHandler.js';-import { findPeerById, peerAllowsOutbound, peerAllowsCategoryPull, peerHasCategory } from './peerSyncShared.js';+import { findPeerById, peerAllowsOutbound, peerOutboundEligible, peerAllowsCategoryPull, peerHasCategory } from './peerSyncShared.js';import { getSettings } from '../settings.js';import { UNKNOWN_INSTANCE_ID } from '../instances.js';@@ -102,9 +102,14 @@ export async function decidePeerPull({ callerId, recordKind = null, syncCategoryconst peer = await findPeerById(callerId);if (!peer) return { allowed: false, reason: PULL_DENY_UNKNOWN_PEER, peer: null, callerId };if (syncCategory) {- return peerAllowsCategoryPull(peer, syncCategory)- ? { allowed: true, reason: null, peer, callerId }- : { allowed: false, reason: PULL_DENY_CATEGORY, peer, callerId };+ // Split the two denials so the reason a caller/log sees means the same+ // thing it does on the record routes: "we don't share with you at all" vs+ // "we don't share THIS with you".+ if (!peerOutboundEligible(peer)) return { allowed: false, reason: PULL_DENY_OUTBOUND, peer, callerId };+ if (!peerAllowsCategoryPull(peer, syncCategory)) {+ return { allowed: false, reason: PULL_DENY_CATEGORY, peer, callerId };+ }+ return { allowed: true, reason: null, peer, callerId };}if (!peerAllowsOutbound(peer)) return { allowed: false, reason: PULL_DENY_OUTBOUND, peer, callerId };if (recordKind && !peerHasCategory(peer, recordKind)) {diff --git a/server/services/sharing/peerPullAuthorization.test.js b/server/services/sharing/peerPullAuthorization.test.jsindex 52e2fa9f6c..9da6a4aba0 100644--- a/server/services/sharing/peerPullAuthorization.test.js+++ b/server/services/sharing/peerPullAuthorization.test.js@@ -217,7 +217,26 @@ describe('peerPullAuthorization', () => {it('denies a peer that only announced itself (inbound-only, never approved here)', async () => {setPeers(universePeer({ directions: ['inbound'] }));- expect((await decidePeerPull({ callerId: PEER_A, syncCategory: 'universe' })).allowed).toBe(false);+ // Reason parity with the record routes: "we don't share with you at all"+ // must not read as "we don't share THIS with you".+ expect(await decidePeerPull({ callerId: PEER_A, syncCategory: 'universe' }))+ .toMatchObject({ allowed: false, reason: PULL_DENY_OUTBOUND });+ });++ it('denies a disabled peer as outbound-disallowed, not category-disabled', async () => {+ setPeers(universePeer({ enabled: false }));+ expect(await decidePeerPull({ callerId: PEER_A, syncCategory: 'universe' }))+ .toMatchObject({ allowed: false, reason: PULL_DENY_OUTBOUND });+ });++ it('lets the master switch beat a stale fullSync flag', async () => {+ // `{ fullSync: true, syncEnabled: false }` is reachable (set full mirror,+ // then flip the switch off) and contradictory — every push is already+ // refused for it, so a pull must not hand it the whole install.+ setPeers(universePeer({ fullSync: true, syncEnabled: false }));+ expect((await decidePeerPull({ callerId: PEER_A, syncCategory: 'digitalTwin' })).allowed).toBe(false);+ // The default-ON category still survives the switch, as everywhere else.+ expect((await decidePeerPull({ callerId: PEER_A, syncCategory: 'usage' })).allowed).toBe(true);});});diff --git a/server/services/sharing/peerSyncShared.js b/server/services/sharing/peerSyncShared.jsindex 91d6ba3dab..df698ece9d 100644--- a/server/services/sharing/peerSyncShared.js+++ b/server/services/sharing/peerSyncShared.js@@ -111,7 +111,7 @@ export const KIND_TO_CATEGORY = Object.freeze({// `directions` is a legacy record (permissive); `['inbound']` is a peer that// merely announced itself to us and was auto-created with no approval step, so// nothing may flow back out to it.-function peerOutboundEligible(peer) {+export function peerOutboundEligible(peer) {if (!peer || peer.enabled === false) return false;const directions = Array.isArray(peer.directions) ? peer.directions : [];return directions.length === 0 || directions.includes('outbound');@@ -143,7 +143,15 @@ export function peerAllowsOutbound(peer) {*/export function peerAllowsCategoryPull(peer, category) {if (!peerOutboundEligible(peer)) return false;- return resolveEffectiveCategories(peer)[category] === true;+ // `resolveEffectiveCategories` short-circuits `fullSync` BEFORE applying the+ // master-switch mask. That is the right reading for a loop deciding what to+ // ASK a peer for, and the wrong one for a gate deciding what to HAND OUT: the+ // reachable-but-contradictory `{ fullSync: true, syncEnabled: false }` state+ // (set full mirror, then flip the switch off) would otherwise be handed every+ // category here while `peerAllowsOutbound` refuses it every push. Resolve+ // with fullSync dropped so the mask wins and only default-ON survives.+ const masked = peer.syncEnabled === false ? { ...peer, fullSync: false } : peer;+ return resolveEffectiveCategories(masked)[category] === true;}export function peerHasCategory(peer, recordKind) {diff --git a/server/services/syncOrchestrator.js b/server/services/syncOrchestrator.jsindex 188a62db47..a4ee47c2e3 100644--- a/server/services/syncOrchestrator.js+++ b/server/services/syncOrchestrator.js@@ -83,10 +83,11 @@ async function withCursors(fn) {// it outright (#5663). `peerFetch` also carries the peer HTTPS agent, so a// self-signed tailnet peer no longer fails TLS validation on these pulls.//-// The timeout bounds the REQUEST only, not the body read — `withAbortTimeout`-// clears its timer as soon as the callback settles, which reproduces what-// `fetchWithTimeout` did here. Bounding the body too would abort a large-// snapshot mid-download on a slow link, every cycle, forever.+// The timeout bounds the peerFetch call and nothing after it, so the JSON+// decode of an already-received body can't be aborted — the same shape+// `fetchWithTimeout` had here. (Over HTTPS the budget does still cover the+// download itself, because the insecure-agent shim buffers the whole body+// before it resolves; that is a property of that transport, not of this call.)// Contract is unchanged: null on transport failure, non-2xx, or bad JSON.async function fetchPeer(peer, path) {const url = `${peerBaseUrl(peer)}${path}`;@@ -112,9 +113,8 @@ async function syncImageFromPeer(peer, avatarPath) {if (exists) return;const url = `${peerBaseUrl(peer)}${avatarPath}`;- // Same `peerFetch` hop as fetchPeer, and the same request-only timeout — an- // avatar download must not be aborted mid-body. Non-critical either way: a- // failure just retries next cycle.+ // Same `peerFetch` hop and the same timeout scope as fetchPeer. Non-critical+ // either way: a failure just retries next cycle.const res = await withAbortTimeout(FETCH_TIMEOUT_MS, (signal) => peerFetch(url, { signal }, peer)).catch(() => null);if (!res?.ok) return;