Files
Amicitia-Tabellarius/.agents/features/wake-up.md
T

7.5 KiB

Wake-up — a closed Android app learns that something arrived (PROTOCOL.md §6, G03)

Amicitia F25b. The relay already lists the push table in §5 (only if the user opts in, G03); this document designs it, its two routes and the request the relay sends. iOS (the publisher's gateway) is later: Amicitia F25c.

1. Why and what

Amicitia's notices (F25a) work only while the app is open in the background: a closed app, or one Android has put to sleep, hears nothing until it is opened. On Android, UnifiedPush lets the person choose a distributor app (ntfy and others), which keeps one connection to its own push server and wakes the apps registered with it. The relay's part is small: hold one endpoint URL per mailbox, given by the mailbox's owner, and POST a constant to it when a blob arrives. The wake carries no content, no sender and no mailbox id — only "something arrived".

2. Design

sequenceDiagram
    participant D as Distributor app (phone)
    participant A as Amicitia (phone)
    participant R as Relay
    participant P as Push server (e.g. ntfy.sh)
    A->>D: REGISTER (one token per mailbox)
    D-->>A: NEW_ENDPOINT (a capability URL at P)
    A->>R: PUT /v0/mailboxes/{id}/push {endpoint} (fetch token)
    Note over R: a contact posts a blob (post token)
    R->>P: POST endpoint, a constant body, at most once a minute per mailbox
    P-->>D: the wake
    D-->>A: MESSAGE → the constant notice, no sync needed

2.1 The wire (§8 additions)

Method and path Auth Effect
PUT /v0/mailboxes/{id}/push {endpoint} fetch token sets (or replaces) the mailbox's endpoint. 204
DELETE /v0/mailboxes/{id}/push fetch token removes it. 204, also when there was none
  • The endpoint (PushEndpoint in the protocol crate): at most 1 000 bytes (UnifiedPush's own limit), printable ASCII, https:// with a host, no user info, no fragment. http:// only to a loopback host (127.0.0.1, [::1], localhost), and a relay accepts those only when started with --push-loopback (tests); otherwise 400.
  • A wrong token or unknown mailbox: §8's bare 404. A bad body: 400.

2.2 The wake (the relay's only outgoing request)

  • After a successful post (the post token checked, the blob stored), if the mailbox has an endpoint and was not woken in the last 60 s, the relay POSTs to it — in the background, so the post's 201 never waits. The 60 s are remembered in memory only (mailbox id → second), never stored: §5 does not change.
  • The request: body wake (4 bytes, the same for every wake), headers TTL: 86400, Urgency: normal, Content-Type: application/octet-stream. No mailbox id, no size, no time. 10 s timeout, no redirects.
  • Where it may go: the host is resolved by the relay, and every address that is not global is dropped before connecting (loopback, private, link-local, CGNAT 100.64/10, multicast, unspecified, broadcast, documentation, ULA fc00::/7, fe80::/10, IPv4-mapped forms of all of these). No address left → no request. This is the guard against a mailbox owner using the relay to reach the relay's own network (SSRF). Loopback is kept only with --push-loopback.
  • At most 16 wakes in flight; beyond that a wake is dropped (the app syncs when opened anyway).
  • A gone endpoint (404 or 410) is removed — only if it is still the one that was woken.
  • The relay prints nothing about a wake, as it prints nothing about a request.

2.2b The NixOS module

The module's sandbox let the relay reach localhost only (IPAddressDeny=any), which would have dropped every wake without a word. New option services.tabellarius.wake (default on, decided by default): the relay may reach global addresses; every non-global range stays denied by systemd too (a second wall behind §2.2), and AF_UNIX is allowed so names resolve through nscd. Off: localhost only, as before. Not run here (no Nix on this machine; test.nix is unrun since F23).

2.3 Storage (§5's push row, made concrete)

push (mailbox_id BLOB PRIMARY KEY, endpoint TEXT). One row per mailbox at most. The mailbox counts already bound the rows.

2.4 The client crate

Transport::set_push(id, fetch, Option<&PushEndpoint>); Client::inboxes() lists the mailboxes this client fetches (each contact's inbox and each open welcome mailbox); Client::set_push(mailbox, endpoint) calls the relay with that mailbox's fetch token. MemoryTransport keeps the relay's rules.

3. Risks

  • Privacy — what the relay learns: an endpoint per mailbox that opted in. The client registers one endpoint per mailbox (UnifiedPush allows many registrations per app), so the relay cannot link two of a person's mailboxes by their endpoint — but it sees the endpoint's host. On a big shared push server (ntfy.sh) that host says little; on a person's own server it links all their mailboxes. Amicitia says so where the choice is made.
  • Privacy — what the push server learns: that the relay's address sent a wake to a capability URL of one device, and when. Not the mailbox, not the sender, not the content (the body is a constant). The person chose that server through their distributor.
  • SSRF: §2.2's resolver filter, no redirects, a short timeout, a cap on wakes in flight.
  • Abuse by a contact: only a valid post token causes a wake, and at most one a minute per mailbox. A blocked contact's posts are refused before the wake.
  • Web Push encryption (RFC 8291) and VAPID: not done. UnifiedPush's Android spec says the message content follows RFC 8291; the relay's body is a public constant, so there is nothing to hide, and ntfy delivers a plain body. A distributor that insists on VAPID refuses the registration (VAPID_REQUIRED); Amicitia says it cannot use that distributor. Gap W1.
  • Events / offline: the relay has no events. A wake that fails is not retried: the next blob wakes again, and opening the app fetches everything.

4. Gaps

  • W1 — Web Push encryption and VAPID for distributors that need them (e.g. Mozilla's autopush behind some distributors). Designed: the client sends its P-256 public key and auth secret with the endpoint; the relay encrypts the constant with a vetted crate (ece) — a new protocol field, and a crate not in the cache today.
  • W2 — iOS through the publisher's gateway (§6), Amicitia F25c.

5. Verification

Case Test
Endpoint rules: https; http only to loopback; length; no user info or fragment protocol/tests/wire.rs
PUT/DELETE need the fetch token; wrong token or unknown id → 404; bad body → 400 relay/tests/wake.rs
A post wakes once with the constant body; no mailbox id, no ciphertext in the request (canary) relay/tests/wake.rs, a loopback push server
Two posts within 60 s → one wake; after 60 s → another (clock injected) relay/tests/wake.rs
A refused post (wrong token, 507) wakes nothing; no endpoint → nothing relay/tests/wake.rs
410 from the push server → the row is gone relay/tests/wake.rs
http:// loopback refused without --push-loopback relay/tests/wake.rs
Non-global addresses dropped (v4, v6, mapped) unit tests in relay/src/wake.rs
§5's schema test includes push (mailbox_id, endpoint) relay/tests/relay.rs
A redirect is not followed; a gone answer does not remove an endpoint set since relay/tests/wake.rs
The client lists its inboxes and sets/clears an endpoint; memory transport and real relay client/tests/real_relay.rs, client/tests/state.rs