From fd0a43e0e47da4199f51d8ff5ca4ac44c3eb9c3f Mon Sep 17 00:00:00 2001 From: Heimdall Date: Sun, 8 Nov 2026 09:29:59 +0000 Subject: [PATCH] Image store: room directory must share the Persona directory's group; never create the Persona directory The room directory's gid must equal the Persona directory's, else store-failed (a setgid directory in the wrong group would still give files the wrong group). A missing Persona directory is refused rather than created; only the room directory is made. The stat is injectable so the group case is testable where the sandbox cannot chown. Co-Authored-By: Claude Sonnet 5.5 --- tests/persona-chat-images.test.mjs | 21 +++++++++++++++++++++ tools/persona-chat-images.mjs | 12 +++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/persona-chat-images.test.mjs b/tests/persona-chat-images.test.mjs index 691f322..05aa980 100644 --- a/tests/persona-chat-images.test.mjs +++ b/tests/persona-chat-images.test.mjs @@ -188,6 +188,27 @@ test("a Persona directory without setgid refuses the image: nothing is written, assert.equal(statSync(join(root, sha("!r:x"))).mode & 0o7777, 0o750, "the room directory it made is left as mkdir made it"); }); + +test("a room directory whose group is not the Persona directory's is refused: nothing is written", () => { + const root = personaStoreRoot(); + const wrong = createImageStore({ root, stat: path => { const s = statSync(path); return path === root ? s : Object.assign(Object.create(Object.getPrototypeOf(s)), s, { gid: s.gid + 1 }); } }); + assert.throws(() => wrong.put("!r:x", png(), "image/png"), { code: "ENOGROUP" }); + assert.deepEqual(files(root), []); + const right = createImageStore({ root }); + assert.ok(right.put("!r:x", png(), "image/png").path, "the same directory is accepted when the group matches"); +}); + +test("a missing Persona directory is refused, not created, and nothing above or below it appears", () => { + const parent = join(personaStoreRoot(), ".."); + const root = join(parent, "absent", "eureka"); + const store = createImageStore({ root }); + assert.throws(() => store.put("!r:x", png(), "image/png"), { code: "ENOPERSONADIR" }); + assert.equal(existsSync(join(parent, "absent")), false); + const file = join(parent, "not-a-dir"); + writeFileSync(file, "x"); + assert.throws(() => createImageStore({ root: file }).put("!r:x", png(), "image/png"), { code: "ENOPERSONADIR" }); +}); + test("a write that cannot finish leaves no temp file and no partial image under the final name", () => { const { root, store } = storeIn(); const bytes = png(6, 6); diff --git a/tools/persona-chat-images.mjs b/tools/persona-chat-images.mjs index 19f19db..9e140e9 100644 --- a/tools/persona-chat-images.mjs +++ b/tools/persona-chat-images.mjs @@ -128,7 +128,7 @@ const sha256 = data => createHash("sha256").update(data).digest("hex"); // A stored image leaves when it is at least RETAIN_MS old and the delivery that carried it is acked, whichever is // later; a room holds at most ROOM_CAP_BYTES, oldest evicted first. What is on disk at start is old and acked: a // delivery still pending after a restart is made again by the next sync and stores its image again. -export function createImageStore({ root, now = Date.now, log = () => {} }) { +export function createImageStore({ root, now = Date.now, log = () => {}, stat = statSync }) { const entries = new Map(); // relative path -> { room, size, storedAt, seq } (seq: the newest delivery carrying it, null once acked) let loaded = false; @@ -174,8 +174,14 @@ export function createImageStore({ root, now = Date.now, log = () => {} }) { const umask = process.umask(0o027); let temp, fd; try { - mkdirSync(dir, { recursive: true, mode: DIR_MODE }); - if ((statSync(dir).mode & 0o2000) === 0) throw Object.assign(new Error("image directory lacks the setgid bit"), { code: "ENOSETGID" }); + // Only the room directory is created: a missing Persona directory is the installer's to make, and is not made up here. + let persona; + try { persona = stat(root); } catch { persona = null; } + if (!persona?.isDirectory()) throw Object.assign(new Error("the Persona's image directory does not exist"), { code: "ENOPERSONADIR" }); + try { mkdirSync(dir, { mode: DIR_MODE }); } catch (error) { if (error.code !== "EEXIST") throw error; } + const made = stat(dir); + if ((made.mode & 0o2000) === 0) throw Object.assign(new Error("image directory lacks the setgid bit"), { code: "ENOSETGID" }); + if (made.gid !== persona.gid) throw Object.assign(new Error("image directory is not in the Persona directory's group"), { code: "ENOGROUP" }); temp = join(dir, `.tmp-${randomBytes(8).toString("hex")}`); fd = openSync(temp, "wx", FILE_MODE); } finally { process.umask(umask); }