~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

The host indexes its groups' spaces, read from each writer's repo

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/space-index

What changes

The host indexes its groups' spaces. index.rs keeps a copy of what each writer has written in each group space, read from wherever their repo is.

  • When it reads: as the space's authority, the host knows the writer set. When a writer's digest isn't the one the index came to, the index reads their repo: as the writer set moves, in the background (index_pending, woken by record_writer and each minute in serve), and before answering for the space.
  • A writer here is read from the store.
  • A writer elsewhere is read from their PDS, as knotless's space_sync.rs reads a space:
    • with a credential the group issues itself, bound to a key made for the read;
    • with a secret agreed against the writer's #atproto_kem key for each request.
  • Reading on: a repo read before is read on from listRepoOps, every page. The set hash is moved by each write and checked against the commit, whose MAC must check. Each record is checked against its write's CID.
  • Reading whole: a repo not read before, or one whose oplog doesn't check out, is read whole from getRepo's STAR archive and checked the same way. A record that isn't a DRISL map is hashed, since the commit covers it, and left out.
  • New tables index_repo and index_record. Deleting a space or an account forgets their part of the index.

The index is served by index_api.rs:

  • fyi.opensocial.listSpaceRecords lists records newest first, by collection, by a field's value (a dotted path), or one by URI, a page at a time.
  • fyi.opensocial.getSpaceBlob serves a file in a space. A file a writer here has is read from the store; one elsewhere is read from the writer's PDS. It must be the file asked for and at most 5 MB.
  • Both answer only those the space's access record lets read, asked on every call.

atproto gains space::read_star and space::parse_mac_header, checked against the vectors' archive and MAC.

From the last two pulls' reviews:

  • Addresses: a migration gives any account made before them its handle as its address.
  • Handle and identity lookups:
    • no redirects followed;
    • bodies capped at 256 KiB;
    • the resolver-service call has the 3s lookup timeout.
  • Spaces:
    • updateSpace refuses readableBy: [] rather than ignoring it;
    • meta's declaration follows the access record after it's written, not before;
    • a long doc comment is wrapped.

Why

This is the last part of step 4. A group's pages read their spaces' contents in one request, rather than a credential and a listing per writer. The prototype's spaceIndex.ts kept the same copy. The reading follows knotless's verified sync rather than the prototype's, including the knotless pull that leaves out a record that isn't a map. checkUserAccess comes with invites.

How it was checked

  • tests/index.rs:
    • Two hosts on one directory: a member of the second writes into a group space on the first. The first reads his repo whole, then reads on through an update, a delete and a create with a file. It serves the file from his host, and the group's own file from the store. Files not in the space are not found, and strangers and anonymous callers are refused.
    • Paging and filters: collection, cursor, a dotted field filter that reads on until the page is full, one by URI, malformed input, a space not the group's, an unknown group, and a switched-off group.
    • A fake writer's PDS answering each failure: reading whole when it's not vouched for, another repo's, not matching its commit, not an archive, or not answered; a non-map record left out. Reading on across two pages. Every way an oplog fails to check out, falling back to the whole read. A write whose value changed since, read whole and accepted. A writer with no host.
    • Files: right, not the one asked for, over 5 MB, and from a writer with no host.
  • tests/identity.rs: a redirect not followed, an oversized resolver answer, the address backfill.
  • tests/interop.rs: the authority test now has the index read the alpha member's repo from the reference PDS and checks her record. This runs in CI.
  • atproto/tests/space.rs: the STAR reader, round-tripped and refused six ways, and the MAC header parser.
  • just cov is at 100% of lines and functions. Clippy is clean.

Shortcomings

  • listSpaceRecords has no rev and no actors yet. Actors come with people's profiles.
  • Files fetched from writers aren't cached.
  • The field filter reads through the index page by page. A rare value in a large space reads far.

🤖 Generated with Claude Code

Asked for changes

This pull adds the space index. It reads each writer's repo from the store or their PDS, checks it against the vouched commit, and serves it through listSpaceRecords and getSpaceBlob. The verification flow looks sound: the MAC is checked, the commit's repo and space are checked, the set hash is moved and compared, and the whole read is the fallback. I read index.rs, index_api.rs and atproto/src/space.rs in full. I did not read the other files: store.rs, lib.rs, identity.rs, group_spaces.rs, group_lifecycle.rs, authority.rs, the docs and the tests. I did not run or read the checks.

Two things should change before it merges:

  1. getSpaceBlob takes any repo DID, not just a writer of the space. For anyone who may read the space, space_blob resolves that DID's document, takes the PDS URL from it, and sends a request there carrying a credential the group signed. A caller can make the host call an arbitrary server of their choosing, so this is an SSRF vector and it hands out group-signed credentials. Check that repo is in the space's writer set (store.writers) before the store lookup or the remote fetch. Add a test that a non-writer's DID gets NotFound and no request goes out.
  2. Remote bodies are read without a size cap. Remote::ask calls res.bytes() on getRepo and listRepoOps answers, so a hostile or broken writer PDS can make the host buffer a body as large as it can send within the 10s timeout. The file path already has FILE_AT_MOST. Cap these reads the same way, with a limit suited to a repo archive and an oplog page.

Also worth a look: list_space_records calls sync_space on every request, which walks all writers one after another over the network. A single slow or unreachable writer can add up to the 10s timeout per read. Failures are only logged, so the caller still gets the stale index with no signal. Consider bounding the time spent, or syncing only the writers whose digest differs, which the loop already does.

crates/group-host/src/index_api.rsline 204Outdated

These lines can no longer be read.

review.knotless.dev

repo is not checked against the space's writers, so any reader can make the host resolve an arbitrary DID, call its PDS, and send a group-signed credential there. Require repo to be a writer of space first.

crates/group-host/src/index.rsline 78Outdated

These lines can no longer be read.

review.knotless.dev

res.bytes() has no size limit, so a getRepo or listRepoOps answer from a hostile PDS is buffered whole. Stream it with a cap, as file does for blobs.

Both changed:

  • getSpaceBlob now refuses a repo not in the space's writer set before any lookup, local or remote. A test checks that a non-writer's DID gets NotFound and the fake PDS is never asked.
  • Remote answers (getRepo, listRepoOps, getBlob) are read in chunks through one capped reader: 64 MiB for a repo or an oplog page, 5 MiB for a file. The file test covers the cap.
  • listSpaceRecords now waits on the index's sync for 5s at most, then answers with the index as it is.
Approved

The pull adds the space index and serves it through listSpaceRecords and getSpaceBlob. The two changes I asked for last round are done, and I found nothing new that must change. I read index.rs and index_api.rs in full. I did not read the other 14 changed files (the atproto STAR reader, store.rs, lib.rs, identity.rs, the lifecycle and authority edits, the docs and the tests), and I did not read the checks.

  • SSRF in getSpaceBlob: fixed. space_blob now returns early unless repo is in the space's writer set (index_api.rs:197-202). That is before any DID resolution or credential signing. I did not read the test for it.
  • Uncapped remote bodies: fixed. Remote::ask now reads through capped(res, REPO_AT_MOST) (index.rs:81), which is 64 MiB. File fetches still use the 5 MB FILE_AT_MOST.
  • Slow writers on read: list_space_records now wraps sync_space in a 5s timeout and serves the index as it is when that runs out (index_api.rs:92). Only writers whose digest differs are fetched.

The 64 MiB cap is generous for one oplog page of 500 writes. A tighter limit for listRepoOps would make a hostile PDS cost less memory per request, but I wouldn't hold the merge for it.

Merged as 7957ebe