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: stop mis-acking peer tombstones + harden asset filenames

Adam Eivy5ebc3f42e376
diff --git a/server/services/mediaCollections.js b/server/services/mediaCollections.js
index a8c8fbce52..832f56b9ff 100644
--- a/server/services/mediaCollections.js
+++ b/server/services/mediaCollections.js
@@ -61,9 +61,15 @@ const sanitizeItem = (raw) => {
// carries the whole file), and downstream code joins the ref onto a
// PATHS dir to hash / pull the asset. A peer-supplied ref like
// `../etc/passwd` would otherwise let one peer make another peer hash
- // (and leak the hash of) arbitrary local files. Same posture as
- // `sanitizeAssetFilename` in `server/services/sharing/peerSync.js`.
- if (ref.includes('/') || ref.includes('\\') || ref.includes('..')) return null;
+ // (and leak the hash of) arbitrary local files.
+ //
+ // Reject separators and parent-directory PATH SEGMENTS (exact `.` or
+ // `..` between slashes, or as the whole basename). Do NOT reject the
+ // substring `..` in general — gallery filenames like `my..render.png`
+ // are valid basenames that the rest of the system stores and serves,
+ // and dropping them would silently disappear existing collection items.
+ if (ref.includes('/') || ref.includes('\\')) return null;
+ if (ref === '.' || ref === '..') return null;
// A hand-edited or corrupted `addedAt` would feed NaN into the cover
// resolver's Date sort — replace anything unparseable with now().
const parsed = typeof raw.addedAt === 'string' ? Date.parse(raw.addedAt) : NaN;
diff --git a/server/services/sharing/peerSync.js b/server/services/sharing/peerSync.js
index 9232b367f3..f630b26385 100644
--- a/server/services/sharing/peerSync.js
+++ b/server/services/sharing/peerSync.js
@@ -461,20 +461,28 @@ export async function buildAssetManifest(record) {
}
async function hashImageForManifest(filename) {
- if (!isStr(filename)) return null;
- const fullPath = join(PATHS.images, filename);
+ // Sanitize before join — a record with `imageRefs` containing a path-
+ // traversal filename (peer-pushed via linkedCollection, hand-edited,
+ // or import bug) would otherwise let us stat/hash files outside
+ // PATHS.images. The share exporter has the same `base !== filename`
+ // posture (services/sharing/exporter.js); peer-sync needs to match.
+ const safeName = sanitizeAssetFilename(filename);
+ if (!safeName) return null;
+ const fullPath = join(PATHS.images, safeName);
const result = await getOrComputeImageSha256(fullPath);
if (!result) return null;
- return { filename, kind: 'image', sha256: result.hash };
+ return { filename: safeName, kind: 'image', sha256: result.hash };
}
async function hashSimpleAsset(filename, kind, sourceDir) {
- if (!isStr(filename) || !isStr(sourceDir)) return null;
- const fullPath = join(sourceDir, filename);
+ if (!isStr(sourceDir)) return null;
+ const safeName = sanitizeAssetFilename(filename);
+ if (!safeName) return null;
+ const fullPath = join(sourceDir, safeName);
if (!existsSync(fullPath)) return null;
const hash = await sha256File(fullPath).catch(() => null);
if (!hash) return null;
- return { filename, kind, sha256: hash };
+ return { filename: safeName, kind, sha256: hash };
}
// --- Receiver-side asset diff -------------------------------------------
@@ -547,7 +555,12 @@ function directoryForAssetKind(kind) {
*/
function sanitizeAssetFilename(name) {
if (typeof name !== 'string' || !name) return null;
- if (name.includes('/') || name.includes('\\') || name.includes('..')) return null;
+ // Reject separators and exact parent-directory segments (`.` / `..`
+ // as the whole basename). A basename like `my..render.png` is
+ // legitimate (the gallery filename validator permits `..` inside a
+ // basename) — only the path-segment forms are traversal.
+ if (name.includes('/') || name.includes('\\')) return null;
+ if (name === '.' || name === '..') return null;
if (basename(name) !== name) return null;
return name;
}
@@ -967,10 +980,18 @@ export async function applyIncomingPush(payload) {
// Compute the deletedAt water-mark we can ack. Use the maximum across the
// record + its issues (a single push can carry multiple tombstones).
+ // We return this to the sender so THEY can advance THEIR cursor (which
+ // tracks what we — the receiver — have acked of THEIR pushes). We do
+ // NOT call ackDeletesUpTo here: that would write
+ // `localCursors[sourceInstanceId] = ackedDeletesUpTo`, which is
+ // mis-directional. Our cursors track "what peer X has acked of OUR
+ // local deletions" so tombstoneGc can prune our local tombstones once
+ // every subscribed peer has confirmed receipt. The receive-side ack
+ // here would let GC prune OUR older local tombstones as if peer-A had
+ // seen them — even though peer-A is just telling us about ITS own
+ // tombstones. In bidirectional sync, that lets peer-A's stale live
+ // records resurrect after GC drops our tombstones.
const ackedDeletesUpTo = computeAckedDeletesFromPayload(record, issues);
- if (ackedDeletesUpTo > 0) {
- await ackDeletesUpTo(sourceInstanceId, ackedDeletesUpTo).catch(() => {});
- }
// Best-effort reverse subscription. Failures don't fail the push — the
// record is already merged and the response will tell the sender what
diff --git a/server/services/sharing/peerSync.test.js b/server/services/sharing/peerSync.test.js
index cc8dc33bbc..6e50efb539 100644
--- a/server/services/sharing/peerSync.test.js
+++ b/server/services/sharing/peerSync.test.js
@@ -1302,23 +1302,30 @@ describe('peerSync', () => {
expect(result.missingAssets).toHaveLength(1);
});
- it('advances the sender cursor when the payload carries a tombstone', async () => {
- await applyIncomingPush({
+ it('returns ackedDeletesUpTo for the sender (does NOT advance the local cursor on receive)', async () => {
+ // Cursors track "what peer X has acked of OUR local deletions" so
+ // tombstoneGc can prune our local tombstones once every subscribed
+ // peer has confirmed receipt. Advancing the cursor for sourceInstanceId
+ // on receive would mis-credit the sender's tombstones as our own pushed-
+ // and-acked ones, letting GC prune local tombstones the sender never
+ // saw — and resurrecting them on the sender's next push.
+ const result = await applyIncomingPush({
kind: 'universe',
record: { id: 'u1', deleted: true, deletedAt: '2026-01-01T00:00:00Z' },
assetManifest: [],
sourceInstanceId: 'peer-a',
});
+ expect(result.ackedDeletesUpTo).toBe(Date.parse('2026-01-01T00:00:00Z'));
const cursors = await listCursors();
- expect(cursors['peer-a'].lastAckedDeleteAt).toBe(Date.parse('2026-01-01T00:00:00Z'));
+ expect(cursors['peer-a']).toBeUndefined();
});
- it('picks the MAX deletedAt across record + bundled issues for the cursor advance', async () => {
- // Regression: if only `record.deletedAt` is considered, a series push
- // bundling multiple tombstoned issues would advance the cursor only
- // by the series' own deletion time — newer issue tombstones in the
- // same push would never be acknowledged until a separate push lands.
- await applyIncomingPush({
+ it('returns the MAX deletedAt across record + bundled issues so the sender can ack all in one round-trip', async () => {
+ // Regression: if only `record.deletedAt` is returned, a series push
+ // bundling multiple tombstoned issues would only ack the series'
+ // own deletion time — newer issue tombstones in the same push would
+ // never be acknowledged until a separate push lands.
+ const result = await applyIncomingPush({
kind: 'series',
record: { id: 's1', deleted: true, deletedAt: '2026-01-01T00:00:00Z' },
issues: [
@@ -1328,8 +1335,7 @@ describe('peerSync', () => {
assetManifest: [],
sourceInstanceId: 'peer-a',
});
- const cursors = await listCursors();
- expect(cursors['peer-a'].lastAckedDeleteAt).toBe(Date.parse('2026-03-01T00:00:00Z'));
+ expect(result.ackedDeletesUpTo).toBe(Date.parse('2026-03-01T00:00:00Z'));
});
it('auto-creates a reverse subscription back to the sender', async () => {