diff --git a/docs/bridge.md b/docs/bridge.md index df630be..46085e4 100644 --- a/docs/bridge.md +++ b/docs/bridge.md @@ -300,7 +300,7 @@ VoidBot still owns room reading, moderation judgment, archive/source retrieval, ### Fetch a named file (`chat_fetch_request.v1`, `chat_fetch_receipt.v1`) -A Persona's Mind shell holds no Matrix token, so the mouth keeps a file for her: `chat_fetch_request.v1` is `[request_id, persona, room, event_id]`, a room (alias or id) and an event id, nothing else. The mouth reads her live membership and the room's full state, reads the event itself and takes `content.url` from it, fetches it as her (5 MiB, magic bytes decide the type), compares the file event's `info.sha256` with the hash it computed, stores the bytes in her image store, and answers `chat_fetch_receipt.v1` `[request_id, outcome, room_id, image, receipt]` with one of 14 fixed outcome words. A file with no `info.sha256` is `StoredUnverified` and gets no receipt; a present hash that is not the computed one is `HashMismatch` and stores nothing. For a stored, hash-matching file the mouth posts the contract's `org.gamecult.file_received` event (`{event_id, sha256, size}`) as her own Matrix user, once per (file, Mind): a reply in the album thread, else a plain event with an `m.reference` to the file. The Matrix sender is the Mind; no content key names her. Idempotence is read from Matrix, not kept. Bridged and encrypted rooms and other people's rooms are refused. The kind is served only with `CHAT_FETCH=1` (exactly that value); off, it is answered as any unknown kind (`UnknownKind`). One 15 s deadline owns each fetch, and one Persona may ask 30 in an hour. +A Persona's Mind shell holds no Matrix token, so the mouth keeps a file for her: `chat_fetch_request.v1` is `[request_id, persona, room, event_id]`, a room (alias or id) and an event id, nothing else. The mouth reads her live membership and the room's full state, reads the event itself and takes `content.url` from it, fetches it as her (5 MiB, magic bytes decide the type), compares the file event's `info.sha256` with the hash it computed, stores the bytes in her image store, and answers `chat_fetch_receipt.v1` `[request_id, outcome, room_id, image, receipt]` with one of 14 fixed outcome words. A file with no `info.sha256` is `StoredUnverified` and gets no receipt; a present hash that is not the computed one is `HashMismatch` and stores nothing. For a stored, hash-matching file the mouth posts the contract's `org.gamecult.file_received` event (`{event_id, sha256, size}`) as her own Matrix user, once per (file, Mind): a reply in the album thread, else a plain event with an `m.reference` to the file. The Matrix sender is the Mind; no content key names her. Idempotence is read from Matrix, not kept. Bridged and encrypted rooms and other people's rooms are refused. The kind is served only with `CHAT_FETCH=1` (exactly that value); off, it is answered as any unknown kind (`UnknownKind`). One 15 s deadline owns each fetch, and one Persona may ask 30 in an hour. Each fetch writes one log line, `fetch persona=... outcome=... receipt=...`, and a failure adds a trailing ` cause=/`, on a thrown fetch failure, a store write that throws, or a receipt not proven: `Class` is the error's class name when it matches `[A-Za-z]{1,32}`, else `Error`; `status` is the HTTP status integer of a failed Matrix call, else `-`. No error text, code, path, URL or id reaches the log. `isFetchId` refuses a room-version-3 event id that contains `/` (standard base64), a fail-closed side: it is answered `NoAttachment`. ## Heimdall Integration Target diff --git a/tests/persona-matrix-mouth-fetch.test.mjs b/tests/persona-matrix-mouth-fetch.test.mjs index de29c90..2d2ed01 100644 --- a/tests/persona-matrix-mouth-fetch.test.mjs +++ b/tests/persona-matrix-mouth-fetch.test.mjs @@ -90,11 +90,11 @@ function synapse({ joined = [ROOM], state = [], events = {}, media = { abc: { by return s; } -function mouthOn(s, { brake = async () => true, deadline = 5000, store = true, clock, gate } = {}) { +function mouthOn(s, { brake = async () => true, deadline = 5000, store = true, clock, gate, put } = {}) { const root = personaStoreRoot(); const real = createImageStore({ root }); const puts = []; - const images = store ? { put: (...a) => { puts.push(a); return real.put(...a); } } : null; + const images = store ? { put: (...a) => { puts.push(a); return put ? put(...a) : real.put(...a); } } : null; const logs = []; const mouth = createMouth({ persona: "eureka", homeserver: "http://hs.test", serverName: HS, rooms: [ALIAS], token: TOKEN, fetch: s.fetch, state: { load: async () => null, save: async () => {} }, @@ -645,3 +645,32 @@ test("a deadline that passes while the brake is re-read before the receipt PUT s assert.equal(s.calls.filter(c => c.startsWith("PUT")).length, 0); assert.equal(m.puts.length, 1); }); + +test("a store that throws is one fetch line with a fixed cause: its code, message and name text reach no line", async () => { + const taint = `/srv/images/secret-path http://hs.test/x mxc://gamecult.org/abc ${TOKEN}`; + const s = synapse(); + const m = mouthOn(s, { put: () => { throw Object.assign(new Error(taint), { code: taint, name: "not a class name" }); } }); + assert.equal((await m.ask()).outcome, "StoreFailed"); + assert.equal(m.fetchLines().length, 1); + assert.match(m.fetchLines()[0], /outcome=StoreFailed .* cause=Error\/-$/); + assert.equal(m.logs.length, m.fetchLines().length, "no other line"); + for (const line of m.logs) for (const bit of ["secret-path", "http", "mxc", TOKEN, "EACCES"]) assert.ok(!line.includes(bit), `${bit} in: ${line}`); + assert.equal(s.calls.filter(c => c.startsWith("PUT")).length, 0, "no receipt for an unstored file"); +}); + +test("an event id with characters encodeURIComponent changes goes out as one encoded path segment in every request built from it", async () => { + const hostile = ["$a+b=c", "$a@b:c", "$a&b", "$a b".replace(" ", "!"), "$a'(b)*", "$a;b,c", "$a$b"]; + for (const [id, threaded] of hostile.flatMap(h => [[h, true], [h, false]])) { + assert.ok(docs.isFetchId("$", id), id); + assert.notEqual(encodeURIComponent(id), id, `${id} is changed by encoding`); + const s = synapse({ events: { [id]: { type: "m.room.message", event_id: id, room_id: ROOM, sender: OTHER, content: fileContent({ "m.relates_to": threaded ? { rel_type: "m.thread", event_id: id } : undefined }) } } }); + const m = mouthOn(s); + assert.equal((await m.ask({ eventId: id, room: ROOM })).outcome, "Stored", id); + const enc = encodeURIComponent(id); + const eventGet = s.raw.filter(p => p.includes("/event/")); + const relGet = s.raw.filter(p => p.includes("/relations/")); + assert.ok(eventGet.length === 1 && eventGet[0] === `/_matrix/client/v3/rooms/${encodeURIComponent(ROOM)}/event/${enc}`, `event GET: ${eventGet}`); + assert.ok(relGet.length >= 1 && relGet.every(p => p.startsWith(`/_matrix/client/v1/rooms/${encodeURIComponent(ROOM)}/relations/${enc}/${threaded ? "m.thread" : "m.reference"}/`)), `relations GET: ${relGet}`); + assert.ok(s.raw.every(p => !p.split("?")[0].includes(id) || id === enc), "no raw id in a request path"); + } +}); diff --git a/tools/persona-matrix-mouth.mjs b/tools/persona-matrix-mouth.mjs index acd7b77..10af28a 100644 --- a/tools/persona-matrix-mouth.mjs +++ b/tools/persona-matrix-mouth.mjs @@ -834,10 +834,7 @@ export function createMouth({ if (claimed && info.sha256 !== computed) return done("HashMismatch"); deadline.throwIfAborted(); let image; - try { image = images.put(roomId, got.bytes, got.mime); } catch (error) { - say(`image not stored: ${error?.code ?? "write failed"}`); - return done("StoreFailed"); - } + try { image = images.put(roomId, got.bytes, got.mime); } catch (error) { return done("StoreFailed", { cause: causeOf(error) }); } stored = true; if (!claimed) return done("StoredUnverified", { image, roomId }); const { receipt, word, cause } = await postFileReceipt(roomId, albumRoot, event.event_id, computed, got.bytes.length, deadline);