Ran the manual pass with a real Mastodon account through a cloudflared tunnel - the one gap every phase doc had flagged as outstanding before merging. WebFinger, actor resolution, follow/unfollow/approve/block, a forced weekly summary posting and deleting, and the now-playing profile field appearing/clearing all worked correctly against mastodon.social and activitypub.academy. Two real bugs found along the way, both fixed here: - docker-compose.yml hardcoded the api/worker services' CORS_ORIGINS to UI_BASE_URL. Pointing UI_BASE_URL at the tunnel host (needed for federation's own identity) silently changed which browser origins the API accepted, blocking the operator's own localhost:8080 UI - .env's own CORS_ORIGINS setting had no effect, since the compose file never referenced it. Now its own variable, same default. - TestConfig wasn't isolating UI_BASE_URL/CORS_ORIGINS from a developer's .env, so the same local change broke six tests that assert on http://localhost:8080 URLs - the same class of gap an earlier session found and fixed for FEDERATION_ENABLED/ LASTFM_API_KEY. Pinned alongside them. Marked the manual pass done in each phase doc (1-4) and the overview. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
15 KiB
ActivityPub phase 2: followers
Status: done on feature/activitypub (a6d3fb6 … fe95f59). The GoToSocial interop test passes locally and in CI. The manual pass with a real Mastodon account through a tunnel is also done: follow, unfollow, manual approval, and block/unblock all worked correctly.
Where this differed from the plan:
- httpcore instead of httpx. httpcore is the transport layer underneath httpx, and it publicly supports the custom network backend that connecting to a pre-checked address needs.
- No in-process test with two Scrobbler instances. Scrobbler can't initiate follows yet, so one instance has nothing to send the other. Scripted remote accounts over real HTTP cover the same paths, and GoToSocial is the real second server.
- Undo of a Block. Unblocking needed it: GoToSocial otherwise keeps the block.
This implements phase 2 of activitypub.md, building on phase 1.
Outcome: people on Mastodon, GoToSocial and similar servers can follow a user who shares. Follows are accepted automatically, or wait for the user's approval if they've turned it on. The user sees their followers and can approve, decline, remove or block them. Scrobbler signs everything it sends and checks the signature on everything it receives. Posts come in phase 3; this phase makes the relationship work end to end.
Everything below is inside scrobbler.federation, and the boundary tests keep holding. Federation off still means nothing is registered.
1. HTTP signatures (pure: protocol/signatures/)
base.py:- A
SignedRequestvalue (method, URL, headers, body). - A
SignatureSchemeinterface withsign(request, key_id, private_key)andverify(request, public_key_for) -> VerifiedSignature(the key ID used and the scheme). SignatureErrorcarries a machine-readable reason:missing,malformed,expired,digest_mismatch,bad_signatureorunsupported.- A registry keyed by scheme name.
- A
draft_cavage.py:- Signing (the header shapes are in Mastodon's security spec):
Signature: keyId="…",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="…"Digest: SHA-256=<base64>for POSTs(request-target)is the lowercase method plus path and query.
- Verification:
- accepts
rsa-sha256andhs2019, which means RSA when the key is RSA - for POSTs, requires
(request-target),host,dateanddigestto be signed Datewithin ±12 hours- the digest must match the body
- accepts
- Signing (the header shapes are in Mastodon's security spec):
rfc9421.py:- Signing:
Signature-Input: sig1=("@method" "@target-uri" "content-digest");created=…;keyid="…";alg="rsa-v1_5-sha256", withSignature: sig1=:…:andContent-Digest: sha-256=:<base64>:. - Verification:
- one signature
createdrequired and within the allowed skew@methodand@target-uricovered, andcontent-digesttoo when there's a body
- A minimal parser for the RFC 8941 structured fields these headers use: inner lists, parameters and byte sequences.
- Signing:
- Order:
- Outgoing:
FEDERATION_SIGNATURE_SCHEMES(defaultdraft-cavage,rfc9421) sets the order schemes are tried. The first is used, and the next is tried only if a server answers 401, the same "double-knocking" Mastodon 4.7 does. - Incoming: whichever scheme is present is verified.
- Outgoing:
- Tests:
- the published test vectors (draft-cavage appendix C; RFC 9421 appendix B for
rsa-v1_5-sha256) - sign-then-verify round trips for both schemes
- tampered body, method, path, date and headers
- expired and future dates, missing components, and a wrong key
- the published test vectors (draft-cavage appendix C; RFC 9421 appendix B for
2. Safe outbound HTTP and remote actors
- New dependency:
httpx(BSD-3-Clause), for pooled clients, timeouts and response streaming. net.py, the only way federation code talks to other servers:- HTTPS only.
- SSRF protection: a host's addresses are resolved once; the request is refused if any is loopback, private, link-local, multicast or otherwise reserved; and the connection goes to the checked address, using the original host name for SNI and
Host. So a DNS change mid-request can't redirect it. - At most 3 redirects, each checked the same way.
- Response bodies are capped at 1 MB.
- Timeouts: 10s to connect and 20s to read.
- A
User-AgentofScrobbler/<version> (+<base url>). signed_get(url, as_actor), which signs with the instance actor by default so "authorized fetch" servers answer; andsigned_post(inbox, activity, as_user).
FEDERATION_INSECURE_TESTING=1is for automated tests only. It allowshttp://and private addresses. It's refused unless the domain ends in.test(a reserved top-level domain) and logs a warning every minute while on. It exists so CI can federate with GoToSocial on a private Docker network.remote.py: the remote-actor cache.get_actor(uri, refresh=False)fetches, validates and stores: the ID must equal the URI it was fetched from, the host must match, it must have aninbox, andpublicKey.ownermust equal the actor ID.key_for(key_id)maps a key ID (…#main-key, or a separate key document) to its owner and PEM.- An actor is refreshed after 24 hours, or once immediately when a signature fails, in case the key was rotated.
3. Data (migration on the federation branch)
| Table | Holds |
|---|---|
federation_remote_actors |
URI, inbox, shared inbox, preferredUsername, host, display name, public key ID and PEM, fetched_at, gone |
federation_followers |
User, remote actor, state (pending or accepted), the Follow activity's ID and JSON, created and accepted times. Unique per user and actor. |
federation_blocks |
User and remote actor, or a whole domain. Operator domain blocks come from FEDERATION_BLOCKED_DOMAINS, not this table. |
federation_inbox |
Received deliveries: raw headers (a fixed allow-list), body, the inbox path, received_at, status (queued, processed, rejected or failed), reason, and the activity ID (unique, for deduplication). Pruned after 30 days. |
federation_activities |
Activities we send: UUID, user, type, JSON, and created_at. Dereferenceable at /activities/<uuid> when public, so it can be the outbox's source in phase 3. |
federation_deliveries |
Activity × inbox: status (pending, delivered, failed or abandoned), attempts, next_attempt_at, the last status code and error |
4. Inbox intake (web.py) and processing (a worker task)
- Intake stays network-free.
POST /inboxandPOST /users/<u>/inboxcheck the size (256 KB), the content type and that the JSON hastype,actorandid, then store the delivery and answer 202.- Deliveries from blocked domains are refused with 403.
- Repeated activity IDs are acknowledged but not stored again.
- The UI's nginx rate-limits the inbox paths per client IP (
limit_req), as a first defence.
- The
federation.inboxworker task processes queued deliveries, oldest first:- Check the signature with the scheme present, fetching the key through
remote.key_for.- On failure, it refreshes the actor once and retries.
- Then it rejects with a reason, which is counted in metrics.
- Check it's who they say. The signing key's owner must equal the activity's
actor, and the activity ID's host must equal the actor's host. - Dispatch to a handler registered by activity type:
- Check the signature with the scheme present, fetching the key through
| Activity | Handling |
|---|---|
Follow (object = a sharing user) |
Store the follower. If manual approval is on, keep it pending; otherwise mark it accepted and queue an Accept. If already accepted (a repeat Follow), queue an Accept again. |
Undo of a Follow |
Remove the follower. The Undo must come from the same actor as the Follow. |
Delete of an actor |
Remove it from every user's followers and mark the cached actor gone. |
Update of a Person or Service |
Refresh the cached actor. |
Accept or Reject |
Recorded; nothing to do until we follow people. |
Like, Announce, Create |
Recorded for metrics only. |
| Anything else | Recorded as unsupported. |
- A delivery that errors unexpectedly is retried twice and then marked
failedwith the error. The worker never stops over it.
5. Sending: activities and deliveries
activities.pybuilds and stores our activities:Accept(Follow): the object is the original Follow, embedded.Reject(Follow): used both to decline a request and to remove an existing follower. Mastodon drops the relationship on either.Block: sent when a user blocks a follower.
- Fan-out turns an activity into delivery rows, one per distinct inbox. Replies to a Follow go to the follower's own inbox.
- The
federation.deliverworker task:- Sends pending deliveries whose time has come, with signed POSTs through
net.signed_post, trying the next scheme on a 401. - Retries: 1m, 5m, 30m, 2h, 6h, then every 12h, abandoning after 2 days.
- No retry after a 4xx, except 401, 408 and 429.
- 410 Gone from an actor's inbox removes that follower.
- It runs several deliveries at once (a bounded thread pool, default 4) and processes at most 50 per pass, so the worker stays responsive.
- Sends pending deliveries whose time has come, with signed POSTs through
- Actor changes:
- The actor now reflects the user's real
manually_approves_followerssetting; phase 1 always said true. - The followers collection's
totalItemsis the accepted-follower count.
- The actor now reflects the user's real
- Turning sharing off queues a
Rejectfor every follower, then removes them.
6. Managing followers (API and UI)
- API (bearer auth, under
/api/v1/federation, documented in OpenAPI):GET /followers?state=accepted|pending, returning handle, display name, actor URL, server and since when.POST /followers/{id}/approve, which sendsAccept.POST /followers/{id}/declineandPOST /followers/{id}/remove, which sendReject.POST /followers/{id}/block, which sendsBlockand remembers it.GET /blocksandDELETE /blocks/{id}to see and undo blocks. Unblocking doesn't restore the follow.- The settings response gains accepted and pending follower counts.
- UI:
- A new
followers.htmlshows requests (Approve / Decline) above followers (Remove / Block, each with a confirm step), and blocked accounts at the bottom. - Remote names are shown as plain text only; no remote HTML or images in this phase.
- The Sharing page shows the follower count and links to the Followers page.
- The "following arrives later" note goes.
- A new
7. Configuration
| Variable | Meaning |
|---|---|
FEDERATION_SIGNATURE_SCHEMES |
Outgoing order; default draft-cavage,rfc9421 |
FEDERATION_BLOCKED_DOMAINS |
Comma-separated domains (subdomains included) whose deliveries are refused and whose follows are rejected |
FEDERATION_DELIVERY_CONCURRENCY |
Parallel deliveries in the worker (default 4) |
FEDERATION_INSECURE_TESTING |
Tests only; see §2 |
8. Metrics, dashboard and alerts
- Metrics:
scrobbler_federation_inbox_activities_total{type,result}, where result isprocessed,rejected,duplicate,blockedorfailedscrobbler_federation_signatures_total{direction,scheme,result}, where result isokor aSignatureErrorreasonscrobbler_federation_deliveries_total{result}:delivered,retry,abandoned,gonescrobbler_federation_delivery_seconds(a histogram)scrobbler_federation_queue{queue,status}, a gauge set by the worker for the inbox and delivery queuesscrobbler_federation_followers{state}, a gauge set by the worker
- Federation dashboard gains:
- inbox activity by type and result
- signature failures by reason
- delivery outcomes and latency
- queue depths
- follower totals
- Alerts:
FederationDeliveryBacklog: more than 100 pending deliveries for 30 minutesFederationInboxBacklog: queued inbox items for 15 minutesFederationSignatureFailures: more than 20% of incoming signatures failing for 15 minutes
9. Tests
-
Unit tests:
- signatures (§1)
- the SSRF guard: private and reserved IPs,
http, redirects to private addresses, the oversized-body cap and timeouts - remote-actor validation: ID mismatch, key owner mismatch, a missing inbox
- each inbox handler
- the retry schedule and 4xx/410 handling
- addressing
-
In-process federation: two federation-enabled apps with different
.testdomains serving each other over real HTTP in the test process, with insecure testing on. Scripted remote actors cover the full path: follow → signature check → Accept delivered and verified by the "remote" → Undo. Also covered:- manual approval
- decline, remove and block
- repeat Follow
- bad signatures rejected with the right reason
- a blocked domain
- turning sharing off
-
A real peer in CI: a new
federation-interopjob runs Scrobbler (API, worker and UI) and a GoToSocial container on one Docker network. The hosts arescrobble.testandgts.test, over HTTP, with each side's test-only settings. A script:- creates a GoToSocial user with its admin CLI,
- gets a user token by driving GoToSocial's sign-in and OAuth screens with plain HTTP (this part gets a short spike first, since GoToSocial has no password grant),
- follows
@alice@scrobble.test, and checks it's accepted, - unfollows, and checks it's gone,
- with manual approval on, checks the request waits until approved through Scrobbler's API.
The job posts to ntfy like every other job. It doesn't run on pull requests from forks.
-
Manual pass before merging: follow and unfollow from a real Mastodon account through a tunnel.
10. Docs
- Update the README and the federation README: following now works, the new settings, and the operator domain blocks.
- Regenerate the OpenAPI export.
- Mark this plan done.
Order of work (a commit per step once its tests pass)
- Signatures, pure, with test vectors
net.pyand the remote-actor cache- Tables and migration
- Inbox intake and processing, with handlers
- Outgoing activities and the delivery task
- Follower management API and UI; the actor reflects the approval setting; turning sharing off
- Metrics, dashboard and alerts
- In-process federation tests, and the GoToSocial CI job
- Docs
Notes
- Licences:
httpxis BSD-3-Clause andcryptographyis Apache-2.0/BSD.- GoToSocial is AGPL, but it runs only as a separate container in CI to test against. Nothing of it is linked into, or shipped with, Scrobbler, so it has no effect on Scrobbler's licence.
- Scale: this is the biggest phase, and signatures plus safe outbound HTTP are where the risk lies. They come first and have the most thorough tests.