[relay] Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives #20

Closed
mnovak wants to merge 0 commits from feature/wake-up into main
Owner

Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives. For Amicitia F25b (a closed Android app hears that something arrived, through UnifiedPush). Analysis: .agents/features/wake-up.md. Protocol change, recorded with its date in docs/PROTOCOL.md §5, §6, §7 and §8.

  • PUT /v0/mailboxes/{id}/push {endpoint} / DELETE …/push, with the fetch token. The endpoint: https://, a host, no user info, no fragment, at most 1 000 bytes; http:// only to loopback, and only on a relay started with --push-loopback (tests).
  • After a blob is stored under a valid post token, the relay POSTs the constant wake to the endpoint, in the background, at most once a minute per mailbox (remembered in memory only), with no mailbox id, no size, no time. It connects only to global addresses (its own resolver drops every non-global one, IPv4-mapped/NAT64/6to4 forms included), follows no redirect, gives up after 10 s, has at most 16 wakes on their way. A 404/410 from the push server removes the endpoint, unless a newer one was set since.
  • §5's push row made concrete: push (mailbox_id, endpoint).
  • Client: Transport::set_push, Client::inboxes() (each contact's inbox and each open welcome mailbox), Client::set_push(mailbox, endpoint). One endpoint per mailbox, so the relay cannot link a person's mailboxes by endpoint.
  • NixOS module: new services.tabellarius.wake (default on, decided by default); the systemd sandbox allowed localhost only, which would have dropped every wake silently. With it on, every non-global range stays denied by systemd as well.
  • New dependency for the relay: ureq 3 with rustls (ring, Mozilla roots), from the local cargo cache — the same crate Amicitia already uses for its relay calls.
**Wake-up: a push endpoint per mailbox, and a constant wake to it when a blob arrives.** For Amicitia F25b (a closed Android app hears that something arrived, through UnifiedPush). Analysis: `.agents/features/wake-up.md`. **Protocol change**, recorded with its date in `docs/PROTOCOL.md` §5, §6, §7 and §8. - `PUT /v0/mailboxes/{id}/push {endpoint}` / `DELETE …/push`, with the **fetch token**. The endpoint: `https://`, a host, no user info, no fragment, at most 1 000 bytes; `http://` only to loopback, and only on a relay started with `--push-loopback` (tests). - After a blob is stored under a valid post token, the relay POSTs the constant `wake` to the endpoint, in the background, **at most once a minute per mailbox** (remembered in memory only), with no mailbox id, no size, no time. It connects only to **global addresses** (its own resolver drops every non-global one, IPv4-mapped/NAT64/6to4 forms included), follows no redirect, gives up after 10 s, has at most 16 wakes on their way. A 404/410 from the push server removes the endpoint, unless a newer one was set since. - §5's `push` row made concrete: `push (mailbox_id, endpoint)`. - Client: `Transport::set_push`, `Client::inboxes()` (each contact's inbox and each open welcome mailbox), `Client::set_push(mailbox, endpoint)`. One endpoint per mailbox, so the relay cannot link a person's mailboxes by endpoint. - NixOS module: new `services.tabellarius.wake` (default on, *decided by default*); the systemd sandbox allowed localhost only, which would have dropped every wake silently. With it on, every non-global range stays denied by systemd as well. - New dependency for the relay: `ureq` 3 with rustls (ring, Mozilla roots), from the local cargo cache — the same crate Amicitia already uses for its relay calls.
mnovak added 1 commit 2026-11-20 12:05:49 +01:00
mnovak reviewed 2026-11-20 12:05:50 +01:00
mnovak left a comment
Author
Owner

Adversarial self-review — wake-up

Evidence

What Result
cargo test --workspace protocol 17 (3 new); relay unit 2 (new: non-global addresses), quotas 8, relay 19, wake 9 (new); client unit 4, block 7, pairing 8, push 3 (new), real_relay 2 (1 new), state 14
clippy -D warnings (workspace, all targets), cargo fmt --check clean
scripts/check-deps.sh relay does not depend on client; protocol depends on nothing here
scripts/test-hooks.sh 7 passed
Planted bugs (mutate_wake.py) 17/17 caught (15 on the first run; the two misses — redirects followed, user info allowed — showed two weak tests, now fixed): no once-a-minute limit; the body carrying the endpoint; private addresses reachable; IPv4-mapped not unwrapped; loopback always reachable; the resolver not used; redirects followed; loopback endpoints accepted by any relay; 410 not gone; the post token setting the endpoint; a gone answer dropping a newer endpoint; a refused post waking; http to any host; user info allowed; Debug showing the capability URL; welcome mailboxes not listed; set_push on someone else's mailbox

What the wake carries (tested)

A canary blob is posted; the push server sees only the endpoint's own path, TTL, Urgency, Content-Type, ureq's User-Agent, and the body wake. The test searches the whole request for the mailbox id, both tokens, the blob's hash and the canary: none is there, and no Authorization header.

Findings

W1–W7 are on their lines.

Not verified

  • A real push server and distributor. Here the push server is an axum server on loopback. Delivery through ntfy to a phone is Amicitia F25b's phone check.
  • The NixOS module change (nix flake check and test.nix need Nix, not on this machine; the VM test is unrun since F23). In particular, that nscd resolves for the sandboxed relay, and that ring's build inside the Nix sandbox is reproducible.
  • Web Push encryption and VAPID (gap W1 in the analysis): a distributor that insists on VAPID cannot be used.

Verdict

Merging.

## Adversarial self-review — wake-up ### Evidence | What | Result | |:--|:--| | `cargo test --workspace` | protocol **17** (3 new); relay unit **2** (new: non-global addresses), quotas **8**, relay **19**, wake **9** (new); client unit **4**, block **7**, pairing **8**, push **3** (new), real_relay **2** (1 new), state **14** | | clippy `-D warnings` (workspace, all targets), `cargo fmt --check` | clean | | `scripts/check-deps.sh` | relay does not depend on client; protocol depends on nothing here | | `scripts/test-hooks.sh` | **7** passed | | Planted bugs (`mutate_wake.py`) | **17/17 caught** (15 on the first run; the two misses — redirects followed, user info allowed — showed two weak tests, now fixed): no once-a-minute limit; the body carrying the endpoint; private addresses reachable; IPv4-mapped not unwrapped; loopback always reachable; the resolver not used; redirects followed; loopback endpoints accepted by any relay; 410 not gone; the post token setting the endpoint; a gone answer dropping a newer endpoint; a refused post waking; http to any host; user info allowed; `Debug` showing the capability URL; welcome mailboxes not listed; `set_push` on someone else's mailbox | ### What the wake carries (tested) A canary blob is posted; the push server sees only the endpoint's own path, `TTL`, `Urgency`, `Content-Type`, ureq's `User-Agent`, and the body `wake`. The test searches the whole request for the mailbox id, both tokens, the blob's hash and the canary: none is there, and no `Authorization` header. ### Findings W1–W7 are on their lines. ### Not verified - **A real push server and distributor.** Here the push server is an axum server on loopback. Delivery through ntfy to a phone is Amicitia F25b's phone check. - **The NixOS module change** (`nix flake check` and `test.nix` need Nix, not on this machine; the VM test is unrun since F23). In particular, that nscd resolves for the sandboxed relay, and that `ring`'s build inside the Nix sandbox is reproducible. - Web Push encryption and VAPID (gap W1 in the analysis): a distributor that insists on VAPID cannot be used. ### Verdict Merging.
@@ -526,0 +526,4 @@
/// The mailboxes this client fetches: each contact's inbox (a blocked contact's too, which
/// nothing reaches) and each open invite's welcome mailbox. For wake-ups (`PROTOCOL.md` §6): an
/// app registers one push endpoint per mailbox, so the relay cannot link them by endpoint.
pub fn inboxes(&self) -> Vec<MailboxId> {
Author
Owner

W6 — privacy (linking). One endpoint per mailbox, so the relay cannot link a person's mailboxes by a shared endpoint; but it sees each endpoint's host. On ntfy.sh that says little; on a person's own push server it links all their mailboxes, and timing already partly does (CLAUDE.md §4, Metadata by timing). Amicitia must say so where wake-ups are turned on. A blocked contact's inbox stays listed, so turning wake-ups off clears it too.

**W6 — privacy (linking).** One endpoint per mailbox, so the relay cannot link a person's mailboxes by a shared endpoint; but it sees each endpoint's **host**. On ntfy.sh that says little; on a person's own push server it links all their mailboxes, and timing already partly does (CLAUDE.md §4, *Metadata by timing*). Amicitia must say so where wake-ups are turned on. A blocked contact's inbox stays listed, so turning wake-ups off clears it too.
@@ -102,0 +120,4 @@
let now = (s.clock)();
s.store.post(&id, &token, &body, now)?;
// Only after the blob is stored under a valid post token; the 201 does not wait for the wake.
if let Ok(Some(endpoint)) = s.store.push_endpoint(&id) {
Author
Owner

W5 — correctness. The wake comes only after store.post succeeded: a stranger, the owner's own fetch token, an over-quota (507) or an oversized post wakes nobody (planted bug "a refused post wakes" caught). The 201 does not wait for the wake (spawned).

**W5 — correctness.** The wake comes only after `store.post` succeeded: a stranger, the owner's own fetch token, an over-quota (507) or an oversized post wakes nobody (planted bug \"a refused post wakes\" caught). The 201 does not wait for the wake (spawned).
@@ -0,0 +49,4 @@
pub fn new(allow_loopback: bool) -> Self {
let config = Config::builder()
.timeout_global(Some(WAKE_TIMEOUT))
.max_redirects(0)
Author
Owner

W2 — security. No redirect is followed: a redirect would reach an address the resolver never judged by the endpoint's host. Test: a push server answering 303 to another path on itself sees nothing there (the first version of this test used 307 on a POST, which ureq would not have followed anyway, so the planted bug was missed; fixed).

**W2 — security.** No redirect is followed: a redirect would reach an address the resolver never judged by the endpoint's host. Test: a push server answering 303 to another path on itself sees nothing there (the first version of this test used 307 on a POST, which ureq would not have followed anyway, so the planted bug was missed; fixed).
@@ -0,0 +77,4 @@
if !self.due(id, now) {
return false;
}
let Ok(slot) = Arc::clone(&self.slots).try_acquire_owned() else {
Author
Owner

W3 — availability. At most 16 wakes in flight, 10 s each. Anyone can make a mailbox (no account), point it at a tarpit that never answers, and post to it once a minute; 16 such mailboxes hold every slot, and other people's wakes are dropped. Nothing leaks and nothing is lost (the app fetches everything when opened), but wakes can be starved. Recorded as a gap: a per-address cap, or a shorter timeout for a host that timed out before. An operator under such abuse can turn wakes off (wake = false).

**W3 — availability.** At most 16 wakes in flight, 10 s each. Anyone can make a mailbox (no account), point it at a tarpit that never answers, and post to it once a minute; 16 such mailboxes hold every slot, and other people's wakes are dropped. Nothing leaks and nothing is lost (the app fetches everything when opened), but wakes can be starved. Recorded as a gap: a per-address cap, or a shorter timeout for a host that timed out before. An operator under such abuse can turn wakes off (`wake = false`).
@@ -0,0 +113,4 @@
.header("TTL", "86400")
.header("Urgency", "normal")
.header("Content-Type", "application/octet-stream")
.send(WAKE_BODY);
Author
Owner

W4 — privacy. The body is a public constant and the headers are fixed: nothing of the mailbox, the blob, its size or its sender. The push server learns that this relay's address woke one of its capability URLs, and when — the cost the person accepts by choosing a distributor. Not Web Push encrypted (RFC 8291): nothing to hide in a constant, but see the analysis's gap W1 for distributors that need VAPID.

**W4 — privacy.** The body is a public constant and the headers are fixed: nothing of the mailbox, the blob, its size or its sender. The push server learns that this relay's address woke one of its capability URLs, and when — the cost the person accepts by choosing a distributor. Not Web Push encrypted (RFC 8291): nothing to hide in a constant, but see the analysis's gap W1 for distributors that need VAPID.
@@ -0,0 +150,4 @@
}
/// Whether the relay may connect to this address: global unicast only (loopback when allowed).
pub fn reachable(ip: IpAddr, allow_loopback: bool) -> bool {
Author
Owner

W1 — security (SSRF). The relay resolves the endpoint's host itself and keeps only global addresses; ureq connects to exactly the list this resolver returns, so DNS rebinding between check and connect has no gap. IPv4-mapped, IPv4-compatible, NAT64, 6to4 and Teredo forms are refused or judged as the IPv4 inside. With no redirects (W2), the relay cannot be steered at its own network by a mailbox owner.

**W1 — security (SSRF).** The relay resolves the endpoint's host itself and keeps only global addresses; ureq connects to exactly the list this resolver returns, so DNS rebinding between check and connect has no gap. IPv4-mapped, IPv4-compatible, NAT64, 6to4 and Teredo forms are refused or judged as the IPv4 inside. With no redirects (W2), the relay cannot be steered at its own network by a mailbox owner.
@@ -84,2 +97,3 @@
# allowed, so localhost (Caddy) stays reachable.
IPAddressAllow = "localhost";
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
IPAddressDeny =
Author
Owner

W7 — found in review: the deploy would have dropped every wake. The sandbox allowed localhost only. Now, with wake on (default), global addresses are allowed and every non-global range is still denied by systemd — a second wall behind W1 — and AF_UNIX lets glibc ask nscd for names. Unrun here: no Nix on this machine.

**W7 — found in review: the deploy would have dropped every wake.** The sandbox allowed localhost only. Now, with `wake` on (default), global addresses are allowed and every non-global range is still denied by systemd — a second wall behind W1 — and `AF_UNIX` lets glibc ask nscd for names. Unrun here: no Nix on this machine.
Author
Owner

Merged into main with --no-ff as 1d7f08927d (remote hash verified).

Merged into `main` with `--no-ff` as 1d7f08927d7bfab752dd64068cac0f3985a6ad79 (remote hash verified).
mnovak closed this pull request 2026-11-20 12:06:02 +01:00
Some checks are pending
ci / check (pull_request) Has been cancelled

Pull request closed

Sign in to join this conversation.