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): map malformed collection ids to 404, not 500

[._.]/ Adam Eivy3f74a82e7aae
diff --git a/server/services/mediaCollections.js b/server/services/mediaCollections.js
index 0ac5bf6bea..e79f98fba1 100644
--- a/server/services/mediaCollections.js
+++ b/server/services/mediaCollections.js
@@ -47,6 +47,19 @@ export const ITEMS_MAX = 5000;
// sync batch). Single source of truth: also passed to `createCollectionStore`.
const COLLECTION_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
+// A malformed/overlong id can't name any stored collection (ids are the fixed
+// path-segment allowlist above), so map it to NOT_FOUND — the same result the
+// pre-split `listCollections` scan gave for an unknown id. Without this, the
+// id-accepting write mutators would hand a bad id to `queueRecordWrite`, which
+// throws a raw (uncoded) store error that the route mapper surfaces as a 500
+// instead of a clean 404. Read paths (`loadOne`) already return null for a bad
+// id, so they don't need this; the write paths throw before `loadOne` runs.
+const assertCollectionId = (id) => {
+ if (typeof id !== 'string' || !COLLECTION_ID_PATTERN.test(id)) {
+ throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
+ }
+};
+
export { REF_MAX_LENGTH, itemKey };
const sanitizeItem = (raw) => {
@@ -596,6 +609,7 @@ export const renameCollectionForUniverse = (universeId, newUniverseName) =>
renameOwnerLinked('universeId', universeId, UNIVERSE_ID_MAX, universeCollectionNameFor, newUniverseName);
export async function updateCollection(id, patch) {
+ assertCollectionId(id);
const merged = await store().queueRecordWrite(id, async () => {
const cur = await store().loadOne(id);
if (!cur) throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
@@ -647,6 +661,7 @@ export async function updateCollection(id, patch) {
}
export async function deleteCollection(id) {
+ assertCollectionId(id);
const { universeId: deletedUniverseId, seriesId: deletedSeriesId, alreadyDeleted } = await store().queueRecordWrite(id, async () => {
const target = await store().loadOne(id);
if (!target) throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
@@ -704,6 +719,7 @@ const validateItemInput = (item) => {
export async function addItem(id, item) {
const { kind, ref } = validateItemInput(item);
+ assertCollectionId(id);
const merged = await store().queueRecordWrite(id, async () => {
const cur = await store().loadOne(id);
if (!cur) throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
@@ -766,6 +782,7 @@ export async function bulkUpdateCollectionItems(id, { add = [], remove = [] } =
}
}
+ assertCollectionId(id);
const result = await store().queueRecordWrite(id, async () => {
const cur = await store().loadOne(id);
if (!cur) throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
@@ -818,6 +835,7 @@ export async function bulkUpdateCollectionItems(id, { add = [], remove = [] } =
}
export async function removeItem(id, key) {
+ assertCollectionId(id);
const merged = await store().queueRecordWrite(id, async () => {
const cur = await store().loadOne(id);
if (!cur) throw makeErr(`Collection not found: ${id}`, ERR_NOT_FOUND);
diff --git a/server/services/mediaCollections.test.js b/server/services/mediaCollections.test.js
index dceb7c3c7e..9744a9518d 100644
--- a/server/services/mediaCollections.test.js
+++ b/server/services/mediaCollections.test.js
@@ -147,6 +147,25 @@ describe('mediaCollections service', () => {
expect(all[0].id).toBe(c.id);
});
+ it('write mutators map a malformed id to ERR_NOT_FOUND (not a raw store error → 500)', async () => {
+ // A path-param id that fails the store allowlist (e.g. "has space",
+ // "../escape") must surface as a clean NOT_FOUND — the same result the
+ // pre-split scan gave — instead of throwing an uncoded error from
+ // queueRecordWrite that the route mapper would turn into a 500.
+ for (const bad of ['has space', '../escape', 'a/b']) {
+ await expect(svc.addItem(bad, { kind: 'image', ref: 'x.png' }))
+ .rejects.toMatchObject({ code: svc.ERR_NOT_FOUND });
+ await expect(svc.updateCollection(bad, { description: 'x' }))
+ .rejects.toMatchObject({ code: svc.ERR_NOT_FOUND });
+ await expect(svc.deleteCollection(bad))
+ .rejects.toMatchObject({ code: svc.ERR_NOT_FOUND });
+ await expect(svc.removeItem(bad, 'image:x.png'))
+ .rejects.toMatchObject({ code: svc.ERR_NOT_FOUND });
+ await expect(svc.bulkUpdateCollectionItems(bad, { add: [{ kind: 'image', ref: 'x.png' }] }))
+ .rejects.toMatchObject({ code: svc.ERR_NOT_FOUND });
+ }
+ });
+
it('deleteCollection clears coverKey on the persisted tombstone (no dangling cover in the wire record)', async () => {
const c = await svc.createCollection({ name: 'WithCover' });
await svc.addItem(c.id, { kind: 'image', ref: 'cover.png' });