From 00ad8e9e6dc4b0aeb5e5c4b747f013dba622593b Mon Sep 17 00:00:00 2001 From: Matthew Daubney Date: Fri, 25 Sep 2026 20:10:08 +0100 Subject: [PATCH] ActivityPub decisions: UI domain, followers-only posts; phase 1 plan - Handles and actor URLs live on the web UI's domain; the UI's nginx forwards the ActivityPub paths to the API - Posts default to followers only, settable per user, with the privacy caveat about automatic follow approval spelled out - docs/design/activitypub-phase1.md: the discoverable-profile phase, step by step Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW --- docs/design/activitypub-phase1.md | 116 +++++++++++++++++++++++++++++ docs/design/activitypub.md | 49 ++++++++---- src/scrobbler/federation/README.md | 4 +- 3 files changed, 152 insertions(+), 17 deletions(-) create mode 100644 docs/design/activitypub-phase1.md diff --git a/docs/design/activitypub-phase1.md b/docs/design/activitypub-phase1.md new file mode 100644 index 0000000..1cb98d4 --- /dev/null +++ b/docs/design/activitypub-phase1.md @@ -0,0 +1,116 @@ +# ActivityPub phase 1: discoverable profile + +This implements phase 1 of [activitypub.md](activitypub.md). Handles and actor URLs are on the **UI's domain**, and posts are **followers only by default** (a per-user setting). + +**Outcome:** once an operator enables federation, a user can switch on sharing, and searching `@alice@` from Mastodon finds their profile. Following needs phase 2 (the inbox and deliveries). Until then the actor advertises `manuallyApprovesFollowers: true`, so a follow shows as a pending request rather than failing silently. With federation off, nothing changes. + +Everything below lives in `scrobbler.federation` unless it says otherwise, and the boundary tests keep holding. + +## 1. Data and keys + +- **New dependency**: `cryptography` (Apache-2.0 or BSD). +- **Migration on the `federation` branch** (`flask db migrate --head federation@head`), adding two tables: + - `federation_settings`, keyed by user: + - `enabled` (false) + - `visibility`: `followers` (the default), `unlisted` or `public` + - `manually_approves_followers` (false) + - `discoverable` and `indexable` (both false) + - `display_name`, `bio` + - `created_at` and `updated_at` + - `federation_keys`: + - a user ID, which is null for the instance actor + - the public key PEM + - the private key PEM encrypted with Fernet + - `created_at` +- `keys.py`: + - An RSA-2048 key pair is created the first time a user enables sharing. The instance actor's pair is created on first use. + - Private keys are encrypted with a key derived by HKDF-SHA256 from **`FEDERATION_KEY_SECRET`**. That setting becomes required when federation is on, and is validated at start-up. + - Keys are never regenerated, because followers pin them. + +## 2. Protocol (pure: `federation/protocol/`) + +- `media.py`: the ActivityPub content types (`application/activity+json`, and `application/ld+json` with the ActivityStreams profile), plus a check for whether a request's `Accept` header wants ActivityPub. +- `vocab.py`, which builds dicts with `@context`: + - `person()`: `id`, `preferredUsername`, `name`, `summary`, `url`, `inbox`, `outbox`, `followers`, `following`, `endpoints.sharedInbox`, `publicKey`, `manuallyApprovesFollowers`, `discoverable`, `indexable` + - `application()` for the instance actor + - `ordered_collection()` +- `webfinger.py`: parses `acct:user@domain` resources (case-insensitive, trimmed, rejecting other domains) and builds the JRD: `subject`, `aliases`, a `self` link and a `profile-page` link. +- `nodeinfo.py`: the discovery document and the NodeInfo 2.1 document. +- `hostmeta.py`: the XRD for `/.well-known/host-meta`, which points at WebFinger. + +These are unit-tested against the documented Mastodon shapes and examples from the WebFinger RFC. + +## 3. ActivityPub endpoints (`federation/web.py`) + +| Path | Response | +|---|---| +| `GET /.well-known/webfinger?resource=acct:u@DOMAIN` | JRD for users who opted in. **404 for unknown or non-opted users, with an identical body.** 400 for a malformed or foreign resource. | +| `GET /.well-known/host-meta` | XRD pointing at WebFinger | +| `GET /.well-known/nodeinfo`, `GET /nodeinfo/2.1` | Software `scrobbler`, the version, `activitypub`, and the count of users who opted in | +| `GET /users/` | Actor JSON when the request asks for ActivityPub; otherwise a **302 to the UI profile page**; 404 if not opted in | +| `GET /users//outbox`, `/followers`, `/following` | Empty collections with `totalItems` (filled in phases 2 and 3) | +| `POST /users//inbox`, `POST /inbox` | 202 and discard, for now (phase 2 handles it) | +| `GET /actor` | The instance actor (an `Application`), used later for signed fetches | + +- All IDs and URLs are built from `FEDERATION_BASE_URL`, never from the request, so a proxy misconfiguration can't produce wrong IDs. +- Responses are cacheable: `Cache-Control: max-age=300` on the actor and WebFinger. +- CORS stays unchanged: remote servers don't send CORS requests. + +## 4. UI-facing API (`federation/api.py`, `/api/v1/federation/*`, documented in OpenAPI) + +- **`GET` and `PUT /settings`** (bearer auth): + - Returns `enabled`, `visibility`, `manually_approves_followers`, `discoverable`, `indexable`, `display_name`, `bio`, the read-only `handle` and `profile_url`, and `followers` (0 until phase 2). + - A 404 means the server doesn't federate, and the UI hides the Sharing page. +- **`GET /profiles/`** is **public** (no auth). It returns the profile shown on the UI profile page, for opted-in users only, and 404 otherwise. +- **Registration**: federation registers its smorest blueprint on the core's `Api` object. This means allowing `scrobbler.api` itself (the `Api` instance) in the boundary test's list of permitted imports, alongside `scrobbler.api.decorators`. + +## 5. Web UI (in `frontend/`, not the Python module) + +- **`sharing.html`**, shown in the nav only when `/federation/settings` exists: + - A **Share my listening on the fediverse** switch, showing the handle and the warning that it's permanent. + - A **Who can see posts** choice: *Followers only* (default), *Unlisted* or *Public*. Its help text says to turn on manual approval for real privacy. + - **Approve followers manually**, **List my profile in directories** (`discoverable`) and **Allow search engines** (`indexable`). + - Display name and bio. +- **`profile.html?u=`**: a public page with no login needed, where browsers opening an actor URL land. + - It shows the display name, handle, bio and how to follow ("search for `@alice@host` in your fediverse app"), with a copy button. + - It uses the existing styles, including the dot-matrix display for the handle. + +## 6. Proxying from the UI domain (`deploy/nginx/default.conf.template`) + +- Forward these paths to `${API_INTERNAL_URL}` (default `http://api:8000`, set in the UI image): + - `/.well-known/webfinger`, `/.well-known/nodeinfo`, `/.well-known/host-meta` + - `/nodeinfo/`, `/users/`, `/inbox`, `/actor` +- Pass `Host`, `X-Forwarded-For` and `X-Forwarded-Proto` through. The proto comes from an outer TLS proxy when there is one. +- The API trusts the UI proxy via `FORWARDED_ALLOW_IPS`. +- No changes to the UI's CSP: these are server-to-server requests. +- Compose adds the federation settings to `api` and `worker`, and `API_INTERNAL_URL` to `ui`. + +## 7. Metrics and dashboard + +- `federation/metrics.py` holds federation's own metrics: + - `scrobbler_federation_lookups_total{kind,result}`, where kind is `webfinger`, `actor`, `nodeinfo` or `profile`, and result is `found` or `not_found` + - `scrobbler_federation_sharing_changes_total{change}`, for `enabled` and `disabled` +- Request rate and latency come free from the existing HTTP metrics (endpoint label `federation.*`). +- A new **Federation** Grafana dashboard: lookups by kind and result, sharing changes, and federation endpoint latency. Phases 2 and 3 add delivery and inbox panels. + +## 8. Tests + +- **Protocol**: WebFinger parsing, including other domains, mixed case and garbage; JRD, actor and NodeInfo shapes; `Accept` negotiation. +- **Keys**: encryption round trip, the wrong secret failing, and keys staying stable across enable and disable. +- **Endpoints**, against a second session app with federation on, sharing the same database: + - opted-in users vs 404 + - identical 404s for unknown and non-opted users + - the browser redirect to the profile page + - IDs built from config + - the 202 inbox stub +- **Settings API**: defaults (followers only, not discoverable), validation, and the public profile endpoint. +- **Still holding**: boundary tests, federation off meaning nothing is registered, and the OpenAPI drift test. +- **Manual pass**: Mastodon needs a public HTTPS domain. A tunnel (for example `cloudflared`) to a local stack, then searching the handle from a Mastodon account, confirms the profile and key resolve. GoToSocial in CI comes with phase 2. + +## 9. Docs + +- The federation README gains the proxy paths and `FEDERATION_KEY_SECRET`. +- The main README gets a "Sharing on the fediverse" section. +- The OpenAPI export is regenerated. + +Commit after each numbered section once its tests pass. diff --git a/docs/design/activitypub.md b/docs/design/activitypub.md index b11430e..1e86adc 100644 --- a/docs/design/activitypub.md +++ b/docs/design/activitypub.md @@ -26,7 +26,7 @@ All of it lives in **one self-contained package, `scrobbler.federation`**, behin - **Weekly summary**: on by default once sharing is on. - **Milestones**: on by default. - **Now playing**: off by default, with two ways to show it. See [Now playing](#now-playing). - - **Who can see posts**: *Unlisted* (recommended: followers and profile visitors only, not public timelines), *Public*, or *Followers only*. + - **Who can see posts**: *Followers only* (**the default**), *Unlisted* (anyone who visits the profile, but kept out of public timelines), or *Public*. The help text explains that followers-only is only as private as who can follow: with automatic approval anyone can follow and then see posts, so for real privacy it suggests turning on **Approve followers manually**. - **Approve followers manually** (off by default). - Profile fields: display name, bio, avatar. The avatar defaults to a generated cover-art-style tile. - **Followers**: a list of remote followers, with *Remove*. There are *Approve* and *Decline* buttons when manual approval is on. @@ -119,14 +119,23 @@ src/scrobbler/ ## Protocol pieces -### Identity and domains ("decide later", made configurable) +### Identity and domains -Two settings: +**Decided: the UI's domain**, which is the name users already know. Two settings configure it: -- `FEDERATION_DOMAIN`: the handle's domain, as in `@alice@`. -- `FEDERATION_BASE_URL`: where the actor documents live, as in `https:///users/alice`. +- `FEDERATION_DOMAIN`: the handle's domain, as in `@alice@`. This is the UI's host. +- `FEDERATION_BASE_URL`: where the actor documents live, as in `https:///users/alice`. It's also the UI's origin, so actor links, profile links and the handle all show one domain, and the API hostname stays an internal detail. -They can be the same (simplest) or different. If they differ, the handle domain only has to answer `/.well-known/webfinger` (and ideally `/.well-known/host-meta`). Mastodon supports this split: it follows the WebFinger `self` link to the actor. So the UI's nginx can forward just `/.well-known/webfinger` to the API and keep the API on its own hostname. +The UI's nginx **forwards the ActivityPub paths to the API**: + +- `/.well-known/webfinger`, `/.well-known/nodeinfo`, `/.well-known/host-meta` +- `/nodeinfo/`, `/users/`, `/inbox`, `/actor` + +All other paths are the static UI, as now. None of these paths collide with the UI's pages. The proxy passes `Host` and `X-Forwarded-*` through, and the API must trust them (`FORWARDED_ALLOW_IPS`), so that signature checks, which cover the request target and host, see the URL the remote server actually signed. + +A browser opening an actor URL (`/users/alice` without an ActivityPub `Accept` header) gets a redirect from the API to the UI's profile page. + +The two settings are kept separate even though they're usually the same host: the design still allows handles on one domain and actors on another, should that ever be needed. Mastodon supports that split through WebFinger's `self` link. **Both values become permanent once anyone follows**: actor IDs are how other servers identify the account. Renaming a user or moving domains later needs the `Move` flow and `alsoKnownAs` (see [Later](#later-following-and-listen-activities)). So we should: @@ -213,13 +222,20 @@ Serve `/.well-known/nodeinfo` and `/nodeinfo/2.1`, giving the software name `scr ## What gets posted -Posts are `Note` objects inside `Create` activities: Mastodon shows `Note` as a normal status, while other types are squashed into a title and link. The HTML uses only tags Mastodon keeps (`p`, `a`, `br`, `strong`, `em`, `ul`, `ol`, `li`), and all track and artist text is escaped. Each post also gets hashtags (`#NowPlaying`, `#Scrobbler`) as `Hashtag` tags. Visibility follows the user's setting: +Posts are `Note` objects inside `Create` activities: Mastodon shows `Note` as a normal status, while other types are squashed into a title and link. The HTML uses only tags Mastodon keeps (`p`, `a`, `br`, `strong`, `em`, `ul`, `ol`, `li`), and all track and artist text is escaped. Each post also gets hashtags (`#NowPlaying`, `#Scrobbler`) as `Hashtag` tags. Visibility follows the user's setting, which defaults to **followers only**: | Setting | `to` | `cc` | |---|---|---| -| Public | `as:Public` | followers | +| Followers only (default) | followers | *(none)* | | Unlisted | followers | `as:Public` | -| Followers only | followers | *(none)* | +| Public | `as:Public` | followers | + +What followers-only means in practice: + +- Posts go only to followers' servers, which show them only to followers. +- They don't appear in the public outbox or on the profile for anyone else. +- They can't be boosted. +- Changing the setting affects new posts only: already-published posts keep the visibility they were sent with. ### Weekly summary @@ -250,7 +266,7 @@ A new post for every track would flood followers' timelines. There are two optio 1. **Profile field (recommended default)**: a "Now playing: *Reckoner* by Radiohead" field on the profile. The field is updated with an `Update(Person)` when the track changes, at most once every 5 minutes, and cleared when playback stops. Nothing appears in timelines; followers see it on the profile. This option is cheap and quiet. 2. **Posts (opt-in)**: a `Note` when a track starts, at most one every 30 minutes, and only for tracks played for at least 30 seconds. - - It uses the user's visibility setting, with a suggested default of *followers only* for these. + - It uses the user's visibility setting (followers only by default). - Optionally, the previous now-playing post is deleted when the next one goes out, so only one exists at a time. ## Build or buy @@ -356,12 +372,15 @@ This is the "profiles plus friends' listening" option, deliberately left out of Settled so far: **build it ourselves** (the project is BSD-3-Clause), inside **one replaceable module**. -Still open: +Also settled: -1. **Domain.** Handles on the UI's domain (nginx forwards `/.well-known/webfinger`) or on the API's? Both are supported; this is a deployment choice, but it's permanent once people follow. -2. **Default visibility** for summaries and milestones: *Unlisted* (recommended) or *Public*? -3. **Now-playing default** once a user turns it on: the profile field (recommended), or posts? -4. **Does it need an admin?** Phase 2 assumes domain blocks set in configuration. A real moderation UI (reports, per-domain policies) would be a separate piece of work. +- **Domain: the UI's.** Handles and actor URLs are both on the UI host, and the UI's nginx forwards the ActivityPub paths to the API. +- **Visibility: followers only by default**, and each user can change it on their Sharing settings. + +Still open (neither is needed before phase 3): + +1. **Now-playing default** once a user turns it on: the profile field (recommended), or posts? +2. **Does it need an admin?** Phase 2 assumes domain blocks set in configuration. A real moderation UI (reports, per-domain policies) would be a separate piece of work. ## Sources diff --git a/src/scrobbler/federation/README.md b/src/scrobbler/federation/README.md index b2ffd4a..046c0eb 100644 --- a/src/scrobbler/federation/README.md +++ b/src/scrobbler/federation/README.md @@ -27,8 +27,8 @@ or switched off without touching anything else. Design: `docs/design/activitypub | Variable | Meaning | |---|---| | `FEDERATION_ENABLED` | `1` to turn federation on (default off) | -| `FEDERATION_DOMAIN` | Handle domain: users are `@name@`. **Permanent once anyone follows.** | -| `FEDERATION_BASE_URL` | Public `https://` URL of the API, where actors live (`/users/`) | +| `FEDERATION_DOMAIN` | Handle domain, normally the web UI's host: users are `@name@`. **Permanent once anyone follows.** | +| `FEDERATION_BASE_URL` | Public `https://` URL actors live under (`/users/`). Normally the web UI's origin, with the UI's nginx forwarding the ActivityPub paths to the API. | ## Migrations