File zone: Mind contract; plaintext sha256 in info on send; 'read by <Mind>' from org.gamecult.file_received
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Void 2026-11-11 21:26:36 +00:00
commit af328bdb90
7 changed files with 268 additions and 7 deletions

View file

@ -0,0 +1,80 @@
# File zone: what a Mind can read from a file event
Ruling `ratatoskr:ruling:persona-file-sharing-zone`. The Mind-side fetch is the mouth's
(`eureka-body:cut_spec:mouth-fetch-attachment.r1`); this page is the client side of the same contract.
A Mind cannot fetch an `mxc://` itself (the media endpoint answers 401), so it never holds one.
## What the Mind passes
Room id and event id of the file event. Never a bare `mxc://`, URL or hash. The mouth fetches the event,
reads the media reference from its content, and enforces the Mind's own membership in that room at fetch
time. A room the Mind is not in is refused by the mouth, whatever the client wrote.
## What the client writes (this repo, `RoomInput.tsx` send path)
A file event is an `m.room.message` whose `msgtype` is `m.audio`, `m.image`, `m.video` or `m.file`:
| field | where | note |
|---|---|---|
| `msgtype` | content | selects the kind from the file's mimetype prefix |
| `body`, `filename` | content | both the file's name (display only; never a path) |
| `url` | content | the `mxc://` of an unencrypted file; the mouth reads it |
| `file` | content | present instead of `url` in an encrypted room: `{url, key, iv, hashes.sha256 (ciphertext), v}`. A Mind has no key; the mouth refuses it |
| `info.mimetype` | content | the browser-reported type, normalised by `safeFile` (e.g. `application/ogg` becomes `audio/ogg`). A claim, not proof: the mouth sniffs magic bytes |
| `info.size` | content | bytes of the file as uploaded |
| `info["org.gamecult.sha256"]` | content | lower-case hex sha256 of the plaintext bytes, computed by the client before send; written in unencrypted rooms only (a plaintext hash in an encrypted room would leak the file's identity) |
The hash is computed over the same bytes that are uploaded. A file sent by something other than this
client may lack `info["org.gamecult.sha256"]`; the mouth then logs the hash it computed and the receipt
below is displayed as unchecked.
Image and video events also carry `info.w`, `info.h`, `info.xyz.amorgan.blurhash` and a thumbnail
reference; audio carries no duration. A Mind should not rely on any of those.
## Limits and refusals
- Size: the mouth streams at most 5 MiB and refuses a larger file. The client does not enforce this cap;
its own limit is the homeserver's `m.upload.size`. A file over 5 MiB can be shared with people and
not read by a Mind.
- Refusals are the mouth's and are typed: not a member of the room, bridged or Discord room, encrypted
file, over the cap, content type not matching the magic bytes. The client does not re-check them and
nothing in the client depends on the mouth's log.
- The Mind cites files by room id and event id only; a file in a room the Mind is not in does not exist
for it.
## Receipt: `org.gamecult.file_received`
After a successful fetch the Mind posts one event of type `org.gamecult.file_received` to the room of the
file, as a reply in the album's thread, so it arrives with the album's own listing (no extra request):
```json
{
"m.relates_to": { "rel_type": "m.thread", "event_id": "<album root event id>" },
"org.gamecult.file_received": {
"event_id": "<file event id>",
"sha256": "<hex sha256 of the bytes the Mind fetched>",
"size": 123456
}
}
```
No paths, no URLs, no filenames. The Mind is the event's `sender`. The receipt carries no `msgtype`, so
older clients and the album's file listing ignore it.
The client draws it on the file row as `read by <Mind>`:
- one receipt per Mind per file, the latest wins, a second Mind adds a name;
- `sha256` equal to `info["org.gamecult.sha256"]`: plain `read by <Mind>`;
- both present and different: `read by <Mind> (hash differs)`;
- either missing: plain `read by <Mind>`, unverified;
- a receipt whose `event_id` is not a file in that album, a malformed one, or a redacted one is ignored.
A receipt is a claim by its sender and is shown to anyone in the room; any member could post one.
It proves nothing about who read the file.
## Not built yet
- Files sent from the composer carry no thread relation, so they reach an album only if written into a
thread by another tool; the album upload path (docs/file-zone.md, "Upload") is unwritten. Receipts are
thread replies, so they show only where the file is.
- Encrypted zones are not listed.

View file

@ -15,6 +15,7 @@ import { getBlobSafeMimeType } from '../../utils/mimeTypes';
import { timeDayMonYear } from '../../utils/time';
import { getMemberDisplayName } from '../../utils/room';
import { Album, AlbumFile } from './buildAlbums';
import { readLine } from './fileReceipt';
import { useAlbums } from './useAlbums';
import * as css from './AlbumView.css';
@ -47,6 +48,11 @@ function FileRow({ room, file, author }: FileRowProps) {
<Text size="T300" truncate>
{file.name}
</Text>
{file.readBy.length > 0 && (
<Text size="T200" className={css.Dim}>
{readLine(file.readBy, (id) => getMemberDisplayName(room, id) ?? id)}
</Text>
)}
{audio ? (
<AudioContent {...audio} renderMediaControl={(p) => <MediaControl {...p} />} />
) : (

View file

@ -214,3 +214,62 @@ describe('buildAlbums', () => {
expect(one(root('$q', 1), [threaded]).count).toBe(1);
});
});
describe('Mind receipts', () => {
const HASH = 'a'.repeat(64);
const file = (id: string, sha?: string) =>
reply(id, 10, 'm.audio', id, { info: sha ? { 'org.gamecult.sha256': sha } : {} });
const receipt = (id: string, ts: number, sender: string, fileId: unknown, sha?: unknown) =>
({
event_id: id,
type: 'org.gamecult.file_received',
sender,
origin_server_ts: ts,
content: { 'org.gamecult.file_received': { event_id: fileId, sha256: sha } },
} as ZoneEvent);
it('shows who read a file, with the hash agreeing', () => {
const a = one(root('$r', 1), [file('$f', HASH), receipt('$x', 20, '@void:x', '$f', HASH)]);
expect(a.files[0].readBy).toEqual([{ sender: '@void:x', ts: 20, hash: 'match' }]);
expect(a.count).toBe(1);
});
it('flags a hash that differs, and leaves one unchecked when either side has none', () => {
const a = one(root('$r', 1), [
file('$f', HASH),
file('$g'),
receipt('$x', 20, '@void:x', '$f', 'b'.repeat(64)),
receipt('$y', 21, '@void:x', '$g', HASH),
receipt('$z', 22, '@void:x', '$f', undefined),
]);
expect(a.files.find((f) => f.id === '$g')?.readBy[0].hash).toBe('unchecked');
expect(a.files.find((f) => f.id === '$f')?.readBy).toEqual([
{ sender: '@void:x', ts: 22, hash: 'unchecked' },
]);
});
it('keeps one receipt per Mind, the latest, and a second Mind beside it', () => {
const a = one(root('$r', 1), [
file('$f', HASH),
receipt('$1', 20, '@void:x', '$f', 'b'.repeat(64)),
receipt('$2', 30, '@void:x', '$f', HASH),
receipt('$3', 25, '@eureka:x', '$f', HASH),
]);
expect(a.files[0].readBy.map((r) => [r.sender, r.hash])).toEqual([
['@eureka:x', 'match'],
['@void:x', 'match'],
]);
});
it('ignores a receipt for a file not in the album, a malformed one, and a redacted one', () => {
const redacted = {
...receipt('$d', 20, '@void:x', '$f', HASH),
unsigned: { redacted_because: {} },
};
const a = one(root('$r', 1), [
file('$f', HASH),
receipt('$a', 20, '@void:x', '$elsewhere', HASH),
receipt('$b', 21, '@void:x', 42, HASH),
redacted,
]);
expect(a.files[0].readBy).toEqual([]);
expect(a.count).toBe(1);
});
});

View file

@ -1,6 +1,7 @@
// Pure: turns the thread roots of a file-zone room and each root's thread replies, in the JSON
// Synapse returns from GET /rooms/{roomId}/threads and GET /rooms/{roomId}/relations/{root}/m.thread,
// into albums. See docs/file-zone.md for the rulings this encodes.
import { FILE_HASH_KEY, FILE_RECEIVED } from './fileReceipt';
/** The slice of a Matrix event this module reads. Every field may be missing. */
export type ZoneEvent = {
@ -16,6 +17,9 @@ export type AlbumInput = { root: ZoneEvent; replies: ZoneEvent[] };
export type FileKind = 'audio' | 'image' | 'video' | 'file';
/** A Mind's receipt for one file. `hash` is whether its sha256 equals the one the file event carries. */
export type FileReader = { sender: string; ts: number; hash: 'match' | 'differs' | 'unchecked' };
export type AlbumFile = {
id: string;
kind: FileKind;
@ -23,6 +27,8 @@ export type AlbumFile = {
ts: number;
/** The event content, as RenderMessageContent reads it. */
content: Record<string, unknown>;
/** Receipts in this album that name this file, one per sender (the latest), oldest first. */
readBy: FileReader[];
};
export type Album = {
@ -70,9 +76,43 @@ const toFile = (e: ZoneEvent): AlbumFile | undefined => {
name: asString(e.content.filename) || asString(e.content.body),
ts: tsOf(e.origin_server_ts),
content: e.content,
readBy: [],
};
};
// A receipt is a thread reply of type org.gamecult.file_received whose content names a file of this
// album; it carries no msgtype, so it never passes `live`.
const receiptOf = (e: ZoneEvent) => {
if (!e || typeof e !== 'object' || e.type !== FILE_RECEIVED || !e.event_id) return undefined;
if (e.unsigned?.redacted_because) return undefined;
const body = e.content?.[FILE_RECEIVED] as { event_id?: unknown; sha256?: unknown } | undefined;
if (typeof body?.event_id !== 'string' || typeof e.sender !== 'string') return undefined;
return {
file: body.event_id,
sender: e.sender,
ts: tsOf(e.origin_server_ts),
sha256: body.sha256,
};
};
const attachReceipts = (files: AlbumFile[], replies: ZoneEvent[]) => {
const byId = new Map(files.map((f) => [f.id, f]));
replies
.map(receiptOf)
.filter((r): r is NonNullable<typeof r> => !!r)
.sort((a, b) => a.ts - b.ts)
.forEach((r) => {
const file = byId.get(r.file);
if (!file) return;
const sent = (file.content.info as Record<string, unknown> | undefined)?.[FILE_HASH_KEY];
let hash: FileReader['hash'] = 'unchecked';
if (typeof sent === 'string' && typeof r.sha256 === 'string')
hash = sent === r.sha256 ? 'match' : 'differs';
const others = file.readBy.filter((x) => x.sender !== r.sender);
file.readBy = [...others, { sender: r.sender, ts: r.ts, hash }];
});
};
export const UNTITLED = 'Untitled album';
// An edited root carries its latest edit under unsigned["m.relations"]["m.replace"]; the title is
@ -96,6 +136,7 @@ const toAlbum = (input: AlbumInput): Album | undefined => {
return true;
})
.sort((a, b) => a.ts - b.ts);
attachReceipts(files, Array.isArray(replies) ? replies : []);
return {
id: root.event_id,
title: titleOf(root),

View file

@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';
import { hashForSend, readLine, sha256Hex } from './fileReceipt';
describe('sha256Hex', () => {
it('hashes the bytes: the FIPS 180 vector for "abc"', async () => {
expect(await sha256Hex(new Blob(['abc']))).toBe(
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
);
});
it('hashes empty input', async () => {
expect(await sha256Hex(new Blob([]))).toBe(
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'
);
});
});
describe('hashForSend', () => {
it('gives no hash, and does not throw, when the blob cannot be read', async () => {
const broken = { arrayBuffer: () => Promise.reject(new Error('gone')) } as unknown as Blob;
expect(await hashForSend(broken)).toBeUndefined();
});
});
describe('readLine', () => {
const name = (s: string) => s.slice(1, s.indexOf(':'));
it('names each reader and flags a hash that differs', () => {
expect(
readLine(
[
{ sender: '@void:x', ts: 1, hash: 'match' },
{ sender: '@eureka:x', ts: 2, hash: 'differs' },
{ sender: '@aqua:x', ts: 3, hash: 'unchecked' },
],
name
)
).toBe('read by void, eureka (hash differs), aqua');
});
});

View file

@ -0,0 +1,29 @@
// The file-zone Mind contract (docs/file-zone-mind-contract.md): the plaintext hash a sender writes
// into a file event, and the receipt a Mind answers with.
import type { FileReader } from './buildAlbums';
/** Hex sha256 of the plaintext bytes, written at `content.info[FILE_HASH_KEY]` in unencrypted rooms. */
export const FILE_HASH_KEY = 'org.gamecult.sha256';
/** A Mind's receipt: event type, and the content key holding { event_id, sha256, size }. */
export const FILE_RECEIVED = 'org.gamecult.file_received';
export const sha256Hex = async (data: Blob): Promise<string> => {
const digest = await crypto.subtle.digest('SHA-256', await data.arrayBuffer());
return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, '0')).join('');
};
/** The hash to write for a file about to be sent in an unencrypted room; none when it cannot be computed. */
export const hashForSend = async (data: Blob): Promise<string | undefined> => {
try {
return await sha256Hex(data);
} catch {
return undefined;
}
};
export const readLine = (readBy: FileReader[], name: (sender: string) => string): string =>
`read by ${readBy
.map((r) => (r.hash === 'differs' ? `${name(r.sender)} (hash differs)` : name(r.sender)))
.join(', ')}`;

View file

@ -99,6 +99,7 @@ import {
getImageMsgContent,
getVideoMsgContent,
} from './msgContent';
import { FILE_HASH_KEY, hashForSend } from '../albums/fileReceipt';
import { getMemberDisplayName, getMentionContent, trimReplyFromBody } from '../../utils/room';
import { CommandAutocomplete } from './CommandAutocomplete';
import { Command, SHRUG, TABLEFLIP, UNFLIP, useCommands } from '../../hooks/useCommands';
@ -280,16 +281,23 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
const fileItem = selectedFiles.find((f) => f.file === upload.file);
if (!fileItem) throw new Error('Broken upload');
let content: IContent;
if (fileItem.file.type.startsWith('image')) {
return getImageMsgContent(mx, fileItem, upload.mxc);
content = await getImageMsgContent(mx, fileItem, upload.mxc);
} else if (fileItem.file.type.startsWith('video')) {
content = await getVideoMsgContent(mx, fileItem, upload.mxc);
} else if (fileItem.file.type.startsWith('audio')) {
content = getAudioMsgContent(fileItem, upload.mxc);
} else {
content = getFileMsgContent(fileItem, upload.mxc);
}
if (fileItem.file.type.startsWith('video')) {
return getVideoMsgContent(mx, fileItem, upload.mxc);
// A Mind verifies what it fetched against this. An encrypted file already carries the
// ciphertext hash in file.hashes; a plaintext hash would leak the file's identity.
if (!fileItem.encInfo) {
const sha256 = await hashForSend(fileItem.originalFile);
if (sha256) content.info = { ...content.info, [FILE_HASH_KEY]: sha256 };
}
if (fileItem.file.type.startsWith('audio')) {
return getAudioMsgContent(fileItem, upload.mxc);
}
return getFileMsgContent(fileItem, upload.mxc);
return content;
});
handleCancelUpload(uploads);
const contents = fulfilledPromiseSettledResult(await Promise.allSettled(contentsPromises));