From 61b5aa4900b13451c05e43e70f513a9f3eaede10 Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Thu, 12 Nov 2026 15:44:14 -0700 Subject: [PATCH] fix(fs): treat path-less entries found by uid or id as missing (#4611) A uid or id lookup can return a legacy row with no path when its parent chain is broken. resolveNode now reports it as not found, so readdir, stat and the other uid-addressed routes answer 404 instead of crashing in the ACL check. --- .../controllers/fs/FSController.http.test.ts | 20 +++++++++++++++++++ src/backend/services/fs/resolveNode.test.ts | 15 ++++++++++++++ src/backend/services/fs/resolveNode.ts | 6 ++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/backend/controllers/fs/FSController.http.test.ts b/src/backend/controllers/fs/FSController.http.test.ts index 1b2255bd7..0aa41d2d4 100644 --- a/src/backend/controllers/fs/FSController.http.test.ts +++ b/src/backend/controllers/fs/FSController.http.test.ts @@ -154,6 +154,26 @@ describe('GET /fs/readdir over HTTP', () => { ).toEqual(['a', 'a/b']); }); + it('answers 404 for a directory whose parent chain is broken', async () => { + const { username, token } = env.users.user; + const path = `/${username}/Documents/http-orphan-${Date.now()}`; + const created = await fetch(new URL('/fs/mkdir', env.apiOrigin), { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ path, auth_token: token }), + }); + expect(created.status).toBe(200); + const { uid } = (await created.json()) as { uid: string }; + await env.server.clients.db.write( + 'UPDATE fsentries SET path = NULL, parent_uid = ? WHERE uuid = ?', + ['does-not-exist', uid], + ); + await env.server.stores.fsEntry.invalidateEntryCacheByUuid(uid); + + const response = await fetch(readdirUrl({ uid, auth_token: token })); + expect(response.status).toBe(404); + }); + it('mkdir does not return internal ids, storage columns or tokens', async () => { const { username, token } = env.users.user; const response = await fetch(new URL('/fs/mkdir', env.apiOrigin), { diff --git a/src/backend/services/fs/resolveNode.test.ts b/src/backend/services/fs/resolveNode.test.ts index 20f43e608..6d01f3fdf 100644 --- a/src/backend/services/fs/resolveNode.test.ts +++ b/src/backend/services/fs/resolveNode.test.ts @@ -124,6 +124,21 @@ describe('resolveNode', () => { expect(error.message).toContain('uuid=ghost'); }); + it('treats a path-less row found by uid or id as missing', async () => { + const orphan = makeEntry({ path: null as unknown as string }); + const store = makeStore({ + getEntryByUuid: vi.fn(async () => orphan), + getEntryById: vi.fn(async () => orphan), + }); + + await expect(resolveNode(store, { uid: 'uuid-7' })).resolves.toBeNull(); + await expectHttpError( + () => resolveNode(store, { id: 7 }, { required: true }), + 404, + 'subject_does_not_exist', + ); + }); + it('falls back to id lookup when uid is blank', async () => { const entry = makeEntry(); const store = makeStore({ getEntryById: vi.fn(async () => entry) }); diff --git a/src/backend/services/fs/resolveNode.ts b/src/backend/services/fs/resolveNode.ts index a36ec01de..7eec526c5 100644 --- a/src/backend/services/fs/resolveNode.ts +++ b/src/backend/services/fs/resolveNode.ts @@ -62,10 +62,12 @@ export async function resolveNode( ): Promise { if (ref.entry) return ref.entry; + // A uid/id lookup can return a legacy row with no path when its parent + // chain is broken. Nothing can authorize or address it, so it's a miss. const uuid = ref.uid ?? ref.uuid; if (isNonEmptyString(uuid)) { const entry = await fsEntryStore.getEntryByUuid(uuid); - if (entry) return entry; + if (entry?.path) return entry; return notFoundOrNull( options.required, `Entry not found: uuid=${uuid}`, @@ -80,7 +82,7 @@ export async function resolveNode( }); } const entry = await fsEntryStore.getEntryById(numericId); - if (entry) return entry; + if (entry?.path) return entry; return notFoundOrNull( options.required, `Entry not found: id=${numericId}`,