# ADR-063: Webmention Implementation
## Status
Accepted (v1.6.0; revised v2.6.8, v2.6.9, v2.7.0, v2.7.2, v2.8.0, v2.10.1)
> Reconstructed on 2026-11-03 from code, changelog and related documents;
> the original was lost. The v1.6.0 release was never committed: its code
> was recovered from the production image (`v1.6.0-rc.2`), but its tests,
> this ADR and the `docs/design/v1.6.0/` folder (`background-worker.md`,
> `api-contract.md`, `display-moderation.md`, `rate-limiting.md`) were not
> in the image. This document describes the design as it exists in
> v2.10.1. The numbering of the decisions below is new, except that
> Decision 9 (SSRF protection) keeps the number the code cites.
## Context
Webmention (W3C Recommendation, https://www.w3.org/TR/webmention/) is how
IndieWeb sites tell each other that one page links to another. It is what
makes replies, likes and reposts from other sites (and from Mastodon or
Bluesky via Bridgy) show up on a note, and what tells the sites a note
replies to or links to that it exists.
Both directions involve fetching URLs chosen by someone else, which is
slow, can fail, and can be abused:
- A receiver must fetch the source page to check that it really links to
the target. Doing that inside the request lets anyone make StarPunk
fetch arbitrary URLs synchronously, and ties up a server process.
- A sender must discover each target's endpoint and POST to it, with
retries when the other site is down.
- Any fetch can be pointed (directly, by redirect or by DNS) at private
addresses on the server's own network (SSRF).
- Content from other sites is shown on our pages (XSS).
StarPunk is a single-user site on SQLite, run under gunicorn with several
processes, with no job queue or message broker.
## Decision
### 1. Endpoint and discovery
- `POST /webmention` receives webmentions (form-encoded as the spec
requires; JSON is accepted as an extension);
`GET /webmention` returns a short description of the endpoint.
- HTML responses advertise it with a `Link: ; rel="webmention"`
header (added alongside other Link headers, not replacing them) and a
`` in `base.html`.
- `WEBMENTION_ENABLED` (default true) turns the feature off.
### 2. Receiving is asynchronous
The endpoint does only cheap, local checks and then answers
**202 Accepted**; verification happens later in the background worker.
Synchronous checks, each answered with 400 on failure: `source` and
`target` are well-formed http(s) URLs of at most 2048 characters, they
differ, and the target is a **published note** on this site (host compared
case-insensitively and without default ports; `#fragment` dropped). Since
v2.7.0 mentions of the home page or other pages are refused instead of
being stored and never shown.
The mention is upserted into `webmentions` keyed by `(source_url,
target_url)` with `validation_status = 'pending'`, so a re-sent mention is
re-verified rather than duplicated.
### 3. Background worker
One daemon thread (`WebmentionWorker`) handles both incoming verification
and outgoing sending, using two SQLite tables as queues (`webmentions`,
`webmention_queue`). No external queue is introduced.
- Started by `create_app()` when `WEBMENTION_WORKER_ENABLED` is true (not
in tests). It polls every `WEBMENTION_POLL_INTERVAL` seconds (10),
handles up to `WEBMENTION_BATCH_SIZE` items (5) per pass, and backs off
`WEBMENTION_ERROR_BACKOFF` seconds (60) after an unexpected error.
- Each pass runs inside `app.app_context()` (v2.6.8: without it every
item failed with "Working outside of application context"). The thread
lives for the life of the process; it is not stopped on app-context
teardown (v2.6.8: that stopped it after the first request).
- **Only one process works the queues** (v2.6.9). Every gunicorn process
starts a worker, but a worker only processes after taking a
non-blocking `flock` on `/webmention-worker.lock`. Others retry
each poll, so one takes over if the holder dies.
- On acquiring the lock it resets outgoing items stuck in `sending` for
more than 5 minutes (orphans from a crash).
- `/health?detailed=true` (authenticated) reports the worker's
statistics.
### 4. Verifying a received webmention
Per Recommendation 3.2.2 and 3.2.4:
- The source is fetched with an `Accept` header, following redirects
(Decision 9), and decoded in its declared character set.
- The source must link to the **exact** target URL, by media type: for
HTML any link attribute (resolved against the final URL after
redirects); for JSON any string value; for `text/*` a substring; other
types never match. Fragments are ignored (v2.7.0).
- The source is parsed with mf2py. The responding entry and its author are
found with the authorship algorithm (v2.8.0); the mention type is one of
`reply`, `like`, `repost`, `bookmark` or `mention`. The source's own
`u-url` and `dt-published` are stored (`source_post_url`,
`source_published`, migration 014) so the response links to and is
dated by the original post.
- If the source is gone (410, other 4xx) or no longer links, the mention
is removed from display. If it can't be reached right now (network
error, 5xx, temporary DNS failure), a previously valid mention stays up
(v2.7.0, v2.10.1).
- A webmention from one of the note's own crossposts (e.g. its GoToSocial
copy returned through Bridgy) is ignored (v2.8.0).
### 5. Moderation and display
- Valid mentions are approved automatically when `WEBMENTION_AUTO_APPROVE`
is true (the default), otherwise they wait in `pending`. The admin can
approve, hide or delete them at `/admin/webmentions` (single and bulk
actions).
- A re-sent mention keeps its moderation: hidden stays hidden, approved
stays approved (v2.7.0).
- Notes show only mentions that are both `valid` and `approved`, grouped
by type. Replies are marked up as `u-comment h-cite`; likes, reposts,
bookmarks and mentions as `u-like`, `u-repost`, `u-bookmark`,
`u-mention` h-cites, each with the author's h-card (v2.8.0).
### 6. Sending
- After a note is created, edited, deleted or restored, links are
extracted from its **rendered HTML**: `` only, excluding links
inside `