Calls: long-poll, TURN credentials, wakes every 10 s (v0.2.0) #27

Closed
mnovak wants to merge 0 commits from feature/calls-relay into main
Owner

The relay's part of calls (.agents/features/calls-relay.md; for Amicitia F36). This is a protocol change, and docs/PROTOCOL.md §5, §6, §7 and §8 say what changed, when and why.

  • GET /v0/mailboxes/{id}/blobs?wait=N (at most 25 s): a long-poll, so a call rings within about a second. In memory only; at most 1 024 held.
  • POST /v0/mailboxes/{id}/turn (fetch token): coturn REST API credentials, valid 600 s, 30 per mailbox per hour. Nothing is stored.
  • Wakes at most every 10 s per mailbox (was 60), so a call right after a message still wakes a closed app.
  • CLI: --turn-secret-file and --turn-url, which come together or not at all. Module: services.tabellarius.turn.{secretFile, urls}; the secret reaches the relay through systemd's LoadCredential and never enters the Nix store.
  • The VM test exercises the TURN route and the long-poll, with a secret made at boot.
  • Version 0.2.0.

The self-review is posted below.

The relay's part of calls (`.agents/features/calls-relay.md`; for Amicitia F36). This is a protocol change, and `docs/PROTOCOL.md` §5, §6, §7 and §8 say what changed, when and why. - `GET /v0/mailboxes/{id}/blobs?wait=N` (at most 25 s): a long-poll, so a call rings within about a second. In memory only; at most 1 024 held. - `POST /v0/mailboxes/{id}/turn` (fetch token): coturn REST API credentials, valid 600 s, 30 per mailbox per hour. Nothing is stored. - Wakes at most every 10 s per mailbox (was 60), so a call right after a message still wakes a closed app. - CLI: `--turn-secret-file` and `--turn-url`, which come together or not at all. Module: `services.tabellarius.turn.{secretFile, urls}`; the secret reaches the relay through systemd's `LoadCredential` and never enters the Nix store. - The VM test exercises the TURN route and the long-poll, with a secret made at boot. - Version 0.2.0. The self-review is posted below.
mnovak added 2 commits 2026-12-03 00:43:27 +01:00
mnovak reviewed 2026-12-03 00:43:36 +01:00
mnovak left a comment
Author
Owner

Self-review (CLAUDE.md §3, adversarial).

Evidence

  • cargo test --workspace: client 76 (unit 24, block 7, files 8, pairing 8, polls 4, push 3, reactions 5, real_relay 3, state 14), protocol 20 (wire), relay 57 (unit 2, calls 11 new, files 8, quotas 8, relay 19, wake 9). cargo clippy --workspace --all-targets -D warnings: clean.
  • Hub, NixOS runner: r4gsqcnj9471h, ok (package with tests; the VM test with turn enabled: TURN 200 for the fetch token, 404 for the post token, ?wait=1 200, the client's IP nowhere, the TURN secret not in the relay's data or /var/log). Then rarh8t363kzbx at the 0.2.0 commit.
  • Planted bugs, 2/2 caught: (1) without arrivals.arrived(&id) in post_blob, a_long_poll_answers_as_soon_as_a_blob_arrives fails (held the full 25 s); (2) TURN authorised by the post token instead of the fetch token: three TURN tests fail.

Findings

  1. Fixed before the PR: turn.secretFile was typed path. A local path would be copied into the world-readable Nix store, secret included. It is a string now, and the secret travels by LoadCredential.
  2. Privacy, documented: a held long-poll tells the relay that a client is online, and a TURN request tells it that a mailbox is calling now. Both were already visible in fetch timing (PROTOCOL.md §8 says so). Nothing new reaches the disk: the waiter map and the TURN counters are in memory, like the wake times.
  3. Abuse: the 1 024-waiter cap is relay-wide, so one client holding many waits pushes everyone back to immediate answers. That degrades to plain polling, not to a denial. A per-mailbox cap is unnecessary, since a mailbox has one owner.
  4. Cost: Arrivals::enter sums every entry, which is O(n) with n ≤ 1 024 under a mutex. Cheap at this size; a counter would do if the cap grows.
  5. Not verified here: that coturn accepts these credentials. The tests compute the HMAC-SHA1 the way coturn's use-auth-secret documents it, but coturn itself is in the next PR (its NixOS service and a VM test with turnutils_uclient). Until then no URL points at a TURN server, and the route stays off.
  6. Rate limit per mailbox: a client with many mailboxes can get 30 credentials an hour for each. The credentials only open the publisher's TURN for 10 minutes; acceptable.
Self-review (CLAUDE.md §3, adversarial). **Evidence** - `cargo test --workspace`: client 76 (unit 24, block 7, files 8, pairing 8, polls 4, push 3, reactions 5, real_relay 3, state 14), protocol 20 (wire), relay 57 (unit 2, **calls 11** new, files 8, quotas 8, relay 19, wake 9). `cargo clippy --workspace --all-targets -D warnings`: clean. - Hub, NixOS runner: r4gsqcnj9471h, ok (package with tests; the VM test with `turn` enabled: TURN 200 for the fetch token, 404 for the post token, `?wait=1` 200, the client's IP nowhere, the TURN secret not in the relay's data or /var/log). Then rarh8t363kzbx at the 0.2.0 commit. - **Planted bugs, 2/2 caught:** (1) without `arrivals.arrived(&id)` in `post_blob`, `a_long_poll_answers_as_soon_as_a_blob_arrives` fails (held the full 25 s); (2) TURN authorised by the post token instead of the fetch token: three TURN tests fail. **Findings** 1. **Fixed before the PR:** `turn.secretFile` was typed `path`. A local path would be copied into the world-readable Nix store, secret included. It is a string now, and the secret travels by `LoadCredential`. 2. **Privacy, documented:** a held long-poll tells the relay that a client is online, and a TURN request tells it that a mailbox is calling now. Both were already visible in fetch timing (PROTOCOL.md §8 says so). Nothing new reaches the disk: the waiter map and the TURN counters are in memory, like the wake times. 3. **Abuse:** the 1 024-waiter cap is relay-wide, so one client holding many waits pushes everyone back to immediate answers. That degrades to plain polling, not to a denial. A per-mailbox cap is unnecessary, since a mailbox has one owner. 4. **Cost:** `Arrivals::enter` sums every entry, which is O(n) with n ≤ 1 024 under a mutex. Cheap at this size; a counter would do if the cap grows. 5. **Not verified here:** that coturn accepts these credentials. The tests compute the HMAC-SHA1 the way coturn's `use-auth-secret` documents it, but coturn itself is in the next PR (its NixOS service and a VM test with `turnutils_uclient`). Until then no URL points at a TURN server, and the route stays off. 6. **Rate limit per mailbox:** a client with many mailboxes can get 30 credentials an hour for each. The credentials only open the publisher's TURN for 10 minutes; acceptable.
@@ -0,0 +29,4 @@
/// A place to wait for `id`, or `None` when the relay already holds `max` waiters.
pub fn enter(&self, id: MailboxId) -> Option<Place<'_>> {
let mut w = self.waiting.lock().unwrap_or_else(|e| e.into_inner());
let total: usize = w.values().map(|(_, n)| n).sum();
Author
Owner

Finding 4: O(n) under the lock, n ≤ 1 024; fine now, a counter if the cap grows.

Finding 4: O(n) under the lock, n ≤ 1 024; fine now, a counter if the cap grows.
@@ -158,0 +200,4 @@
};
let notified = place.notify.notified();
tokio::pin!(notified);
notified.as_mut().enable();
Author
Owner

The waiter is enabled before the store is read, so a blob posted between the read and the wait still wakes it. Planted bug 1 removes the notify in post_blob and the test catches it.

The waiter is enabled before the store is read, so a blob posted between the read and the wait still wakes it. Planted bug 1 removes the notify in post_blob and the test catches it.
@@ -67,0 +67,4 @@
turn = {
secretFile = lib.mkOption {
# A string, not a path: a path would copy the secret into the world-readable Nix store.
type = lib.types.nullOr lib.types.str;
Author
Owner

Finding 1: a path option would copy the secret into /nix/store (world-readable). Kept a string; LoadCredential below hands it to the DynamicUser.

Finding 1: a `path` option would copy the secret into /nix/store (world-readable). Kept a string; LoadCredential below hands it to the DynamicUser.
Author
Owner

Merged into main as 7a2b825 (--no-ff; the remote hash was checked). Tagged v0.2.0 on the merge, the tag the home server deploys from.

Merged into main as 7a2b825 (`--no-ff`; the remote hash was checked). Tagged **v0.2.0** on the merge, the tag the home server deploys from.
mnovak closed this pull request 2026-12-03 01:31:16 +01:00
Some checks are pending
ci / check (pull_request) Failing after 42s

Pull request closed

Sign in to join this conversation.