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: fail closed on unverifiable local owners when binding peer push bundles (#12427)
[._.]/ Adam Eivyf5760943ebf5
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.jsindex 718d3d17ee..ec1a2ac1a1 100644--- a/server/services/sharing/peerSync.test.js+++ b/server/services/sharing/peerSync.test.js@@ -3557,6 +3557,20 @@ describe('peerSync', () => {expect(mergeMediaCollectionsFromSync).toHaveBeenCalledTimes(1);});+ it('refuses a linkedCollection whose local owner is another category or cannot be verified', async () => {+ const incoming = { id: 'col-x', universeId: 'shared-id', items: [] };+ const push = () => applyIncomingPush({+ kind: 'universe', record: { id: 'shared-id', ...live }, linkedCollection: incoming, assetManifest: [], sourceInstanceId: 'peer-a',+ });+ // Same id, different parent kind (a series parent named like this universe).+ vi.mocked(getCollection).mockResolvedValue({ id: 'col-x', seriesId: 'shared-id', items: [] });+ await push();+ // A storage failure is not "no local collection".+ vi.mocked(getCollection).mockRejectedValue(new Error('db unavailable'));+ await push();+ expect(mergeMediaCollectionsFromSync).not.toHaveBeenCalled();+ });+it('refuses a linkedCollection on a kind that has no collection parent', async () => {await applyIncomingPush({kind: 'musicVideoProject', record: { id: 'mv-1', ...live },diff --git a/server/services/sharing/peerSyncPush.js b/server/services/sharing/peerSyncPush.jsindex a9dcc6a9e6..f9b90d0619 100644--- a/server/services/sharing/peerSyncPush.js+++ b/server/services/sharing/peerSyncPush.js@@ -37,6 +37,7 @@ import {peerAllowsOutbound,peerHasCategory,findPeerById,+ loadOrNotFound,readState,writeState,withStateLock,@@ -106,25 +107,6 @@ export function __resetPushFailureLogForTests() {loggedPushFailures.clear();}-// A store read that fails is NOT "record does not exist": only a not-found-// error (ServerError 404 / a `*NOT_FOUND` code, as the record loaders throw)-// maps to null. Anything else (DB down, a sanitizer throwing) rethrows so the-// push rejects before any network call or lastPushedHash write, and the caller-// logs it (#11455).-function isNotFoundError(err) {- const code = err?.code;- return err?.status === 404 || (typeof code === 'string' && (code === 'NOT_FOUND' || code.endsWith('_NOT_FOUND')));-}--async function loadOrNotFound(fn) {- try {- return await fn();- } catch (err) {- if (isNotFoundError(err)) return null;- throw err;- }-}-async function isSubscriptionRecordTombstone(sub) {const desc = RECORD_KINDS[sub.recordKind];if (!desc) return false;diff --git a/server/services/sharing/peerSyncReceive.js b/server/services/sharing/peerSyncReceive.jsindex 28f052b8a2..38b9923e58 100644--- a/server/services/sharing/peerSyncReceive.js+++ b/server/services/sharing/peerSyncReceive.js@@ -40,6 +40,7 @@ import { findPeerSubscription, subscribePeer } from './peerSubscriptions.js';import {makeErr,findPeerById,+ loadOrNotFound,peerSyncEvents,ERR_VALIDATION,ERR_SCHEMA_VERSION_AHEAD,@@ -91,11 +92,19 @@ async function bindLinkedCollection({ kind, record, linkedCollection, sourceInst// The merge overwrites the collection's own parent link by id, so a peer that// names another collection's id while claiming this record as parent would// re-parent it. Refuse when the local collection is linked elsewhere.- const local = isNonBlankStr(linkedCollection.id)- ? await RECORD_KINDS.mediaCollection.load(linkedCollection.id).catch(() => null)- : null;- const localParentId = local?.universeId || local?.seriesId || null;- if (localParentId && localParentId !== record.id) {+ // A failed lookup is not "no local collection": skip the bundle (the snapshot+ // sync reconciles collections) rather than merge past an unverified owner.+ let local;+ try {+ local = await loadOrNotFound(() => RECORD_KINDS.mediaCollection.load(linkedCollection.id));+ } catch (err) {+ console.warn(`⚠️ peerSync: skipped linkedCollection from ${sourceInstanceId} — local collection lookup failed: ${err.message}`);+ return null;+ }+ // Parent field and id must both match: a local collection parented by a+ // series that shares this universe's id is still another category's record.+ const linkedElsewhere = local && (local.universeId || local.seriesId) && local[parentField] !== record.id;+ if (linkedElsewhere) {console.warn(`⚠️ peerSync: skipped linkedCollection from ${sourceInstanceId} — local collection belongs to another parent`);return null;}@@ -124,8 +133,8 @@ async function bindLinkedTrack({ record, linkedTrack, sourceInstanceId, onLookuplet localProject;try {[localTrack, localProject] = await Promise.all([- RECORD_KINDS.track.load(linkedTrack.id),- RECORD_KINDS.musicVideoProject.load(record.id).catch(() => null),+ loadOrNotFound(() => RECORD_KINDS.track.load(linkedTrack.id)),+ loadOrNotFound(() => RECORD_KINDS.musicVideoProject.load(record.id)),]);} catch (err) {onLookupError(err);diff --git a/server/services/sharing/peerSyncShared.js b/server/services/sharing/peerSyncShared.jsindex 9603936d36..846e01d652 100644--- a/server/services/sharing/peerSyncShared.js+++ b/server/services/sharing/peerSyncShared.js@@ -231,6 +231,25 @@ export async function findPeerById(peerId) {const peers = await getPeers().catch(() => []);return peers.find((p) => p.instanceId === peerId) || null;}+// A store read that fails is NOT "record does not exist": only a not-found+// error (ServerError 404 / a `*NOT_FOUND` code, as the record loaders throw)+// maps to null. Anything else (DB down, a sanitizer throwing) rethrows so the+// push rejects before any network call or lastPushedHash write, and the caller+// logs it (#11455).+export function isNotFoundError(err) {+ const code = err?.code;+ return err?.status === 404 || (typeof code === 'string' && (code === 'NOT_FOUND' || code.endsWith('_NOT_FOUND')));+}++export async function loadOrNotFound(fn) {+ try {+ return await fn();+ } catch (err) {+ if (isNotFoundError(err)) return null;+ throw err;+ }+}+export const FORCE_REVALIDATE_EVERY = 10; // ~10 min at the 60s sweep cadence/**