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

address review (codex): scope draft-body pulls to the pushed work + reject unstorable work ids (#1565)

[._.]/ Adam Eivy733811edddb4
diff --git a/server/services/sharing/peerSync.js b/server/services/sharing/peerSync.js
index b897b7c355..fca3fa8282 100644
--- a/server/services/sharing/peerSync.js
+++ b/server/services/sharing/peerSync.js
@@ -2205,7 +2205,15 @@ export async function applyIncomingPush(payload) {
// Same guards as the asset path: skip for local-ephemeral and tombstone pushes.
let missingDraftBodies = [];
if (kind === 'writersRoomWork' && !localEphemeral && record.deleted !== true) {
- missingDraftBodies = await diffWorkBodyManifest(draftBodyManifest, { includeMismatched: workMergeApplied });
+ // Scope the manifest to THIS work: a body entry's path is works/<workId>/...,
+ // so an entry whose workId != the pushed record's id would write bytes into a
+ // DIFFERENT local work's draft (clobbering unrelated prose when the merge
+ // accepted the remote). A peer may only replicate the bodies of the work it
+ // actually pushed.
+ const ownBodies = Array.isArray(draftBodyManifest)
+ ? draftBodyManifest.filter((e) => e && e.workId === record.id)
+ : [];
+ missingDraftBodies = await diffWorkBodyManifest(ownBodies, { includeMismatched: workMergeApplied });
if (missingDraftBodies.length > 0) {
pullMissingWorkBodies(sourceInstanceId, missingDraftBodies).catch((err) => {
console.log(`⚠️ peerSync: draft-body pull from ${sourceInstanceId} failed: ${err.message}`);
diff --git a/server/services/writersRoom/syncLogic.js b/server/services/writersRoom/syncLogic.js
index 55aabbe40f..39e2b67d36 100644
--- a/server/services/writersRoom/syncLogic.js
+++ b/server/services/writersRoom/syncLogic.js
@@ -37,7 +37,11 @@ export const WRITERS_ROOM_DRAFT_ASSET_KIND = 'writers-room-draft';
*/
export function sanitizeWorkForSync(raw) {
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
- if (!isStr(raw.id) || !raw.id) return null;
+ // The id is used as a filesystem path segment (works/<id>/...) and the path
+ // helpers enforce WORK_ID_RE — so drop a peer-supplied id that isn't a valid
+ // work id BEFORE merge/persist, rather than letting it throw in the file
+ // backend's saveManifest or plant an unaddressable PG row.
+ if (!isStr(raw.id) || !WORK_ID_RE.test(raw.id)) return null;
const createdAt = isStr(raw.createdAt) ? raw.createdAt : new Date().toISOString();
const updatedAt = isStr(raw.updatedAt) ? raw.updatedAt : createdAt;
const { deleted, deletedAt } = sanitizeSoftDeleteFields(raw);
diff --git a/server/services/writersRoom/syncLogic.test.js b/server/services/writersRoom/syncLogic.test.js
index 00f918cd07..1d5336341b 100644
--- a/server/services/writersRoom/syncLogic.test.js
+++ b/server/services/writersRoom/syncLogic.test.js
@@ -29,11 +29,17 @@ const work = (extra = {}) => ({
});
describe('sanitizeWorkForSync', () => {
- it('drops non-objects and records without a usable id', () => {
+ it('drops non-objects, missing ids, and ids that are not valid work ids', () => {
expect(sanitizeWorkForSync(null)).toBeNull();
expect(sanitizeWorkForSync([])).toBeNull();
expect(sanitizeWorkForSync({ title: 'no id' })).toBeNull();
expect(sanitizeWorkForSync({ id: '' })).toBeNull();
+ // Unstorable id (not WORK_ID_RE) — would throw in the path layer / plant an
+ // unaddressable row, so it's dropped before merge.
+ expect(sanitizeWorkForSync({ id: 'not-a-work' })).toBeNull();
+ expect(sanitizeWorkForSync({ id: '../etc/passwd' })).toBeNull();
+ // A real work id passes.
+ expect(sanitizeWorkForSync(work())?.id).toBe(WORK);
});
it('normalizes the soft-delete trio and preserves the body verbatim', () => {