Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fa551de197 | ||
|
|
b2043286fa | ||
|
|
409b8b31b4 | ||
|
|
b520b8b4d3 | ||
|
|
720a1e6d6f | ||
|
|
c89c2cd5ca | ||
|
|
ab6ff60f5c | ||
|
|
57682165a4 | ||
|
|
8c51b52ed6 | ||
|
|
42c77e34b7 | ||
|
|
438465592c | ||
|
|
c8a7ef309f | ||
|
|
0640730416 | ||
|
|
51f711ea4a | ||
|
|
6a76ee26af | ||
|
|
43ec7cb8be | ||
|
|
4aaa7a8122 | ||
|
|
0cfc9965df | ||
|
|
106495d114 | ||
|
|
3c1e109358 | ||
|
|
d43361d505 | ||
|
|
f924413934 | ||
|
|
4b2ae4c7fc | ||
|
|
d3148063e3 | ||
|
|
39c8259f3f | ||
|
|
1ce88b38a5 | ||
|
|
4ffe36f9d3 | ||
|
|
a551770c76 | ||
|
|
d0bfc80c2e | ||
|
|
d16463ef8a | ||
|
|
4b56f7cd4b | ||
|
|
2474628646 | ||
|
|
0fe341161e | ||
|
|
a62077ab30 | ||
|
|
fade75fd2d | ||
|
|
0dd59fbc18 | ||
|
|
11e09a0d02 | ||
|
|
565adaeecf | ||
|
|
53d10ed271 | ||
|
|
e8c1b03980 | ||
|
|
0d3a5cd519 | ||
|
|
88088e10e1 | ||
|
|
391bdb9db3 | ||
|
|
c51cf726b0 | ||
|
|
2616ef5060 | ||
|
|
0ae16bd1fc | ||
|
|
cc09098741 | ||
|
|
16925663a3 | ||
|
|
e92c27a84c | ||
|
|
ed63e70c46 | ||
|
|
57b02ad6c7 | ||
|
|
94e8c672df | ||
|
|
011f7cc87d | ||
|
|
f46130569f | ||
|
|
b4e6cf3cc3 | ||
|
|
2d0568d9c2 | ||
|
|
f117913797 | ||
|
|
b84cf23e1a | ||
|
|
d3fa5b7a2a |
No files matched your search
+57
-5
@@ -22,11 +22,24 @@ SITE_DESCRIPTION=My personal IndieWeb site
|
||||
# AUTHENTICATION
|
||||
# =============================================================================
|
||||
|
||||
# Your IndieWeb identity URL (REQUIRED)
|
||||
# This is YOUR personal website URL that you authenticate with
|
||||
# Example: https://yourname.com or https://github.com/yourname
|
||||
# Your identity URL (REQUIRED)
|
||||
# Your personal website. Used as the author h-card source and as the "me"
|
||||
# value in Micropub tokens. It should link to this site's IndieAuth metadata
|
||||
# (see docs/decisions/ADR-064) so Micropub apps can find StarPunk.
|
||||
ADMIN_ME=https://your-website.com
|
||||
|
||||
# Admin login via OpenID Connect (REQUIRED in production)
|
||||
# Register StarPunk as a confidential client with your provider (e.g. Pocket ID)
|
||||
# using the callback URL: <SITE_URL>/auth/callback
|
||||
OIDC_ISSUER=https://id.example.com
|
||||
OIDC_CLIENT_ID=
|
||||
OIDC_CLIENT_SECRET=
|
||||
# The provider's subject ("sub") of the one account allowed to log in.
|
||||
# Pocket ID: the user's ID shown in the admin user list.
|
||||
OIDC_ADMIN_SUB=
|
||||
# Name shown on the login button (default: "single sign-on")
|
||||
OIDC_PROVIDER_NAME=Pocket ID
|
||||
|
||||
# Session secret key (REQUIRED - GENERATE A RANDOM VALUE)
|
||||
# Generate with: python3 -c "import secrets; print(secrets.token_hex(32))"
|
||||
SESSION_SECRET=REPLACE_WITH_RANDOM_SECRET
|
||||
@@ -34,8 +47,29 @@ SESSION_SECRET=REPLACE_WITH_RANDOM_SECRET
|
||||
# Session lifetime in days (default: 30)
|
||||
SESSION_LIFETIME=30
|
||||
|
||||
# IndieLogin service URL (usually don't change this)
|
||||
INDIELOGIN_URL=https://indielogin.com
|
||||
# Lifetime of Micropub access tokens StarPunk issues, in days (default: 90)
|
||||
INDIEAUTH_TOKEN_LIFETIME_DAYS=90
|
||||
|
||||
# =============================================================================
|
||||
# CROSSPOSTING (optional, v2.1.0)
|
||||
# =============================================================================
|
||||
|
||||
# GoToSocial or Mastodon account to copy new notes to. Create an app token
|
||||
# with scopes: write:statuses write:media
|
||||
GOTOSOCIAL_URL=
|
||||
GOTOSOCIAL_ACCESS_TOKEN=
|
||||
# Name shown in the editor and Micropub apps (default: GoToSocial)
|
||||
GOTOSOCIAL_NAME=GoToSocial
|
||||
# public, unlisted, private (default: public)
|
||||
GOTOSOCIAL_VISIBILITY=public
|
||||
|
||||
# Threads account (v2.6.0). A long-lived user token with threads_basic,
|
||||
# threads_content_publish and threads_delete; StarPunk refreshes it daily and
|
||||
# keeps the current one in DATA_PATH/threads_token.json.
|
||||
# How to get one: docs/design/v2.6.0/2026-10-31-threads-crossposting.md
|
||||
THREADS_ACCESS_TOKEN=
|
||||
# Your Threads username (for the Micropub syndicate-to uid)
|
||||
THREADS_USERNAME=
|
||||
|
||||
# =============================================================================
|
||||
# DATA STORAGE
|
||||
@@ -91,6 +125,24 @@ WORKER_TIMEOUT=30
|
||||
# Max requests per worker before restart (prevents memory leaks)
|
||||
MAX_REQUESTS=1000
|
||||
|
||||
# Behind a reverse proxy (Caddy, nginx): take the client address from
|
||||
# X-Forwarded-For. Without it every visitor shares the proxy's address, so one
|
||||
# busy client can rate-limit everyone on /webmention and /websub.
|
||||
# Only enable when the proxy sets X-Forwarded-For itself (Caddy does).
|
||||
TRUSTED_PROXY=true
|
||||
|
||||
# =============================================================================
|
||||
# WEBSUB (v2.10.0)
|
||||
# =============================================================================
|
||||
|
||||
# Built-in hub at /websub for this site's feeds. Set WEBSUB_HUB_URL to use
|
||||
# another hub instead (StarPunk then pings it on changes).
|
||||
WEBSUB_ENABLED=true
|
||||
# WEBSUB_HUB_URL=https://hub.example.com/
|
||||
# WEBSUB_MAX_SUBSCRIPTIONS=1000
|
||||
# Per callback server (resolved address)
|
||||
# WEBSUB_MAX_PER_HOST=25
|
||||
|
||||
# =============================================================================
|
||||
# DEVELOPMENT OPTIONS
|
||||
# =============================================================================
|
||||
|
||||
@@ -1,58 +1,46 @@
|
||||
# Gitea Actions workflow for StarPunk
|
||||
# Builds and pushes container images on version tags
|
||||
|
||||
# Runs the tests, then builds and pushes container images on version tags.
|
||||
#
|
||||
# The runner has no Docker daemon (that would give workflows root on the
|
||||
# server). Images are built by the rootless BuildKit container next to it,
|
||||
# reached through its socket.
|
||||
name: Build Container
|
||||
|
||||
on:
|
||||
# Trigger on version tags
|
||||
push:
|
||||
tags:
|
||||
- 'v[0-9]+.[0-9]+.[0-9]+'
|
||||
# Allow manual trigger from Gitea UI
|
||||
workflow_dispatch:
|
||||
|
||||
env:
|
||||
REGISTRY: git.thesatelliteoflove.com
|
||||
BUILDKIT: unix:///run/buildkit/buildkit/buildkitd.sock
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: docker
|
||||
steps:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
if command -v apk > /dev/null; then
|
||||
apk add --no-cache nodejs npm docker git
|
||||
elif command -v apt-get > /dev/null; then
|
||||
apt-get update && apt-get install -y nodejs npm docker.io git
|
||||
elif command -v yum > /dev/null; then
|
||||
yum install -y nodejs npm docker git
|
||||
fi
|
||||
|
||||
run: apk add --no-cache nodejs git docker-cli docker-cli-buildx
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Extract registry URL
|
||||
id: registry
|
||||
run: |
|
||||
# Extract hostname from server URL (remove protocol)
|
||||
REGISTRY_URL=$(echo "${{ github.server_url }}" | sed 's|https://||' | sed 's|http://||')
|
||||
echo "url=${REGISTRY_URL}" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Login to Gitea Container Registry
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ${{ steps.registry.outputs.url }}
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
# The automatic Actions token can't log in to the container registry, so
|
||||
# this uses a personal access token with only the write:package scope,
|
||||
# stored as the repository secret REGISTRY_TOKEN.
|
||||
- name: Log in to the Gitea registry
|
||||
run: echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "$REGISTRY" -u "${{ github.repository_owner }}" --password-stdin
|
||||
- name: Use the BuildKit socket
|
||||
run: docker buildx create --name ci --driver remote "$BUILDKIT" --use
|
||||
# Builds the Containerfile's test stage, which runs pytest; a failure
|
||||
# stops the workflow before anything is pushed
|
||||
- name: Run tests
|
||||
run: docker buildx build --target test --progress plain -f Containerfile .
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
file: ./Containerfile
|
||||
push: true
|
||||
tags: |
|
||||
${{ steps.registry.outputs.url }}/${{ github.repository }}:${{ github.ref_name }}
|
||||
${{ steps.registry.outputs.url }}/${{ github.repository }}:latest
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
run: |
|
||||
image="$REGISTRY/$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')"
|
||||
docker buildx build --push -f Containerfile \
|
||||
-t "$image:${{ github.ref_name }}" -t "$image:latest" .
|
||||
- name: Clean up
|
||||
if: always()
|
||||
run: |
|
||||
docker buildx rm ci || true
|
||||
docker logout "$REGISTRY" || true
|
||||
+392
@@ -7,6 +7,398 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [2.10.1] - 2026-11-03
|
||||
|
||||
WebSub fixes from an adversarial review of v2.10.0 against the [W3C Recommendation](https://www.w3.org/TR/websub/) (details in ADR-071, Amendments).
|
||||
|
||||
### Security
|
||||
|
||||
- One subscription request could stop the hub for good: a callback that answered verification with a large page, or a callback URL with a control character, crashed the worker on every pass. Each queued item is now handled on its own
|
||||
- A subscriber sending its response one byte at a time could hold the hub for hours. Every request the hub (and webmention fetching) makes now has a hard time limit
|
||||
- The subscription limits only counted verified subscriptions and could be dodged with subdomains. They now count pending requests too, per callback server address, and at most 200 requests can wait for verification
|
||||
- Callbacks must be printable URLs of reasonable length, not on this site, and on ports 80, 443 or 1024 and up
|
||||
|
||||
### Fixed
|
||||
|
||||
- New posts with photos were sent to subscribers (and cached in the feeds) without their photos, and photo-only edits sent the old feed. Notes with photos are announced once the photos are attached, and the feed cache follows photo and caption changes
|
||||
- A subscriber whose deliveries failed for three days was removed; WebSub says to keep it until its lease ends. Deliveries are retried and given up, the subscription stays
|
||||
- A temporary DNS failure or an error rendering the feed lost the delivery instead of retrying it. A feed that no longer exists now ends its subscriptions with a `hub.mode=denied` notice
|
||||
- Verification sent our normalised topic (`/tag/python`) instead of the one requested (`/tag/Python`), so strict subscribers refused it. Percent-encoded topics (`/%66eed.atom`) are now understood, the Content-Type check ignores case, unsubscribe requests ignore `hub.lease_seconds`, and an empty `hub.secret` is refused instead of turning signatures off
|
||||
- A newer subscription request arriving while an older one was being verified was lost (migration 017)
|
||||
- Unsubscribing works while `WEBSUB_HUB_URL` points to another hub
|
||||
- Feed ETags differed between server processes, so conditional requests rarely matched; `/feed` now sends `Vary: Accept`
|
||||
- A like no longer causes pushes of the home page and feeds, which don't show likes; each feed is rendered once per delivery round, not once per subscriber
|
||||
- Internationalised callback domains can be verified
|
||||
|
||||
### Changed
|
||||
|
||||
- `.env.example` documents `TRUSTED_PROXY` (needed behind a reverse proxy for per-client rate limits) and the WebSub settings
|
||||
|
||||
## [2.10.0] - 2026-11-03
|
||||
|
||||
### Added
|
||||
|
||||
- **WebSub** ([ADR-071](docs/decisions/ADR-071-websub.md)): readers that support it get new posts, edits and deletions within seconds instead of polling. StarPunk runs its own hub at `/websub` for its own feeds: the home page, `/feed.atom`, `/feed.rss`, `/feed.json`, `/likes` and each tag page. Each advertises the hub and itself (`Link` headers, and links inside the page or feed). Subscriptions are verified with a challenge, leases expire, deliveries carry the full feed, signed with `X-Hub-Signature` when the subscriber gave a secret, and failures are retried for about nine hours. Requests to subscribers go through the same private-address protections as webmentions, and there are limits per subscriber host and in total. Set `WEBSUB_HUB_URL` to use another hub instead; `WEBSUB_ENABLED=false` turns it off
|
||||
- The admin dashboard shows WebSub subscribers per feed
|
||||
|
||||
### Fixed
|
||||
|
||||
- Feeds could be up to five minutes out of date after posting, editing or deleting, and each server process cached its own copy. Any change to a public note now refreshes the feeds everywhere at once
|
||||
- The memory monitor stopped after the first request (the same bug the webmention worker had before v2.6.8)
|
||||
|
||||
### Changed
|
||||
|
||||
- The RSS feed calls itself `/feed.rss` instead of the older `/feed.xml`. Both addresses still work
|
||||
|
||||
## [2.9.0] - 2026-11-03
|
||||
|
||||
Micropub review against the [W3C Recommendation](https://www.w3.org/TR/micropub/).
|
||||
|
||||
### Changed
|
||||
|
||||
- **Micropub posts are only crossposted to the targets the client asks for.** If a client doesn't send `mp-syndicate-to`, nothing is crossposted (before, everything was). Posts made in the admin editor are unchanged
|
||||
- Posts sent with `visibility` private or unlisted are saved as drafts instead of being published (StarPunk has no private posts)
|
||||
- A token with only the `draft` scope can create drafts, and nothing else
|
||||
- `q=source` and `q=category` need a token that can post (`create`, `update`, `delete` or `draft`); before, any token could list every post, drafts included
|
||||
- Sending the access token in both the header and the body is refused (400)
|
||||
|
||||
### Added
|
||||
|
||||
- Bookmarks: Micropub `bookmark-of` is stored and shown ("🔖 Bookmarked …", `u-bookmark-of`), returned by `q=source`, listed in `q=config`, and crossposts lead with the bookmarked link (migration 015)
|
||||
- A post with only a `summary` uses it as the text; a post with only a `name` is a titled post with no body
|
||||
|
||||
### Fixed
|
||||
|
||||
- Quill and other JSON clients put `mp-syndicate-to` and `mp-slug` inside `properties`; they were ignored, so posts crossposted everywhere and slugs were not used
|
||||
- HTML in posts is sanitized: scripts, event handlers, iframes and `javascript:` links are removed when notes are shown. Formatting, images, code highlighting, footnotes and microformats are kept
|
||||
- A photo that can't be downloaded or found made the post without it while reporting success. Create and update now fail with 400 and change nothing
|
||||
- Remote photos are fetched the same way as webmentions: every redirect is checked for private addresses and the connection goes to the checked address
|
||||
- JSON values that weren't lists were read a character at a time (`"category": "solo"` became tags s, o, l). Single values are now treated as a one-item list; `delete` values in updates must be lists
|
||||
- Posts written as HTML are returned by `q=source` as `{"html": …}`, as the spec requires; empty `content` is left out
|
||||
- A client-supplied `published` time is stored as UTC like every other post; a date that can't be read is refused instead of silently replaced with now
|
||||
- Malformed JSON gets 400 instead of 500, and error messages no longer include Python internals
|
||||
- `video` and `audio` (not supported) are refused with 400 instead of being dropped; person tags in `category` are ignored instead of failing the post
|
||||
- An update with no `replace`, `add` or `delete` is refused
|
||||
- `Authorization: bearer …` is accepted (the scheme is case-insensitive), and 401 responses include `WWW-Authenticate`
|
||||
|
||||
## [2.8.1] - 2026-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Starting the server after an upgrade with a database migration could fail: several server processes applied the same migration at once and one stopped with "duplicate column name", taking the server down until it restarted (this happened deploying v2.8.0, for about two seconds). The migration lock was released right after it was taken, and again before each migration was recorded. Migrations now run entirely inside the lock
|
||||
|
||||
## [2.8.0] - 2026-11-03
|
||||
|
||||
Microformats2 review: the markup now matches the [microformats2](https://microformats.org/wiki/microformats2) vocabularies, so IndieWeb readers and tools see what the page shows.
|
||||
|
||||
### Fixed
|
||||
|
||||
- Replies to a note weren't marked up as its comments, so readers and threading tools saw none. They are now `u-comment h-cite`s with the replier's h-card (name, link, photo) and the reply's HTML
|
||||
- Likes, reposts, bookmarks and mentions on a note had no microformats at all. Each is now a `u-like`, `u-repost`, `u-bookmark` or `u-mention` h-cite with its author, including likes past the eight faces shown
|
||||
- Responses link to and are dated by the original post (its own URL and published time) instead of the address that sent the webmention and when it arrived. New database columns hold these (migration 014)
|
||||
- Post times had no time zone (and production showed microseconds), so readers could shift them by hours. Times on pages and in Micropub `q=source` are now UTC with a `Z`, matching the Atom feed
|
||||
- A note starting with a hashtag (`#indieweb is great…`) was treated as a titled article: its text became a heading and the body was empty, and a note starting with a bare `#` line got its slug as a title. Only a Markdown heading (`#` then a space) makes a title now
|
||||
- A webmention from one of a note's own crossposts (for example your GoToSocial copy, sent back through Bridgy) showed up as a response on the note. It is now ignored
|
||||
- Micropub and IndieAuth discovery links disappeared from pages when webmentions were turned off
|
||||
- A new site had no `rel="me"` link to its author, so readers couldn't tell whose site it was; pages now always link `rel="me"` and `rel="author"` to the author's home page
|
||||
- Finding the author's profile could pick someone else's h-card from their home page. It now follows the [representative h-card](https://microformats.org/wiki/representative-h-card-parsing) rules
|
||||
- A webmention's author is found by the [authorship algorithm](https://indieweb.org/authorship-spec), from the entry on the source page that actually responds to the note: an author given as a name or link, and the author of a feed the entry is in, now count
|
||||
|
||||
### Added
|
||||
|
||||
- Tag pages and the likes page have a feed URL and author, like the home page
|
||||
- Note previews have `u-uid`
|
||||
|
||||
## [2.7.2] - 2026-11-03
|
||||
|
||||
### Added
|
||||
|
||||
- When the replies, likes and other responses shown on a note change (a webmention arrives, is updated or removed, or you approve, hide or delete one), the note's webmentions are sent again, as the Webmention spec recommends, so the sites it links to can pick up the conversation. A response that is checked again without changing sends nothing
|
||||
|
||||
### Security
|
||||
|
||||
- Webmention fetches and sends connect to the exact address that passed the private-address check. Before, the hostname was looked up a second time when connecting, so a DNS server that changed its answer could still point StarPunk at a private address. TLS certificates are still checked against the hostname. IPv4 addresses are preferred when a host has both
|
||||
|
||||
## [2.7.1] - 2026-11-03
|
||||
|
||||
### Changed
|
||||
|
||||
- Releases run the full test suite first: CI builds the Containerfile's new `test` stage (same Python image as production) and stops before pushing an image if any test fails
|
||||
|
||||
### Fixed
|
||||
|
||||
- Eight tests that had been failing: the image validation tests still expected bytes (validation has returned an image object since v1.5.0), and a migration test started the app in production mode
|
||||
|
||||
## [2.7.0] - 2026-11-03
|
||||
|
||||
Webmention now follows the [W3C Recommendation](https://www.w3.org/TR/webmention/) throughout.
|
||||
|
||||
### Changed
|
||||
|
||||
- Webmentions are only accepted for published notes. Mentions of the home page or other pages were stored but never shown; they're now refused with 400
|
||||
- Deleted notes answer 410 Gone with a "This post has been deleted" page instead of 404
|
||||
- A received mention that is sent again keeps its moderation: one you hid stays hidden, one you approved stays approved
|
||||
|
||||
### Fixed
|
||||
|
||||
- Sending: endpoint discovery now handles `rel` values with several words (`rel="webmention other"`), any capitalisation, `rel` after other Link parameters, and several Link headers, and takes the first `<link>` or `<a>` in page order
|
||||
- Sending: the endpoint is looked up again every time (the 1–24 hour cache broke the spec's rule for updated posts)
|
||||
- Sending: editing a note notifies every page it mentioned before, including an old reply target and links the edit removed. Deleting or restoring a note notifies them too, so they can remove or restore the mention
|
||||
- Sending: a redirect from the endpoint is no longer treated as success (the POST was turned into a GET); "429 Too Many Requests" is retried
|
||||
- Receiving: the source must link to the exact target URL (in a link, image, or other HTML attribute, any JSON value, or plain text). A longer URL that starts with the target, or the target mentioned in plain text or an HTML comment, no longer counts
|
||||
- Receiving: relative links in the source are resolved, using the URL after redirects
|
||||
- Receiving: the source is fetched with an `Accept` header and decoded in its declared character set
|
||||
- Receiving: if the source can't be reached when a mention is sent again, the existing mention stays up instead of disappearing
|
||||
- Receiving: the target's host is compared without regard to case or a default port, and `#fragment`s are dropped, so one mention isn't stored twice
|
||||
- Mention snippets cut at 500 characters no longer leave HTML tags open, and the "Mentions" list no longer shows raw HTML tags
|
||||
- The `Link: rel="webmention"` header is added alongside other Link headers instead of replacing them
|
||||
- Links at the top level of a code block were still sent webmentions
|
||||
|
||||
### Security
|
||||
|
||||
- Fetches for webmentions check every redirect, not just the first URL, so a public page can't redirect StarPunk to a private address. Every address a hostname resolves to (IPv4 and IPv6) must be public; carrier-grade NAT, `0.0.0.0/8`, multicast, reserved and IPv4-mapped addresses are blocked too. Each fetch has a total time limit and a 1 MB size limit, including endpoint discovery
|
||||
|
||||
## [2.6.9] - 2026-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Received webmentions failed when the sender's profile photo had alt text (Bridgy, Mastodon and most IndieWeb sites do this). The photo is now stored as its URL
|
||||
- Outgoing webmentions were never sent: endpoint discovery crashed on every target that had an endpoint, and the target was then marked as having none. Discovery now works, and network errors during discovery are retried instead of giving up
|
||||
- Each server process ran its own webmention worker, so a webmention could be processed or sent more than once. Only one process handles the queues at a time now; if it stops, another takes over
|
||||
|
||||
### Security
|
||||
|
||||
- A webmention sender could set its author link to a `javascript:` URL, which was shown as a clickable link on the note page. Author links and photos are now only kept if they are http or https URLs
|
||||
|
||||
## [2.6.8] - 2026-11-03
|
||||
|
||||
### Fixed
|
||||
|
||||
- Webmentions were never processed. The background worker checked URLs against this site's settings without the app loaded, so every incoming webmention was marked "error" ("Working outside of application context") and every outgoing one kept failing and retrying. The worker now runs with the app loaded. A received webmention showed up in the admin list but never on the note
|
||||
- The webmention worker also stopped as soon as any request finished, so after startup it often wasn't running at all. It now runs for the life of the server process
|
||||
|
||||
## [2.6.7] - 2026-11-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- Admin pages were misaligned: the notes table ran past the right edge of the header and admin menu, because every page sat in the 40rem reading column and the table couldn't shrink to fit it. Admin pages now use a 60rem frame for the header, menu, content and footer alike, long post previews wrap, and on narrow screens tables scroll sideways instead of spilling out
|
||||
|
||||
## [2.6.6] - 2026-11-02
|
||||
|
||||
### Fixed
|
||||
|
||||
- Crossposts left out photos that were embedded in the post text instead of attached. Some Micropub clients (OutPost, for one) upload a photo to the media endpoint and then put it in `content` as an image rather than sending it as `photo`; both are valid Micropub. Images in the text that are in this site's media library are now sent to GoToSocial, Bluesky and Threads like attached photos, with their alt text, after any attached ones and within each target's photo limit. GoToSocial's text no longer includes the image Markdown. Images hosted on other sites are still left out
|
||||
|
||||
## [2.6.5] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- A Micropub post that failed partway (for example, the same photo listed twice) answered "Failed to create post" but stayed published without its photos, so a client retrying would post it twice. Photos are now looked up before the post is made, and a post that fails after that is removed again
|
||||
- After such a failure, every request failed with "database is locked" for about 10 seconds: database connections now roll back unfinished writes when they're returned to the pool
|
||||
- The same photo listed twice in one post is shown once instead of failing the post
|
||||
- Replies didn't read as replies to IndieWeb readers and webmention receivers: on note pages `u-in-reply-to` sat inside an unrelated `h-cite`, and the home page didn't mark replies at all, so [Post Type Discovery](https://www.w3.org/TR/post-type-discovery/) saw notes (or photo posts). The reply context is now the entry's `u-in-reply-to h-cite`, and home page entries show "↩ In reply to"
|
||||
- Photos without alt text used "Image" as their description, which IndieWeb readers passed on as if you'd written it. They now use the post's text (up to 150 characters), and fall back to "Image" only when the post has no text
|
||||
- Home page entries always have an `e-content`, so a photo post without text never gets an implied name (which readers would show as its caption)
|
||||
|
||||
### Changed
|
||||
|
||||
- Photo posts no longer need text. Micropub doesn't require `content`, and `q=config` already offered a Photo post type with only `photo`. A Micropub update may also clear the text of a post that keeps its photos. Crossposts of a photo post without text start with its link, and its page is titled "Photo"
|
||||
|
||||
## [2.6.4] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- Tags keep the order you entered them in. The first tag is the note's main tag: it's the Threads topic, and Bluesky keeps the first tags when space runs out. Before, both used the alphabetically first tag. Public pages still list tags alphabetically
|
||||
- Opening a note in the editor or updating it via Micropub reordered its tags alphabetically on save; the editor and Micropub `q=source` now show, and keep, the entered order
|
||||
|
||||
## [2.6.3] - 2026-11-01
|
||||
|
||||
### Changed
|
||||
|
||||
- **A note's own link gets the preview card** (ADR-070). When a note links somewhere (say, an article), the crosspost's card shows that page instead of the note. Bluesky cards are built from the page's title, description and image; Threads previews it from the text. Notes without links keep their own card
|
||||
|
||||
### Fixed
|
||||
|
||||
- Markdown links (`[text](url)`) lost their URL on Bluesky and Threads. On Bluesky the link text is now clickable; on Threads it's written "text (url)"
|
||||
- Notes with many links failed on Threads (Meta allows 5 per post); extra links now keep just their text
|
||||
|
||||
## [2.6.2] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- New notes failed their first Threads crosspost with "Invalid Link Attachment" (Meta rejects an explicit link preview for a page it hasn't fetched yet) and only went out on the retry a minute later. StarPunk no longer sends the explicit link preview; Threads still shows one, built from the note's link in the text
|
||||
|
||||
## [2.6.1] - 2026-11-01
|
||||
|
||||
### Fixed
|
||||
|
||||
- Threads crossposts gave up after Meta's publish step returned a one-off `HTTP 400 Fatal`; publish errors are now retried (a retry checks for an existing post first, so nothing is posted twice). Only a bad token or missing permission stops retries
|
||||
- Threads errors now include Meta's error code, subcode, user message and trace id, so the editor shows why a post failed
|
||||
- Wait a moment before the first readiness check on a Threads post, since Meta can report it ready too early
|
||||
|
||||
## [2.6.0] - 2026-11-01
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to Threads** (ADR-069): set `THREADS_ACCESS_TOKEN` (and optionally `THREADS_USERNAME`) to add a Threads target. Text posts get a link preview; 1 image posts as an image, 2–4 as a carousel, with alt text; the note's first tag becomes the post's topic. Shortened to 500 characters. Edits don't follow (the API can't edit); unpublish and delete remove the post. Likes aren't crossposted to Threads; reposts post "🔁 title url" (with the comment, if any)
|
||||
- The Threads token (60-day lifetime) is refreshed automatically once a day and kept in `data/threads_token.json`, so it survives restarts; setting a new token in the config replaces it. Setup steps: `docs/design/v2.6.0/`
|
||||
|
||||
## [2.5.0] - 2026-11-01
|
||||
|
||||
### Added
|
||||
|
||||
- **Likes and reposts** via Micropub `like-of` / `repost-of` (ADR-068); a comment is optional
|
||||
- Likes are kept off the home page and feeds and listed on a new `/likes` page (linked in the header)
|
||||
- Reposts appear in the stream and feeds with a "🔁 Reposted" line
|
||||
- The liked/reposted post's title and author are fetched and shown; it is sent a webmention
|
||||
- GoToSocial: likes/reposts of fediverse posts become native favourites/boosts; reposts of other pages (or with a comment) post "🔁 title url"
|
||||
- Bluesky: likes/reposts of Bluesky posts become native likes/reposts; a repost with a comment is a quote post; reposts of other pages post with a link card
|
||||
- Likes of ordinary web pages are not crossposted; deleting a like/repost undoes it everywhere
|
||||
- Migration 013: `notes.like_of`, `notes.repost_of`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Outgoing webmentions named posts `https://site//note/<slug>` (double slash)
|
||||
|
||||
### Notes
|
||||
|
||||
- The GoToSocial token needs the extra scopes `read:search` and `write:favourites`
|
||||
|
||||
## [2.4.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Micropub update** (`action=update`, JSON): replace/add/delete for `content`, `name`, `category`, `photo`, `in-reply-to`, `post-status`; edits follow to crossposts (ADR-067)
|
||||
- **Micropub delete and undelete**: soft delete, restorable; crossposts are removed and re-posted
|
||||
- **Drafts** via `post-status: draft`; **replies** via `in-reply-to`
|
||||
- Photos uploaded in a multipart create request; photos hosted elsewhere are downloaded (public addresses only, 20 MB cap)
|
||||
- `q=source` without `url` lists recent posts (drafts included, `limit`/`offset`); `properties[]` filter; returns photos, syndication links, reply and post status
|
||||
- `q=category` (tag suggestions, optional `filter`); `q=config` lists supported queries and post types
|
||||
- Media endpoint accepts the `media` scope
|
||||
|
||||
### Fixed
|
||||
|
||||
- Media endpoint was advertised at `/micropub/media` but served at `/media` (404 for clients); now both work
|
||||
- Unsupported Micropub types (e.g. `h=event`) were silently stored as notes; now rejected
|
||||
|
||||
## [2.3.3] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Micropub `name` was ignored, so titles sent by Micropub clients never appeared. It is now stored as the note's `# Heading` first line, which is how StarPunk titles work
|
||||
- Micropub `q=source` returned `/notes/<slug>` links (a 404); it now returns the real permalink `/note/<slug>`
|
||||
- Micropub `q=source` reported every note's first line as its `name`; it now returns `name` only for titled notes, with the heading left out of `content`, matching what was posted
|
||||
- Titled notes showed their title twice: as the page heading and again at the top of the body (and of the home-page summary)
|
||||
|
||||
## [2.3.2] - 2026-10-31
|
||||
|
||||
### Changed
|
||||
|
||||
- Header link reads "Admin" (was "Dashboard") when logged in
|
||||
|
||||
### Fixed
|
||||
|
||||
- Public pages never knew you were logged in (only `/admin` routes checked the session), so the header always said "Log in"
|
||||
|
||||
## [2.3.1] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- "Log in" is back in the header nav (2.3.0 had moved it to the footer, where it was easy to miss)
|
||||
|
||||
## [2.3.0] - 2026-10-31
|
||||
|
||||
### Changed
|
||||
|
||||
- **Public pages restyled**: compact header with the site name from `SITE_NAME`, serif reading font for notes, one-line metadata (date · #tags · Also on …), dark mode via `prefers-color-scheme`, footer pinned to the bottom, "Log in" moved to the footer
|
||||
- Home and tag pages show short notes in full; titled notes show a summary and "Read more"
|
||||
- Author h-card kept for parsers but visually hidden on this single-author site
|
||||
- Page titles use the note text and `SITE_NAME` instead of the slug and "StarPunk"
|
||||
|
||||
### Fixed
|
||||
|
||||
- Every note's metadata was styled as the page footer (centered, ruled, padded)
|
||||
- Previews cut the note's HTML at 300 characters, which could break markup
|
||||
- Bare URLs in notes are now links
|
||||
|
||||
## [2.2.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to Bluesky**, ADR-066
|
||||
- Plain-text note (Markdown stripped) with clickable link and hashtag facets, shortened to 300 characters with the permalink kept
|
||||
- Images (up to 4, shrunk under Bluesky's 1 MB limit) with alt text; notes without images get a link card
|
||||
- Unpublishing, unticking and deleting remove the Bluesky post; edits don't follow (Bluesky has no edits)
|
||||
- Logs in with an app password: `BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD` (optional `BLUESKY_SERVICE`, default `https://bsky.social`)
|
||||
- Shown in the editor and Micropub `syndicate-to` next to GoToSocial
|
||||
|
||||
## [2.1.0] - 2026-10-31
|
||||
|
||||
### Added
|
||||
|
||||
- **Crossposting to GoToSocial** (or any Mastodon-API server), ADR-065
|
||||
- Full note as Markdown with images (alt text from captions), permalink and hashtags
|
||||
- Edits, unpublishing and deletes follow the note
|
||||
- On by default for new notes; untick in the editor, or send `mp-syndicate-to` from Micropub apps
|
||||
- Micropub `q=syndicate-to` and `q=config` list the target
|
||||
- Editor shows crosspost status, errors and a retry button; note pages link the copy with `u-syndication`
|
||||
- Configure with `GOTOSOCIAL_URL` and `GOTOSOCIAL_ACCESS_TOKEN` (scopes `write:statuses write:media`)
|
||||
- Migration 012: `syndications`
|
||||
|
||||
### Fixed
|
||||
|
||||
- Micropub `Location` header pointed at `/notes/<slug>` (404); now the real permalink
|
||||
- "Delete Note" on the edit page submitted the edit form (nested forms)
|
||||
- Search results linked to `/notes/<slug>` (404); now `/note/<slug>`
|
||||
|
||||
## [2.0.2] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Container builds in Gitea Actions log in to the registry with the
|
||||
`REGISTRY_TOKEN` secret (a `write:package` token); the automatic Actions
|
||||
token is refused by the registry. No application changes.
|
||||
|
||||
## [2.0.1] - 2026-10-31
|
||||
|
||||
### Fixed
|
||||
|
||||
- Page links built from the request said `http://` behind an HTTPS reverse proxy
|
||||
(RSS/OPML `<link>`s and microformats `u-url`/`u-uid` permalinks). All absolute
|
||||
URLs in templates now come from `SITE_URL` via `absolute_url_for()`.
|
||||
- Container builds in Gitea Actions: build through the server's rootless BuildKit
|
||||
and log in to the public registry name (the old workflow never succeeded).
|
||||
|
||||
## [2.0.0] - 2026-10-31
|
||||
|
||||
### Changed (breaking)
|
||||
|
||||
- **Admin login uses OpenID Connect** instead of IndieAuth (ADR-064)
|
||||
- Works with Pocket ID or any OIDC provider; authorization code flow with PKCE and nonce
|
||||
- Only the account whose subject matches `OIDC_ADMIN_SUB` can log in
|
||||
- New required settings: `OIDC_ISSUER`, `OIDC_CLIENT_ID`, `OIDC_CLIENT_SECRET`, `OIDC_ADMIN_SUB`
|
||||
- `INDIELOGIN_URL` and `TOKEN_ENDPOINT` are ignored
|
||||
- **Micropub tokens are issued by StarPunk**, not an external IndieAuth server
|
||||
- Your `ADMIN_ME` page must point Micropub apps at StarPunk's
|
||||
`/.well-known/oauth-authorization-server` (or its `/auth/authorize` and `/auth/token`)
|
||||
- Apps authorized through an external server must be authorized again
|
||||
|
||||
### Added
|
||||
|
||||
- Built-in IndieAuth server: metadata, consent screen, token, revocation endpoints
|
||||
- **Connected Apps** admin page (`/admin/tokens`) to review and revoke Micropub tokens
|
||||
- Discovery links (`micropub`, `indieauth-metadata`, `authorization_endpoint`, `token_endpoint`) on every page
|
||||
- Migration 011: `oidc_login_state`, `indieauth_codes`, `indieauth_tokens`
|
||||
|
||||
### Removed
|
||||
|
||||
- IndieAuth endpoint discovery and external token verification (`starpunk/auth_external.py`)
|
||||
|
||||
### Notes
|
||||
|
||||
- Existing notes, media, tags, webmentions, author profile and admin sessions are preserved
|
||||
- v1.6.0 source (webmentions) recovered from the production image; it had not been committed
|
||||
|
||||
## [1.5.0] - 2025-12-17
|
||||
|
||||
### Added
|
||||
|
||||
+21
-1
@@ -23,7 +23,27 @@ RUN uv venv /opt/venv && \
|
||||
uv pip install --no-cache -r requirements.txt
|
||||
|
||||
# ============================================================================
|
||||
# Runtime Stage - Minimal production image
|
||||
# Test Stage - Runs the test suite (CI builds it with --target test)
|
||||
# ============================================================================
|
||||
FROM python:3.11-slim AS test
|
||||
|
||||
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
|
||||
COPY --from=builder /opt/venv /opt/venv
|
||||
RUN uv pip install --no-cache --python /opt/venv/bin/python pytest pytest-mock
|
||||
|
||||
ENV PATH="/opt/venv/bin:$PATH" \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
SESSION_SECRET=ci-test-secret \
|
||||
SITE_URL=http://localhost:5000 \
|
||||
ADMIN_ME=https://test.example.com
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
RUN python -m pytest -q -p no:cacheprovider tests
|
||||
|
||||
# ============================================================================
|
||||
# Runtime Stage - Minimal production image (default target: keep it last)
|
||||
# ============================================================================
|
||||
FROM python:3.11-slim
|
||||
|
||||
|
||||
@@ -27,7 +27,8 @@ StarPunk is designed for a single user who wants to:
|
||||
## Features
|
||||
|
||||
- **File-based storage**: Notes are markdown files, owned by you
|
||||
- **IndieAuth authentication**: Use your own website as identity
|
||||
- **OIDC admin login**: Sign in through your identity provider (e.g. Pocket ID)
|
||||
- **Built-in IndieAuth server**: Approve Micropub apps without a separate auth service
|
||||
- **Micropub support**: Full W3C Micropub specification compliance
|
||||
- **Media attachments**: Upload and display images with your notes
|
||||
- **Microformats2**: Full h-entry, h-card, and h-feed markup for IndieWeb compatibility
|
||||
@@ -62,7 +63,7 @@ uv pip install -r requirements.txt
|
||||
|
||||
# Configure
|
||||
cp .env.example .env
|
||||
# Edit .env and set ADMIN_ME and SESSION_SECRET
|
||||
# Edit .env and set ADMIN_ME, SESSION_SECRET and the OIDC_* values
|
||||
|
||||
# Initialize database
|
||||
mkdir -p data/notes
|
||||
@@ -82,6 +83,8 @@ All configuration is in the `.env` file. Required settings:
|
||||
- `ADMIN_ME` - Your IndieWeb identity URL (e.g., https://yoursite.com)
|
||||
- `SESSION_SECRET` - Random secret key (generate with `python3 -c "import secrets; print(secrets.token_hex(32))"`)
|
||||
- `SITE_URL` - Public URL of your site
|
||||
- `OIDC_ISSUER`, `OIDC_CLIENT_ID`, `OIDC_CLIENT_SECRET` - OIDC client for admin login (callback: `<SITE_URL>/auth/callback`)
|
||||
- `OIDC_ADMIN_SUB` - Subject ID of the one account allowed to log in
|
||||
|
||||
See `.env.example` for all options.
|
||||
|
||||
@@ -110,7 +113,7 @@ starpunk/
|
||||
|
||||
**Via Micropub Client**:
|
||||
1. Configure client with your site URL
|
||||
2. Authenticate via IndieAuth
|
||||
2. Sign in with your OIDC provider
|
||||
3. Publish from any Micropub-compatible app
|
||||
|
||||
### Backing Up Your Data
|
||||
@@ -192,7 +195,6 @@ Built with:
|
||||
- [python-markdown](https://python-markdown.github.io/) - Markdown processing
|
||||
- [feedgen](https://feedgen.kiesow.be/) - RSS generation
|
||||
- [httpx](https://www.python-httpx.org/) - HTTP client
|
||||
- [IndieLogin](https://indielogin.com/) - Authentication service
|
||||
|
||||
## Contributing
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
# ADR-064: OIDC Admin Login and Built-in IndieAuth Server
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.0.0, 2026-10-31)
|
||||
|
||||
Supersedes the login parts of ADR-021, ADR-025, ADR-027 and ADR-043, and the
|
||||
external token verification of ADR-030 and ADR-036.
|
||||
|
||||
## Context
|
||||
|
||||
Up to v1.6.0 StarPunk was an IndieAuth *client*. Admin login discovered an
|
||||
authorization endpoint from `ADMIN_ME` and used the authentication-only
|
||||
flow; Micropub verified bearer tokens by calling the token endpoint
|
||||
discovered from `ADMIN_ME`. In practice both pointed at Gondulf, a separate
|
||||
self-hosted IndieAuth server.
|
||||
|
||||
The operator now runs Pocket ID (OIDC, passkeys) for every other
|
||||
self-hosted service and wants StarPunk to use it too. Pocket ID cannot act
|
||||
as an IndieAuth server: IndieAuth clients identify themselves by URL
|
||||
(`client_id` = their homepage) and are not pre-registered, while OIDC
|
||||
clients must be registered with the provider.
|
||||
|
||||
Micropub apps such as Quill and Indigenous only speak IndieAuth, so moving
|
||||
login to OIDC alone would end Micropub posting.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Admin login uses OIDC** (authorization code flow, PKCE S256, nonce).
|
||||
StarPunk is a confidential client. The ID token's signature (provider
|
||||
JWKS), `iss`, `aud`, `exp` and `nonce` are verified, and only the
|
||||
configured `OIDC_ADMIN_SUB` may log in. A successful login creates the
|
||||
same local session as before, for `ADMIN_ME`.
|
||||
2. **StarPunk becomes its own IndieAuth server** for Micropub clients:
|
||||
`/.well-known/oauth-authorization-server`, `/auth/authorize` (consent
|
||||
screen, admin session required, so approval is protected by OIDC),
|
||||
`/auth/token` and `/auth/revoke`. Only `response_type=code` with PKCE
|
||||
S256 is supported. Tokens are stored as SHA-256 hashes and verified
|
||||
locally by `/micropub` and `/media`.
|
||||
3. **`ADMIN_ME` stays** as the site owner's identity URL: it is the author
|
||||
h-card source and the `me` in issued tokens. That page must advertise
|
||||
StarPunk's IndieAuth metadata (or `authorization_endpoint` /
|
||||
`token_endpoint` links) so Micropub apps discover StarPunk.
|
||||
4. **New tables** (`oidc_login_state`, `indieauth_codes`,
|
||||
`indieauth_tokens`, migration 011). The legacy `auth_state`, `tokens`
|
||||
and `authorization_codes` tables are not reused because migration 004
|
||||
drops the last two after `INITIAL_SCHEMA_SQL` creates them.
|
||||
5. Client `redirect_uri` must share the `client_id` origin, or be published
|
||||
by the client (JSON `redirect_uris` or `rel="redirect_uri"`). The fetch
|
||||
reuses the webmention SSRF guard.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Gondulf is no longer needed and can be retired.
|
||||
- Existing admin sessions stay valid (sessions are keyed by `ADMIN_ME`, which
|
||||
does not change). Notes, media, tags and webmentions are untouched.
|
||||
- Micropub apps must be re-authorized once, since their old tokens were
|
||||
issued by Gondulf. Tokens are visible and revocable at `/admin/tokens`.
|
||||
- Production requires `OIDC_ISSUER`, `OIDC_CLIENT_ID`,
|
||||
`OIDC_CLIENT_SECRET`, `OIDC_ADMIN_SUB`. `INDIELOGIN_URL` and
|
||||
`TOKEN_ENDPOINT` are ignored.
|
||||
- New dependency: `PyJWT[crypto]`.
|
||||
- Major version bump (2.0.0): configuration and endpoint discovery change
|
||||
incompatibly.
|
||||
|
||||
## Alternatives Considered
|
||||
|
||||
- **Keep Gondulf for Micropub only**: two auth systems to run and patch.
|
||||
- **Drop Micropub**: simplest, but loses posting from apps.
|
||||
- **Manually created API tokens**: Quill and most apps require IndieAuth.
|
||||
@@ -0,0 +1,45 @@
|
||||
# ADR-065: Direct Syndication (POSSE) to GoToSocial
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.1.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
StarPunk posts were copied to other networks by Echofeed reading the feed.
|
||||
That gives no control per post, no record of where a post went, and edits
|
||||
and deletes never follow. The operator runs GoToSocial locally and wants
|
||||
StarPunk to post there directly, with Bluesky likely next.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Publish here, copy out (POSSE).** Each (note, target) has a row in
|
||||
`syndications` (migration 012). Creating, editing, unpublishing or
|
||||
deleting a note marks its rows pending; a background worker reconciles
|
||||
the remote post: create, update (`PUT /api/v1/statuses/:id`) or delete.
|
||||
2. **Defaults.** New notes go to every configured target unless unticked in
|
||||
the editor or a Micropub client sends `mp-syndicate-to` (an explicit
|
||||
list, possibly empty). Existing notes are never backfilled.
|
||||
3. **Content.** The full note as Markdown (`content_type: text/markdown`),
|
||||
then the permalink and the note's tags as hashtags. Longer than the
|
||||
target's limit: shortened at a word boundary with the link kept.
|
||||
Images are uploaded first (the `large` variant) with their captions as
|
||||
alt text.
|
||||
4. **Delivery.** Rows are claimed atomically so the four gunicorn workers'
|
||||
threads never send twice; an `Idempotency-Key` guards retries; a row
|
||||
`version` keeps edits made during a send. Network errors and 5xx/429
|
||||
retry with exponential backoff (8 attempts); other 4xx fail at once.
|
||||
A remote post deleted by hand is posted again on the next edit.
|
||||
5. **Backlinks.** Synced URLs are shown on the note page as `u-syndication`
|
||||
links, and in the editor with status, last error and a retry button.
|
||||
6. **Targets** are config-driven. One kind exists, a Mastodon-API client
|
||||
(`GOTOSOCIAL_URL`, `GOTOSOCIAL_ACCESS_TOKEN`); it also works for
|
||||
Mastodon. A Bluesky target would add a second client behind the same
|
||||
reconcile loop.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Echofeed must stop posting to GoToSocial or posts appear twice.
|
||||
- The token needs only `write:statuses write:media`.
|
||||
- `syndications.note_id` has no foreign key so a hard-deleted note's
|
||||
remote post can still be deleted.
|
||||
@@ -0,0 +1,37 @@
|
||||
# ADR-066: Crossposting to Bluesky
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.2.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
ADR-065 added direct crossposting to GoToSocial. Bluesky was still fed by
|
||||
Echofeed, with the same problems: no per-post control, no record of the
|
||||
copy, deletes never follow.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Second client, same loop.** `BlueskyClient` sits behind the reconcile
|
||||
loop and `syndications` table from ADR-065, as target id `bluesky`.
|
||||
Clients share a small interface: `create`, `update`, `delete`, `can_edit`.
|
||||
2. **Auth.** An app password (`BLUESKY_HANDLE`, `BLUESKY_APP_PASSWORD`).
|
||||
`createSession` against `BLUESKY_SERVICE` (default `https://bsky.social`);
|
||||
writes go to the PDS named in the returned DID document. Expired access
|
||||
tokens are refreshed, falling back to a new session.
|
||||
3. **Content.** Plain text (Markdown rendered and stripped), then the
|
||||
permalink and hashtags (at most a third of the space), shortened to 300
|
||||
characters at a word boundary. Link and hashtag facets use UTF-8 byte
|
||||
offsets. Images (up to 4) are re-encoded as JPEG if over 1 MB, with alt
|
||||
text and aspect ratio; a note without images gets a link card.
|
||||
4. **Edits don't follow.** Bluesky has no edit; rewriting the record would
|
||||
break likes and replies, and delete-and-repost would lose them. The post
|
||||
stays as first sent. Unpublish, untick and delete remove it.
|
||||
5. **No double posts.** When a create is retried, recent posts are checked
|
||||
for one already linking to the note (a send whose reply was lost).
|
||||
|
||||
## Consequences
|
||||
|
||||
- Echofeed is no longer needed; it must stop posting to Bluesky or posts
|
||||
appear twice.
|
||||
- A post deleted by hand on Bluesky is not re-created.
|
||||
@@ -0,0 +1,34 @@
|
||||
# ADR-067: Micropub update, delete, drafts and replies
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.4.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
The Micropub endpoint could only create published notes. Apps could not edit
|
||||
or delete posts, drafts and replies were dropped, and photos sent with the
|
||||
post or hosted elsewhere were lost.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Update** (JSON only, `update` scope). Supported properties are the ones a
|
||||
note can store: `content`, `name` (the `# Heading` first line),
|
||||
`category`, `photo`, `in-reply-to`, `post-status`. Anything else is a 400,
|
||||
not silently ignored. Order: delete, then replace, then add (`add` only for
|
||||
`category` and `photo`). Success is 204.
|
||||
2. **Delete is soft** (`delete` scope) so `undelete` can restore the note and
|
||||
its file from `.trash/`. Crossposts are removed on delete and posted again
|
||||
on undelete.
|
||||
3. **Drafts**: `post-status: draft` creates an unpublished note; crossposts
|
||||
wait until it is published.
|
||||
4. **Photos**: multipart uploads are saved like media-endpoint uploads; remote
|
||||
URLs are downloaded only from public IP addresses, up to 20 MB, max 4.
|
||||
5. **Queries**: `q=source` returns everything create accepts (round-trip);
|
||||
without `url` it lists recent posts, drafts included, for editing apps.
|
||||
`q=category` lists tags by use.
|
||||
|
||||
## Consequences
|
||||
|
||||
- `published` (backdating) can be set on create but not changed by update.
|
||||
- Likes, reposts and bookmarks remain unsupported (round 2).
|
||||
@@ -0,0 +1,32 @@
|
||||
# ADR-068: Likes and reposts
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.5.0, 2026-11-01). Bookmarks were considered and declined.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Storage.** `notes.like_of` / `notes.repost_of` (migration 013). The note's
|
||||
content is an optional comment and may be empty.
|
||||
2. **Display.** Likes stay off the home page and feeds and are listed on
|
||||
`/likes`. Reposts appear in the stream and feeds. Both show the target's
|
||||
fetched title and author (the reply-context cache) and carry
|
||||
`u-like-of` / `u-repost-of`. The target is sent a webmention.
|
||||
3. **Crossposting, per target.**
|
||||
|
||||
| | Target is a post on that network | Anything else |
|
||||
|---|---|---|
|
||||
| Like | native favourite / like | nothing |
|
||||
| Repost, no comment | native boost / repost | "🔁 title url" post |
|
||||
| Repost, with comment | Bluesky quote post; GoToSocial status | comment + "🔁 title url" post |
|
||||
|
||||
GoToSocial finds fediverse posts with `/api/v2/search?resolve=true`
|
||||
(needs `read:search`; favourites need `write:favourites`). Bluesky
|
||||
recognises `bsky.app/profile/<actor>/post/<rkey>` URLs. Native actions
|
||||
are stored as `favourite:<id>` / `reblog:<id>` or the like/repost record
|
||||
URI, and deleting the note undoes them.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Editing a like/repost doesn't change a native favourite/boost.
|
||||
- A repost with a comment can't be a GoToSocial boost (no quote posts there).
|
||||
@@ -0,0 +1,57 @@
|
||||
# ADR-069: Crossposting to Threads
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.6.0, 2026-10-31)
|
||||
|
||||
## Context
|
||||
|
||||
ADR-065 and ADR-066 crosspost notes to GoToSocial and Bluesky. Threads
|
||||
(Meta) has a publishing API, so it can be a third target on the same loop.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **Third client, same loop.** `ThreadsClient` is target id `threads`
|
||||
behind the `syndications` table and reconcile loop from ADR-065.
|
||||
2. **Auth.** A long-lived Threads user token (`THREADS_ACCESS_TOKEN`) with
|
||||
`threads_basic`, `threads_content_publish` and `threads_delete`. These
|
||||
tokens expire after 60 days, so StarPunk refreshes the token about once a
|
||||
day (`th_refresh_token`) from the worker loop, even when nothing is being
|
||||
posted. The current token is kept in `DATA_PATH/threads_token.json`
|
||||
(mode 0600) with a hash of the configured token. A different configured
|
||||
token replaces the saved one. A file lock stops gunicorn workers from
|
||||
refreshing at the same time. The file avoids a migration for one value.
|
||||
3. **Posting.** Create a container, poll its status until `FINISHED`, then
|
||||
`threads_publish`. Text-only notes are `TEXT` posts. Threads previews
|
||||
the first link in the text (the permalink) by itself. An explicit
|
||||
`link_attachment` is not sent, because Meta rejects one for a page it
|
||||
hasn't fetched yet ("Invalid Link Attachment", subcode 4279047), which
|
||||
is always the case for a new note (v2.6.2). One image is an `IMAGE` post and 2–4 images are a
|
||||
`CAROUSEL`. Meta fetches images from their public `/media/` URL. Only
|
||||
JPEG and PNG up to 8 MB are allowed, so GIF and WebP images are left off.
|
||||
Alt text goes in `alt_text`.
|
||||
4. **Content.** Plain text (as for Bluesky), then the permalink, shortened
|
||||
to 500 characters. Non-ASCII characters are counted as UTF-8 bytes,
|
||||
because Threads counts emoji that way. Threads allows one tag per post,
|
||||
so the note's first tag is sent as `topic_tag` and no hashtags are added
|
||||
to the text.
|
||||
5. **Edits don't follow.** The API has no way to edit a post. As with
|
||||
Bluesky, the post stays as first sent, and unpublishing, unticking the
|
||||
target or deleting the note removes it.
|
||||
6. **Likes and reposts (ADR-068).** The API can't like or repost outside
|
||||
content, so likes are not crossposted. Reposts are text posts with
|
||||
"🔁 title" and the URL, plus the comment if there is one, and the
|
||||
reposted page as the link preview.
|
||||
7. **No double posts.** A retried create first looks for a recent post that
|
||||
already contains the permalink.
|
||||
|
||||
## Consequences
|
||||
|
||||
- Meta limits accounts to 250 posts and 100 deletes per day. Rate-limit
|
||||
errors (Graph codes 4, 17, 32, 613) are retried.
|
||||
- If StarPunk is down for more than 60 days, the token expires and a new
|
||||
one is needed.
|
||||
- Getting the first token requires a Meta developer app with the Threads
|
||||
use case. See the setup notes in `docs/design/v2.6.0/`.
|
||||
- Notes with more than five links fail on Threads (a Meta limit). The
|
||||
error is shown in the editor.
|
||||
@@ -0,0 +1,44 @@
|
||||
# ADR-070: Links and link cards in crossposts
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.6.3, 2026-11-01)
|
||||
|
||||
## Context
|
||||
|
||||
Every crosspost carries the note's permalink. Notes often link elsewhere
|
||||
too, usually to the article being discussed. Before this change:
|
||||
|
||||
- Bluesky and Threads get plain text, and a Markdown link `[text](url)`
|
||||
lost its URL, so readers there couldn't follow it.
|
||||
- The Bluesky card always showed the note, and the Threads preview showed
|
||||
whichever link came first.
|
||||
- Threads rejects posts with more than 5 different links.
|
||||
|
||||
## Decision
|
||||
|
||||
1. **The card shows what the note links to.** If the note has its own link
|
||||
(a Markdown link, `<url>` or a bare URL), the first one gets the
|
||||
preview card. A note without links gets a card for itself, as before.
|
||||
Images still take the place of a card on Bluesky.
|
||||
2. **Bluesky.** The card is built by StarPunk: it fetches the page's Open
|
||||
Graph, Twitter or plain HTML title, description and image, and uploads
|
||||
the image (shrunk to under 1 MB) as the thumbnail. If the page can't
|
||||
be fetched, the card has just the URL. Markdown link text becomes a
|
||||
clickable link facet, so no characters are spent on the URL.
|
||||
3. **Threads.** It has no clickable link text, so Markdown links are
|
||||
written "text (url)". Threads previews the first link in the text, which
|
||||
is now the linked page. To stay within Meta's 5-link limit, after 4
|
||||
different links in the body a Markdown link keeps only its text.
|
||||
4. **GoToSocial** already gets Markdown, so its links are unchanged.
|
||||
Receiving servers build their own cards.
|
||||
5. **Fetching is careful.** Only http(s) URLs on public addresses are
|
||||
fetched, and this is checked again at every redirect. At most 512 KB of
|
||||
a page and 5 MB of an image are read. A failed fetch never fails the
|
||||
post.
|
||||
|
||||
## Consequences
|
||||
|
||||
- One or two extra requests to the linked site when a note is crossposted
|
||||
to Bluesky.
|
||||
- Reposts (ADR-068) are unchanged: their card is the reposted page.
|
||||
@@ -0,0 +1,206 @@
|
||||
# ADR-071: WebSub publishing with a built-in hub
|
||||
|
||||
## Status
|
||||
|
||||
Accepted (v2.10.0, 2026-11-03)
|
||||
|
||||
## Context
|
||||
|
||||
Readers poll StarPunk's feeds. WebSub (W3C Recommendation,
|
||||
https://www.w3.org/TR/websub/) lets readers that support it (Monocle,
|
||||
Aperture, Inoreader, NewsBlur, Miniflux and others) subscribe once and
|
||||
have new content pushed to them within seconds.
|
||||
|
||||
WebSub has three roles. StarPunk is the **publisher**. Someone has to run
|
||||
the **hub** that keeps subscriptions and delivers content. Public hubs
|
||||
exist (switchboard.p3k.io, pubsubhubbub.appspot.com), but they are a
|
||||
third-party dependency and some have shut down. The rest of this site is
|
||||
self-hosted.
|
||||
|
||||
ADR-014 deferred WebSub for v1 as "complexity, external dependency".
|
||||
|
||||
Two existing problems matter here:
|
||||
|
||||
- Feeds are cached for `FEED_CACHE_SECONDS` (5 minutes) and nothing clears
|
||||
the cache when a note changes. A hub fetching right after a change would
|
||||
get stale content. Regular readers are also up to 5 minutes behind.
|
||||
- The RSS feed is served at `/feed.rss` and `/feed.xml` (legacy alias), and
|
||||
calls itself `/feed.xml`.
|
||||
|
||||
## Decision
|
||||
|
||||
StarPunk runs its own hub, **for its own topics only**, at `/websub`.
|
||||
`WEBSUB_HUB_URL` can point at an external hub instead; then StarPunk
|
||||
advertises that hub, pings it on changes, and its own `/websub` is off.
|
||||
|
||||
### 1. Topics
|
||||
|
||||
Absolute URLs under `SITE_URL`:
|
||||
|
||||
| Topic | Content type |
|
||||
|---|---|
|
||||
| `/` (h-feed) | text/html |
|
||||
| `/feed.atom` | application/atom+xml |
|
||||
| `/feed.rss` | application/rss+xml |
|
||||
| `/feed.json` | application/feed+json |
|
||||
| `/likes` | text/html |
|
||||
| `/tag/<name>` | text/html |
|
||||
|
||||
`/feed.xml` and the negotiated `/feed` advertise the topic of the format
|
||||
they serve as `rel=self` (`/feed.rss`, `/feed.atom`, `/feed.json`). The
|
||||
RSS feed's own self link changes from `/feed.xml` to `/feed.rss`.
|
||||
|
||||
### 2. Discovery (publisher, spec 4)
|
||||
|
||||
Every topic response carries `Link: <hub>; rel="hub"` and exactly one
|
||||
`Link: <topic>; rel="self"`. Embedded links as well: `<link rel="hub">` and
|
||||
`<link rel="self">` in the HTML head of topic pages, `<link rel="hub">`
|
||||
next to the existing self link in Atom, `<atom:link rel="hub">` in RSS, and
|
||||
`"hubs": [{"type": "WebSub", "url": ...}]` in JSON Feed.
|
||||
|
||||
### 3. What changed, and when
|
||||
|
||||
`websub.note_changed(note, old_tags=())` is called wherever
|
||||
`syndication.note_changed` is, plus on create. It:
|
||||
|
||||
1. clears the feed cache (all formats), and
|
||||
2. if the note is or was public, queues delivery for `/`, the three
|
||||
feeds, `/likes` if it is a like, and each `/tag/<name>` for its tags
|
||||
before and after the change.
|
||||
|
||||
Drafts that stay drafts change nothing.
|
||||
|
||||
### 4. Hub endpoint (spec 5)
|
||||
|
||||
`POST /websub`, `application/x-www-form-urlencoded`:
|
||||
|
||||
- `hub.mode` subscribe or unsubscribe; `hub.topic` must be one of the
|
||||
topics above (after normalising), else 400 with a plain-text reason;
|
||||
`hub.callback` must be an absolute http(s) URL that passes the SSRF
|
||||
check; `hub.secret` under 200 bytes; `hub.lease_seconds` clamped to
|
||||
1 day to 30 days (default 10 days). Unknown parameters are ignored.
|
||||
- Response: 202 Accepted, independent of what verification later finds.
|
||||
- Limits: the Webmention rate limiter (per IP); at most 25 subscriptions
|
||||
per callback host and 1,000 in total; beyond that, 429.
|
||||
|
||||
### 5. Verification of intent (spec 5.3)
|
||||
|
||||
A background worker sends `GET <callback>` with the callback's query
|
||||
string kept and `hub.mode`, `hub.topic`, `hub.challenge` (random, 32
|
||||
bytes) and `hub.lease_seconds` appended. Success is a 2xx with the body
|
||||
equal to the challenge; anything else, including redirects, fails and
|
||||
leaves the subscription state unchanged. On success a subscribe becomes
|
||||
active with `expires_at = now + lease`, and an unsubscribe deletes it.
|
||||
A re-subscribe replaces the secret and lease only once verified.
|
||||
|
||||
### 6. Content distribution (spec 6, 7)
|
||||
|
||||
For each change, one delivery per active, unexpired subscription to the
|
||||
topic. The worker renders the topic inside the app (no HTTP round trip
|
||||
through Caddy), so subscribers get exactly what the public URL serves,
|
||||
then `POST`s it to the callback with:
|
||||
|
||||
- `Content-Type` of the topic,
|
||||
- `Link` headers with `rel=hub` and `rel=self`,
|
||||
- `X-Hub-Signature: sha256=<HMAC-SHA256(secret, body)>` if a secret was
|
||||
given.
|
||||
|
||||
A 2xx is success. 410 Gone removes the subscription. Anything else is
|
||||
retried after 1 min, 5 min, 30 min, 2 h and 6 h, then dropped; a
|
||||
subscription whose deliveries fail for 3 days is removed. Only the latest
|
||||
content is sent: a queued delivery for a topic is replaced by a newer one.
|
||||
|
||||
### 7. Outbound safety
|
||||
|
||||
Callbacks are arbitrary URLs, so verification and delivery go through
|
||||
`webmention_ssrf` (`safe_get`/`safe_post`): public addresses only, the
|
||||
connection pinned to the checked address, no redirects, 10 s time limit,
|
||||
1 MB response cap.
|
||||
|
||||
### 8. Data (migration 016)
|
||||
|
||||
- `websub_subscriptions(id, topic, callback, secret, lease_seconds,
|
||||
state ['pending','active'], expires_at, created_at, verified_at,
|
||||
last_success_at, last_error)`, unique on `(topic, callback)`.
|
||||
- `websub_intents(id, topic, callback, mode, secret, lease_seconds,
|
||||
attempts, next_attempt_at, created_at)`: verifications to do.
|
||||
- `websub_deliveries(id, subscription_id, attempts, next_attempt_at,
|
||||
last_error, created_at)`, unique on `subscription_id`.
|
||||
|
||||
### 9. Worker
|
||||
|
||||
The same pattern as the Webmention worker: one thread per gunicorn
|
||||
process, a file lock so only one process works the queues, app context
|
||||
around each poll. Expired subscriptions are pruned once a day.
|
||||
|
||||
### 10. Admin
|
||||
|
||||
The admin dashboard shows active subscribers per topic and failing
|
||||
deliveries.
|
||||
|
||||
### 11. Configuration
|
||||
|
||||
`WEBSUB_ENABLED` (default true), `WEBSUB_HUB_URL` (default: built-in),
|
||||
`WEBSUB_MAX_SUBSCRIPTIONS` (1000), `WEBSUB_MAX_PER_HOST` (25).
|
||||
|
||||
## Consequences
|
||||
|
||||
- Readers that support WebSub get posts and edits within seconds.
|
||||
- Feeds are fresh right after a change for every reader, because the cache
|
||||
is cleared.
|
||||
- StarPunk sends HTTP requests to subscriber-chosen URLs. The SSRF
|
||||
protections, per-host and total limits, and rate limiting bound that.
|
||||
- websub.rocks' hub tests use websub.rocks' own topics, which this hub
|
||||
refuses by design. Testing uses unit tests of each spec section and a
|
||||
real subscription from a reader after deploy.
|
||||
- The RSS self link changes to `/feed.rss`. Readers keep whichever URL
|
||||
they subscribed to; both keep working.
|
||||
|
||||
## Alternatives considered
|
||||
|
||||
- **Public hub only.** Least code, but a third-party dependency for a
|
||||
self-hosted site. Still available through `WEBSUB_HUB_URL`.
|
||||
- **A separate hub service** (another container). More to deploy and
|
||||
keep updated, and few maintained hubs exist.
|
||||
- **Accepting any topic** (a general-purpose hub). Not needed, and it
|
||||
would let the hub be used to fetch and push arbitrary URLs.
|
||||
|
||||
## Amendments (v2.10.1, 2026-11-03)
|
||||
|
||||
An adversarial review of v2.10.0 against the Recommendation led to these
|
||||
changes; where they differ, they replace the text above.
|
||||
|
||||
- **Worker robustness.** Each queued item is handled on its own: an
|
||||
unexpected error drops (verification) or retries (delivery) that item
|
||||
only. Before, a callback answering with a large page or containing a
|
||||
control character stopped the worker for good.
|
||||
- **Time limits.** `safe_get` and `safe_post` close the connection when
|
||||
the whole exchange exceeds its time limit (10 s), not just a single read.
|
||||
Verification replies over 4 KB fail; delivery responses are read up to
|
||||
4 KB.
|
||||
- **Leases are kept (spec 7).** A failing subscriber is not removed; its
|
||||
deliveries are retried and then given up, and the next change is
|
||||
delivered again. Only lease expiry, 410 Gone or unsubscribing end a
|
||||
subscription. DNS failures are retried. A topic that renders 404/410
|
||||
ends its subscriptions with a `hub.mode=denied` notice; other render
|
||||
errors are retried.
|
||||
- **Photos.** A note with photos is announced once they are attached
|
||||
(`attach_media_to_note` notifies), and the feed cache key and ETag
|
||||
include `content_version`, so photo and caption changes reach feeds and
|
||||
subscribers. ETags no longer depend on the feed's build time.
|
||||
- **Subscription requests (spec 5.1).** Unreserved percent-encodings are
|
||||
decoded; the Content-Type check is case-insensitive; `hub.secret` and
|
||||
`hub.lease_seconds` are ignored for unsubscriptions; an empty secret is
|
||||
refused; verification echoes `hub.topic` exactly as requested
|
||||
(`requested_topic`); unsubscribing works while an external hub is
|
||||
configured. Callbacks must be printable, at most 2,048 characters, not on
|
||||
this site, on port 80, 443 or 1024+, and are stored with an IDNA host.
|
||||
- **Limits** count pending requests as well as subscriptions, per callback
|
||||
*address* (so subdomains on one server share a limit), with at most 200
|
||||
pending requests. Rate limiting needs `TRUSTED_PROXY=true` behind Caddy.
|
||||
- **Races.** Each request has a `request_id`; verifying an older request
|
||||
doesn't consume a newer one (migration 017).
|
||||
- **Less work.** A topic is rendered once per worker pass; likes only
|
||||
update `/likes` and tag pages.
|
||||
- Corrections: there is no `state` column (a subscription row exists only
|
||||
once verified); expired leases are pruned hourly.
|
||||
@@ -0,0 +1,56 @@
|
||||
# v2.0.0 Implementation Report: OIDC Login and Built-in IndieAuth Server
|
||||
|
||||
Date: 2026-10-31
|
||||
Decision: ADR-064
|
||||
|
||||
## Summary
|
||||
|
||||
Admin login moved from IndieAuth (via Gondulf) to OIDC (Pocket ID).
|
||||
StarPunk now issues and verifies its own Micropub tokens, so Gondulf is no
|
||||
longer needed. No data migration is required: notes, media, tags,
|
||||
webmentions, the author profile and admin sessions are unchanged.
|
||||
|
||||
## Source recovery
|
||||
|
||||
Production ran `starpunk:v1.6.0-rc.2`, but the v1.6.0 code was never
|
||||
committed (`main` was at v1.5.0). The application code was copied out of
|
||||
the running container's `/app` and committed as `v1.6.0-rc.2` before any
|
||||
v2.0.0 changes. The v1.6.0 tests and docs were not in the image, so ADR-063
|
||||
(webmentions) and the v1.6.0 design folder are still missing.
|
||||
|
||||
## Changes
|
||||
|
||||
| Area | Change |
|
||||
|---|---|
|
||||
| `starpunk/auth.py` | OIDC client: discovery (cached), PKCE S256, nonce, ID token check against the provider JWKS, `OIDC_ADMIN_SUB` check. Session functions unchanged. |
|
||||
| `starpunk/indieauth.py` | New: authorization codes, access tokens (hashed), client `redirect_uri` checks, revocation. |
|
||||
| `starpunk/routes/auth.py` | Login is a single "Sign in with ..." button; callback handles provider errors and `next`. |
|
||||
| `starpunk/routes/indieauth.py` | New: metadata, consent, approve, token, legacy token verify, revoke. |
|
||||
| `starpunk/routes/micropub.py` | Verifies tokens locally. |
|
||||
| `starpunk/routes/admin.py`, `templates/admin/tokens.html` | Connected Apps page with revoke. |
|
||||
| `templates/base.html` | Discovery links built from `SITE_URL` (not the request host, which is `http://` behind the proxy). |
|
||||
| `migrations/011_*.sql` | `oidc_login_state`, `indieauth_codes`, `indieauth_tokens`. |
|
||||
| `starpunk/auth_external.py` | Removed. |
|
||||
|
||||
## Testing
|
||||
|
||||
- New: `tests/test_auth_oidc.py` (real RS256-signed ID tokens: bad audience, issuer,
|
||||
key, expiry, nonce, subject; state reuse; routes) and `tests/test_indieauth.py`
|
||||
(validation, PKCE, single-use codes, consent, full Micropub post with an issued
|
||||
token, revocation, admin page).
|
||||
- `tests/test_auth.py` keeps the session tests; IndieLogin flow tests removed.
|
||||
- Full suite: 905 passed. Remaining failures predate this work and are in the
|
||||
recovered v1.6.0 baseline: seven in `test_media_upload.py` (image
|
||||
validation, HEIC, MPO) and one or two timing-sensitive tests in
|
||||
`test_migration_race_condition.py` that fail on a different test each run.
|
||||
- Migration rehearsed on a copy of the production database: 011 applied,
|
||||
row counts for notes, media, tags, sessions and author profile unchanged.
|
||||
|
||||
## Deployment notes
|
||||
|
||||
- Pocket ID client "StarPunk": confidential, PKCE on, callback
|
||||
`https://starpunk.thesatelliteoflove.com/auth/callback`.
|
||||
- The container needs `id.thesatelliteoflove.com` mapped to Caddy in
|
||||
`extra_hosts` (hairpin NAT does not work from the `lava` network).
|
||||
- `thesatelliteoflove.com` (static file in `/opt/stacks/caddy/site/tsol/`)
|
||||
must advertise StarPunk's IndieAuth endpoints instead of Gondulf's.
|
||||
@@ -0,0 +1,23 @@
|
||||
# v2.3.3: Micropub titles, q=source URL, single title display
|
||||
|
||||
Found while building tuipub, a terminal Micropub client, against StarPunk.
|
||||
|
||||
## Problems
|
||||
|
||||
1. **Micropub `name` dropped.** `handle_create` called `extract_title()` and threw the result away. StarPunk has no title column: `Note.title` is derived from the content's first line, and a `# Heading` first line marks a titled note (Q22). So a `name` sent by a client never showed up.
|
||||
2. **`q=source` URL wrong.** It built `{SITE_URL}notes/{slug}`, but the route is `/note/<slug>`, so the URL was a 404.
|
||||
3. **`q=source` name wrong.** It returned `note.title` for every note. For untitled notes that is just the first line of the content, so clients saw a `name` they never sent.
|
||||
4. **Title shown twice.** `note.html` renders the title as `<h1 class="p-name">` and then the whole content, which includes the `# Heading` again. Home-page previews did the same with the excerpt.
|
||||
|
||||
## Decisions
|
||||
|
||||
- Store `name` as a `# {name}` first line (`micropub.add_title_heading`) rather than adding a title column. This matches how titled notes already work everywhere (pages, feeds, search, admin editor), and needs no migration. If the content already starts with that exact heading, it isn't added twice.
|
||||
- `models.split_title()` is the single definition of "title heading": the first non-blank line starting with `#` (the same rule as the templates' `has_explicit_title`). `q=source` uses it to return `name` + `content` the way they were posted.
|
||||
- `Note.body_html` renders the content without the title heading; `note.html` uses it. `Note.excerpt` also leaves the title out. `Note.html` is unchanged, so feeds and syndication behave as before (feed items carry a separate title anyway).
|
||||
- `extract_title()` no longer derives a fallback title from content: that value was never used.
|
||||
|
||||
## Tests
|
||||
|
||||
- `tests/test_micropub.py`: name stored as heading, no duplicate heading, `q=source` url equals the Location permalink, `name` only for titled notes, round trip.
|
||||
- `tests/test_models.py`: `split_title`, `body_html`, excerpt without title.
|
||||
- Checked end to end with tuipub against a local dev server: titled post → page shows the title once, `q=source` returns `name`, `content` and a working url.
|
||||
@@ -0,0 +1,62 @@
|
||||
# v2.6.0: Crossposting to Threads
|
||||
|
||||
Implements ADR-069.
|
||||
|
||||
## What changed
|
||||
|
||||
- `starpunk/syndication.py`: `ThreadsClient` (target `threads`); media
|
||||
entries now carry their public `url`; `process_due()` calls each client's
|
||||
optional `maintain()` hook, which Threads uses to refresh its token.
|
||||
- `starpunk/config.py`: `THREADS_ACCESS_TOKEN`, `THREADS_USERNAME`,
|
||||
`THREADS_NAME`, `THREADS_MAX_CHARS` (500), `THREADS_MAX_MEDIA` (4).
|
||||
- `tests/test_syndication_threads.py`: a fake Graph API covering text,
|
||||
image and carousel posts; container polling; edits not following; deletes
|
||||
(including posts already deleted by hand); a lost publish reply;
|
||||
permanent and rate-limit errors; and token refresh, persistence and
|
||||
replacement.
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Meaning |
|
||||
|---|---|
|
||||
| `THREADS_ACCESS_TOKEN` | Long-lived Threads user token. Enables the target. |
|
||||
| `THREADS_USERNAME` | Optional. Used for the Micropub `syndicate-to` uid (`https://www.threads.com/@name`) and for fallback links. |
|
||||
| `THREADS_NAME` | Label in the editor (default `Threads`) |
|
||||
|
||||
After the first daily refresh, the working token lives in
|
||||
`DATA_PATH/threads_token.json`. The configured value only seeds it.
|
||||
Changing `THREADS_ACCESS_TOKEN` switches to the new token.
|
||||
|
||||
## Getting a token (one time)
|
||||
|
||||
1. At <https://developers.facebook.com/apps>, create an app with the
|
||||
**Access the Threads API** use case.
|
||||
2. Under the use case's permissions, add `threads_basic`,
|
||||
`threads_content_publish` and `threads_delete`.
|
||||
3. Under **App roles → Roles**, add your Threads account as a Threads
|
||||
tester. Accept the invite in Threads (Settings → Account → Website
|
||||
permissions → Invites). The app can stay in development mode.
|
||||
4. In the use case settings, add `https://<site>/` as a redirect URI. Then
|
||||
open:
|
||||
|
||||
https://threads.net/oauth/authorize?client_id=APP_ID&redirect_uri=https://<site>/&scope=threads_basic,threads_content_publish,threads_delete&response_type=code
|
||||
|
||||
After you approve, the browser lands on `https://<site>/?code=...`. Copy
|
||||
the code (drop a trailing `#_`).
|
||||
5. Exchange the code for a short-lived token, then exchange that for a
|
||||
long-lived one:
|
||||
|
||||
curl -s -X POST https://graph.threads.net/oauth/access_token \
|
||||
-F client_id=APP_ID -F client_secret=APP_SECRET \
|
||||
-F grant_type=authorization_code -F redirect_uri=https://<site>/ -F code=CODE
|
||||
curl -s "https://graph.threads.net/access_token?grant_type=th_exchange_token&client_secret=APP_SECRET&access_token=SHORT_TOKEN"
|
||||
|
||||
6. Set `THREADS_ACCESS_TOKEN` to the long-lived `access_token`.
|
||||
|
||||
The app secret is only needed for these steps. StarPunk doesn't use it.
|
||||
|
||||
## Not done
|
||||
|
||||
- Video, GIF and WebP images (Threads takes video by URL. StarPunk only
|
||||
stores still images, and Threads rejects GIF and WebP).
|
||||
- Edits (no API for them).
|
||||
@@ -0,0 +1,89 @@
|
||||
# v2.6.5: Micropub media fixes
|
||||
|
||||
Found while adding media uploads to tuipub, a Micropub client written to the
|
||||
spec rather than to StarPunk.
|
||||
|
||||
## Problems
|
||||
|
||||
1. **Failed creates left a post behind.** `handle_create` made the note, then
|
||||
attached photos. Listing one photo twice broke the `note_media` unique key,
|
||||
so the client got a 500 while the note stayed published without photos.
|
||||
A retry would post it twice.
|
||||
2. **Then the database stayed locked.** `attach_media_to_note` didn't roll
|
||||
back, and `ConnectionPool.return_connection` pooled the connection with
|
||||
its write transaction still open. Other requests got "database is locked"
|
||||
until the 10 s timeout.
|
||||
3. **Photo posts needed text.** The Micropub spec doesn't require `content`,
|
||||
and `q=config` advertised a `photo` post type with only `photo`, yet
|
||||
creates without content got 400 "Content is required".
|
||||
|
||||
## Changes
|
||||
|
||||
- `database/pool.py`: `return_connection` rolls back an open transaction
|
||||
(or closes the connection if that fails).
|
||||
- `media.py`: `attach_media_to_note` rolls back on error.
|
||||
- `micropub.py`:
|
||||
- `handle_create` resolves photos (by media URL, downloading remote ones)
|
||||
before `create_note`. If attaching or crosspost selection fails, it
|
||||
hard-deletes the note before answering 500.
|
||||
- `_resolve_photos` skips repeated media.
|
||||
- Content is optional when photos resolve. Content-less posts whose photos
|
||||
all fail still get a 400 from `create_note`.
|
||||
- `handle_update` allows `replace: {content: [""]}` and `delete: ["content"]`
|
||||
while the note keeps photos (or is a like or repost). Otherwise it answers
|
||||
400 "A post needs text or a photo".
|
||||
- `notes.py`: `create_note` and `update_note` take `has_media`, so
|
||||
empty content is allowed for photo posts.
|
||||
- `syndication.py`: `compose_text` drops the leading blank line when there's
|
||||
no body.
|
||||
|
||||
## Post types (IndieWeb standards check)
|
||||
|
||||
Post Type Discovery (W3C Note) has no separate "photo" type to store. A post
|
||||
with a valid `photo` is a photo post, unless it's first a repost, like or
|
||||
reply (step order: repost, like, reply, video, photo). Otherwise it's a note,
|
||||
or an article when its `name` isn't a prefix of its content. h-entry treats
|
||||
`u-photo` as the primary content, with `name` as a caption. mf2 implies a
|
||||
`name` only when an entry has no other `p-*`/`e-*` properties and no nested
|
||||
microformats.
|
||||
|
||||
StarPunk already stores photo posts as notes with media and renders
|
||||
`u-photo` outside `e-content`, which is what the standards ask for. Checking
|
||||
the rendered pages with mf2py found:
|
||||
|
||||
- **Replies weren't replies.** In `note.html`, `u-in-reply-to` was inside a
|
||||
bare `h-cite`, so it was the h-cite's property, not the entry's.
|
||||
`note_preview.html` had no reply markup at all. Fixed: the blockquote is
|
||||
`u-in-reply-to h-cite` (with `u-url` inside), and previews show
|
||||
"↩ In reply to" with `u-in-reply-to`.
|
||||
- **Implied names were possible.** Previews left out `e-content` for posts
|
||||
without text. They worked only because the author h-card blocked an
|
||||
implied name. Previews now always emit `e-content`.
|
||||
- Page title for text-free posts follows PTD order: Repost, Like, Reply,
|
||||
Photo.
|
||||
|
||||
Photos without a caption take the post's text as alt (`partials/media.html`
|
||||
`fallback_alt`) instead of the made-up "Image". The Micropub `q=source`
|
||||
still reports the stored caption (`""`).
|
||||
|
||||
`tests/test_post_types.py` parses note pages and the home page with mf2py and
|
||||
runs PTD for notes, articles, photo posts (no text, caption, title), replies
|
||||
(with and without a photo) and reposts.
|
||||
|
||||
## Not changed (noted for later)
|
||||
|
||||
- Text-free photos without alt text still render `alt="Image"`. Captioned
|
||||
photos use their caption, and others now use the post's text (`display_media`
|
||||
`fallback_alt`, the excerpt cut to 150 characters).
|
||||
|
||||
- More than 4 photos are still cut to 4 without an error (ADR-057).
|
||||
- `video` and `audio` are ignored. This is allowed by the spec, and they
|
||||
aren't in `q=config` post-types.
|
||||
- Media endpoint errors for non-images expose Python text
|
||||
("cannot identify image file <_io.BytesIO …>").
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_micropub_v265.py`. The full suite runs with
|
||||
`SESSION_SECRET=test`. 8 tests in `test_media_upload.py` and
|
||||
`test_migration_race_condition.py` already fail on main, and still fail here.
|
||||
@@ -0,0 +1,37 @@
|
||||
# v2.6.8: Webmention worker app context
|
||||
|
||||
## Problem
|
||||
|
||||
Production webmention #1 (Bridgy, reply from Mastodon to note 20261101162512)
|
||||
was stored with `validation_status = 'error'`:
|
||||
"Working outside of application context". Both outgoing queue items had the
|
||||
same error and were on their last retry.
|
||||
|
||||
## Causes
|
||||
|
||||
1. `webmention_ssrf.safe_resolve()` reads `current_app.config['DEV_MODE']`.
|
||||
`WebmentionWorker` is a plain thread with no Flask app context, so every
|
||||
`validate_url_for_fetch()` call raised `RuntimeError`, for both
|
||||
validation (incoming) and endpoint discovery and sending (outgoing).
|
||||
2. `create_app()` registered a `teardown_appcontext` handler that called
|
||||
`webmention_worker.stop()`. Teardown runs whenever any app context ends
|
||||
(every request), so the worker stopped soon after startup.
|
||||
|
||||
## Fix
|
||||
|
||||
- `WebmentionWorker` takes the Flask app and runs each poll inside
|
||||
`app.app_context()`, as `SyndicationWorker` already does.
|
||||
- The teardown handler is removed. The worker is a daemon thread and ends with
|
||||
the process.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_worker.py`: an outgoing send completes through the
|
||||
worker thread, and the worker is still alive after an app context and a
|
||||
request end. Both fail without the fix.
|
||||
|
||||
## Production data
|
||||
|
||||
After deploying, reset webmention 1 to `validation_status = 'pending'` and the
|
||||
two queue items to `retry_count = 0, next_retry_at = NULL` so the worker
|
||||
processes them again.
|
||||
@@ -0,0 +1,36 @@
|
||||
# v2.6.9: Webmention processing fixes
|
||||
|
||||
Found after v2.6.8 let the worker run in production, and by a review of the
|
||||
implementation against the W3C Webmention Recommendation.
|
||||
|
||||
## Fixes
|
||||
|
||||
1. **Author photo with alt text** (`webmention_processor.extract_author`).
|
||||
mf2py returns `u-photo` with an `alt` attribute as
|
||||
`{'value': url, 'alt': ...}`; the dict was bound to SQLite and the update
|
||||
failed. `_first_str()` reduces mf2 values to their `value`.
|
||||
2. **Endpoint discovery** (`WebmentionWorker._discover_endpoint`).
|
||||
`response.url` is an `httpx.URL`; `urljoin(str, URL)` raised `TypeError`,
|
||||
which was caught and returned as "no endpoint", so every target was marked
|
||||
`no_endpoint`. Pass `str(response.url)`. Fetch errors now propagate so the
|
||||
caller schedules a retry, instead of being treated as "no endpoint".
|
||||
3. **Duplicate processing.** gunicorn runs 4 processes, each with a worker,
|
||||
and queue items were claimed by SELECT then UPDATE. The worker now takes a
|
||||
non-blocking `flock` on `<data>/webmention-worker.lock` before processing;
|
||||
others wait and retry each poll, so one takes over if the holder dies.
|
||||
Orphan cleanup runs when the lock is acquired.
|
||||
4. **XSS (spec 4.1).** `author_url` and `author_photo` are kept only if they
|
||||
are absolute http(s) URLs; otherwise the source URL / no photo is used.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_worker.py`: the send test uses a real `httpx.Response`
|
||||
(the old `MagicMock` with a string URL hid fix 2), plus tests for the lock,
|
||||
an alt-text photo and a `javascript:` author URL. All fail on v2.6.8.
|
||||
|
||||
## Remaining spec gaps
|
||||
|
||||
Tracked separately: rel-token and document-order endpoint discovery, exact
|
||||
target match during verification, Accept header, re-sending on delete, 410
|
||||
for deleted notes, SSRF re-validation on redirects, and the others listed in
|
||||
the compliance review.
|
||||
@@ -0,0 +1,51 @@
|
||||
# v2.7.0: Webmention W3C compliance
|
||||
|
||||
Review of StarPunk's Webmention support against the W3C Recommendation
|
||||
(https://www.w3.org/TR/webmention/, 12 January 2017), and the changes made.
|
||||
v2.6.8 and v2.6.9 fixed the bugs that stopped webmentions working at all.
|
||||
|
||||
## Sending
|
||||
|
||||
| Section | Requirement | Change |
|
||||
|---|---|---|
|
||||
| 3.1.2 | Link header with rel=webmention first (MUST) | RFC 8288 parsing: several links per header, several headers, `rel` in any position, token list, case-insensitive |
|
||||
| 3.1.2 | Then first `<link>`/`<a>` rel=webmention in document order (MUST) | Single pass over `link` and `a` in document order; rel token match; elements without `href` skipped |
|
||||
| 3.1.2 | Relative URL resolved against target; empty href is the target; query kept (MUST) | `urljoin` against the final URL after redirects |
|
||||
| 3.1.2 | Follow redirects | `safe_get` follows up to 10, checking each hop |
|
||||
| 3.1.2 | User-Agent with "Webmention" (MAY) | `StarPunk/<version> Webmention (+site)` |
|
||||
| 3.1.3 | Form-encoded POST; any 2xx success (MUST) | Unchanged. Redirects not followed (httpx turned 301/302 POST into GET); 429 retried |
|
||||
| 3.1.4 | Re-discover endpoint on update (MUST) | Discovery cache removed |
|
||||
| 3.1.4 | Re-send to previously sent targets, including removed links (SHOULD) | `previous_targets()` from the queue, plus removed links; not capped |
|
||||
| 3.1.5 | Deleted post returns 410 (SHOULD), tombstone (SHOULD) | `public.note` aborts 410 for soft-deleted notes and hard-deleted notes that sent webmentions; `410.html` |
|
||||
| 3.1.5 | Re-send on delete (SHOULD) | `requeue_previous_webmentions()`; for a hard delete the rows are detached (`source_note_id = NULL`) first, since the foreign key cascades. Also on undelete |
|
||||
|
||||
## Receiving
|
||||
|
||||
| Section | Requirement | Change |
|
||||
|---|---|---|
|
||||
| 3.2 | 202 for async processing (MUST) | Unchanged |
|
||||
| 3.2.1 | Valid http(s) URLs; source ≠ target; 400 on error (MUST) | Unchanged |
|
||||
| 3.2.1 | Target is a valid resource (SHOULD) | Only published notes; others 400 |
|
||||
| 3.2.1 | Fragment ignored | Stripped before storing (no duplicate rows) |
|
||||
| 3.2.2 | GET source, follow redirects, limit redirects | `safe_get` |
|
||||
| 3.2.2 | Accept header (SHOULD) | Added |
|
||||
| 3.2.2 | Exact match of target, per media type (MUST) | HTML: every link attribute (`lxml iterlinks`) resolved against the final URL; JSON: any string value; `text/*`: substring; other types: no match. Fragments ignored |
|
||||
| 3.2.4 | Update existing data; 410 / no link removes it (SHOULD) | Unchanged in effect. Moderation decisions kept on update; transient errors keep a valid mention |
|
||||
|
||||
## Security (section 4)
|
||||
|
||||
- 4.1 XSS: author URL/photo must be http(s) (v2.6.9); snippets re-sanitized after truncation; Mentions list uses `striptags`.
|
||||
- 4.2 / 4.5: `safe_get` checks every redirect hop, checks all `getaddrinfo` addresses, blocks private, loopback, link-local, multicast, reserved, unspecified, CGNAT, `0.0.0.0/8` and IPv4-mapped forms; 1 MB and 10 s total limits.
|
||||
- 4.3: endpoint URL checked before POST, no redirects followed.
|
||||
|
||||
## Known limits
|
||||
|
||||
- Homepage mentions are refused rather than stored.
|
||||
- Discovery does not apply HTTP cache headers (5.3); targets are only
|
||||
fetched when a note is created, edited or deleted.
|
||||
- The 410 page is a generic tombstone without microformats.
|
||||
- `error` rows (unexpected failures) are not retried automatically.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_spec.py` (section-numbered), `tests/test_webmention_worker.py`.
|
||||
@@ -0,0 +1,36 @@
|
||||
# v2.7.2: Remaining Webmention SHOULDs
|
||||
|
||||
Follow-up to the v2.7.0 compliance review. Every SHOULD in the
|
||||
Recommendation was checked; two were not met.
|
||||
|
||||
## 4.5 DNS pinning
|
||||
|
||||
`validate_url_for_fetch()` now returns the checked address.
|
||||
`_pinned_request()` builds each request against that IP, with the original
|
||||
host in the `Host` header and, for https, in the `sni_hostname` extension.
|
||||
httpcore uses `sni_hostname` as `server_hostname` with `check_hostname`
|
||||
on, so the certificate is verified against the real hostname. Used by
|
||||
`safe_get()` (every redirect hop) and the new `safe_post()` (the webmention
|
||||
POST). IPv4 is preferred when a host has both families.
|
||||
|
||||
## 3.1.4 Displayed responses are an update
|
||||
|
||||
"If the source post displays responses... then sender SHOULD treat that as
|
||||
an update of the source URL and re-send any previously sent Webmentions."
|
||||
|
||||
`note_responses_changed()` re-queues a published note's previous webmentions.
|
||||
It is called when:
|
||||
|
||||
- the worker validates a webmention and what the note shows for it changed
|
||||
(`_display_state()` before and after: shown or not, type, author, snippet).
|
||||
A re-send from the source resets the row to `pending`; a previously valid
|
||||
row still counts as shown, so an unchanged re-validation sends nothing.
|
||||
This keeps two sites that both implement this from ping-ponging.
|
||||
- an admin approves, hides or deletes webmentions and a valid one changes
|
||||
between shown and not shown.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_webmention_spec.py`: pinning (URL, Host, SNI, one lookup per
|
||||
request), IPv4 preference, re-send on a new response, none on an unchanged
|
||||
one, re-send on moderation changes.
|
||||
@@ -0,0 +1,38 @@
|
||||
# v2.8.0: Microformats2 review
|
||||
|
||||
Review of StarPunk's published and consumed microformats against
|
||||
microformats2, h-entry, h-card, h-feed, h-cite, representative h-card
|
||||
parsing, the authorship spec and Post Type Discovery. Every page type was
|
||||
rendered, parsed with mf2py and checked with mf2util.
|
||||
|
||||
Posts (h-entry) were largely correct: no implied names, PTD right for
|
||||
note/article/reply/like/repost/photo, author h-card, u-url/u-uid,
|
||||
p-category, u-syndication.
|
||||
|
||||
## Fixes
|
||||
|
||||
| # | Problem | Fix |
|
||||
|---|---|---|
|
||||
| 1 | Replies were unattached `h-cite` children with a plain-text author | `u-comment h-cite`, `p-author h-card` (u-url on an inner link, u-photo, p-name), `e-content` (`templates/partials/responses.html`) |
|
||||
| 2 | Likes/reposts/bookmarks/mentions had no mf2 | `u-like` / `u-repost` / `u-bookmark` / `u-mention` h-cites; faces past 8 are `hidden`, not omitted |
|
||||
| 3 | `dt-*` naive, with microseconds | `iso_utc` filter/helper: RFC 3339 UTC `Z`, no fraction; Micropub `q=source` too |
|
||||
| 4 | `#hashtag` first line = title; bare `#` = slug title | `split_title` requires an ATX heading (`#{1,6}` + space); templates use `Note.has_title` |
|
||||
| 5 | Note's own syndicated copy shown as a mention | Worker rejects a source whose url/uid (or URL) is one of the note's `syndications.remote_url` as invalid |
|
||||
| 6 | Micropub/IndieAuth links gated on WEBMENTION_ENABLED | Only `rel=webmention` is gated |
|
||||
| 7 | No rel=me on a fresh install | `rel=me` and `rel=author` to `author.url or author.me or ADMIN_ME`, always |
|
||||
| 8 | Author discovery guessed h-cards | `mf2_utils.representative_hcard` (uid+url, rel=me, single h-card); parsed with the URL after redirects |
|
||||
| 9 | Webmention entry/author/time | `mf2_utils.responding_entry` (entry whose in-reply-to/like-of/... or content refers to the target), `entry_author` (author property as h-card/URL/name, parent h-feed author, rel=author, representative h-card), `source_post_url` and `source_published` stored (migration 014) |
|
||||
|
||||
Also: tag and likes h-feeds have `u-url` and `p-author`; previews have `u-uid`.
|
||||
|
||||
## Not done
|
||||
|
||||
- Reply-context h-cite does not carry the cached author h-card, name or published time.
|
||||
- Photo alt falls back to "Image" when a photo-only post has no caption.
|
||||
- Person tags (`p-category h-card`) are not supported.
|
||||
- Existing webmentions keep NULL source details until they are re-validated.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_microformats_v280.py`. `tests/test_microformats.py`
|
||||
`test_no_relme_without_author` became `test_relme_to_admin_me_without_author`.
|
||||
@@ -0,0 +1,33 @@
|
||||
# v2.8.1: Migration lock held for the whole run
|
||||
|
||||
## Incident
|
||||
|
||||
Deploying v2.8.0 (migration 014 adds two columns): one gunicorn worker
|
||||
applied 014, another failed with `duplicate column name: source_post_url`,
|
||||
gunicorn shut down ("Worker failed to boot") and Docker restarted the
|
||||
container. About two seconds of downtime; schema correct afterwards.
|
||||
|
||||
## Causes
|
||||
|
||||
`run_migrations` takes `BEGIN IMMEDIATE` so one worker migrates at a time,
|
||||
but the lock was released early twice:
|
||||
|
||||
1. `create_migrations_table()`, called right after `BEGIN IMMEDIATE`,
|
||||
ended with `conn.commit()`. Everything after it ran without the lock.
|
||||
2. `conn.executescript()` COMMITs any open transaction before running the
|
||||
script, so the lock was dropped before the migration was recorded in
|
||||
`schema_migrations`.
|
||||
|
||||
## Fix
|
||||
|
||||
- `create_migrations_table(conn, commit=True)`; `run_migrations` passes
|
||||
`commit=False`.
|
||||
- `execute_in_transaction()` runs each statement from
|
||||
`split_sql_statements()` (built on `sqlite3.complete_statement`, so
|
||||
trigger bodies stay whole) with `conn.execute`, inside the transaction.
|
||||
|
||||
## Verification
|
||||
|
||||
Four processes migrating a database at 013 simultaneously: 6/30 trials
|
||||
failed before, 0/30 after. `tests/test_migration_concurrency.py` runs this
|
||||
five times, plus a rollback test showing a migration commits nothing early.
|
||||
@@ -0,0 +1,51 @@
|
||||
# v2.9.0: Micropub review
|
||||
|
||||
Review against the W3C Micropub Recommendation (https://www.w3.org/TR/micropub/),
|
||||
run through the Flask test client and mapped to the micropub.rocks server
|
||||
tests. Everyday create/update/delete/query/media/auth already passed; the
|
||||
gaps were privacy, security and silent data loss.
|
||||
|
||||
## Decisions (user)
|
||||
|
||||
- No `mp-syndicate-to` from a Micropub client means no crossposting
|
||||
(was: all targets, v2.1.0). The admin editor's default is unchanged.
|
||||
- `visibility` private/unlisted are saved as drafts.
|
||||
- Rendered HTML is sanitized with an allowlist (nh3 defaults plus class,
|
||||
title, id, lang, dir on all tags; video/audio/source/track/picture).
|
||||
|
||||
## Changes
|
||||
|
||||
| Area | Before | After |
|
||||
|---|---|---|
|
||||
| `mp-*` in JSON `properties` | ignored (all targets, timestamp slug) | read as commands, never stored |
|
||||
| absent `mp-syndicate-to` | all targets | none |
|
||||
| `q=source` / `q=category` | any token | create, update, delete or draft scope |
|
||||
| HTML content | rendered raw (stored XSS) | `render_markdown()` = Markdown + nh3 |
|
||||
| `visibility` | ignored (published) | private/unlisted -> draft; unknown -> 400 |
|
||||
| `draft` scope | 403 | creates drafts only |
|
||||
| photo fetch/lookup failure | dropped, 201 | 400, nothing created or changed |
|
||||
| remote photo fetch | first host checked, redirects followed | `webmention_ssrf.safe_get` (per hop, pinned) |
|
||||
| JSON scalars | indexed per character | wrapped on create; update `delete` values must be arrays |
|
||||
| `bookmark-of` | dropped | `notes.bookmark_of`, kind `bookmark` (migration 015) |
|
||||
| HTML-authored content in `q=source` | string | `{"html": ...}` via `notes.content_format` |
|
||||
| `summary` / `name` only | 400 | used as text / titled post |
|
||||
| `video` / `audio` | dropped | 400 |
|
||||
| `published` | stored with offset; garbage -> now | naive UTC; garbage -> 400 |
|
||||
| malformed JSON | 500 | 400 |
|
||||
| update without operations | 204 | 400 |
|
||||
| auth | `Bearer` only; header+body accepted; no challenge | case-insensitive; both -> 400; `WWW-Authenticate` |
|
||||
|
||||
## Kept as is
|
||||
|
||||
- 403 (RFC 6750) for insufficient_scope; the Micropub table says 401.
|
||||
- `add` only for category and photo; `published`, `syndication`,
|
||||
`location` not updatable.
|
||||
- `location`/`checkin` ignored (3.3.5 allows).
|
||||
- GET `access_token` query parameter still accepted.
|
||||
|
||||
## Tests
|
||||
|
||||
`tests/test_micropub_v290.py` (31 of 34 fail on v2.8.1). Updated:
|
||||
likes/reposts helper sends `mp-syndicate-to` in properties;
|
||||
`test_micropub_default_is_no_targets`; remote photo test uses the pinned
|
||||
fetcher; corrupted-image message.
|
||||
@@ -0,0 +1,79 @@
|
||||
-- Migration 010: Add Webmention support
|
||||
-- Version: 1.6.0
|
||||
-- Per ADR-063: Webmention Implementation
|
||||
-- Per W3C Webmention Recommendation
|
||||
|
||||
-- Add reply-to support to notes table
|
||||
ALTER TABLE notes ADD COLUMN in_reply_to TEXT;
|
||||
|
||||
-- Incoming webmentions storage
|
||||
CREATE TABLE IF NOT EXISTS webmentions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
source_url TEXT NOT NULL,
|
||||
target_url TEXT NOT NULL,
|
||||
target_note_id INTEGER,
|
||||
mention_type TEXT NOT NULL DEFAULT 'mention'
|
||||
CHECK (mention_type IN ('reply', 'like', 'repost', 'bookmark', 'mention')),
|
||||
author_name TEXT,
|
||||
author_url TEXT,
|
||||
author_photo TEXT,
|
||||
content_snippet TEXT,
|
||||
validation_status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (validation_status IN ('pending', 'valid', 'invalid', 'error')),
|
||||
moderation_status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (moderation_status IN ('pending', 'approved', 'hidden', 'deleted')),
|
||||
validation_error TEXT,
|
||||
validated_at TIMESTAMP,
|
||||
received_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (target_note_id) REFERENCES notes(id) ON DELETE SET NULL,
|
||||
UNIQUE(source_url, target_url)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_webmentions_target_note ON webmentions(target_note_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_webmentions_moderation ON webmentions(moderation_status);
|
||||
CREATE INDEX IF NOT EXISTS idx_webmentions_validation ON webmentions(validation_status);
|
||||
CREATE INDEX IF NOT EXISTS idx_webmentions_received ON webmentions(received_at DESC);
|
||||
|
||||
-- Outgoing webmention queue
|
||||
CREATE TABLE IF NOT EXISTS webmention_queue (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
source_url TEXT NOT NULL,
|
||||
target_url TEXT NOT NULL,
|
||||
source_note_id INTEGER,
|
||||
endpoint_url TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (status IN ('pending', 'sending', 'sent', 'failed', 'no_endpoint')),
|
||||
retry_count INTEGER NOT NULL DEFAULT 0,
|
||||
next_retry_at TIMESTAMP,
|
||||
last_error TEXT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
started_at TIMESTAMP,
|
||||
sent_at TIMESTAMP,
|
||||
FOREIGN KEY (source_note_id) REFERENCES notes(id) ON DELETE CASCADE,
|
||||
UNIQUE(source_url, target_url)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_webmention_queue_status ON webmention_queue(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_webmention_queue_retry ON webmention_queue(next_retry_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_webmention_queue_note ON webmention_queue(source_note_id);
|
||||
|
||||
-- Cached reply contexts
|
||||
CREATE TABLE IF NOT EXISTS reply_contexts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
url TEXT UNIQUE NOT NULL,
|
||||
title TEXT,
|
||||
content_snippet TEXT,
|
||||
published_at TIMESTAMP,
|
||||
author_name TEXT,
|
||||
author_url TEXT,
|
||||
author_photo TEXT,
|
||||
site_name TEXT,
|
||||
fetch_status TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (fetch_status IN ('pending', 'fetched', 'error')),
|
||||
fetch_error TEXT,
|
||||
fetched_at TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_reply_contexts_url ON reply_contexts(url);
|
||||
@@ -0,0 +1,49 @@
|
||||
-- Migration 011: OIDC admin login + built-in IndieAuth server
|
||||
-- Version: 2.0.0
|
||||
-- Per ADR-064: Replace IndieAuth login with OIDC, issue Micropub tokens locally
|
||||
--
|
||||
-- New tables only. The legacy auth_state, tokens and authorization_codes
|
||||
-- tables are left alone: INITIAL_SCHEMA_SQL recreates tokens and
|
||||
-- authorization_codes on every start while migration 004 drops them, so
|
||||
-- nothing new can depend on them.
|
||||
|
||||
-- Pending OIDC logins (state, nonce and PKCE verifier for one round trip)
|
||||
CREATE TABLE IF NOT EXISTS oidc_login_state (
|
||||
state TEXT PRIMARY KEY,
|
||||
nonce TEXT NOT NULL,
|
||||
code_verifier TEXT NOT NULL,
|
||||
next_url TEXT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_oidc_login_state_expires ON oidc_login_state(expires_at);
|
||||
|
||||
-- Authorization codes issued to IndieAuth (Micropub) clients
|
||||
CREATE TABLE IF NOT EXISTS indieauth_codes (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
code_hash TEXT UNIQUE NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
redirect_uri TEXT NOT NULL,
|
||||
scope TEXT NOT NULL DEFAULT '',
|
||||
code_challenge TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
used_at TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_indieauth_codes_expires ON indieauth_codes(expires_at);
|
||||
|
||||
-- Access tokens issued to IndieAuth (Micropub) clients
|
||||
CREATE TABLE IF NOT EXISTS indieauth_tokens (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
token_hash TEXT UNIQUE NOT NULL,
|
||||
client_id TEXT NOT NULL,
|
||||
scope TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
last_used_at TIMESTAMP,
|
||||
revoked_at TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_indieauth_tokens_expires ON indieauth_tokens(expires_at);
|
||||
@@ -0,0 +1,29 @@
|
||||
-- Migration 012: Crossposting (POSSE) to syndication targets
|
||||
-- Version: 2.1.0
|
||||
-- Per ADR-065: Direct syndication to GoToSocial / Mastodon-API servers
|
||||
--
|
||||
-- One row per (note, target). The worker reconciles the remote post with the
|
||||
-- note: create, update, or delete as needed. note_id has no foreign key on
|
||||
-- purpose: a row must outlive a hard-deleted note so the remote post can be
|
||||
-- deleted too.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS syndications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
note_id INTEGER NOT NULL,
|
||||
target TEXT NOT NULL,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
remote_id TEXT,
|
||||
remote_url TEXT,
|
||||
state TEXT NOT NULL DEFAULT 'pending'
|
||||
CHECK (state IN ('pending', 'synced', 'failed')),
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
last_error TEXT,
|
||||
next_attempt_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
claimed_until TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE (note_id, target)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_syndications_due ON syndications(state, next_attempt_at);
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Migration 013: Likes and reposts (v2.5.0, ADR-068)
|
||||
-- A note with like_of is a like; with repost_of, a repost. Content is the
|
||||
-- optional comment and may be empty.
|
||||
|
||||
ALTER TABLE notes ADD COLUMN like_of TEXT;
|
||||
ALTER TABLE notes ADD COLUMN repost_of TEXT;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_notes_like_of ON notes(like_of);
|
||||
@@ -0,0 +1,6 @@
|
||||
-- Migration 014: Source details for received webmentions (v2.8.0)
|
||||
-- The source's own u-url and dt-published, so a response links to and dates
|
||||
-- the original post rather than the URL that sent it and when it arrived.
|
||||
|
||||
ALTER TABLE webmentions ADD COLUMN source_post_url TEXT;
|
||||
ALTER TABLE webmentions ADD COLUMN source_published TIMESTAMP;
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Migration 015: Bookmarks and HTML-authored content (v2.9.0)
|
||||
-- bookmark_of: URL bookmarked (Micropub bookmark-of); the note is a bookmark.
|
||||
-- content_format: 'html' when a Micropub client sent the content as HTML,
|
||||
-- so q=source returns it as {"html": ...} (Micropub 3.7.2). NULL = Markdown.
|
||||
|
||||
ALTER TABLE notes ADD COLUMN bookmark_of TEXT;
|
||||
ALTER TABLE notes ADD COLUMN content_format TEXT;
|
||||
@@ -0,0 +1,55 @@
|
||||
-- Migration 016: WebSub hub (v2.10.0, ADR-071)
|
||||
|
||||
-- Active and pending subscriptions to this site's topics
|
||||
CREATE TABLE IF NOT EXISTS websub_subscriptions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
topic TEXT NOT NULL,
|
||||
callback TEXT NOT NULL,
|
||||
secret TEXT,
|
||||
lease_seconds INTEGER NOT NULL,
|
||||
expires_at TIMESTAMP NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
verified_at TIMESTAMP,
|
||||
last_success_at TIMESTAMP,
|
||||
last_error TEXT,
|
||||
UNIQUE(topic, callback)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_websub_subscriptions_topic ON websub_subscriptions(topic);
|
||||
|
||||
-- Subscribe/unsubscribe requests waiting for verification of intent
|
||||
CREATE TABLE IF NOT EXISTS websub_intents (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
topic TEXT NOT NULL,
|
||||
callback TEXT NOT NULL,
|
||||
mode TEXT NOT NULL CHECK (mode IN ('subscribe', 'unsubscribe')),
|
||||
secret TEXT,
|
||||
lease_seconds INTEGER,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
next_attempt_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE(topic, callback)
|
||||
);
|
||||
|
||||
-- Content to push: one row per subscription (built-in hub) or per topic
|
||||
-- (ping to an external hub, subscription_id NULL)
|
||||
CREATE TABLE IF NOT EXISTS websub_deliveries (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
subscription_id INTEGER REFERENCES websub_subscriptions(id) ON DELETE CASCADE,
|
||||
topic TEXT NOT NULL,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
next_attempt_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_error TEXT,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_websub_deliveries_subscription
|
||||
ON websub_deliveries(subscription_id) WHERE subscription_id IS NOT NULL;
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_websub_deliveries_ping
|
||||
ON websub_deliveries(topic) WHERE subscription_id IS NULL;
|
||||
|
||||
-- Site-wide values. content_version changes whenever a public note does, so
|
||||
-- every gunicorn process knows its cached feed note list is stale.
|
||||
CREATE TABLE IF NOT EXISTS site_state (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
INSERT OR IGNORE INTO site_state (key, value) VALUES ('content_version', '0');
|
||||
@@ -0,0 +1,12 @@
|
||||
-- Migration 017: WebSub hardening (v2.10.1)
|
||||
-- request_id: each subscription request gets its own id, so verifying an
|
||||
-- older request can't consume a newer one for the same topic and callback.
|
||||
-- requested_topic: the hub.topic exactly as sent; verification echoes it.
|
||||
-- callback_ip: address the callback resolved to, for per-address limits.
|
||||
|
||||
ALTER TABLE websub_intents ADD COLUMN request_id TEXT;
|
||||
ALTER TABLE websub_intents ADD COLUMN requested_topic TEXT;
|
||||
ALTER TABLE websub_intents ADD COLUMN callback_ip TEXT;
|
||||
ALTER TABLE websub_subscriptions ADD COLUMN callback_ip TEXT;
|
||||
CREATE INDEX IF NOT EXISTS idx_websub_subscriptions_ip ON websub_subscriptions(callback_ip);
|
||||
CREATE INDEX IF NOT EXISTS idx_websub_intents_ip ON websub_intents(callback_ip);
|
||||
@@ -36,3 +36,7 @@ Pillow==10.1.*
|
||||
|
||||
# HEIC/HEIF Support (v1.4.2 - iPhone photos)
|
||||
pillow-heif==0.18.*
|
||||
|
||||
# HTML Sanitization (v1.6.0 - Webmention content)
|
||||
nh3==0.2.*
|
||||
PyJWT[crypto]==2.10.*
|
||||
+147
-9
@@ -156,6 +156,10 @@ def create_app(config=None):
|
||||
configure_cache(max_size=max_size, ttl=ttl)
|
||||
app.logger.info(f"Feed cache enabled (max_size={max_size}, ttl={ttl}s)")
|
||||
|
||||
# Initialize rate limiters (v1.6.0 Phase 2)
|
||||
from starpunk.rate_limiter import init_rate_limiters
|
||||
init_rate_limiters(app)
|
||||
|
||||
# Initialize FTS index if needed
|
||||
from pathlib import Path
|
||||
from starpunk.search import has_fts_table, rebuild_fts_index
|
||||
@@ -187,6 +191,10 @@ def create_app(config=None):
|
||||
register_routes(app)
|
||||
|
||||
# Template context processor - Inject author profile (v1.2.0 Phase 2)
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
app.jinja_env.globals["absolute_url_for"] = absolute_url_for
|
||||
|
||||
@app.context_processor
|
||||
def inject_author():
|
||||
"""
|
||||
@@ -211,12 +219,101 @@ def create_app(config=None):
|
||||
|
||||
return {'author': author}
|
||||
|
||||
# Template filter - RFC 3339 UTC for dt-* properties (v2.8.0)
|
||||
from starpunk.utils import iso_utc
|
||||
app.add_template_filter(iso_utc, 'iso_utc')
|
||||
|
||||
# Template filter - URL to color for facepile (v1.6.0)
|
||||
@app.template_filter('url_to_color')
|
||||
def url_to_color_filter(url):
|
||||
"""
|
||||
Generate consistent color from URL hash for facepile initials
|
||||
|
||||
Per display-moderation.md design: Hash URL to generate deterministic
|
||||
color for facepile fallback initials.
|
||||
|
||||
Args:
|
||||
url: Author URL or any string
|
||||
|
||||
Returns:
|
||||
CSS color string (hex format)
|
||||
"""
|
||||
import hashlib
|
||||
|
||||
if not url:
|
||||
return '#6B7280' # Gray fallback
|
||||
|
||||
# Hash the URL to get consistent color
|
||||
hash_value = int(hashlib.md5(url.encode()).hexdigest()[:6], 16)
|
||||
|
||||
# Generate pleasant, readable colors
|
||||
# Use HSL-style generation for better color distribution
|
||||
hue = hash_value % 360
|
||||
saturation = 65 # Medium saturation for readability
|
||||
lightness = 50 # Medium lightness
|
||||
|
||||
# Convert HSL to RGB (simplified)
|
||||
def hsl_to_rgb(h, s, l):
|
||||
s = s / 100.0
|
||||
l = l / 100.0
|
||||
c = (1 - abs(2 * l - 1)) * s
|
||||
x = c * (1 - abs((h / 60) % 2 - 1))
|
||||
m = l - c / 2
|
||||
|
||||
if h < 60:
|
||||
r, g, b = c, x, 0
|
||||
elif h < 120:
|
||||
r, g, b = x, c, 0
|
||||
elif h < 180:
|
||||
r, g, b = 0, c, x
|
||||
elif h < 240:
|
||||
r, g, b = 0, x, c
|
||||
elif h < 300:
|
||||
r, g, b = x, 0, c
|
||||
else:
|
||||
r, g, b = c, 0, x
|
||||
|
||||
return int((r + m) * 255), int((g + m) * 255), int((b + m) * 255)
|
||||
|
||||
r, g, b = hsl_to_rgb(hue, saturation, lightness)
|
||||
return f'#{r:02x}{g:02x}{b:02x}'
|
||||
|
||||
# Request middleware - Add correlation ID to each request
|
||||
@app.before_request
|
||||
def before_request():
|
||||
"""Add correlation ID to request context for tracing"""
|
||||
add_correlation_id()
|
||||
|
||||
# Know who is logged in on every page, not just /admin (v2.3.2), so
|
||||
# the header can say "Admin" instead of "Log in". Only requests that
|
||||
# carry a session cookie touch the database.
|
||||
from flask import g, request
|
||||
token = request.cookies.get("starpunk_session")
|
||||
if token:
|
||||
from starpunk.auth import verify_session
|
||||
try:
|
||||
info = verify_session(token)
|
||||
except Exception:
|
||||
info = None
|
||||
if info:
|
||||
g.user = info
|
||||
g.me = info["me"]
|
||||
|
||||
# Response middleware - Add webmention Link header (v1.6.0)
|
||||
@app.after_request
|
||||
def add_webmention_headers(response):
|
||||
"""Add webmention Link header to HTML responses"""
|
||||
if (app.config.get('WEBMENTION_ENABLED', True) and
|
||||
response.content_type and
|
||||
'text/html' in response.content_type):
|
||||
response.headers.add('Link', '</webmention>; rel="webmention"')
|
||||
return response
|
||||
|
||||
# WebSub discovery: Link rel=hub and rel=self on topics (v2.10.0)
|
||||
from starpunk.websub import add_discovery_headers, template_links
|
||||
app.after_request(add_discovery_headers)
|
||||
app.add_template_global(template_links, 'websub_links')
|
||||
|
||||
# Register centralized error handlers
|
||||
from starpunk.errors import register_error_handlers
|
||||
|
||||
@@ -230,12 +327,44 @@ def create_app(config=None):
|
||||
memory_monitor.start()
|
||||
app.memory_monitor = memory_monitor
|
||||
app.logger.info(f"Memory monitor started (interval={memory_monitor.interval}s)")
|
||||
# No teardown_appcontext stop: that ran after every request and stopped
|
||||
# the monitor straight away (v2.10.0). It is a daemon thread.
|
||||
|
||||
# Register cleanup handler
|
||||
@app.teardown_appcontext
|
||||
def cleanup_memory_monitor(error=None):
|
||||
if hasattr(app, 'memory_monitor') and app.memory_monitor.is_alive():
|
||||
app.memory_monitor.stop()
|
||||
# Start webmention worker thread (v1.6.0 Phase 2)
|
||||
# Per background-worker.md: Skip in test mode
|
||||
if app.config.get('WEBMENTION_WORKER_ENABLED', True) and not app.config.get('TESTING', False):
|
||||
from starpunk.webmention_worker import WebmentionWorker
|
||||
webmention_worker = WebmentionWorker(
|
||||
app,
|
||||
db_path=app.config['DATABASE_PATH'],
|
||||
site_url=app.config['SITE_URL'],
|
||||
poll_interval=app.config.get('WEBMENTION_POLL_INTERVAL', 10),
|
||||
error_backoff=app.config.get('WEBMENTION_ERROR_BACKOFF', 60),
|
||||
batch_size=app.config.get('WEBMENTION_BATCH_SIZE', 5),
|
||||
auto_approve=app.config.get('WEBMENTION_AUTO_APPROVE', True)
|
||||
)
|
||||
webmention_worker.start()
|
||||
app.webmention_worker = webmention_worker
|
||||
app.logger.info(
|
||||
f"Webmention worker started "
|
||||
f"(poll_interval={webmention_worker.poll_interval}s, "
|
||||
f"batch_size={webmention_worker.batch_size})"
|
||||
)
|
||||
|
||||
# Start WebSub worker (v2.10.0)
|
||||
from starpunk.websub_worker import start_worker
|
||||
app.websub_worker = start_worker(app)
|
||||
if app.websub_worker:
|
||||
app.logger.info("WebSub worker started")
|
||||
|
||||
# Start syndication worker when a target is configured (v2.1.0)
|
||||
from starpunk.syndication import SyndicationWorker, targets_configured
|
||||
if targets_configured(app.config) and not app.config.get("TESTING", False):
|
||||
app.syndication_worker = SyndicationWorker(
|
||||
app, poll_interval=app.config["SYNDICATION_POLL_INTERVAL"]
|
||||
)
|
||||
app.syndication_worker.start()
|
||||
app.logger.info("Syndication worker started")
|
||||
|
||||
# Health check endpoint for containers and monitoring
|
||||
@app.route("/health")
|
||||
@@ -322,15 +451,24 @@ def create_app(config=None):
|
||||
else:
|
||||
# Basic mode - just return 200 OK (for load balancers)
|
||||
# No authentication required, minimal checks
|
||||
return jsonify({
|
||||
response = {
|
||||
"status": "ok",
|
||||
"version": app.config.get("VERSION", __version__)
|
||||
}), 200
|
||||
}
|
||||
|
||||
# Add webmention worker stats if enabled (v1.6.0 Phase 2)
|
||||
if hasattr(app, 'webmention_worker'):
|
||||
try:
|
||||
response['webmention_worker'] = app.webmention_worker.get_stats()
|
||||
except Exception as e:
|
||||
app.logger.debug(f"Failed to get webmention worker stats: {e}")
|
||||
|
||||
return jsonify(response), 200
|
||||
|
||||
return app
|
||||
|
||||
|
||||
# Package version (Semantic Versioning 2.0.0)
|
||||
# See docs/standards/versioning-strategy.md for details
|
||||
__version__ = "1.5.0"
|
||||
__version_info__ = (1, 5, 0)
|
||||
__version__ = "2.10.1"
|
||||
__version_info__ = (2, 10, 1)
|
||||
+233
-395
@@ -1,20 +1,21 @@
|
||||
"""
|
||||
Authentication module for StarPunk
|
||||
|
||||
Implements IndieLogin authentication for admin access using indielogin.com
|
||||
as a delegated authentication provider. No passwords are stored locally.
|
||||
Admin login uses OpenID Connect (authorization code flow with PKCE) against
|
||||
a configured provider such as Pocket ID. Exactly one OIDC subject
|
||||
(OIDC_ADMIN_SUB) may log in; a successful login creates a local session
|
||||
for the site owner's identity URL (ADMIN_ME). No passwords are stored.
|
||||
|
||||
Security features:
|
||||
- CSRF protection via state tokens
|
||||
- Secure session tokens (cryptographically random)
|
||||
- Token hashing in database (SHA-256)
|
||||
- CSRF protection via single-use state tokens
|
||||
- PKCE (S256) and nonce binding of the ID token to the login attempt
|
||||
- ID token signature, issuer, audience and expiry checks
|
||||
- Session tokens hashed in the database (SHA-256)
|
||||
- HttpOnly, Secure, SameSite cookies
|
||||
- Single-admin authorization
|
||||
- Automatic session cleanup
|
||||
|
||||
Functions:
|
||||
initiate_login: Start IndieLogin authentication flow
|
||||
handle_callback: Process IndieLogin callback
|
||||
initiate_login: Start the OIDC login flow
|
||||
handle_callback: Finish the OIDC login flow and create a session
|
||||
create_session: Create authenticated session
|
||||
verify_session: Check if session is valid
|
||||
destroy_session: Logout and cleanup
|
||||
@@ -24,153 +25,50 @@ Exceptions:
|
||||
AuthError: Base authentication exception
|
||||
InvalidStateError: CSRF state validation failed
|
||||
UnauthorizedError: User not authorized as admin
|
||||
IndieLoginError: External service error
|
||||
OIDCError: Identity provider error
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import logging
|
||||
import secrets
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from functools import wraps
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict, Optional, Tuple
|
||||
from urllib.parse import urlencode
|
||||
|
||||
import httpx
|
||||
import jwt
|
||||
from flask import current_app, g, redirect, request, session, url_for
|
||||
|
||||
from starpunk.auth_external import discover_endpoints, DiscoveryError, normalize_url
|
||||
from starpunk.database import get_db
|
||||
from starpunk.utils import is_valid_url
|
||||
|
||||
# Cached provider metadata: {issuer: (fetched_at, metadata)}
|
||||
_METADATA_CACHE: Dict[str, Tuple[float, dict]] = {}
|
||||
_METADATA_TTL = 3600
|
||||
# One JWKS client per jwks_uri; PyJWKClient caches the keys itself
|
||||
_JWKS_CLIENTS: Dict[str, jwt.PyJWKClient] = {}
|
||||
|
||||
LOGIN_STATE_MINUTES = 10
|
||||
|
||||
|
||||
# Custom exceptions
|
||||
class AuthError(Exception):
|
||||
"""Base exception for authentication errors"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class InvalidStateError(AuthError):
|
||||
"""CSRF state validation failed"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class UnauthorizedError(AuthError):
|
||||
"""User not authorized as admin"""
|
||||
|
||||
pass
|
||||
|
||||
class OIDCError(AuthError):
|
||||
"""Identity provider error"""
|
||||
|
||||
|
||||
class IndieLoginError(AuthError):
|
||||
"""IndieLogin service error"""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# Logging helper functions
|
||||
def _redact_token(value: str, show_chars: int = 6) -> str:
|
||||
"""
|
||||
Redact sensitive token for logging
|
||||
|
||||
Shows first N and last 4 characters with asterisks in between.
|
||||
|
||||
Args:
|
||||
value: Token to redact
|
||||
show_chars: Number of characters to show at start (default: 6)
|
||||
|
||||
Returns:
|
||||
Redacted token string like "abc123...********...xyz9"
|
||||
"""
|
||||
if not value or len(value) <= (show_chars + 4):
|
||||
return "***REDACTED***"
|
||||
|
||||
return f"{value[:show_chars]}...{'*' * 8}...{value[-4:]}"
|
||||
|
||||
|
||||
def _log_http_request(method: str, url: str, data: dict, headers: dict = None) -> None:
|
||||
"""
|
||||
Log HTTP request details at DEBUG level
|
||||
|
||||
Automatically redacts sensitive parameters (code, state, authorization)
|
||||
|
||||
Args:
|
||||
method: HTTP method (GET, POST, etc.)
|
||||
url: Request URL
|
||||
data: Request data/parameters
|
||||
headers: Optional request headers
|
||||
"""
|
||||
if not current_app.logger.isEnabledFor(logging.DEBUG):
|
||||
return
|
||||
|
||||
# Redact sensitive data
|
||||
safe_data = data.copy()
|
||||
if "code" in safe_data:
|
||||
safe_data["code"] = _redact_token(safe_data["code"])
|
||||
if "state" in safe_data:
|
||||
safe_data["state"] = _redact_token(safe_data["state"], 8)
|
||||
if "code_verifier" in safe_data:
|
||||
safe_data["code_verifier"] = _redact_token(safe_data["code_verifier"])
|
||||
|
||||
current_app.logger.debug(
|
||||
f"IndieAuth HTTP Request:\n"
|
||||
f" Method: {method}\n"
|
||||
f" URL: {url}\n"
|
||||
f" Data: {safe_data}"
|
||||
)
|
||||
|
||||
if headers:
|
||||
safe_headers = {
|
||||
k: v
|
||||
for k, v in headers.items()
|
||||
if k.lower() not in ["authorization", "cookie"]
|
||||
}
|
||||
current_app.logger.debug(f" Headers: {safe_headers}")
|
||||
|
||||
|
||||
def _log_http_response(status_code: int, headers: dict, body: str) -> None:
|
||||
"""
|
||||
Log HTTP response details at DEBUG level
|
||||
|
||||
Automatically redacts sensitive response data
|
||||
|
||||
Args:
|
||||
status_code: HTTP status code
|
||||
headers: Response headers
|
||||
body: Response body (JSON string or text)
|
||||
"""
|
||||
if not current_app.logger.isEnabledFor(logging.DEBUG):
|
||||
return
|
||||
|
||||
# Parse and redact JSON body if present
|
||||
safe_body = body
|
||||
try:
|
||||
import json
|
||||
|
||||
data = json.loads(body)
|
||||
if "access_token" in data:
|
||||
data["access_token"] = _redact_token(data["access_token"])
|
||||
if "code" in data:
|
||||
data["code"] = _redact_token(data["code"])
|
||||
safe_body = json.dumps(data, indent=2)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
# Not JSON or parsing failed, log as-is (likely error message)
|
||||
pass
|
||||
|
||||
# Redact sensitive headers
|
||||
safe_headers = {
|
||||
k: v for k, v in headers.items() if k.lower() not in ["set-cookie", "authorization"]
|
||||
}
|
||||
|
||||
current_app.logger.debug(
|
||||
f"IndieAuth HTTP Response:\n"
|
||||
f" Status: {status_code}\n"
|
||||
f" Headers: {safe_headers}\n"
|
||||
f" Body: {safe_body}"
|
||||
)
|
||||
|
||||
|
||||
# Helper functions
|
||||
def _hash_token(token: str) -> str:
|
||||
"""
|
||||
Hash token using SHA-256
|
||||
@@ -185,323 +83,264 @@ def _hash_token(token: str) -> str:
|
||||
|
||||
|
||||
def _generate_state_token() -> str:
|
||||
"""
|
||||
Generate CSRF state token
|
||||
|
||||
Returns:
|
||||
URL-safe random token
|
||||
"""
|
||||
"""Generate a URL-safe random token"""
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def _verify_state_token(state: str) -> bool:
|
||||
def _pkce_challenge(verifier: str) -> str:
|
||||
"""S256 PKCE challenge for a verifier (RFC 7636)"""
|
||||
digest = hashlib.sha256(verifier.encode("ascii")).digest()
|
||||
return base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")
|
||||
|
||||
|
||||
def _redirect_uri() -> str:
|
||||
"""OIDC redirect URI registered with the provider"""
|
||||
return current_app.config["SITE_URL"].rstrip("/") + "/auth/callback"
|
||||
|
||||
|
||||
def _safe_next_url(next_url: Optional[str]) -> Optional[str]:
|
||||
"""Only allow redirects back to this site after login"""
|
||||
if not next_url:
|
||||
return None
|
||||
site = current_app.config["SITE_URL"].rstrip("/")
|
||||
if next_url.startswith(site + "/"):
|
||||
return next_url
|
||||
if next_url.startswith("/") and not next_url.startswith("//"):
|
||||
return next_url
|
||||
return None
|
||||
|
||||
|
||||
def _cleanup_expired_sessions() -> None:
|
||||
"""Remove expired sessions and pending login state"""
|
||||
db = get_db(current_app)
|
||||
db.execute("DELETE FROM sessions WHERE expires_at <= datetime('now')")
|
||||
db.execute("DELETE FROM oidc_login_state WHERE expires_at <= datetime('now')")
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_oidc_metadata() -> dict:
|
||||
"""
|
||||
Verify and consume CSRF state token.
|
||||
Fetch (and cache) the provider's OpenID configuration
|
||||
|
||||
Raises:
|
||||
OIDCError: Discovery failed or the issuer does not match
|
||||
"""
|
||||
issuer = current_app.config.get("OIDC_ISSUER", "").rstrip("/")
|
||||
if not issuer:
|
||||
raise OIDCError("OIDC_ISSUER is not configured")
|
||||
|
||||
cached = _METADATA_CACHE.get(issuer)
|
||||
if cached and time.time() - cached[0] < _METADATA_TTL:
|
||||
return cached[1]
|
||||
|
||||
url = f"{issuer}/.well-known/openid-configuration"
|
||||
try:
|
||||
response = httpx.get(url, timeout=10.0)
|
||||
response.raise_for_status()
|
||||
metadata = response.json()
|
||||
except (httpx.HTTPError, ValueError) as e:
|
||||
raise OIDCError(f"OIDC discovery failed: {e}")
|
||||
|
||||
if metadata.get("issuer", "").rstrip("/") != issuer:
|
||||
raise OIDCError(
|
||||
f"OIDC issuer mismatch: expected {issuer}, got {metadata.get('issuer')}"
|
||||
)
|
||||
for key in ("authorization_endpoint", "token_endpoint", "jwks_uri"):
|
||||
if not metadata.get(key):
|
||||
raise OIDCError(f"OIDC metadata is missing {key}")
|
||||
|
||||
_METADATA_CACHE[issuer] = (time.time(), metadata)
|
||||
return metadata
|
||||
|
||||
|
||||
def _get_signing_key(metadata: dict, id_token: str):
|
||||
"""Resolve the key that signed an ID token from the provider's JWKS"""
|
||||
jwks_uri = metadata["jwks_uri"]
|
||||
client = _JWKS_CLIENTS.get(jwks_uri)
|
||||
if client is None:
|
||||
client = jwt.PyJWKClient(jwks_uri, timeout=10)
|
||||
_JWKS_CLIENTS[jwks_uri] = client
|
||||
return client.get_signing_key_from_jwt(id_token).key
|
||||
|
||||
|
||||
def _validate_id_token(metadata: dict, id_token: str, nonce: str) -> dict:
|
||||
"""
|
||||
Verify the ID token signature and standard claims (OIDC Core 3.1.3.7)
|
||||
|
||||
Raises:
|
||||
OIDCError: Token is invalid
|
||||
"""
|
||||
algorithms = metadata.get("id_token_signing_alg_values_supported") or ["RS256"]
|
||||
algorithms = [alg for alg in algorithms if alg != "none"]
|
||||
try:
|
||||
key = _get_signing_key(metadata, id_token)
|
||||
claims = jwt.decode(
|
||||
id_token,
|
||||
key,
|
||||
algorithms=algorithms,
|
||||
audience=current_app.config["OIDC_CLIENT_ID"],
|
||||
issuer=metadata["issuer"],
|
||||
options={"require": ["exp", "iat", "sub", "iss", "aud"]},
|
||||
leeway=60,
|
||||
)
|
||||
except jwt.PyJWTError as e:
|
||||
raise OIDCError(f"Invalid ID token: {e}")
|
||||
|
||||
if not nonce or not secrets.compare_digest(str(claims.get("nonce", "")), nonce):
|
||||
raise OIDCError("ID token nonce does not match login request")
|
||||
|
||||
return claims
|
||||
|
||||
|
||||
# Core authentication functions
|
||||
def initiate_login(next_url: Optional[str] = None) -> str:
|
||||
"""
|
||||
Start the OIDC authorization code flow
|
||||
|
||||
Args:
|
||||
state: State token to verify
|
||||
next_url: Where to send the admin after login (same site only)
|
||||
|
||||
Returns:
|
||||
True if valid, False if invalid or expired
|
||||
"""
|
||||
db = get_db(current_app)
|
||||
Redirect URL to the provider's authorization endpoint
|
||||
|
||||
# Check if state exists and not expired
|
||||
result = db.execute(
|
||||
Raises:
|
||||
OIDCError: Provider discovery failed
|
||||
"""
|
||||
metadata = get_oidc_metadata()
|
||||
|
||||
state = _generate_state_token()
|
||||
nonce = secrets.token_urlsafe(32)
|
||||
code_verifier = secrets.token_urlsafe(64)
|
||||
|
||||
db = get_db(current_app)
|
||||
db.execute(
|
||||
"""
|
||||
SELECT 1 FROM auth_state
|
||||
INSERT INTO oidc_login_state (state, nonce, code_verifier, next_url, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
state,
|
||||
nonce,
|
||||
code_verifier,
|
||||
_safe_next_url(next_url),
|
||||
datetime.utcnow() + timedelta(minutes=LOGIN_STATE_MINUTES),
|
||||
),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
params = {
|
||||
"response_type": "code",
|
||||
"client_id": current_app.config["OIDC_CLIENT_ID"],
|
||||
"redirect_uri": _redirect_uri(),
|
||||
"scope": "openid",
|
||||
"state": state,
|
||||
"nonce": nonce,
|
||||
"code_challenge": _pkce_challenge(code_verifier),
|
||||
"code_challenge_method": "S256",
|
||||
}
|
||||
|
||||
current_app.logger.info("Auth: OIDC login initiated")
|
||||
return f"{metadata['authorization_endpoint']}?{urlencode(params)}"
|
||||
|
||||
|
||||
def _consume_login_state(state: str) -> Optional[dict]:
|
||||
"""Fetch and delete a pending login (single use)"""
|
||||
db = get_db(current_app)
|
||||
row = db.execute(
|
||||
"""
|
||||
SELECT nonce, code_verifier, next_url FROM oidc_login_state
|
||||
WHERE state = ? AND expires_at > datetime('now')
|
||||
""",
|
||||
(state,),
|
||||
).fetchone()
|
||||
|
||||
if not result:
|
||||
return False
|
||||
|
||||
# Delete state (single-use)
|
||||
db.execute("DELETE FROM auth_state WHERE state = ?", (state,))
|
||||
db.execute("DELETE FROM oidc_login_state WHERE state = ?", (state,))
|
||||
db.commit()
|
||||
|
||||
return True
|
||||
return dict(row) if row else None
|
||||
|
||||
|
||||
def _cleanup_expired_sessions() -> None:
|
||||
"""Remove expired sessions and state tokens"""
|
||||
db = get_db(current_app)
|
||||
|
||||
# Delete expired sessions
|
||||
db.execute(
|
||||
"""
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at <= datetime('now')
|
||||
def handle_callback(
|
||||
code: str, state: str, iss: Optional[str] = None
|
||||
) -> Tuple[str, Optional[str]]:
|
||||
"""
|
||||
)
|
||||
|
||||
# Delete expired state tokens
|
||||
db.execute(
|
||||
"""
|
||||
DELETE FROM auth_state
|
||||
WHERE expires_at <= datetime('now')
|
||||
"""
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
|
||||
# Core authentication functions
|
||||
def initiate_login(me_url: str) -> str:
|
||||
"""
|
||||
Initiate IndieAuth authentication flow with endpoint discovery.
|
||||
|
||||
Per W3C IndieAuth spec, discovers authorization_endpoint from user's
|
||||
profile URL rather than using a hardcoded service.
|
||||
Finish the OIDC flow: exchange the code, verify the ID token, check
|
||||
the subject is the configured admin, and create a session.
|
||||
|
||||
Args:
|
||||
me_url: User's IndieWeb identity URL
|
||||
|
||||
Returns:
|
||||
Redirect URL to discovered authorization endpoint
|
||||
|
||||
Raises:
|
||||
ValueError: Invalid me_url format
|
||||
DiscoveryError: Failed to discover endpoints from profile
|
||||
"""
|
||||
# Validate URL format
|
||||
if not is_valid_url(me_url):
|
||||
raise ValueError(f"Invalid URL format: {me_url}")
|
||||
|
||||
current_app.logger.debug(f"Auth: Validating me URL: {me_url}")
|
||||
|
||||
# Discover authorization endpoint from user's profile URL
|
||||
# Per IndieAuth spec: clients MUST discover endpoints, not hardcode them
|
||||
try:
|
||||
endpoints = discover_endpoints(me_url)
|
||||
except DiscoveryError as e:
|
||||
current_app.logger.error(f"Auth: Endpoint discovery failed for {me_url}: {e}")
|
||||
raise
|
||||
|
||||
auth_endpoint = endpoints.get('authorization_endpoint')
|
||||
if not auth_endpoint:
|
||||
raise DiscoveryError(
|
||||
f"No authorization_endpoint found at {me_url}. "
|
||||
"Ensure your profile has IndieAuth link elements or headers."
|
||||
)
|
||||
|
||||
current_app.logger.info(f"Auth: Discovered authorization_endpoint: {auth_endpoint}")
|
||||
|
||||
# Generate CSRF state token
|
||||
state = _generate_state_token()
|
||||
current_app.logger.debug(f"Auth: Generated state token: {_redact_token(state, 8)}")
|
||||
|
||||
# Store state in database (5-minute expiry)
|
||||
db = get_db(current_app)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
redirect_uri = f"{current_app.config['SITE_URL']}auth/callback"
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO auth_state (state, expires_at, redirect_uri)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(state, expires_at, redirect_uri),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Build authorization URL
|
||||
# Per W3C IndieAuth spec: use response_type=id for authentication-only flow
|
||||
# (identity verification without access token). This allows code redemption
|
||||
# at the authorization_endpoint rather than requiring token_endpoint.
|
||||
params = {
|
||||
"me": me_url,
|
||||
"client_id": current_app.config["SITE_URL"],
|
||||
"redirect_uri": redirect_uri,
|
||||
"state": state,
|
||||
"response_type": "id",
|
||||
}
|
||||
|
||||
current_app.logger.debug(
|
||||
f"Auth: Building authorization URL with params:\n"
|
||||
f" me: {me_url}\n"
|
||||
f" client_id: {current_app.config['SITE_URL']}\n"
|
||||
f" redirect_uri: {redirect_uri}\n"
|
||||
f" state: {_redact_token(state, 8)}\n"
|
||||
f" response_type: id (authentication-only flow)"
|
||||
)
|
||||
|
||||
auth_url = f"{auth_endpoint}?{urlencode(params)}"
|
||||
|
||||
current_app.logger.debug(f"Auth: Complete authorization URL: {auth_url}")
|
||||
current_app.logger.info(f"Auth: Authentication initiated for {me_url}")
|
||||
|
||||
return auth_url
|
||||
|
||||
|
||||
def handle_callback(code: str, state: str, iss: Optional[str] = None) -> Optional[str]:
|
||||
"""
|
||||
Handle IndieAuth callback with endpoint discovery.
|
||||
|
||||
Discovers authorization_endpoint from ADMIN_ME profile and exchanges
|
||||
authorization code for identity verification.
|
||||
|
||||
Per IndieAuth spec: Authentication-only flows POST to the authorization
|
||||
endpoint (not token endpoint) and do not include grant_type.
|
||||
|
||||
Args:
|
||||
code: Authorization code from authorization server
|
||||
code: Authorization code from the provider
|
||||
state: CSRF state token
|
||||
iss: Issuer identifier (optional, for security validation)
|
||||
iss: Issuer from the redirect (RFC 9207), checked when present
|
||||
|
||||
Returns:
|
||||
Session token if successful, None otherwise
|
||||
(session_token, next_url)
|
||||
|
||||
Raises:
|
||||
InvalidStateError: State token validation failed
|
||||
UnauthorizedError: User not authorized as admin
|
||||
IndieLoginError: Code exchange failed
|
||||
UnauthorizedError: Subject is not the configured admin
|
||||
OIDCError: Provider or token error
|
||||
"""
|
||||
current_app.logger.debug(f"Auth: Verifying state token: {_redact_token(state, 8)}")
|
||||
|
||||
# Verify state token (CSRF protection)
|
||||
if not _verify_state_token(state):
|
||||
current_app.logger.warning(
|
||||
"Auth: Invalid state token received (possible CSRF or expired token)"
|
||||
)
|
||||
pending = _consume_login_state(state)
|
||||
if not pending:
|
||||
current_app.logger.warning("Auth: Invalid or expired OIDC state")
|
||||
raise InvalidStateError("Invalid or expired state token")
|
||||
|
||||
current_app.logger.debug("Auth: State token valid")
|
||||
metadata = get_oidc_metadata()
|
||||
|
||||
# Discover authorization endpoint from ADMIN_ME profile
|
||||
admin_me = current_app.config.get("ADMIN_ME")
|
||||
if not admin_me:
|
||||
current_app.logger.error("Auth: ADMIN_ME not configured")
|
||||
raise IndieLoginError("ADMIN_ME not configured")
|
||||
if iss and iss.rstrip("/") != metadata["issuer"].rstrip("/"):
|
||||
raise OIDCError(f"Unexpected issuer in callback: {iss}")
|
||||
|
||||
try:
|
||||
endpoints = discover_endpoints(admin_me)
|
||||
except DiscoveryError as e:
|
||||
current_app.logger.error(f"Auth: Endpoint discovery failed: {e}")
|
||||
raise IndieLoginError(f"Failed to discover endpoints: {e}")
|
||||
|
||||
# Use authorization_endpoint for authentication-only flow (identity verification)
|
||||
# Per IndieAuth spec: auth-only flows POST to authorization_endpoint, not token_endpoint
|
||||
auth_endpoint = endpoints.get('authorization_endpoint')
|
||||
if not auth_endpoint:
|
||||
raise IndieLoginError(
|
||||
f"No authorization_endpoint found at {admin_me}. "
|
||||
"Ensure your profile has IndieAuth endpoints configured."
|
||||
)
|
||||
|
||||
current_app.logger.debug(f"Auth: Using authorization_endpoint: {auth_endpoint}")
|
||||
|
||||
# Verify issuer if provided (security check - optional)
|
||||
if iss:
|
||||
current_app.logger.debug(f"Auth: Issuer provided: {iss}")
|
||||
|
||||
# Prepare code verification request
|
||||
# Note: grant_type is NOT included for authentication-only flows per IndieAuth spec
|
||||
token_exchange_data = {
|
||||
"code": code,
|
||||
"client_id": current_app.config["SITE_URL"],
|
||||
"redirect_uri": f"{current_app.config['SITE_URL']}auth/callback",
|
||||
}
|
||||
|
||||
# Log the request
|
||||
_log_http_request(
|
||||
method="POST",
|
||||
url=auth_endpoint,
|
||||
data=token_exchange_data,
|
||||
)
|
||||
|
||||
current_app.logger.debug(
|
||||
"Auth: Sending code verification request to authorization endpoint:\n"
|
||||
" Method: POST\n"
|
||||
" URL: %s\n"
|
||||
" Data: code=%s, client_id=%s, redirect_uri=%s",
|
||||
auth_endpoint,
|
||||
_redact_token(code),
|
||||
token_exchange_data["client_id"],
|
||||
token_exchange_data["redirect_uri"],
|
||||
)
|
||||
|
||||
# Exchange code for identity at authorization endpoint (authentication-only flow)
|
||||
try:
|
||||
response = httpx.post(
|
||||
auth_endpoint,
|
||||
data=token_exchange_data,
|
||||
metadata["token_endpoint"],
|
||||
data={
|
||||
"grant_type": "authorization_code",
|
||||
"code": code,
|
||||
"redirect_uri": _redirect_uri(),
|
||||
"code_verifier": pending["code_verifier"],
|
||||
},
|
||||
auth=(
|
||||
current_app.config["OIDC_CLIENT_ID"],
|
||||
current_app.config["OIDC_CLIENT_SECRET"],
|
||||
),
|
||||
timeout=10.0,
|
||||
)
|
||||
|
||||
current_app.logger.debug(
|
||||
"Auth: Received code verification response:\n"
|
||||
" Status: %d\n"
|
||||
" Headers: %s\n"
|
||||
" Body: %s",
|
||||
response.status_code,
|
||||
{k: v for k, v in dict(response.headers).items()
|
||||
if k.lower() not in ["set-cookie", "authorization"]},
|
||||
_redact_token(response.text) if response.text else "(empty)",
|
||||
)
|
||||
|
||||
_log_http_response(
|
||||
status_code=response.status_code,
|
||||
headers=dict(response.headers),
|
||||
body=response.text,
|
||||
)
|
||||
|
||||
response.raise_for_status()
|
||||
except httpx.RequestError as e:
|
||||
current_app.logger.error(f"Auth: Authorization endpoint request failed: {e}")
|
||||
raise IndieLoginError(f"Failed to verify code: {e}")
|
||||
except httpx.HTTPStatusError as e:
|
||||
raise OIDCError(f"Token request failed: {e}")
|
||||
|
||||
if response.status_code != 200:
|
||||
current_app.logger.error(
|
||||
f"Auth: Authorization endpoint returned error: {e.response.status_code} - {e.response.text}"
|
||||
)
|
||||
raise IndieLoginError(
|
||||
f"Authorization endpoint returned error: {e.response.status_code}"
|
||||
f"Auth: OIDC token endpoint returned {response.status_code}: {response.text[:200]}"
|
||||
)
|
||||
raise OIDCError(f"Token endpoint returned {response.status_code}")
|
||||
|
||||
# Parse response
|
||||
try:
|
||||
data = response.json()
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Auth: Failed to parse authorization endpoint response: {e}")
|
||||
raise IndieLoginError("Invalid JSON response from authorization endpoint")
|
||||
id_token = response.json().get("id_token")
|
||||
except ValueError:
|
||||
raise OIDCError("Token endpoint returned invalid JSON")
|
||||
if not id_token:
|
||||
raise OIDCError("Token endpoint returned no id_token")
|
||||
|
||||
me = data.get("me")
|
||||
claims = _validate_id_token(metadata, id_token, pending["nonce"])
|
||||
|
||||
admin_sub = current_app.config.get("OIDC_ADMIN_SUB", "")
|
||||
if not admin_sub or not secrets.compare_digest(claims["sub"], admin_sub):
|
||||
current_app.logger.warning(f"Auth: Unauthorized OIDC subject {claims['sub']}")
|
||||
raise UnauthorizedError("This account is not the site admin")
|
||||
|
||||
me = current_app.config.get("ADMIN_ME")
|
||||
if not me:
|
||||
current_app.logger.error("Auth: No identity returned from authorization endpoint")
|
||||
raise IndieLoginError("No identity returned from authorization endpoint")
|
||||
raise OIDCError("ADMIN_ME not configured")
|
||||
|
||||
current_app.logger.debug(f"Auth: Received identity: {me}")
|
||||
|
||||
# Verify this is the admin user
|
||||
current_app.logger.info(f"Auth: Verifying admin authorization for me={me}")
|
||||
|
||||
# Normalize URLs for comparison (handles trailing slashes and case differences)
|
||||
# This is correct per IndieAuth spec - the returned 'me' is the canonical form
|
||||
if normalize_url(me) != normalize_url(admin_me):
|
||||
current_app.logger.warning(
|
||||
f"Auth: Unauthorized login attempt: {me} (expected {admin_me})"
|
||||
)
|
||||
raise UnauthorizedError(f"User {me} is not authorized")
|
||||
|
||||
current_app.logger.debug("Auth: Admin verification passed")
|
||||
|
||||
# Create session
|
||||
session_token = create_session(me)
|
||||
|
||||
# Trigger author profile discovery (v1.2.0 Phase 2)
|
||||
# Per Q14: Never block login, always allow fallback
|
||||
# Refresh the author h-card; never block login on it
|
||||
try:
|
||||
from starpunk.author_discovery import get_author_profile
|
||||
author_profile = get_author_profile(me, refresh=True)
|
||||
current_app.logger.info(f"Author profile refreshed for {me}")
|
||||
|
||||
get_author_profile(me, refresh=True)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Author discovery failed: {e}")
|
||||
# Continue login anyway - never block per Q14
|
||||
|
||||
return session_token
|
||||
return session_token, pending["next_url"]
|
||||
|
||||
|
||||
def create_session(me: str) -> str:
|
||||
@@ -567,7 +406,6 @@ def verify_session(token: str) -> Optional[Dict[str, Any]]:
|
||||
current_app.logger.debug("Auth: No session token provided")
|
||||
return None
|
||||
|
||||
current_app.logger.debug(f"Auth: Verifying session token: {_redact_token(token)}")
|
||||
|
||||
token_hash = _hash_token(token)
|
||||
|
||||
|
||||
@@ -1,614 +0,0 @@
|
||||
"""
|
||||
External IndieAuth Token Verification with Endpoint Discovery
|
||||
|
||||
This module handles verification of bearer tokens issued by external
|
||||
IndieAuth providers. Following the IndieAuth specification, endpoints
|
||||
are discovered dynamically from the user's profile URL, not hardcoded.
|
||||
|
||||
For StarPunk V1 (single-user CMS), we always discover endpoints from
|
||||
ADMIN_ME since only the site owner can post content.
|
||||
|
||||
Key Components:
|
||||
EndpointCache: Simple in-memory cache for discovered endpoints and tokens
|
||||
verify_external_token: Main entry point for token verification
|
||||
discover_endpoints: Discovers IndieAuth endpoints from profile URL
|
||||
|
||||
Configuration (via Flask app.config):
|
||||
ADMIN_ME: Site owner's profile URL (required)
|
||||
DEBUG: Allow HTTP endpoints in debug mode
|
||||
|
||||
ADR: ADR-031 IndieAuth Endpoint Discovery Implementation
|
||||
Date: 2025-11-24
|
||||
Version: v1.0.0-rc.5
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
import time
|
||||
from typing import Dict, Optional, Any
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import httpx
|
||||
from bs4 import BeautifulSoup
|
||||
from flask import current_app
|
||||
|
||||
|
||||
# Timeouts
|
||||
DISCOVERY_TIMEOUT = 5.0 # Profile fetch (cached, so can be slower)
|
||||
VERIFICATION_TIMEOUT = 3.0 # Token verification (every request)
|
||||
|
||||
# Cache TTLs
|
||||
ENDPOINT_CACHE_TTL = 3600 # 1 hour for endpoints
|
||||
TOKEN_CACHE_TTL = 300 # 5 minutes for token verifications
|
||||
|
||||
|
||||
class EndpointCache:
|
||||
"""
|
||||
Simple in-memory cache for endpoint discovery and token verification
|
||||
|
||||
V1 single-user implementation: We only cache one user's endpoints
|
||||
since StarPunk V1 is explicitly single-user (only ADMIN_ME can post).
|
||||
|
||||
When V2 adds multi-user support, this will need refactoring to
|
||||
cache endpoints per profile URL.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# Endpoint cache (single-user V1)
|
||||
self.endpoints: Optional[Dict[str, str]] = None
|
||||
self.endpoints_expire: float = 0
|
||||
|
||||
# Token verification cache (token_hash -> (info, expiry))
|
||||
self.token_cache: Dict[str, tuple[Dict[str, Any], float]] = {}
|
||||
|
||||
def get_endpoints(self, ignore_expiry: bool = False) -> Optional[Dict[str, str]]:
|
||||
"""
|
||||
Get cached endpoints if still valid
|
||||
|
||||
Args:
|
||||
ignore_expiry: Return cached endpoints even if expired (grace period)
|
||||
|
||||
Returns:
|
||||
Cached endpoints dict or None if not cached or expired
|
||||
"""
|
||||
if self.endpoints is None:
|
||||
return None
|
||||
|
||||
if ignore_expiry or time.time() < self.endpoints_expire:
|
||||
return self.endpoints
|
||||
|
||||
return None
|
||||
|
||||
def set_endpoints(self, endpoints: Dict[str, str], ttl: int = ENDPOINT_CACHE_TTL):
|
||||
"""Cache discovered endpoints"""
|
||||
self.endpoints = endpoints
|
||||
self.endpoints_expire = time.time() + ttl
|
||||
|
||||
def get_token_info(self, token_hash: str) -> Optional[Dict[str, Any]]:
|
||||
"""Get cached token verification if still valid"""
|
||||
if token_hash in self.token_cache:
|
||||
info, expiry = self.token_cache[token_hash]
|
||||
if time.time() < expiry:
|
||||
return info
|
||||
else:
|
||||
# Expired, remove from cache
|
||||
del self.token_cache[token_hash]
|
||||
return None
|
||||
|
||||
def set_token_info(self, token_hash: str, info: Dict[str, Any], ttl: int = TOKEN_CACHE_TTL):
|
||||
"""Cache token verification result"""
|
||||
expiry = time.time() + ttl
|
||||
self.token_cache[token_hash] = (info, expiry)
|
||||
|
||||
|
||||
# Global cache instance (singleton for V1)
|
||||
_cache = EndpointCache()
|
||||
|
||||
|
||||
class DiscoveryError(Exception):
|
||||
"""Raised when endpoint discovery fails"""
|
||||
pass
|
||||
|
||||
|
||||
class TokenVerificationError(Exception):
|
||||
"""Raised when token verification fails"""
|
||||
pass
|
||||
|
||||
|
||||
def verify_external_token(token: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Verify bearer token with external IndieAuth provider
|
||||
|
||||
This is the main entry point for token verification. For StarPunk V1
|
||||
(single-user), we always discover endpoints from ADMIN_ME since only
|
||||
the site owner can post content.
|
||||
|
||||
Process:
|
||||
1. Check token verification cache
|
||||
2. Discover endpoints from ADMIN_ME (with caching)
|
||||
3. Verify token with discovered endpoint
|
||||
4. Validate token belongs to ADMIN_ME
|
||||
5. Cache successful verification
|
||||
|
||||
Args:
|
||||
token: Bearer token to verify
|
||||
|
||||
Returns:
|
||||
Dict with token info (me, client_id, scope) if valid
|
||||
None if token is invalid or verification fails
|
||||
|
||||
Token info dict contains:
|
||||
me: User's profile URL
|
||||
client_id: Client application URL
|
||||
scope: Space-separated list of scopes
|
||||
"""
|
||||
admin_me = current_app.config.get("ADMIN_ME")
|
||||
|
||||
if not admin_me:
|
||||
current_app.logger.error(
|
||||
"ADMIN_ME not configured. Cannot verify token ownership."
|
||||
)
|
||||
return None
|
||||
|
||||
# Check token cache first
|
||||
token_hash = _hash_token(token)
|
||||
cached_info = _cache.get_token_info(token_hash)
|
||||
if cached_info:
|
||||
current_app.logger.debug("Token verification cache hit")
|
||||
return cached_info
|
||||
|
||||
# Discover endpoints from ADMIN_ME (V1 single-user assumption)
|
||||
try:
|
||||
endpoints = discover_endpoints(admin_me)
|
||||
except DiscoveryError as e:
|
||||
current_app.logger.error(f"Endpoint discovery failed: {e}")
|
||||
return None
|
||||
|
||||
token_endpoint = endpoints.get('token_endpoint')
|
||||
if not token_endpoint:
|
||||
current_app.logger.error("No token endpoint found in discovery")
|
||||
return None
|
||||
|
||||
# Verify token with discovered endpoint
|
||||
try:
|
||||
token_info = _verify_with_endpoint(token_endpoint, token)
|
||||
except TokenVerificationError as e:
|
||||
current_app.logger.warning(f"Token verification failed: {e}")
|
||||
return None
|
||||
|
||||
# Validate token belongs to admin (single-user security check)
|
||||
token_me = token_info.get('me', '')
|
||||
if normalize_url(token_me) != normalize_url(admin_me):
|
||||
current_app.logger.warning(
|
||||
f"Token 'me' mismatch: {token_me} != {admin_me}"
|
||||
)
|
||||
return None
|
||||
|
||||
# Cache successful verification
|
||||
_cache.set_token_info(token_hash, token_info)
|
||||
|
||||
current_app.logger.debug(f"Token verified successfully for {token_me}")
|
||||
return token_info
|
||||
|
||||
|
||||
def discover_endpoints(profile_url: str) -> Dict[str, str]:
|
||||
"""
|
||||
Discover IndieAuth endpoints from a profile URL
|
||||
|
||||
Implements IndieAuth endpoint discovery per W3C spec:
|
||||
https://www.w3.org/TR/indieauth/#discovery-by-clients
|
||||
|
||||
Discovery priority:
|
||||
1. HTTP Link headers (highest priority)
|
||||
2. HTML link elements
|
||||
|
||||
Args:
|
||||
profile_url: User's profile URL (their IndieWeb identity)
|
||||
|
||||
Returns:
|
||||
Dict with discovered endpoints:
|
||||
{
|
||||
'authorization_endpoint': 'https://...',
|
||||
'token_endpoint': 'https://...'
|
||||
}
|
||||
|
||||
Raises:
|
||||
DiscoveryError: If discovery fails or no endpoints found
|
||||
"""
|
||||
# Check cache first
|
||||
cached_endpoints = _cache.get_endpoints()
|
||||
if cached_endpoints:
|
||||
current_app.logger.debug("Endpoint discovery cache hit")
|
||||
return cached_endpoints
|
||||
|
||||
# Validate profile URL
|
||||
_validate_profile_url(profile_url)
|
||||
|
||||
try:
|
||||
# Fetch profile with discovery
|
||||
endpoints = _fetch_and_parse(profile_url)
|
||||
|
||||
# Cache successful discovery
|
||||
_cache.set_endpoints(endpoints)
|
||||
|
||||
return endpoints
|
||||
|
||||
except Exception as e:
|
||||
# Check cache even if expired (grace period for network failures)
|
||||
cached = _cache.get_endpoints(ignore_expiry=True)
|
||||
if cached:
|
||||
current_app.logger.warning(
|
||||
f"Using expired cache due to discovery failure: {e}"
|
||||
)
|
||||
return cached
|
||||
|
||||
# No cache available, must fail
|
||||
raise DiscoveryError(f"Endpoint discovery failed: {e}")
|
||||
|
||||
|
||||
def _fetch_and_parse(profile_url: str) -> Dict[str, str]:
|
||||
"""
|
||||
Fetch profile URL and parse endpoints from headers and HTML
|
||||
|
||||
Args:
|
||||
profile_url: User's profile URL
|
||||
|
||||
Returns:
|
||||
Dict with discovered endpoints
|
||||
|
||||
Raises:
|
||||
DiscoveryError: If fetch fails or no endpoints found
|
||||
"""
|
||||
try:
|
||||
response = httpx.get(
|
||||
profile_url,
|
||||
timeout=DISCOVERY_TIMEOUT,
|
||||
follow_redirects=True,
|
||||
headers={
|
||||
'Accept': 'text/html,application/xhtml+xml',
|
||||
'User-Agent': f'StarPunk/{current_app.config.get("VERSION", "1.0")}'
|
||||
}
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
except httpx.TimeoutException:
|
||||
raise DiscoveryError(f"Timeout fetching profile: {profile_url}")
|
||||
except httpx.HTTPStatusError as e:
|
||||
raise DiscoveryError(f"HTTP {e.response.status_code} fetching profile")
|
||||
except httpx.RequestError as e:
|
||||
raise DiscoveryError(f"Network error fetching profile: {e}")
|
||||
|
||||
endpoints = {}
|
||||
|
||||
# 1. Parse HTTP Link headers (highest priority)
|
||||
link_header = response.headers.get('Link', '')
|
||||
if link_header:
|
||||
link_endpoints = _parse_link_header(link_header, profile_url)
|
||||
endpoints.update(link_endpoints)
|
||||
|
||||
# 2. Parse HTML link elements
|
||||
content_type = response.headers.get('Content-Type', '')
|
||||
if 'text/html' in content_type or 'application/xhtml+xml' in content_type:
|
||||
try:
|
||||
html_endpoints = _parse_html_links(response.text, profile_url)
|
||||
# Merge: Link headers take priority (so update HTML first)
|
||||
html_endpoints.update(endpoints)
|
||||
endpoints = html_endpoints
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"HTML parsing failed: {e}")
|
||||
# Continue with Link header endpoints if HTML parsing fails
|
||||
|
||||
# Validate we found at least one endpoint
|
||||
# - authorization_endpoint: Required for authentication-only flows (admin login)
|
||||
# - token_endpoint: Required for Micropub token verification
|
||||
# Having at least one allows the appropriate flow to work
|
||||
if 'token_endpoint' not in endpoints and 'authorization_endpoint' not in endpoints:
|
||||
raise DiscoveryError(
|
||||
f"No IndieAuth endpoints found at {profile_url}. "
|
||||
"Ensure your profile has authorization_endpoint or token_endpoint configured."
|
||||
)
|
||||
|
||||
# Validate endpoint URLs
|
||||
for rel, url in endpoints.items():
|
||||
_validate_endpoint_url(url, rel)
|
||||
|
||||
current_app.logger.info(
|
||||
f"Discovered endpoints from {profile_url}: "
|
||||
f"token={endpoints.get('token_endpoint')}, "
|
||||
f"auth={endpoints.get('authorization_endpoint')}"
|
||||
)
|
||||
|
||||
return endpoints
|
||||
|
||||
|
||||
def _parse_link_header(header: str, base_url: str) -> Dict[str, str]:
|
||||
"""
|
||||
Parse HTTP Link header for IndieAuth endpoints
|
||||
|
||||
Basic RFC 8288 support - handles simple Link headers.
|
||||
Limitations: Only supports quoted rel values, single Link headers.
|
||||
|
||||
Example:
|
||||
Link: <https://auth.example.com/token>; rel="token_endpoint"
|
||||
|
||||
Args:
|
||||
header: Link header value
|
||||
base_url: Base URL for resolving relative URLs
|
||||
|
||||
Returns:
|
||||
Dict with discovered endpoints
|
||||
"""
|
||||
endpoints = {}
|
||||
|
||||
# Pattern: <url>; rel="relation"
|
||||
# Note: Simplified - doesn't handle all RFC 8288 edge cases
|
||||
pattern = r'<([^>]+)>;\s*rel="([^"]+)"'
|
||||
matches = re.findall(pattern, header)
|
||||
|
||||
for url, rel in matches:
|
||||
if rel == 'authorization_endpoint':
|
||||
endpoints['authorization_endpoint'] = urljoin(base_url, url)
|
||||
elif rel == 'token_endpoint':
|
||||
endpoints['token_endpoint'] = urljoin(base_url, url)
|
||||
|
||||
return endpoints
|
||||
|
||||
|
||||
def _parse_html_links(html: str, base_url: str) -> Dict[str, str]:
|
||||
"""
|
||||
Extract IndieAuth endpoints from HTML link elements
|
||||
|
||||
Looks for:
|
||||
<link rel="authorization_endpoint" href="...">
|
||||
<link rel="token_endpoint" href="...">
|
||||
|
||||
Args:
|
||||
html: HTML content
|
||||
base_url: Base URL for resolving relative URLs
|
||||
|
||||
Returns:
|
||||
Dict with discovered endpoints
|
||||
"""
|
||||
endpoints = {}
|
||||
|
||||
try:
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
|
||||
# Find all link elements (check both head and body - be liberal)
|
||||
for link in soup.find_all('link', rel=True):
|
||||
rel = link.get('rel')
|
||||
href = link.get('href')
|
||||
|
||||
if not href:
|
||||
continue
|
||||
|
||||
# rel can be a list or string
|
||||
if isinstance(rel, list):
|
||||
rel = ' '.join(rel)
|
||||
|
||||
# Check for IndieAuth endpoints
|
||||
if 'authorization_endpoint' in rel:
|
||||
endpoints['authorization_endpoint'] = urljoin(base_url, href)
|
||||
elif 'token_endpoint' in rel:
|
||||
endpoints['token_endpoint'] = urljoin(base_url, href)
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"HTML parsing error: {e}")
|
||||
# Return what we found so far
|
||||
|
||||
return endpoints
|
||||
|
||||
|
||||
def _verify_with_endpoint(endpoint: str, token: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Verify token with the discovered token endpoint
|
||||
|
||||
Makes GET request to endpoint with Authorization header.
|
||||
Implements retry logic for network errors only.
|
||||
|
||||
Args:
|
||||
endpoint: Token endpoint URL
|
||||
token: Bearer token to verify
|
||||
|
||||
Returns:
|
||||
Token info dict from endpoint
|
||||
|
||||
Raises:
|
||||
TokenVerificationError: If verification fails
|
||||
"""
|
||||
headers = {
|
||||
'Authorization': f'Bearer {token}',
|
||||
'Accept': 'application/json',
|
||||
}
|
||||
|
||||
max_retries = 3
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
response = httpx.get(
|
||||
endpoint,
|
||||
headers=headers,
|
||||
timeout=VERIFICATION_TIMEOUT,
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
# Handle HTTP status codes
|
||||
if response.status_code == 200:
|
||||
token_info = response.json()
|
||||
|
||||
# Validate required fields
|
||||
if 'me' not in token_info:
|
||||
raise TokenVerificationError("Token response missing 'me' field")
|
||||
|
||||
return token_info
|
||||
|
||||
# Client errors - don't retry
|
||||
elif response.status_code in [400, 401, 403, 404]:
|
||||
raise TokenVerificationError(
|
||||
f"Token verification failed: HTTP {response.status_code}"
|
||||
)
|
||||
|
||||
# Server errors - retry
|
||||
elif response.status_code in [500, 502, 503, 504]:
|
||||
if attempt < max_retries - 1:
|
||||
wait_time = 2 ** attempt # Exponential backoff
|
||||
current_app.logger.debug(
|
||||
f"Server error {response.status_code}, retrying in {wait_time}s..."
|
||||
)
|
||||
time.sleep(wait_time)
|
||||
continue
|
||||
else:
|
||||
raise TokenVerificationError(
|
||||
f"Token endpoint error: HTTP {response.status_code}"
|
||||
)
|
||||
|
||||
# Other status codes
|
||||
else:
|
||||
raise TokenVerificationError(
|
||||
f"Unexpected response: HTTP {response.status_code}"
|
||||
)
|
||||
|
||||
except httpx.TimeoutException:
|
||||
if attempt < max_retries - 1:
|
||||
wait_time = 2 ** attempt
|
||||
current_app.logger.debug(f"Timeout, retrying in {wait_time}s...")
|
||||
time.sleep(wait_time)
|
||||
continue
|
||||
else:
|
||||
raise TokenVerificationError("Token verification timeout")
|
||||
|
||||
except httpx.NetworkError as e:
|
||||
if attempt < max_retries - 1:
|
||||
wait_time = 2 ** attempt
|
||||
current_app.logger.debug(f"Network error, retrying in {wait_time}s...")
|
||||
time.sleep(wait_time)
|
||||
continue
|
||||
else:
|
||||
raise TokenVerificationError(f"Network error: {e}")
|
||||
|
||||
except Exception as e:
|
||||
# Don't retry for unexpected errors
|
||||
raise TokenVerificationError(f"Verification failed: {e}")
|
||||
|
||||
# Should never reach here, but just in case
|
||||
raise TokenVerificationError("Maximum retries exceeded")
|
||||
|
||||
|
||||
def _validate_profile_url(url: str) -> None:
|
||||
"""
|
||||
Validate profile URL format and security requirements
|
||||
|
||||
Args:
|
||||
url: Profile URL to validate
|
||||
|
||||
Raises:
|
||||
DiscoveryError: If URL is invalid or insecure
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
|
||||
# Must be absolute
|
||||
if not parsed.scheme or not parsed.netloc:
|
||||
raise DiscoveryError(f"Invalid profile URL format: {url}")
|
||||
|
||||
# HTTPS required in production
|
||||
if not current_app.debug and parsed.scheme != 'https':
|
||||
raise DiscoveryError(
|
||||
f"HTTPS required for profile URLs in production. Got: {url}"
|
||||
)
|
||||
|
||||
# Allow localhost only in debug mode
|
||||
if not current_app.debug and parsed.hostname in ['localhost', '127.0.0.1', '::1']:
|
||||
raise DiscoveryError(
|
||||
"Localhost URLs not allowed in production"
|
||||
)
|
||||
|
||||
|
||||
def _validate_endpoint_url(url: str, rel: str) -> None:
|
||||
"""
|
||||
Validate discovered endpoint URL
|
||||
|
||||
Args:
|
||||
url: Endpoint URL to validate
|
||||
rel: Endpoint relation (for error messages)
|
||||
|
||||
Raises:
|
||||
DiscoveryError: If URL is invalid or insecure
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
|
||||
# Must be absolute
|
||||
if not parsed.scheme or not parsed.netloc:
|
||||
raise DiscoveryError(f"Invalid {rel} URL format: {url}")
|
||||
|
||||
# HTTPS required in production
|
||||
if not current_app.debug and parsed.scheme != 'https':
|
||||
raise DiscoveryError(
|
||||
f"HTTPS required for {rel} in production. Got: {url}"
|
||||
)
|
||||
|
||||
# Allow localhost only in debug mode
|
||||
if not current_app.debug and parsed.hostname in ['localhost', '127.0.0.1', '::1']:
|
||||
raise DiscoveryError(
|
||||
f"Localhost not allowed for {rel} in production"
|
||||
)
|
||||
|
||||
|
||||
def normalize_url(url: str) -> str:
|
||||
"""
|
||||
Normalize URL for comparison
|
||||
|
||||
Removes trailing slash and converts to lowercase.
|
||||
Used only for comparison, not for storage.
|
||||
|
||||
Args:
|
||||
url: URL to normalize
|
||||
|
||||
Returns:
|
||||
Normalized URL
|
||||
"""
|
||||
return url.rstrip('/').lower()
|
||||
|
||||
|
||||
def _hash_token(token: str) -> str:
|
||||
"""
|
||||
Hash token for secure caching
|
||||
|
||||
Uses SHA-256 to prevent tokens from appearing in logs
|
||||
and to create fixed-length cache keys.
|
||||
|
||||
Args:
|
||||
token: Bearer token
|
||||
|
||||
Returns:
|
||||
SHA-256 hash of token (hex)
|
||||
"""
|
||||
return hashlib.sha256(token.encode()).hexdigest()
|
||||
|
||||
|
||||
def check_scope(required_scope: str, token_scope: str) -> bool:
|
||||
"""
|
||||
Check if token has required scope
|
||||
|
||||
Scopes are space-separated in token_scope string.
|
||||
Any scope in the list satisfies the requirement.
|
||||
|
||||
Args:
|
||||
required_scope: Scope needed (e.g., "create")
|
||||
token_scope: Space-separated scope string from token
|
||||
|
||||
Returns:
|
||||
True if token has required scope, False otherwise
|
||||
|
||||
Examples:
|
||||
>>> check_scope("create", "create update")
|
||||
True
|
||||
>>> check_scope("create", "read")
|
||||
False
|
||||
>>> check_scope("create", "")
|
||||
False
|
||||
"""
|
||||
if not token_scope:
|
||||
return False
|
||||
|
||||
scopes = token_scope.split()
|
||||
return required_scope in scopes
|
||||
@@ -82,10 +82,12 @@ def discover_author_profile(me_url: str) -> Optional[Dict]:
|
||||
response.raise_for_status()
|
||||
|
||||
# Parse microformats from HTML
|
||||
parsed = mf2py.parse(doc=response.text, url=me_url)
|
||||
# Relative URLs and the representative h-card use the URL after redirects
|
||||
page_url = str(response.url) if isinstance(response.url, (str, httpx.URL)) else me_url
|
||||
parsed = mf2py.parse(doc=response.text, url=page_url)
|
||||
|
||||
# Extract h-card (per Q16: first representative h-card)
|
||||
hcard = _find_representative_hcard(parsed, me_url)
|
||||
# Representative h-card (v2.8.0: per the parsing spec)
|
||||
hcard = _find_representative_hcard(parsed, page_url)
|
||||
|
||||
if not hcard:
|
||||
current_app.logger.warning(f"No h-card found at {me_url}")
|
||||
@@ -132,47 +134,21 @@ def discover_author_profile(me_url: str) -> Optional[Dict]:
|
||||
|
||||
def _find_representative_hcard(parsed: dict, me_url: str) -> Optional[dict]:
|
||||
"""
|
||||
Find representative h-card from parsed microformats
|
||||
Find the representative h-card of the author's page (v2.8.0)
|
||||
|
||||
Per Q16: First representative h-card = first h-card with p-name
|
||||
Per Q18: First h-card with url property matching profile URL
|
||||
Follows https://microformats.org/wiki/representative-h-card-parsing
|
||||
(uid+url match, rel=me match, or the only h-card with a matching url).
|
||||
Other h-cards on the page may be other people, so none is guessed.
|
||||
|
||||
Args:
|
||||
parsed: Parsed microformats data from mf2py
|
||||
me_url: Profile URL for matching
|
||||
me_url: Profile URL (the page that was parsed)
|
||||
|
||||
Returns:
|
||||
h-card dict or None if not found
|
||||
h-card dict or None if the page has no representative h-card
|
||||
"""
|
||||
items = parsed.get('items', [])
|
||||
|
||||
# First try: h-card with matching URL (most specific)
|
||||
for item in items:
|
||||
if 'h-card' in item.get('type', []):
|
||||
properties = item.get('properties', {})
|
||||
urls = properties.get('url', [])
|
||||
|
||||
# Check if any URL matches the profile URL
|
||||
for url in urls:
|
||||
if isinstance(url, dict):
|
||||
url = url.get('value', '')
|
||||
if _normalize_url(url) == _normalize_url(me_url):
|
||||
# Found matching h-card
|
||||
return item
|
||||
|
||||
# Second try: First h-card with p-name (representative h-card)
|
||||
for item in items:
|
||||
if 'h-card' in item.get('type', []):
|
||||
properties = item.get('properties', {})
|
||||
if properties.get('name'):
|
||||
return item
|
||||
|
||||
# Third try: Just use first h-card if any
|
||||
for item in items:
|
||||
if 'h-card' in item.get('type', []):
|
||||
return item
|
||||
|
||||
return None
|
||||
from starpunk.mf2_utils import representative_hcard
|
||||
return representative_hcard(parsed, me_url)
|
||||
|
||||
|
||||
def _get_property(hcard: dict, prop_name: str) -> Optional[str]:
|
||||
|
||||
+66
-17
@@ -35,24 +35,23 @@ def load_config(app, config_override=None):
|
||||
app.config["SESSION_SECRET"] = os.getenv("SESSION_SECRET")
|
||||
app.config["SESSION_LIFETIME"] = int(os.getenv("SESSION_LIFETIME", "30"))
|
||||
|
||||
# DEPRECATED: TOKEN_ENDPOINT no longer used (v1.0.0-rc.5+)
|
||||
# Endpoints are now discovered from ADMIN_ME profile (ADR-031)
|
||||
if 'TOKEN_ENDPOINT' in os.environ:
|
||||
app.logger.warning(
|
||||
"TOKEN_ENDPOINT is deprecated and will be ignored. "
|
||||
"Remove it from your configuration. "
|
||||
"Endpoints are now discovered automatically from your ADMIN_ME profile. "
|
||||
"See docs/migration/fix-hardcoded-endpoints.md for details."
|
||||
)
|
||||
# Admin login via OpenID Connect (v2.0.0, ADR-064)
|
||||
# ADMIN_ME stays the site owner's identity URL (h-card, Micropub "me");
|
||||
# OIDC decides who may log in as that owner.
|
||||
app.config["OIDC_ISSUER"] = os.getenv("OIDC_ISSUER", "").rstrip("/")
|
||||
app.config["OIDC_CLIENT_ID"] = os.getenv("OIDC_CLIENT_ID", "")
|
||||
app.config["OIDC_CLIENT_SECRET"] = os.getenv("OIDC_CLIENT_SECRET", "")
|
||||
app.config["OIDC_ADMIN_SUB"] = os.getenv("OIDC_ADMIN_SUB", "")
|
||||
app.config["OIDC_PROVIDER_NAME"] = os.getenv("OIDC_PROVIDER_NAME", "single sign-on")
|
||||
|
||||
# DEPRECATED: INDIELOGIN_URL no longer used (hotfix 2025-12-17)
|
||||
# Authorization endpoint is now discovered from ADMIN_ME profile per IndieAuth spec
|
||||
if 'INDIELOGIN_URL' in os.environ:
|
||||
app.logger.warning(
|
||||
"INDIELOGIN_URL is deprecated and will be ignored. "
|
||||
"Remove it from your configuration. "
|
||||
"The authorization endpoint is now discovered automatically from your ADMIN_ME profile."
|
||||
)
|
||||
# Micropub access tokens issued by StarPunk's IndieAuth server (v2.0.0)
|
||||
app.config["INDIEAUTH_TOKEN_LIFETIME_DAYS"] = int(
|
||||
os.getenv("INDIEAUTH_TOKEN_LIFETIME_DAYS", "90")
|
||||
)
|
||||
|
||||
for legacy in ("TOKEN_ENDPOINT", "INDIELOGIN_URL"):
|
||||
if legacy in os.environ:
|
||||
app.logger.warning(f"{legacy} is no longer used and will be ignored.")
|
||||
|
||||
# Validate required configuration
|
||||
if not app.config["SESSION_SECRET"]:
|
||||
@@ -110,6 +109,49 @@ def load_config(app, config_override=None):
|
||||
app.config["DEBUG_FILE_MAX_AGE_DAYS"] = int(os.getenv("DEBUG_FILE_MAX_AGE_DAYS", "7"))
|
||||
app.config["DEBUG_FILE_MAX_SIZE_MB"] = int(os.getenv("DEBUG_FILE_MAX_SIZE_MB", "100"))
|
||||
|
||||
# Webmention configuration (v1.6.0)
|
||||
app.config["WEBMENTION_ENABLED"] = os.getenv("WEBMENTION_ENABLED", "true").lower() == "true"
|
||||
|
||||
# WebSub (v2.10.0, ADR-071): built-in hub unless WEBSUB_HUB_URL names another
|
||||
app.config["WEBSUB_ENABLED"] = os.getenv("WEBSUB_ENABLED", "true").lower() == "true"
|
||||
app.config["WEBSUB_HUB_URL"] = os.getenv("WEBSUB_HUB_URL", "").strip()
|
||||
app.config["WEBSUB_MAX_SUBSCRIPTIONS"] = int(os.getenv("WEBSUB_MAX_SUBSCRIPTIONS", "1000"))
|
||||
app.config["WEBSUB_MAX_PER_HOST"] = int(os.getenv("WEBSUB_MAX_PER_HOST", "25"))
|
||||
app.config["WEBSUB_POLL_INTERVAL"] = int(os.getenv("WEBSUB_POLL_INTERVAL", "5"))
|
||||
app.config["WEBMENTION_WORKER_ENABLED"] = os.getenv("WEBMENTION_WORKER_ENABLED", "true").lower() == "true"
|
||||
app.config["WEBMENTION_POLL_INTERVAL"] = int(os.getenv("WEBMENTION_POLL_INTERVAL", "10"))
|
||||
app.config["WEBMENTION_ERROR_BACKOFF"] = int(os.getenv("WEBMENTION_ERROR_BACKOFF", "60"))
|
||||
app.config["WEBMENTION_BATCH_SIZE"] = int(os.getenv("WEBMENTION_BATCH_SIZE", "5"))
|
||||
app.config["WEBMENTION_AUTO_APPROVE"] = os.getenv("WEBMENTION_AUTO_APPROVE", "true").lower() == "true"
|
||||
|
||||
# Webmention rate limiting (v1.6.0 Phase 2)
|
||||
app.config["WEBMENTION_RATE_LIMIT_ENABLED"] = os.getenv("WEBMENTION_RATE_LIMIT_ENABLED", "true").lower() == "true"
|
||||
app.config["WEBMENTION_RATE_LIMIT_IP"] = int(os.getenv("WEBMENTION_RATE_LIMIT_IP", "60"))
|
||||
app.config["WEBMENTION_RATE_LIMIT_DOMAIN"] = int(os.getenv("WEBMENTION_RATE_LIMIT_DOMAIN", "10"))
|
||||
app.config["WEBMENTION_RATE_LIMIT_WINDOW"] = int(os.getenv("WEBMENTION_RATE_LIMIT_WINDOW", "60"))
|
||||
app.config["TRUSTED_PROXY"] = os.getenv("TRUSTED_PROXY", "false").lower() == "true"
|
||||
|
||||
# Crossposting to GoToSocial / Mastodon-API (v2.1.0, ADR-065)
|
||||
# Enabled when both URL and token are set
|
||||
app.config["GOTOSOCIAL_URL"] = os.getenv("GOTOSOCIAL_URL", "")
|
||||
app.config["GOTOSOCIAL_ACCESS_TOKEN"] = os.getenv("GOTOSOCIAL_ACCESS_TOKEN", "")
|
||||
app.config["GOTOSOCIAL_NAME"] = os.getenv("GOTOSOCIAL_NAME", "GoToSocial")
|
||||
app.config["GOTOSOCIAL_VISIBILITY"] = os.getenv("GOTOSOCIAL_VISIBILITY", "public")
|
||||
app.config["GOTOSOCIAL_MAX_CHARS"] = int(os.getenv("GOTOSOCIAL_MAX_CHARS", "5000"))
|
||||
app.config["GOTOSOCIAL_MAX_MEDIA"] = int(os.getenv("GOTOSOCIAL_MAX_MEDIA", "4"))
|
||||
app.config["BLUESKY_HANDLE"] = os.getenv("BLUESKY_HANDLE", "")
|
||||
app.config["BLUESKY_APP_PASSWORD"] = os.getenv("BLUESKY_APP_PASSWORD", "")
|
||||
app.config["BLUESKY_SERVICE"] = os.getenv("BLUESKY_SERVICE", "https://bsky.social")
|
||||
app.config["BLUESKY_NAME"] = os.getenv("BLUESKY_NAME", "Bluesky")
|
||||
app.config["BLUESKY_MAX_CHARS"] = int(os.getenv("BLUESKY_MAX_CHARS", "300"))
|
||||
# Crossposting to Threads (v2.6.0, ADR-069): long-lived token, refreshed automatically
|
||||
app.config["THREADS_ACCESS_TOKEN"] = os.getenv("THREADS_ACCESS_TOKEN", "")
|
||||
app.config["THREADS_USERNAME"] = os.getenv("THREADS_USERNAME", "")
|
||||
app.config["THREADS_NAME"] = os.getenv("THREADS_NAME", "Threads")
|
||||
app.config["THREADS_MAX_CHARS"] = int(os.getenv("THREADS_MAX_CHARS", "500"))
|
||||
app.config["THREADS_MAX_MEDIA"] = int(os.getenv("THREADS_MAX_MEDIA", "4"))
|
||||
app.config["SYNDICATION_POLL_INTERVAL"] = int(os.getenv("SYNDICATION_POLL_INTERVAL", "10"))
|
||||
|
||||
# Apply overrides if provided
|
||||
if config_override:
|
||||
app.config.update(config_override)
|
||||
@@ -231,6 +273,13 @@ def validate_config(app):
|
||||
"Set ADMIN_ME=https://your-site.com in .env"
|
||||
)
|
||||
|
||||
# Production mode: OIDC admin login must be configured
|
||||
# (tests configure OIDC per test, so skip the check there)
|
||||
if not app.config.get("TESTING"):
|
||||
for field in ("OIDC_ISSUER", "OIDC_CLIENT_ID", "OIDC_CLIENT_SECRET", "OIDC_ADMIN_SUB"):
|
||||
if not app.config.get(field):
|
||||
errors.append(f"Production mode requires {field} to be set (see .env.example)")
|
||||
|
||||
# If there are validation errors, fail fast with clear message
|
||||
if errors:
|
||||
error_msg = "\n".join([
|
||||
|
||||
@@ -107,6 +107,16 @@ class ConnectionPool:
|
||||
if not conn:
|
||||
return
|
||||
|
||||
# A request that failed mid-write must not hand an open transaction
|
||||
# (and its write lock) to the next request (v2.6.5)
|
||||
if conn.in_transaction:
|
||||
try:
|
||||
conn.rollback()
|
||||
except Exception:
|
||||
conn.close()
|
||||
self._stats['connections_closed'] += 1
|
||||
return
|
||||
|
||||
with self._lock:
|
||||
if len(self._pool) < self.pool_size:
|
||||
# Return to pool
|
||||
|
||||
@@ -82,6 +82,11 @@ def register_error_handlers(app):
|
||||
|
||||
return render_template('404.html'), 404
|
||||
|
||||
@app.errorhandler(410)
|
||||
def gone(error):
|
||||
"""Handle 410 Gone (deleted notes, Webmention 3.1.5)"""
|
||||
return render_template('410.html'), 410
|
||||
|
||||
@app.errorhandler(405)
|
||||
def method_not_allowed(error):
|
||||
"""Handle 405 Method Not Allowed errors"""
|
||||
|
||||
@@ -198,7 +198,7 @@ def generate_atom_streaming(
|
||||
yield f' <link rel="enclosure" type="{_escape_xml(mime_type)}" href="{_escape_xml(media_url)}" length="{size}"{title_attr}/>\n'
|
||||
|
||||
# Content - include media as HTML (per Q24)
|
||||
if note.html:
|
||||
if note.feed_html:
|
||||
# Build HTML content with media at top
|
||||
html_content = ""
|
||||
|
||||
@@ -212,7 +212,7 @@ def generate_atom_streaming(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += note.html
|
||||
html_content += note.feed_html
|
||||
|
||||
# HTML content - escaped
|
||||
yield ' <content type="html">'
|
||||
|
||||
@@ -169,7 +169,11 @@ class FeedCache:
|
||||
- Adds new entry or updates existing entry
|
||||
"""
|
||||
cache_key = self._generate_cache_key(format_name, notes_checksum)
|
||||
etag = self._generate_etag(content)
|
||||
# From what the feed is made of, not its bytes: the bytes include a
|
||||
# build time, so each server process made a different ETag and
|
||||
# conditional requests rarely matched (v2.10.1). Weak ETag: same
|
||||
# notes, semantically the same feed.
|
||||
etag = self._generate_etag(f"{format_name}:{notes_checksum}")
|
||||
timestamp = time.time()
|
||||
|
||||
# Evict if cache is full
|
||||
|
||||
@@ -267,7 +267,7 @@ def _build_item_object(site_url: str, note: Note) -> Dict[str, Any]:
|
||||
|
||||
# Add content (HTML or text)
|
||||
# Per Q24: Include media as HTML in content_html
|
||||
if note.html:
|
||||
if note.feed_html:
|
||||
content_html = ""
|
||||
|
||||
# Add media at top if present (v1.2.0 Phase 3)
|
||||
@@ -280,7 +280,7 @@ def _build_item_object(site_url: str, note: Note) -> Dict[str, Any]:
|
||||
content_html += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
content_html += note.html
|
||||
content_html += note.feed_html
|
||||
item["content_html"] = content_html
|
||||
else:
|
||||
item["content_text"] = note.content
|
||||
|
||||
@@ -93,7 +93,7 @@ def generate_rss(
|
||||
fg.language("en")
|
||||
|
||||
# Add self-link for feed discovery (Atom namespace)
|
||||
fg.link(href=f"{site_url}/feed.xml", rel="self", type="application/rss+xml")
|
||||
fg.link(href=f"{site_url}/feed.rss", rel="self", type="application/rss+xml")
|
||||
|
||||
# Set last build date to now
|
||||
fg.lastBuildDate(datetime.now(timezone.utc))
|
||||
@@ -137,7 +137,7 @@ def generate_rss(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += clean_html_for_rss(note.html)
|
||||
html_content += clean_html_for_rss(note.feed_html)
|
||||
|
||||
# feedgen automatically wraps content in CDATA for RSS
|
||||
fe.description(html_content)
|
||||
@@ -246,7 +246,7 @@ def generate_rss_streaming(
|
||||
yield f" <description>{_escape_xml(site_description or site_name)}</description>\n"
|
||||
yield " <language>en</language>\n"
|
||||
yield f" <lastBuildDate>{last_build}</lastBuildDate>\n"
|
||||
yield f' <atom:link href="{_escape_xml(site_url)}/feed.xml" rel="self" type="application/rss+xml"/>\n'
|
||||
yield f' <atom:link href="{_escape_xml(site_url)}/feed.rss" rel="self" type="application/rss+xml"/>\n'
|
||||
|
||||
# Yield items (newest first)
|
||||
# Notes from database are already in DESC order (newest first)
|
||||
@@ -278,7 +278,7 @@ def generate_rss_streaming(
|
||||
html_content += '</div>'
|
||||
|
||||
# Add text content below media
|
||||
html_content += clean_html_for_rss(note.html)
|
||||
html_content += clean_html_for_rss(note.feed_html)
|
||||
|
||||
# Build item XML
|
||||
item_xml = f""" <item>
|
||||
|
||||
@@ -0,0 +1,342 @@
|
||||
"""
|
||||
Built-in IndieAuth authorization server for StarPunk (v2.0.0, ADR-064)
|
||||
|
||||
Micropub clients (Quill, Indigenous, ...) obtain tokens through IndieAuth.
|
||||
StarPunk issues them itself: the site owner approves a client after logging
|
||||
in with OIDC, and StarPunk hands out an authorization code and then an
|
||||
access token. Tokens are stored hashed and checked locally by /micropub.
|
||||
|
||||
Only the authorization code flow with PKCE (S256) is supported, per the
|
||||
current IndieAuth spec: https://indieauth.spec.indieweb.org/
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import secrets
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Dict, List, Optional
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import httpx
|
||||
from bs4 import BeautifulSoup
|
||||
from flask import current_app
|
||||
|
||||
from starpunk.database import get_db
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
CODE_LIFETIME_MINUTES = 10
|
||||
SUPPORTED_SCOPES = ["create", "update", "delete", "media", "profile"]
|
||||
|
||||
|
||||
class AuthorizationRequestError(Exception):
|
||||
"""
|
||||
Invalid authorization request
|
||||
|
||||
redirectable=False means the client_id or redirect_uri cannot be trusted,
|
||||
so the error must be shown to the user instead of sent to the client.
|
||||
"""
|
||||
|
||||
def __init__(self, error: str, description: str, redirectable: bool = True):
|
||||
super().__init__(description)
|
||||
self.error = error
|
||||
self.description = description
|
||||
self.redirectable = redirectable
|
||||
|
||||
|
||||
class InvalidGrantError(Exception):
|
||||
"""Authorization code is invalid, expired, used, or does not match"""
|
||||
|
||||
|
||||
def _hash(value: str) -> str:
|
||||
return hashlib.sha256(value.encode()).hexdigest()
|
||||
|
||||
|
||||
def issuer() -> str:
|
||||
"""Issuer identifier (RFC 9207): the site URL with trailing slash"""
|
||||
return current_app.config["SITE_URL"].rstrip("/") + "/"
|
||||
|
||||
|
||||
def admin_me() -> str:
|
||||
"""The single identity StarPunk issues tokens for"""
|
||||
return current_app.config["ADMIN_ME"]
|
||||
|
||||
|
||||
def check_scope(required_scope: str, token_scope: str) -> bool:
|
||||
"""True if required_scope is one of the space-separated token scopes"""
|
||||
return bool(token_scope) and required_scope in token_scope.split()
|
||||
|
||||
|
||||
def _is_http_url(url: str) -> bool:
|
||||
parsed = urlparse(url or "")
|
||||
return parsed.scheme in ("http", "https") and bool(parsed.netloc) and not parsed.fragment
|
||||
|
||||
|
||||
def _same_origin(a: str, b: str) -> bool:
|
||||
pa, pb = urlparse(a), urlparse(b)
|
||||
return (pa.scheme, pa.hostname, pa.port) == (pb.scheme, pb.hostname, pb.port)
|
||||
|
||||
|
||||
def fetch_client_redirect_uris(client_id: str) -> List[str]:
|
||||
"""
|
||||
Fetch the redirect URIs a client publishes at its client_id URL
|
||||
|
||||
Supports a JSON client metadata document ("redirect_uris") and the older
|
||||
HTML/Link header form (rel="redirect_uri"). Returns [] on any failure.
|
||||
"""
|
||||
from starpunk.webmention_ssrf import SSRFBlockedError, validate_url_for_fetch
|
||||
|
||||
try:
|
||||
if not current_app.config.get("DEV_MODE"):
|
||||
validate_url_for_fetch(client_id)
|
||||
response = httpx.get(
|
||||
client_id,
|
||||
timeout=5.0,
|
||||
follow_redirects=True,
|
||||
headers={"Accept": "application/json, text/html;q=0.9"},
|
||||
)
|
||||
except (httpx.HTTPError, SSRFBlockedError, ValueError) as e:
|
||||
current_app.logger.info(f"IndieAuth: could not fetch client {client_id}: {e}")
|
||||
return []
|
||||
|
||||
if response.status_code != 200:
|
||||
return []
|
||||
|
||||
base = str(response.url)
|
||||
uris: List[str] = []
|
||||
|
||||
if "json" in response.headers.get("Content-Type", ""):
|
||||
try:
|
||||
data = response.json()
|
||||
except ValueError:
|
||||
return []
|
||||
if isinstance(data, dict) and isinstance(data.get("redirect_uris"), list):
|
||||
uris.extend(u for u in data["redirect_uris"] if isinstance(u, str))
|
||||
return uris
|
||||
|
||||
link = response.links.get("redirect_uri")
|
||||
if link and link.get("url"):
|
||||
uris.append(urljoin(base, link["url"]))
|
||||
|
||||
soup = BeautifulSoup(response.text[:500_000], "html.parser")
|
||||
for tag in soup.find_all(["link", "a"], href=True):
|
||||
if "redirect_uri" in (tag.get("rel") or []):
|
||||
uris.append(urljoin(base, tag["href"]))
|
||||
return uris
|
||||
|
||||
|
||||
def validate_authorization_request(params: Dict[str, str]) -> Dict[str, Any]:
|
||||
"""
|
||||
Validate an IndieAuth authorization request
|
||||
|
||||
Args:
|
||||
params: Query (or form) parameters from the client
|
||||
|
||||
Returns:
|
||||
Normalized request: client_id, redirect_uri, state, scopes,
|
||||
code_challenge
|
||||
|
||||
Raises:
|
||||
AuthorizationRequestError
|
||||
"""
|
||||
client_id = (params.get("client_id") or "").strip()
|
||||
redirect_uri = (params.get("redirect_uri") or "").strip()
|
||||
|
||||
# client_id / redirect_uri problems are never redirected to the client
|
||||
if not _is_http_url(client_id):
|
||||
raise AuthorizationRequestError(
|
||||
"invalid_request", "client_id must be an http(s) URL", redirectable=False
|
||||
)
|
||||
if not redirect_uri or urlparse(redirect_uri).fragment:
|
||||
raise AuthorizationRequestError(
|
||||
"invalid_request", "redirect_uri is missing or invalid", redirectable=False
|
||||
)
|
||||
if not _same_origin(client_id, redirect_uri):
|
||||
if redirect_uri not in fetch_client_redirect_uris(client_id):
|
||||
raise AuthorizationRequestError(
|
||||
"invalid_request",
|
||||
"redirect_uri is not registered by this client",
|
||||
redirectable=False,
|
||||
)
|
||||
|
||||
if params.get("response_type", "code") != "code":
|
||||
raise AuthorizationRequestError(
|
||||
"unsupported_response_type", "Only response_type=code is supported"
|
||||
)
|
||||
if not params.get("state"):
|
||||
raise AuthorizationRequestError("invalid_request", "state is required")
|
||||
|
||||
code_challenge = params.get("code_challenge") or ""
|
||||
if not code_challenge:
|
||||
raise AuthorizationRequestError("invalid_request", "PKCE code_challenge is required")
|
||||
if params.get("code_challenge_method", "S256") != "S256":
|
||||
raise AuthorizationRequestError(
|
||||
"invalid_request", "Only code_challenge_method=S256 is supported"
|
||||
)
|
||||
|
||||
scopes = [s for s in (params.get("scope") or "").split() if s]
|
||||
|
||||
return {
|
||||
"client_id": client_id,
|
||||
"redirect_uri": redirect_uri,
|
||||
"state": params["state"],
|
||||
"scopes": scopes,
|
||||
"code_challenge": code_challenge,
|
||||
}
|
||||
|
||||
|
||||
def create_authorization_code(
|
||||
client_id: str, redirect_uri: str, scope: str, code_challenge: str
|
||||
) -> str:
|
||||
"""Issue a single-use authorization code (stored hashed)"""
|
||||
code = secrets.token_urlsafe(32)
|
||||
db = get_db(current_app)
|
||||
db.execute("DELETE FROM indieauth_codes WHERE expires_at <= datetime('now')")
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO indieauth_codes
|
||||
(code_hash, client_id, redirect_uri, scope, code_challenge, expires_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
_hash(code),
|
||||
client_id,
|
||||
redirect_uri,
|
||||
scope,
|
||||
code_challenge,
|
||||
datetime.utcnow() + timedelta(minutes=CODE_LIFETIME_MINUTES),
|
||||
),
|
||||
)
|
||||
db.commit()
|
||||
return code
|
||||
|
||||
|
||||
def redeem_authorization_code(
|
||||
code: str, client_id: str, redirect_uri: str, code_verifier: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Redeem an authorization code exactly once
|
||||
|
||||
Returns:
|
||||
{"me": ..., "scope": ..., "client_id": ...}
|
||||
|
||||
Raises:
|
||||
InvalidGrantError
|
||||
"""
|
||||
if not code or not code_verifier:
|
||||
raise InvalidGrantError("code and code_verifier are required")
|
||||
|
||||
db = get_db(current_app)
|
||||
# Atomic single use: only the first redemption flips used_at
|
||||
cursor = db.execute(
|
||||
"""
|
||||
UPDATE indieauth_codes SET used_at = datetime('now')
|
||||
WHERE code_hash = ? AND used_at IS NULL AND expires_at > datetime('now')
|
||||
""",
|
||||
(_hash(code),),
|
||||
)
|
||||
db.commit()
|
||||
if cursor.rowcount != 1:
|
||||
raise InvalidGrantError("Authorization code is invalid, expired or already used")
|
||||
|
||||
row = db.execute(
|
||||
"SELECT client_id, redirect_uri, scope, code_challenge FROM indieauth_codes WHERE code_hash = ?",
|
||||
(_hash(code),),
|
||||
).fetchone()
|
||||
|
||||
if row["client_id"] != client_id or row["redirect_uri"] != redirect_uri:
|
||||
raise InvalidGrantError("client_id or redirect_uri does not match the code")
|
||||
|
||||
digest = hashlib.sha256(code_verifier.encode("ascii", "ignore")).digest()
|
||||
challenge = base64.urlsafe_b64encode(digest).rstrip(b"=").decode("ascii")
|
||||
if not secrets.compare_digest(challenge, row["code_challenge"]):
|
||||
raise InvalidGrantError("PKCE verification failed")
|
||||
|
||||
return {"me": admin_me(), "scope": row["scope"], "client_id": row["client_id"]}
|
||||
|
||||
|
||||
def create_access_token(client_id: str, scope: str) -> Dict[str, Any]:
|
||||
"""Issue an access token (stored hashed); returns the token response"""
|
||||
token = secrets.token_urlsafe(32)
|
||||
lifetime_days = current_app.config.get("INDIEAUTH_TOKEN_LIFETIME_DAYS", 90)
|
||||
expires_at = datetime.utcnow() + timedelta(days=lifetime_days)
|
||||
|
||||
db = get_db(current_app)
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO indieauth_tokens (token_hash, client_id, scope, expires_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(_hash(token), client_id, scope, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
current_app.logger.info(f"IndieAuth: token issued to {client_id} (scope: {scope})")
|
||||
return {
|
||||
"access_token": token,
|
||||
"token_type": "Bearer",
|
||||
"scope": scope,
|
||||
"me": admin_me(),
|
||||
"expires_in": lifetime_days * 86400,
|
||||
}
|
||||
|
||||
|
||||
def verify_access_token(token: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Check a bearer token issued by StarPunk
|
||||
|
||||
Returns:
|
||||
{"me", "client_id", "scope"} if active, otherwise None
|
||||
"""
|
||||
if not token:
|
||||
return None
|
||||
db = get_db(current_app)
|
||||
row = db.execute(
|
||||
"""
|
||||
SELECT id, client_id, scope FROM indieauth_tokens
|
||||
WHERE token_hash = ? AND revoked_at IS NULL AND expires_at > datetime('now')
|
||||
""",
|
||||
(_hash(token),),
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
|
||||
db.execute(
|
||||
"UPDATE indieauth_tokens SET last_used_at = datetime('now') WHERE id = ?",
|
||||
(row["id"],),
|
||||
)
|
||||
db.commit()
|
||||
return {"me": admin_me(), "client_id": row["client_id"], "scope": row["scope"]}
|
||||
|
||||
|
||||
def revoke_access_token(token: str) -> None:
|
||||
"""Revoke a token by value (unknown tokens are ignored, per RFC 7009)"""
|
||||
db = get_db(current_app)
|
||||
db.execute(
|
||||
"UPDATE indieauth_tokens SET revoked_at = datetime('now') WHERE token_hash = ? AND revoked_at IS NULL",
|
||||
(_hash(token or ""),),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def revoke_token_by_id(token_id: int) -> None:
|
||||
"""Revoke a token from the admin UI"""
|
||||
db = get_db(current_app)
|
||||
db.execute(
|
||||
"UPDATE indieauth_tokens SET revoked_at = datetime('now') WHERE id = ? AND revoked_at IS NULL",
|
||||
(token_id,),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def list_active_tokens() -> List[Dict[str, Any]]:
|
||||
"""Tokens that can still be used, newest first"""
|
||||
db = get_db(current_app)
|
||||
rows = db.execute(
|
||||
"""
|
||||
SELECT id, client_id, scope, created_at, expires_at, last_used_at
|
||||
FROM indieauth_tokens
|
||||
WHERE revoked_at IS NULL AND expires_at > datetime('now')
|
||||
ORDER BY created_at DESC
|
||||
"""
|
||||
).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Markdown extension: turn bare URLs into links (v2.3.0)
|
||||
|
||||
Python-Markdown only links <https://...>; notes often contain pasted URLs.
|
||||
"""
|
||||
|
||||
import re
|
||||
import xml.etree.ElementTree as etree
|
||||
|
||||
from markdown.extensions import Extension
|
||||
from markdown.inlinepatterns import InlineProcessor
|
||||
from markdown.treeprocessors import Treeprocessor
|
||||
from markdown.util import AtomicString
|
||||
|
||||
# Stop at whitespace and brackets; trailing punctuation is not part of the URL
|
||||
BARE_URL_RE = r"(?<![\w/\"'=])https?://[^\s<>()\[\]\"']*[^\s<>()\[\]\"'.,;:!?…]"
|
||||
|
||||
|
||||
class _BareUrl(InlineProcessor):
|
||||
def handleMatch(self, m, data):
|
||||
url = m.group(0)
|
||||
el = etree.Element("a")
|
||||
el.set("href", url)
|
||||
el.text = AtomicString(url)
|
||||
return el, m.start(0), m.end(0)
|
||||
|
||||
|
||||
class _UnnestLinks(Treeprocessor):
|
||||
"""[https://x](https://x) would put a link inside a link; keep the outer one"""
|
||||
|
||||
def run(self, root):
|
||||
for outer in root.iter("a"):
|
||||
for inner in list(outer.iter("a"))[1:]:
|
||||
inner.tag = "span"
|
||||
inner.attrib.clear()
|
||||
|
||||
|
||||
class BareUrlExtension(Extension):
|
||||
def extendMarkdown(self, md):
|
||||
# Below links (160) and <autolinks> (120), so those win
|
||||
md.inlinePatterns.register(_BareUrl(BARE_URL_RE, md), "bare_url", 105)
|
||||
md.treeprocessors.register(_UnnestLinks(md), "unnest_links", 0)
|
||||
+172
-70
@@ -14,15 +14,21 @@ Debug file management (v1.5.0 Phase 2):
|
||||
- Debug file saving disabled by default
|
||||
- Automatic cleanup of old debug files
|
||||
- Size limit enforcement
|
||||
|
||||
Memory-safe image processing (v1.5.1):
|
||||
- Early downscale for large images (>4096px) to prevent OOM
|
||||
- PIL Image passed through pipeline (not bytes) to avoid memory copies
|
||||
- Explicit memory cleanup after processing steps
|
||||
"""
|
||||
|
||||
from PIL import Image, ImageOps
|
||||
from pathlib import Path
|
||||
from datetime import datetime, timedelta
|
||||
import time
|
||||
import uuid
|
||||
import io
|
||||
import shutil
|
||||
from typing import Optional, List, Dict, Tuple
|
||||
from typing import Optional, List, Dict, Tuple, Union
|
||||
from flask import current_app
|
||||
|
||||
# HEIC/HEIF support - import registers with Pillow automatically
|
||||
@@ -50,6 +56,11 @@ MIN_QUALITY = 70 # Minimum JPEG quality before rejection (v1.4.
|
||||
MIN_DIMENSION = 640 # Minimum dimension before rejection (v1.4.0)
|
||||
MAX_IMAGES_PER_NOTE = 4
|
||||
|
||||
# Memory safety limits (v1.5.1 - fix for OOM on large phone images)
|
||||
# Early downscale very large images to prevent memory exhaustion
|
||||
# A 4096x4096 image = 50MB uncompressed, manageable per worker
|
||||
MEMORY_SAFE_DIMENSION = 4096 # Downscale to this before heavy processing
|
||||
|
||||
# Variant specifications (v1.4.0 Phase 2)
|
||||
VARIANT_SPECS = {
|
||||
'thumb': {'size': (150, 150), 'crop': True},
|
||||
@@ -82,21 +93,26 @@ def get_optimization_params(file_size: int) -> Tuple[int, int]:
|
||||
return (1280, 85)
|
||||
|
||||
|
||||
def validate_image(file_data: bytes, filename: str) -> Tuple[bytes, str, int, int]:
|
||||
# Shown to clients; the parser's own message names internal objects (v2.9.0)
|
||||
UNSUPPORTED_IMAGE = "Not a supported image: use JPEG, PNG, GIF, WebP or HEIC"
|
||||
|
||||
|
||||
def validate_image(file_data: bytes, filename: str) -> Tuple[Image.Image, str, int, int]:
|
||||
"""
|
||||
Validate image file
|
||||
Validate image file and return PIL Image for memory-efficient processing
|
||||
|
||||
Per Q11: Validate MIME type using Pillow
|
||||
Per Q6: Reject if >50MB or >4096px (updated v1.4.0)
|
||||
Per Q6: Reject if >50MB or >12000px
|
||||
Per v1.4.2: Convert HEIC to JPEG (browsers cannot display HEIC)
|
||||
Per v1.5.1: Return PIL Image (not bytes) to avoid memory copies
|
||||
|
||||
Args:
|
||||
file_data: Raw file bytes
|
||||
filename: Original filename
|
||||
|
||||
Returns:
|
||||
Tuple of (file_data, mime_type, width, height)
|
||||
Note: file_data may be converted (e.g., HEIC to JPEG)
|
||||
Tuple of (pil_image, mime_type, original_width, original_height)
|
||||
Note: Image may be downscaled for memory safety but original dimensions returned
|
||||
|
||||
Raises:
|
||||
ValueError: If file is invalid
|
||||
@@ -146,44 +162,38 @@ def validate_image(file_data: bytes, filename: str) -> Tuple[bytes, str, int, in
|
||||
current_app.logger.info(f'Saved failed upload for analysis: {debug_file}')
|
||||
except RuntimeError:
|
||||
pass # Outside app context (e.g., tests)
|
||||
raise ValueError(f"Invalid or corrupted image: {e}")
|
||||
raise ValueError(UNSUPPORTED_IMAGE) from e
|
||||
else:
|
||||
raise ValueError(f"Invalid or corrupted image: {e}")
|
||||
raise ValueError(UNSUPPORTED_IMAGE) from e
|
||||
|
||||
# Store original format before any conversions
|
||||
original_format = img.format
|
||||
|
||||
# HEIC/HEIF conversion (v1.4.2)
|
||||
# HEIC cannot be displayed in browsers, convert to JPEG
|
||||
if img.format in ('HEIF', 'HEIC'):
|
||||
if original_format in ('HEIF', 'HEIC'):
|
||||
if not HEIC_SUPPORTED:
|
||||
raise ValueError(
|
||||
"HEIC/HEIF images require pillow-heif library. "
|
||||
"Please convert to JPEG before uploading."
|
||||
)
|
||||
# Convert HEIC to JPEG in memory
|
||||
output = io.BytesIO()
|
||||
# Convert to RGB if needed (HEIC may have alpha channel)
|
||||
if img.mode in ('RGBA', 'P'):
|
||||
img = img.convert('RGB')
|
||||
img.save(output, format='JPEG', quality=95)
|
||||
output.seek(0)
|
||||
# Re-open as JPEG for further processing
|
||||
file_data = output.getvalue()
|
||||
img = Image.open(io.BytesIO(file_data))
|
||||
# Mark format as JPEG for later processing
|
||||
original_format = 'JPEG'
|
||||
|
||||
# MPO (Multi-Picture Object) conversion (v1.4.2)
|
||||
# MPO is used by iPhones for depth/portrait photos - extract primary image as JPEG
|
||||
if img.format == 'MPO':
|
||||
output = io.BytesIO()
|
||||
# MPO is used by iPhones for depth/portrait photos - extract primary image
|
||||
if original_format == 'MPO':
|
||||
# Convert to RGB if needed
|
||||
if img.mode in ('RGBA', 'P'):
|
||||
img = img.convert('RGB')
|
||||
img.save(output, format='JPEG', quality=95)
|
||||
output.seek(0)
|
||||
file_data = output.getvalue()
|
||||
img = Image.open(io.BytesIO(file_data))
|
||||
original_format = 'JPEG'
|
||||
|
||||
# Check format is allowed
|
||||
if img.format:
|
||||
format_lower = img.format.lower()
|
||||
if original_format:
|
||||
format_lower = original_format.lower()
|
||||
mime_type = f'image/{format_lower}'
|
||||
|
||||
# Special case: JPEG format can be reported as 'jpeg'
|
||||
@@ -195,26 +205,29 @@ def validate_image(file_data: bytes, filename: str) -> Tuple[bytes, str, int, in
|
||||
try:
|
||||
current_app.logger.warning(
|
||||
f'Media upload rejected format: filename="{filename}", '
|
||||
f'detected_format="{img.format}", mime_type="{mime_type}"'
|
||||
f'detected_format="{original_format}", mime_type="{mime_type}"'
|
||||
)
|
||||
except RuntimeError:
|
||||
pass # Outside app context
|
||||
raise ValueError(f"Invalid image format '{img.format}'. Accepted: JPEG, PNG, GIF, WebP")
|
||||
raise ValueError(f"Invalid image format '{original_format}'. Accepted: JPEG, PNG, GIF, WebP")
|
||||
else:
|
||||
raise ValueError("Could not determine image format")
|
||||
|
||||
# Store original dimensions before any resizing
|
||||
orig_width, orig_height = img.size
|
||||
|
||||
# Check dimensions (v1.4.2: increased to 12000px to support modern phone cameras)
|
||||
# Images will be resized by optimize_image() anyway
|
||||
width, height = img.size
|
||||
if max(width, height) > MAX_DIMENSION:
|
||||
if max(orig_width, orig_height) > MAX_DIMENSION:
|
||||
raise ValueError(f"Image dimensions too large. Maximum is {MAX_DIMENSION}x{MAX_DIMENSION} pixels")
|
||||
|
||||
# Check for animated GIF (v1.4.0)
|
||||
# Animated GIFs cannot be resized, so reject if >10MB
|
||||
if img.format == 'GIF':
|
||||
is_animated_gif = False
|
||||
if original_format == 'GIF':
|
||||
try:
|
||||
img.seek(1) # Try to seek to second frame
|
||||
# If successful, it's animated
|
||||
is_animated_gif = True
|
||||
if file_size > MAX_OUTPUT_SIZE:
|
||||
raise ValueError(
|
||||
"Animated GIF too large. Maximum size for animated GIFs is 10MB. "
|
||||
@@ -225,10 +238,38 @@ def validate_image(file_data: bytes, filename: str) -> Tuple[bytes, str, int, in
|
||||
# Not animated, continue normally
|
||||
pass
|
||||
|
||||
return file_data, mime_type, width, height
|
||||
# v1.5.1: Early downscale for memory safety
|
||||
# Large phone images (48MP+) can use 500MB+ per copy in memory
|
||||
# Downscale early to MEMORY_SAFE_DIMENSION to reduce memory footprint
|
||||
# This happens BEFORE optimize_image to prevent OOM during processing
|
||||
if not is_animated_gif and max(orig_width, orig_height) > MEMORY_SAFE_DIMENSION:
|
||||
try:
|
||||
current_app.logger.info(
|
||||
f'Memory safety downscale: {orig_width}x{orig_height} -> '
|
||||
f'{MEMORY_SAFE_DIMENSION}x{MEMORY_SAFE_DIMENSION} max'
|
||||
)
|
||||
except RuntimeError:
|
||||
pass # Outside app context
|
||||
|
||||
# Apply EXIF orientation before resize
|
||||
img = ImageOps.exif_transpose(img)
|
||||
|
||||
# Use thumbnail() for memory-efficient in-place resize
|
||||
img.thumbnail((MEMORY_SAFE_DIMENSION, MEMORY_SAFE_DIMENSION), Image.Resampling.LANCZOS)
|
||||
|
||||
# Store the format on the image object for later use
|
||||
img.format = original_format
|
||||
|
||||
# Clear file_data reference to allow garbage collection
|
||||
del file_data
|
||||
|
||||
return img, mime_type, orig_width, orig_height
|
||||
|
||||
|
||||
def optimize_image(image_data: bytes, original_size: int = None) -> Tuple[Image.Image, int, int, bytes]:
|
||||
def optimize_image(
|
||||
image_input: Union[Image.Image, bytes],
|
||||
original_size: int = None
|
||||
) -> Tuple[Image.Image, int, int, bytes]:
|
||||
"""
|
||||
Optimize image for web display with size-aware strategy
|
||||
|
||||
@@ -237,8 +278,12 @@ def optimize_image(image_data: bytes, original_size: int = None) -> Tuple[Image.
|
||||
- Iterative quality reduction if needed
|
||||
- Target output <=10MB
|
||||
|
||||
Per v1.5.1:
|
||||
- Accept PIL Image directly to avoid memory copies
|
||||
- Image may already be downscaled for memory safety
|
||||
|
||||
Args:
|
||||
image_data: Raw image bytes
|
||||
image_input: PIL Image object or raw image bytes
|
||||
original_size: Original file size (for tiered optimization)
|
||||
|
||||
Returns:
|
||||
@@ -247,24 +292,35 @@ def optimize_image(image_data: bytes, original_size: int = None) -> Tuple[Image.
|
||||
Raises:
|
||||
ValueError: If image cannot be optimized to target size
|
||||
"""
|
||||
if original_size is None:
|
||||
original_size = len(image_data)
|
||||
# Handle both PIL Image and bytes input (v1.5.1)
|
||||
if isinstance(image_input, Image.Image):
|
||||
img = image_input
|
||||
# For PIL input, use provided size or estimate from current dimensions
|
||||
if original_size is None:
|
||||
# Estimate: assume worst case of uncompressed RGB
|
||||
original_size = img.width * img.height * 3
|
||||
else:
|
||||
# Legacy bytes input path
|
||||
if original_size is None:
|
||||
original_size = len(image_input)
|
||||
img = Image.open(io.BytesIO(image_input))
|
||||
|
||||
# Get initial optimization parameters based on input size
|
||||
max_dim, quality = get_optimization_params(original_size)
|
||||
|
||||
img = Image.open(io.BytesIO(image_data))
|
||||
|
||||
# Save original format before any processing (copy() loses this)
|
||||
original_format = img.format
|
||||
original_format = img.format or 'JPEG'
|
||||
|
||||
# Correct EXIF orientation (per ADR-058), except for GIFs
|
||||
img = ImageOps.exif_transpose(img) if img.format != 'GIF' else img
|
||||
# Note: For v1.5.1, this may already be done in validate_image for large images
|
||||
if original_format != 'GIF':
|
||||
img = ImageOps.exif_transpose(img)
|
||||
|
||||
# For animated GIFs, return as-is (already validated in validate_image)
|
||||
if img.format == 'GIF' and getattr(img, 'is_animated', False):
|
||||
# Already checked size in validate_image, just return original
|
||||
return img, img.size[0], img.size[1], image_data
|
||||
# For animated GIFs, serialize and return (already validated in validate_image)
|
||||
if original_format == 'GIF' and getattr(img, 'is_animated', False):
|
||||
output = io.BytesIO()
|
||||
img.save(output, format='GIF')
|
||||
return img, img.size[0], img.size[1], output.getvalue()
|
||||
|
||||
# Iterative optimization loop
|
||||
while True:
|
||||
@@ -277,8 +333,7 @@ def optimize_image(image_data: bytes, original_size: int = None) -> Tuple[Image.
|
||||
|
||||
# Save to bytes to check size
|
||||
output = io.BytesIO()
|
||||
# Use original format (copy() loses the format attribute)
|
||||
save_format = original_format or 'JPEG'
|
||||
save_format = original_format
|
||||
save_kwargs = {'optimize': True}
|
||||
|
||||
if save_format in ['JPEG', 'JPG']:
|
||||
@@ -528,6 +583,7 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
Per Q2: Date-organized path: /media/YYYY/MM/uuid.ext
|
||||
Per Q6: Validate, optimize, then save
|
||||
Per v1.4.0: Size-aware optimization with iterative quality reduction
|
||||
Per v1.6.0 Phase 0: Timing logs for performance monitoring
|
||||
|
||||
Args:
|
||||
file_data: Raw file bytes
|
||||
@@ -544,24 +600,42 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
# Capture file size for logging
|
||||
file_size = len(file_data)
|
||||
|
||||
# Timing instrumentation (v1.6.0 Phase 0)
|
||||
timing_start = time.monotonic()
|
||||
timing_validation = 0.0
|
||||
timing_optimization = 0.0
|
||||
timing_variants = 0.0
|
||||
timing_save = 0.0
|
||||
|
||||
try:
|
||||
# Validate image (returns 4-tuple with potentially converted bytes)
|
||||
# Validate image (v1.5.1: returns PIL Image to avoid memory copies)
|
||||
try:
|
||||
file_data, mime_type, orig_width, orig_height = validate_image(file_data, filename)
|
||||
t_start = time.monotonic()
|
||||
validated_img, mime_type, orig_width, orig_height = validate_image(file_data, filename)
|
||||
timing_validation = time.monotonic() - t_start
|
||||
# Clear file_data early to reduce memory pressure
|
||||
del file_data
|
||||
except ValueError as e:
|
||||
timing_validation = time.monotonic() - timing_start
|
||||
current_app.logger.warning(
|
||||
f'Media upload validation failed: filename="{filename}", '
|
||||
f'size={file_size}b, error="{e}"'
|
||||
f'size={file_size}b, error="{e}", timing: validation={timing_validation:.1f}s'
|
||||
)
|
||||
raise
|
||||
|
||||
# Optimize image with size-aware strategy (now returns 4-tuple with bytes)
|
||||
# Optimize image with size-aware strategy (v1.5.1: accepts PIL Image)
|
||||
try:
|
||||
optimized_img, width, height, optimized_bytes = optimize_image(file_data, file_size)
|
||||
t_start = time.monotonic()
|
||||
optimized_img, width, height, optimized_bytes = optimize_image(validated_img, file_size)
|
||||
timing_optimization = time.monotonic() - t_start
|
||||
# Clear validated_img reference (optimized_img is the result now)
|
||||
del validated_img
|
||||
except ValueError as e:
|
||||
timing_optimization = time.monotonic() - t_start
|
||||
current_app.logger.warning(
|
||||
f'Media upload optimization failed: filename="{filename}", '
|
||||
f'size={file_size}b, error="{e}"'
|
||||
f'size={file_size}b, error="{e}", '
|
||||
f'timing: validation={timing_validation:.1f}s, optimization={timing_optimization:.1f}s'
|
||||
)
|
||||
raise
|
||||
|
||||
@@ -601,7 +675,8 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
temp_subdir = None
|
||||
|
||||
try:
|
||||
# Step 1: Save original to temp directory
|
||||
# Step 1: Generate variants to temp directory
|
||||
t_start = time.monotonic()
|
||||
media_dir = Path(current_app.config.get('DATA_PATH', 'data')) / 'media'
|
||||
temp_dir = media_dir / '.tmp'
|
||||
temp_dir.mkdir(parents=True, exist_ok=True)
|
||||
@@ -623,8 +698,10 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
optimized_bytes,
|
||||
db
|
||||
)
|
||||
timing_variants = time.monotonic() - t_start
|
||||
|
||||
# Step 3: Begin transaction
|
||||
# Step 3: Begin transaction and save to database
|
||||
t_start = time.monotonic()
|
||||
db.execute("BEGIN TRANSACTION")
|
||||
|
||||
# Step 4: Insert media record
|
||||
@@ -667,6 +744,7 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
|
||||
# Step 7: Commit transaction (after files are moved successfully)
|
||||
db.commit()
|
||||
timing_save = time.monotonic() - t_start
|
||||
|
||||
# Step 8: Clean up temp directory
|
||||
try:
|
||||
@@ -679,6 +757,9 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
variants = [v for v in variant_metadata if v['variant_type'] != 'original']
|
||||
|
||||
except Exception as e:
|
||||
# Capture timing for failed save operation
|
||||
timing_save = time.monotonic() - t_start if 't_start' in locals() else 0.0
|
||||
|
||||
# Rollback database on any failure (best effort)
|
||||
try:
|
||||
db.rollback()
|
||||
@@ -722,19 +803,27 @@ def save_media(file_data: bytes, filename: str) -> Dict:
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# Log and re-raise
|
||||
# Calculate total timing
|
||||
timing_total = time.monotonic() - timing_start
|
||||
|
||||
# Log and re-raise (with timing data)
|
||||
current_app.logger.warning(
|
||||
f'Media upload atomic operation failed: filename="{filename}", '
|
||||
f'error="{e}"'
|
||||
f'error="{e}", timing: validation={timing_validation:.1f}s, '
|
||||
f'optimization={timing_optimization:.1f}s, variants={timing_variants:.1f}s, '
|
||||
f'save={timing_save:.1f}s, total={timing_total:.1f}s'
|
||||
)
|
||||
raise
|
||||
|
||||
# Log success
|
||||
# Log success with timing data (v1.6.0 Phase 0)
|
||||
timing_total = time.monotonic() - timing_start
|
||||
was_optimized = len(optimized_bytes) < file_size
|
||||
current_app.logger.info(
|
||||
f'Media upload successful: filename="{filename}", '
|
||||
f'stored="{stored_filename}", size={len(optimized_bytes)}b, '
|
||||
f'optimized={was_optimized}, variants={len(variants)}'
|
||||
f'optimized={was_optimized}, variants={len(variants)}, '
|
||||
f'timing: validation={timing_validation:.1f}s, optimization={timing_optimization:.1f}s, '
|
||||
f'variants={timing_variants:.1f}s, save={timing_save:.1f}s, total={timing_total:.1f}s'
|
||||
)
|
||||
|
||||
return {
|
||||
@@ -783,20 +872,33 @@ def attach_media_to_note(note_id: int, media_ids: List[int], captions: List[str]
|
||||
|
||||
db = get_db(current_app)
|
||||
|
||||
# Delete existing associations (for edit case)
|
||||
db.execute("DELETE FROM note_media WHERE note_id = ?", (note_id,))
|
||||
try:
|
||||
# Delete existing associations (for edit case)
|
||||
db.execute("DELETE FROM note_media WHERE note_id = ?", (note_id,))
|
||||
|
||||
# Insert new associations
|
||||
for i, (media_id, caption) in enumerate(zip(media_ids, captions)):
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO note_media (note_id, media_id, display_order, caption)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(note_id, media_id, i, caption or None)
|
||||
)
|
||||
# Insert new associations
|
||||
for i, (media_id, caption) in enumerate(zip(media_ids, captions)):
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO note_media (note_id, media_id, display_order, caption)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(note_id, media_id, i, caption or None)
|
||||
)
|
||||
|
||||
db.commit()
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
|
||||
# Feeds and pages show the photos: refresh caches and tell WebSub (v2.10.1)
|
||||
try:
|
||||
from starpunk.notes import _websub_changed, get_note
|
||||
note = get_note(id=note_id, load_content=False)
|
||||
if note is not None:
|
||||
_websub_changed(note, was_public=note.published)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue WebSub update for note {note_id}: {e}")
|
||||
|
||||
|
||||
def get_note_media(note_id: int) -> List[Dict]:
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
"""
|
||||
Microformats2 consuming helpers (v2.8.0)
|
||||
|
||||
Shared by Webmention processing and author discovery:
|
||||
- representative h-card of a page
|
||||
- the h-entry on a page that responds to a given URL
|
||||
- the author of an h-entry (authorship algorithm)
|
||||
|
||||
References:
|
||||
- https://microformats.org/wiki/representative-h-card-parsing
|
||||
- https://indieweb.org/authorship-spec
|
||||
"""
|
||||
|
||||
from typing import Iterator, Optional
|
||||
from urllib.parse import urldefrag, urlparse
|
||||
|
||||
RESPONSE_PROPERTIES = ('in-reply-to', 'like-of', 'repost-of', 'bookmark-of')
|
||||
|
||||
|
||||
def normalize_url(url: str) -> str:
|
||||
"""URL for comparison: no fragment, no trailing slash, lowercase scheme/host"""
|
||||
if not url:
|
||||
return ''
|
||||
url = urldefrag(url.strip())[0]
|
||||
parsed = urlparse(url)
|
||||
url = parsed._replace(scheme=parsed.scheme.lower(), netloc=parsed.netloc.lower()).geturl()
|
||||
return url.rstrip('/')
|
||||
|
||||
|
||||
def value_of(value) -> Optional[str]:
|
||||
"""Plain value of an mf2 property value (strings, {'value':...}, {'html':...})"""
|
||||
if isinstance(value, dict):
|
||||
value = value.get('value')
|
||||
return value if isinstance(value, str) and value else None
|
||||
|
||||
|
||||
def first(item: dict, prop: str) -> Optional[str]:
|
||||
"""First plain value of an item's property"""
|
||||
for value in item.get('properties', {}).get(prop, []):
|
||||
result = value_of(value)
|
||||
if result:
|
||||
return result
|
||||
return None
|
||||
|
||||
|
||||
def values(item: dict, prop: str) -> list[str]:
|
||||
"""All plain values of an item's property"""
|
||||
return [v for v in (value_of(x) for x in item.get('properties', {}).get(prop, [])) if v]
|
||||
|
||||
|
||||
def walk(items: list) -> Iterator[dict]:
|
||||
"""Every microformat in items, depth first: top level, children, nested properties"""
|
||||
for item in items:
|
||||
if not isinstance(item, dict) or 'type' not in item:
|
||||
continue
|
||||
yield item
|
||||
yield from walk(item.get('children', []))
|
||||
for prop_values in item.get('properties', {}).values():
|
||||
yield from walk([v for v in prop_values if isinstance(v, dict)])
|
||||
|
||||
|
||||
def is_type(item: dict, mf_type: str) -> bool:
|
||||
return mf_type in item.get('type', [])
|
||||
|
||||
|
||||
def representative_hcard(parsed: dict, page_url: str) -> Optional[dict]:
|
||||
"""
|
||||
The page's representative h-card, per the parsing spec, in order:
|
||||
|
||||
1. An h-card whose uid and url both match the page URL
|
||||
2. An h-card whose url matches one of the page's rel=me URLs
|
||||
3. The only h-card on the page, if its url matches the page URL
|
||||
|
||||
Otherwise the page has none.
|
||||
"""
|
||||
page = normalize_url(page_url)
|
||||
cards = [item for item in walk(parsed.get('items', [])) if is_type(item, 'h-card')]
|
||||
|
||||
for card in cards:
|
||||
if page in map(normalize_url, values(card, 'uid')) and \
|
||||
page in map(normalize_url, values(card, 'url')):
|
||||
return card
|
||||
|
||||
rel_me = {normalize_url(u) for u in parsed.get('rels', {}).get('me', [])}
|
||||
for card in cards:
|
||||
if rel_me & set(map(normalize_url, values(card, 'url'))):
|
||||
return card
|
||||
|
||||
if len(cards) == 1 and page in map(normalize_url, values(cards[0], 'url')):
|
||||
return cards[0]
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def responding_entry(parsed: dict, target_url: str) -> Optional[dict]:
|
||||
"""
|
||||
The h-entry on a page that refers to target_url
|
||||
|
||||
Prefers an entry whose in-reply-to / like-of / repost-of / bookmark-of
|
||||
is the target, then one whose content links to it, then the first
|
||||
top-level h-entry.
|
||||
"""
|
||||
target = normalize_url(target_url)
|
||||
entries = [item for item in walk(parsed.get('items', [])) if is_type(item, 'h-entry')]
|
||||
top = [item for item in parsed.get('items', []) if is_type(item, 'h-entry')]
|
||||
|
||||
if not target:
|
||||
return top[0] if top else (entries[0] if entries else None)
|
||||
|
||||
for entry in entries:
|
||||
for prop in RESPONSE_PROPERTIES:
|
||||
if target in map(normalize_url, values(entry, prop)):
|
||||
return entry
|
||||
|
||||
for entry in entries:
|
||||
for content in entry.get('properties', {}).get('content', []):
|
||||
html = content.get('html', '') if isinstance(content, dict) else str(content)
|
||||
if target_url in html or target in html:
|
||||
return entry
|
||||
|
||||
return top[0] if top else (entries[0] if entries else None)
|
||||
|
||||
|
||||
def response_type(entry: dict, target_url: str) -> str:
|
||||
"""reply / like / repost / bookmark if the entry is one to target_url, else mention"""
|
||||
target = normalize_url(target_url)
|
||||
for prop, kind in zip(RESPONSE_PROPERTIES, ('reply', 'like', 'repost', 'bookmark')):
|
||||
if target in map(normalize_url, values(entry, prop)):
|
||||
return kind
|
||||
return 'mention'
|
||||
|
||||
|
||||
def entry_author(parsed: dict, entry: Optional[dict], page_url: str) -> dict:
|
||||
"""
|
||||
Author of an h-entry, following the authorship algorithm without fetching
|
||||
|
||||
1. The entry's author property (an h-card, a URL, or a name)
|
||||
2. The author of an h-feed that contains the entry
|
||||
3. The page's rel=author link
|
||||
4. The page's representative h-card
|
||||
|
||||
Returns:
|
||||
{'name', 'url', 'photo'}; values may be None
|
||||
"""
|
||||
author = None
|
||||
if entry:
|
||||
author = (entry.get('properties', {}).get('author') or [None])[0]
|
||||
if author is None:
|
||||
for feed in walk(parsed.get('items', [])):
|
||||
if is_type(feed, 'h-feed') and entry in feed.get('children', []):
|
||||
author = (feed.get('properties', {}).get('author') or [None])[0]
|
||||
break
|
||||
|
||||
if author is None:
|
||||
rel_author = parsed.get('rels', {}).get('author', [])
|
||||
author = rel_author[0] if rel_author else representative_hcard(parsed, page_url)
|
||||
|
||||
if isinstance(author, dict) and 'type' in author:
|
||||
return {
|
||||
'name': first(author, 'name'),
|
||||
'url': first(author, 'url'),
|
||||
'photo': first(author, 'photo'),
|
||||
}
|
||||
|
||||
text = value_of(author)
|
||||
if text and urlparse(text).scheme in ('http', 'https'):
|
||||
return {'name': None, 'url': text, 'photo': None}
|
||||
return {'name': text, 'url': None, 'photo': None}
|
||||
+625
-221
File diff suppressed because it is too large.
Load diff
+42
-6
@@ -31,12 +31,47 @@ class MigrationError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def create_migrations_table(conn):
|
||||
def split_sql_statements(script: str) -> list[str]:
|
||||
"""
|
||||
Split a SQL script into complete statements (v2.8.1)
|
||||
|
||||
Uses sqlite3.complete_statement, so semicolons inside strings and
|
||||
trigger bodies (BEGIN ... END) don't split a statement.
|
||||
"""
|
||||
statements = []
|
||||
current = ""
|
||||
for line in script.splitlines(keepends=True):
|
||||
current += line
|
||||
if sqlite3.complete_statement(current):
|
||||
if current.strip().strip(";").strip():
|
||||
statements.append(current.strip())
|
||||
current = ""
|
||||
if current.strip():
|
||||
statements.append(current.strip())
|
||||
return statements
|
||||
|
||||
|
||||
def execute_in_transaction(conn, script: str) -> None:
|
||||
"""
|
||||
Run a SQL script inside the connection's open transaction
|
||||
|
||||
Not conn.executescript(): that COMMITs first, which released the
|
||||
migration lock mid-migration and let another gunicorn worker apply the
|
||||
same migration again ("duplicate column name", v2.8.0 deploy).
|
||||
"""
|
||||
for statement in split_sql_statements(script):
|
||||
conn.execute(statement)
|
||||
|
||||
|
||||
def create_migrations_table(conn, commit=True):
|
||||
"""
|
||||
Create schema_migrations tracking table if it doesn't exist
|
||||
|
||||
Args:
|
||||
conn: SQLite connection
|
||||
commit: Commit afterwards. run_migrations passes False: committing
|
||||
there released its BEGIN IMMEDIATE lock straight away, so
|
||||
gunicorn workers applied migrations concurrently (v2.8.1)
|
||||
"""
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
@@ -51,7 +86,8 @@ def create_migrations_table(conn):
|
||||
ON schema_migrations(migration_name)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
if commit:
|
||||
conn.commit()
|
||||
|
||||
|
||||
def is_schema_current(conn):
|
||||
@@ -287,7 +323,7 @@ def apply_migration(conn, migration_name, migration_path, logger=None):
|
||||
|
||||
# Execute migration in transaction
|
||||
conn.execute("BEGIN")
|
||||
conn.executescript(migration_sql)
|
||||
execute_in_transaction(conn, migration_sql)
|
||||
|
||||
# Record migration as applied
|
||||
conn.execute(
|
||||
@@ -374,7 +410,7 @@ def run_migrations(db_path, logger=None):
|
||||
|
||||
try:
|
||||
# Ensure migrations tracking table exists
|
||||
create_migrations_table(conn)
|
||||
create_migrations_table(conn, commit=False)
|
||||
|
||||
# Quick check: have migrations already been applied by another worker?
|
||||
cursor = conn.execute("SELECT COUNT(*) FROM schema_migrations")
|
||||
@@ -472,8 +508,8 @@ def run_migrations(db_path, logger=None):
|
||||
|
||||
logger.debug(f"Applying migration: {migration_name}")
|
||||
|
||||
# Execute migration (already in transaction)
|
||||
conn.executescript(migration_sql)
|
||||
# Execute migration inside the locked transaction
|
||||
execute_in_transaction(conn, migration_sql)
|
||||
|
||||
# Record migration as applied
|
||||
conn.execute(
|
||||
|
||||
+120
-9
@@ -11,10 +11,12 @@ from dataclasses import dataclass, field, replace
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
# Third-party imports
|
||||
import markdown
|
||||
import nh3
|
||||
|
||||
# Local imports
|
||||
from starpunk.utils import (
|
||||
@@ -34,13 +36,63 @@ DEFAULT_AUTH_STATE_EXPIRY_MINUTES = 5
|
||||
DEFAULT_TOKEN_EXPIRY_DAYS = 90
|
||||
|
||||
# Constants - Markdown rendering
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br"]
|
||||
from starpunk.markdown_ext import BareUrlExtension
|
||||
|
||||
MARKDOWN_EXTENSIONS = ["extra", "codehilite", "nl2br", BareUrlExtension()]
|
||||
|
||||
# Constants - Content limits
|
||||
MAX_TITLE_LENGTH = 200
|
||||
|
||||
# HTML allowed in rendered notes (v2.9.0). Markdown passes raw HTML through,
|
||||
# and Micropub clients may send HTML, so scripts, event handlers, iframes and
|
||||
# javascript: links are stripped. class keeps code highlighting and
|
||||
# microformats; id keeps footnote links.
|
||||
ALLOWED_TAGS = set(nh3.ALLOWED_TAGS) | {"video", "audio", "source", "track", "picture"}
|
||||
ALLOWED_ATTRIBUTES = {
|
||||
**{tag: set(attrs) for tag, attrs in nh3.ALLOWED_ATTRIBUTES.items()},
|
||||
"*": {"class", "title", "id", "lang", "dir"},
|
||||
"img": set(nh3.ALLOWED_ATTRIBUTES.get("img", ())) | {"loading", "srcset", "sizes"},
|
||||
"video": {"src", "controls", "poster", "width", "height", "loop", "muted", "playsinline", "preload"},
|
||||
"audio": {"src", "controls", "loop", "muted", "preload"},
|
||||
"source": {"src", "type", "srcset", "media", "sizes"},
|
||||
"track": {"src", "kind", "label", "srclang", "default"},
|
||||
}
|
||||
|
||||
|
||||
def render_markdown(text: str) -> str:
|
||||
"""Note Markdown (or HTML) to sanitized HTML"""
|
||||
html = markdown.markdown(text, extensions=MARKDOWN_EXTENSIONS)
|
||||
return nh3.clean(html, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES)
|
||||
|
||||
|
||||
# Likes, reposts and bookmarks (v2.5.0, v2.9.0)
|
||||
REACTION_VERBS = {"like": "Liked", "repost": "Reposted", "bookmark": "Bookmarked"}
|
||||
REACTION_ICONS = {"like": "★", "repost": "🔁", "bookmark": "🔖"}
|
||||
|
||||
# A Markdown ATX heading: up to 6 '#', whitespace, then text
|
||||
TITLE_RE = re.compile(r"#{1,6}[ \t]+(\S.*)$")
|
||||
EXCERPT_LENGTH = 200
|
||||
|
||||
|
||||
def split_title(content: str) -> tuple[Optional[str], str]:
|
||||
"""
|
||||
Split a note's '# Heading' first line (its title, per Q22) off its content
|
||||
|
||||
The first line must be a Markdown heading: 1-6 '#' then a space and text.
|
||||
'#indieweb ...' (a hashtag) or a bare '#' is not a title. This is the one
|
||||
place that decides whether a note has a title (templates use has_title).
|
||||
|
||||
Returns:
|
||||
(title, remaining content); title is None if the note has no heading
|
||||
"""
|
||||
stripped = content.lstrip()
|
||||
first_line, _, rest = stripped.partition("\n")
|
||||
match = TITLE_RE.match(first_line)
|
||||
if not match:
|
||||
return None, content
|
||||
return match.group(1).strip().rstrip("#").strip(), rest.strip()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Note:
|
||||
"""
|
||||
@@ -114,6 +166,11 @@ class Note:
|
||||
# Optional fields
|
||||
deleted_at: Optional[datetime] = None
|
||||
content_hash: Optional[str] = None
|
||||
in_reply_to: Optional[str] = None # v1.6.0: URL being replied to
|
||||
like_of: Optional[str] = None # v2.5.0: URL liked (this note is a like)
|
||||
repost_of: Optional[str] = None # v2.5.0: URL reposted (this note is a repost)
|
||||
bookmark_of: Optional[str] = None # v2.9.0: URL bookmarked (this note is a bookmark)
|
||||
content_format: Optional[str] = None # v2.9.0: 'html' if sent as HTML via Micropub
|
||||
_cached_content: Optional[str] = field(
|
||||
default=None, repr=False, compare=False, init=False
|
||||
)
|
||||
@@ -171,6 +228,11 @@ class Note:
|
||||
deleted_at=deleted_at,
|
||||
_data_dir=data_dir,
|
||||
content_hash=data.get("content_hash"),
|
||||
in_reply_to=data.get("in_reply_to"),
|
||||
like_of=data.get("like_of"),
|
||||
repost_of=data.get("repost_of"),
|
||||
bookmark_of=data.get("bookmark_of"),
|
||||
content_format=data.get("content_format"),
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -229,17 +291,35 @@ class Note:
|
||||
<p>This is my note content...</p>
|
||||
"""
|
||||
if self._cached_html is None:
|
||||
# Render markdown to HTML
|
||||
html = markdown.markdown(
|
||||
self.content, # This triggers lazy load of content
|
||||
extensions=MARKDOWN_EXTENSIONS,
|
||||
)
|
||||
# Render markdown to sanitized HTML (v2.9.0)
|
||||
html = render_markdown(self.content) # triggers lazy load of content
|
||||
|
||||
# Cache it
|
||||
object.__setattr__(self, "_cached_html", html)
|
||||
|
||||
return self._cached_html
|
||||
|
||||
@property
|
||||
def body_html(self) -> str:
|
||||
"""
|
||||
Rendered HTML without the title heading
|
||||
|
||||
Pages show a titled note's title as their own heading, so the body
|
||||
leaves it out to avoid showing it twice. Untitled notes: same as html.
|
||||
"""
|
||||
title, body = split_title(self.content)
|
||||
if title is None:
|
||||
return self.html
|
||||
return render_markdown(body)
|
||||
|
||||
@property
|
||||
def has_title(self) -> bool:
|
||||
"""True if the note starts with a '# Heading' title (an article)"""
|
||||
try:
|
||||
return split_title(self.content)[0] is not None
|
||||
except (FileNotFoundError, OSError):
|
||||
return False
|
||||
|
||||
@property
|
||||
def title(self) -> str:
|
||||
"""
|
||||
@@ -307,10 +387,10 @@ class Note:
|
||||
'This is my note content. It has some interesting points...'
|
||||
"""
|
||||
try:
|
||||
# Get content
|
||||
content = self.content
|
||||
# Leave out the title; previews show it separately
|
||||
_, content = split_title(self.content)
|
||||
|
||||
# Remove markdown heading syntax from first line
|
||||
# Remove markdown heading syntax
|
||||
lines = content.split("\n")
|
||||
cleaned_lines = []
|
||||
for line in lines:
|
||||
@@ -333,12 +413,43 @@ class Note:
|
||||
excerpt = excerpt[:last_space]
|
||||
excerpt += "..."
|
||||
|
||||
if not excerpt and self.reacts_to:
|
||||
# A like, repost or bookmark with no comment
|
||||
return f"{REACTION_VERBS[self.kind]} {self.reacts_to}"
|
||||
|
||||
return excerpt
|
||||
|
||||
except (FileNotFoundError, OSError):
|
||||
# If file doesn't exist, return slug
|
||||
return self.slug
|
||||
|
||||
@property
|
||||
def kind(self) -> str:
|
||||
"""'like', 'repost', 'bookmark' (v2.9.0) or 'note' (v2.5.0)"""
|
||||
if self.like_of:
|
||||
return "like"
|
||||
if self.repost_of:
|
||||
return "repost"
|
||||
if self.bookmark_of:
|
||||
return "bookmark"
|
||||
return "note"
|
||||
|
||||
@property
|
||||
def reacts_to(self) -> Optional[str]:
|
||||
"""The URL a like, repost or bookmark points at"""
|
||||
return self.like_of or self.repost_of or self.bookmark_of
|
||||
|
||||
@property
|
||||
def feed_html(self) -> str:
|
||||
"""HTML for feeds: a repost/like line, then the note's own HTML"""
|
||||
from html import escape
|
||||
|
||||
if not self.reacts_to:
|
||||
return self.html
|
||||
url = escape(self.reacts_to, quote=True)
|
||||
line = f'<p>{REACTION_ICONS[self.kind]} {REACTION_VERBS[self.kind]} <a href="{url}">{url}</a></p>'
|
||||
return line + (self.html or "")
|
||||
|
||||
@property
|
||||
def permalink(self) -> str:
|
||||
"""
|
||||
|
||||
+294
-18
@@ -133,12 +133,22 @@ def _get_existing_slugs(db) -> set[str]:
|
||||
# Core CRUD Functions
|
||||
|
||||
|
||||
# update_note: leave a field as it is
|
||||
UNCHANGED = object()
|
||||
|
||||
|
||||
def create_note(
|
||||
content: str,
|
||||
published: bool = False,
|
||||
created_at: Optional[datetime] = None,
|
||||
custom_slug: Optional[str] = None,
|
||||
tags: Optional[list[str]] = None
|
||||
tags: Optional[list[str]] = None,
|
||||
in_reply_to: Optional[str] = None,
|
||||
like_of: Optional[str] = None,
|
||||
repost_of: Optional[str] = None,
|
||||
has_media: bool = False,
|
||||
bookmark_of: Optional[str] = None,
|
||||
content_format: Optional[str] = None,
|
||||
) -> Note:
|
||||
"""
|
||||
Create a new note
|
||||
@@ -153,6 +163,7 @@ def create_note(
|
||||
created_at: Creation timestamp (default: current UTC time)
|
||||
custom_slug: Optional custom slug (from Micropub mp-slug property)
|
||||
tags: Optional list of tag display names (v1.3.0)
|
||||
in_reply_to: Optional URL being replied to (v1.6.0)
|
||||
|
||||
Returns:
|
||||
Note object with all metadata and content loaded
|
||||
@@ -201,7 +212,14 @@ def create_note(
|
||||
- created_at and updated_at set to same value initially
|
||||
"""
|
||||
# 1. VALIDATION (before any changes)
|
||||
if not content or not content.strip():
|
||||
# Likes and reposts may have no comment (v2.5.0), photo posts no text (v2.6.5)
|
||||
# Bookmarks too (v2.9.0)
|
||||
if sum(bool(u) for u in (like_of, repost_of, bookmark_of)) > 1:
|
||||
raise InvalidNoteDataError(
|
||||
"like_of", like_of, "A note can only be one of a like, repost or bookmark"
|
||||
)
|
||||
content = content or ""
|
||||
if not content.strip() and not (like_of or repost_of or bookmark_of or has_media):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
@@ -262,10 +280,12 @@ def create_note(
|
||||
try:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO notes (slug, file_path, published, created_at, updated_at, content_hash)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO notes (slug, file_path, published, created_at, updated_at, content_hash,
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(slug, file_path_rel, published, created_at, updated_at, content_hash),
|
||||
(slug, file_path_rel, published, created_at, updated_at, content_hash,
|
||||
in_reply_to, like_of, repost_of, bookmark_of, content_format),
|
||||
)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
@@ -313,9 +333,72 @@ def create_note(
|
||||
# FTS update failure should not prevent note creation
|
||||
current_app.logger.warning(f"Failed to update FTS index for note {slug}: {e}")
|
||||
|
||||
# 11. QUEUE WEBMENTIONS (v1.6.0 Phase 3)
|
||||
# Only queue if note is published (not draft)
|
||||
if published:
|
||||
try:
|
||||
from starpunk.webmention_sender import queue_webmentions_for_note
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
from starpunk.utils import absolute_url_for
|
||||
note_url = absolute_url_for("public.note", slug=slug)
|
||||
|
||||
# Get rendered HTML from note
|
||||
html_content = note.html
|
||||
|
||||
# Queue webmentions (best-effort, failures logged but don't fail note creation)
|
||||
# The reply/like/repost target is notified first (v2.5.0)
|
||||
queue_webmentions_for_note(
|
||||
db_path=db_path,
|
||||
note_id=note_id,
|
||||
note_url=note_url,
|
||||
content_html=html_content,
|
||||
in_reply_to=in_reply_to or like_of or repost_of or bookmark_of
|
||||
)
|
||||
except Exception as e:
|
||||
# Webmention queueing failure should not prevent note creation
|
||||
current_app.logger.warning(f"Failed to queue webmentions for note {slug}: {e}")
|
||||
|
||||
# 12. FETCH REPLY CONTEXT (v1.6.0 Phase 3)
|
||||
# Fetch context for the post being replied to, liked or reposted
|
||||
context_url = in_reply_to or like_of or repost_of or bookmark_of
|
||||
if context_url:
|
||||
try:
|
||||
from starpunk.reply_context import fetch_reply_context
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
|
||||
# Fetch context (best-effort, failures logged but don't fail note creation)
|
||||
fetch_reply_context(db_path, context_url, site_url)
|
||||
except Exception as e:
|
||||
# Reply context fetch failure should not prevent note creation
|
||||
current_app.logger.warning(f"Failed to fetch reply context for {in_reply_to}: {e}")
|
||||
|
||||
# A note with photos is announced when they're attached
|
||||
# (attach_media_to_note), so subscribers never get it without them
|
||||
if not has_media:
|
||||
_websub_changed(note)
|
||||
return note
|
||||
|
||||
|
||||
def _tag_names(note_id: int) -> list[str]:
|
||||
"""A note's tag names, for WebSub tag topics (best effort)"""
|
||||
try:
|
||||
from starpunk.tags import get_note_tags
|
||||
return [t["name"] for t in get_note_tags(note_id)]
|
||||
except Exception:
|
||||
return []
|
||||
|
||||
|
||||
def _websub_changed(note, was_public: bool = False, old_tags=()) -> None:
|
||||
"""Clear cached feeds and queue WebSub deliveries (v2.10.0, best effort)"""
|
||||
try:
|
||||
from starpunk import websub
|
||||
websub.note_changed(note, was_public=was_public, old_tags=old_tags)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue WebSub update for note {note.slug}: {e}")
|
||||
|
||||
|
||||
def get_note(
|
||||
slug: Optional[str] = None, id: Optional[int] = None, load_content: bool = True
|
||||
) -> Optional[Note]:
|
||||
@@ -432,6 +515,7 @@ def list_notes(
|
||||
offset: int = 0,
|
||||
order_by: str = "created_at",
|
||||
order_dir: str = "DESC",
|
||||
likes: Optional[bool] = None,
|
||||
) -> list[Note]:
|
||||
"""
|
||||
List notes with filtering and pagination
|
||||
@@ -524,6 +608,11 @@ def list_notes(
|
||||
params = []
|
||||
if published_only:
|
||||
query += " AND published = 1"
|
||||
# likes: None = all notes, False = leave likes out, True = only likes (v2.5.0)
|
||||
if likes is True:
|
||||
query += " AND like_of IS NOT NULL"
|
||||
elif likes is False:
|
||||
query += " AND like_of IS NULL"
|
||||
|
||||
# Add ordering (safe because order_by validated above)
|
||||
query += f" ORDER BY {order_by} {order_dir}"
|
||||
@@ -548,11 +637,16 @@ def update_note(
|
||||
id: Optional[int] = None,
|
||||
content: Optional[str] = None,
|
||||
published: Optional[bool] = None,
|
||||
tags: Optional[list[str]] = None
|
||||
tags: Optional[list[str]] = None,
|
||||
in_reply_to: Optional[str] = None,
|
||||
has_media: bool = False,
|
||||
content_format=UNCHANGED,
|
||||
) -> Note:
|
||||
"""
|
||||
Update a note's content and/or published status
|
||||
|
||||
content_format (v2.9.0): 'html' or None (Markdown); left alone unless given
|
||||
|
||||
Updates note content and/or metadata, maintaining atomic synchronization
|
||||
between file and database. At least one of content or published must
|
||||
be provided.
|
||||
@@ -563,6 +657,7 @@ def update_note(
|
||||
content: New markdown content (None = no change)
|
||||
published: New published status (None = no change)
|
||||
tags: New tags list (None = no change, [] = remove all tags) (v1.3.0)
|
||||
in_reply_to: New in-reply-to URL (None = no change) (v1.6.0)
|
||||
|
||||
Returns:
|
||||
Updated Note object with new content and metadata
|
||||
@@ -618,22 +713,26 @@ def update_note(
|
||||
if slug is not None and id is not None:
|
||||
raise ValueError("Cannot provide both slug and id")
|
||||
|
||||
if content is None and published is None and tags is None:
|
||||
raise ValueError("Must provide at least one of content, published, or tags to update")
|
||||
if content is None and published is None and tags is None and in_reply_to is None:
|
||||
raise ValueError("Must provide at least one of content, published, tags, or in_reply_to to update")
|
||||
|
||||
# Validate content if provided
|
||||
if content is not None:
|
||||
if not content or not content.strip():
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
# Validate content if provided (checked against the note below for
|
||||
# likes/reposts, whose comment may be empty)
|
||||
empty_content = content is not None and not content.strip()
|
||||
|
||||
# 2. GET EXISTING NOTE
|
||||
existing_note = get_note(slug=slug, id=id, load_content=False)
|
||||
# Phase 6: Load content to get old HTML for webmention URL diffing
|
||||
existing_note = get_note(slug=slug, id=id, load_content=True)
|
||||
|
||||
if existing_note is None:
|
||||
identifier = slug if slug is not None else id
|
||||
raise NoteNotFoundError(identifier)
|
||||
old_tag_names = _tag_names(existing_note.id) # for WebSub tag topics (v2.10.0)
|
||||
|
||||
if empty_content and not (existing_note.reacts_to or has_media):
|
||||
raise InvalidNoteDataError(
|
||||
"content", content, "Content cannot be empty or whitespace-only"
|
||||
)
|
||||
|
||||
# 3. SETUP
|
||||
updated_at = datetime.utcnow()
|
||||
@@ -648,7 +747,18 @@ def update_note(
|
||||
"Path validation failed",
|
||||
)
|
||||
|
||||
# 4. UPDATE FILE (if content changed)
|
||||
# 4. CAPTURE OLD HTML (Phase 6: for webmention URL diffing)
|
||||
# Capture old HTML before writing new content
|
||||
# Only if note is/was published (unpublished notes don't send webmentions)
|
||||
old_html = None
|
||||
if existing_note.published:
|
||||
try:
|
||||
old_html = existing_note.html
|
||||
except Exception as e:
|
||||
# HTML rendering failed - continue with update, skip old_html
|
||||
current_app.logger.warning(f"Failed to render old HTML for note {existing_note.slug}: {e}")
|
||||
|
||||
# 5. UPDATE FILE (if content changed)
|
||||
new_content_hash = existing_note.content_hash
|
||||
if content is not None:
|
||||
try:
|
||||
@@ -664,7 +774,7 @@ def update_note(
|
||||
f"Could not update note file: {note_path}",
|
||||
)
|
||||
|
||||
# 5. UPDATE DATABASE
|
||||
# 6. UPDATE DATABASE
|
||||
db = get_db(current_app)
|
||||
|
||||
# Build update query based on what changed
|
||||
@@ -679,6 +789,14 @@ def update_note(
|
||||
update_fields.append("published = ?")
|
||||
params.append(published)
|
||||
|
||||
if in_reply_to is not None:
|
||||
update_fields.append("in_reply_to = ?")
|
||||
params.append(in_reply_to)
|
||||
|
||||
if content_format is not UNCHANGED:
|
||||
update_fields.append("content_format = ?")
|
||||
params.append(content_format)
|
||||
|
||||
# Add WHERE clause parameter
|
||||
if slug is not None:
|
||||
where_clause = "slug = ?"
|
||||
@@ -726,9 +844,84 @@ def update_note(
|
||||
# FTS update failure should not prevent note update
|
||||
current_app.logger.warning(f"Failed to update FTS index for note {existing_note.slug}: {e}")
|
||||
|
||||
# 8. RETURN UPDATED NOTE
|
||||
# 8. QUEUE WEBMENTIONS (v1.6.0 Phase 6)
|
||||
# Queue webmentions if:
|
||||
# - Note is being unpublished (notify removed URLs)
|
||||
# - Note is being published (content changed OR publish status changed)
|
||||
# - Note was published and content/in_reply_to changed
|
||||
updated_note = get_note(slug=existing_note.slug, load_content=True)
|
||||
|
||||
# Detect unpublishing (published -> unpublished)
|
||||
is_unpublishing = existing_note.published and published is False
|
||||
|
||||
# Check if content actually changed (Phase 6 optimization)
|
||||
content_changed = (new_content_hash != existing_note.content_hash)
|
||||
publish_status_changed = (published is not None and published != existing_note.published)
|
||||
|
||||
# Determine if we should queue webmentions
|
||||
should_queue = False
|
||||
if is_unpublishing:
|
||||
# Unpublishing - queue for ALL previously-linked URLs
|
||||
should_queue = True
|
||||
elif published is not None and published:
|
||||
# Being published now - queue if content changed OR just being published
|
||||
should_queue = content_changed or publish_status_changed
|
||||
elif existing_note.published and (content_changed or in_reply_to is not None):
|
||||
# Was already published and content/reply-to changed
|
||||
should_queue = True
|
||||
|
||||
if should_queue:
|
||||
try:
|
||||
from starpunk.webmention_sender import queue_webmentions_for_note
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
from starpunk.utils import absolute_url_for
|
||||
note_url = absolute_url_for("public.note", slug=updated_note.slug)
|
||||
|
||||
# Get rendered HTML from updated note (if published)
|
||||
html_content = updated_note.html if updated_note.published else None
|
||||
|
||||
# Determine in_reply_to (use new value if provided, otherwise existing)
|
||||
reply_to = (in_reply_to if in_reply_to is not None else updated_note.in_reply_to) or updated_note.reacts_to
|
||||
|
||||
# Queue webmentions (best-effort)
|
||||
# Pass old_html for URL diffing (Phase 6)
|
||||
queue_webmentions_for_note(
|
||||
db_path=db_path,
|
||||
note_id=updated_note.id,
|
||||
note_url=note_url,
|
||||
content_html=html_content,
|
||||
in_reply_to=reply_to,
|
||||
old_content_html=old_html # Phase 6: for URL diffing
|
||||
)
|
||||
except Exception as e:
|
||||
# Webmention queueing failure should not prevent note update
|
||||
current_app.logger.warning(f"Failed to queue webmentions for note {updated_note.slug}: {e}")
|
||||
|
||||
# 9. FETCH REPLY CONTEXT (v1.6.0 Phase 3)
|
||||
# Fetch context if in_reply_to changed and is set ("" clears it)
|
||||
if in_reply_to:
|
||||
try:
|
||||
from starpunk.reply_context import fetch_reply_context
|
||||
db_path = Path(current_app.config["DATABASE_PATH"])
|
||||
site_url = current_app.config["SITE_URL"]
|
||||
|
||||
# Fetch context (best-effort)
|
||||
fetch_reply_context(db_path, in_reply_to, site_url)
|
||||
except Exception as e:
|
||||
# Reply context fetch failure should not prevent note update
|
||||
current_app.logger.warning(f"Failed to fetch reply context for {in_reply_to}: {e}")
|
||||
|
||||
# Crossposts follow the note (v2.1.0, best-effort)
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(updated_note.id)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication update: {e}")
|
||||
|
||||
_websub_changed(updated_note, was_public=existing_note.published, old_tags=old_tag_names)
|
||||
|
||||
# 10. RETURN UPDATED NOTE
|
||||
return updated_note
|
||||
|
||||
|
||||
@@ -827,6 +1020,10 @@ def delete_note(
|
||||
# For idempotency, don't raise error - just return
|
||||
return
|
||||
|
||||
# What feeds showed before, for WebSub (v2.10.0)
|
||||
was_public = bool(existing_note.published) and not getattr(existing_note, "deleted_at", None)
|
||||
deleted_tags = _tag_names(existing_note.id)
|
||||
|
||||
# 4. SETUP
|
||||
data_dir = Path(current_app.config["DATA_PATH"])
|
||||
note_path = data_dir / existing_note.file_path
|
||||
@@ -839,6 +1036,17 @@ def delete_note(
|
||||
"Path validation failed",
|
||||
)
|
||||
|
||||
# Re-send webmentions so receivers learn the post is gone (spec 3.1.5).
|
||||
# A hard delete would cascade the queue rows away, so detach them first.
|
||||
if existing_note.published:
|
||||
try:
|
||||
from starpunk.webmention_sender import requeue_previous_webmentions
|
||||
requeue_previous_webmentions(
|
||||
Path(current_app.config["DATABASE_PATH"]), existing_note.id, detach=not soft
|
||||
)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue webmentions for deleted note: {e}")
|
||||
|
||||
# 5. PERFORM DELETION
|
||||
db = get_db(current_app)
|
||||
|
||||
@@ -895,5 +1103,73 @@ def delete_note(
|
||||
)
|
||||
# Don't fail - database record already deleted
|
||||
|
||||
# Delete crossposts too (v2.1.0, best-effort)
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(existing_note.id)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication delete: {e}")
|
||||
|
||||
# Feeds and pages no longer show it (v2.10.0)
|
||||
object.__setattr__(existing_note, "published", False)
|
||||
_websub_changed(existing_note, was_public=was_public, old_tags=deleted_tags)
|
||||
|
||||
# 6. RETURN (no value)
|
||||
return None
|
||||
|
||||
|
||||
def undelete_note(slug: str) -> Note:
|
||||
"""
|
||||
Restore a soft-deleted note (v2.4.0, Micropub undelete)
|
||||
|
||||
Moves the file back from .trash/ and clears deleted_at. Crossposts are
|
||||
queued again, so a note deleted from them is posted again.
|
||||
|
||||
Raises:
|
||||
NoteNotFoundError: If there is no soft-deleted note with this slug
|
||||
"""
|
||||
import shutil
|
||||
from starpunk.utils import TRASH_DIR_NAME
|
||||
|
||||
db = get_db(current_app)
|
||||
row = db.execute(
|
||||
"SELECT id, file_path, created_at FROM notes WHERE slug = ? AND deleted_at IS NOT NULL",
|
||||
(slug,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise NoteNotFoundError(slug)
|
||||
|
||||
data_dir = Path(current_app.config["DATA_PATH"])
|
||||
note_path = data_dir / row["file_path"]
|
||||
if not note_path.exists():
|
||||
# delete_note_file put it in .trash/<year>/<month>/<name>
|
||||
parts = Path(row["file_path"]).parts
|
||||
candidates = []
|
||||
if "notes" in parts and len(parts) > parts.index("notes") + 2:
|
||||
i = parts.index("notes")
|
||||
candidates.append(data_dir / TRASH_DIR_NAME / parts[i + 1] / parts[i + 2] / note_path.name)
|
||||
candidates += sorted((data_dir / TRASH_DIR_NAME).glob(f"*/*/{note_path.name}"))
|
||||
source = next((c for c in candidates if c.exists()), None)
|
||||
if source is None:
|
||||
raise NoteSyncError("undelete", f"Trashed file not found for {slug}", "Cannot restore note file")
|
||||
note_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.move(str(source), str(note_path))
|
||||
|
||||
db.execute("UPDATE notes SET deleted_at = NULL WHERE id = ?", (row["id"],))
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
from starpunk.syndication import note_changed
|
||||
note_changed(row["id"])
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue syndication after undelete: {e}")
|
||||
|
||||
try:
|
||||
from starpunk.webmention_sender import requeue_previous_webmentions
|
||||
requeue_previous_webmentions(Path(current_app.config["DATABASE_PATH"]), row["id"])
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to queue webmentions after undelete: {e}")
|
||||
|
||||
restored = get_note(slug=slug)
|
||||
_websub_changed(restored)
|
||||
return restored
|
||||
@@ -0,0 +1,210 @@
|
||||
"""
|
||||
Rate limiting for webmention endpoint
|
||||
|
||||
Implements sliding window log algorithm with thread-safe in-memory storage.
|
||||
Per ADR-064, this uses in-memory counters consistent with the existing
|
||||
_discovery_cache pattern in webmention_worker.py.
|
||||
|
||||
References:
|
||||
- ADR-064: Webmention Rate Limiting
|
||||
- Design: /docs/design/v1.6.0/rate-limiting.md
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
"""
|
||||
Thread-safe sliding window rate limiter.
|
||||
|
||||
Uses sliding window log algorithm:
|
||||
- Stores timestamp of each request in window
|
||||
- Removes expired entries on each check
|
||||
- Rejects if count >= limit
|
||||
"""
|
||||
|
||||
def __init__(self, limit: int, window_seconds: int):
|
||||
"""
|
||||
Initialize rate limiter.
|
||||
|
||||
Args:
|
||||
limit: Maximum requests allowed in window
|
||||
window_seconds: Time window in seconds
|
||||
"""
|
||||
self.limit = limit
|
||||
self.window_seconds = window_seconds
|
||||
self._requests: Dict[str, List[float]] = {}
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def is_allowed(self, key: str) -> bool:
|
||||
"""
|
||||
Check if request is allowed and record it if so.
|
||||
|
||||
Args:
|
||||
key: Identifier (e.g., IP address or domain)
|
||||
|
||||
Returns:
|
||||
True if request is allowed, False if rate limited
|
||||
"""
|
||||
with self._lock:
|
||||
now = time.time()
|
||||
cutoff = now - self.window_seconds
|
||||
|
||||
# Get or create request log for this key
|
||||
if key not in self._requests:
|
||||
self._requests[key] = []
|
||||
|
||||
# Remove expired entries
|
||||
self._requests[key] = [ts for ts in self._requests[key] if ts > cutoff]
|
||||
|
||||
# Check if at or over limit
|
||||
if len(self._requests[key]) >= self.limit:
|
||||
return False
|
||||
|
||||
# Allow request and record timestamp
|
||||
self._requests[key].append(now)
|
||||
return True
|
||||
|
||||
def get_retry_after(self, key: str) -> int:
|
||||
"""
|
||||
Get seconds until oldest request expires (for Retry-After header).
|
||||
|
||||
Args:
|
||||
key: Identifier (e.g., IP address or domain)
|
||||
|
||||
Returns:
|
||||
Seconds until rate limit resets, or window_seconds if no requests
|
||||
"""
|
||||
with self._lock:
|
||||
if key not in self._requests or not self._requests[key]:
|
||||
return self.window_seconds
|
||||
|
||||
now = time.time()
|
||||
oldest_request = self._requests[key][0]
|
||||
expires_at = oldest_request + self.window_seconds
|
||||
retry_after = int(expires_at - now)
|
||||
|
||||
# Return at least 1 second, max window_seconds
|
||||
return max(1, min(retry_after, self.window_seconds))
|
||||
|
||||
def cleanup(self, max_idle_seconds: int = 300):
|
||||
"""
|
||||
Remove keys with no recent requests to prevent memory growth.
|
||||
|
||||
Args:
|
||||
max_idle_seconds: Remove keys idle for this many seconds
|
||||
"""
|
||||
with self._lock:
|
||||
now = time.time()
|
||||
cutoff = now - max_idle_seconds
|
||||
keys_to_remove = []
|
||||
|
||||
for key, timestamps in self._requests.items():
|
||||
# Remove expired timestamps first
|
||||
self._requests[key] = [ts for ts in timestamps if ts > cutoff]
|
||||
# Mark empty keys for removal
|
||||
if not self._requests[key]:
|
||||
keys_to_remove.append(key)
|
||||
|
||||
for key in keys_to_remove:
|
||||
del self._requests[key]
|
||||
|
||||
def get_request_count(self, key: str) -> int:
|
||||
"""
|
||||
Get current request count for a key (for testing/logging).
|
||||
|
||||
Args:
|
||||
key: Identifier (e.g., IP address or domain)
|
||||
|
||||
Returns:
|
||||
Number of requests in current window
|
||||
"""
|
||||
with self._lock:
|
||||
if key not in self._requests:
|
||||
return 0
|
||||
|
||||
now = time.time()
|
||||
cutoff = now - self.window_seconds
|
||||
# Count non-expired requests
|
||||
return len([ts for ts in self._requests[key] if ts > cutoff])
|
||||
|
||||
|
||||
# Module-level rate limiters (initialized in init_rate_limiters)
|
||||
_ip_limiter: RateLimiter | None = None
|
||||
_domain_limiter: RateLimiter | None = None
|
||||
|
||||
|
||||
def init_rate_limiters(app) -> None:
|
||||
"""
|
||||
Initialize rate limiters with Flask app configuration.
|
||||
|
||||
Args:
|
||||
app: Flask application instance
|
||||
"""
|
||||
global _ip_limiter, _domain_limiter
|
||||
|
||||
if not app.config.get('WEBMENTION_RATE_LIMIT_ENABLED', True):
|
||||
app.logger.info("Rate limiting disabled")
|
||||
_ip_limiter = None
|
||||
_domain_limiter = None
|
||||
return
|
||||
|
||||
ip_limit = app.config.get('WEBMENTION_RATE_LIMIT_IP', 60)
|
||||
domain_limit = app.config.get('WEBMENTION_RATE_LIMIT_DOMAIN', 10)
|
||||
window = app.config.get('WEBMENTION_RATE_LIMIT_WINDOW', 60)
|
||||
|
||||
_ip_limiter = RateLimiter(ip_limit, window)
|
||||
_domain_limiter = RateLimiter(domain_limit, window)
|
||||
|
||||
app.logger.info(
|
||||
f"Webmention rate limiter initialized: {ip_limit}/min IP, "
|
||||
f"{domain_limit}/min domain"
|
||||
)
|
||||
|
||||
|
||||
def check_rate_limit(ip: str, source_domain: str | None) -> Tuple[bool, str | None]:
|
||||
"""
|
||||
Check rate limits for a request.
|
||||
|
||||
Args:
|
||||
ip: Client IP address
|
||||
source_domain: Source URL domain (optional)
|
||||
|
||||
Returns:
|
||||
(allowed, error_message) tuple
|
||||
- allowed: True if request allowed, False if rate limited
|
||||
- error_message: Error description if rate limited, None otherwise
|
||||
"""
|
||||
# If rate limiting disabled, allow all requests
|
||||
if _ip_limiter is None:
|
||||
return True, None
|
||||
|
||||
# Check IP limit first
|
||||
if not _ip_limiter.is_allowed(ip):
|
||||
retry_after = _ip_limiter.get_retry_after(ip)
|
||||
count = _ip_limiter.get_request_count(ip)
|
||||
return False, f"Too many requests from your IP. Please retry after {retry_after} seconds."
|
||||
|
||||
# Check domain limit if source domain provided
|
||||
if source_domain and _domain_limiter is not None:
|
||||
if not _domain_limiter.is_allowed(source_domain):
|
||||
retry_after = _domain_limiter.get_retry_after(source_domain)
|
||||
count = _domain_limiter.get_request_count(source_domain)
|
||||
return False, f"Too many requests from source domain. Please retry after {retry_after} seconds."
|
||||
|
||||
return True, None
|
||||
|
||||
|
||||
def cleanup_rate_limiters() -> None:
|
||||
"""
|
||||
Cleanup expired entries from rate limiters.
|
||||
|
||||
Called periodically to prevent memory growth.
|
||||
"""
|
||||
if _ip_limiter:
|
||||
_ip_limiter.cleanup()
|
||||
if _domain_limiter:
|
||||
_domain_limiter.cleanup()
|
||||
@@ -0,0 +1,329 @@
|
||||
"""
|
||||
Reply context fetching for in-reply-to URLs
|
||||
|
||||
Per database-schema.md Story 2.4:
|
||||
- Fetch context from in_reply_to URL when note is reply
|
||||
- Parse microformats for author, content
|
||||
- Store in reply_contexts table
|
||||
- Graceful degradation on fetch failure
|
||||
|
||||
References:
|
||||
- Database Schema: /docs/design/v1.6.0/database-schema.md
|
||||
- Background Worker: /docs/design/v1.6.0/background-worker.md
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
from starpunk.webmention_processor import (
|
||||
fetch_source,
|
||||
extract_author,
|
||||
extract_content,
|
||||
ValidationError
|
||||
)
|
||||
import mf2py
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def fetch_reply_context(
|
||||
db_path: Path,
|
||||
reply_url: str,
|
||||
site_url: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Fetch and parse reply context from a URL.
|
||||
|
||||
Per database-schema.md:
|
||||
- Fetch URL content with SSRF protection
|
||||
- Parse h-entry for title, content, published date
|
||||
- Parse h-card for author information
|
||||
- Store in reply_contexts table
|
||||
- Graceful degradation: errors don't prevent reply creation
|
||||
|
||||
Args:
|
||||
db_path: Path to SQLite database
|
||||
reply_url: URL being replied to
|
||||
site_url: Site URL for User-Agent header
|
||||
|
||||
Returns:
|
||||
Dict with fetch status and context data
|
||||
"""
|
||||
# Check if context already exists
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT * FROM reply_contexts WHERE url = ?
|
||||
""", (reply_url,))
|
||||
|
||||
existing = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
if existing:
|
||||
logger.debug(f"Reply context already cached for {reply_url}")
|
||||
return dict(existing)
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to check existing context: {e}")
|
||||
|
||||
# Fetch and parse context
|
||||
try:
|
||||
# Fetch URL content
|
||||
content, content_type = fetch_source(reply_url, site_url)
|
||||
|
||||
# Parse microformats (only for HTML)
|
||||
if 'html' not in content_type.lower():
|
||||
# Not HTML - store minimal context
|
||||
return _store_minimal_context(db_path, reply_url, "Not HTML content")
|
||||
|
||||
parsed_mf2 = mf2py.parse(doc=content, url=reply_url)
|
||||
|
||||
# Extract h-entry data
|
||||
entry_data = _extract_entry_data(parsed_mf2)
|
||||
|
||||
# Extract author data
|
||||
author_data = extract_author(parsed_mf2, reply_url)
|
||||
|
||||
# Store in database
|
||||
context = _store_context(
|
||||
db_path,
|
||||
reply_url,
|
||||
entry_data.get('title'),
|
||||
entry_data.get('content_snippet'),
|
||||
entry_data.get('published_at'),
|
||||
author_data.get('author_name'),
|
||||
author_data.get('author_url'),
|
||||
author_data.get('author_photo'),
|
||||
entry_data.get('site_name')
|
||||
)
|
||||
|
||||
logger.info(f"Fetched reply context for {reply_url}")
|
||||
return context
|
||||
|
||||
except ValidationError as e:
|
||||
# Fetch/validation error - store error status
|
||||
logger.warning(f"Failed to fetch reply context for {reply_url}: {e}")
|
||||
return _store_minimal_context(db_path, reply_url, str(e))
|
||||
|
||||
except Exception as e:
|
||||
# Unexpected error - store error status
|
||||
logger.error(f"Error fetching reply context for {reply_url}: {e}", exc_info=True)
|
||||
return _store_minimal_context(db_path, reply_url, f"Unexpected error: {e}")
|
||||
|
||||
|
||||
def _extract_entry_data(parsed_mf2: Dict) -> Dict[str, Any]:
|
||||
"""
|
||||
Extract entry data from parsed microformats.
|
||||
|
||||
Args:
|
||||
parsed_mf2: Parsed microformats data from mf2py
|
||||
|
||||
Returns:
|
||||
Dict with title, content_snippet, published_at, site_name
|
||||
"""
|
||||
title = None
|
||||
content_snippet = None
|
||||
published_at = None
|
||||
site_name = None
|
||||
|
||||
# Find h-entry
|
||||
for item in parsed_mf2.get('items', []):
|
||||
if 'h-entry' not in item.get('type', []):
|
||||
continue
|
||||
|
||||
props = item.get('properties', {})
|
||||
|
||||
# Extract title (p-name)
|
||||
name = props.get('name', [])
|
||||
if name:
|
||||
title = name[0]
|
||||
if isinstance(title, dict):
|
||||
title = title.get('value', '')
|
||||
# Truncate if too long
|
||||
if title and len(title) > 200:
|
||||
title = title[:197] + '...'
|
||||
|
||||
# Extract content (reuse processor function)
|
||||
content_snippet = extract_content(parsed_mf2)
|
||||
|
||||
# Extract published date
|
||||
published = props.get('published', [])
|
||||
if published:
|
||||
pub_str = published[0]
|
||||
if isinstance(pub_str, dict):
|
||||
pub_str = pub_str.get('value', '')
|
||||
# Try to parse ISO 8601 datetime
|
||||
try:
|
||||
from dateutil import parser as dateutil_parser
|
||||
published_at = dateutil_parser.parse(pub_str)
|
||||
except Exception:
|
||||
# Parsing failed, leave as None
|
||||
pass
|
||||
|
||||
break # Use first h-entry
|
||||
|
||||
# Try to extract site name from h-card
|
||||
for item in parsed_mf2.get('items', []):
|
||||
if 'h-card' in item.get('type', []):
|
||||
props = item.get('properties', {})
|
||||
name = props.get('name', [])
|
||||
if name:
|
||||
site_name = name[0]
|
||||
if isinstance(site_name, dict):
|
||||
site_name = site_name.get('value', '')
|
||||
break
|
||||
|
||||
return {
|
||||
'title': title,
|
||||
'content_snippet': content_snippet,
|
||||
'published_at': published_at,
|
||||
'site_name': site_name
|
||||
}
|
||||
|
||||
|
||||
def _store_context(
|
||||
db_path: Path,
|
||||
url: str,
|
||||
title: Optional[str],
|
||||
content_snippet: Optional[str],
|
||||
published_at: Optional[datetime],
|
||||
author_name: Optional[str],
|
||||
author_url: Optional[str],
|
||||
author_photo: Optional[str],
|
||||
site_name: Optional[str]
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Store reply context in database.
|
||||
|
||||
Args:
|
||||
db_path: Database path
|
||||
url: Reply-to URL
|
||||
title: Entry title
|
||||
content_snippet: Content snippet
|
||||
published_at: Published datetime
|
||||
author_name: Author name
|
||||
author_url: Author URL
|
||||
author_photo: Author photo URL
|
||||
site_name: Site name
|
||||
|
||||
Returns:
|
||||
Stored context dict
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT OR REPLACE INTO reply_contexts
|
||||
(url, title, content_snippet, published_at,
|
||||
author_name, author_url, author_photo, site_name,
|
||||
fetch_status, fetched_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'fetched', CURRENT_TIMESTAMP)
|
||||
""", (
|
||||
url, title, content_snippet, published_at,
|
||||
author_name, author_url, author_photo, site_name
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'url': url,
|
||||
'title': title,
|
||||
'content_snippet': content_snippet,
|
||||
'published_at': published_at,
|
||||
'author_name': author_name,
|
||||
'author_url': author_url,
|
||||
'author_photo': author_photo,
|
||||
'site_name': site_name,
|
||||
'fetch_status': 'fetched'
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to store reply context: {e}")
|
||||
raise
|
||||
|
||||
|
||||
def _store_minimal_context(
|
||||
db_path: Path,
|
||||
url: str,
|
||||
error: str
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Store minimal context with error status.
|
||||
|
||||
Args:
|
||||
db_path: Database path
|
||||
url: Reply-to URL
|
||||
error: Error message
|
||||
|
||||
Returns:
|
||||
Minimal context dict with error
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
INSERT OR REPLACE INTO reply_contexts
|
||||
(url, fetch_status, fetch_error, created_at)
|
||||
VALUES (?, 'error', ?, CURRENT_TIMESTAMP)
|
||||
""", (url, error))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'url': url,
|
||||
'fetch_status': 'error',
|
||||
'fetch_error': error
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to store minimal context: {e}")
|
||||
# Return in-memory dict even if storage fails
|
||||
return {
|
||||
'url': url,
|
||||
'fetch_status': 'error',
|
||||
'fetch_error': error
|
||||
}
|
||||
|
||||
|
||||
def get_reply_context(db_path: Path, reply_url: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Get cached reply context from database.
|
||||
|
||||
Args:
|
||||
db_path: Database path
|
||||
reply_url: Reply-to URL
|
||||
|
||||
Returns:
|
||||
Context dict if exists, None otherwise
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
cursor.execute("""
|
||||
SELECT * FROM reply_contexts WHERE url = ?
|
||||
""", (reply_url,))
|
||||
|
||||
row = cursor.fetchone()
|
||||
conn.close()
|
||||
|
||||
if row:
|
||||
return dict(row)
|
||||
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get reply context: {e}")
|
||||
return None
|
||||
@@ -7,7 +7,7 @@ admin, auth, and (conditionally) dev auth routes.
|
||||
|
||||
from flask import Flask
|
||||
|
||||
from starpunk.routes import admin, auth, micropub, public, search
|
||||
from starpunk.routes import admin, auth, indieauth, micropub, public, search, webmention, websub
|
||||
|
||||
|
||||
def register_routes(app: Flask) -> None:
|
||||
@@ -29,6 +29,7 @@ def register_routes(app: Flask) -> None:
|
||||
|
||||
# Register auth routes
|
||||
app.register_blueprint(auth.bp)
|
||||
app.register_blueprint(indieauth.bp)
|
||||
|
||||
# Register Micropub routes
|
||||
app.register_blueprint(micropub.bp)
|
||||
@@ -39,6 +40,13 @@ def register_routes(app: Flask) -> None:
|
||||
# Register search routes
|
||||
app.register_blueprint(search.bp)
|
||||
|
||||
# Register webmention routes (v1.6.0)
|
||||
if app.config.get("WEBMENTION_ENABLED", True):
|
||||
app.register_blueprint(webmention.bp)
|
||||
|
||||
# WebSub hub (v2.10.0); refuses requests when disabled or using another hub
|
||||
app.register_blueprint(websub.bp)
|
||||
|
||||
# Conditionally register dev auth routes
|
||||
if app.config.get("DEV_MODE"):
|
||||
app.logger.warning(
|
||||
|
||||
+299
-10
@@ -6,10 +6,13 @@ editing, and deletion. All routes require authentication.
|
||||
"""
|
||||
|
||||
from flask import Blueprint, flash, g, jsonify, redirect, render_template, request, url_for
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
from starpunk.auth import require_auth
|
||||
from starpunk.notes import (
|
||||
create_note,
|
||||
@@ -32,17 +35,62 @@ def dashboard():
|
||||
Displays all notes (published and drafts) with management controls.
|
||||
Requires authentication.
|
||||
|
||||
Per v1.6.0: Added pagination (25 items per page)
|
||||
|
||||
Returns:
|
||||
Rendered dashboard template with complete note list
|
||||
Rendered dashboard template with paginated note list
|
||||
|
||||
Decorator: @require_auth
|
||||
Template: templates/admin/dashboard.html
|
||||
Access: g.user_me (set by require_auth decorator)
|
||||
"""
|
||||
# Get all notes (published and drafts)
|
||||
notes = list_notes()
|
||||
from flask import current_app
|
||||
import math
|
||||
|
||||
return render_template("admin/dashboard.html", notes=notes, user_me=g.me)
|
||||
# Get pagination params (v1.6.0)
|
||||
page = int(request.args.get('page', 1))
|
||||
per_page = 25
|
||||
|
||||
# Get all notes (published and drafts)
|
||||
all_notes = list_notes()
|
||||
|
||||
# Calculate pagination
|
||||
total_notes = len(all_notes)
|
||||
total_pages = math.ceil(total_notes / per_page) if total_notes > 0 else 1
|
||||
|
||||
# Slice for current page
|
||||
start_idx = (page - 1) * per_page
|
||||
end_idx = start_idx + per_page
|
||||
notes = all_notes[start_idx:end_idx]
|
||||
|
||||
# Get pending webmention count (v1.6.0)
|
||||
webmention_pending_count = 0
|
||||
if current_app.config.get('WEBMENTION_ENABLED', False):
|
||||
try:
|
||||
from starpunk.webmention_queries import count_webmentions
|
||||
webmention_pending_count = count_webmentions(status_filter='pending')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# WebSub subscribers (v2.10.0)
|
||||
websub_stats = None
|
||||
if current_app.config.get('WEBSUB_ENABLED', True):
|
||||
try:
|
||||
from starpunk import websub
|
||||
from starpunk.database import get_db
|
||||
websub_stats = websub.stats(get_db(current_app))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return render_template(
|
||||
"admin/dashboard.html",
|
||||
notes=notes,
|
||||
page=page,
|
||||
total_pages=total_pages,
|
||||
webmention_pending_count=webmention_pending_count,
|
||||
websub_stats=websub_stats,
|
||||
user_me=g.me
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/new", methods=["GET"])
|
||||
@@ -60,7 +108,9 @@ def new_note_form():
|
||||
Decorator: @require_auth
|
||||
Template: templates/admin/new.html
|
||||
"""
|
||||
return render_template("admin/new.html")
|
||||
from starpunk.syndication import get_targets
|
||||
|
||||
return render_template("admin/new.html", syndication_targets=get_targets().values())
|
||||
|
||||
|
||||
@bp.route("/new", methods=["POST"])
|
||||
@@ -100,13 +150,17 @@ def create_note_submit():
|
||||
# Parse tags (v1.3.0 Phase 3)
|
||||
tags = parse_tag_input(tags_input)
|
||||
|
||||
# With photos, WebSub hears about the note once they're attached (v2.10.1)
|
||||
has_uploads = any(f.filename for f in request.files.getlist('media_files'))
|
||||
|
||||
try:
|
||||
# Create note first (per Q4)
|
||||
note = create_note(
|
||||
content,
|
||||
published=published,
|
||||
custom_slug=custom_slug if custom_slug else None,
|
||||
tags=tags if tags else None
|
||||
tags=tags if tags else None,
|
||||
has_media=has_uploads,
|
||||
)
|
||||
|
||||
# Handle media uploads (v1.2.0 Phase 3)
|
||||
@@ -145,6 +199,16 @@ def create_note_submit():
|
||||
if errors:
|
||||
flash(f"Note created, but some images failed: {'; '.join(errors)}", "warning")
|
||||
|
||||
if not media_ids:
|
||||
# No photo made it; announce the note as it is (v2.10.1)
|
||||
from starpunk.notes import _websub_changed
|
||||
_websub_changed(note)
|
||||
|
||||
# Crosspost after media is attached so images go out too (v2.1.0)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
select_targets(note.id, request.form.getlist("syndicate_to"))
|
||||
|
||||
flash(f"Note created: {note.slug}", "success")
|
||||
return redirect(url_for("admin.dashboard"))
|
||||
|
||||
@@ -182,11 +246,21 @@ def edit_note_form(note_id: int):
|
||||
flash("Note not found", "error")
|
||||
return redirect(url_for("admin.dashboard")), 404
|
||||
|
||||
# Pre-load tags for the edit form (v1.3.0 Phase 3)
|
||||
tags = get_note_tags(note.id)
|
||||
# Pre-load tags for the edit form (v1.3.0 Phase 3), as entered so saving keeps the order
|
||||
tags = get_note_tags(note.id, entered_order=True)
|
||||
object.__setattr__(note, '_cached_tags', tags)
|
||||
|
||||
return render_template("admin/edit.html", note=note)
|
||||
from starpunk.syndication import get_note_syndications, get_targets
|
||||
|
||||
syndications = get_note_syndications(note.id)
|
||||
enabled = {s["target"] for s in syndications if s["enabled"]}
|
||||
return render_template(
|
||||
"admin/edit.html",
|
||||
note=note,
|
||||
syndication_targets=get_targets().values(),
|
||||
syndications=syndications,
|
||||
syndication_enabled=enabled,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/edit/<int:note_id>", methods=["POST"])
|
||||
@@ -223,7 +297,7 @@ def update_note_submit(note_id: int):
|
||||
published = "published" in request.form
|
||||
tags_input = request.form.get("tags", "")
|
||||
|
||||
if not content:
|
||||
if not content and not existing_note.reacts_to: # likes/reposts may have no comment
|
||||
flash("Content cannot be empty", "error")
|
||||
return redirect(url_for("admin.edit_note_form", note_id=note_id))
|
||||
|
||||
@@ -237,6 +311,9 @@ def update_note_submit(note_id: int):
|
||||
published=published,
|
||||
tags=tags if tags else None
|
||||
)
|
||||
if "syndication_form" in request.form:
|
||||
from starpunk.syndication import select_targets
|
||||
select_targets(note.id, request.form.getlist("syndicate_to"))
|
||||
flash(f"Note updated: {note.slug}", "success")
|
||||
return redirect(url_for("admin.dashboard"))
|
||||
except ValueError as e:
|
||||
@@ -600,3 +677,215 @@ def health_diagnostics():
|
||||
diagnostics["status"] = "healthy" if overall_healthy else "unhealthy"
|
||||
|
||||
return jsonify(diagnostics), 200 if overall_healthy else 500
|
||||
|
||||
|
||||
# Webmention Admin Routes (v1.6.0)
|
||||
|
||||
@bp.route("/webmentions")
|
||||
@require_auth
|
||||
def webmentions():
|
||||
"""
|
||||
Webmention list with filtering and pagination (v1.6.0 Phase 5)
|
||||
|
||||
Lists all webmentions with status filters and pagination.
|
||||
|
||||
Query params:
|
||||
status: Filter by moderation_status ('pending', 'approved', 'hidden')
|
||||
page: Page number (default: 1)
|
||||
|
||||
Returns:
|
||||
Rendered webmentions list template
|
||||
|
||||
Decorator: @require_auth
|
||||
Template: templates/admin/webmentions.html
|
||||
"""
|
||||
from starpunk.webmention_queries import list_webmentions
|
||||
|
||||
status_filter = request.args.get('status')
|
||||
page = int(request.args.get('page', 1))
|
||||
|
||||
webmentions, total_pages = list_webmentions(
|
||||
status_filter=status_filter,
|
||||
page=page,
|
||||
per_page=25
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"admin/webmentions.html",
|
||||
webmentions=webmentions,
|
||||
status_filter=status_filter,
|
||||
page=page,
|
||||
total_pages=total_pages,
|
||||
user_me=g.me
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/webmentions/<int:id>")
|
||||
@require_auth
|
||||
def webmention_detail(id: int):
|
||||
"""
|
||||
Webmention detail view (v1.6.0 Phase 5)
|
||||
|
||||
Shows full details of a single webmention with moderation actions.
|
||||
|
||||
Args:
|
||||
id: Webmention database ID
|
||||
|
||||
Returns:
|
||||
Rendered webmention detail template or 404 if not found
|
||||
|
||||
Decorator: @require_auth
|
||||
Template: templates/admin/webmention_detail.html
|
||||
"""
|
||||
from starpunk.webmention_queries import get_webmention
|
||||
|
||||
webmention = get_webmention(id)
|
||||
|
||||
if not webmention:
|
||||
flash("Webmention not found", "error")
|
||||
return redirect(url_for("admin.webmentions")), 404
|
||||
|
||||
return render_template(
|
||||
"admin/webmention_detail.html",
|
||||
webmention=webmention,
|
||||
user_me=g.me
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/webmentions/<int:id>/action", methods=["POST"])
|
||||
@require_auth
|
||||
def webmention_action(id: int):
|
||||
"""
|
||||
Individual webmention moderation action (v1.6.0 Phase 5)
|
||||
|
||||
Approve, hide, or delete a single webmention.
|
||||
|
||||
Args:
|
||||
id: Webmention database ID
|
||||
|
||||
Form data:
|
||||
action: 'approve', 'hide', or 'delete'
|
||||
|
||||
Returns:
|
||||
Redirect to webmention detail or list
|
||||
|
||||
Decorator: @require_auth
|
||||
"""
|
||||
from starpunk.webmention_queries import update_webmention_status, delete_webmentions
|
||||
|
||||
action = request.form.get('action')
|
||||
|
||||
if action not in ('approve', 'hide', 'delete'):
|
||||
flash("Invalid action", "error")
|
||||
return redirect(url_for("admin.webmention_detail", id=id))
|
||||
|
||||
try:
|
||||
if action == 'delete':
|
||||
deleted = delete_webmentions([id])
|
||||
if deleted > 0:
|
||||
flash("Webmention deleted", "success")
|
||||
else:
|
||||
flash("Webmention not found", "error")
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
else:
|
||||
# Approve or hide
|
||||
status = 'approved' if action == 'approve' else 'hidden'
|
||||
updated = update_webmention_status([id], status)
|
||||
if updated > 0:
|
||||
flash(f"Webmention {status}", "success")
|
||||
else:
|
||||
flash("Webmention not found", "error")
|
||||
return redirect(url_for("admin.webmention_detail", id=id))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error performing webmention action: {e}")
|
||||
flash(f"Error: {e}", "error")
|
||||
return redirect(url_for("admin.webmention_detail", id=id))
|
||||
|
||||
|
||||
@bp.route("/webmentions/bulk-action", methods=["POST"])
|
||||
@require_auth
|
||||
def webmention_bulk_action():
|
||||
"""
|
||||
Bulk webmention moderation actions (v1.6.0 Phase 5)
|
||||
|
||||
Approve, hide, or delete multiple webmentions at once.
|
||||
|
||||
Form data:
|
||||
action: 'approve', 'hide', or 'delete'
|
||||
ids[]: List of webmention IDs
|
||||
|
||||
Returns:
|
||||
Redirect to webmentions list
|
||||
|
||||
Decorator: @require_auth
|
||||
"""
|
||||
from starpunk.webmention_queries import update_webmention_status, delete_webmentions
|
||||
|
||||
action = request.form.get('action')
|
||||
ids = request.form.getlist('ids')
|
||||
|
||||
if not ids:
|
||||
flash("No webmentions selected", "warning")
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
|
||||
# Convert IDs to integers
|
||||
try:
|
||||
webmention_ids = [int(id) for id in ids]
|
||||
except ValueError:
|
||||
flash("Invalid webmention IDs", "error")
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
|
||||
if action not in ('approve', 'hide', 'delete'):
|
||||
flash("Invalid action", "error")
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
|
||||
try:
|
||||
if action == 'delete':
|
||||
deleted = delete_webmentions(webmention_ids)
|
||||
flash(f"Deleted {deleted} webmention(s)", "success")
|
||||
else:
|
||||
# Approve or hide
|
||||
status = 'approved' if action == 'approve' else 'hidden'
|
||||
updated = update_webmention_status(webmention_ids, status)
|
||||
flash(f"Updated {updated} webmention(s) to {status}", "success")
|
||||
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error performing bulk action: {e}")
|
||||
flash(f"Error: {e}", "error")
|
||||
return redirect(url_for("admin.webmentions"))
|
||||
|
||||
|
||||
# Connected Micropub apps (v2.0.0)
|
||||
|
||||
@bp.route("/tokens")
|
||||
@require_auth
|
||||
def tokens():
|
||||
"""List active IndieAuth tokens. Template: templates/admin/tokens.html"""
|
||||
from starpunk.indieauth import list_active_tokens
|
||||
|
||||
return render_template("admin/tokens.html", tokens=list_active_tokens(), user_me=g.me)
|
||||
|
||||
|
||||
@bp.route("/tokens/<int:token_id>/revoke", methods=["POST"])
|
||||
@require_auth
|
||||
def revoke_token(token_id):
|
||||
"""Revoke one IndieAuth token"""
|
||||
from starpunk.indieauth import revoke_token_by_id
|
||||
|
||||
revoke_token_by_id(token_id)
|
||||
flash("App access revoked", "success")
|
||||
return redirect(url_for("admin.tokens"))
|
||||
|
||||
|
||||
@bp.route("/syndication/<int:syndication_id>/retry", methods=["POST"])
|
||||
@require_auth
|
||||
def retry_syndication(syndication_id):
|
||||
"""Retry a crosspost now"""
|
||||
from starpunk.syndication import retry
|
||||
|
||||
retry(syndication_id)
|
||||
flash("Crosspost queued", "success")
|
||||
return redirect(request.referrer or url_for("admin.dashboard"))
|
||||
+49
-112
@@ -1,15 +1,14 @@
|
||||
"""
|
||||
Authentication routes for StarPunk
|
||||
|
||||
Handles IndieLogin authentication flow including login form, OAuth callback,
|
||||
logout functionality, and IndieAuth endpoints for Micropub clients.
|
||||
Admin login form, OIDC start/callback, and logout.
|
||||
The IndieAuth server endpoints for Micropub clients live in routes/indieauth.py.
|
||||
"""
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
current_app,
|
||||
flash,
|
||||
jsonify,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
@@ -18,8 +17,9 @@ from flask import (
|
||||
)
|
||||
|
||||
from starpunk.auth import (
|
||||
IndieLoginError,
|
||||
AuthError,
|
||||
InvalidStateError,
|
||||
OIDCError,
|
||||
UnauthorizedError,
|
||||
destroy_session,
|
||||
handle_callback,
|
||||
@@ -27,167 +27,104 @@ from starpunk.auth import (
|
||||
require_auth,
|
||||
verify_session,
|
||||
)
|
||||
from starpunk.auth_external import DiscoveryError
|
||||
|
||||
# Create blueprint
|
||||
bp = Blueprint("auth", __name__, url_prefix="/auth")
|
||||
|
||||
SESSION_COOKIE = "starpunk_session"
|
||||
|
||||
|
||||
@bp.route("/login", methods=["GET"])
|
||||
def login_form():
|
||||
"""
|
||||
Display login form
|
||||
|
||||
If user is already authenticated, redirects to admin dashboard.
|
||||
Otherwise shows login form for IndieLogin authentication.
|
||||
|
||||
Returns:
|
||||
Redirect to dashboard if authenticated, otherwise login template
|
||||
Show the login page, or go to the dashboard if already logged in
|
||||
|
||||
Template: templates/admin/login.html
|
||||
"""
|
||||
# Check if already logged in
|
||||
session_token = request.cookies.get("starpunk_session")
|
||||
session_token = request.cookies.get(SESSION_COOKIE)
|
||||
if session_token and verify_session(session_token):
|
||||
return redirect(url_for("admin.dashboard"))
|
||||
return redirect(session.pop("next", None) or url_for("admin.dashboard"))
|
||||
|
||||
return render_template("admin/login.html")
|
||||
|
||||
|
||||
@bp.route("/login", methods=["POST"])
|
||||
def login_initiate():
|
||||
"""
|
||||
Initiate IndieLogin authentication flow
|
||||
|
||||
Validates the submitted 'me' URL and redirects to IndieLogin.com
|
||||
for authentication.
|
||||
|
||||
Form data:
|
||||
me: User's personal website URL
|
||||
|
||||
Returns:
|
||||
Redirect to IndieLogin.com or back to login form on error
|
||||
|
||||
Raises:
|
||||
Flashes error message and redirects on validation failure
|
||||
"""
|
||||
me_url = request.form.get("me", "").strip()
|
||||
|
||||
if not me_url:
|
||||
flash("Please enter your website URL", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
"""Start OIDC login and redirect to the identity provider"""
|
||||
try:
|
||||
# Initiate IndieAuth flow
|
||||
auth_url = initiate_login(me_url)
|
||||
return redirect(auth_url)
|
||||
except ValueError as e:
|
||||
flash(str(e), "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
except DiscoveryError as e:
|
||||
current_app.logger.error(f"Endpoint discovery failed for {me_url}: {e}")
|
||||
flash("Unable to verify your profile URL. Please check that it's correct and try again.", "error")
|
||||
return redirect(initiate_login(session.pop("next", None)))
|
||||
except OIDCError as e:
|
||||
current_app.logger.error(f"OIDC login could not start: {e}")
|
||||
flash("The identity provider is unavailable. Please try again later.", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
|
||||
@bp.route("/callback")
|
||||
def callback():
|
||||
"""
|
||||
Handle IndieLogin callback
|
||||
|
||||
Processes the OAuth callback from IndieLogin.com, validates the
|
||||
authorization code, state token, and issuer, then creates an
|
||||
authenticated session using PKCE verification.
|
||||
Handle the identity provider's redirect
|
||||
|
||||
Query parameters:
|
||||
code: Authorization code from IndieLogin
|
||||
state: CSRF state token
|
||||
iss: Issuer identifier (should be https://indielogin.com/)
|
||||
|
||||
Returns:
|
||||
Redirect to admin dashboard on success, login form on failure
|
||||
code, state: Authorization response
|
||||
iss: Issuer identifier (RFC 9207)
|
||||
error, error_description: Provider-reported failure
|
||||
|
||||
Sets:
|
||||
session cookie (HttpOnly, Secure, SameSite=Lax, 30 day expiry)
|
||||
session cookie (HttpOnly, Secure, SameSite=Lax)
|
||||
"""
|
||||
if request.args.get("error"):
|
||||
current_app.logger.warning(
|
||||
f"OIDC provider returned error: {request.args.get('error')} "
|
||||
f"{request.args.get('error_description', '')}"
|
||||
)
|
||||
flash("Login was cancelled or refused by the identity provider", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
code = request.args.get("code")
|
||||
state = request.args.get("state")
|
||||
iss = request.args.get("iss") # Extract issuer parameter
|
||||
|
||||
if not code or not state:
|
||||
flash("Missing authentication parameters", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
try:
|
||||
# Handle callback and create session with PKCE verification
|
||||
session_token = handle_callback(code, state, iss) # Pass issuer
|
||||
|
||||
# Create response with redirect
|
||||
response = redirect(url_for("admin.dashboard"))
|
||||
|
||||
# Set secure session cookie
|
||||
secure = current_app.config.get("SITE_URL", "").startswith("https://")
|
||||
response.set_cookie(
|
||||
"starpunk_session",
|
||||
session_token,
|
||||
httponly=True,
|
||||
secure=secure,
|
||||
samesite="Lax",
|
||||
max_age=30 * 24 * 60 * 60, # 30 days
|
||||
)
|
||||
|
||||
flash("Login successful!", "success")
|
||||
return response
|
||||
|
||||
except InvalidStateError as e:
|
||||
current_app.logger.error(f"Invalid state error: {e}")
|
||||
flash("Authentication failed: Invalid state token (possible CSRF)", "error")
|
||||
session_token, next_url = handle_callback(code, state, request.args.get("iss"))
|
||||
except InvalidStateError:
|
||||
flash("Login expired or was already used. Please try again.", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
except UnauthorizedError as e:
|
||||
current_app.logger.error(f"Unauthorized: {e}")
|
||||
except UnauthorizedError:
|
||||
flash("Authentication failed: Not authorized as admin", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
except IndieLoginError as e:
|
||||
current_app.logger.error(f"IndieLogin error: {e}")
|
||||
flash(f"Authentication failed: {e}", "error")
|
||||
except AuthError as e:
|
||||
current_app.logger.error(f"OIDC login failed: {e}")
|
||||
flash("Authentication failed. Please try again.", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Unexpected auth error: {e}")
|
||||
flash("Authentication failed: An unexpected error occurred", "error")
|
||||
return redirect(url_for("auth.login_form"))
|
||||
response = redirect(next_url or url_for("admin.dashboard"))
|
||||
response.set_cookie(
|
||||
SESSION_COOKIE,
|
||||
session_token,
|
||||
httponly=True,
|
||||
secure=current_app.config.get("SITE_URL", "").startswith("https://"),
|
||||
samesite="Lax",
|
||||
max_age=current_app.config.get("SESSION_LIFETIME", 30) * 24 * 60 * 60,
|
||||
)
|
||||
if not next_url:
|
||||
flash("Login successful!", "success")
|
||||
return response
|
||||
|
||||
|
||||
@bp.route("/logout", methods=["POST"])
|
||||
@require_auth
|
||||
def logout():
|
||||
"""
|
||||
Logout and destroy session
|
||||
|
||||
Destroys the user's session and clears the session cookie.
|
||||
Requires authentication (user must be logged in to logout).
|
||||
|
||||
Returns:
|
||||
Redirect to homepage with session cookie cleared
|
||||
|
||||
Decorator: @require_auth
|
||||
"""
|
||||
session_token = request.cookies.get("starpunk_session")
|
||||
|
||||
# Destroy session in database
|
||||
"""Destroy the session and clear the cookie"""
|
||||
session_token = request.cookies.get(SESSION_COOKIE)
|
||||
if session_token:
|
||||
try:
|
||||
destroy_session(session_token)
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error destroying session: {e}")
|
||||
|
||||
# Clear cookie and redirect
|
||||
response = redirect(url_for("public.index"))
|
||||
response.delete_cookie("starpunk_session")
|
||||
|
||||
response.delete_cookie(SESSION_COOKIE)
|
||||
flash("Logged out successfully", "success")
|
||||
return response
|
||||
|
||||
|
||||
@@ -0,0 +1,232 @@
|
||||
"""
|
||||
IndieAuth server routes for StarPunk (v2.0.0, ADR-064)
|
||||
|
||||
Endpoints:
|
||||
GET /.well-known/oauth-authorization-server Server metadata
|
||||
GET /auth/authorize Consent screen (admin login required)
|
||||
POST /auth/authorize Profile-only code redemption
|
||||
POST /auth/authorize/approve Consent decision
|
||||
POST /auth/token Code -> access token (and legacy revoke)
|
||||
GET /auth/token Legacy token verification
|
||||
POST /auth/revoke Token revocation (RFC 7009)
|
||||
"""
|
||||
|
||||
from urllib.parse import urlencode, urlparse, urlunparse, parse_qsl
|
||||
|
||||
from flask import (
|
||||
Blueprint,
|
||||
current_app,
|
||||
jsonify,
|
||||
redirect,
|
||||
render_template,
|
||||
request,
|
||||
session,
|
||||
url_for,
|
||||
)
|
||||
|
||||
from starpunk.auth import verify_session
|
||||
from starpunk.indieauth import (
|
||||
SUPPORTED_SCOPES,
|
||||
AuthorizationRequestError,
|
||||
InvalidGrantError,
|
||||
create_access_token,
|
||||
create_authorization_code,
|
||||
issuer,
|
||||
redeem_authorization_code,
|
||||
revoke_access_token,
|
||||
validate_authorization_request,
|
||||
verify_access_token,
|
||||
)
|
||||
from starpunk.micropub import MicropubError, extract_bearer_token
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
bp = Blueprint("indieauth", __name__)
|
||||
|
||||
# Parameters carried from the consent screen back to the approve handler
|
||||
_REQUEST_FIELDS = (
|
||||
"response_type",
|
||||
"client_id",
|
||||
"redirect_uri",
|
||||
"state",
|
||||
"code_challenge",
|
||||
"code_challenge_method",
|
||||
"scope",
|
||||
)
|
||||
|
||||
|
||||
def _json(payload: dict, status: int = 200):
|
||||
response = jsonify(payload)
|
||||
response.status_code = status
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return response
|
||||
|
||||
|
||||
def _oauth_error(error: str, description: str, status: int = 400):
|
||||
return _json({"error": error, "error_description": description}, status)
|
||||
|
||||
|
||||
def _redirect_with(redirect_uri: str, params: dict):
|
||||
"""Append params to redirect_uri, keeping any query it already has"""
|
||||
parts = urlparse(redirect_uri)
|
||||
query = parse_qsl(parts.query, keep_blank_values=True) + list(params.items())
|
||||
return redirect(urlunparse(parts._replace(query=urlencode(query))))
|
||||
|
||||
|
||||
def _admin_session():
|
||||
return verify_session(request.cookies.get("starpunk_session"))
|
||||
|
||||
|
||||
def _request_error_page(error: AuthorizationRequestError):
|
||||
return (
|
||||
render_template("admin/authorize.html", request_error=error.description),
|
||||
400,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/.well-known/oauth-authorization-server")
|
||||
def metadata():
|
||||
"""IndieAuth server metadata"""
|
||||
return _json(
|
||||
{
|
||||
"issuer": issuer(),
|
||||
"authorization_endpoint": absolute_url_for("indieauth.authorize"),
|
||||
"token_endpoint": absolute_url_for("indieauth.token"),
|
||||
"revocation_endpoint": absolute_url_for("indieauth.revoke"),
|
||||
"revocation_endpoint_auth_methods_supported": ["none"],
|
||||
"scopes_supported": SUPPORTED_SCOPES,
|
||||
"response_types_supported": ["code"],
|
||||
"grant_types_supported": ["authorization_code"],
|
||||
"code_challenge_methods_supported": ["S256"],
|
||||
"authorization_response_iss_parameter_supported": True,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/auth/authorize", methods=["GET"])
|
||||
def authorize():
|
||||
"""Show the consent screen to the logged-in admin"""
|
||||
try:
|
||||
auth_request = validate_authorization_request(request.args.to_dict())
|
||||
except AuthorizationRequestError as e:
|
||||
if not e.redirectable:
|
||||
return _request_error_page(e)
|
||||
return _redirect_with(
|
||||
request.args["redirect_uri"],
|
||||
{
|
||||
"error": e.error,
|
||||
"error_description": e.description,
|
||||
"state": request.args.get("state", ""),
|
||||
"iss": issuer(),
|
||||
},
|
||||
)
|
||||
|
||||
if not _admin_session():
|
||||
# Come back here after logging in with OIDC
|
||||
session["next"] = request.url
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
return render_template(
|
||||
"admin/authorize.html",
|
||||
auth_request=auth_request,
|
||||
fields={k: request.args.get(k, "") for k in _REQUEST_FIELDS},
|
||||
me=current_app.config["ADMIN_ME"],
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/auth/authorize/approve", methods=["POST"])
|
||||
def approve():
|
||||
"""Record the admin's consent decision and redirect to the client"""
|
||||
if not _admin_session():
|
||||
return redirect(url_for("auth.login_form"))
|
||||
|
||||
params = {k: request.form.get(k, "") for k in _REQUEST_FIELDS}
|
||||
try:
|
||||
auth_request = validate_authorization_request(params)
|
||||
except AuthorizationRequestError as e:
|
||||
return _request_error_page(e)
|
||||
|
||||
if request.form.get("decision") != "approve":
|
||||
return _redirect_with(
|
||||
auth_request["redirect_uri"],
|
||||
{"error": "access_denied", "state": auth_request["state"], "iss": issuer()},
|
||||
)
|
||||
|
||||
# Only scopes that were both requested and left checked
|
||||
granted = [s for s in auth_request["scopes"] if s in request.form.getlist("scope_granted")]
|
||||
code = create_authorization_code(
|
||||
auth_request["client_id"],
|
||||
auth_request["redirect_uri"],
|
||||
" ".join(granted),
|
||||
auth_request["code_challenge"],
|
||||
)
|
||||
current_app.logger.info(
|
||||
f"IndieAuth: approved {auth_request['client_id']} (scope: {' '.join(granted) or 'none'})"
|
||||
)
|
||||
return _redirect_with(
|
||||
auth_request["redirect_uri"],
|
||||
{"code": code, "state": auth_request["state"], "iss": issuer()},
|
||||
)
|
||||
|
||||
|
||||
def _redeem_from_form():
|
||||
return redeem_authorization_code(
|
||||
request.form.get("code", ""),
|
||||
request.form.get("client_id", ""),
|
||||
request.form.get("redirect_uri", ""),
|
||||
request.form.get("code_verifier", ""),
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/auth/authorize", methods=["POST"])
|
||||
def authorize_redeem():
|
||||
"""Profile-only flow: redeem a code for the user's identity"""
|
||||
if request.form.get("grant_type", "authorization_code") != "authorization_code":
|
||||
return _oauth_error("unsupported_grant_type", "Only authorization_code is supported")
|
||||
try:
|
||||
result = _redeem_from_form()
|
||||
except InvalidGrantError as e:
|
||||
return _oauth_error("invalid_grant", str(e))
|
||||
return _json({"me": result["me"]})
|
||||
|
||||
|
||||
@bp.route("/auth/token", methods=["POST"])
|
||||
def token():
|
||||
"""Exchange an authorization code for an access token"""
|
||||
if request.form.get("action") == "revoke":
|
||||
# Legacy IndieAuth revocation
|
||||
revoke_access_token(request.form.get("token", ""))
|
||||
return _json({})
|
||||
|
||||
if request.form.get("grant_type") != "authorization_code":
|
||||
return _oauth_error("unsupported_grant_type", "Only authorization_code is supported")
|
||||
|
||||
try:
|
||||
result = _redeem_from_form()
|
||||
except InvalidGrantError as e:
|
||||
return _oauth_error("invalid_grant", str(e))
|
||||
|
||||
if not result["scope"]:
|
||||
# Spec: a code issued without scope cannot be used for an access token
|
||||
return _oauth_error("invalid_grant", "No scope was granted; use the authorization endpoint")
|
||||
|
||||
return _json(create_access_token(result["client_id"], result["scope"]))
|
||||
|
||||
|
||||
@bp.route("/auth/token", methods=["GET"])
|
||||
def token_verify():
|
||||
"""Legacy token verification for clients that still call it"""
|
||||
try:
|
||||
token = extract_bearer_token(request)
|
||||
except MicropubError as e:
|
||||
return _oauth_error("invalid_request", e.error_description)
|
||||
info = verify_access_token(token)
|
||||
if not info:
|
||||
return _oauth_error("unauthorized", "Invalid or expired access token", 401)
|
||||
return _json(info)
|
||||
|
||||
|
||||
@bp.route("/auth/revoke", methods=["POST"])
|
||||
def revoke():
|
||||
"""RFC 7009 token revocation (always 200)"""
|
||||
revoke_access_token(request.form.get("token", ""))
|
||||
return _json({})
|
||||
+34
-24
@@ -17,6 +17,7 @@ References:
|
||||
- W3C Micropub Specification: https://www.w3.org/TR/micropub/
|
||||
- ADR-028: Micropub Implementation Strategy
|
||||
- ADR-029: Micropub IndieAuth Integration Strategy
|
||||
- ADR-064: Tokens are issued and verified by StarPunk itself
|
||||
"""
|
||||
|
||||
from flask import Blueprint, current_app, request, make_response
|
||||
@@ -26,9 +27,11 @@ from starpunk.micropub import (
|
||||
extract_bearer_token,
|
||||
error_response,
|
||||
handle_create,
|
||||
handle_delete,
|
||||
handle_update,
|
||||
handle_query,
|
||||
)
|
||||
from starpunk.auth_external import verify_external_token, check_scope
|
||||
from starpunk.indieauth import verify_access_token, check_scope
|
||||
|
||||
# Create blueprint
|
||||
bp = Blueprint("micropub", __name__)
|
||||
@@ -48,8 +51,8 @@ def micropub_endpoint():
|
||||
POST requests:
|
||||
Handle action operations (form-encoded or JSON):
|
||||
- action=create (or no action): Create new post
|
||||
- action=update: Update existing post (not supported in V1)
|
||||
- action=delete: Delete post (not supported in V1)
|
||||
- action=update: Update existing post (JSON; replace/add/delete)
|
||||
- action=delete / action=undelete: Delete or restore a post
|
||||
|
||||
Authentication:
|
||||
Requires valid bearer token in Authorization header or
|
||||
@@ -67,18 +70,21 @@ def micropub_endpoint():
|
||||
}
|
||||
"""
|
||||
# Extract and verify token
|
||||
token = extract_bearer_token(request)
|
||||
try:
|
||||
token = extract_bearer_token(request)
|
||||
except MicropubError as e:
|
||||
return error_response(e.error, e.error_description, e.status_code)
|
||||
if not token:
|
||||
return error_response("unauthorized", "No access token provided", 401)
|
||||
|
||||
token_info = verify_external_token(token)
|
||||
token_info = verify_access_token(token)
|
||||
if not token_info:
|
||||
return error_response("unauthorized", "Invalid or expired access token", 401)
|
||||
|
||||
# Handle query endpoints (GET requests)
|
||||
if request.method == "GET":
|
||||
try:
|
||||
return handle_query(request.args.to_dict(), token_info)
|
||||
return handle_query(request.args, token_info)
|
||||
except MicropubError as e:
|
||||
return error_response(e.error, e.error_description, e.status_code)
|
||||
except Exception as e:
|
||||
@@ -91,26 +97,25 @@ def micropub_endpoint():
|
||||
try:
|
||||
# Parse request based on content type
|
||||
if "application/json" in content_type:
|
||||
data = request.get_json() or {}
|
||||
# Malformed JSON is the client's error, not ours (v2.9.0)
|
||||
data = request.get_json(silent=True)
|
||||
if not isinstance(data, dict):
|
||||
return error_response("invalid_request", "Request body must be a JSON object", 400)
|
||||
action = data.get("action", "create")
|
||||
else:
|
||||
# Form-encoded or multipart (V1 only supports form-encoded)
|
||||
# Form-encoded or multipart (files handled separately)
|
||||
data = request.form.to_dict(flat=False)
|
||||
action = data.get("action", ["create"])[0]
|
||||
|
||||
# Route to appropriate handler
|
||||
if action == "create":
|
||||
return handle_create(data, token_info)
|
||||
return handle_create(data, token_info, files=request.files)
|
||||
elif action == "update":
|
||||
# V1: Update not supported
|
||||
return error_response(
|
||||
"invalid_request", "Update action not supported in V1", 400
|
||||
)
|
||||
elif action == "delete":
|
||||
# V1: Delete not supported
|
||||
return error_response(
|
||||
"invalid_request", "Delete action not supported in V1", 400
|
||||
)
|
||||
if "application/json" not in content_type:
|
||||
return error_response("invalid_request", "Updates must be sent as JSON", 400)
|
||||
return handle_update(data, token_info)
|
||||
elif action in ("delete", "undelete"):
|
||||
return handle_delete(data, token_info, undelete=(action == "undelete"))
|
||||
else:
|
||||
return error_response("invalid_request", f"Unknown action: {action}", 400)
|
||||
|
||||
@@ -121,7 +126,8 @@ def micropub_endpoint():
|
||||
return error_response("server_error", "An unexpected error occurred", 500)
|
||||
|
||||
|
||||
@bp.route('/media', methods=['POST'])
|
||||
@bp.route('/micropub/media', methods=['POST'])
|
||||
@bp.route('/media', methods=['POST']) # old path, kept for existing clients
|
||||
def media_endpoint():
|
||||
"""
|
||||
Micropub media endpoint for file uploads
|
||||
@@ -136,19 +142,23 @@ def media_endpoint():
|
||||
from starpunk.media import save_media
|
||||
|
||||
# Extract and verify token
|
||||
token = extract_bearer_token(request)
|
||||
try:
|
||||
token = extract_bearer_token(request)
|
||||
except MicropubError as e:
|
||||
return error_response(e.error, e.error_description, e.status_code)
|
||||
if not token:
|
||||
return error_response("unauthorized", "No access token provided", 401)
|
||||
|
||||
token_info = verify_external_token(token)
|
||||
token_info = verify_access_token(token)
|
||||
if not token_info:
|
||||
return error_response("unauthorized", "Invalid or expired access token", 401)
|
||||
|
||||
# Check scope (create scope allows media upload)
|
||||
if not check_scope("create", token_info.get("scope", "")):
|
||||
# Check scope: "media", or "create" for older clients
|
||||
scope = token_info.get("scope", "")
|
||||
if not (check_scope("media", scope) or check_scope("create", scope)):
|
||||
return error_response(
|
||||
"insufficient_scope",
|
||||
"Token lacks create scope",
|
||||
"Token lacks media or create scope",
|
||||
403
|
||||
)
|
||||
|
||||
|
||||
+122
-14
@@ -11,6 +11,7 @@ from datetime import datetime, timedelta
|
||||
from flask import Blueprint, abort, render_template, Response, current_app, request, send_from_directory
|
||||
|
||||
from starpunk.notes import list_notes, get_note
|
||||
from starpunk.websub import embed_hub
|
||||
from starpunk.feed import generate_feed_streaming # Legacy RSS
|
||||
from starpunk.feeds import (
|
||||
generate_rss,
|
||||
@@ -55,8 +56,14 @@ def _get_cached_notes():
|
||||
cache_duration = timedelta(seconds=cache_seconds)
|
||||
now = datetime.utcnow()
|
||||
|
||||
# The cached list is stale once any public note changed, in any process
|
||||
# (v2.10.0): every change bumps content_version in the database
|
||||
from starpunk.database import get_db
|
||||
from starpunk.websub import get_content_version
|
||||
version = (str(current_app.config["DATABASE_PATH"]), get_content_version(get_db(current_app)))
|
||||
|
||||
# Check if note list cache is valid
|
||||
if _feed_cache["notes"] and _feed_cache["timestamp"]:
|
||||
if _feed_cache["notes"] and _feed_cache["timestamp"] and _feed_cache.get("version") == version:
|
||||
cache_age = now - _feed_cache["timestamp"]
|
||||
if cache_age < cache_duration:
|
||||
# Use cached note list
|
||||
@@ -64,7 +71,7 @@ def _get_cached_notes():
|
||||
|
||||
# Cache expired or empty, fetch fresh notes
|
||||
max_items = current_app.config.get("FEED_MAX_ITEMS", 50)
|
||||
notes = list_notes(published_only=True, limit=max_items)
|
||||
notes = list_notes(published_only=True, limit=max_items, likes=False)
|
||||
|
||||
# Batch load media and tags for all notes (v1.5.0 Phase 3)
|
||||
# Reduces query count from O(n) to O(1) for both media and tags
|
||||
@@ -82,6 +89,7 @@ def _get_cached_notes():
|
||||
|
||||
_feed_cache["notes"] = notes
|
||||
_feed_cache["timestamp"] = now
|
||||
_feed_cache["version"] = version
|
||||
|
||||
return notes
|
||||
|
||||
@@ -122,6 +130,7 @@ def _generate_feed_with_cache(format_name: str, non_streaming_generator):
|
||||
notes=notes,
|
||||
limit=max_items,
|
||||
)
|
||||
content = embed_hub(format_name, content) # WebSub hub link (v2.10.0)
|
||||
|
||||
# Create response with proper Content-Type including charset
|
||||
mime_type = get_mime_type(format_name)
|
||||
@@ -132,7 +141,12 @@ def _generate_feed_with_cache(format_name: str, non_streaming_generator):
|
||||
|
||||
# Caching enabled - use FeedCache
|
||||
feed_cache = get_cache()
|
||||
notes_checksum = feed_cache.generate_notes_checksum(notes)
|
||||
# The content version covers changes the note list doesn't show, such as
|
||||
# photos attached or captions edited (v2.10.1)
|
||||
from starpunk.database import get_db
|
||||
from starpunk.websub import get_content_version
|
||||
notes_checksum = (feed_cache.generate_notes_checksum(notes)
|
||||
+ ":" + get_content_version(get_db(current_app)))
|
||||
|
||||
# Check If-None-Match header for conditional requests
|
||||
if_none_match = request.headers.get('If-None-Match')
|
||||
@@ -170,6 +184,7 @@ def _generate_feed_with_cache(format_name: str, non_streaming_generator):
|
||||
notes=notes,
|
||||
limit=max_items,
|
||||
)
|
||||
content = embed_hub(format_name, content) # WebSub hub link (v2.10.0)
|
||||
|
||||
# Store in cache and get ETag
|
||||
etag = feed_cache.set(format_name, content, notes_checksum)
|
||||
@@ -249,8 +264,8 @@ def index():
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
# Get recent published notes (limit 20)
|
||||
notes = list_notes(published_only=True, limit=20)
|
||||
# Get recent published notes (limit 20); likes have their own page (v2.5.0)
|
||||
notes = list_notes(published_only=True, limit=20, likes=False)
|
||||
|
||||
# Attach media and tags to each note for display
|
||||
for note in notes:
|
||||
@@ -262,9 +277,59 @@ def index():
|
||||
tags = get_note_tags(note.id)
|
||||
object.__setattr__(note, '_cached_tags', tags)
|
||||
|
||||
attach_contexts(notes)
|
||||
return render_template("index.html", notes=notes)
|
||||
|
||||
|
||||
def attach_contexts(notes) -> None:
|
||||
"""Give likes and reposts the fetched context of the post they point at"""
|
||||
from starpunk.webmention_queries import get_reply_context
|
||||
|
||||
for note in notes:
|
||||
context = None
|
||||
if note.reacts_to:
|
||||
try:
|
||||
context = get_reply_context(note.reacts_to)
|
||||
except Exception:
|
||||
context = None
|
||||
object.__setattr__(note, 'context', context)
|
||||
|
||||
|
||||
@bp.route("/likes")
|
||||
def likes():
|
||||
"""Likes, kept off the home page and feeds (v2.5.0)"""
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
|
||||
notes = list_notes(published_only=True, limit=50, likes=True)
|
||||
for note in notes:
|
||||
object.__setattr__(note, 'media', get_note_media(note.id))
|
||||
object.__setattr__(note, '_cached_tags', get_note_tags(note.id))
|
||||
attach_contexts(notes)
|
||||
return render_template("likes.html", notes=notes)
|
||||
|
||||
|
||||
def _note_was_deleted(slug: str) -> bool:
|
||||
"""
|
||||
True if this slug was a note that has been deleted
|
||||
|
||||
Soft-deleted notes keep their row; hard-deleted notes that sent
|
||||
webmentions keep their queue rows (detached from the note).
|
||||
"""
|
||||
from starpunk.database import get_db
|
||||
from starpunk.utils import absolute_url_for
|
||||
|
||||
db = get_db(current_app)
|
||||
if db.execute(
|
||||
"SELECT 1 FROM notes WHERE slug = ? AND deleted_at IS NOT NULL", (slug,)
|
||||
).fetchone():
|
||||
return True
|
||||
return db.execute(
|
||||
"SELECT 1 FROM webmention_queue WHERE source_url = ? AND source_note_id IS NULL",
|
||||
(absolute_url_for("public.note", slug=slug),),
|
||||
).fetchone() is not None
|
||||
|
||||
|
||||
@bp.route("/note/<slug>")
|
||||
def note(slug: str):
|
||||
"""
|
||||
@@ -281,6 +346,8 @@ def note(slug: str):
|
||||
|
||||
Template: templates/note.html
|
||||
Microformats: h-entry
|
||||
|
||||
Per v1.6.0: Added webmentions and reply context display
|
||||
"""
|
||||
from starpunk.media import get_note_media
|
||||
from starpunk.tags import get_note_tags
|
||||
@@ -288,8 +355,10 @@ def note(slug: str):
|
||||
# Get note by slug
|
||||
note_obj = get_note(slug=slug)
|
||||
|
||||
# Return 404 if note doesn't exist or isn't published
|
||||
# Deleted notes are 410 Gone (Webmention 3.1.5); unknown or draft 404
|
||||
if not note_obj or not note_obj.published:
|
||||
if not note_obj and _note_was_deleted(slug):
|
||||
abort(410)
|
||||
abort(404)
|
||||
|
||||
# Get media for note (v1.2.0 Phase 3)
|
||||
@@ -303,7 +372,45 @@ def note(slug: str):
|
||||
tags = get_note_tags(note_obj.id)
|
||||
object.__setattr__(note_obj, '_cached_tags', tags)
|
||||
|
||||
return render_template("note.html", note=note_obj)
|
||||
# Get webmentions for note (v1.6.0 Phase 5)
|
||||
webmentions = None
|
||||
reply_context = None
|
||||
|
||||
if current_app.config.get('WEBMENTION_ENABLED', False):
|
||||
try:
|
||||
from starpunk.webmention_queries import get_webmentions_for_note, get_reply_context
|
||||
|
||||
# Get approved webmentions grouped by type
|
||||
grouped = get_webmentions_for_note(note_obj.id)
|
||||
|
||||
# Check if there are any webmentions
|
||||
has_webmentions = any(grouped.values())
|
||||
if has_webmentions:
|
||||
webmentions = grouped
|
||||
|
||||
# Get reply context if this is a reply post
|
||||
if note_obj.in_reply_to:
|
||||
reply_context = get_reply_context(note_obj.in_reply_to)
|
||||
|
||||
except Exception as e:
|
||||
# Log error but don't fail page load
|
||||
import logging
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.error(f"Error loading webmentions for note {slug}: {e}")
|
||||
|
||||
from starpunk.syndication import get_note_syndications
|
||||
|
||||
syndications = [s for s in get_note_syndications(note_obj.id) if s["state"] == "synced"]
|
||||
attach_contexts([note_obj])
|
||||
|
||||
return render_template(
|
||||
"note.html",
|
||||
note=note_obj,
|
||||
syndications=syndications,
|
||||
webmentions=webmentions,
|
||||
reply_context=reply_context,
|
||||
**grouped if webmentions else {}
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/tag/<tag>")
|
||||
@@ -345,6 +452,7 @@ def tag(tag: str):
|
||||
for note in notes:
|
||||
media = get_note_media(note.id)
|
||||
object.__setattr__(note, 'media', media)
|
||||
attach_contexts(notes)
|
||||
|
||||
return render_template(
|
||||
"tag.html",
|
||||
@@ -405,15 +513,15 @@ def feed():
|
||||
)
|
||||
|
||||
# Route to appropriate generator
|
||||
if format_name == 'rss':
|
||||
return feed_rss()
|
||||
elif format_name == 'atom':
|
||||
return feed_atom()
|
||||
if format_name == 'atom':
|
||||
response = feed_atom()
|
||||
elif format_name == 'json':
|
||||
return feed_json()
|
||||
response = feed_json()
|
||||
else:
|
||||
# Shouldn't reach here, but be defensive
|
||||
return feed_rss()
|
||||
response = feed_rss()
|
||||
# The format (and its rel=self) depends on Accept (v2.10.1)
|
||||
response.headers.add('Vary', 'Accept')
|
||||
return response
|
||||
|
||||
|
||||
@bp.route("/feed.rss")
|
||||
|
||||
@@ -108,7 +108,7 @@ def api_search():
|
||||
"title": r["title"] or f"Note from {r['created_at'][:10]}",
|
||||
"excerpt": r["snippet"], # Already has <mark> tags
|
||||
"published_at": r["created_at"],
|
||||
"url": f"/notes/{r['slug']}",
|
||||
"url": f"/note/{r['slug']}",
|
||||
}
|
||||
for r in results
|
||||
],
|
||||
@@ -176,7 +176,7 @@ def search_page():
|
||||
"title": r["title"] or f"Note from {r['created_at'][:10]}",
|
||||
"excerpt": Markup(safe_snippet), # Mark as safe since we've escaped it ourselves
|
||||
"published_at": r["created_at"],
|
||||
"url": f"/notes/{r['slug']}",
|
||||
"url": f"/note/{r['slug']}",
|
||||
})
|
||||
results = formatted_results
|
||||
except Exception as e:
|
||||
|
||||
@@ -0,0 +1,280 @@
|
||||
"""
|
||||
Webmention routes for StarPunk
|
||||
|
||||
Per W3C Webmention Recommendation and ADR-063:
|
||||
- POST /webmention - Receive webmention notifications
|
||||
- GET /webmention - Optional endpoint information
|
||||
|
||||
References:
|
||||
- W3C Webmention Specification: https://www.w3.org/TR/webmention/
|
||||
- ADR-063: Webmention Implementation
|
||||
"""
|
||||
|
||||
from flask import Blueprint, current_app, request, jsonify
|
||||
from urllib.parse import urldefrag, urlparse
|
||||
from starpunk.database import get_db
|
||||
from starpunk.notes import get_note, NoteNotFoundError
|
||||
from starpunk.rate_limiter import check_rate_limit
|
||||
|
||||
bp = Blueprint("webmention", __name__)
|
||||
|
||||
|
||||
def get_client_ip() -> str:
|
||||
"""
|
||||
Get client IP address, accounting for reverse proxy.
|
||||
|
||||
If TRUSTED_PROXY is enabled, uses X-Forwarded-For header.
|
||||
Otherwise uses request.remote_addr directly.
|
||||
"""
|
||||
if current_app.config.get('TRUSTED_PROXY', False):
|
||||
forwarded = request.headers.get('X-Forwarded-For')
|
||||
if forwarded:
|
||||
# First IP in chain is the client
|
||||
return forwarded.split(',')[0].strip()
|
||||
return request.remote_addr or '0.0.0.0'
|
||||
|
||||
|
||||
@bp.before_request
|
||||
def rate_limit_webmention():
|
||||
"""
|
||||
Check rate limits before processing webmention requests.
|
||||
|
||||
Per ADR-064, checks:
|
||||
1. IP rate limit (60/min by default)
|
||||
2. Source domain rate limit (10/min by default)
|
||||
|
||||
Returns 429 with Retry-After header if rate limited.
|
||||
"""
|
||||
# Only rate limit POST requests (the actual webmention submission)
|
||||
if request.method != 'POST':
|
||||
return None
|
||||
|
||||
# Get client IP
|
||||
ip = get_client_ip()
|
||||
|
||||
# Get source domain from request (if available)
|
||||
source_domain = None
|
||||
content_type = request.headers.get("Content-Type", "")
|
||||
|
||||
if "application/json" in content_type:
|
||||
data = request.get_json(silent=True) or {}
|
||||
source = data.get('source')
|
||||
else:
|
||||
source = request.form.get('source')
|
||||
|
||||
if source:
|
||||
try:
|
||||
source_domain = urlparse(source).netloc
|
||||
except Exception:
|
||||
# Invalid URL, domain check will be skipped
|
||||
# This is acceptable - IP limit still applies
|
||||
current_app.logger.debug(f"Could not parse source URL for domain extraction: {source}")
|
||||
|
||||
# Check rate limits
|
||||
allowed, error = check_rate_limit(ip, source_domain)
|
||||
if not allowed:
|
||||
current_app.logger.warning(
|
||||
f"Rate limit exceeded - IP: {ip}, Domain: {source_domain or 'N/A'}"
|
||||
)
|
||||
return jsonify({
|
||||
"error": "rate_limit_exceeded",
|
||||
"error_description": error
|
||||
}), 429, {'Retry-After': str(current_app.config.get('WEBMENTION_RATE_LIMIT_WINDOW', 60))}
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def validate_url(url: str) -> tuple[bool, str | None]:
|
||||
"""
|
||||
Validate a URL is well-formed and uses http/https scheme.
|
||||
|
||||
Returns:
|
||||
(is_valid, error_message)
|
||||
"""
|
||||
if not url:
|
||||
return False, "URL is required"
|
||||
|
||||
try:
|
||||
parsed = urlparse(url)
|
||||
if parsed.scheme not in ('http', 'https'):
|
||||
return False, "Only http and https URLs are supported"
|
||||
if not parsed.netloc:
|
||||
return False, "URL must include a hostname"
|
||||
if len(url) > 2048:
|
||||
return False, "URL exceeds maximum length of 2048 characters"
|
||||
return True, None
|
||||
except Exception:
|
||||
return False, "URL is malformed"
|
||||
|
||||
|
||||
def _host_port(url: str) -> tuple[str, int | None]:
|
||||
parsed = urlparse(url)
|
||||
default = {'http': 80, 'https': 443}.get(parsed.scheme.lower())
|
||||
try:
|
||||
port = parsed.port or default
|
||||
except ValueError:
|
||||
port = None
|
||||
return (parsed.hostname or '').lower(), port
|
||||
|
||||
|
||||
def is_target_on_site(target_url: str) -> bool:
|
||||
"""
|
||||
Check if target URL is on this site.
|
||||
|
||||
Compares host (case-insensitively) and effective port with SITE_URL.
|
||||
"""
|
||||
site_url = current_app.config.get('SITE_URL', '')
|
||||
return _host_port(site_url) == _host_port(target_url)
|
||||
|
||||
|
||||
def resolve_target_note(target_url: str) -> int | None:
|
||||
"""
|
||||
Resolve target URL to a note ID if it points to a note.
|
||||
|
||||
Returns:
|
||||
Note ID if target is a valid note, None otherwise
|
||||
"""
|
||||
target_parsed = urlparse(target_url)
|
||||
|
||||
# Strip query params and fragments for matching
|
||||
target_path = target_parsed.path.rstrip('/')
|
||||
|
||||
# Check if it's a note URL pattern: /note/{slug}
|
||||
if target_path.startswith('/note/'):
|
||||
slug = target_path[6:] # Remove '/note/' prefix
|
||||
if slug:
|
||||
try:
|
||||
note = get_note(slug=slug)
|
||||
# get_note() returns None if not found, or a Note object
|
||||
if note and note.published:
|
||||
return note.id
|
||||
except NoteNotFoundError:
|
||||
pass
|
||||
|
||||
# Homepage or other valid page (not a note)
|
||||
return None
|
||||
|
||||
|
||||
@bp.route("/webmention", methods=["GET"])
|
||||
def webmention_info():
|
||||
"""
|
||||
Optional endpoint information.
|
||||
|
||||
Returns basic information about the webmention endpoint.
|
||||
"""
|
||||
return jsonify({
|
||||
"name": "Webmention endpoint",
|
||||
"spec": "https://www.w3.org/TR/webmention/"
|
||||
})
|
||||
|
||||
|
||||
@bp.route("/webmention", methods=["POST"])
|
||||
def receive_webmention():
|
||||
"""
|
||||
Receive webmention notification per W3C spec.
|
||||
|
||||
Accepts application/x-www-form-urlencoded (required) and
|
||||
application/json (optional extension).
|
||||
|
||||
Returns:
|
||||
202 Accepted with JSON body on success
|
||||
400 Bad Request with error details on failure
|
||||
"""
|
||||
# Parse request based on content type
|
||||
content_type = request.headers.get("Content-Type", "")
|
||||
|
||||
if "application/json" in content_type:
|
||||
data = request.get_json() or {}
|
||||
source = data.get("source")
|
||||
target = data.get("target")
|
||||
else:
|
||||
# Default to form-encoded (required by W3C spec)
|
||||
source = request.form.get("source")
|
||||
target = request.form.get("target")
|
||||
|
||||
# Validate required parameters
|
||||
if not source:
|
||||
return jsonify({
|
||||
"error": "invalid_request",
|
||||
"error_description": "Missing required parameter: source"
|
||||
}), 400
|
||||
|
||||
if not target:
|
||||
return jsonify({
|
||||
"error": "invalid_request",
|
||||
"error_description": "Missing required parameter: target"
|
||||
}), 400
|
||||
|
||||
# Validate source URL
|
||||
source_valid, source_error = validate_url(source)
|
||||
if not source_valid:
|
||||
return jsonify({
|
||||
"error": "invalid_request",
|
||||
"error_description": f"Source must be a valid URL"
|
||||
}), 400
|
||||
|
||||
# Validate target URL
|
||||
target_valid, target_error = validate_url(target)
|
||||
if not target_valid:
|
||||
return jsonify({
|
||||
"error": "invalid_request",
|
||||
"error_description": f"Target must be a valid URL"
|
||||
}), 400
|
||||
|
||||
# Validate source and target are different
|
||||
if source == target:
|
||||
return jsonify({
|
||||
"error": "invalid_request",
|
||||
"error_description": "Source and target must be different"
|
||||
}), 400
|
||||
|
||||
# Validate target is on this site
|
||||
if not is_target_on_site(target):
|
||||
return jsonify({
|
||||
"error": "invalid_target",
|
||||
"error_description": "Target URL is not on this site"
|
||||
}), 400
|
||||
|
||||
# The fragment is ignored (spec 3.2.1) and not stored, so target#a and
|
||||
# target#b are the same mention
|
||||
target = urldefrag(target)[0]
|
||||
|
||||
# Only published notes accept webmentions (spec 3.2.1: target must be a
|
||||
# valid resource that accepts them)
|
||||
target_note_id = resolve_target_note(target)
|
||||
if target_note_id is None:
|
||||
return jsonify({
|
||||
"error": "invalid_target",
|
||||
"error_description": "Target URL does not accept webmentions"
|
||||
}), 400
|
||||
|
||||
# Insert webmention into database (queued for validation)
|
||||
# Use INSERT ... ON CONFLICT to handle duplicate webmentions
|
||||
db = get_db()
|
||||
try:
|
||||
db.execute("""
|
||||
INSERT INTO webmentions (source_url, target_url, target_note_id)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(source_url, target_url) DO UPDATE SET
|
||||
validation_status = 'pending',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
""", (source, target, target_note_id))
|
||||
db.commit()
|
||||
|
||||
current_app.logger.info(
|
||||
f"Webmention received: {source} -> {target}"
|
||||
f"{' (note_id=' + str(target_note_id) + ')' if target_note_id else ''}"
|
||||
)
|
||||
|
||||
return jsonify({
|
||||
"status": "accepted",
|
||||
"message": "Webmention queued for verification"
|
||||
}), 202
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
current_app.logger.error(f"Failed to queue webmention: {e}")
|
||||
return jsonify({
|
||||
"error": "server_error",
|
||||
"error_description": "Failed to queue webmention"
|
||||
}), 500
|
||||
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
WebSub hub endpoint (v2.10.0, ADR-071)
|
||||
|
||||
POST /websub with hub.mode subscribe/unsubscribe, hub.topic, hub.callback,
|
||||
optional hub.secret and hub.lease_seconds. Answers 202 and verifies later
|
||||
(spec 5.1); errors are 4xx with a plain-text reason.
|
||||
"""
|
||||
|
||||
from flask import Blueprint, current_app, request
|
||||
|
||||
from starpunk.database import get_db
|
||||
from starpunk.rate_limiter import RateLimiter
|
||||
from starpunk import websub
|
||||
|
||||
bp = Blueprint("websub", __name__)
|
||||
|
||||
# Subscription requests per client IP per minute
|
||||
# (per gunicorn process; needs TRUSTED_PROXY=true behind a reverse proxy,
|
||||
# or every client shares the proxy's address)
|
||||
_limiter = RateLimiter(limit=30, window_seconds=60)
|
||||
_requests = 0
|
||||
|
||||
|
||||
def _text(message: str, status: int):
|
||||
return message + "\n", status, {"Content-Type": "text/plain; charset=utf-8"}
|
||||
|
||||
|
||||
@bp.route("/websub", methods=["POST"])
|
||||
def hub():
|
||||
if not websub.enabled():
|
||||
return _text("WebSub is not enabled on this site", 404)
|
||||
|
||||
from starpunk.routes.webmention import get_client_ip
|
||||
global _requests
|
||||
_requests += 1
|
||||
if _requests % 500 == 0:
|
||||
_limiter.cleanup() # forget idle clients so memory doesn't grow
|
||||
if not _limiter.is_allowed(get_client_ip()):
|
||||
return _text("Too many requests", 429)
|
||||
|
||||
# Media types are case-insensitive (v2.10.1)
|
||||
if request.mimetype != "application/x-www-form-urlencoded":
|
||||
return _text("Send an application/x-www-form-urlencoded body", 400)
|
||||
|
||||
try:
|
||||
websub.accept_subscription_request(request.form, get_db(current_app))
|
||||
except websub.SubscriptionError as e:
|
||||
return _text(str(e), e.status)
|
||||
|
||||
return _text("Accepted; the subscription will be verified", 202)
|
||||
|
||||
|
||||
@bp.route("/websub", methods=["GET"])
|
||||
def hub_info():
|
||||
return _text(
|
||||
"WebSub hub for this site's own feeds. Subscribe with a POST "
|
||||
"(https://www.w3.org/TR/websub/).", 200)
|
||||
File diff suppressed because it is too large.
Load diff
+11
-6
@@ -115,27 +115,32 @@ def add_tags_to_note(note_id: int, tags: list[str]) -> None:
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_note_tags(note_id: int) -> list[dict]:
|
||||
def get_note_tags(note_id: int, entered_order: bool = False) -> list[dict]:
|
||||
"""
|
||||
Get all tags for a note
|
||||
|
||||
Args:
|
||||
note_id: Note database ID
|
||||
entered_order: In the order they were entered (the first is the
|
||||
note's main tag, e.g. its Threads topic) instead of alphabetical.
|
||||
add_tags_to_note() rewrites the links in entry order on every
|
||||
save, so note_tags.id records it. Use this wherever the tags are
|
||||
saved back (editor, Micropub update) so the order survives.
|
||||
|
||||
Returns:
|
||||
List of tag dicts with 'name' and 'display_name'
|
||||
Ordered alphabetically by display_name (case-insensitive)
|
||||
|
||||
Query should use: ORDER BY LOWER(tags.display_name) ASC
|
||||
Ordered alphabetically by display_name (case-insensitive), unless
|
||||
entered_order
|
||||
"""
|
||||
db = get_db(current_app)
|
||||
order = "note_tags.id" if entered_order else "LOWER(tags.display_name)"
|
||||
tags = db.execute(
|
||||
"""
|
||||
f"""
|
||||
SELECT tags.name, tags.display_name
|
||||
FROM tags
|
||||
JOIN note_tags ON tags.id = note_tags.tag_id
|
||||
WHERE note_tags.note_id = ?
|
||||
ORDER BY LOWER(tags.display_name) ASC
|
||||
ORDER BY {order} ASC
|
||||
""",
|
||||
(note_id,)
|
||||
).fetchall()
|
||||
|
||||
+36
-1
@@ -11,7 +11,7 @@ import hashlib
|
||||
import re
|
||||
import secrets
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
@@ -681,3 +681,38 @@ def parse_iso8601(date_string: str) -> datetime:
|
||||
|
||||
# Parse using fromisoformat
|
||||
return datetime.fromisoformat(date_string)
|
||||
|
||||
|
||||
def absolute_url_for(endpoint: str, **values) -> str:
|
||||
"""
|
||||
Absolute URL for an endpoint, built from SITE_URL
|
||||
|
||||
Use instead of url_for(..., _external=True): behind a reverse proxy the
|
||||
request arrives over plain HTTP, so _external URLs would say http://.
|
||||
"""
|
||||
from flask import current_app, url_for
|
||||
|
||||
return current_app.config["SITE_URL"].rstrip("/") + url_for(endpoint, **values)
|
||||
|
||||
|
||||
def iso_utc(value) -> str:
|
||||
"""
|
||||
Format a timestamp as RFC 3339 UTC for dt-* and Micropub (v2.8.0)
|
||||
|
||||
Stored times are naive UTC. Without an offset, microformats parsers read
|
||||
them as local time, so this adds 'Z' and drops microseconds (HTML
|
||||
<time datetime> allows at most 3 fractional digits).
|
||||
|
||||
Args:
|
||||
value: datetime (naive = UTC) or ISO 8601 string
|
||||
|
||||
Returns:
|
||||
e.g. '2026-11-01T16:25:12Z', or '' for empty values
|
||||
"""
|
||||
if not value:
|
||||
return ''
|
||||
if isinstance(value, str):
|
||||
value = datetime.fromisoformat(value.strip().replace('Z', '+00:00'))
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.astimezone(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
|
||||
@@ -0,0 +1,344 @@
|
||||
"""
|
||||
Webmention validation and processing utilities
|
||||
|
||||
Per background-worker.md and api-contract.md:
|
||||
- Fetch and validate source URLs
|
||||
- Parse microformats with mf2py
|
||||
- Extract author information from h-cards
|
||||
- Detect mention types (reply, like, repost, etc.)
|
||||
- Sanitize content with nh3
|
||||
|
||||
References:
|
||||
- Background Worker Design: /docs/design/v1.6.0/background-worker.md
|
||||
- API Contract: /docs/design/v1.6.0/api-contract.md
|
||||
- W3C Webmention: https://www.w3.org/TR/webmention/
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import mf2py
|
||||
import nh3
|
||||
import httpx
|
||||
from typing import Dict, Any, Optional
|
||||
from urllib.parse import urldefrag, urlparse
|
||||
|
||||
from starpunk import mf2_utils
|
||||
from starpunk.utils import iso_utc
|
||||
from starpunk.webmention_ssrf import (
|
||||
FetchTooLargeError, SSRFBlockedError, safe_get, user_agent,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ValidationError(Exception):
|
||||
"""Raised when webmention validation fails"""
|
||||
pass
|
||||
|
||||
|
||||
ACCEPT = ('text/html, application/xhtml+xml;q=0.9, application/json;q=0.8, '
|
||||
'text/plain;q=0.7, */*;q=0.1')
|
||||
|
||||
|
||||
class TransientError(ValidationError):
|
||||
"""Source could not be checked right now (network error, 5xx)"""
|
||||
pass
|
||||
|
||||
|
||||
def fetch_source(url: str, site_url: str) -> tuple[str, str, str]:
|
||||
"""
|
||||
Fetch source URL content with SSRF protection.
|
||||
|
||||
Per spec 3.2.2: GET following redirects (limited), with an Accept header.
|
||||
|
||||
Args:
|
||||
url: Source URL to fetch
|
||||
site_url: Site URL for User-Agent header
|
||||
|
||||
Returns:
|
||||
Tuple of (content, content_type, final_url)
|
||||
|
||||
Raises:
|
||||
ValidationError: Source is blocked, too large, or returned 4xx
|
||||
TransientError: Network error, timeout or 5xx
|
||||
"""
|
||||
try:
|
||||
result = safe_get(url, headers={'User-Agent': user_agent(site_url), 'Accept': ACCEPT})
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
logger.warning(f"SSRF protection blocked URL {url}: {e}")
|
||||
raise ValidationError(f"Blocked request: {e}")
|
||||
except FetchTooLargeError as e:
|
||||
raise ValidationError(f"Source {e}")
|
||||
except httpx.TimeoutException:
|
||||
raise TransientError("Source fetch timed out")
|
||||
except httpx.HTTPError as e:
|
||||
raise TransientError(f"Failed to fetch source: {e}")
|
||||
|
||||
if 400 <= result.status_code < 500:
|
||||
raise ValidationError(f"Source returned {result.status_code}")
|
||||
if result.status_code >= 300:
|
||||
raise TransientError(f"Source server error: {result.status_code}")
|
||||
|
||||
return result.text, result.headers.get('Content-Type', ''), result.url
|
||||
|
||||
|
||||
def _strip_fragment(url: str) -> str:
|
||||
return urldefrag(url.strip())[0]
|
||||
|
||||
|
||||
def _json_strings(value):
|
||||
"""Yield every string in a parsed JSON document"""
|
||||
if isinstance(value, str):
|
||||
yield value
|
||||
elif isinstance(value, dict):
|
||||
for item in value.values():
|
||||
yield from _json_strings(item)
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
yield from _json_strings(item)
|
||||
|
||||
|
||||
def verify_link_presence(content: str, target_url: str, content_type: str,
|
||||
source_url: str = '') -> bool:
|
||||
"""
|
||||
Verify the source links to the target (spec 3.2.2).
|
||||
|
||||
The source MUST have an exact match of the target URL, checked per media
|
||||
type: in HTML, any link-bearing attribute (a/link href, img/video/audio
|
||||
src, ...) resolved against the source URL; in JSON, any string value; in
|
||||
other text, the URL anywhere. Fragments are ignored on both sides.
|
||||
|
||||
Args:
|
||||
content: Source content
|
||||
target_url: Target URL to find
|
||||
content_type: Content type of source
|
||||
source_url: Final source URL, for resolving relative links
|
||||
|
||||
Returns:
|
||||
True if the target is linked, False otherwise
|
||||
"""
|
||||
target = _strip_fragment(target_url)
|
||||
media_type = content_type.split(';')[0].strip().lower()
|
||||
|
||||
if 'html' in media_type:
|
||||
from lxml import html as lxml_html
|
||||
try:
|
||||
doc = lxml_html.document_fromstring(content)
|
||||
except Exception as e:
|
||||
logger.debug(f"HTML parsing failed: {e}")
|
||||
return False
|
||||
if source_url:
|
||||
doc.make_links_absolute(source_url, resolve_base_href=True,
|
||||
handle_failures='ignore')
|
||||
return any(_strip_fragment(link) == target for _, _, link, _ in doc.iterlinks())
|
||||
|
||||
if media_type == 'application/json' or media_type.endswith('+json'):
|
||||
try:
|
||||
data = json.loads(content)
|
||||
except ValueError:
|
||||
return False
|
||||
return any(_strip_fragment(s) == target for s in _json_strings(data))
|
||||
|
||||
if media_type.startswith('text/'):
|
||||
return target in content
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def detect_mention_type(parsed_mf2: Dict, target_url: str) -> str:
|
||||
"""
|
||||
Detect mention type from the h-entry that responds to the target
|
||||
|
||||
Returns:
|
||||
'reply', 'like', 'repost', 'bookmark', or 'mention'
|
||||
"""
|
||||
entry = mf2_utils.responding_entry(parsed_mf2, target_url)
|
||||
return mf2_utils.response_type(entry, target_url) if entry else 'mention'
|
||||
|
||||
|
||||
def _is_http_url(value: Optional[str]) -> bool:
|
||||
"""True if value is an absolute http or https URL"""
|
||||
if not value:
|
||||
return False
|
||||
parsed = urlparse(value.strip())
|
||||
return parsed.scheme.lower() in ('http', 'https') and bool(parsed.netloc)
|
||||
|
||||
|
||||
def extract_author(parsed_mf2: Dict, source_url: str,
|
||||
entry: Optional[Dict] = None) -> Dict[str, Optional[str]]:
|
||||
"""
|
||||
Author of the source's h-entry (authorship algorithm, without fetching)
|
||||
|
||||
Args:
|
||||
parsed_mf2: Parsed microformats data from mf2py
|
||||
source_url: Source URL (fallback for author URL and name)
|
||||
entry: The h-entry to attribute (default: first top-level h-entry)
|
||||
|
||||
Returns:
|
||||
Dict with author_name, author_url, author_photo; only http(s) URLs
|
||||
are kept, so they are safe to render as links (XSS)
|
||||
"""
|
||||
if entry is None:
|
||||
entry = mf2_utils.responding_entry(parsed_mf2, '')
|
||||
author = mf2_utils.entry_author(parsed_mf2, entry, source_url)
|
||||
|
||||
author_url = author['url'] if _is_http_url(author['url']) else source_url
|
||||
author_photo = author['photo'] if _is_http_url(author['photo']) else None
|
||||
author_name = author['name'] or urlparse(author_url).netloc
|
||||
|
||||
return {
|
||||
'author_name': author_name,
|
||||
'author_url': author_url,
|
||||
'author_photo': author_photo
|
||||
}
|
||||
|
||||
|
||||
def extract_content(parsed_mf2: Dict, entry: Optional[Dict] = None) -> Optional[str]:
|
||||
"""
|
||||
Content snippet of an h-entry (e-content, else p-summary)
|
||||
|
||||
Args:
|
||||
parsed_mf2: Parsed microformats data from mf2py
|
||||
entry: The h-entry (default: first top-level h-entry)
|
||||
|
||||
Returns:
|
||||
Sanitized content snippet (max 500 chars) or None
|
||||
"""
|
||||
if entry is None:
|
||||
entry = mf2_utils.responding_entry(parsed_mf2, '')
|
||||
if not entry:
|
||||
return None
|
||||
|
||||
props = entry.get('properties', {})
|
||||
for content in props.get('content', []):
|
||||
html = (content.get('html') or content.get('value')) if isinstance(content, dict) else content
|
||||
if html:
|
||||
return _snippet(html)
|
||||
|
||||
summary = mf2_utils.first(entry, 'summary')
|
||||
return _snippet(summary) if summary else None
|
||||
|
||||
|
||||
def extract_source_details(entry: Optional[Dict]) -> Dict[str, Optional[str]]:
|
||||
"""
|
||||
The source post's own URL, uid and published time
|
||||
|
||||
Returns:
|
||||
source_post_url, source_uid (http(s) only) and source_published
|
||||
(RFC 3339 UTC, or None if missing or unparseable)
|
||||
"""
|
||||
if not entry:
|
||||
return {'source_post_url': None, 'source_uid': None, 'source_published': None}
|
||||
|
||||
url = mf2_utils.first(entry, 'url')
|
||||
uid = mf2_utils.first(entry, 'uid')
|
||||
published = mf2_utils.first(entry, 'published')
|
||||
try:
|
||||
published = iso_utc(published) or None
|
||||
except (ValueError, TypeError):
|
||||
published = None
|
||||
|
||||
return {
|
||||
'source_post_url': url if _is_http_url(url) else None,
|
||||
'source_uid': uid,
|
||||
'source_published': published,
|
||||
}
|
||||
|
||||
|
||||
def _snippet(html: str, limit: int = 500) -> str:
|
||||
"""Sanitized HTML of at most ~limit characters, with tags closed"""
|
||||
sanitized = sanitize_content(html)
|
||||
if len(sanitized) > limit:
|
||||
# Cutting can leave a tag or attribute open; sanitizing again closes it
|
||||
sanitized = sanitize_content(sanitized[:limit - 3] + '...')
|
||||
return sanitized
|
||||
|
||||
|
||||
def sanitize_content(html: str) -> str:
|
||||
"""
|
||||
Sanitize HTML content using nh3.
|
||||
|
||||
Per api-contract.md:
|
||||
- Allowed tags: p, br, strong, em, b, i, a, code, pre, blockquote
|
||||
- Allowed attributes: a[href, rel]
|
||||
|
||||
Args:
|
||||
html: HTML content to sanitize
|
||||
|
||||
Returns:
|
||||
Sanitized HTML
|
||||
"""
|
||||
allowed_tags = {'p', 'br', 'strong', 'em', 'b', 'i', 'a', 'code', 'pre', 'blockquote'}
|
||||
allowed_attributes = {'a': {'href'}}
|
||||
|
||||
return nh3.clean(
|
||||
html,
|
||||
tags=allowed_tags,
|
||||
attributes=allowed_attributes,
|
||||
link_rel='nofollow noopener'
|
||||
)
|
||||
|
||||
|
||||
def process_webmention(source_url: str, target_url: str, site_url: str) -> Dict[str, Any]:
|
||||
"""
|
||||
Process and validate a webmention.
|
||||
|
||||
Args:
|
||||
source_url: Source URL of the webmention
|
||||
target_url: Target URL being mentioned
|
||||
site_url: Site URL for User-Agent header
|
||||
|
||||
Returns:
|
||||
Dict with validation results:
|
||||
- valid: bool
|
||||
- error: str (if valid=False)
|
||||
- mention_type: str
|
||||
- author_name: str
|
||||
- author_url: str
|
||||
- author_photo: str or None
|
||||
- content_snippet: str or None
|
||||
|
||||
Raises:
|
||||
ValidationError: If validation fails with specific error
|
||||
"""
|
||||
# Fetch source content
|
||||
content, content_type, final_url = fetch_source(source_url, site_url)
|
||||
|
||||
# Verify target link presence
|
||||
if not verify_link_presence(content, target_url, content_type, final_url):
|
||||
raise ValidationError("Source does not link to target")
|
||||
|
||||
# Parse microformats (only for HTML)
|
||||
parsed_mf2 = None
|
||||
if 'html' in content_type.lower():
|
||||
try:
|
||||
parsed_mf2 = mf2py.parse(doc=content, url=final_url)
|
||||
except Exception as e:
|
||||
logger.debug(f"Microformat parsing failed for {source_url}: {e}")
|
||||
|
||||
# Extract data
|
||||
mention_type = 'mention'
|
||||
author_info = {
|
||||
'author_name': urlparse(source_url).netloc,
|
||||
'author_url': source_url,
|
||||
'author_photo': None
|
||||
}
|
||||
content_snippet = None
|
||||
|
||||
source_details = extract_source_details(None)
|
||||
|
||||
if parsed_mf2:
|
||||
entry = mf2_utils.responding_entry(parsed_mf2, target_url)
|
||||
mention_type = mf2_utils.response_type(entry, target_url) if entry else 'mention'
|
||||
author_info = extract_author(parsed_mf2, final_url, entry)
|
||||
content_snippet = extract_content(parsed_mf2, entry)
|
||||
source_details = extract_source_details(entry)
|
||||
|
||||
return {
|
||||
'valid': True,
|
||||
'mention_type': mention_type,
|
||||
**author_info,
|
||||
'content_snippet': content_snippet,
|
||||
**source_details,
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
"""
|
||||
Webmention database query functions
|
||||
|
||||
Provides database query functions for webmention management.
|
||||
Per display-moderation.md design specification.
|
||||
|
||||
Functions:
|
||||
- get_webmentions_for_note: Get approved webmentions for public display
|
||||
- list_webmentions: List webmentions for admin with filters and pagination
|
||||
- get_webmention: Get single webmention by ID
|
||||
- count_webmentions: Count webmentions (optionally filtered)
|
||||
- update_webmention_status: Update moderation status
|
||||
- delete_webmentions: Delete webmentions by IDs
|
||||
- get_reply_context: Get cached reply context for a URL
|
||||
"""
|
||||
|
||||
import logging
|
||||
import math
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
from flask import current_app
|
||||
from starpunk.database import get_db
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_webmentions_for_note(note_id: int) -> Dict[str, List[Dict[str, Any]]]:
|
||||
"""
|
||||
Get approved webmentions for a note, grouped by type for public display.
|
||||
|
||||
Per display-moderation.md:
|
||||
- Only returns validation_status='valid' AND moderation_status='approved'
|
||||
- Groups by mention_type (reply, like, repost, bookmark, mention)
|
||||
- Sorts within groups by received_at ASC (oldest first)
|
||||
|
||||
Args:
|
||||
note_id: Note database ID
|
||||
|
||||
Returns:
|
||||
Dict with keys: 'replies', 'likes', 'reposts', 'bookmarks', 'mentions'
|
||||
Each value is a list of webmention dicts
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
# Query for approved webmentions only
|
||||
query = """
|
||||
SELECT * FROM webmentions
|
||||
WHERE target_note_id = ?
|
||||
AND validation_status = 'valid'
|
||||
AND moderation_status = 'approved'
|
||||
ORDER BY
|
||||
CASE mention_type
|
||||
WHEN 'reply' THEN 1
|
||||
WHEN 'like' THEN 2
|
||||
WHEN 'repost' THEN 3
|
||||
WHEN 'bookmark' THEN 4
|
||||
WHEN 'mention' THEN 5
|
||||
END,
|
||||
received_at ASC
|
||||
"""
|
||||
|
||||
rows = db.execute(query, (note_id,)).fetchall()
|
||||
|
||||
# Group by type
|
||||
grouped = {
|
||||
'replies': [],
|
||||
'likes': [],
|
||||
'reposts': [],
|
||||
'bookmarks': [],
|
||||
'mentions': []
|
||||
}
|
||||
|
||||
for row in rows:
|
||||
wm = dict(row)
|
||||
# Parse timestamps (source_published may be missing or malformed)
|
||||
for key in ('received_at', 'source_published'):
|
||||
if isinstance(wm.get(key), str):
|
||||
try:
|
||||
wm[key] = datetime.fromisoformat(wm[key].replace('Z', '+00:00'))
|
||||
except ValueError:
|
||||
wm[key] = None
|
||||
|
||||
mention_type = wm.get('mention_type', 'mention')
|
||||
if mention_type == 'reply':
|
||||
grouped['replies'].append(wm)
|
||||
elif mention_type == 'like':
|
||||
grouped['likes'].append(wm)
|
||||
elif mention_type == 'repost':
|
||||
grouped['reposts'].append(wm)
|
||||
elif mention_type == 'bookmark':
|
||||
grouped['bookmarks'].append(wm)
|
||||
else:
|
||||
grouped['mentions'].append(wm)
|
||||
|
||||
return grouped
|
||||
|
||||
|
||||
def list_webmentions(
|
||||
status_filter: Optional[str] = None,
|
||||
page: int = 1,
|
||||
per_page: int = 25
|
||||
) -> tuple[List[Dict[str, Any]], int]:
|
||||
"""
|
||||
List webmentions for admin with pagination and filtering.
|
||||
|
||||
Args:
|
||||
status_filter: Filter by moderation_status ('pending', 'approved', 'hidden')
|
||||
page: Page number (1-indexed)
|
||||
per_page: Items per page (default: 25)
|
||||
|
||||
Returns:
|
||||
Tuple of (webmentions list, total_pages)
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
# Build query
|
||||
base_query = """
|
||||
SELECT w.*, n.slug as target_slug
|
||||
FROM webmentions w
|
||||
LEFT JOIN notes n ON w.target_note_id = n.id
|
||||
"""
|
||||
count_query = "SELECT COUNT(*) FROM webmentions"
|
||||
|
||||
params = []
|
||||
where_clause = ""
|
||||
|
||||
if status_filter:
|
||||
where_clause = " WHERE w.moderation_status = ?"
|
||||
count_query += " WHERE moderation_status = ?"
|
||||
params.append(status_filter)
|
||||
|
||||
# Count total
|
||||
total_count = db.execute(count_query, params).fetchone()[0]
|
||||
total_pages = math.ceil(total_count / per_page) if total_count > 0 else 1
|
||||
|
||||
# Get paginated results
|
||||
offset = (page - 1) * per_page
|
||||
query = base_query + where_clause + " ORDER BY w.received_at DESC LIMIT ? OFFSET ?"
|
||||
params.extend([per_page, offset])
|
||||
|
||||
rows = db.execute(query, params).fetchall()
|
||||
|
||||
# Convert to dicts
|
||||
webmentions = []
|
||||
for row in rows:
|
||||
wm = dict(row)
|
||||
# Parse timestamps
|
||||
if isinstance(wm.get('received_at'), str):
|
||||
wm['received_at'] = datetime.fromisoformat(wm['received_at'].replace('Z', '+00:00'))
|
||||
if wm.get('validated_at') and isinstance(wm['validated_at'], str):
|
||||
wm['validated_at'] = datetime.fromisoformat(wm['validated_at'].replace('Z', '+00:00'))
|
||||
webmentions.append(wm)
|
||||
|
||||
return webmentions, total_pages
|
||||
|
||||
|
||||
def get_webmention(webmention_id: int) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Get single webmention by ID.
|
||||
|
||||
Args:
|
||||
webmention_id: Webmention database ID
|
||||
|
||||
Returns:
|
||||
Webmention dict or None if not found
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
query = """
|
||||
SELECT w.*, n.slug as target_slug
|
||||
FROM webmentions w
|
||||
LEFT JOIN notes n ON w.target_note_id = n.id
|
||||
WHERE w.id = ?
|
||||
"""
|
||||
|
||||
row = db.execute(query, (webmention_id,)).fetchone()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
wm = dict(row)
|
||||
# Parse timestamps
|
||||
if isinstance(wm.get('received_at'), str):
|
||||
wm['received_at'] = datetime.fromisoformat(wm['received_at'].replace('Z', '+00:00'))
|
||||
if wm.get('validated_at') and isinstance(wm['validated_at'], str):
|
||||
wm['validated_at'] = datetime.fromisoformat(wm['validated_at'].replace('Z', '+00:00'))
|
||||
|
||||
return wm
|
||||
|
||||
|
||||
def count_webmentions(status_filter: Optional[str] = None) -> int:
|
||||
"""
|
||||
Count webmentions, optionally filtered by status.
|
||||
|
||||
Args:
|
||||
status_filter: Filter by moderation_status
|
||||
|
||||
Returns:
|
||||
Count of webmentions
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
if status_filter:
|
||||
query = "SELECT COUNT(*) FROM webmentions WHERE moderation_status = ?"
|
||||
result = db.execute(query, (status_filter,)).fetchone()
|
||||
else:
|
||||
query = "SELECT COUNT(*) FROM webmentions"
|
||||
result = db.execute(query).fetchone()
|
||||
|
||||
return result[0] if result else 0
|
||||
|
||||
|
||||
def update_webmention_status(webmention_ids: List[int], status: str) -> int:
|
||||
"""
|
||||
Update moderation status for multiple webmentions.
|
||||
|
||||
Args:
|
||||
webmention_ids: List of webmention IDs
|
||||
status: New moderation_status ('approved', 'hidden', 'pending')
|
||||
|
||||
Returns:
|
||||
Number of webmentions updated
|
||||
"""
|
||||
if not webmention_ids:
|
||||
return 0
|
||||
|
||||
if status not in ('approved', 'hidden', 'pending'):
|
||||
raise ValueError(f"Invalid status: {status}")
|
||||
|
||||
db = get_db()
|
||||
|
||||
# Use parameterized query with placeholders
|
||||
placeholders = ','.join('?' * len(webmention_ids))
|
||||
query = f"""
|
||||
UPDATE webmentions
|
||||
SET moderation_status = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id IN ({placeholders})
|
||||
"""
|
||||
|
||||
# Notes whose displayed mentions change (valid ones switching between
|
||||
# approved and not)
|
||||
changed_notes = _visibility_changes(
|
||||
db, webmention_ids,
|
||||
"validation_status = 'valid' AND (moderation_status = 'approved') != (? = 'approved')",
|
||||
[status],
|
||||
)
|
||||
|
||||
params = [status] + webmention_ids
|
||||
cursor = db.execute(query, params)
|
||||
db.commit()
|
||||
|
||||
_notify_responses_changed(changed_notes)
|
||||
return cursor.rowcount
|
||||
|
||||
|
||||
def delete_webmentions(webmention_ids: List[int]) -> int:
|
||||
"""
|
||||
Delete webmentions by IDs.
|
||||
|
||||
Args:
|
||||
webmention_ids: List of webmention IDs to delete
|
||||
|
||||
Returns:
|
||||
Number of webmentions deleted
|
||||
"""
|
||||
if not webmention_ids:
|
||||
return 0
|
||||
|
||||
db = get_db()
|
||||
|
||||
changed_notes = _visibility_changes(
|
||||
db, webmention_ids, "validation_status = 'valid' AND moderation_status = 'approved'", [],
|
||||
)
|
||||
|
||||
placeholders = ','.join('?' * len(webmention_ids))
|
||||
query = f"DELETE FROM webmentions WHERE id IN ({placeholders})"
|
||||
|
||||
cursor = db.execute(query, webmention_ids)
|
||||
db.commit()
|
||||
|
||||
_notify_responses_changed(changed_notes)
|
||||
return cursor.rowcount
|
||||
|
||||
|
||||
def _visibility_changes(db, webmention_ids: List[int], condition: str, params: list) -> List[int]:
|
||||
"""Target note IDs of the given webmentions that match condition"""
|
||||
placeholders = ','.join('?' * len(webmention_ids))
|
||||
rows = db.execute(
|
||||
f"SELECT DISTINCT target_note_id FROM webmentions "
|
||||
f"WHERE id IN ({placeholders}) AND {condition}",
|
||||
list(webmention_ids) + params,
|
||||
).fetchall()
|
||||
return [row[0] for row in rows]
|
||||
|
||||
|
||||
def _notify_responses_changed(note_ids: List[int]) -> None:
|
||||
"""Re-send webmentions for notes whose shown responses changed (best effort)"""
|
||||
if not note_ids:
|
||||
return
|
||||
try:
|
||||
from starpunk.webmention_sender import note_responses_changed
|
||||
note_responses_changed(Path(current_app.config['DATABASE_PATH']), note_ids)
|
||||
except Exception as e:
|
||||
current_app.logger.warning(f"Failed to re-send webmentions after moderation: {e}")
|
||||
|
||||
|
||||
def get_reply_context(url: str) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Get cached reply context for a URL.
|
||||
|
||||
Args:
|
||||
url: The in_reply_to URL
|
||||
|
||||
Returns:
|
||||
Reply context dict or None if not found
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
query = "SELECT * FROM reply_contexts WHERE url = ?"
|
||||
row = db.execute(query, (url,)).fetchone()
|
||||
|
||||
if not row:
|
||||
return None
|
||||
|
||||
context = dict(row)
|
||||
|
||||
# Parse timestamps
|
||||
if context.get('fetched_at') and isinstance(context['fetched_at'], str):
|
||||
context['fetched_at'] = datetime.fromisoformat(context['fetched_at'].replace('Z', '+00:00'))
|
||||
if context.get('created_at') and isinstance(context['created_at'], str):
|
||||
context['created_at'] = datetime.fromisoformat(context['created_at'].replace('Z', '+00:00'))
|
||||
if context.get('published_at') and isinstance(context['published_at'], str):
|
||||
context['published_at'] = datetime.fromisoformat(context['published_at'].replace('Z', '+00:00'))
|
||||
|
||||
return context
|
||||
@@ -0,0 +1,248 @@
|
||||
"""
|
||||
Outgoing webmention link extraction and queueing
|
||||
|
||||
Per background-worker.md and architect's answers:
|
||||
- Extract links from rendered HTML (not markdown)
|
||||
- Extract only <a href> elements, exclude <pre> and <code> blocks
|
||||
- Queue max 10 links per note per operation
|
||||
- Priority: in_reply_to first, then document order
|
||||
|
||||
References:
|
||||
- Background Worker Design: /docs/design/v1.6.0/background-worker.md
|
||||
- API Contract: /docs/design/v1.6.0/api-contract.md
|
||||
"""
|
||||
|
||||
import logging
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from lxml import html as lxml_html
|
||||
from urllib.parse import urlparse
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def extract_links_from_html(html_content: str, exclude_code_blocks: bool = True) -> list[str]:
|
||||
"""
|
||||
Extract links from HTML content.
|
||||
|
||||
Per architect's answer to Q1:
|
||||
- Extract from rendered HTML (not markdown)
|
||||
- Extract <a href> elements only
|
||||
- Exclude links in <pre> and <code> blocks
|
||||
- Return in document order
|
||||
|
||||
Args:
|
||||
html_content: Rendered HTML content
|
||||
exclude_code_blocks: If True, skip links in <pre> and <code> (default: True)
|
||||
|
||||
Returns:
|
||||
List of URLs in document order
|
||||
"""
|
||||
if not html_content or not html_content.strip():
|
||||
return []
|
||||
|
||||
try:
|
||||
# Parse HTML inside a wrapper, so a top-level <pre> can be removed too
|
||||
doc = lxml_html.fragment_fromstring(html_content, create_parent='div')
|
||||
|
||||
if exclude_code_blocks:
|
||||
# Remove <pre> and <code> elements before extracting links
|
||||
for element in doc.xpath('.//pre | .//code'):
|
||||
element.drop_tree()
|
||||
|
||||
# Extract all <a href> attributes in document order
|
||||
links = doc.xpath('.//a/@href')
|
||||
|
||||
# Filter to valid absolute URLs (http/https)
|
||||
absolute_links = []
|
||||
for link in links:
|
||||
parsed = urlparse(link)
|
||||
if parsed.scheme in ('http', 'https') and parsed.netloc:
|
||||
absolute_links.append(link)
|
||||
|
||||
return absolute_links
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to parse HTML for link extraction: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def _dedupe(urls: list) -> list:
|
||||
"""Drop empty and repeated URLs, keeping the first occurrence"""
|
||||
seen = set()
|
||||
result = []
|
||||
for url in urls:
|
||||
if url and url not in seen:
|
||||
seen.add(url)
|
||||
result.append(url)
|
||||
return result
|
||||
|
||||
|
||||
def previous_targets(db_path: Path, note_id: int) -> list[str]:
|
||||
"""Targets this note has been queued to send webmentions to before"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT target_url FROM webmention_queue WHERE source_note_id = ? ORDER BY id",
|
||||
(note_id,),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [row[0] for row in rows]
|
||||
|
||||
|
||||
def requeue_previous_webmentions(db_path: Path, note_id: int, detach: bool = False) -> int:
|
||||
"""
|
||||
Re-send every webmention this note has sent (spec 3.1.5 deleted posts)
|
||||
|
||||
Args:
|
||||
db_path: Path to SQLite database
|
||||
note_id: Note being deleted or restored
|
||||
detach: Clear source_note_id so the rows survive a hard delete
|
||||
(the foreign key cascades)
|
||||
|
||||
Returns:
|
||||
Number of webmentions queued
|
||||
"""
|
||||
conn = sqlite3.connect(db_path)
|
||||
try:
|
||||
cursor = conn.execute(f"""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'pending',
|
||||
retry_count = 0,
|
||||
next_retry_at = NULL,
|
||||
last_error = NULL,
|
||||
endpoint_url = NULL,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
{', source_note_id = NULL' if detach else ''}
|
||||
WHERE source_note_id = ?
|
||||
""", (note_id,))
|
||||
conn.commit()
|
||||
queued = cursor.rowcount
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
if queued:
|
||||
logger.info(f"Re-queued {queued} webmentions for note {note_id}")
|
||||
return queued
|
||||
|
||||
|
||||
def note_responses_changed(db_path: Path, note_ids) -> int:
|
||||
"""
|
||||
Re-send a note's webmentions because the responses it shows changed
|
||||
|
||||
Spec 3.1.4: if the source post displays responses (e.g. comments), a
|
||||
change to them is an update of the post. Only published, undeleted notes.
|
||||
|
||||
Args:
|
||||
db_path: Path to SQLite database
|
||||
note_ids: IDs of notes whose displayed responses changed
|
||||
|
||||
Returns:
|
||||
Number of webmentions queued
|
||||
"""
|
||||
ids = sorted({i for i in note_ids if i is not None})
|
||||
if not ids:
|
||||
return 0
|
||||
|
||||
conn = sqlite3.connect(db_path)
|
||||
try:
|
||||
placeholders = ','.join('?' * len(ids))
|
||||
live = [row[0] for row in conn.execute(
|
||||
f"SELECT id FROM notes WHERE id IN ({placeholders}) "
|
||||
"AND published = 1 AND deleted_at IS NULL", ids,
|
||||
)]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
return sum(requeue_previous_webmentions(db_path, note_id) for note_id in live)
|
||||
|
||||
|
||||
def queue_webmentions_for_note(
|
||||
db_path: Path,
|
||||
note_id: int,
|
||||
note_url: str,
|
||||
content_html: str,
|
||||
in_reply_to: Optional[str] = None,
|
||||
old_content_html: Optional[str] = None # Phase 6: update semantics
|
||||
) -> int:
|
||||
"""
|
||||
Extract links and queue webmentions for a note.
|
||||
|
||||
Per architect's answers (Phase 6):
|
||||
- Called from notes.py AFTER successful database commit
|
||||
- For creates: reply target first, then links in document order (max 10)
|
||||
- For updates: the same, plus every previously sent target and links
|
||||
removed by the edit (spec 3.1.4)
|
||||
- Webmention sending is best-effort; failures do not affect note creation
|
||||
|
||||
Args:
|
||||
db_path: Path to SQLite database
|
||||
note_id: Database ID of the note
|
||||
note_url: Full URL of the published note
|
||||
content_html: Rendered HTML content (None if unpublishing)
|
||||
in_reply_to: Reply-to URL (if reply post)
|
||||
old_content_html: Previous HTML (for update diff, Phase 6)
|
||||
|
||||
Returns:
|
||||
Number of webmentions queued
|
||||
"""
|
||||
# Targets for the current content: reply target first, then links in
|
||||
# document order, at most 10
|
||||
links = extract_links_from_html(content_html) if content_html else []
|
||||
current = _dedupe(([in_reply_to] if in_reply_to else []) + links)[:10]
|
||||
|
||||
if old_content_html is not None:
|
||||
# Update (spec 3.1.4): also re-send to every target notified before,
|
||||
# and to links the edit removed, so they can update or remove the
|
||||
# mention. These are not capped.
|
||||
removed = [u for u in extract_links_from_html(old_content_html) if u not in links]
|
||||
target_urls = _dedupe(current + previous_targets(db_path, note_id) + removed)
|
||||
else:
|
||||
target_urls = current
|
||||
|
||||
if not target_urls:
|
||||
logger.debug(f"No links to send webmentions for note {note_id}")
|
||||
return 0
|
||||
|
||||
# Queue webmentions
|
||||
queued = 0
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
for target_url in target_urls:
|
||||
try:
|
||||
# Use UPSERT to handle duplicate sends
|
||||
# Phase 6: Force endpoint rediscovery per W3C MUST requirement
|
||||
cursor.execute("""
|
||||
INSERT INTO webmention_queue
|
||||
(source_url, target_url, source_note_id, status, created_at)
|
||||
VALUES (?, ?, ?, 'pending', CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(source_url, target_url) DO UPDATE SET
|
||||
status = 'pending',
|
||||
retry_count = 0,
|
||||
next_retry_at = NULL,
|
||||
last_error = NULL,
|
||||
endpoint_url = NULL,
|
||||
created_at = CURRENT_TIMESTAMP
|
||||
""", (note_url, target_url, note_id))
|
||||
|
||||
queued += 1
|
||||
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
f"Failed to queue webmention {note_url} -> {target_url}: {e}"
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.info(f"Queued {queued} webmentions for note {note_id}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to queue webmentions for note {note_id}: {e}")
|
||||
|
||||
return queued
|
||||
@@ -0,0 +1,311 @@
|
||||
"""
|
||||
SSRF protection for webmention fetches
|
||||
|
||||
Per ADR-063 Decision 9 and Webmention sections 4.2, 4.3 and 4.5:
|
||||
- Every address a hostname resolves to must be public
|
||||
- Redirects are followed by hand so each hop is checked
|
||||
- Each request connects to the address that was checked (no second DNS
|
||||
lookup that a rebinding DNS server could answer differently)
|
||||
- Redirect count, response size and total time are limited
|
||||
- DEV_MODE exception for localhost testing
|
||||
|
||||
References:
|
||||
- ADR-063: Webmention Implementation
|
||||
- W3C Webmention: https://www.w3.org/TR/webmention/#security-considerations
|
||||
"""
|
||||
|
||||
import ipaddress
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
from urllib.parse import urljoin, urlparse
|
||||
|
||||
import httpx
|
||||
from flask import current_app
|
||||
|
||||
MAX_REDIRECTS = 10
|
||||
MAX_BYTES = 1024 * 1024
|
||||
TIMEOUT = 10
|
||||
|
||||
# Tests can set an httpx transport here (e.g. httpx.MockTransport)
|
||||
TRANSPORT = None
|
||||
|
||||
CGNAT = ipaddress.ip_network('100.64.0.0/10')
|
||||
THIS_NETWORK = ipaddress.ip_network('0.0.0.0/8')
|
||||
|
||||
|
||||
class SSRFBlockedError(Exception):
|
||||
"""Raised when SSRF protection blocks a request"""
|
||||
pass
|
||||
|
||||
|
||||
class ResolutionError(ValueError):
|
||||
"""The host name could not be resolved (may be temporary; v2.10.1)"""
|
||||
pass
|
||||
|
||||
|
||||
class FetchTooLargeError(Exception):
|
||||
"""Raised when a response is larger than the limit"""
|
||||
pass
|
||||
|
||||
|
||||
def user_agent(site_url: str) -> str:
|
||||
"""User-Agent for webmention requests (spec 3.1.2 suggests including 'Webmention')"""
|
||||
from starpunk import __version__
|
||||
return f"StarPunk/{__version__} Webmention (+{site_url})"
|
||||
|
||||
|
||||
def is_private_ip(ip: str) -> bool:
|
||||
"""
|
||||
Check if an IP address must not be fetched.
|
||||
|
||||
Blocks private, loopback, link-local, multicast, reserved, unspecified,
|
||||
carrier-grade NAT and IPv4-mapped forms of these.
|
||||
|
||||
Args:
|
||||
ip: IP address string (IPv4 or IPv6)
|
||||
|
||||
Returns:
|
||||
True if IP is blocked, False otherwise
|
||||
"""
|
||||
try:
|
||||
addr = ipaddress.ip_address(ip.split('%')[0])
|
||||
except ValueError:
|
||||
return True
|
||||
|
||||
if isinstance(addr, ipaddress.IPv6Address) and addr.ipv4_mapped:
|
||||
addr = addr.ipv4_mapped
|
||||
|
||||
if isinstance(addr, ipaddress.IPv4Address) and (addr in CGNAT or addr in THIS_NETWORK):
|
||||
return True
|
||||
|
||||
return (
|
||||
addr.is_private
|
||||
or addr.is_loopback
|
||||
or addr.is_link_local
|
||||
or addr.is_multicast
|
||||
or addr.is_reserved
|
||||
or addr.is_unspecified
|
||||
)
|
||||
|
||||
|
||||
def safe_resolve(hostname: str) -> str:
|
||||
"""
|
||||
Resolve hostname and check every address is public.
|
||||
|
||||
Per background-worker.md: DEV_MODE exception for localhost testing.
|
||||
|
||||
Returns:
|
||||
Address to connect to (IPv4 preferred)
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError: If any address is blocked
|
||||
socket.gaierror: If hostname cannot be resolved
|
||||
"""
|
||||
if current_app.config.get('DEV_MODE') and hostname in ('localhost', '127.0.0.1', '::1'):
|
||||
return '127.0.0.1'
|
||||
|
||||
addresses = sorted({info[4][0] for info in socket.getaddrinfo(hostname, None)},
|
||||
key=lambda ip: (':' in ip, ip))
|
||||
for ip in addresses:
|
||||
if is_private_ip(ip):
|
||||
raise SSRFBlockedError(f"Blocked request to private IP: {ip}")
|
||||
|
||||
return addresses[0]
|
||||
|
||||
|
||||
def validate_url_for_fetch(url: str) -> str:
|
||||
"""
|
||||
Validate URL is safe to fetch (http(s), public addresses only).
|
||||
|
||||
Returns:
|
||||
The checked IP address to connect to
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError: If URL points to a blocked address
|
||||
ValueError: If URL is malformed or its host does not resolve
|
||||
"""
|
||||
parsed = urlparse(url)
|
||||
|
||||
if parsed.scheme not in ('http', 'https'):
|
||||
raise ValueError(f"Unsupported URL scheme: {parsed.scheme}")
|
||||
|
||||
if not parsed.hostname:
|
||||
raise ValueError("URL must include a hostname")
|
||||
|
||||
try:
|
||||
return safe_resolve(parsed.hostname)
|
||||
except socket.gaierror as e:
|
||||
raise ResolutionError(f"Could not resolve {parsed.hostname}: {e}")
|
||||
|
||||
|
||||
def _pinned_request(client: httpx.Client, method: str, url: str, headers: dict,
|
||||
**kwargs) -> httpx.Request:
|
||||
"""
|
||||
Build a request that connects to the checked address for url's host.
|
||||
|
||||
The URL's host is replaced by the IP that passed the SSRF check; the
|
||||
real host goes in the Host header and, for https, in SNI, so virtual
|
||||
hosting and certificate verification still use the hostname.
|
||||
"""
|
||||
ip = validate_url_for_fetch(url)
|
||||
parsed = urlparse(url)
|
||||
ip_host = f"[{ip}]" if ':' in ip else ip
|
||||
port = f":{parsed.port}" if parsed.port else ''
|
||||
pinned_url = parsed._replace(netloc=f"{ip_host}{port}").geturl()
|
||||
host_header = f"{parsed.hostname}{port}"
|
||||
|
||||
extensions = {'sni_hostname': parsed.hostname} if parsed.scheme == 'https' else {}
|
||||
return client.build_request(
|
||||
method, pinned_url, headers={**headers, 'Host': host_header},
|
||||
extensions=extensions, **kwargs,
|
||||
)
|
||||
|
||||
|
||||
class _Watchdog:
|
||||
"""
|
||||
Close a client when its deadline passes (v2.10.1)
|
||||
|
||||
httpx timeouts apply per read, so a server sending one byte at a time
|
||||
could hold a request for hours; closing the client ends it.
|
||||
"""
|
||||
|
||||
def __init__(self, client: httpx.Client, seconds: float):
|
||||
self.fired = False
|
||||
|
||||
def fire():
|
||||
self.fired = True
|
||||
try:
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self._timer = threading.Timer(max(seconds, 0.01), fire)
|
||||
self._timer.daemon = True
|
||||
|
||||
def __enter__(self):
|
||||
self._timer.start()
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc, tb):
|
||||
self._timer.cancel()
|
||||
if self.fired and exc_type is not None and not isinstance(exc, httpx.TimeoutException):
|
||||
raise httpx.TimeoutException("Exceeded time limit") from exc
|
||||
return False
|
||||
|
||||
|
||||
@dataclass
|
||||
class FetchResult:
|
||||
"""A fetched response, after redirects"""
|
||||
url: str
|
||||
status_code: int
|
||||
headers: httpx.Headers
|
||||
content: bytes
|
||||
encoding: str
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
try:
|
||||
return self.content.decode(self.encoding, errors='replace')
|
||||
except LookupError:
|
||||
return self.content.decode('utf-8', errors='replace')
|
||||
|
||||
|
||||
def safe_post(url: str, data: Optional[dict] = None, headers: Optional[dict] = None,
|
||||
timeout: float = TIMEOUT, content: Optional[bytes] = None,
|
||||
max_bytes: int = 4096) -> FetchResult:
|
||||
"""
|
||||
POST form data (or a raw body) to a checked, pinned address.
|
||||
|
||||
Redirects are not followed. `timeout` bounds the whole exchange, so a
|
||||
server trickling its response can't hold the caller (v2.10.1). Only the
|
||||
first max_bytes of the response are read (callers need the status and
|
||||
maybe an error message).
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError, ValueError: URL is not allowed
|
||||
httpx.HTTPError: Network errors
|
||||
"""
|
||||
kwargs = {'content': content} if content is not None else {'data': data}
|
||||
deadline = time.monotonic() + timeout
|
||||
with httpx.Client(follow_redirects=False, transport=TRANSPORT, timeout=timeout) as client, \
|
||||
_Watchdog(client, timeout):
|
||||
request = _pinned_request(client, 'POST', url, headers or {}, **kwargs)
|
||||
response = client.send(request, stream=True)
|
||||
try:
|
||||
body = b''
|
||||
for chunk in response.iter_bytes(chunk_size=1024):
|
||||
body += chunk
|
||||
if len(body) >= max_bytes:
|
||||
body = body[:max_bytes]
|
||||
break
|
||||
if time.monotonic() > deadline:
|
||||
raise httpx.TimeoutException("Response exceeded time limit")
|
||||
return FetchResult(url=url, status_code=response.status_code, headers=response.headers,
|
||||
content=body, encoding=response.charset_encoding or 'utf-8')
|
||||
finally:
|
||||
response.close()
|
||||
|
||||
|
||||
def safe_get(url: str, headers: dict, max_bytes: int = MAX_BYTES,
|
||||
max_redirects: int = MAX_REDIRECTS, timeout: float = TIMEOUT) -> FetchResult:
|
||||
"""
|
||||
GET a URL, following redirects by hand and checking each hop.
|
||||
|
||||
Args:
|
||||
url: URL to fetch
|
||||
headers: Request headers
|
||||
max_bytes: Response body limit
|
||||
max_redirects: Redirect limit
|
||||
timeout: Total time limit in seconds
|
||||
|
||||
Returns:
|
||||
FetchResult for the final response (any status code)
|
||||
|
||||
Raises:
|
||||
SSRFBlockedError, ValueError: URL or a redirect target is not allowed
|
||||
FetchTooLargeError: Body exceeds max_bytes
|
||||
httpx.HTTPError: Network errors, too many redirects, time limit
|
||||
"""
|
||||
deadline = time.monotonic() + timeout
|
||||
|
||||
with httpx.Client(follow_redirects=False, transport=TRANSPORT) as client, \
|
||||
_Watchdog(client, timeout):
|
||||
for _ in range(max_redirects + 1):
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise httpx.TimeoutException("Fetch exceeded time limit")
|
||||
|
||||
request = _pinned_request(client, 'GET', url, headers, timeout=remaining)
|
||||
response = client.send(request, stream=True)
|
||||
try:
|
||||
location = response.headers.get('Location')
|
||||
if response.is_redirect and location:
|
||||
url = urljoin(url, location)
|
||||
continue
|
||||
|
||||
content_length = response.headers.get('Content-Length', '')
|
||||
if content_length.isdigit() and int(content_length) > max_bytes:
|
||||
raise FetchTooLargeError(f"Response exceeds size limit ({max_bytes} bytes)")
|
||||
|
||||
content = b''
|
||||
for chunk in response.iter_bytes(chunk_size=8192):
|
||||
content += chunk
|
||||
if len(content) > max_bytes:
|
||||
raise FetchTooLargeError(f"Response exceeds size limit ({max_bytes} bytes)")
|
||||
if time.monotonic() > deadline:
|
||||
raise httpx.TimeoutException("Fetch exceeded time limit")
|
||||
|
||||
return FetchResult(
|
||||
url=url,
|
||||
status_code=response.status_code,
|
||||
headers=response.headers,
|
||||
content=content,
|
||||
encoding=response.charset_encoding or 'utf-8',
|
||||
)
|
||||
finally:
|
||||
response.close()
|
||||
|
||||
raise httpx.TooManyRedirects(f"More than {max_redirects} redirects")
|
||||
@@ -0,0 +1,884 @@
|
||||
"""
|
||||
Background worker thread for webmention processing
|
||||
|
||||
Per background-worker.md:
|
||||
- Daemon thread that processes webmention queues
|
||||
- Handles both incoming validation and outgoing sending
|
||||
- Graceful shutdown on app termination
|
||||
- Orphan detection and cleanup on startup
|
||||
|
||||
References:
|
||||
- Background Worker Design: /docs/design/v1.6.0/background-worker.md
|
||||
- ADR-063: Webmention Implementation
|
||||
"""
|
||||
|
||||
import fcntl
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import time
|
||||
import sqlite3
|
||||
import httpx
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
from urllib.parse import urljoin
|
||||
from lxml import html as lxml_html
|
||||
|
||||
from starpunk.webmention_processor import process_webmention, TransientError, ValidationError
|
||||
from starpunk.webmention_sender import note_responses_changed
|
||||
from starpunk.mf2_utils import normalize_url
|
||||
from starpunk.webmention_ssrf import (
|
||||
FetchTooLargeError, SSRFBlockedError, safe_get, safe_post, user_agent,
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Link header (RFC 8288): <uri> then ;-separated params; quoted values may
|
||||
# contain , and ;
|
||||
LINK_RE = re.compile(r'<([^>]*)>((?:\s*;\s*[^;,"=\s]+\s*(?:=\s*(?:"[^"]*"|[^;,]*))?)*)')
|
||||
LINK_PARAM_RE = re.compile(r';\s*([^;,"=\s]+)\s*(?:=\s*(?:"([^"]*)"|([^;,]*)))?')
|
||||
|
||||
|
||||
|
||||
class WebmentionWorker(threading.Thread):
|
||||
"""
|
||||
Background thread for webmention queue processing
|
||||
|
||||
Follows MemoryMonitor pattern: daemon thread with stop event
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
app,
|
||||
db_path: Path,
|
||||
site_url: str,
|
||||
poll_interval: int = 10,
|
||||
error_backoff: int = 60,
|
||||
batch_size: int = 5,
|
||||
auto_approve: bool = True
|
||||
):
|
||||
"""
|
||||
Initialize webmention worker thread
|
||||
|
||||
Args:
|
||||
app: Flask app (the SSRF checks read its config)
|
||||
db_path: Path to SQLite database
|
||||
site_url: Site URL for User-Agent header
|
||||
poll_interval: Seconds between queue checks (default: 10)
|
||||
error_backoff: Seconds to wait after error (default: 60)
|
||||
batch_size: Items to process per iteration (default: 5)
|
||||
auto_approve: Auto-approve valid webmentions (default: True)
|
||||
"""
|
||||
super().__init__(daemon=True, name="webmention-worker")
|
||||
self.app = app
|
||||
self.db_path = db_path
|
||||
self.site_url = site_url
|
||||
self.poll_interval = poll_interval
|
||||
self.error_backoff = error_backoff
|
||||
self.batch_size = batch_size
|
||||
self.auto_approve = auto_approve
|
||||
self._stop_event = threading.Event()
|
||||
self._last_processed = None
|
||||
self._lock_file = None
|
||||
|
||||
def run(self):
|
||||
"""
|
||||
Main processing loop
|
||||
|
||||
Per background-worker.md:
|
||||
1. Clean up orphaned items on startup
|
||||
2. Process pending validations (incoming)
|
||||
3. Process pending sends (outgoing) - Phase 3
|
||||
4. Sleep if nothing to process
|
||||
"""
|
||||
try:
|
||||
logger.info("Webmention worker started")
|
||||
|
||||
# Main processing loop
|
||||
while not self._stop_event.is_set():
|
||||
# Every gunicorn process starts a worker; only the one holding
|
||||
# the lock processes queues, so nothing is sent twice
|
||||
if not self._acquire_lock():
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
continue
|
||||
|
||||
try:
|
||||
processed = 0
|
||||
|
||||
with self.app.app_context():
|
||||
# Process incoming webmentions
|
||||
processed += self._process_pending_validations()
|
||||
|
||||
# Phase 3: Process outgoing webmentions
|
||||
processed += self._process_pending_sends()
|
||||
|
||||
# Update last processed time
|
||||
if processed > 0:
|
||||
self._last_processed = datetime.utcnow()
|
||||
|
||||
# Sleep if nothing processed
|
||||
if processed == 0:
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Worker error: {e}", exc_info=True)
|
||||
self._stop_event.wait(self.error_backoff)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Webmention worker thread failed: {e}", exc_info=True)
|
||||
finally:
|
||||
self._release_lock()
|
||||
logger.info("Webmention worker stopped")
|
||||
|
||||
def _acquire_lock(self) -> bool:
|
||||
"""
|
||||
Take the cross-process worker lock (non-blocking)
|
||||
|
||||
On first acquisition, also reset items left mid-send by a previous
|
||||
holder. The OS releases the lock if this process dies.
|
||||
|
||||
Returns:
|
||||
True if this worker holds the lock
|
||||
"""
|
||||
if self._lock_file:
|
||||
return True
|
||||
|
||||
lock_file = open(Path(self.db_path).parent / "webmention-worker.lock", "w")
|
||||
try:
|
||||
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
lock_file.close()
|
||||
return False
|
||||
|
||||
self._lock_file = lock_file
|
||||
logger.info("Webmention worker holds the queue lock")
|
||||
self._cleanup_orphaned_items()
|
||||
return True
|
||||
|
||||
def _release_lock(self):
|
||||
"""Release the cross-process worker lock if held"""
|
||||
if self._lock_file:
|
||||
self._lock_file.close()
|
||||
self._lock_file = None
|
||||
|
||||
def _cleanup_orphaned_items(self):
|
||||
"""
|
||||
Clean up orphaned 'sending' status items on startup
|
||||
|
||||
Per background-worker.md:
|
||||
Reset items stuck in 'sending' status from previous crashes
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Reset orphaned outgoing webmentions
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'pending',
|
||||
started_at = NULL
|
||||
WHERE status = 'sending'
|
||||
AND started_at < datetime('now', '-5 minutes')
|
||||
""")
|
||||
|
||||
orphaned = cursor.rowcount
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
if orphaned > 0:
|
||||
logger.info(f"Reset {orphaned} orphaned webmention queue items")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to cleanup orphaned items: {e}")
|
||||
|
||||
def _process_pending_validations(self) -> int:
|
||||
"""
|
||||
Process pending incoming webmentions
|
||||
|
||||
Returns:
|
||||
Number of webmentions processed
|
||||
"""
|
||||
processed = 0
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Get pending webmentions
|
||||
cursor.execute("""
|
||||
SELECT id, source_url, target_url, target_note_id
|
||||
FROM webmentions
|
||||
WHERE validation_status = 'pending'
|
||||
ORDER BY received_at ASC
|
||||
LIMIT ?
|
||||
""", (self.batch_size,))
|
||||
|
||||
pending = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
# Process each webmention
|
||||
for row in pending:
|
||||
try:
|
||||
self._validate_webmention(
|
||||
row['id'],
|
||||
row['source_url'],
|
||||
row['target_url'],
|
||||
row['target_note_id']
|
||||
)
|
||||
processed += 1
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to validate webmention {row['id']}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching pending validations: {e}")
|
||||
|
||||
return processed
|
||||
|
||||
def _validate_webmention(
|
||||
self,
|
||||
webmention_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
target_note_id: Optional[int]
|
||||
):
|
||||
"""
|
||||
Validate a webmention, then re-send the target note's webmentions if
|
||||
what the note shows changed (spec 3.1.4: displayed responses are an
|
||||
update of the post). Unchanged re-validations send nothing, so two
|
||||
sites doing this don't ping-pong.
|
||||
"""
|
||||
before = self._display_state(webmention_id)
|
||||
try:
|
||||
self._validate_and_store(webmention_id, source_url, target_url, target_note_id)
|
||||
finally:
|
||||
after = self._display_state(webmention_id)
|
||||
if before != after and target_note_id is not None:
|
||||
try:
|
||||
note_responses_changed(self.db_path, [target_note_id])
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to re-send webmentions for note {target_note_id}: {e}")
|
||||
|
||||
def _is_own_syndicated_copy(self, note_id: Optional[int], source_url: str,
|
||||
result: Dict[str, Any]) -> bool:
|
||||
"""True if the source is one of the note's own u-syndication copies"""
|
||||
if note_id is None:
|
||||
return False
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
copies = {normalize_url(row[0]) for row in conn.execute(
|
||||
"SELECT remote_url FROM syndications WHERE note_id = ? AND remote_url IS NOT NULL",
|
||||
(note_id,),
|
||||
)}
|
||||
finally:
|
||||
conn.close()
|
||||
source = {normalize_url(u) for u in (
|
||||
source_url, result.get('source_post_url'), result.get('source_uid')) if u}
|
||||
return bool(copies & source)
|
||||
|
||||
def _display_state(self, webmention_id: int) -> Optional[tuple]:
|
||||
"""
|
||||
What the note page shows for this webmention, or None if hidden
|
||||
|
||||
A row reset to 'pending' by a re-send still counts as shown if it was
|
||||
valid before (validated with no error), as it was until now.
|
||||
"""
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
row = conn.execute("""
|
||||
SELECT mention_type, author_name, author_url, author_photo, content_snippet
|
||||
FROM webmentions
|
||||
WHERE id = ?
|
||||
AND moderation_status = 'approved'
|
||||
AND (validation_status = 'valid'
|
||||
OR (validation_status = 'pending' AND validated_at IS NOT NULL
|
||||
AND validation_error IS NULL))
|
||||
""", (webmention_id,)).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
return tuple(row) if row else None
|
||||
|
||||
def _validate_and_store(
|
||||
self,
|
||||
webmention_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
target_note_id: Optional[int]
|
||||
):
|
||||
"""
|
||||
Validate a single webmention
|
||||
|
||||
Args:
|
||||
webmention_id: Database ID of webmention
|
||||
source_url: Source URL
|
||||
target_url: Target URL
|
||||
target_note_id: Target note ID (if applicable)
|
||||
"""
|
||||
try:
|
||||
# Process webmention (fetch, validate, extract)
|
||||
result = process_webmention(source_url, target_url, self.site_url)
|
||||
|
||||
# A copy of this very note on a silo (e.g. backfed by Bridgy) is
|
||||
# not a response to it
|
||||
if self._is_own_syndicated_copy(target_note_id, source_url, result):
|
||||
raise ValidationError("Source is this note's own syndicated copy")
|
||||
|
||||
# Update database with successful validation
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
moderation_status = 'approved' if self.auto_approve else 'pending'
|
||||
|
||||
# An update to an existing mention (spec 3.2.4) keeps the admin's
|
||||
# moderation decision; only a first-time valid mention gets the
|
||||
# default. Previously valid = validated with no error.
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET moderation_status = CASE
|
||||
WHEN moderation_status IN ('hidden', 'deleted') THEN moderation_status
|
||||
WHEN validated_at IS NOT NULL AND validation_error IS NULL
|
||||
THEN moderation_status
|
||||
ELSE ?
|
||||
END,
|
||||
validation_status = 'valid',
|
||||
validation_error = NULL,
|
||||
validated_at = CURRENT_TIMESTAMP,
|
||||
mention_type = ?,
|
||||
author_name = ?,
|
||||
author_url = ?,
|
||||
author_photo = ?,
|
||||
content_snippet = ?,
|
||||
source_post_url = ?,
|
||||
source_published = ?,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""", (
|
||||
moderation_status,
|
||||
result['mention_type'],
|
||||
result['author_name'],
|
||||
result['author_url'],
|
||||
result['author_photo'],
|
||||
result['content_snippet'],
|
||||
result.get('source_post_url'),
|
||||
result.get('source_published'),
|
||||
webmention_id
|
||||
))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.info(
|
||||
f"Validated webmention: {source_url} -> {target_url} "
|
||||
f"(type={result['mention_type']})"
|
||||
)
|
||||
|
||||
except TransientError as e:
|
||||
# Source unreachable right now: keep a previously valid mention
|
||||
self._mark_validation_failed(webmention_id, source_url, target_url, str(e),
|
||||
transient=True)
|
||||
|
||||
except ValidationError as e:
|
||||
# Validation failed (410/4xx, no link): the mention is removed
|
||||
# from display (spec 3.2.4)
|
||||
self._mark_validation_failed(webmention_id, source_url, target_url, str(e))
|
||||
|
||||
except Exception as e:
|
||||
# Unexpected error - mark as error
|
||||
self._mark_validation_failed(
|
||||
webmention_id,
|
||||
source_url,
|
||||
target_url,
|
||||
f"Unexpected error: {e}"
|
||||
)
|
||||
raise
|
||||
|
||||
def _mark_validation_failed(
|
||||
self,
|
||||
webmention_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
error: str,
|
||||
transient: bool = False
|
||||
):
|
||||
"""
|
||||
Mark webmention validation as failed/invalid
|
||||
|
||||
A transient failure of a mention that was valid before restores it to
|
||||
valid with its previous data, rather than hiding it.
|
||||
|
||||
Args:
|
||||
webmention_id: Database ID
|
||||
source_url: Source URL
|
||||
target_url: Target URL
|
||||
error: Error message
|
||||
"""
|
||||
# Determine if error or invalid
|
||||
# Per background-worker.md: HTTP 4xx and "no link" are invalid
|
||||
# All other errors are 'error' status
|
||||
if transient:
|
||||
status = 'error'
|
||||
elif any(x in error.lower() for x in ['does not link', 'returned 4', 'own syndicated copy']):
|
||||
status = 'invalid'
|
||||
else:
|
||||
status = 'error'
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
if transient:
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET validation_status = 'valid',
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ? AND validated_at IS NOT NULL AND validation_error IS NULL
|
||||
""", (webmention_id,))
|
||||
if cursor.rowcount:
|
||||
conn.commit()
|
||||
conn.close()
|
||||
logger.warning(
|
||||
f"Could not re-check webmention, keeping it: "
|
||||
f"{source_url} -> {target_url}: {error}"
|
||||
)
|
||||
return
|
||||
|
||||
cursor.execute("""
|
||||
UPDATE webmentions
|
||||
SET validation_status = ?,
|
||||
validation_error = ?,
|
||||
validated_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""", (status, error, webmention_id))
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.warning(
|
||||
f"Invalid webmention: {source_url} -> {target_url}: {error}"
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark webmention as failed: {e}")
|
||||
|
||||
def _process_pending_sends(self) -> int:
|
||||
"""
|
||||
Process pending outgoing webmentions (Phase 3)
|
||||
|
||||
Returns:
|
||||
Number of webmentions processed
|
||||
"""
|
||||
processed = 0
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Get pending webmentions ready to send
|
||||
cursor.execute("""
|
||||
SELECT id, source_url, target_url, endpoint_url, retry_count
|
||||
FROM webmention_queue
|
||||
WHERE status = 'pending'
|
||||
AND (next_retry_at IS NULL OR next_retry_at <= datetime('now'))
|
||||
ORDER BY created_at ASC
|
||||
LIMIT ?
|
||||
""", (self.batch_size,))
|
||||
|
||||
pending = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
# Process each webmention
|
||||
for row in pending:
|
||||
try:
|
||||
self._send_webmention(
|
||||
row['id'],
|
||||
row['source_url'],
|
||||
row['target_url'],
|
||||
row['endpoint_url'],
|
||||
row['retry_count']
|
||||
)
|
||||
processed += 1
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to send webmention {row['id']}: {e}",
|
||||
exc_info=True
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching pending sends: {e}")
|
||||
|
||||
return processed
|
||||
|
||||
def _send_webmention(
|
||||
self,
|
||||
queue_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
cached_endpoint: Optional[str],
|
||||
retry_count: int
|
||||
):
|
||||
"""
|
||||
Send a single outgoing webmention
|
||||
|
||||
Args:
|
||||
queue_id: Database ID of queue item
|
||||
source_url: Source URL (our note)
|
||||
target_url: Target URL to notify
|
||||
cached_endpoint: Previously discovered endpoint (if any)
|
||||
retry_count: Current retry count
|
||||
"""
|
||||
# Mark as sending
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'sending',
|
||||
started_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""", (queue_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
try:
|
||||
# Discover endpoint if not cached
|
||||
endpoint = cached_endpoint
|
||||
if not endpoint:
|
||||
endpoint = self._discover_endpoint(target_url)
|
||||
|
||||
if not endpoint:
|
||||
# No endpoint found - permanent failure
|
||||
self._mark_send_no_endpoint(queue_id, source_url, target_url)
|
||||
return
|
||||
|
||||
# Cache the discovered endpoint in database
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET endpoint_url = ?
|
||||
WHERE id = ?
|
||||
""", (endpoint, queue_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# Send webmention POST (spec 3.1.3)
|
||||
try:
|
||||
# SSRF-checked and pinned to the checked address (spec 4.3).
|
||||
# Redirects are not followed: a 301/302 would turn the POST
|
||||
# into a GET without the body
|
||||
response = safe_post(
|
||||
endpoint,
|
||||
data={
|
||||
'source': source_url,
|
||||
'target': target_url
|
||||
},
|
||||
headers={'User-Agent': user_agent(self.site_url)},
|
||||
)
|
||||
|
||||
# Any 2xx is success (spec 3.1.3)
|
||||
if 200 <= response.status_code < 300:
|
||||
self._mark_send_success(queue_id, source_url, target_url)
|
||||
|
||||
elif response.status_code == 429 or response.status_code >= 500:
|
||||
# Rate limited or server error - retry
|
||||
error = f"HTTP {response.status_code}: {response.text[:200]}"
|
||||
self._mark_send_retry(queue_id, source_url, target_url, retry_count, error)
|
||||
|
||||
else:
|
||||
# Permanent failure (3xx, 4xx)
|
||||
error = f"HTTP {response.status_code}: {response.text[:200]}"
|
||||
self._mark_send_failed(queue_id, source_url, target_url, error)
|
||||
|
||||
except (httpx.TimeoutException, httpx.HTTPError) as e:
|
||||
# Connection error - retry
|
||||
self._mark_send_retry(queue_id, source_url, target_url, retry_count, str(e))
|
||||
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
# SSRF blocked - permanent failure
|
||||
logger.warning(f"SSRF blocked endpoint {endpoint}: {e}")
|
||||
self._mark_send_failed(queue_id, source_url, target_url, f"Blocked: {e}")
|
||||
|
||||
except Exception as e:
|
||||
# Discovery failed - retry
|
||||
self._mark_send_retry(queue_id, source_url, target_url, retry_count, str(e))
|
||||
|
||||
def _discover_endpoint(self, target_url: str) -> Optional[str]:
|
||||
"""
|
||||
Discover the webmention endpoint for a target URL (spec 3.1.2)
|
||||
|
||||
- GET the target, following redirects (each hop SSRF-checked)
|
||||
- The first Link header with rel=webmention wins, then the first
|
||||
<link> or <a> with rel=webmention and an href, in document order
|
||||
- rel is a space-separated token list, matched case-insensitively
|
||||
- Relative endpoints resolve against the final URL; an empty href
|
||||
is the target itself; query strings are kept
|
||||
- Not cached: an updated post must re-discover (spec 3.1.4)
|
||||
|
||||
Returns:
|
||||
Endpoint URL, or None if the target has none (or is blocked/4xx)
|
||||
|
||||
Raises:
|
||||
httpx.HTTPError: Network errors and 5xx, so the caller retries
|
||||
"""
|
||||
try:
|
||||
result = safe_get(target_url, headers={'User-Agent': user_agent(self.site_url)})
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
logger.warning(f"SSRF protection blocked target {target_url}: {e}")
|
||||
return None
|
||||
except FetchTooLargeError:
|
||||
logger.info(f"Target too large for discovery: {target_url}")
|
||||
return None
|
||||
|
||||
if result.status_code >= 500:
|
||||
raise httpx.HTTPError(f"Target returned {result.status_code}")
|
||||
if result.status_code >= 400:
|
||||
logger.info(f"Target returned {result.status_code}: {target_url}")
|
||||
return None
|
||||
|
||||
for link_header in result.headers.get_list('Link'):
|
||||
endpoint = self._parse_link_header(link_header, result.url)
|
||||
if endpoint is not None:
|
||||
return endpoint
|
||||
|
||||
content_type = result.headers.get('Content-Type', '')
|
||||
if 'html' in content_type.lower():
|
||||
endpoint = self._parse_html_for_endpoint(result.text, result.url)
|
||||
if endpoint is not None:
|
||||
return endpoint
|
||||
|
||||
logger.info(f"No webmention endpoint found for {target_url}")
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def _has_webmention_rel(rel: str) -> bool:
|
||||
return 'webmention' in rel.lower().split()
|
||||
|
||||
def _parse_link_header(self, link_header: str, base_url: str) -> Optional[str]:
|
||||
"""
|
||||
Parse a Link header (RFC 8288) for a rel=webmention link
|
||||
|
||||
Args:
|
||||
link_header: Link header value (may hold several links)
|
||||
base_url: Base URL for resolving relative URLs
|
||||
|
||||
Returns:
|
||||
Absolute endpoint URL of the first match, None otherwise
|
||||
"""
|
||||
for match in LINK_RE.finditer(link_header):
|
||||
uri, params = match.group(1), match.group(2)
|
||||
for param in LINK_PARAM_RE.finditer(params):
|
||||
if param.group(1).lower() != 'rel':
|
||||
continue
|
||||
rel = param.group(2) if param.group(2) is not None else (param.group(3) or '')
|
||||
if self._has_webmention_rel(rel):
|
||||
return urljoin(base_url, uri.strip())
|
||||
break # only the first rel parameter counts (RFC 8288)
|
||||
|
||||
return None
|
||||
|
||||
def _parse_html_for_endpoint(self, html_content: str, base_url: str) -> Optional[str]:
|
||||
"""
|
||||
Find the first <link> or <a> with rel=webmention, in document order
|
||||
|
||||
Args:
|
||||
html_content: HTML content
|
||||
base_url: Base URL for resolving relative URLs
|
||||
|
||||
Returns:
|
||||
Absolute endpoint URL if found, None otherwise
|
||||
"""
|
||||
try:
|
||||
doc = lxml_html.document_fromstring(html_content)
|
||||
except Exception as e:
|
||||
logger.debug(f"HTML parsing failed: {e}")
|
||||
return None
|
||||
|
||||
for element in doc.iter('link', 'a'):
|
||||
href = element.get('href')
|
||||
if href is not None and self._has_webmention_rel(element.get('rel', '')):
|
||||
return urljoin(base_url, href.strip())
|
||||
|
||||
return None
|
||||
|
||||
def _mark_send_success(self, queue_id: int, source_url: str, target_url: str):
|
||||
"""Mark webmention send as successful"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'sent',
|
||||
sent_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
""", (queue_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.info(f"Sent webmention: {source_url} -> {target_url}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark send success: {e}")
|
||||
|
||||
def _mark_send_failed(self, queue_id: int, source_url: str, target_url: str, error: str):
|
||||
"""Mark webmention send as permanently failed"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'failed',
|
||||
last_error = ?
|
||||
WHERE id = ?
|
||||
""", (error, queue_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.warning(f"Webmention send failed permanently: {source_url} -> {target_url}: {error}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark send failed: {e}")
|
||||
|
||||
def _mark_send_no_endpoint(self, queue_id: int, source_url: str, target_url: str):
|
||||
"""Mark webmention as no endpoint found"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'no_endpoint'
|
||||
WHERE id = ?
|
||||
""", (queue_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
logger.info(f"No webmention endpoint for {target_url}")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark no endpoint: {e}")
|
||||
|
||||
def _mark_send_retry(
|
||||
self,
|
||||
queue_id: int,
|
||||
source_url: str,
|
||||
target_url: str,
|
||||
current_retry_count: int,
|
||||
error: str
|
||||
):
|
||||
"""
|
||||
Mark webmention for retry with exponential backoff
|
||||
|
||||
Per architect's answer to Q5 and background-worker.md:
|
||||
- Increment retry_count AFTER failed attempt
|
||||
- Check retry_count >= 4 AFTER incrementing
|
||||
- Retry schedule: 1h, 6h, 24h
|
||||
"""
|
||||
new_retry_count = current_retry_count + 1
|
||||
|
||||
# Calculate next retry time
|
||||
next_retry_at = None
|
||||
if new_retry_count < 4:
|
||||
delays = {
|
||||
1: timedelta(hours=1),
|
||||
2: timedelta(hours=6),
|
||||
3: timedelta(hours=24),
|
||||
}
|
||||
delay = delays.get(new_retry_count)
|
||||
if delay:
|
||||
next_retry_at = datetime.utcnow() + delay
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
if new_retry_count >= 4:
|
||||
# Max retries reached - mark as failed
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'failed',
|
||||
retry_count = ?,
|
||||
last_error = ?
|
||||
WHERE id = ?
|
||||
""", (new_retry_count, error, queue_id))
|
||||
|
||||
logger.warning(
|
||||
f"Webmention send failed permanently after {new_retry_count} attempts: "
|
||||
f"{source_url} -> {target_url}: {error}"
|
||||
)
|
||||
|
||||
else:
|
||||
# Schedule retry
|
||||
cursor.execute("""
|
||||
UPDATE webmention_queue
|
||||
SET status = 'pending',
|
||||
retry_count = ?,
|
||||
next_retry_at = ?,
|
||||
last_error = ?
|
||||
WHERE id = ?
|
||||
""", (new_retry_count, next_retry_at, error, queue_id))
|
||||
|
||||
logger.warning(
|
||||
f"Webmention send failed, retry {new_retry_count}: "
|
||||
f"{source_url} -> {target_url}: {error}"
|
||||
)
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to mark send retry: {e}")
|
||||
|
||||
def stop(self):
|
||||
"""
|
||||
Stop the worker thread gracefully
|
||||
|
||||
Sets the stop event to signal the thread to exit
|
||||
"""
|
||||
logger.info("Stopping webmention worker")
|
||||
self._stop_event.set()
|
||||
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
"""
|
||||
Get current worker statistics
|
||||
|
||||
Returns:
|
||||
Dict with worker stats for health check
|
||||
"""
|
||||
try:
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Count pending validations
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) FROM webmentions
|
||||
WHERE validation_status = 'pending'
|
||||
""")
|
||||
pending_validations = cursor.fetchone()[0]
|
||||
|
||||
# Count pending sends (Phase 3)
|
||||
cursor.execute("""
|
||||
SELECT COUNT(*) FROM webmention_queue
|
||||
WHERE status = 'pending'
|
||||
""")
|
||||
pending_sends = cursor.fetchone()[0]
|
||||
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'running': self.is_alive(),
|
||||
'last_processed': self._last_processed.isoformat() if self._last_processed else None,
|
||||
'pending_validations': pending_validations,
|
||||
'pending_sends': pending_sends
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get worker stats: {e}")
|
||||
return {
|
||||
'running': self.is_alive(),
|
||||
'error': str(e)
|
||||
}
|
||||
@@ -0,0 +1,443 @@
|
||||
"""
|
||||
WebSub publishing and the built-in hub (v2.10.0, ADR-071)
|
||||
|
||||
Publisher: topic responses advertise the hub and their own URL; a change to a
|
||||
public note clears cached feeds and queues delivery of every affected topic.
|
||||
|
||||
Hub: subscriptions to this site's own topics only. Requests are accepted
|
||||
here (202) and verified, delivered and retried by the WebSub worker.
|
||||
|
||||
References:
|
||||
- W3C WebSub: https://www.w3.org/TR/websub/
|
||||
- ADR-071: WebSub publishing with a built-in hub
|
||||
"""
|
||||
|
||||
import json
|
||||
import secrets
|
||||
from typing import Iterable, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from flask import current_app, request
|
||||
|
||||
|
||||
# Topic endpoints and their fixed paths (tag pages are added per tag)
|
||||
FEED_TOPICS = {
|
||||
"public.feed_atom": "/feed.atom",
|
||||
"public.feed_rss": "/feed.rss",
|
||||
"public.feed_xml_legacy": "/feed.rss", # legacy alias of the RSS topic
|
||||
"public.feed_json": "/feed.json",
|
||||
}
|
||||
PAGE_TOPICS = {"public.index": "/", "public.likes": "/likes"}
|
||||
NEGOTIATED = { # /feed: the topic of whichever format was served
|
||||
"application/atom+xml": "/feed.atom",
|
||||
"application/rss+xml": "/feed.rss",
|
||||
"application/feed+json": "/feed.json",
|
||||
"application/json": "/feed.json",
|
||||
}
|
||||
|
||||
LEASE_DEFAULT = 10 * 24 * 3600
|
||||
LEASE_MIN = 24 * 3600
|
||||
LEASE_MAX = 30 * 24 * 3600
|
||||
MAX_SECRET_BYTES = 200
|
||||
|
||||
|
||||
class SubscriptionError(Exception):
|
||||
"""A subscription request the hub refuses (4xx with a plain-text reason)"""
|
||||
|
||||
def __init__(self, message: str, status: int = 400):
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
|
||||
|
||||
# --- Configuration -----------------------------------------------------------
|
||||
|
||||
def enabled() -> bool:
|
||||
return bool(current_app.config.get("WEBSUB_ENABLED", True))
|
||||
|
||||
|
||||
def external_hub() -> Optional[str]:
|
||||
"""URL of an external hub, if one is configured instead of the built-in one"""
|
||||
return current_app.config.get("WEBSUB_HUB_URL") or None
|
||||
|
||||
|
||||
def hub_url() -> str:
|
||||
# Built from SITE_URL so the worker can use it outside a request
|
||||
return external_hub() or site_url() + "/websub"
|
||||
|
||||
|
||||
def site_url() -> str:
|
||||
return current_app.config["SITE_URL"].rstrip("/")
|
||||
|
||||
|
||||
# --- Topics ------------------------------------------------------------------
|
||||
|
||||
def topic_url(path: str) -> str:
|
||||
return site_url() + path
|
||||
|
||||
|
||||
def tag_topic(tag_name: str) -> str:
|
||||
from urllib.parse import quote
|
||||
return f"{site_url()}/tag/{quote(tag_name, safe='')}"
|
||||
|
||||
|
||||
def topic_for_request(response=None) -> Optional[str]:
|
||||
"""The topic URL the current request serves, or None if it isn't a topic"""
|
||||
endpoint = request.endpoint
|
||||
if endpoint in PAGE_TOPICS:
|
||||
return topic_url(PAGE_TOPICS[endpoint])
|
||||
if endpoint in FEED_TOPICS:
|
||||
return topic_url(FEED_TOPICS[endpoint])
|
||||
if endpoint == "public.feed" and response is not None:
|
||||
mime = (response.mimetype or "").lower()
|
||||
return topic_url(NEGOTIATED[mime]) if mime in NEGOTIATED else None
|
||||
if endpoint == "public.tag":
|
||||
from starpunk.tags import normalize_tag
|
||||
tag = (request.view_args or {}).get("tag", "")
|
||||
return tag_topic(normalize_tag(tag)[0]) if tag else None
|
||||
return None
|
||||
|
||||
|
||||
_UNRESERVED = set("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~")
|
||||
|
||||
|
||||
def decode_unreserved(value: str) -> str:
|
||||
"""
|
||||
Decode percent-encoded unreserved characters (%66 -> f), leaving the rest
|
||||
|
||||
Spec 5.1.1: hubs MUST always decode non-reserved characters in URL
|
||||
parameters, so /%66eed.atom and /feed.atom are the same topic.
|
||||
"""
|
||||
out, i = [], 0
|
||||
while i < len(value):
|
||||
if value[i] == "%" and i + 3 <= len(value):
|
||||
try:
|
||||
char = chr(int(value[i + 1:i + 3], 16))
|
||||
except ValueError:
|
||||
char = None
|
||||
if char is not None and char in _UNRESERVED:
|
||||
out.append(char)
|
||||
i += 3
|
||||
continue
|
||||
out.append(value[i])
|
||||
i += 1
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def canonical_topic(url: str) -> Optional[str]:
|
||||
"""
|
||||
A subscription's hub.topic as one of this site's topics, or None
|
||||
|
||||
Unreserved percent-encodings are decoded and the scheme and host are
|
||||
compared case-insensitively. Tag topics must name an existing tag.
|
||||
"""
|
||||
if not url:
|
||||
return None
|
||||
site = urlparse(site_url())
|
||||
parsed = urlparse(decode_unreserved(url.strip()))
|
||||
if (parsed.scheme.lower(), parsed.netloc.lower()) != (site.scheme, site.netloc.lower()):
|
||||
return None
|
||||
path = parsed.path or "/"
|
||||
if parsed.query or parsed.fragment:
|
||||
return None
|
||||
if path in set(PAGE_TOPICS.values()) | set(FEED_TOPICS.values()):
|
||||
return topic_url(path)
|
||||
if path.startswith("/tag/"):
|
||||
from urllib.parse import unquote
|
||||
from starpunk.tags import get_tag_by_name, normalize_tag
|
||||
name = normalize_tag(unquote(path[len("/tag/"):]))[0]
|
||||
if name and get_tag_by_name(name):
|
||||
return tag_topic(name)
|
||||
return None
|
||||
|
||||
|
||||
def link_header_values(topic: str) -> list[str]:
|
||||
return [f'<{hub_url()}>; rel="hub"', f'<{topic}>; rel="self"']
|
||||
|
||||
|
||||
def add_discovery_headers(response):
|
||||
"""after_request: Link rel=hub and rel=self on topic responses (spec 4)"""
|
||||
if not enabled() or response.status_code not in (200, 304):
|
||||
return response
|
||||
topic = topic_for_request(response)
|
||||
if topic:
|
||||
for value in link_header_values(topic):
|
||||
response.headers.add("Link", value)
|
||||
return response
|
||||
|
||||
|
||||
def template_links() -> Optional[dict]:
|
||||
"""For base.html: {'hub', 'self'} on topic pages"""
|
||||
if not enabled() or not request:
|
||||
return None
|
||||
topic = topic_for_request()
|
||||
return {"hub": hub_url(), "self": topic} if topic else None
|
||||
|
||||
|
||||
def embed_hub(format_name: str, content: str) -> str:
|
||||
"""Add the hub link inside a generated feed, next to its self link"""
|
||||
if not enabled():
|
||||
return content
|
||||
hub = hub_url()
|
||||
if format_name == "atom":
|
||||
marker = '<link rel="self"'
|
||||
return content.replace(marker, f'<link rel="hub" href="{_xml(hub)}"/>\n {marker}', 1)
|
||||
if format_name == "rss":
|
||||
marker = "<atom:link "
|
||||
return content.replace(marker, f'<atom:link href="{_xml(hub)}" rel="hub"/>\n {marker}', 1)
|
||||
if format_name == "json":
|
||||
feed = json.loads(content)
|
||||
feed["hubs"] = [{"type": "WebSub", "url": hub}]
|
||||
return json.dumps(feed, ensure_ascii=False, indent=2)
|
||||
return content
|
||||
|
||||
|
||||
def _xml(value: str) -> str:
|
||||
return (value.replace("&", "&").replace("<", "<")
|
||||
.replace(">", ">").replace('"', """))
|
||||
|
||||
|
||||
# --- Changes -------------------------------------------------------------------
|
||||
|
||||
def get_content_version(db) -> str:
|
||||
row = db.execute("SELECT value FROM site_state WHERE key = 'content_version'").fetchone()
|
||||
return row[0] if row else "0"
|
||||
|
||||
|
||||
def bump_content_version(db) -> None:
|
||||
db.execute(
|
||||
"INSERT INTO site_state (key, value) VALUES ('content_version', '1') "
|
||||
"ON CONFLICT(key) DO UPDATE SET value = CAST(value AS INTEGER) + 1"
|
||||
)
|
||||
|
||||
|
||||
def note_changed(note, was_public: bool = False, old_tags: Iterable[str] = ()) -> None:
|
||||
"""
|
||||
A note was created, edited, deleted, restored or (un)published
|
||||
|
||||
Clears cached feeds in every process and queues delivery of the topics
|
||||
that show it. Changes that are never public (a draft edited) do nothing.
|
||||
|
||||
Args:
|
||||
note: The note (after the change; for a delete, before it)
|
||||
was_public: Whether it was public before the change
|
||||
old_tags: Its tag names before the change
|
||||
"""
|
||||
from starpunk.database import get_db
|
||||
|
||||
is_public = bool(note.published) and not getattr(note, "deleted_at", None)
|
||||
if not (is_public or was_public):
|
||||
return
|
||||
|
||||
db = get_db(current_app)
|
||||
bump_content_version(db)
|
||||
db.commit()
|
||||
|
||||
if not enabled():
|
||||
return
|
||||
|
||||
from starpunk.tags import get_note_tags
|
||||
try:
|
||||
tags = {t["name"] for t in get_note_tags(note.id)}
|
||||
except Exception:
|
||||
tags = set()
|
||||
tags |= set(old_tags)
|
||||
|
||||
# Likes appear on /likes, not on the home page or in the feeds
|
||||
if getattr(note, "like_of", None):
|
||||
topics = [topic_url("/likes")]
|
||||
else:
|
||||
topics = [topic_url(p) for p in ("/", "/feed.atom", "/feed.rss", "/feed.json")]
|
||||
topics += [tag_topic(name) for name in sorted(tags)]
|
||||
queue_topics(db, topics)
|
||||
|
||||
|
||||
def queue_topics(db, topics: Iterable[str]) -> int:
|
||||
"""Queue delivery of topics: to their subscribers, or a ping to an external hub"""
|
||||
topics = list(dict.fromkeys(topics))
|
||||
if not topics:
|
||||
return 0
|
||||
if external_hub():
|
||||
for topic in topics:
|
||||
db.execute("DELETE FROM websub_deliveries WHERE subscription_id IS NULL AND topic = ?", (topic,))
|
||||
db.execute("INSERT INTO websub_deliveries (topic) VALUES (?)", (topic,))
|
||||
db.commit()
|
||||
return len(topics)
|
||||
|
||||
placeholders = ",".join("?" * len(topics))
|
||||
rows = db.execute(
|
||||
f"SELECT id, topic FROM websub_subscriptions WHERE topic IN ({placeholders}) "
|
||||
"AND verified_at IS NOT NULL AND expires_at > datetime('now')",
|
||||
topics,
|
||||
).fetchall()
|
||||
for sub_id, topic in rows:
|
||||
# The newest content replaces anything still waiting (ADR-071 6)
|
||||
db.execute("DELETE FROM websub_deliveries WHERE subscription_id = ?", (sub_id,))
|
||||
db.execute("INSERT INTO websub_deliveries (subscription_id, topic) VALUES (?, ?)", (sub_id, topic))
|
||||
db.commit()
|
||||
return len(rows)
|
||||
|
||||
|
||||
# --- Hub: subscription requests (spec 5.1) -------------------------------------
|
||||
|
||||
MAX_CALLBACK_LENGTH = 2048
|
||||
MAX_PENDING_INTENTS = 200
|
||||
ALLOWED_PORTS = {80, 443} # plus any port from 1024 up
|
||||
|
||||
|
||||
def _clean_callback(callback: str) -> str:
|
||||
"""
|
||||
Check a hub.callback and return it with an ASCII (IDNA) host
|
||||
|
||||
Raises:
|
||||
SubscriptionError: if it is not an acceptable callback URL
|
||||
"""
|
||||
import httpx
|
||||
|
||||
if not callback or len(callback) > MAX_CALLBACK_LENGTH:
|
||||
raise SubscriptionError(f"hub.callback must be a URL of at most {MAX_CALLBACK_LENGTH} characters")
|
||||
if any(ord(c) < 0x20 or ord(c) == 0x7f for c in callback) or " " in callback:
|
||||
raise SubscriptionError("hub.callback contains spaces or control characters")
|
||||
parsed = urlparse(callback)
|
||||
if parsed.scheme.lower() not in ("http", "https") or not parsed.hostname:
|
||||
raise SubscriptionError("hub.callback must be an absolute http(s) URL")
|
||||
try:
|
||||
port = parsed.port
|
||||
except ValueError:
|
||||
raise SubscriptionError("hub.callback has an invalid port")
|
||||
if port is not None and port not in ALLOWED_PORTS and port < 1024:
|
||||
raise SubscriptionError("hub.callback may not use that port")
|
||||
|
||||
try:
|
||||
host = parsed.hostname.encode("idna").decode("ascii")
|
||||
except UnicodeError:
|
||||
raise SubscriptionError("hub.callback has an invalid host name")
|
||||
netloc = host + (f":{port}" if port is not None else "")
|
||||
callback = parsed._replace(scheme=parsed.scheme.lower(), netloc=netloc).geturl()
|
||||
|
||||
if host.lower() == (urlparse(site_url()).hostname or "").lower():
|
||||
raise SubscriptionError("hub.callback may not be on this site")
|
||||
try:
|
||||
httpx.URL(callback)
|
||||
except Exception:
|
||||
raise SubscriptionError("hub.callback is not a valid URL")
|
||||
return callback
|
||||
|
||||
|
||||
def accept_subscription_request(form, db) -> None:
|
||||
"""
|
||||
Validate a subscription request and queue it for verification
|
||||
|
||||
Raises:
|
||||
SubscriptionError: with the status and plain-text reason to return
|
||||
"""
|
||||
from starpunk.webmention_ssrf import SSRFBlockedError, validate_url_for_fetch
|
||||
|
||||
mode = (form.get("hub.mode") or "").strip()
|
||||
if mode not in ("subscribe", "unsubscribe"):
|
||||
raise SubscriptionError("hub.mode must be subscribe or unsubscribe")
|
||||
if mode == "subscribe" and external_hub():
|
||||
# Unsubscribes are still taken, so old subscribers can leave
|
||||
raise SubscriptionError("This site uses another hub: " + external_hub(), 400)
|
||||
|
||||
callback = _clean_callback((form.get("hub.callback") or "").strip())
|
||||
try:
|
||||
callback_ip = validate_url_for_fetch(callback)
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
raise SubscriptionError(f"hub.callback is not reachable: {e}")
|
||||
|
||||
requested_topic = (form.get("hub.topic") or "").strip()
|
||||
topic = canonical_topic(requested_topic)
|
||||
if not topic:
|
||||
raise SubscriptionError("hub.topic is not a topic of this site; use the rel=self URL")
|
||||
|
||||
secret, lease = None, None
|
||||
if mode == "subscribe":
|
||||
# hub.secret and hub.lease_seconds MUST be ignored for unsubscriptions
|
||||
secret = form.get("hub.secret")
|
||||
if secret is not None:
|
||||
if secret == "":
|
||||
raise SubscriptionError("hub.secret may not be empty")
|
||||
if len(secret.encode("utf-8")) >= MAX_SECRET_BYTES:
|
||||
raise SubscriptionError("hub.secret must be less than 200 bytes")
|
||||
lease = LEASE_DEFAULT
|
||||
if form.get("hub.lease_seconds"):
|
||||
try:
|
||||
lease = int(form["hub.lease_seconds"])
|
||||
except ValueError:
|
||||
raise SubscriptionError("hub.lease_seconds must be a number")
|
||||
lease = max(LEASE_MIN, min(lease, LEASE_MAX))
|
||||
_check_limits(db, topic, callback, callback_ip)
|
||||
|
||||
# Each request gets its own id; the latest replaces any pending one
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO websub_intents
|
||||
(topic, callback, mode, secret, lease_seconds, request_id, requested_topic, callback_ip)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(topic, callback) DO UPDATE SET
|
||||
mode = excluded.mode, secret = excluded.secret,
|
||||
lease_seconds = excluded.lease_seconds, request_id = excluded.request_id,
|
||||
requested_topic = excluded.requested_topic, callback_ip = excluded.callback_ip,
|
||||
attempts = 0, next_attempt_at = CURRENT_TIMESTAMP, created_at = CURRENT_TIMESTAMP
|
||||
""",
|
||||
(topic, callback, mode, secret, lease, secrets.token_hex(16), requested_topic, callback_ip),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
|
||||
def _check_limits(db, topic: str, callback: str, callback_ip: str) -> None:
|
||||
"""
|
||||
Subscription limits, counting pending requests too (ADR-071 4)
|
||||
|
||||
Per callback address rather than host name, so many subdomains on one
|
||||
server share a limit. Renewing an existing subscription is always fine.
|
||||
"""
|
||||
if db.execute("SELECT 1 FROM websub_subscriptions WHERE topic = ? AND callback = ?",
|
||||
(topic, callback)).fetchone():
|
||||
return
|
||||
|
||||
pending = db.execute("SELECT COUNT(*) FROM websub_intents").fetchone()[0]
|
||||
if pending >= MAX_PENDING_INTENTS:
|
||||
raise SubscriptionError("Too many subscription requests waiting; try again later", 429)
|
||||
|
||||
total = db.execute(
|
||||
"""
|
||||
SELECT COUNT(*) FROM (
|
||||
SELECT topic, callback FROM websub_subscriptions
|
||||
UNION
|
||||
SELECT topic, callback FROM websub_intents WHERE mode = 'subscribe'
|
||||
)
|
||||
"""
|
||||
).fetchone()[0]
|
||||
if total >= current_app.config.get("WEBSUB_MAX_SUBSCRIPTIONS", 1000):
|
||||
raise SubscriptionError("This hub has reached its subscription limit", 429)
|
||||
|
||||
per_address = db.execute(
|
||||
"""
|
||||
SELECT COUNT(*) FROM (
|
||||
SELECT topic, callback FROM websub_subscriptions WHERE callback_ip = ?
|
||||
UNION
|
||||
SELECT topic, callback FROM websub_intents WHERE mode = 'subscribe' AND callback_ip = ?
|
||||
)
|
||||
""",
|
||||
(callback_ip, callback_ip),
|
||||
).fetchone()[0]
|
||||
if per_address >= current_app.config.get("WEBSUB_MAX_PER_HOST", 25):
|
||||
raise SubscriptionError("Too many subscriptions for that callback's server", 429)
|
||||
|
||||
|
||||
def stats(db) -> dict:
|
||||
"""Active subscribers per topic and deliveries being retried (admin)"""
|
||||
by_topic = db.execute(
|
||||
"SELECT topic, COUNT(*) FROM websub_subscriptions WHERE expires_at > datetime('now') "
|
||||
"GROUP BY topic ORDER BY COUNT(*) DESC, topic"
|
||||
).fetchall()
|
||||
retrying = db.execute(
|
||||
"SELECT COUNT(*) FROM websub_deliveries WHERE attempts > 0"
|
||||
).fetchone()[0]
|
||||
return {"by_topic": [(t, n) for t, n in by_topic], "retrying": retrying,
|
||||
"external": external_hub()}
|
||||
|
||||
|
||||
def new_challenge() -> str:
|
||||
return secrets.token_urlsafe(32)
|
||||
@@ -0,0 +1,362 @@
|
||||
"""
|
||||
WebSub worker: verification of intent and content distribution (v2.10.0)
|
||||
|
||||
Runs like the Webmention worker: a daemon thread in every gunicorn process,
|
||||
but only the process holding the file lock works the queues.
|
||||
|
||||
- Intents (spec 5.3): GET the callback with a challenge; a 2xx echoing it
|
||||
verifies. Anything else (including redirects) leaves the subscription as
|
||||
it was. Network errors, DNS failures and 5xx are retried a few times.
|
||||
- Deliveries (spec 7): render the topic in-app and POST it with
|
||||
Link rel=hub/self and, if there is a secret, X-Hub-Signature (sha256).
|
||||
Failures are retried; the subscription stays until its lease ends.
|
||||
- External hub: a delivery without a subscription is a publish ping.
|
||||
|
||||
v2.10.1: every queue item is handled on its own, so one bad item can't stop
|
||||
the worker; all outbound requests have a hard time limit.
|
||||
|
||||
References:
|
||||
- https://www.w3.org/TR/websub/
|
||||
- ADR-071
|
||||
"""
|
||||
|
||||
import fcntl
|
||||
import hashlib
|
||||
import hmac
|
||||
import logging
|
||||
import threading
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
from urllib.parse import urlencode, urlparse, urlsplit, urlunsplit
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DELIVERY_BACKOFF = [60, 300, 1800, 7200, 21600] # then give up on this delivery
|
||||
INTENT_BACKOFF = [60, 300, 900]
|
||||
PRUNE_EVERY = timedelta(hours=1)
|
||||
BATCH = 10
|
||||
VERIFY_MAX_BYTES = 4096 # a challenge reply is short; larger means not verified
|
||||
|
||||
|
||||
def callback_with_params(callback: str, params: dict) -> str:
|
||||
"""Append parameters to the callback, keeping its query string (spec 5.3)"""
|
||||
parts = urlsplit(callback)
|
||||
query = parts.query + ("&" if parts.query else "") + urlencode(params)
|
||||
return urlunsplit((parts.scheme, parts.netloc, parts.path, query, parts.fragment))
|
||||
|
||||
|
||||
def signature(secret: str, body: bytes) -> str:
|
||||
return "sha256=" + hmac.new(secret.encode("utf-8"), body, hashlib.sha256).hexdigest()
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
def _later(seconds: int) -> str:
|
||||
return (datetime.utcnow() + timedelta(seconds=seconds)).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
|
||||
class WebSubWorker(threading.Thread):
|
||||
"""Daemon thread that verifies subscriptions and delivers content"""
|
||||
|
||||
def __init__(self, app, poll_interval: int = 5):
|
||||
super().__init__(daemon=True, name="websub-worker")
|
||||
self.app = app
|
||||
self.poll_interval = poll_interval
|
||||
self._stop_event = threading.Event()
|
||||
self._lock_file = None
|
||||
self._last_prune = None
|
||||
|
||||
# --- thread plumbing ----------------------------------------------------
|
||||
|
||||
def run(self):
|
||||
logger.info("WebSub worker started")
|
||||
while not self._stop_event.is_set():
|
||||
if not self._acquire_lock():
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
continue
|
||||
try:
|
||||
with self.app.app_context():
|
||||
done = self.process_once()
|
||||
except Exception as e:
|
||||
logger.error(f"WebSub worker error: {e}", exc_info=True)
|
||||
done = 0
|
||||
if not done:
|
||||
self._stop_event.wait(self.poll_interval)
|
||||
self._release_lock()
|
||||
|
||||
def stop(self):
|
||||
self._stop_event.set()
|
||||
|
||||
def _acquire_lock(self) -> bool:
|
||||
if self._lock_file:
|
||||
return True
|
||||
path = Path(self.app.config["DATABASE_PATH"]).parent / "websub-worker.lock"
|
||||
lock_file = open(path, "w")
|
||||
try:
|
||||
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError:
|
||||
lock_file.close()
|
||||
return False
|
||||
self._lock_file = lock_file
|
||||
logger.info("WebSub worker holds the queue lock")
|
||||
return True
|
||||
|
||||
def _release_lock(self):
|
||||
if self._lock_file:
|
||||
self._lock_file.close()
|
||||
self._lock_file = None
|
||||
|
||||
# --- one pass (also called directly by tests) ---------------------------
|
||||
|
||||
def process_once(self) -> int:
|
||||
from starpunk.database import get_db
|
||||
|
||||
db = get_db(self.app)
|
||||
done = 0
|
||||
rendered = {} # topic -> (status, content type, body), once per pass
|
||||
|
||||
for row in db.execute(
|
||||
"SELECT * FROM websub_intents WHERE next_attempt_at <= ? ORDER BY next_attempt_at, id LIMIT ?",
|
||||
(_now(), BATCH),
|
||||
).fetchall():
|
||||
self._guarded(db, row, "verification", self.verify, self._intent_crashed)
|
||||
done += 1
|
||||
for row in db.execute(
|
||||
"SELECT * FROM websub_deliveries WHERE next_attempt_at <= ? ORDER BY next_attempt_at, id LIMIT ?",
|
||||
(_now(), BATCH),
|
||||
).fetchall():
|
||||
self._guarded(db, row, "delivery", lambda d, r: self.deliver(d, r, rendered), self._retry_or_drop)
|
||||
done += 1
|
||||
if self._last_prune is None or datetime.utcnow() - self._last_prune > PRUNE_EVERY:
|
||||
self.prune(db)
|
||||
return done
|
||||
|
||||
def _guarded(self, db, row, what, handler, on_crash) -> None:
|
||||
"""Handle one queue item; an unexpected error affects only that item"""
|
||||
try:
|
||||
handler(db, row)
|
||||
except Exception as e:
|
||||
logger.error(f"WebSub {what} {row['id']} failed unexpectedly: {e}", exc_info=True)
|
||||
try:
|
||||
db.rollback()
|
||||
on_crash(db, row, f"{type(e).__name__}: {e}"[:200])
|
||||
db.commit()
|
||||
except Exception as cleanup:
|
||||
logger.error(f"WebSub could not record the failure: {cleanup}")
|
||||
|
||||
def _intent_crashed(self, db, intent, error) -> None:
|
||||
db.execute("DELETE FROM websub_intents WHERE id = ? AND request_id IS ?",
|
||||
(intent["id"], intent["request_id"]))
|
||||
|
||||
# --- verification of intent (spec 5.3) ----------------------------------
|
||||
|
||||
def verify(self, db, intent) -> None:
|
||||
from starpunk.websub import new_challenge
|
||||
from starpunk.webmention_ssrf import (
|
||||
FetchTooLargeError, ResolutionError, SSRFBlockedError, safe_get, user_agent,
|
||||
)
|
||||
|
||||
challenge = new_challenge()
|
||||
# The topic exactly as the subscriber sent it (spec 5.3)
|
||||
params = {"hub.mode": intent["mode"],
|
||||
"hub.topic": intent["requested_topic"] or intent["topic"],
|
||||
"hub.challenge": challenge}
|
||||
if intent["mode"] == "subscribe":
|
||||
params["hub.lease_seconds"] = intent["lease_seconds"]
|
||||
url = callback_with_params(intent["callback"], params)
|
||||
|
||||
retry = False
|
||||
try:
|
||||
result = safe_get(url, headers={"User-Agent": user_agent(self.app.config["SITE_URL"])},
|
||||
max_redirects=0, max_bytes=VERIFY_MAX_BYTES)
|
||||
# Exact match; a trailing newline from a framework is tolerated
|
||||
verified = 200 <= result.status_code < 300 and result.text.rstrip("\r\n") == challenge
|
||||
retry = result.status_code >= 500
|
||||
error = None if verified else f"HTTP {result.status_code}" + (
|
||||
"" if result.status_code >= 300 else ": challenge not echoed")
|
||||
except ResolutionError as e:
|
||||
verified, retry, error = False, True, str(e)
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
verified, error = False, f"Blocked: {e}"
|
||||
except FetchTooLargeError:
|
||||
verified, error = False, "Reply too large to be a challenge"
|
||||
except httpx.TooManyRedirects:
|
||||
verified, error = False, "Redirected (not allowed)"
|
||||
except httpx.HTTPError as e:
|
||||
verified, retry, error = False, True, (str(e) or type(e).__name__)[:200]
|
||||
|
||||
# Only act if no newer request replaced this one meanwhile
|
||||
current = db.execute("SELECT request_id FROM websub_intents WHERE id = ?", (intent["id"],)).fetchone()
|
||||
if current is None or current[0] != intent["request_id"]:
|
||||
return
|
||||
|
||||
if verified:
|
||||
if intent["mode"] == "subscribe":
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO websub_subscriptions
|
||||
(topic, callback, secret, lease_seconds, expires_at, verified_at, callback_ip)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(topic, callback) DO UPDATE SET
|
||||
secret = excluded.secret, lease_seconds = excluded.lease_seconds,
|
||||
expires_at = excluded.expires_at, verified_at = excluded.verified_at,
|
||||
callback_ip = excluded.callback_ip, last_error = NULL
|
||||
""",
|
||||
(intent["topic"], intent["callback"], intent["secret"], intent["lease_seconds"],
|
||||
_later(intent["lease_seconds"]), _now(), intent["callback_ip"]),
|
||||
)
|
||||
else:
|
||||
db.execute("DELETE FROM websub_subscriptions WHERE topic = ? AND callback = ?",
|
||||
(intent["topic"], intent["callback"]))
|
||||
db.execute("DELETE FROM websub_intents WHERE id = ? AND request_id IS ?",
|
||||
(intent["id"], intent["request_id"]))
|
||||
logger.info(f"WebSub {intent['mode']} verified: {intent['callback']} -> {intent['topic']}")
|
||||
elif retry and intent["attempts"] < len(INTENT_BACKOFF):
|
||||
db.execute(
|
||||
"UPDATE websub_intents SET attempts = attempts + 1, next_attempt_at = ? "
|
||||
"WHERE id = ? AND request_id IS ?",
|
||||
(_later(INTENT_BACKOFF[intent["attempts"]]), intent["id"], intent["request_id"]),
|
||||
)
|
||||
else:
|
||||
# Verification failed: the subscription state stays as it was
|
||||
db.execute("DELETE FROM websub_intents WHERE id = ? AND request_id IS ?",
|
||||
(intent["id"], intent["request_id"]))
|
||||
logger.info(f"WebSub {intent['mode']} not verified for {intent['callback']}: {error}")
|
||||
db.commit()
|
||||
|
||||
# --- content distribution (spec 7) ---------------------------------------
|
||||
|
||||
def render_topic(self, topic: str):
|
||||
"""(status, content_type, body) of a topic, rendered in-app"""
|
||||
path = urlparse(topic).path or "/"
|
||||
response = self.app.test_client().get(path, headers={"Accept": "*/*"})
|
||||
return response.status_code, response.headers.get("Content-Type", ""), response.get_data()
|
||||
|
||||
def deliver(self, db, delivery, rendered: Optional[dict] = None) -> None:
|
||||
from starpunk.websub import hub_url
|
||||
from starpunk.webmention_ssrf import ResolutionError, SSRFBlockedError, safe_post, user_agent
|
||||
|
||||
rendered = {} if rendered is None else rendered
|
||||
site = self.app.config["SITE_URL"]
|
||||
sub = None
|
||||
if delivery["subscription_id"] is None:
|
||||
# External hub: ping it (hub.url is the de facto parameter)
|
||||
target, kwargs = hub_url(), {
|
||||
"data": {"hub.mode": "publish", "hub.url": delivery["topic"], "hub.topic": delivery["topic"]},
|
||||
"headers": {"User-Agent": user_agent(site)},
|
||||
}
|
||||
else:
|
||||
sub = db.execute(
|
||||
"SELECT * FROM websub_subscriptions WHERE id = ? AND expires_at > ?",
|
||||
(delivery["subscription_id"], _now()),
|
||||
).fetchone()
|
||||
if sub is None: # unsubscribed or expired meanwhile
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
db.commit()
|
||||
return
|
||||
if sub["topic"] not in rendered:
|
||||
rendered[sub["topic"]] = self.render_topic(sub["topic"])
|
||||
status, content_type, body = rendered[sub["topic"]]
|
||||
if status in (404, 410):
|
||||
# The topic is gone: end the subscription and say so (spec 5.2)
|
||||
self._deny(sub, "The topic no longer exists")
|
||||
db.execute("DELETE FROM websub_subscriptions WHERE id = ?", (sub["id"],))
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
db.commit()
|
||||
return
|
||||
if status != 200:
|
||||
self._retry_or_drop(db, delivery, f"Topic rendered HTTP {status}")
|
||||
db.commit()
|
||||
return
|
||||
headers = {
|
||||
"Content-Type": content_type,
|
||||
"Link": f'<{hub_url()}>; rel="hub", <{sub["topic"]}>; rel="self"',
|
||||
"User-Agent": user_agent(site),
|
||||
}
|
||||
if sub["secret"] is not None:
|
||||
headers["X-Hub-Signature"] = signature(sub["secret"], body)
|
||||
target, kwargs = sub["callback"], {"content": body, "headers": headers}
|
||||
|
||||
permanent = False
|
||||
try:
|
||||
result = safe_post(target, **kwargs)
|
||||
status, error = result.status_code, f"HTTP {result.status_code}"
|
||||
except ResolutionError as e:
|
||||
status, error = None, str(e) # DNS can recover: retry
|
||||
except (SSRFBlockedError, ValueError) as e:
|
||||
status, error, permanent = None, f"Blocked: {e}", True
|
||||
except httpx.HTTPError as e:
|
||||
status, error = None, (str(e) or type(e).__name__)[:200]
|
||||
|
||||
succeeded = status is not None and 200 <= status < 300
|
||||
if succeeded:
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
if sub is not None:
|
||||
db.execute("UPDATE websub_subscriptions SET last_success_at = ?, last_error = NULL WHERE id = ?",
|
||||
(_now(), sub["id"]))
|
||||
elif sub is not None and status == 410:
|
||||
db.execute("DELETE FROM websub_subscriptions WHERE id = ?", (sub["id"],))
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
logger.info(f"WebSub subscriber gone (410), removed: {sub['callback']}")
|
||||
else:
|
||||
if permanent:
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
logger.warning(f"WebSub delivery of {delivery['topic']} refused: {error}")
|
||||
else:
|
||||
self._retry_or_drop(db, delivery, error)
|
||||
if sub is not None:
|
||||
db.execute("UPDATE websub_subscriptions SET last_error = ? WHERE id = ?", (error, sub["id"]))
|
||||
db.commit()
|
||||
|
||||
def _retry_or_drop(self, db, delivery, error: str) -> None:
|
||||
"""
|
||||
Retry a failed delivery with backoff, then give up on it
|
||||
|
||||
The subscription itself stays active until its lease ends (spec 7);
|
||||
the next change is delivered again.
|
||||
"""
|
||||
if delivery["attempts"] < len(DELIVERY_BACKOFF):
|
||||
db.execute(
|
||||
"UPDATE websub_deliveries SET attempts = attempts + 1, next_attempt_at = ?, last_error = ? WHERE id = ?",
|
||||
(_later(DELIVERY_BACKOFF[delivery["attempts"]]), error, delivery["id"]),
|
||||
)
|
||||
else:
|
||||
db.execute("DELETE FROM websub_deliveries WHERE id = ?", (delivery["id"],))
|
||||
logger.warning(f"WebSub delivery of {delivery['topic']} gave up: {error}")
|
||||
|
||||
def _deny(self, sub, reason: str) -> None:
|
||||
"""Tell a subscriber its subscription has ended (hub.mode=denied), best effort"""
|
||||
from starpunk.webmention_ssrf import safe_get, user_agent
|
||||
url = callback_with_params(sub["callback"], {"hub.mode": "denied", "hub.topic": sub["topic"],
|
||||
"hub.reason": reason})
|
||||
try:
|
||||
safe_get(url, headers={"User-Agent": user_agent(self.app.config["SITE_URL"])},
|
||||
max_redirects=0, max_bytes=VERIFY_MAX_BYTES)
|
||||
except Exception as e:
|
||||
logger.info(f"WebSub denial to {sub['callback']} not delivered: {e}")
|
||||
|
||||
# --- housekeeping -------------------------------------------------------
|
||||
|
||||
def prune(self, db) -> int:
|
||||
"""Remove subscriptions whose lease has run out, and their deliveries"""
|
||||
cursor = db.execute("DELETE FROM websub_subscriptions WHERE expires_at <= ?", (_now(),))
|
||||
db.execute("DELETE FROM websub_deliveries WHERE subscription_id IS NOT NULL AND "
|
||||
"subscription_id NOT IN (SELECT id FROM websub_subscriptions)")
|
||||
db.commit()
|
||||
self._last_prune = datetime.utcnow()
|
||||
if cursor.rowcount:
|
||||
logger.info(f"WebSub: removed {cursor.rowcount} expired subscriptions")
|
||||
return cursor.rowcount
|
||||
|
||||
|
||||
def start_worker(app) -> Optional[WebSubWorker]:
|
||||
if not app.config.get("WEBSUB_ENABLED", True) or app.config.get("TESTING", False):
|
||||
return None
|
||||
worker = WebSubWorker(app, poll_interval=app.config.get("WEBSUB_POLL_INTERVAL", 5))
|
||||
worker.start()
|
||||
return worker
|
||||
+376
-26
@@ -1,36 +1,75 @@
|
||||
/* StarPunk CSS - Minimal responsive stylesheet */
|
||||
|
||||
:root {
|
||||
--color-text: #333; --color-text-light: #666; --color-bg: #fff; --color-bg-alt: #f5f5f5;
|
||||
--color-link: #0066cc; --color-link-hover: #004499; --color-border: #ddd;
|
||||
--color-text: #1f2328; --color-text-light: #59636e; --color-bg: #fff; --color-bg-alt: #f4f5f7;
|
||||
--color-link: #0a5cc2; --color-link-hover: #083f85; --color-border: #e3e6ea; --color-accent-soft: #e8f0fb;
|
||||
--color-success: #28a745; --color-error: #dc3545; --color-warning: #ffc107; --color-info: #17a2b8;
|
||||
--font-body: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
|
||||
--font-mono: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
|
||||
--font-body: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
--font-reading: Charter, 'Bitstream Charter', 'Sitka Text', Cambria, 'Noto Serif', Georgia, serif;
|
||||
--font-mono: ui-monospace, 'SF Mono', Monaco, Consolas, 'Liberation Mono', monospace;
|
||||
--spacing-xs: 0.25rem; --spacing-sm: 0.5rem; --spacing-md: 1rem; --spacing-lg: 2rem; --spacing-xl: 4rem;
|
||||
--max-width: 42rem; --border-radius: 4px;
|
||||
--max-width: 40rem; --border-radius: 6px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
--color-text: #e4e6e9; --color-text-light: #9aa3ad; --color-bg: #121417; --color-bg-alt: #1c1f24;
|
||||
--color-link: #74b0ff; --color-link-hover: #a8ceff; --color-border: #2b3038; --color-accent-soft: #1a2738;
|
||||
}
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body { font-family: var(--font-body); font-size: 1rem; line-height: 1.6; color: var(--color-text); background: var(--color-bg); padding: var(--spacing-md); }
|
||||
body {
|
||||
font-family: var(--font-body); font-size: 1rem; line-height: 1.6; color: var(--color-text); background: var(--color-bg);
|
||||
padding: var(--spacing-md); min-height: 100vh; display: flex; flex-direction: column;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
|
||||
h1, h2, h3 { margin-bottom: var(--spacing-md); line-height: 1.2; font-weight: 600; }
|
||||
h1 { font-size: 2rem; } h2 { font-size: 1.5rem; } h3 { font-size: 1.25rem; }
|
||||
h1, h2, h3 { margin-bottom: var(--spacing-md); line-height: 1.25; font-weight: 650; letter-spacing: -0.01em; }
|
||||
h1 { font-size: 1.75rem; } h2 { font-size: 1.375rem; } h3 { font-size: 1.125rem; }
|
||||
p { margin-bottom: var(--spacing-md); }
|
||||
a { color: var(--color-link); text-decoration: none; }
|
||||
a:hover { color: var(--color-link-hover); text-decoration: underline; }
|
||||
:focus-visible { outline: 2px solid var(--color-link); outline-offset: 2px; border-radius: 2px; }
|
||||
|
||||
code, pre { font-family: var(--font-mono); background: var(--color-bg-alt); padding: 0.125rem 0.25rem; border-radius: var(--border-radius); font-size: 0.875rem; }
|
||||
code, pre { font-family: var(--font-mono); background: var(--color-bg-alt); padding: 0.125rem 0.3rem; border-radius: var(--border-radius); font-size: 0.875em; }
|
||||
pre { padding: var(--spacing-md); overflow-x: auto; margin-bottom: var(--spacing-md); }
|
||||
pre code { padding: 0; background: none; }
|
||||
|
||||
header, main, footer { max-width: var(--max-width); margin: 0 auto; }
|
||||
header { margin-bottom: var(--spacing-lg); padding-bottom: var(--spacing-md); border-bottom: 2px solid var(--color-border); }
|
||||
header h1 { margin-bottom: var(--spacing-sm); }
|
||||
header h1 a { color: var(--color-text); text-decoration: none; }
|
||||
nav { display: flex; gap: var(--spacing-md); flex-wrap: wrap; }
|
||||
nav a { padding: var(--spacing-sm) var(--spacing-md); border-radius: var(--border-radius); transition: background 0.2s; }
|
||||
nav a:hover { background: var(--color-bg-alt); text-decoration: none; }
|
||||
main { min-height: 60vh; margin-bottom: var(--spacing-lg); }
|
||||
footer { padding-top: var(--spacing-lg); border-top: 1px solid var(--color-border); text-align: center; color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
|
||||
|
||||
/* Page frame */
|
||||
.site-header, main, .site-footer { width: 100%; max-width: var(--max-width); margin-left: auto; margin-right: auto; }
|
||||
main { flex: 1; margin-bottom: var(--spacing-xl); }
|
||||
|
||||
.site-header {
|
||||
display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: var(--spacing-sm) var(--spacing-lg);
|
||||
padding: var(--spacing-md) 0 var(--spacing-md); margin-bottom: var(--spacing-lg); border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
.site-title { font-size: 1.375rem; font-weight: 700; letter-spacing: -0.02em; color: var(--color-text); }
|
||||
.site-title:hover { color: var(--color-link); text-decoration: none; }
|
||||
.site-nav { display: flex; align-items: center; flex-wrap: wrap; gap: var(--spacing-md); font-size: 0.9375rem; }
|
||||
.site-nav > a { color: var(--color-text-light); }
|
||||
.site-nav > a:hover { color: var(--color-link); text-decoration: none; }
|
||||
.site-search { display: flex; }
|
||||
.site-search input[type="search"] {
|
||||
width: 10rem; padding: 0.3rem 0.6rem; font: inherit; font-size: 0.875rem; color: var(--color-text);
|
||||
background: var(--color-bg-alt); border: 1px solid var(--color-border); border-right: 0; border-radius: var(--border-radius) 0 0 var(--border-radius);
|
||||
}
|
||||
.site-search button {
|
||||
padding: 0.3rem 0.6rem; font: inherit; font-size: 0.875rem; cursor: pointer; color: var(--color-text-light);
|
||||
background: var(--color-bg-alt); border: 1px solid var(--color-border); border-radius: 0 var(--border-radius) var(--border-radius) 0;
|
||||
}
|
||||
.site-search button:hover { color: var(--color-link); }
|
||||
|
||||
.site-footer { padding: var(--spacing-md) 0; border-top: 1px solid var(--color-border); color: var(--color-text-light); font-size: 0.8125rem; }
|
||||
.site-footer p { margin: 0; }
|
||||
.site-footer a { color: var(--color-text-light); text-decoration: underline; text-decoration-color: var(--color-border); }
|
||||
.site-footer a:hover { color: var(--color-link); }
|
||||
|
||||
.page-title { font-size: 1.375rem; margin-bottom: var(--spacing-lg); }
|
||||
.tag-name { color: var(--color-link); }
|
||||
.tag-name::before { content: "#"; }
|
||||
|
||||
.dev-mode-warning { background: var(--color-error); color: white; padding: var(--spacing-md); text-align: center; font-weight: bold; margin: calc(-1 * var(--spacing-md)); margin-bottom: var(--spacing-lg); }
|
||||
|
||||
@@ -40,17 +79,46 @@ footer { padding-top: var(--spacing-lg); border-top: 1px solid var(--color-borde
|
||||
.flash-warning { background: #fff3cd; border-color: var(--color-warning); color: #856404; }
|
||||
.flash-info { background: #d1ecf1; border-color: var(--color-info); color: #0c5460; }
|
||||
|
||||
/* Notes */
|
||||
.h-entry { margin-bottom: var(--spacing-lg); padding-bottom: var(--spacing-lg); border-bottom: 1px solid var(--color-border); }
|
||||
.h-entry:last-child { border-bottom: none; }
|
||||
.note-preview .e-content { margin-bottom: var(--spacing-md); }
|
||||
.note-meta { color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.h-entry > .p-name a { color: var(--color-text); }
|
||||
.h-entry > .p-name a:hover { color: var(--color-link); }
|
||||
.e-content, .p-summary { font-family: var(--font-reading); font-size: 1.125rem; line-height: 1.65; overflow-wrap: anywhere; }
|
||||
.e-content > :last-child, .p-summary > :last-child { margin-bottom: 0; }
|
||||
.e-content a { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; text-decoration-color: color-mix(in srgb, currentColor 40%, transparent); }
|
||||
.e-content a:hover { text-decoration-color: currentColor; }
|
||||
.e-content blockquote { margin: 0 0 var(--spacing-md); padding-left: var(--spacing-md); border-left: 3px solid var(--color-border); color: var(--color-text-light); }
|
||||
.e-content ul, .e-content ol { margin: 0 0 var(--spacing-md) 1.25rem; }
|
||||
.e-content img { max-width: 100%; height: auto; }
|
||||
.e-content h1, .e-content h2, .e-content h3 { font-family: var(--font-body); }
|
||||
.reaction { margin-bottom: var(--spacing-sm); font-size: 0.9375rem; color: var(--color-text-light); overflow-wrap: anywhere; }
|
||||
.reaction-verb { font-weight: 600; margin-right: 0.25rem; }
|
||||
.reaction-like .reaction-verb { color: #c2410c; }
|
||||
.reaction-repost .reaction-verb { color: #15803d; }
|
||||
.reaction a { color: var(--color-text); font-weight: 500; }
|
||||
.reaction a:hover { color: var(--color-link); }
|
||||
.reaction-by { margin-left: 0.25rem; }
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.reaction-like .reaction-verb { color: #fb923c; }
|
||||
.reaction-repost .reaction-verb { color: #4ade80; }
|
||||
}
|
||||
.read-more { margin: var(--spacing-sm) 0 0; font-size: 0.9375rem; }
|
||||
|
||||
.note-meta { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 0.4rem; margin-top: var(--spacing-sm); color: var(--color-text-light); font-size: 0.8125rem; }
|
||||
.note-meta a { color: var(--color-text-light); }
|
||||
.note-nav { margin-top: var(--spacing-md); }
|
||||
.note-meta a:hover { color: var(--color-link); }
|
||||
.note-meta > :not(.visually-hidden) + :not(.visually-hidden)::before { content: "·"; margin-right: 0.4rem; color: var(--color-text-light); }
|
||||
.note-tags { display: inline-flex; flex-wrap: wrap; gap: 0 0.4rem; }
|
||||
.note-meta > .note-tags::before { margin-right: 0; }
|
||||
.note-tags .p-category::before { content: "#"; opacity: 0.7; }
|
||||
.h-entry:not(.note-preview) .note-meta { margin-top: var(--spacing-md); }
|
||||
.note-nav { margin-top: var(--spacing-lg); font-size: 0.9375rem; }
|
||||
.empty-state { text-align: center; padding: var(--spacing-xl); color: var(--color-text-light); }
|
||||
|
||||
.form-group { margin-bottom: var(--spacing-md); }
|
||||
label { display: block; margin-bottom: var(--spacing-sm); font-weight: 600; }
|
||||
input[type="text"], input[type="url"], input[type="email"], textarea { width: 100%; padding: var(--spacing-sm); border: 1px solid var(--color-border); border-radius: var(--border-radius); font-family: inherit; font-size: 1rem; }
|
||||
input[type="text"], input[type="url"], input[type="email"], input[type="search"], textarea { color: var(--color-text); background: var(--color-bg); width: 100%; padding: var(--spacing-sm); border: 1px solid var(--color-border); border-radius: var(--border-radius); font-family: inherit; font-size: 1rem; }
|
||||
textarea { font-family: var(--font-mono); resize: vertical; min-height: 10rem; }
|
||||
small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-light); font-size: 0.875rem; }
|
||||
.form-checkbox { display: flex; align-items: center; gap: var(--spacing-sm); }
|
||||
@@ -68,7 +136,8 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
.button-warning { background: var(--color-warning); color: #333; border-color: var(--color-warning); }
|
||||
.button-small { padding: 0.25rem 0.5rem; font-size: 0.875rem; }
|
||||
|
||||
.admin-container { max-width: 60rem; }
|
||||
.admin-page { --max-width: 60rem; }
|
||||
.table-scroll { overflow-x: auto; }
|
||||
.admin-nav { display: flex; gap: var(--spacing-md); padding: var(--spacing-md); background: var(--color-bg-alt); border-radius: var(--border-radius); margin-bottom: var(--spacing-lg); flex-wrap: wrap; align-items: center; }
|
||||
.admin-nav .logout-form { margin-left: auto; }
|
||||
.admin-nav .logout-form button { margin: 0; }
|
||||
@@ -79,7 +148,7 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
.note-table { width: 100%; border-collapse: collapse; margin-top: var(--spacing-md); }
|
||||
.note-table th, .note-table td { padding: var(--spacing-md); text-align: left; border-bottom: 1px solid var(--color-border); }
|
||||
.note-table th { background: var(--color-bg-alt); font-weight: 600; }
|
||||
.note-content-preview { max-width: 30rem; }
|
||||
.note-content-preview { min-width: 14rem; overflow-wrap: anywhere; }
|
||||
.note-excerpt { margin-bottom: var(--spacing-xs); }
|
||||
.note-slug { color: var(--color-text-light); font-family: var(--font-mono); font-size: 0.75rem; }
|
||||
.note-date { white-space: nowrap; color: var(--color-text-light); }
|
||||
@@ -171,11 +240,14 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
}
|
||||
|
||||
@media (min-width: 768px) {
|
||||
body { padding: var(--spacing-lg); }
|
||||
h1 { font-size: 2.5rem; } h2 { font-size: 2rem; } h3 { font-size: 1.5rem; }
|
||||
body { padding: var(--spacing-md) var(--spacing-lg); }
|
||||
h1 { font-size: 2rem; } h2 { font-size: 1.5rem; } h3 { font-size: 1.25rem; }
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
.site-nav { width: 100%; }
|
||||
.site-search { flex: 1; justify-content: flex-end; }
|
||||
.site-search input[type="search"] { width: 100%; max-width: 12rem; }
|
||||
.note-table { font-size: 0.875rem; }
|
||||
.note-table th, .note-table td { padding: var(--spacing-sm); }
|
||||
.note-actions .button { font-size: 0.75rem; padding: 0.25rem 0.5rem; }
|
||||
@@ -191,3 +263,281 @@ small { display: block; margin-top: var(--spacing-xs); color: var(--color-text-l
|
||||
aspect-ratio: 16 / 9; /* Wider aspect on mobile */
|
||||
}
|
||||
}
|
||||
|
||||
/* Webmention Styles (v1.6.0 Phase 5) */
|
||||
|
||||
/* Reply Context */
|
||||
.reply-context {
|
||||
margin-bottom: var(--spacing-md);
|
||||
padding: var(--spacing-sm);
|
||||
border-left: 3px solid var(--color-link);
|
||||
background: var(--color-bg-alt);
|
||||
}
|
||||
|
||||
.reply-context .reply-label {
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-text-light);
|
||||
display: block;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.reply-context blockquote {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.reply-context .p-content {
|
||||
font-style: italic;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
/* Webmentions Section */
|
||||
.webmentions {
|
||||
margin-top: var(--spacing-lg);
|
||||
padding-top: var(--spacing-lg);
|
||||
border-top: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.webmentions h2 {
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
.webmention-group {
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.webmention-group h3 {
|
||||
font-size: 1rem;
|
||||
margin-bottom: var(--spacing-sm);
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
/* Reply Webmentions */
|
||||
.webmention-reply {
|
||||
margin-bottom: var(--spacing-md);
|
||||
padding: var(--spacing-sm);
|
||||
background: var(--color-bg-alt);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.webmention-author {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
margin-bottom: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.webmention-author img {
|
||||
border-radius: 50%;
|
||||
}
|
||||
|
||||
.webmention-author a {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-sm);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.webmention-date {
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
/* Facepile */
|
||||
.facepile {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.facepile .face {
|
||||
display: block;
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 50%;
|
||||
overflow: hidden;
|
||||
border: 2px solid var(--color-bg);
|
||||
margin-left: -8px;
|
||||
}
|
||||
|
||||
.facepile .face:first-child {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.facepile .face > .h-card,
|
||||
.facepile .face > .h-card > a {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.facepile .face img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.facepile .face-initial {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.facepile .face-overflow {
|
||||
margin-left: var(--spacing-xs);
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
/* Generic Mentions */
|
||||
.webmention-mention {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
padding: var(--spacing-xs);
|
||||
background: var(--color-bg-alt);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.webmention-mention .h-card img {
|
||||
border-radius: 50%;
|
||||
vertical-align: middle;
|
||||
margin-right: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.webmention-mention p {
|
||||
margin: var(--spacing-xs) 0 0 0;
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
/* Admin Webmention Styles */
|
||||
|
||||
.filter-tabs {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
margin-bottom: var(--spacing-md);
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.filter-tabs a {
|
||||
padding: var(--spacing-sm) var(--spacing-md);
|
||||
text-decoration: none;
|
||||
color: var(--color-text-light);
|
||||
border-bottom: 2px solid transparent;
|
||||
}
|
||||
|
||||
.filter-tabs a.active {
|
||||
color: var(--color-link);
|
||||
border-bottom-color: var(--color-link);
|
||||
}
|
||||
|
||||
.bulk-actions {
|
||||
display: flex;
|
||||
gap: var(--spacing-sm);
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
.webmention-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
.webmention-table th,
|
||||
.webmention-table td {
|
||||
padding: var(--spacing-sm);
|
||||
text-align: left;
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
}
|
||||
|
||||
.webmention-table th {
|
||||
background: var(--color-bg-alt);
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.badge {
|
||||
display: inline-block;
|
||||
padding: 0.25rem 0.5rem;
|
||||
font-size: 0.75rem;
|
||||
border-radius: 4px;
|
||||
background: var(--color-bg-alt);
|
||||
}
|
||||
|
||||
.badge-reply { background: #DBEAFE; color: #1E40AF; }
|
||||
.badge-like { background: #FECACA; color: #991B1B; }
|
||||
.badge-repost { background: #D1FAE5; color: #065F46; }
|
||||
.badge-bookmark { background: #FEF3C7; color: #92400E; }
|
||||
.badge-mention { background: #E5E7EB; color: #374151; }
|
||||
|
||||
.status-badge {
|
||||
display: inline-block;
|
||||
padding: 0.25rem 0.5rem;
|
||||
font-size: 0.75rem;
|
||||
border-radius: 4px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.status-pending { background: #FEF3C7; color: #92400E; }
|
||||
.status-approved { background: #D1FAE5; color: #065F46; }
|
||||
.status-hidden { background: #E5E7EB; color: #6B7280; }
|
||||
.status-valid { background: #D1FAE5; color: #065F46; }
|
||||
.status-invalid { background: #FECACA; color: #991B1B; }
|
||||
.status-error { background: #FECACA; color: #991B1B; }
|
||||
|
||||
.pagination {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--spacing-md);
|
||||
justify-content: center;
|
||||
margin-top: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.detail-card {
|
||||
background: var(--color-bg-alt);
|
||||
padding: var(--spacing-md);
|
||||
border-radius: 4px;
|
||||
margin-bottom: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.detail-row {
|
||||
margin-bottom: var(--spacing-md);
|
||||
}
|
||||
|
||||
.detail-row strong {
|
||||
display: block;
|
||||
margin-bottom: var(--spacing-xs);
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
.content-preview {
|
||||
padding: var(--spacing-sm);
|
||||
background: var(--color-bg);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 4px;
|
||||
margin-top: var(--spacing-xs);
|
||||
}
|
||||
|
||||
.moderation-actions {
|
||||
margin-top: var(--spacing-lg);
|
||||
}
|
||||
|
||||
.moderation-actions h3 {
|
||||
margin-bottom: var(--spacing-sm);
|
||||
}
|
||||
|
||||
.error-text {
|
||||
color: #991B1B;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: var(--spacing-lg);
|
||||
color: var(--color-text-light);
|
||||
}
|
||||
|
||||
.empty-state .text-muted {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Post Deleted - {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article class="error-page">
|
||||
<h1>410 - Post Deleted</h1>
|
||||
<p>This post has been deleted.</p>
|
||||
<p><a href="/">Return to homepage</a></p>
|
||||
</article>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,43 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}Authorize App - StarPunk{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="login-container authorize">
|
||||
{% if request_error %}
|
||||
<h2>Cannot authorize this app</h2>
|
||||
<p>{{ request_error }}</p>
|
||||
<p><a href="/">Return to homepage</a></p>
|
||||
{% else %}
|
||||
<h2>Authorize app</h2>
|
||||
<p>
|
||||
<strong>{{ auth_request.client_id }}</strong> wants to sign in as
|
||||
<strong>{{ me }}</strong>.
|
||||
</p>
|
||||
<p><small>After approval you will be sent to {{ auth_request.redirect_uri }}</small></p>
|
||||
|
||||
<form action="{{ url_for('indieauth.approve') }}" method="POST" class="login-form">
|
||||
{% for name, value in fields.items() %}
|
||||
<input type="hidden" name="{{ name }}" value="{{ value }}">
|
||||
{% endfor %}
|
||||
|
||||
{% if auth_request.scopes %}
|
||||
<fieldset>
|
||||
<legend>Permissions requested</legend>
|
||||
{% for scope in auth_request.scopes %}
|
||||
<label>
|
||||
<input type="checkbox" name="scope_granted" value="{{ scope }}" checked>
|
||||
{{ scope }}
|
||||
</label><br>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% else %}
|
||||
<p>This app only wants to confirm your identity. It cannot post.</p>
|
||||
{% endif %}
|
||||
|
||||
<button type="submit" name="decision" value="approve" class="button button-primary">Approve</button>
|
||||
<button type="submit" name="decision" value="deny" class="button button-secondary">Deny</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -1,11 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{# Admin pages use a wider frame so tables fit under the header #}
|
||||
{% block body_class %} class="admin-page"{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="admin-container">
|
||||
<nav class="admin-nav">
|
||||
<a href="{{ url_for('admin.dashboard') }}">Dashboard</a>
|
||||
<a href="{{ url_for('admin.new_note_form') }}">New Note</a>
|
||||
<a href="{{ url_for('admin.metrics_dashboard') }}">Metrics</a>
|
||||
<a href="{{ url_for('admin.tokens') }}">Connected Apps</a>
|
||||
<form action="{{ url_for('auth.logout') }}" method="POST" class="logout-form">
|
||||
<button type="submit" class="button button-secondary">Logout</button>
|
||||
</form>
|
||||
|
||||
@@ -10,9 +10,33 @@
|
||||
<a href="{{ url_for('admin.new_note_form') }}" class="button button-primary">
|
||||
+ New Note
|
||||
</a>
|
||||
{% if config.WEBMENTION_ENABLED %}
|
||||
<a href="{{ url_for('admin.webmentions') }}" class="button">
|
||||
Webmentions
|
||||
{% if webmention_pending_count > 0 %}
|
||||
<span class="badge">{{ webmention_pending_count }}</span>
|
||||
{% endif %}
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{# WebSub subscribers (v2.10.0) #}
|
||||
{% if websub_stats %}
|
||||
<p class="note-meta websub-stats">
|
||||
{% if websub_stats.external %}
|
||||
WebSub: using {{ websub_stats.external }}
|
||||
{% elif websub_stats.by_topic %}
|
||||
WebSub subscribers:
|
||||
{% for topic, count in websub_stats.by_topic %}{{ topic|replace(config.SITE_URL.rstrip('/'), '') or '/' }} {{ count }}{% if not loop.last %}, {% endif %}{% endfor %}
|
||||
{% if websub_stats.retrying %} · {{ websub_stats.retrying }} deliveries being retried{% endif %}
|
||||
{% else %}
|
||||
WebSub: no subscribers yet
|
||||
{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
|
||||
{% if notes %}
|
||||
<div class="table-scroll">
|
||||
<table class="note-table">
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -59,6 +83,21 @@
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{# Pagination (v1.6.0) #}
|
||||
{% if total_pages > 1 %}
|
||||
<nav class="pagination">
|
||||
{% if page > 1 %}
|
||||
<a href="?page={{ page - 1 }}" class="button">Previous</a>
|
||||
{% endif %}
|
||||
<span>Page {{ page }} of {{ total_pages }}</span>
|
||||
{% if page < total_pages %}
|
||||
<a href="?page={{ page + 1 }}" class="button">Next</a>
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<p>No notes yet. Create your first note!</p>
|
||||
|
||||
@@ -12,12 +12,15 @@
|
||||
|
||||
<form action="{{ url_for('admin.update_note_submit', note_id=note.id) }}" method="POST" class="note-form">
|
||||
<div class="form-group">
|
||||
<label for="content">Content (Markdown)</label>
|
||||
{% if note.reacts_to %}
|
||||
<p class="note-meta">{{ {'like': '★ Like of', 'repost': '🔁 Repost of', 'bookmark': '🔖 Bookmark of'}[note.kind] }} <a href="{{ note.reacts_to }}">{{ note.reacts_to }}</a></p>
|
||||
{% endif %}
|
||||
<label for="content">{% if note.reacts_to %}Comment (optional, Markdown){% else %}Content (Markdown){% endif %}</label>
|
||||
<textarea
|
||||
id="content"
|
||||
name="content"
|
||||
rows="20"
|
||||
required
|
||||
rows="{{ 6 if note.reacts_to else 20 }}"
|
||||
{% if not note.reacts_to %}required{% endif %}
|
||||
autofocus
|
||||
>{{ note.content }}</textarea>
|
||||
<small>Use Markdown syntax for formatting</small>
|
||||
@@ -52,15 +55,58 @@
|
||||
<label for="published">Published</label>
|
||||
</div>
|
||||
|
||||
{% if syndication_targets %}
|
||||
<input type="hidden" name="syndication_form" value="1">
|
||||
<fieldset class="form-group">
|
||||
<legend>Crosspost</legend>
|
||||
{% for target in syndication_targets %}
|
||||
<div class="form-checkbox">
|
||||
<input type="checkbox" id="syndicate-{{ target.id }}" name="syndicate_to" value="{{ target.id }}"{% if target.id in syndication_enabled %} checked{% endif %}>
|
||||
<label for="syndicate-{{ target.id }}">{{ target.name }}</label>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="button button-primary">Update Note</button>
|
||||
<a href="{{ url_for('admin.dashboard') }}" class="button button-secondary">Cancel</a>
|
||||
<form action="{{ url_for('admin.delete_note_submit', note_id=note.id) }}" method="POST" class="delete-form" style="display: inline;" onsubmit="return confirm('Are you sure you want to delete this note? This cannot be undone.');">
|
||||
<input type="hidden" name="confirm" value="yes">
|
||||
<button type="submit" class="button button-danger">Delete Note</button>
|
||||
</form>
|
||||
<button type="submit" form="delete-note-form" class="button button-danger">Delete Note</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{# Outside the edit form: nested forms are not allowed in HTML #}
|
||||
<form id="delete-note-form" action="{{ url_for('admin.delete_note_submit', note_id=note.id) }}" method="POST" class="delete-form" onsubmit="return confirm('Are you sure you want to delete this note? This cannot be undone.');">
|
||||
<input type="hidden" name="confirm" value="yes">
|
||||
</form>
|
||||
|
||||
{% if syndications %}
|
||||
<section class="syndication-status">
|
||||
<h3>Crossposts</h3>
|
||||
<ul>
|
||||
{% for s in syndications %}
|
||||
<li>
|
||||
<strong>{{ s.name }}</strong>:
|
||||
{% if s.state == 'synced' and s.remote_url %}
|
||||
<a href="{{ s.remote_url }}" rel="noopener">posted</a>
|
||||
{% elif s.state == 'synced' %}
|
||||
removed
|
||||
{% elif s.state == 'pending' %}
|
||||
pending{% if s.attempts %} (retry {{ s.attempts }}){% endif %}
|
||||
{% else %}
|
||||
failed
|
||||
{% endif %}
|
||||
{% if s.last_error %}<br><small>{{ s.last_error }}</small>{% endif %}
|
||||
{% if s.state != 'synced' %}
|
||||
<form action="{{ url_for('admin.retry_syndication', syndication_id=s.id) }}" method="POST" style="display: inline;">
|
||||
<button type="submit" class="button button-secondary">Retry now</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</section>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
|
||||
@@ -5,23 +5,10 @@
|
||||
{% block content %}
|
||||
<div class="login-container">
|
||||
<h2>Admin Login</h2>
|
||||
<p>Sign in with your personal website using IndieLogin</p>
|
||||
<p>Sign in with {{ config.OIDC_PROVIDER_NAME }} to manage this site.</p>
|
||||
|
||||
<form action="{{ url_for('auth.login_initiate') }}" method="POST" class="login-form">
|
||||
<div class="form-group">
|
||||
<label for="me">Your Website URL</label>
|
||||
<input
|
||||
type="url"
|
||||
id="me"
|
||||
name="me"
|
||||
placeholder="https://example.com"
|
||||
required
|
||||
autofocus
|
||||
>
|
||||
<small>Enter your website URL (must match admin configuration)</small>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="button button-primary">Sign in with IndieLogin</button>
|
||||
<button type="submit" class="button button-primary">Sign in with {{ config.OIDC_PROVIDER_NAME }}</button>
|
||||
</form>
|
||||
|
||||
{% if config.DEV_MODE %}
|
||||
@@ -33,16 +20,5 @@
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="login-help">
|
||||
<h3>What is IndieLogin?</h3>
|
||||
<p>
|
||||
IndieLogin allows you to sign in using your own website.
|
||||
No password required - just authenticate with your domain.
|
||||
</p>
|
||||
<a href="https://indielogin.com/api" target="_blank" rel="noopener">
|
||||
Learn more about IndieLogin
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -65,6 +65,19 @@
|
||||
<label for="published">Publish immediately</label>
|
||||
</div>
|
||||
|
||||
{% if syndication_targets %}
|
||||
<input type="hidden" name="syndication_form" value="1">
|
||||
<fieldset class="form-group">
|
||||
<legend>Crosspost</legend>
|
||||
{% for target in syndication_targets %}
|
||||
<div class="form-checkbox">
|
||||
<input type="checkbox" id="syndicate-{{ target.id }}" name="syndicate_to" value="{{ target.id }}" checked>
|
||||
<label for="syndicate-{{ target.id }}">{{ target.name }}</label>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</fieldset>
|
||||
{% endif %}
|
||||
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="button button-primary">Create Note</button>
|
||||
<a href="{{ url_for('admin.dashboard') }}" class="button button-secondary">Cancel</a>
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
{% extends "admin/base.html" %}
|
||||
|
||||
{% block title %}Connected Apps - StarPunk Admin{% endblock %}
|
||||
|
||||
{% block admin_content %}
|
||||
<div class="tokens-admin">
|
||||
<h2>Connected Apps</h2>
|
||||
<p>Micropub apps you have approved. Revoke any you no longer use.</p>
|
||||
|
||||
{% if tokens %}
|
||||
<div class="table-scroll">
|
||||
<table class="note-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>App</th>
|
||||
<th>Scope</th>
|
||||
<th>Approved</th>
|
||||
<th>Last used</th>
|
||||
<th>Expires</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for t in tokens %}
|
||||
<tr>
|
||||
<td>{{ t.client_id }}</td>
|
||||
<td>{{ t.scope }}</td>
|
||||
<td>{{ t.created_at }}</td>
|
||||
<td>{{ t.last_used_at or "never" }}</td>
|
||||
<td>{{ t.expires_at }}</td>
|
||||
<td>
|
||||
<form method="POST" action="{{ url_for('admin.revoke_token', token_id=t.id) }}">
|
||||
<button type="submit" class="button button-danger"
|
||||
onclick="return confirm('Revoke this app\'s access?')">Revoke</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
<p>No apps connected.</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,108 @@
|
||||
{% extends "admin/base.html" %}
|
||||
|
||||
{% block title %}Webmention Detail - StarPunk Admin{% endblock %}
|
||||
|
||||
{% block admin_content %}
|
||||
<div class="webmention-detail">
|
||||
<h2>Webmention Detail</h2>
|
||||
|
||||
<div class="dashboard-actions">
|
||||
<a href="{{ url_for('admin.webmentions') }}" class="button">
|
||||
Back to Webmentions
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="detail-card">
|
||||
<div class="detail-row">
|
||||
<strong>Source:</strong>
|
||||
<a href="{{ webmention.source_url }}" target="_blank">{{ webmention.source_url }}</a>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Target:</strong>
|
||||
{% if webmention.target_note_id %}
|
||||
<a href="{{ url_for('public.note', slug=webmention.target_slug) }}">{{ webmention.target_url }}</a>
|
||||
{% else %}
|
||||
{{ webmention.target_url }} (Homepage)
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Type:</strong>
|
||||
<span class="badge badge-{{ webmention.mention_type }}">{{ webmention.mention_type }}</span>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Author:</strong>
|
||||
{{ webmention.author_name or 'Anonymous' }}
|
||||
{% if webmention.author_url %}
|
||||
(<a href="{{ webmention.author_url }}" target="_blank">{{ webmention.author_url }}</a>)
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% if webmention.author_photo %}
|
||||
<div class="detail-row">
|
||||
<strong>Author Photo:</strong>
|
||||
<img src="{{ webmention.author_photo }}" alt="{{ webmention.author_name or 'Author' }}" width="48" height="48" loading="lazy">
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if webmention.content_snippet %}
|
||||
<div class="detail-row">
|
||||
<strong>Content:</strong>
|
||||
<div class="content-preview">{{ webmention.content_snippet|safe }}</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Validation Status:</strong>
|
||||
<span class="status-badge status-{{ webmention.validation_status }}">{{ webmention.validation_status }}</span>
|
||||
{% if webmention.validation_error %}
|
||||
<br><small class="error-text">{{ webmention.validation_error }}</small>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Moderation Status:</strong>
|
||||
<span class="status-badge status-{{ webmention.moderation_status }}">{{ webmention.moderation_status }}</span>
|
||||
</div>
|
||||
|
||||
<div class="detail-row">
|
||||
<strong>Received:</strong>
|
||||
{{ webmention.received_at.strftime('%B %d, %Y at %I:%M %p') }}
|
||||
</div>
|
||||
|
||||
{% if webmention.validated_at %}
|
||||
<div class="detail-row">
|
||||
<strong>Validated:</strong>
|
||||
{{ webmention.validated_at.strftime('%B %d, %Y at %I:%M %p') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{# Moderation Actions #}
|
||||
<div class="moderation-actions">
|
||||
<h3>Actions</h3>
|
||||
<form method="POST" action="{{ url_for('admin.webmention_action', id=webmention.id) }}" style="display: inline;">
|
||||
<input type="hidden" name="action" value="approve">
|
||||
<button type="submit" class="button button-primary" {% if webmention.moderation_status == 'approved' %}disabled{% endif %}>
|
||||
Approve
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form method="POST" action="{{ url_for('admin.webmention_action', id=webmention.id) }}" style="display: inline;">
|
||||
<input type="hidden" name="action" value="hide">
|
||||
<button type="submit" class="button" {% if webmention.moderation_status == 'hidden' %}disabled{% endif %}>
|
||||
Hide
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<form method="POST" action="{{ url_for('admin.webmention_action', id=webmention.id) }}" style="display: inline;">
|
||||
<input type="hidden" name="action" value="delete">
|
||||
<button type="submit" class="button button-danger" onclick="return confirm('Delete this webmention permanently?')">
|
||||
Delete
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,127 @@
|
||||
{% extends "admin/base.html" %}
|
||||
|
||||
{% block title %}Webmentions - StarPunk Admin{% endblock %}
|
||||
|
||||
{% block admin_content %}
|
||||
<div class="webmentions-admin">
|
||||
<h2>Webmentions</h2>
|
||||
|
||||
<div class="dashboard-actions">
|
||||
<a href="{{ url_for('admin.dashboard') }}" class="button">
|
||||
Back to Dashboard
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{# Filter tabs #}
|
||||
<div class="filter-tabs">
|
||||
<a href="{{ url_for('admin.webmentions') }}" class="{% if not status_filter %}active{% endif %}">
|
||||
All
|
||||
</a>
|
||||
<a href="{{ url_for('admin.webmentions', status='pending') }}" class="{% if status_filter == 'pending' %}active{% endif %}">
|
||||
Pending
|
||||
</a>
|
||||
<a href="{{ url_for('admin.webmentions', status='approved') }}" class="{% if status_filter == 'approved' %}active{% endif %}">
|
||||
Approved
|
||||
</a>
|
||||
<a href="{{ url_for('admin.webmentions', status='hidden') }}" class="{% if status_filter == 'hidden' %}active{% endif %}">
|
||||
Hidden
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{% if webmentions %}
|
||||
<form method="POST" action="{{ url_for('admin.webmention_bulk_action') }}">
|
||||
<div class="bulk-actions">
|
||||
<button type="submit" name="action" value="approve" class="button">
|
||||
Approve Selected
|
||||
</button>
|
||||
<button type="submit" name="action" value="hide" class="button">
|
||||
Hide Selected
|
||||
</button>
|
||||
<button type="submit" name="action" value="delete" class="button button-danger"
|
||||
onclick="return confirm('Delete selected webmentions permanently?')">
|
||||
Delete Selected
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="table-scroll">
|
||||
<table class="webmention-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><input type="checkbox" id="select-all"></th>
|
||||
<th>Source</th>
|
||||
<th>Target</th>
|
||||
<th>Type</th>
|
||||
<th>Author</th>
|
||||
<th>Status</th>
|
||||
<th>Received</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for wm in webmentions %}
|
||||
<tr>
|
||||
<td><input type="checkbox" name="ids" value="{{ wm.id }}"></td>
|
||||
<td><a href="{{ wm.source_url }}" target="_blank">{{ wm.source_url|truncate(40) }}</a></td>
|
||||
<td>
|
||||
{% if wm.target_note_id %}
|
||||
<a href="{{ url_for('public.note', slug=wm.target_slug) }}">{{ wm.target_slug }}</a>
|
||||
{% else %}
|
||||
Homepage
|
||||
{% endif %}
|
||||
</td>
|
||||
<td><span class="badge badge-{{ wm.mention_type }}">{{ wm.mention_type }}</span></td>
|
||||
<td>{{ wm.author_name or 'Anonymous' }}</td>
|
||||
<td>
|
||||
<span class="status-badge status-{{ wm.moderation_status }}">
|
||||
{{ wm.moderation_status }}
|
||||
</span>
|
||||
</td>
|
||||
<td>{{ wm.received_at.strftime('%b %d, %Y %H:%M') }}</td>
|
||||
<td>
|
||||
<a href="{{ url_for('admin.webmention_detail', id=wm.id) }}" class="button button-small">
|
||||
View
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{# Pagination #}
|
||||
{% if total_pages > 1 %}
|
||||
<nav class="pagination">
|
||||
{% if page > 1 %}
|
||||
<a href="?page={{ page - 1 }}{% if status_filter %}&status={{ status_filter }}{% endif %}" class="button">Previous</a>
|
||||
{% endif %}
|
||||
<span>Page {{ page }} of {{ total_pages }}</span>
|
||||
{% if page < total_pages %}
|
||||
<a href="?page={{ page + 1 }}{% if status_filter %}&status={{ status_filter }}{% endif %}" class="button">Next</a>
|
||||
{% endif %}
|
||||
</nav>
|
||||
{% endif %}
|
||||
|
||||
{% else %}
|
||||
{# Empty states #}
|
||||
{% if status_filter %}
|
||||
<div class="empty-state">
|
||||
<p>No webmentions match the current filters.</p>
|
||||
<a href="{{ url_for('admin.webmentions') }}" class="button">Clear Filters</a>
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="empty-state">
|
||||
<p>No webmentions received yet.</p>
|
||||
<p class="text-muted">They will appear here when other sites mention your content.</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Select All functionality
|
||||
document.getElementById('select-all')?.addEventListener('change', function() {
|
||||
document.querySelectorAll('input[name="ids"]').forEach(cb => cb.checked = this.checked);
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
+46
-23
@@ -3,48 +3,69 @@
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{% block title %}StarPunk{% endblock %}</title>
|
||||
<title>{% block title %}{{ config.SITE_NAME }}{% endblock %}</title>
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
||||
<link rel="alternate" type="application/rss+xml" title="{{ config.SITE_NAME }} RSS Feed" href="{{ url_for('public.feed', _external=True) }}">
|
||||
<link rel="alternate" type="application/xml+opml" title="{{ config.SITE_NAME }} Feed Subscription List" href="{{ url_for('public.opml', _external=True) }}">
|
||||
<link rel="alternate" type="application/rss+xml" title="{{ config.SITE_NAME }} RSS Feed" href="{{ absolute_url_for('public.feed') }}">
|
||||
<link rel="alternate" type="application/xml+opml" title="{{ config.SITE_NAME }} Feed Subscription List" href="{{ absolute_url_for('public.opml') }}">
|
||||
|
||||
{# Webmention endpoint (v1.6.0) #}
|
||||
{% if config.WEBMENTION_ENABLED %}
|
||||
<link rel="webmention" href="/webmention">
|
||||
{% endif %}
|
||||
|
||||
{# WebSub hub and topic on feed pages (v2.10.0) #}
|
||||
{% set websub = websub_links() %}
|
||||
{% if websub %}
|
||||
<link rel="hub" href="{{ websub.hub }}">
|
||||
<link rel="self" href="{{ websub.self }}">
|
||||
{% endif %}
|
||||
|
||||
{# Micropub and IndieAuth discovery (independent of Webmention, v2.8.0) #}
|
||||
<link rel="micropub" href="{{ absolute_url_for('micropub.micropub_endpoint') }}">
|
||||
<link rel="indieauth-metadata" href="{{ absolute_url_for('indieauth.metadata') }}">
|
||||
<link rel="authorization_endpoint" href="{{ absolute_url_for('indieauth.authorize') }}">
|
||||
<link rel="token_endpoint" href="{{ absolute_url_for('indieauth.token') }}">
|
||||
|
||||
{# Author's home page: rel=me makes the h-card linking to it this site's
|
||||
representative h-card; rel=author is the authorship fallback (v2.8.0) #}
|
||||
{% set author_home = (author.url or author.me) if author else config.ADMIN_ME %}
|
||||
{% if author_home %}
|
||||
<link rel="me" href="{{ author_home }}">
|
||||
<link rel="author" href="{{ author_home }}">
|
||||
{% endif %}
|
||||
|
||||
{# rel-me links from discovered author profile (v1.2.0 Phase 2) #}
|
||||
{% if author and author.rel_me_links %}
|
||||
{% for profile_url in author.rel_me_links %}
|
||||
{% for profile_url in author.rel_me_links if profile_url != author_home %}
|
||||
<link rel="me" href="{{ profile_url }}">
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% block head %}{% endblock %}
|
||||
</head>
|
||||
<body>
|
||||
<body{% block body_class %}{% endblock %}>
|
||||
{% if config.DEV_MODE %}
|
||||
<div class="dev-mode-warning">
|
||||
WARNING: DEVELOPMENT MODE - Authentication bypassed
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<header>
|
||||
<h1><a href="/">StarPunk</a></h1>
|
||||
<nav>
|
||||
<header class="site-header">
|
||||
<a class="site-title" href="/">{{ config.SITE_NAME }}</a>
|
||||
<nav class="site-nav">
|
||||
<a href="/">Home</a>
|
||||
<a href="{{ url_for('public.likes') }}">Likes</a>
|
||||
<a href="{{ url_for('public.feed') }}">RSS</a>
|
||||
{% if g.me %}
|
||||
<a href="{{ url_for('admin.dashboard') }}">Admin</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('auth.login_form') }}">Log in</a>
|
||||
{% endif %}
|
||||
<form action="/search" method="get" role="search" style="margin-left: auto; display: flex; gap: var(--spacing-sm);">
|
||||
<input
|
||||
type="search"
|
||||
name="q"
|
||||
placeholder="Search..."
|
||||
aria-label="Search"
|
||||
value="{{ request.args.get('q', '') }}"
|
||||
minlength="2"
|
||||
maxlength="100"
|
||||
required
|
||||
style="width: 200px; padding: var(--spacing-xs) var(--spacing-sm);"
|
||||
>
|
||||
<button type="submit" class="button button-small" style="padding: var(--spacing-xs) var(--spacing-sm);">🔍</button>
|
||||
<form class="site-search" action="/search" method="get" role="search">
|
||||
<input type="search" name="q" placeholder="Search" aria-label="Search"
|
||||
value="{{ request.args.get('q', '') }}" minlength="2" maxlength="100" required>
|
||||
<button type="submit" aria-label="Search">Search</button>
|
||||
</form>
|
||||
</nav>
|
||||
</header>
|
||||
@@ -61,8 +82,10 @@
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<p>StarPunk v{{ config.get('VERSION', '0.5.0') }}</p>
|
||||
<footer class="site-footer">
|
||||
<p>
|
||||
Powered by <a href="https://git.thesatelliteoflove.com/phil/StarPunk">StarPunk</a> v{{ config.get('VERSION', '0.5.0') }}
|
||||
</p>
|
||||
</footer>
|
||||
</body>
|
||||
</html>
|
||||
+5
-48
@@ -1,15 +1,15 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}StarPunk - Home{% endblock %}
|
||||
{% block title %}{{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
{# h-feed required properties per microformats.org/wiki/h-feed #}
|
||||
<h2 class="p-name">{{ config.SITE_NAME or 'Recent Notes' }}</h2>
|
||||
<h1 class="p-name visually-hidden">{{ config.SITE_NAME or 'Recent Notes' }}</h1>
|
||||
|
||||
{# u-url for feed (self-reference) #}
|
||||
<a class="u-url" href="{{ url_for('public.index', _external=True) }}" style="display: none;">Feed URL</a>
|
||||
<a class="u-url" href="{{ absolute_url_for('public.index') }}" style="display: none;">Feed URL</a>
|
||||
|
||||
{# Feed-level author h-card with all properties #}
|
||||
{# Hidden because it's semantic-only markup for parsers, not visual content #}
|
||||
@@ -34,50 +34,7 @@
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Detect if note has explicit title (starts with # heading) - per Q22 #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
<h3 class="p-name">{{ note.title }}</h3>
|
||||
{% endif %}
|
||||
|
||||
{# Media preview (if available) #}
|
||||
{{ display_media(note.media) }}
|
||||
|
||||
{# e-content: note content (preview) #}
|
||||
<div class="e-content">
|
||||
{{ note.html[:300]|safe }}{% if note.html|length > 300 %}...{% endif %}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url for permalink #}
|
||||
<a class="u-url" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# Tags in preview #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>
|
||||
{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card nested in each h-entry (per Q20) #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">No notes published yet. Check back soon!</p>
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}Likes · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
<h1 class="p-name page-title">Likes</h1>
|
||||
|
||||
{# Feed URL and author, as on the home page (v2.8.0) #}
|
||||
<a class="u-url" href="{{ absolute_url_for(request.endpoint, **request.view_args) }}" hidden>Feed URL</a>
|
||||
{% if author %}
|
||||
<span class="p-author h-card" hidden><a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a></span>
|
||||
{% endif %}
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">Nothing liked yet.</p>
|
||||
{% endif %}
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
</div>
|
||||
{% endblock %}
|
||||
+94
-39
@@ -1,72 +1,127 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/reaction.html" import reaction %}
|
||||
{% import "partials/responses.html" as responses with context %}
|
||||
|
||||
{% block title %}{{ note.slug }} - StarPunk{% endblock %}
|
||||
{% block title %}{% if note.has_title %}{{ note.title }}{% elif note.excerpt.strip() %}{{ note.excerpt|truncate(60) }}{% elif note.repost_of %}Repost{% elif note.like_of %}Like{% elif note.bookmark_of %}Bookmark{% elif note.in_reply_to %}Reply{% else %}Photo{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article class="h-entry">
|
||||
{# Detect if note has explicit title (starts with # heading) - per Q22 #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
{# Notes starting with a '# Heading' have a title (Q22; Note.has_title) #}
|
||||
{% set has_explicit_title = note.has_title %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
<h1 class="p-name">{{ note.title }}</h1>
|
||||
{% endif %}
|
||||
|
||||
{# Reply context (v1.6.0 Phase 4) - displayed if this is a reply post #}
|
||||
{% if note.in_reply_to %}
|
||||
<div class="reply-context">
|
||||
<span class="reply-label">In reply to:</span>
|
||||
{# u-in-reply-to on the h-cite itself makes it the entry's property (Post Type Discovery) #}
|
||||
<blockquote class="u-in-reply-to h-cite">
|
||||
{% if reply_context and reply_context.fetch_status == 'fetched' %}
|
||||
{% if reply_context.content_snippet %}
|
||||
<p class="p-content">{{ reply_context.content_snippet }}</p>
|
||||
{% endif %}
|
||||
<cite>
|
||||
<a class="u-url" href="{{ note.in_reply_to }}">
|
||||
{% if reply_context.author_name %}{{ reply_context.author_name }}{% endif %}
|
||||
{% if reply_context.site_name %} on {{ reply_context.site_name }}{% endif %}
|
||||
{% if not reply_context.author_name and not reply_context.site_name %}{{ note.in_reply_to }}{% endif %}
|
||||
</a>
|
||||
</cite>
|
||||
{% else %}
|
||||
<cite>
|
||||
<a class="u-url" href="{{ note.in_reply_to }}">{{ note.in_reply_to }}</a>
|
||||
</cite>
|
||||
{% endif %}
|
||||
</blockquote>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{{ reaction(note) }}
|
||||
|
||||
{# u-photo placement: Per draft spec, u-photo must be direct child of h-entry, #}
|
||||
{# NOT inside e-content. Media is rendered ABOVE e-content to meet this requirement. #}
|
||||
{{ display_media(note.media) }}
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
|
||||
{# e-content: note content BELOW media (per ADR-057) #}
|
||||
<div class="e-content">
|
||||
{{ note.html|safe }}
|
||||
{{ note.body_html|safe }}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url and u-uid same for notes (per Q23) #}
|
||||
<a class="u-url u-uid" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y at %I:%M %p') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# dt-updated if note was modified #}
|
||||
<a class="u-url u-uid" href="{{ absolute_url_for('public.note', slug=note.slug) }}"><time class="dt-published" datetime="{{ note.created_at|iso_utc }}">{{ note.created_at.strftime('%B %-d, %Y · %-I:%M %p') }}</time></a>
|
||||
{% if note.updated_at and note.updated_at != note.created_at %}
|
||||
<span class="updated">
|
||||
(Updated: <time class="dt-updated" datetime="{{ note.updated_at.isoformat() }}">{{ note.updated_at.strftime('%B %d, %Y') }}</time>)
|
||||
<span class="updated">Updated <time class="dt-updated" datetime="{{ note.updated_at|iso_utc }}">{{ note.updated_at.strftime('%b %-d, %Y') }}</time></span>
|
||||
{% endif %}
|
||||
{# rel="tag" per microformats2 p-category #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Tags / Categories #}
|
||||
{# rel="tag" per microformats2 p-category specification #}
|
||||
{% if note.tags %}
|
||||
<div class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">
|
||||
{{ tag.display_name }}
|
||||
</a>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% set synced = (syndications or [])|selectattr('remote_url')|list %}
|
||||
{% if synced %}
|
||||
<span class="syndication">Also on {% for s in synced %}<a class="u-syndication" href="{{ s.remote_url }}" rel="syndication">{{ s.name }}</a>{% if not loop.last %}, {% endif %}{% endfor %}</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card (nested within h-entry per Q20) #}
|
||||
{# Author h-card (nested within h-entry per Q20); hidden on a single-author site #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
{% if author.photo %}
|
||||
<img class="u-photo" src="{{ author.photo }}" alt="{{ author.name or 'Author' }}" width="48" height="48">
|
||||
{% endif %}
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
{% if author.note %}
|
||||
<span class="p-note" style="display: none;">{{ author.note }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
<span class="p-author h-card visually-hidden">
|
||||
{% if author.photo %}<img class="u-photo" src="{{ author.photo }}" alt="">{% endif %}
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a>
|
||||
{% if author.note %}<span class="p-note">{{ author.note }}</span>{% endif %}
|
||||
</span>
|
||||
{% endif %}
|
||||
</footer>
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">Back to all notes</a>
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
|
||||
{# Webmentions section (v1.6.0 Phase 5) - only shown if approved webmentions exist #}
|
||||
{% if webmentions %}
|
||||
<section class="webmentions">
|
||||
<h2>Responses</h2>
|
||||
|
||||
{% if replies %}
|
||||
<div class="webmention-group webmention-replies">
|
||||
<h3>{{ replies|length }} {{ 'Reply' if replies|length == 1 else 'Replies' }}</h3>
|
||||
{% for wm in replies %}{{ responses.reply(wm) }}{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if likes %}
|
||||
<div class="webmention-group webmention-likes">
|
||||
<h3>{{ likes|length }} {{ 'Like' if likes|length == 1 else 'Likes' }}</h3>
|
||||
{{ responses.facepile(likes, 'u-like') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if reposts %}
|
||||
<div class="webmention-group webmention-reposts">
|
||||
<h3>{{ reposts|length }} {{ 'Repost' if reposts|length == 1 else 'Reposts' }}</h3>
|
||||
{{ responses.facepile(reposts, 'u-repost') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if bookmarks %}
|
||||
<div class="webmention-group webmention-bookmarks">
|
||||
<h3>{{ bookmarks|length }} {{ 'Bookmark' if bookmarks|length == 1 else 'Bookmarks' }}</h3>
|
||||
{{ responses.facepile(bookmarks, 'u-bookmark') }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if mentions %}
|
||||
<div class="webmention-group webmention-mentions">
|
||||
<h3>{{ mentions|length }} {{ 'Mention' if mentions|length == 1 else 'Mentions' }}</h3>
|
||||
{% for wm in mentions %}{{ responses.mention(wm) }}{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</section>
|
||||
{% endif %}
|
||||
</article>
|
||||
{% endblock %}
|
||||
@@ -1,11 +1,12 @@
|
||||
{# Reusable media display macro #}
|
||||
{% macro display_media(media_items) %}
|
||||
{# fallback_alt: for photos without a caption, usually the post's text (v2.6.5) #}
|
||||
{% macro display_media(media_items, fallback_alt='') %}
|
||||
{% if media_items %}
|
||||
<div class="note-media">
|
||||
{% for item in media_items %}
|
||||
<figure class="media-item">
|
||||
<img src="{{ url_for('public.media_file', path=item.path) }}"
|
||||
alt="{{ item.caption or 'Image' }}"
|
||||
alt="{{ item.caption or fallback_alt or 'Image' }}"
|
||||
class="u-photo"
|
||||
loading="lazy">
|
||||
{# No figcaption - caption is for alt text only #}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
{# One note in a list (home, tag pages). v2.3.0 #}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/reaction.html" import reaction %}
|
||||
{% macro note_preview(note, author) %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Notes starting with a '# Heading' have a title (Q22; Note.has_title) #}
|
||||
{% set has_explicit_title = note.has_title %}
|
||||
{% if has_explicit_title %}
|
||||
<h2 class="p-name"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">{{ note.title }}</a></h2>
|
||||
{% endif %}
|
||||
|
||||
{% if note.in_reply_to %}
|
||||
<p class="reaction reply-to"><span class="reaction-verb">↩ In reply to</span> <a class="u-in-reply-to" href="{{ note.in_reply_to }}">{{ note.in_reply_to }}</a></p>
|
||||
{% endif %}
|
||||
|
||||
{{ reaction(note) }}
|
||||
|
||||
{{ display_media(note.media, note.excerpt|truncate(150)) }}
|
||||
|
||||
{% if has_explicit_title %}
|
||||
{# Titled notes are articles: show a summary and link through #}
|
||||
<div class="p-summary">
|
||||
<p>{{ note.excerpt }}</p>
|
||||
</div>
|
||||
<p class="read-more"><a href="{{ absolute_url_for('public.note', slug=note.slug) }}">Read more →</a></p>
|
||||
{% else %}
|
||||
{# Always present, even empty: without it a text-free photo post gets an implied name (mf2) #}
|
||||
<div class="e-content">
|
||||
{{ note.html|safe }}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<footer class="note-meta">
|
||||
<a class="u-url u-uid" href="{{ absolute_url_for('public.note', slug=note.slug) }}"><time class="dt-published" datetime="{{ note.created_at|iso_utc }}">{{ note.created_at.strftime('%b %-d, %Y') }}</time></a>
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
{% if author %}
|
||||
{# Single-author site: keep the h-card for parsers, hide it from readers #}
|
||||
<span class="p-author h-card visually-hidden"><a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a></span>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{% endmacro %}
|
||||
@@ -0,0 +1,12 @@
|
||||
{# "Liked" / "Reposted" line for likes and reposts (v2.5.0) #}
|
||||
{% macro reaction(note) %}
|
||||
{% if note.reacts_to %}
|
||||
{% set ctx = note.context if note.context is defined else None %}
|
||||
{% if ctx and ctx.title %}{% set label = ctx.title %}{% elif ctx and ctx.content_snippet %}{% set label = ctx.content_snippet|striptags|truncate(90) %}{% elif ctx and (ctx.author_name or ctx.site_name) %}{% set label = 'a post' %}{% else %}{% set label = note.reacts_to %}{% endif %}
|
||||
<p class="reaction reaction-{{ note.kind }}">
|
||||
<span class="reaction-verb">{{ {'like': '★ Liked', 'repost': '🔁 Reposted', 'bookmark': '🔖 Bookmarked'}[note.kind] }}</span>
|
||||
<a class="u-{{ note.kind }}-of" href="{{ note.reacts_to }}">{{ label }}</a>
|
||||
{% if ctx and (ctx.author_name or ctx.site_name) %}<span class="reaction-by">by {{ ctx.author_name or ctx.site_name }}</span>{% endif %}
|
||||
</p>
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
@@ -0,0 +1,62 @@
|
||||
{# Received webmentions as microformats2 responses (v2.8.0)
|
||||
Each response is an h-cite in a property of the note's h-entry:
|
||||
u-comment (replies), u-like, u-repost, u-bookmark, u-mention.
|
||||
See https://indieweb.org/comments-presentation and microformats.org/wiki/h-entry #}
|
||||
|
||||
{# When the source said when it was posted, use that; else when we received it #}
|
||||
{% macro response_time(wm, fmt='%B %-d, %Y') %}
|
||||
{% set when = wm.source_published or wm.received_at %}
|
||||
<time class="dt-published" datetime="{{ when|iso_utc }}">{{ when.strftime(fmt) if when.strftime is defined else when }}</time>
|
||||
{%- endmacro %}
|
||||
|
||||
{# The response's own URL: the source's u-url if it gave one #}
|
||||
{% macro response_url(wm) %}{{ wm.source_post_url or wm.source_url }}{% endmacro %}
|
||||
|
||||
{# Author h-card: name, URL and photo. u-url is on an inner link: on the
|
||||
h-card element itself it would belong to the enclosing h-cite #}
|
||||
{% macro author_card(wm, class='', photo_size=48, show_name=True) %}
|
||||
<span class="p-author h-card {{ class }}"><a class="u-url" href="{{ wm.author_url or wm.source_url }}" title="{{ wm.author_name or 'Anonymous' }}">
|
||||
{%- if wm.author_photo %}<img class="u-photo" src="{{ wm.author_photo }}" alt="" width="{{ photo_size }}" height="{{ photo_size }}" loading="lazy">
|
||||
{%- elif not show_name %}<span class="face-initial" style="background-color: {{ wm.author_url|url_to_color }}" aria-hidden="true">{{ (wm.author_name or '?')[0]|upper }}</span>
|
||||
{%- endif %}
|
||||
<span class="p-name{{ '' if show_name else ' visually-hidden' }}">{{ wm.author_name or 'Anonymous' }}</span>
|
||||
</a></span>
|
||||
{%- endmacro %}
|
||||
|
||||
{# A reply, shown as a comment #}
|
||||
{% macro reply(wm) %}
|
||||
<article class="webmention webmention-reply u-comment h-cite">
|
||||
<div class="webmention-author">{{ author_card(wm) }}</div>
|
||||
{% if wm.content_snippet %}
|
||||
<div class="e-content">{{ wm.content_snippet|safe }}</div>
|
||||
{% endif %}
|
||||
<a class="u-url webmention-date" href="{{ response_url(wm) }}">{{ response_time(wm) }}</a>
|
||||
</article>
|
||||
{%- endmacro %}
|
||||
|
||||
{# Likes, reposts or bookmarks as a facepile; prop is u-like / u-repost / u-bookmark.
|
||||
Faces past `shown` are hidden on screen but kept for parsers. #}
|
||||
{% macro facepile(items, prop, shown=8) %}
|
||||
<div class="facepile">
|
||||
{% for wm in items %}
|
||||
<span class="face {{ prop }} h-cite"{% if loop.index > shown %} hidden{% endif %}>
|
||||
{{ author_card(wm, photo_size=32, show_name=False) }}
|
||||
<a class="u-url" href="{{ response_url(wm) }}" hidden>{{ response_time(wm) }}</a>
|
||||
</span>
|
||||
{% endfor %}
|
||||
{% if items|length > shown %}
|
||||
<span class="face-overflow">+{{ items|length - shown }} others</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
|
||||
{# A post that mentions this note without replying #}
|
||||
{% macro mention(wm) %}
|
||||
<div class="webmention webmention-mention u-mention h-cite">
|
||||
{{ author_card(wm, photo_size=24) }}
|
||||
{% if wm.content_snippet %}
|
||||
<p class="p-content">{{ wm.content_snippet|striptags|truncate(200) }}</p>
|
||||
{% endif %}
|
||||
<a class="u-url webmention-date" href="{{ response_url(wm) }}">{{ response_time(wm) }}</a>
|
||||
</div>
|
||||
{%- endmacro %}
|
||||
@@ -1,6 +1,6 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block title %}{% if query %}Search: {{ query }}{% else %}Search{% endif %} - StarPunk{% endblock %}
|
||||
{% block title %}{% if query %}Search: {{ query }}{% else %}Search{% endif %} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="search-container">
|
||||
|
||||
+11
-48
@@ -1,65 +1,28 @@
|
||||
{% extends "base.html" %}
|
||||
{% from "partials/media.html" import display_media %}
|
||||
{% from "partials/note_preview.html" import note_preview %}
|
||||
|
||||
{% block title %}{{ tag.display_name }} - StarPunk{% endblock %}
|
||||
{% block title %}{{ tag.display_name }} · {{ config.SITE_NAME }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<div class="h-feed">
|
||||
<h1 class="p-name">Notes tagged "{{ tag.display_name }}"</h1>
|
||||
<h1 class="p-name page-title">Notes tagged <span class="tag-name">{{ tag.display_name }}</span></h1>
|
||||
|
||||
{# Feed URL and author, as on the home page (v2.8.0) #}
|
||||
<a class="u-url" href="{{ absolute_url_for(request.endpoint, **request.view_args) }}" hidden>Feed URL</a>
|
||||
{% if author %}
|
||||
<span class="p-author h-card" hidden><a class="p-name u-url" href="{{ author.url or author.me }}">{{ author.name or author.url or author.me }}</a></span>
|
||||
{% endif %}
|
||||
|
||||
{% if notes %}
|
||||
{% for note in notes %}
|
||||
<article class="h-entry note-preview">
|
||||
{# Detect if note has explicit title (starts with # heading) - per Q22 #}
|
||||
{% set has_explicit_title = note.content.strip().startswith('#') %}
|
||||
|
||||
{# p-name only if note has explicit title (per Q22) #}
|
||||
{% if has_explicit_title %}
|
||||
<h3 class="p-name">{{ note.title }}</h3>
|
||||
{% endif %}
|
||||
|
||||
{# Media preview (if available) #}
|
||||
{{ display_media(note.media) }}
|
||||
|
||||
{# e-content: note content (preview) #}
|
||||
<div class="e-content">
|
||||
{{ note.html[:300]|safe }}{% if note.html|length > 300 %}...{% endif %}
|
||||
</div>
|
||||
|
||||
<footer class="note-meta">
|
||||
{# u-url for permalink #}
|
||||
<a class="u-url" href="{{ url_for('public.note', slug=note.slug, _external=True) }}">
|
||||
<time class="dt-published" datetime="{{ note.created_at.isoformat() }}">
|
||||
{{ note.created_at.strftime('%B %d, %Y') }}
|
||||
</time>
|
||||
</a>
|
||||
|
||||
{# Tags in preview #}
|
||||
{% if note.tags %}
|
||||
<span class="note-tags">
|
||||
{% for tag in note.tags %}
|
||||
<a class="p-category" rel="tag" href="{{ url_for('public.tag', tag=tag.name) }}">{{ tag.display_name }}</a>
|
||||
{% endfor %}
|
||||
</span>
|
||||
{% endif %}
|
||||
|
||||
{# Author h-card nested in each h-entry (per Q20) #}
|
||||
{% if author %}
|
||||
<div class="p-author h-card">
|
||||
<a class="p-name u-url" href="{{ author.url or author.me }}">
|
||||
{{ author.name or author.url or author.me }}
|
||||
</a>
|
||||
</div>
|
||||
{% endif %}
|
||||
</footer>
|
||||
</article>
|
||||
{{ note_preview(note, author) }}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<p class="empty-state">No notes with this tag.</p>
|
||||
{% endif %}
|
||||
|
||||
<nav class="note-nav">
|
||||
<a href="/">Back to all notes</a>
|
||||
<a href="/">← All notes</a>
|
||||
</nav>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
Absolute URLs come from SITE_URL, not the request (v2.0.1)
|
||||
|
||||
Behind a reverse proxy the app is reached over plain HTTP, so URLs built
|
||||
from the request said http:// on an https site.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from starpunk import create_app
|
||||
from starpunk.notes import create_note
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
data = tmp_path / "data"
|
||||
return create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_pages_use_site_url_behind_http_proxy(app):
|
||||
with app.app_context():
|
||||
note = create_note(content="Hello over the proxy", published=True, tags=["proxy"])
|
||||
client = app.test_client()
|
||||
|
||||
pages = ["/", f"/note/{note.slug}", "/tag/proxy"]
|
||||
for path in pages:
|
||||
# The test client makes plain http://localhost requests, like Caddy does
|
||||
response = client.get(path)
|
||||
assert response.status_code == 200, path
|
||||
html = response.data.decode()
|
||||
assert "http://localhost" not in html, path
|
||||
|
||||
home = client.get("/").data.decode()
|
||||
assert 'href="https://blog.example.com/feed"' in home
|
||||
assert 'href="https://blog.example.com/opml.xml"' in home
|
||||
|
||||
note_html = client.get(f"/note/{note.slug}").data.decode()
|
||||
assert f'href="https://blog.example.com/note/{note.slug}"' in note_html
|
||||
@@ -12,21 +12,11 @@ import pytest
|
||||
from flask import g
|
||||
|
||||
from starpunk.auth import (
|
||||
AuthError,
|
||||
IndieLoginError,
|
||||
InvalidStateError,
|
||||
UnauthorizedError,
|
||||
_cleanup_expired_sessions,
|
||||
_generate_state_token,
|
||||
_hash_token,
|
||||
_log_http_request,
|
||||
_log_http_response,
|
||||
_redact_token,
|
||||
_verify_state_token,
|
||||
create_session,
|
||||
destroy_session,
|
||||
handle_callback,
|
||||
initiate_login,
|
||||
require_auth,
|
||||
verify_session,
|
||||
)
|
||||
@@ -104,45 +94,6 @@ class TestHelpers:
|
||||
assert len(set(tokens)) == 10
|
||||
|
||||
|
||||
class TestStateTokenVerification:
|
||||
def test_verify_valid_state_token(self, app, db):
|
||||
"""Test verifying a valid state token"""
|
||||
with app.app_context():
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
assert _verify_state_token(state) is True
|
||||
|
||||
# Token should be deleted after verification
|
||||
result = db.execute(
|
||||
"SELECT 1 FROM auth_state WHERE state = ?", (state,)
|
||||
).fetchone()
|
||||
assert result is None
|
||||
|
||||
def test_verify_invalid_state_token(self, app):
|
||||
"""Test verifying an invalid state token"""
|
||||
with app.app_context():
|
||||
assert _verify_state_token("invalid-token") is False
|
||||
|
||||
def test_verify_expired_state_token(self, app, db):
|
||||
"""Test verifying an expired state token"""
|
||||
with app.app_context():
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() - timedelta(minutes=5)
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
assert _verify_state_token(state) is False
|
||||
|
||||
|
||||
class TestCleanup:
|
||||
@@ -170,25 +121,6 @@ class TestCleanup:
|
||||
).fetchone()
|
||||
assert result is None
|
||||
|
||||
def test_cleanup_expired_auth_state(self, app, db):
|
||||
"""Test cleanup of expired auth state"""
|
||||
with app.app_context():
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() - timedelta(minutes=10)
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
_cleanup_expired_sessions()
|
||||
|
||||
# Expired state should be deleted
|
||||
result = db.execute(
|
||||
"SELECT 1 FROM auth_state WHERE state = ?", (state,)
|
||||
).fetchone()
|
||||
assert result is None
|
||||
|
||||
def test_cleanup_keeps_valid_sessions(self, app, db):
|
||||
"""Test that cleanup keeps valid sessions"""
|
||||
@@ -214,185 +146,8 @@ class TestCleanup:
|
||||
assert result is not None
|
||||
|
||||
|
||||
class TestInitiateLogin:
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
def test_initiate_login_success(self, mock_discover, app, db):
|
||||
"""Test successful login initiation"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
me_url = "https://example.com"
|
||||
auth_url = initiate_login(me_url)
|
||||
|
||||
# Changed: Check for discovered endpoint instead of indielogin.com
|
||||
assert "auth.example.com/authorize" in auth_url
|
||||
assert "me=https%3A%2F%2Fexample.com" in auth_url
|
||||
assert "client_id=" in auth_url
|
||||
assert "redirect_uri=" in auth_url
|
||||
assert "state=" in auth_url
|
||||
# Per W3C IndieAuth: response_type=id for authentication-only (identity verification)
|
||||
assert "response_type=id" in auth_url
|
||||
|
||||
# State should be stored in database
|
||||
result = db.execute("SELECT COUNT(*) as count FROM auth_state").fetchone()
|
||||
assert result["count"] > 0
|
||||
|
||||
def test_initiate_login_invalid_url(self, app):
|
||||
"""Test login initiation with invalid URL"""
|
||||
with app.app_context():
|
||||
with pytest.raises(ValueError, match="Invalid URL format"):
|
||||
initiate_login("not-a-url")
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
def test_initiate_login_stores_state(self, mock_discover, app, db):
|
||||
"""Test that state token is stored"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
me_url = "https://example.com"
|
||||
auth_url = initiate_login(me_url)
|
||||
|
||||
# Extract state from URL
|
||||
state_param = [p for p in auth_url.split("&") if p.startswith("state=")][0]
|
||||
state = state_param.split("=")[1]
|
||||
|
||||
# State should exist in database
|
||||
result = db.execute(
|
||||
"SELECT expires_at FROM auth_state WHERE state = ?", (state,)
|
||||
).fetchone()
|
||||
assert result is not None
|
||||
|
||||
|
||||
class TestHandleCallback:
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
@patch("starpunk.auth.httpx.post")
|
||||
def test_handle_callback_success(self, mock_post, mock_discover, app, db, client):
|
||||
"""Test successful callback handling"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.test_request_context():
|
||||
# Setup state token
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Mock authorization endpoint response
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"me": "https://example.com"}
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
# Handle callback
|
||||
code = "test-code"
|
||||
session_token = handle_callback(code, state)
|
||||
|
||||
assert session_token is not None
|
||||
assert isinstance(session_token, str)
|
||||
|
||||
# Session should be created
|
||||
token_hash = _hash_token(session_token)
|
||||
result = db.execute(
|
||||
"SELECT me FROM sessions WHERE session_token_hash = ?", (token_hash,)
|
||||
).fetchone()
|
||||
assert result is not None
|
||||
assert result["me"] == "https://example.com"
|
||||
|
||||
def test_handle_callback_invalid_state(self, app):
|
||||
"""Test callback with invalid state"""
|
||||
with app.app_context():
|
||||
with pytest.raises(InvalidStateError):
|
||||
handle_callback("code", "invalid-state")
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
@patch("starpunk.auth.httpx.post")
|
||||
def test_handle_callback_unauthorized_user(self, mock_post, mock_discover, app, db):
|
||||
"""Test callback with unauthorized user"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
# Setup state token
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Mock authorization endpoint response with different user
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"me": "https://attacker.com"}
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with pytest.raises(UnauthorizedError):
|
||||
handle_callback("code", state)
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
@patch("starpunk.auth.httpx.post")
|
||||
def test_handle_callback_indielogin_error(self, mock_post, mock_discover, app, db):
|
||||
"""Test callback with authorization endpoint error"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
# Setup state token
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Mock authorization endpoint error
|
||||
mock_post.side_effect = httpx.RequestError("Connection failed")
|
||||
|
||||
with pytest.raises(IndieLoginError):
|
||||
handle_callback("code", state)
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
@patch("starpunk.auth.httpx.post")
|
||||
def test_handle_callback_no_identity(self, mock_post, mock_discover, app, db):
|
||||
"""Test callback with no identity in response"""
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
# Setup state token
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Mock authorization endpoint response without 'me' field
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {}
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with pytest.raises(IndieLoginError, match="No identity returned"):
|
||||
handle_callback("code", state)
|
||||
|
||||
|
||||
class TestCreateSession:
|
||||
@@ -625,23 +380,6 @@ class TestSecurityFeatures:
|
||||
assert result["session_token_hash"] != session_token
|
||||
assert len(result["session_token_hash"]) == 64 # SHA-256 hex length
|
||||
|
||||
def test_state_tokens_are_single_use(self, app, db):
|
||||
"""Test that state tokens can only be used once"""
|
||||
with app.app_context():
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# First verification should succeed
|
||||
assert _verify_state_token(state) is True
|
||||
|
||||
# Second verification should fail (token deleted)
|
||||
assert _verify_state_token(state) is False
|
||||
|
||||
def test_session_expiry(self, app, db, client):
|
||||
"""Test that sessions expire correctly"""
|
||||
@@ -668,273 +406,13 @@ class TestSecurityFeatures:
|
||||
assert delta.total_seconds() <= 86401 # At most 1 day + 1 second
|
||||
|
||||
|
||||
class TestExceptionHierarchy:
|
||||
def test_exception_inheritance(self):
|
||||
"""Test that custom exceptions inherit correctly"""
|
||||
assert issubclass(InvalidStateError, AuthError)
|
||||
assert issubclass(UnauthorizedError, AuthError)
|
||||
assert issubclass(IndieLoginError, AuthError)
|
||||
assert issubclass(AuthError, Exception)
|
||||
|
||||
def test_exception_messages(self):
|
||||
"""Test that exceptions can carry messages"""
|
||||
error = InvalidStateError("Test message")
|
||||
assert str(error) == "Test message"
|
||||
|
||||
error = UnauthorizedError("Unauthorized")
|
||||
assert str(error) == "Unauthorized"
|
||||
|
||||
error = IndieLoginError("Service error")
|
||||
assert str(error) == "Service error"
|
||||
|
||||
|
||||
class TestLoggingHelpers:
|
||||
def test_redact_token_normal(self):
|
||||
"""Test token redaction for normal-length tokens"""
|
||||
token = "abcdefghijklmnopqrstuvwxyz"
|
||||
result = _redact_token(token, 6)
|
||||
assert result == "abcdef...********...wxyz"
|
||||
|
||||
def test_redact_token_short(self):
|
||||
"""Test token redaction for short tokens"""
|
||||
token = "short"
|
||||
result = _redact_token(token, 6)
|
||||
assert result == "***REDACTED***"
|
||||
|
||||
def test_redact_token_empty(self):
|
||||
"""Test token redaction for empty tokens"""
|
||||
result = _redact_token("", 6)
|
||||
assert result == "***REDACTED***"
|
||||
|
||||
result = _redact_token(None, 6)
|
||||
assert result == "***REDACTED***"
|
||||
|
||||
def test_redact_token_custom_length(self):
|
||||
"""Test token redaction with custom show_chars"""
|
||||
token = "abcdefghijklmnopqrstuvwxyz"
|
||||
result = _redact_token(token, 8)
|
||||
assert result == "abcdefgh...********...wxyz"
|
||||
|
||||
def test_log_http_request_redacts_code(self, app, caplog):
|
||||
"""Test that code parameter is redacted in request logs"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
# Set DEBUG level for logging
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_log_http_request(
|
||||
method="POST",
|
||||
url="https://indielogin.com/auth",
|
||||
data={"code": "sensitive_code_12345"},
|
||||
)
|
||||
|
||||
# Should log but with redacted code
|
||||
assert "sensitive_code_12345" not in caplog.text
|
||||
assert "sensit...********...2345" in caplog.text
|
||||
|
||||
def test_log_http_request_redacts_state(self, app, caplog):
|
||||
"""Test that state parameter is redacted in request logs"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_log_http_request(
|
||||
method="POST",
|
||||
url="https://indielogin.com/auth",
|
||||
data={"state": "state_token_123456789"},
|
||||
)
|
||||
|
||||
# Should log but with redacted state (8 chars shown at start)
|
||||
assert "state_token_123456789" not in caplog.text
|
||||
assert "state_to...********...6789" in caplog.text
|
||||
|
||||
def test_log_http_request_not_logged_at_info(self, app, caplog):
|
||||
"""Test that HTTP requests are not logged at INFO level"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.INFO)
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
_log_http_request(
|
||||
method="POST",
|
||||
url="https://indielogin.com/auth",
|
||||
data={"code": "test_code"},
|
||||
)
|
||||
|
||||
# Should not log anything
|
||||
assert "IndieAuth HTTP Request" not in caplog.text
|
||||
|
||||
def test_log_http_response_redacts_tokens(self, app, caplog):
|
||||
"""Test that response tokens are redacted"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_log_http_response(
|
||||
status_code=200,
|
||||
headers={"content-type": "application/json"},
|
||||
body='{"access_token": "secret_token_xyz789"}',
|
||||
)
|
||||
|
||||
# Should log but with redacted token
|
||||
assert "secret_token_xyz789" not in caplog.text
|
||||
assert "secret...********...z789" in caplog.text
|
||||
|
||||
def test_log_http_response_handles_non_json(self, app, caplog):
|
||||
"""Test that non-JSON responses are logged as-is"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_log_http_response(
|
||||
status_code=500, headers={}, body="Internal Server Error"
|
||||
)
|
||||
|
||||
# Should log the plain text body
|
||||
assert "Internal Server Error" in caplog.text
|
||||
|
||||
def test_log_http_response_redacts_sensitive_headers(self, app, caplog):
|
||||
"""Test that sensitive headers are redacted"""
|
||||
import logging
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
_log_http_response(
|
||||
status_code=200,
|
||||
headers={
|
||||
"content-type": "application/json",
|
||||
"set-cookie": "sensitive_cookie",
|
||||
"authorization": "Bearer token",
|
||||
},
|
||||
body='{"me": "https://example.com"}',
|
||||
)
|
||||
|
||||
# Should log content-type but not sensitive headers
|
||||
assert "content-type" in caplog.text
|
||||
assert "set-cookie" not in caplog.text
|
||||
assert "authorization" not in caplog.text
|
||||
assert "sensitive_cookie" not in caplog.text
|
||||
|
||||
|
||||
class TestLoggingIntegration:
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
def test_initiate_login_logs_at_debug(self, mock_discover, app, db, caplog):
|
||||
"""Test that initiate_login logs at DEBUG level"""
|
||||
import logging
|
||||
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
me_url = "https://example.com"
|
||||
initiate_login(me_url)
|
||||
|
||||
# Should see DEBUG logs
|
||||
assert "Validating me URL" in caplog.text
|
||||
assert "Generated state token" in caplog.text
|
||||
assert "Building authorization URL" in caplog.text
|
||||
|
||||
# Should see INFO log
|
||||
assert "Authentication initiated" in caplog.text
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
def test_initiate_login_info_level(self, mock_discover, app, db, caplog):
|
||||
"""Test that initiate_login only shows milestones at INFO level"""
|
||||
import logging
|
||||
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.app_context():
|
||||
app.logger.setLevel(logging.INFO)
|
||||
|
||||
with caplog.at_level(logging.INFO):
|
||||
me_url = "https://example.com"
|
||||
initiate_login(me_url)
|
||||
|
||||
# Should see INFO milestone
|
||||
assert "Authentication initiated" in caplog.text
|
||||
|
||||
# Should NOT see DEBUG details
|
||||
assert "Validating me URL" not in caplog.text
|
||||
assert "Generated state token" not in caplog.text
|
||||
|
||||
@patch("starpunk.auth.discover_endpoints")
|
||||
@patch("starpunk.auth.httpx.post")
|
||||
def test_handle_callback_logs_http_details(self, mock_post, mock_discover, app, db, client, caplog):
|
||||
"""Test that handle_callback logs HTTP request/response at DEBUG"""
|
||||
import logging
|
||||
|
||||
mock_discover.return_value = {
|
||||
'authorization_endpoint': 'https://auth.example.com/authorize',
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
with app.test_request_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
# Setup state token
|
||||
state = secrets.token_urlsafe(32)
|
||||
expires_at = datetime.utcnow() + timedelta(minutes=5)
|
||||
db.execute(
|
||||
"INSERT INTO auth_state (state, expires_at) VALUES (?, ?)",
|
||||
(state, expires_at),
|
||||
)
|
||||
db.commit()
|
||||
|
||||
# Mock authorization endpoint response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {"content-type": "application/json"}
|
||||
mock_response.text = '{"me": "https://example.com"}'
|
||||
mock_response.json.return_value = {"me": "https://example.com"}
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
code = "test_authorization_code"
|
||||
handle_callback(code, state)
|
||||
|
||||
# Should see HTTP request/response logs
|
||||
assert "IndieAuth HTTP Request" in caplog.text
|
||||
assert "IndieAuth HTTP Response" in caplog.text
|
||||
|
||||
# Code should be redacted
|
||||
assert "test_authorization_code" not in caplog.text
|
||||
assert "test_a...********...code" in caplog.text
|
||||
|
||||
def test_create_session_logs_details(self, app, db, client, caplog):
|
||||
"""Test that create_session logs session details at DEBUG"""
|
||||
import logging
|
||||
|
||||
with app.test_request_context():
|
||||
app.logger.setLevel(logging.DEBUG)
|
||||
|
||||
with caplog.at_level(logging.DEBUG):
|
||||
me = "https://example.com"
|
||||
create_session(me)
|
||||
|
||||
# Should see DEBUG logs
|
||||
assert "Session token generated" in caplog.text
|
||||
assert "Session expiry" in caplog.text
|
||||
assert "Request metadata" in caplog.text
|
||||
|
||||
# Should see INFO log
|
||||
assert "Session created" in caplog.text
|
||||
@@ -1,637 +0,0 @@
|
||||
"""
|
||||
Tests for external IndieAuth token verification with endpoint discovery
|
||||
|
||||
Tests cover:
|
||||
- Endpoint discovery from HTTP Link headers
|
||||
- Endpoint discovery from HTML link elements
|
||||
- Token verification with discovered endpoints
|
||||
- Caching behavior for endpoints and tokens
|
||||
- Error handling and edge cases
|
||||
- HTTPS validation
|
||||
- URL normalization
|
||||
|
||||
ADR: ADR-031 IndieAuth Endpoint Discovery Implementation
|
||||
"""
|
||||
|
||||
import hashlib
|
||||
import time
|
||||
from unittest.mock import Mock, patch
|
||||
import pytest
|
||||
import httpx
|
||||
|
||||
from starpunk.auth_external import (
|
||||
verify_external_token,
|
||||
discover_endpoints,
|
||||
check_scope,
|
||||
normalize_url,
|
||||
_parse_link_header,
|
||||
_parse_html_links,
|
||||
_cache,
|
||||
DiscoveryError,
|
||||
TokenVerificationError,
|
||||
ENDPOINT_CACHE_TTL,
|
||||
TOKEN_CACHE_TTL,
|
||||
)
|
||||
|
||||
|
||||
# Test Fixtures
|
||||
# -------------
|
||||
|
||||
@pytest.fixture
|
||||
def mock_profile_html():
|
||||
"""HTML profile with IndieAuth link elements"""
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="authorization_endpoint" href="https://auth.example.com/authorize">
|
||||
<link rel="token_endpoint" href="https://auth.example.com/token">
|
||||
<title>Test Profile</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello World</h1>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_profile_html_relative():
|
||||
"""HTML profile with relative URLs"""
|
||||
return """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="authorization_endpoint" href="/auth/authorize">
|
||||
<link rel="token_endpoint" href="/auth/token">
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_link_headers():
|
||||
"""HTTP Link headers with IndieAuth endpoints"""
|
||||
return (
|
||||
'<https://auth.example.com/authorize>; rel="authorization_endpoint", '
|
||||
'<https://auth.example.com/token>; rel="token_endpoint"'
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_token_response():
|
||||
"""Valid token verification response"""
|
||||
return {
|
||||
'me': 'https://alice.example.com/',
|
||||
'client_id': 'https://app.example.com/',
|
||||
'scope': 'create update',
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clear_cache():
|
||||
"""Clear cache before each test"""
|
||||
_cache.endpoints = None
|
||||
_cache.endpoints_expire = 0
|
||||
_cache.token_cache.clear()
|
||||
yield
|
||||
# Clear after test too
|
||||
_cache.endpoints = None
|
||||
_cache.endpoints_expire = 0
|
||||
_cache.token_cache.clear()
|
||||
|
||||
|
||||
# Endpoint Discovery Tests
|
||||
# -------------------------
|
||||
|
||||
def test_parse_link_header_both_endpoints(mock_link_headers):
|
||||
"""Parse Link header with both authorization and token endpoints"""
|
||||
endpoints = _parse_link_header(mock_link_headers, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints['authorization_endpoint'] == 'https://auth.example.com/authorize'
|
||||
assert endpoints['token_endpoint'] == 'https://auth.example.com/token'
|
||||
|
||||
|
||||
def test_parse_link_header_single_endpoint():
|
||||
"""Parse Link header with only token endpoint"""
|
||||
header = '<https://auth.example.com/token>; rel="token_endpoint"'
|
||||
endpoints = _parse_link_header(header, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints['token_endpoint'] == 'https://auth.example.com/token'
|
||||
assert 'authorization_endpoint' not in endpoints
|
||||
|
||||
|
||||
def test_parse_link_header_relative_url():
|
||||
"""Parse Link header with relative URL"""
|
||||
header = '</auth/token>; rel="token_endpoint"'
|
||||
endpoints = _parse_link_header(header, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints['token_endpoint'] == 'https://alice.example.com/auth/token'
|
||||
|
||||
|
||||
def test_parse_html_links_both_endpoints(mock_profile_html):
|
||||
"""Parse HTML with both authorization and token endpoints"""
|
||||
endpoints = _parse_html_links(mock_profile_html, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints['authorization_endpoint'] == 'https://auth.example.com/authorize'
|
||||
assert endpoints['token_endpoint'] == 'https://auth.example.com/token'
|
||||
|
||||
|
||||
def test_parse_html_links_relative_urls(mock_profile_html_relative):
|
||||
"""Parse HTML with relative endpoint URLs"""
|
||||
endpoints = _parse_html_links(
|
||||
mock_profile_html_relative,
|
||||
'https://alice.example.com/'
|
||||
)
|
||||
|
||||
assert endpoints['authorization_endpoint'] == 'https://alice.example.com/auth/authorize'
|
||||
assert endpoints['token_endpoint'] == 'https://alice.example.com/auth/token'
|
||||
|
||||
|
||||
def test_parse_html_links_empty():
|
||||
"""Parse HTML with no IndieAuth links"""
|
||||
html = '<html><head></head><body></body></html>'
|
||||
endpoints = _parse_html_links(html, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints == {}
|
||||
|
||||
|
||||
def test_parse_html_links_malformed():
|
||||
"""Parse malformed HTML gracefully"""
|
||||
html = '<html><head><link rel="token_endpoint"' # Missing closing tags
|
||||
endpoints = _parse_html_links(html, 'https://alice.example.com/')
|
||||
|
||||
# Should return empty dict, not crash
|
||||
assert isinstance(endpoints, dict)
|
||||
|
||||
|
||||
def test_parse_html_links_rel_as_list():
|
||||
"""Parse HTML where rel attribute is a list"""
|
||||
html = '''
|
||||
<html><head>
|
||||
<link rel="authorization_endpoint me" href="https://auth.example.com/authorize">
|
||||
</head></html>
|
||||
'''
|
||||
endpoints = _parse_html_links(html, 'https://alice.example.com/')
|
||||
|
||||
assert endpoints['authorization_endpoint'] == 'https://auth.example.com/authorize'
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_from_html(mock_get, app_with_admin_me, mock_profile_html):
|
||||
"""Discover endpoints from HTML link elements"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {'Content-Type': 'text/html'}
|
||||
mock_response.text = mock_profile_html
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
endpoints = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert endpoints['token_endpoint'] == 'https://auth.example.com/token'
|
||||
assert endpoints['authorization_endpoint'] == 'https://auth.example.com/authorize'
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_from_link_header(mock_get, app_with_admin_me, mock_link_headers):
|
||||
"""Discover endpoints from HTTP Link headers"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {
|
||||
'Content-Type': 'text/html',
|
||||
'Link': mock_link_headers
|
||||
}
|
||||
mock_response.text = '<html></html>'
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
endpoints = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert endpoints['token_endpoint'] == 'https://auth.example.com/token'
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_link_header_priority(mock_get, app_with_admin_me, mock_profile_html, mock_link_headers):
|
||||
"""Link headers take priority over HTML link elements"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {
|
||||
'Content-Type': 'text/html',
|
||||
'Link': '<https://different.example.com/token>; rel="token_endpoint"'
|
||||
}
|
||||
# HTML has different endpoint
|
||||
mock_response.text = mock_profile_html
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
endpoints = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Link header should win
|
||||
assert endpoints['token_endpoint'] == 'https://different.example.com/token'
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_no_token_endpoint(mock_get, app_with_admin_me):
|
||||
"""Raise error if no IndieAuth endpoints found"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {'Content-Type': 'text/html'}
|
||||
mock_response.text = '<html><head></head><body></body></html>'
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert 'No IndieAuth endpoints found' in str(exc_info.value)
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_http_error(mock_get, app_with_admin_me):
|
||||
"""Handle HTTP errors during discovery"""
|
||||
mock_get.side_effect = httpx.HTTPStatusError(
|
||||
"404 Not Found",
|
||||
request=Mock(),
|
||||
response=Mock(status_code=404)
|
||||
)
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert 'HTTP 404' in str(exc_info.value)
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_timeout(mock_get, app_with_admin_me):
|
||||
"""Handle timeout during discovery"""
|
||||
mock_get.side_effect = httpx.TimeoutException("Timeout")
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert 'Timeout' in str(exc_info.value)
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_network_error(mock_get, app_with_admin_me):
|
||||
"""Handle network errors during discovery"""
|
||||
mock_get.side_effect = httpx.NetworkError("Connection failed")
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert 'Network error' in str(exc_info.value)
|
||||
|
||||
|
||||
# HTTPS Validation Tests
|
||||
# -----------------------
|
||||
|
||||
def test_discover_endpoints_http_not_allowed_production(app_with_admin_me):
|
||||
"""HTTP profile URLs not allowed in production"""
|
||||
with app_with_admin_me.app_context():
|
||||
app_with_admin_me.config['DEBUG'] = False
|
||||
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('http://alice.example.com/')
|
||||
|
||||
assert 'HTTPS required' in str(exc_info.value)
|
||||
|
||||
|
||||
def test_discover_endpoints_http_allowed_debug(app_with_admin_me):
|
||||
"""HTTP profile URLs allowed in debug mode"""
|
||||
with app_with_admin_me.app_context():
|
||||
app_with_admin_me.config['DEBUG'] = True
|
||||
|
||||
# Should validate without raising (mock would be needed for full test)
|
||||
# Just test validation doesn't raise
|
||||
from starpunk.auth_external import _validate_profile_url
|
||||
_validate_profile_url('http://localhost:5000/')
|
||||
|
||||
|
||||
def test_discover_endpoints_localhost_not_allowed_production(app_with_admin_me):
|
||||
"""Localhost URLs not allowed in production"""
|
||||
with app_with_admin_me.app_context():
|
||||
app_with_admin_me.config['DEBUG'] = False
|
||||
|
||||
with pytest.raises(DiscoveryError) as exc_info:
|
||||
discover_endpoints('https://localhost/')
|
||||
|
||||
assert 'Localhost' in str(exc_info.value)
|
||||
|
||||
|
||||
# Caching Tests
|
||||
# -------------
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_caching(mock_get, app_with_admin_me, mock_profile_html):
|
||||
"""Discovered endpoints are cached"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {'Content-Type': 'text/html'}
|
||||
mock_response.text = mock_profile_html
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
# First call - should fetch
|
||||
endpoints1 = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Second call - should use cache
|
||||
endpoints2 = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Should only call httpx.get once
|
||||
assert mock_get.call_count == 1
|
||||
assert endpoints1 == endpoints2
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_cache_expiry(mock_get, app_with_admin_me, mock_profile_html):
|
||||
"""Endpoint cache expires after TTL"""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {'Content-Type': 'text/html'}
|
||||
mock_response.text = mock_profile_html
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
# First call
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Expire cache manually
|
||||
_cache.endpoints_expire = time.time() - 1
|
||||
|
||||
# Second call should fetch again
|
||||
discover_endpoints('https://alice.example.com/')
|
||||
|
||||
assert mock_get.call_count == 2
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_discover_endpoints_grace_period(mock_get, app_with_admin_me, mock_profile_html):
|
||||
"""Use expired cache on network failure (grace period)"""
|
||||
# First call succeeds
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {'Content-Type': 'text/html'}
|
||||
mock_response.text = mock_profile_html
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
endpoints1 = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Expire cache
|
||||
_cache.endpoints_expire = time.time() - 1
|
||||
|
||||
# Second call fails, but should use expired cache
|
||||
mock_get.side_effect = httpx.NetworkError("Connection failed")
|
||||
|
||||
endpoints2 = discover_endpoints('https://alice.example.com/')
|
||||
|
||||
# Should return cached endpoints despite network failure
|
||||
assert endpoints1 == endpoints2
|
||||
|
||||
|
||||
# Token Verification Tests
|
||||
# -------------------------
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_success(mock_get, mock_discover, app_with_admin_me, mock_token_response):
|
||||
"""Successfully verify token with discovered endpoint"""
|
||||
# Mock discovery
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
# Mock token verification
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = mock_token_response
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
token_info = verify_external_token('test-token-123')
|
||||
|
||||
assert token_info is not None
|
||||
assert token_info['me'] == 'https://alice.example.com/'
|
||||
assert token_info['scope'] == 'create update'
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_wrong_me(mock_get, mock_discover, app_with_admin_me):
|
||||
"""Reject token for different user"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
# Token for wrong user
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
'me': 'https://bob.example.com/', # Not ADMIN_ME
|
||||
'scope': 'create',
|
||||
}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
token_info = verify_external_token('test-token-123')
|
||||
|
||||
# Should reject
|
||||
assert token_info is None
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_401(mock_get, mock_discover, app_with_admin_me):
|
||||
"""Handle 401 Unauthorized from token endpoint"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 401
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
token_info = verify_external_token('invalid-token')
|
||||
|
||||
assert token_info is None
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_missing_me(mock_get, mock_discover, app_with_admin_me):
|
||||
"""Reject token response missing 'me' field"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
'scope': 'create',
|
||||
# Missing 'me' field
|
||||
}
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
token_info = verify_external_token('test-token')
|
||||
|
||||
assert token_info is None
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_retry_on_500(mock_get, mock_discover, app_with_admin_me, mock_token_response):
|
||||
"""Retry token verification on 500 server error"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
# First call: 500 error
|
||||
error_response = Mock()
|
||||
error_response.status_code = 500
|
||||
|
||||
# Second call: success
|
||||
success_response = Mock()
|
||||
success_response.status_code = 200
|
||||
success_response.json.return_value = mock_token_response
|
||||
|
||||
mock_get.side_effect = [error_response, success_response]
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
with patch('time.sleep'): # Skip sleep delay
|
||||
token_info = verify_external_token('test-token')
|
||||
|
||||
assert token_info is not None
|
||||
assert mock_get.call_count == 2
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_no_retry_on_403(mock_get, mock_discover, app_with_admin_me):
|
||||
"""Don't retry on 403 Forbidden (client error)"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 403
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
token_info = verify_external_token('test-token')
|
||||
|
||||
assert token_info is None
|
||||
# Should only call once (no retries)
|
||||
assert mock_get.call_count == 1
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
@patch('starpunk.auth_external.httpx.get')
|
||||
def test_verify_external_token_caching(mock_get, mock_discover, app_with_admin_me, mock_token_response):
|
||||
"""Token verifications are cached"""
|
||||
mock_discover.return_value = {
|
||||
'token_endpoint': 'https://auth.example.com/token'
|
||||
}
|
||||
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = mock_token_response
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with app_with_admin_me.app_context():
|
||||
# First call
|
||||
token_info1 = verify_external_token('test-token')
|
||||
|
||||
# Second call should use cache
|
||||
token_info2 = verify_external_token('test-token')
|
||||
|
||||
assert token_info1 == token_info2
|
||||
# Should only verify once
|
||||
assert mock_get.call_count == 1
|
||||
|
||||
|
||||
@patch('starpunk.auth_external.discover_endpoints')
|
||||
def test_verify_external_token_no_admin_me(mock_discover, app):
|
||||
"""Fail if ADMIN_ME not configured"""
|
||||
with app.app_context():
|
||||
# app fixture has no ADMIN_ME
|
||||
token_info = verify_external_token('test-token')
|
||||
|
||||
assert token_info is None
|
||||
# Should not even attempt discovery
|
||||
mock_discover.assert_not_called()
|
||||
|
||||
|
||||
# URL Normalization Tests
|
||||
# ------------------------
|
||||
|
||||
def test_normalize_url_removes_trailing_slash():
|
||||
"""Normalize URL removes trailing slash"""
|
||||
assert normalize_url('https://example.com/') == 'https://example.com'
|
||||
assert normalize_url('https://example.com') == 'https://example.com'
|
||||
|
||||
|
||||
def test_normalize_url_lowercase():
|
||||
"""Normalize URL converts to lowercase"""
|
||||
assert normalize_url('https://Example.COM/') == 'https://example.com'
|
||||
assert normalize_url('HTTPS://EXAMPLE.COM') == 'https://example.com'
|
||||
|
||||
|
||||
def test_normalize_url_path_preserved():
|
||||
"""Normalize URL preserves path"""
|
||||
assert normalize_url('https://example.com/path/') == 'https://example.com/path'
|
||||
assert normalize_url('https://Example.com/Path') == 'https://example.com/path'
|
||||
|
||||
|
||||
# Scope Checking Tests
|
||||
# ---------------------
|
||||
|
||||
def test_check_scope_present():
|
||||
"""Check scope returns True when scope is present"""
|
||||
assert check_scope('create', 'create update delete') is True
|
||||
assert check_scope('create', 'create') is True
|
||||
|
||||
|
||||
def test_check_scope_missing():
|
||||
"""Check scope returns False when scope is missing"""
|
||||
assert check_scope('create', 'update delete') is False
|
||||
assert check_scope('create', '') is False
|
||||
assert check_scope('create', 'created') is False # Partial match
|
||||
|
||||
|
||||
def test_check_scope_empty():
|
||||
"""Check scope handles empty scope string"""
|
||||
assert check_scope('create', '') is False
|
||||
assert check_scope('create', None) is False
|
||||
|
||||
|
||||
# Fixtures
|
||||
# --------
|
||||
|
||||
@pytest.fixture
|
||||
def app():
|
||||
"""Create test Flask app without ADMIN_ME"""
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
app.config['TESTING'] = True
|
||||
app.config['DEBUG'] = False
|
||||
return app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app_with_admin_me():
|
||||
"""Create test Flask app with ADMIN_ME configured"""
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
app.config['TESTING'] = True
|
||||
app.config['DEBUG'] = False
|
||||
app.config['ADMIN_ME'] = 'https://alice.example.com/'
|
||||
app.config['VERSION'] = '1.0.0-test'
|
||||
return app
|
||||
@@ -0,0 +1,243 @@
|
||||
"""
|
||||
Tests for OIDC admin login (starpunk/auth.py, v2.0.0)
|
||||
"""
|
||||
|
||||
import time
|
||||
from unittest.mock import MagicMock, patch
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import jwt
|
||||
import pytest
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
|
||||
from starpunk import auth
|
||||
from starpunk.auth import (
|
||||
InvalidStateError,
|
||||
OIDCError,
|
||||
UnauthorizedError,
|
||||
_pkce_challenge,
|
||||
handle_callback,
|
||||
initiate_login,
|
||||
verify_session,
|
||||
)
|
||||
|
||||
ISSUER = "https://id.example.com"
|
||||
CLIENT_ID = "starpunk-client"
|
||||
ADMIN_SUB = "admin-sub-123"
|
||||
|
||||
METADATA = {
|
||||
"issuer": ISSUER,
|
||||
"authorization_endpoint": f"{ISSUER}/authorize",
|
||||
"token_endpoint": f"{ISSUER}/api/oidc/token",
|
||||
"jwks_uri": f"{ISSUER}/.well-known/jwks.json",
|
||||
"id_token_signing_alg_values_supported": ["RS256"],
|
||||
}
|
||||
|
||||
PRIVATE_KEY = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
||||
OTHER_KEY = rsa.generate_private_key(public_exponent=65537, key_size=2048)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
from starpunk import create_app
|
||||
|
||||
data = tmp_path / "data"
|
||||
return create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
"OIDC_ISSUER": ISSUER,
|
||||
"OIDC_CLIENT_ID": CLIENT_ID,
|
||||
"OIDC_CLIENT_SECRET": "secret",
|
||||
"OIDC_ADMIN_SUB": ADMIN_SUB,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def provider():
|
||||
"""Fake identity provider: discovery, JWKS key and token endpoint"""
|
||||
auth._METADATA_CACHE.clear()
|
||||
metadata_response = MagicMock(status_code=200)
|
||||
metadata_response.json.return_value = METADATA
|
||||
metadata_response.raise_for_status.return_value = None
|
||||
with patch("starpunk.auth.httpx.get", return_value=metadata_response) as get, patch(
|
||||
"starpunk.auth._get_signing_key", return_value=PRIVATE_KEY.public_key()
|
||||
), patch("starpunk.auth.httpx.post") as post, patch(
|
||||
"starpunk.author_discovery.get_author_profile"
|
||||
):
|
||||
yield {"get": get, "post": post}
|
||||
|
||||
|
||||
def make_id_token(nonce, sub=ADMIN_SUB, aud=CLIENT_ID, iss=ISSUER, key=PRIVATE_KEY, exp_in=300):
|
||||
now = int(time.time())
|
||||
claims = {"iss": iss, "aud": aud, "sub": sub, "iat": now, "exp": now + exp_in, "nonce": nonce}
|
||||
return jwt.encode(claims, key, algorithm="RS256")
|
||||
|
||||
|
||||
def start_login(app, next_url=None):
|
||||
"""Run initiate_login and return (state, nonce) from the stored row"""
|
||||
with app.test_request_context():
|
||||
url = initiate_login(next_url)
|
||||
query = parse_qs(urlparse(url).query)
|
||||
return query["state"][0], query["nonce"][0], query
|
||||
|
||||
|
||||
def token_response(provider, id_token, status=200):
|
||||
response = MagicMock(status_code=status, text="")
|
||||
response.json.return_value = {"id_token": id_token, "access_token": "x"}
|
||||
provider["post"].return_value = response
|
||||
|
||||
|
||||
class TestInitiateLogin:
|
||||
def test_redirects_to_provider_with_pkce(self, app):
|
||||
with app.test_request_context():
|
||||
url = initiate_login()
|
||||
parsed = urlparse(url)
|
||||
query = parse_qs(parsed.query)
|
||||
assert f"{parsed.scheme}://{parsed.netloc}{parsed.path}" == METADATA["authorization_endpoint"]
|
||||
assert query["client_id"] == [CLIENT_ID]
|
||||
assert query["redirect_uri"] == ["https://blog.example.com/auth/callback"]
|
||||
assert query["response_type"] == ["code"]
|
||||
assert query["scope"] == ["openid"]
|
||||
assert query["code_challenge_method"] == ["S256"]
|
||||
assert query["state"][0] and query["nonce"][0]
|
||||
|
||||
def test_stores_verifier_matching_challenge(self, app):
|
||||
state, _, query = start_login(app)
|
||||
from starpunk.database import get_db
|
||||
|
||||
with app.app_context():
|
||||
row = get_db(app).execute(
|
||||
"SELECT code_verifier FROM oidc_login_state WHERE state = ?", (state,)
|
||||
).fetchone()
|
||||
assert _pkce_challenge(row["code_verifier"]) == query["code_challenge"][0]
|
||||
|
||||
def test_rejects_offsite_next_url(self, app):
|
||||
state, _, _ = start_login(app, "https://evil.example/steal")
|
||||
from starpunk.database import get_db
|
||||
|
||||
with app.app_context():
|
||||
row = get_db(app).execute(
|
||||
"SELECT next_url FROM oidc_login_state WHERE state = ?", (state,)
|
||||
).fetchone()
|
||||
assert row["next_url"] is None
|
||||
|
||||
def test_issuer_mismatch_fails(self, app, provider):
|
||||
provider["get"].return_value.json.return_value = {**METADATA, "issuer": "https://other"}
|
||||
with app.test_request_context(), pytest.raises(OIDCError):
|
||||
initiate_login()
|
||||
|
||||
|
||||
class TestHandleCallback:
|
||||
def test_success_creates_admin_session(self, app, provider):
|
||||
state, nonce, _ = start_login(app, "/admin/new")
|
||||
token_response(provider, make_id_token(nonce))
|
||||
|
||||
with app.test_request_context():
|
||||
session_token, next_url = handle_callback("code", state, ISSUER)
|
||||
assert verify_session(session_token)["me"] == "https://example.com"
|
||||
assert next_url == "/admin/new"
|
||||
|
||||
sent = provider["post"].call_args
|
||||
assert sent.kwargs["data"]["grant_type"] == "authorization_code"
|
||||
assert sent.kwargs["data"]["code_verifier"]
|
||||
assert sent.kwargs["auth"] == (CLIENT_ID, "secret")
|
||||
|
||||
def test_state_is_single_use(self, app, provider):
|
||||
state, nonce, _ = start_login(app)
|
||||
token_response(provider, make_id_token(nonce))
|
||||
with app.test_request_context():
|
||||
handle_callback("code", state)
|
||||
with pytest.raises(InvalidStateError):
|
||||
handle_callback("code", state)
|
||||
|
||||
def test_unknown_state(self, app):
|
||||
with app.test_request_context(), pytest.raises(InvalidStateError):
|
||||
handle_callback("code", "nope")
|
||||
|
||||
def test_wrong_subject_is_unauthorized(self, app, provider):
|
||||
state, nonce, _ = start_login(app)
|
||||
token_response(provider, make_id_token(nonce, sub="someone-else"))
|
||||
with app.test_request_context(), pytest.raises(UnauthorizedError):
|
||||
handle_callback("code", state)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[
|
||||
{"aud": "another-client"},
|
||||
{"iss": "https://evil.example"},
|
||||
{"key": OTHER_KEY},
|
||||
{"exp_in": -3600},
|
||||
],
|
||||
)
|
||||
def test_invalid_id_token_rejected(self, app, provider, kwargs):
|
||||
state, nonce, _ = start_login(app)
|
||||
token_response(provider, make_id_token(nonce, **kwargs))
|
||||
with app.test_request_context(), pytest.raises(OIDCError):
|
||||
handle_callback("code", state)
|
||||
|
||||
def test_nonce_mismatch_rejected(self, app, provider):
|
||||
state, _, _ = start_login(app)
|
||||
token_response(provider, make_id_token("wrong-nonce"))
|
||||
with app.test_request_context(), pytest.raises(OIDCError):
|
||||
handle_callback("code", state)
|
||||
|
||||
def test_callback_issuer_mismatch_rejected(self, app, provider):
|
||||
state, nonce, _ = start_login(app)
|
||||
token_response(provider, make_id_token(nonce))
|
||||
with app.test_request_context(), pytest.raises(OIDCError):
|
||||
handle_callback("code", state, "https://evil.example")
|
||||
|
||||
def test_token_endpoint_error(self, app, provider):
|
||||
state, _, _ = start_login(app)
|
||||
token_response(provider, None, status=400)
|
||||
with app.test_request_context(), pytest.raises(OIDCError):
|
||||
handle_callback("code", state)
|
||||
|
||||
|
||||
class TestRoutes:
|
||||
def test_login_page_shows_sso_button(self, app):
|
||||
response = app.test_client().get("/auth/login")
|
||||
assert response.status_code == 200
|
||||
assert b"Sign in with" in response.data
|
||||
assert b'name="me"' not in response.data
|
||||
|
||||
def test_login_post_redirects_to_provider(self, app):
|
||||
response = app.test_client().post("/auth/login")
|
||||
assert response.status_code == 302
|
||||
assert response.headers["Location"].startswith(METADATA["authorization_endpoint"])
|
||||
|
||||
def test_callback_sets_cookie_and_returns_to_next(self, app, provider):
|
||||
client = app.test_client()
|
||||
with client.session_transaction() as s:
|
||||
s["next"] = "https://blog.example.com/admin/tokens"
|
||||
login = client.post("/auth/login")
|
||||
query = parse_qs(urlparse(login.headers["Location"]).query)
|
||||
token_response(provider, make_id_token(query["nonce"][0]))
|
||||
|
||||
response = client.get(f"/auth/callback?code=abc&state={query['state'][0]}&iss={ISSUER}")
|
||||
assert response.status_code == 302
|
||||
assert response.headers["Location"] == "https://blog.example.com/admin/tokens"
|
||||
cookie = response.headers["Set-Cookie"]
|
||||
assert "starpunk_session=" in cookie and "HttpOnly" in cookie and "Secure" in cookie
|
||||
|
||||
def test_callback_wrong_user_gets_no_cookie(self, app, provider):
|
||||
client = app.test_client()
|
||||
login = client.post("/auth/login")
|
||||
query = parse_qs(urlparse(login.headers["Location"]).query)
|
||||
token_response(provider, make_id_token(query["nonce"][0], sub="intruder"))
|
||||
|
||||
response = client.get(f"/auth/callback?code=abc&state={query['state'][0]}")
|
||||
assert response.headers["Location"].endswith("/auth/login")
|
||||
assert "starpunk_session" not in response.headers.get("Set-Cookie", "")
|
||||
|
||||
def test_callback_provider_error(self, app):
|
||||
response = app.test_client().get("/auth/callback?error=access_denied")
|
||||
assert response.status_code == 302
|
||||
assert response.headers["Location"].endswith("/auth/login")
|
||||
+1
-1
@@ -230,7 +230,7 @@ class TestGenerateFeed:
|
||||
|
||||
# Check for Atom namespace and self-link
|
||||
assert "atom" in feed_xml
|
||||
assert "feed.xml" in feed_xml
|
||||
assert "feed.rss" in feed_xml # self link (v2.10.0: /feed.rss is the RSS topic)
|
||||
assert 'rel="self"' in feed_xml
|
||||
|
||||
def test_generate_feed_item_structure(self, app, sample_notes):
|
||||
|
||||
@@ -0,0 +1,329 @@
|
||||
"""
|
||||
Tests for the built-in IndieAuth server (starpunk/indieauth.py, v2.0.0)
|
||||
"""
|
||||
|
||||
import base64
|
||||
import hashlib
|
||||
import secrets
|
||||
from unittest.mock import MagicMock, patch
|
||||
from urllib.parse import parse_qs, urlencode, urlparse
|
||||
|
||||
import pytest
|
||||
|
||||
from starpunk.auth import create_session
|
||||
from starpunk.indieauth import (
|
||||
AuthorizationRequestError,
|
||||
InvalidGrantError,
|
||||
check_scope,
|
||||
create_access_token,
|
||||
create_authorization_code,
|
||||
fetch_client_redirect_uris,
|
||||
redeem_authorization_code,
|
||||
validate_authorization_request,
|
||||
verify_access_token,
|
||||
)
|
||||
|
||||
CLIENT_ID = "https://quill.example/"
|
||||
REDIRECT_URI = "https://quill.example/auth/callback"
|
||||
|
||||
|
||||
def challenge_for(verifier):
|
||||
digest = hashlib.sha256(verifier.encode()).digest()
|
||||
return base64.urlsafe_b64encode(digest).rstrip(b"=").decode()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def app(tmp_path):
|
||||
from starpunk import create_app
|
||||
|
||||
data = tmp_path / "data"
|
||||
return create_app(
|
||||
{
|
||||
"TESTING": True,
|
||||
"SITE_URL": "https://blog.example.com",
|
||||
"ADMIN_ME": "https://example.com",
|
||||
"SESSION_SECRET": "test-secret",
|
||||
"DATA_PATH": data,
|
||||
"NOTES_PATH": data / "notes",
|
||||
"DATABASE_PATH": data / "starpunk.db",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(app):
|
||||
return app.test_client()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin_client(app):
|
||||
"""Test client with a valid admin session cookie"""
|
||||
client = app.test_client()
|
||||
with app.test_request_context():
|
||||
token = create_session("https://example.com")
|
||||
client.set_cookie("starpunk_session", token)
|
||||
return client
|
||||
|
||||
|
||||
def auth_params(verifier="v" * 50, **overrides):
|
||||
params = {
|
||||
"response_type": "code",
|
||||
"client_id": CLIENT_ID,
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"state": "client-state",
|
||||
"code_challenge": challenge_for(verifier),
|
||||
"code_challenge_method": "S256",
|
||||
"scope": "create media",
|
||||
}
|
||||
params.update(overrides)
|
||||
return params
|
||||
|
||||
|
||||
def approve(admin_client, granted=("create", "media"), **overrides):
|
||||
form = auth_params(**overrides)
|
||||
form["decision"] = "approve"
|
||||
form["scope_granted"] = list(granted)
|
||||
response = admin_client.post("/auth/authorize/approve", data=form)
|
||||
assert response.status_code == 302
|
||||
return parse_qs(urlparse(response.headers["Location"]).query)
|
||||
|
||||
|
||||
class TestValidation:
|
||||
def test_valid_request(self, app):
|
||||
with app.test_request_context():
|
||||
result = validate_authorization_request(auth_params())
|
||||
assert result["scopes"] == ["create", "media"]
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"overrides",
|
||||
[{"client_id": "not-a-url"}, {"redirect_uri": ""}, {"client_id": "ftp://x.example/"}],
|
||||
)
|
||||
def test_untrusted_client_errors_are_not_redirected(self, app, overrides):
|
||||
with app.test_request_context(), pytest.raises(AuthorizationRequestError) as e:
|
||||
validate_authorization_request(auth_params(**overrides))
|
||||
assert e.value.redirectable is False
|
||||
|
||||
def test_missing_pkce_is_redirectable_error(self, app):
|
||||
with app.test_request_context(), pytest.raises(AuthorizationRequestError) as e:
|
||||
validate_authorization_request(auth_params(code_challenge=""))
|
||||
assert e.value.redirectable is True
|
||||
|
||||
def test_plain_pkce_rejected(self, app):
|
||||
with app.test_request_context(), pytest.raises(AuthorizationRequestError):
|
||||
validate_authorization_request(auth_params(code_challenge_method="plain"))
|
||||
|
||||
def test_cross_origin_redirect_needs_client_registration(self, app):
|
||||
params = auth_params(redirect_uri="https://evil.example/cb")
|
||||
with app.test_request_context(), patch(
|
||||
"starpunk.indieauth.fetch_client_redirect_uris", return_value=[]
|
||||
), pytest.raises(AuthorizationRequestError) as e:
|
||||
validate_authorization_request(params)
|
||||
assert e.value.redirectable is False
|
||||
|
||||
def test_cross_origin_redirect_allowed_when_published(self, app):
|
||||
params = auth_params(redirect_uri="app://callback")
|
||||
with app.test_request_context(), patch(
|
||||
"starpunk.indieauth.fetch_client_redirect_uris", return_value=["app://callback"]
|
||||
):
|
||||
assert validate_authorization_request(params)["redirect_uri"] == "app://callback"
|
||||
|
||||
|
||||
class TestClientMetadata:
|
||||
def _response(self, content_type, body, links=None):
|
||||
response = MagicMock(status_code=200, text=body, url="https://quill.example/")
|
||||
response.headers = {"Content-Type": content_type}
|
||||
response.links = links or {}
|
||||
response.json.return_value = __import__("json").loads(body) if "json" in content_type else None
|
||||
return response
|
||||
|
||||
def test_json_metadata(self, app):
|
||||
body = '{"client_id": "https://quill.example/", "redirect_uris": ["app://cb"]}'
|
||||
with app.test_request_context(), patch(
|
||||
"starpunk.webmention_ssrf.validate_url_for_fetch"
|
||||
), patch("starpunk.indieauth.httpx.get", return_value=self._response("application/json", body)):
|
||||
assert fetch_client_redirect_uris(CLIENT_ID) == ["app://cb"]
|
||||
|
||||
def test_html_link(self, app):
|
||||
body = '<html><head><link rel="redirect_uri" href="/cb"></head></html>'
|
||||
with app.test_request_context(), patch(
|
||||
"starpunk.webmention_ssrf.validate_url_for_fetch"
|
||||
), patch("starpunk.indieauth.httpx.get", return_value=self._response("text/html", body)):
|
||||
assert fetch_client_redirect_uris(CLIENT_ID) == ["https://quill.example/cb"]
|
||||
|
||||
|
||||
class TestCodesAndTokens:
|
||||
def test_full_exchange(self, app):
|
||||
verifier = secrets.token_urlsafe(50)
|
||||
with app.test_request_context():
|
||||
code = create_authorization_code(CLIENT_ID, REDIRECT_URI, "create", challenge_for(verifier))
|
||||
result = redeem_authorization_code(code, CLIENT_ID, REDIRECT_URI, verifier)
|
||||
assert result == {"me": "https://example.com", "scope": "create", "client_id": CLIENT_ID}
|
||||
|
||||
token = create_access_token(CLIENT_ID, "create")
|
||||
info = verify_access_token(token["access_token"])
|
||||
assert info["me"] == "https://example.com"
|
||||
assert info["scope"] == "create"
|
||||
|
||||
def test_code_single_use(self, app):
|
||||
verifier = "v" * 50
|
||||
with app.test_request_context():
|
||||
code = create_authorization_code(CLIENT_ID, REDIRECT_URI, "create", challenge_for(verifier))
|
||||
redeem_authorization_code(code, CLIENT_ID, REDIRECT_URI, verifier)
|
||||
with pytest.raises(InvalidGrantError):
|
||||
redeem_authorization_code(code, CLIENT_ID, REDIRECT_URI, verifier)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"client_id,redirect_uri,verifier",
|
||||
[
|
||||
("https://other.example/", REDIRECT_URI, "v" * 50),
|
||||
(CLIENT_ID, "https://quill.example/other", "v" * 50),
|
||||
(CLIENT_ID, REDIRECT_URI, "wrong-verifier"),
|
||||
],
|
||||
)
|
||||
def test_code_mismatch_rejected(self, app, client_id, redirect_uri, verifier):
|
||||
with app.test_request_context():
|
||||
code = create_authorization_code(CLIENT_ID, REDIRECT_URI, "create", challenge_for("v" * 50))
|
||||
with pytest.raises(InvalidGrantError):
|
||||
redeem_authorization_code(code, client_id, redirect_uri, verifier)
|
||||
|
||||
def test_token_stored_hashed(self, app):
|
||||
from starpunk.database import get_db
|
||||
|
||||
with app.test_request_context():
|
||||
token = create_access_token(CLIENT_ID, "create")["access_token"]
|
||||
rows = get_db(app).execute("SELECT token_hash FROM indieauth_tokens").fetchall()
|
||||
assert token not in [r["token_hash"] for r in rows]
|
||||
|
||||
def test_unknown_token(self, app):
|
||||
with app.test_request_context():
|
||||
assert verify_access_token("nope") is None
|
||||
assert verify_access_token("") is None
|
||||
|
||||
def test_check_scope(self):
|
||||
assert check_scope("create", "create media")
|
||||
assert not check_scope("create", "media")
|
||||
assert not check_scope("create", "")
|
||||
|
||||
|
||||
class TestEndpoints:
|
||||
def test_metadata(self, client):
|
||||
data = client.get("/.well-known/oauth-authorization-server").get_json()
|
||||
assert data["issuer"] == "https://blog.example.com/"
|
||||
assert data["authorization_endpoint"] == "https://blog.example.com/auth/authorize"
|
||||
assert data["token_endpoint"] == "https://blog.example.com/auth/token"
|
||||
assert data["code_challenge_methods_supported"] == ["S256"]
|
||||
|
||||
def test_discovery_links_on_homepage(self, client):
|
||||
html = client.get("/").data.decode()
|
||||
assert 'rel="indieauth-metadata"' in html
|
||||
assert 'rel="micropub"' in html
|
||||
|
||||
def test_authorize_requires_login(self, client):
|
||||
response = client.get("/auth/authorize?" + urlencode(auth_params()))
|
||||
assert response.status_code == 302
|
||||
assert response.headers["Location"].endswith("/auth/login")
|
||||
with client.session_transaction() as s:
|
||||
assert "/auth/authorize?" in s["next"]
|
||||
|
||||
def test_authorize_shows_consent(self, admin_client):
|
||||
response = admin_client.get("/auth/authorize?" + urlencode(auth_params()))
|
||||
assert response.status_code == 200
|
||||
assert b"quill.example" in response.data
|
||||
assert b'value="create"' in response.data
|
||||
|
||||
def test_authorize_bad_client_shows_error_page(self, admin_client):
|
||||
response = admin_client.get("/auth/authorize?" + urlencode(auth_params(client_id="nope")))
|
||||
assert response.status_code == 400
|
||||
|
||||
def test_approve_requires_login(self, client):
|
||||
response = client.post("/auth/authorize/approve", data={**auth_params(), "decision": "approve"})
|
||||
assert response.headers["Location"].endswith("/auth/login")
|
||||
|
||||
def test_deny(self, admin_client):
|
||||
form = {**auth_params(), "decision": "deny"}
|
||||
query = parse_qs(urlparse(admin_client.post("/auth/authorize/approve", data=form).headers["Location"]).query)
|
||||
assert query["error"] == ["access_denied"]
|
||||
assert query["state"] == ["client-state"]
|
||||
|
||||
def test_full_micropub_flow(self, admin_client, client):
|
||||
verifier = "v" * 50
|
||||
query = approve(admin_client, granted=["create"])
|
||||
assert query["state"] == ["client-state"]
|
||||
assert query["iss"] == ["https://blog.example.com/"]
|
||||
|
||||
response = client.post(
|
||||
"/auth/token",
|
||||
data={
|
||||
"grant_type": "authorization_code",
|
||||
"code": query["code"][0],
|
||||
"client_id": CLIENT_ID,
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"code_verifier": verifier,
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert response.headers["Cache-Control"] == "no-store"
|
||||
body = response.get_json()
|
||||
assert body["me"] == "https://example.com"
|
||||
assert body["scope"] == "create" # media was unchecked
|
||||
|
||||
token = body["access_token"]
|
||||
micropub = client.post(
|
||||
"/micropub",
|
||||
data={"h": "entry", "content": "Posted through the built-in IndieAuth server"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
assert micropub.status_code == 201
|
||||
|
||||
verify = client.get("/auth/token", headers={"Authorization": f"Bearer {token}"})
|
||||
assert verify.get_json()["client_id"] == CLIENT_ID
|
||||
|
||||
client.post("/auth/revoke", data={"token": token})
|
||||
again = client.post(
|
||||
"/micropub",
|
||||
data={"h": "entry", "content": "should fail"},
|
||||
headers={"Authorization": f"Bearer {token}"},
|
||||
)
|
||||
assert again.status_code == 401
|
||||
|
||||
def test_profile_only_code_cannot_get_token(self, admin_client, client):
|
||||
query = approve(admin_client, granted=[], scope="")
|
||||
form = {
|
||||
"grant_type": "authorization_code",
|
||||
"code": query["code"][0],
|
||||
"client_id": CLIENT_ID,
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"code_verifier": "v" * 50,
|
||||
}
|
||||
assert client.post("/auth/token", data=form).get_json()["error"] == "invalid_grant"
|
||||
|
||||
def test_profile_only_redemption(self, admin_client, client):
|
||||
query = approve(admin_client, granted=[], scope="")
|
||||
response = client.post(
|
||||
"/auth/authorize",
|
||||
data={
|
||||
"grant_type": "authorization_code",
|
||||
"code": query["code"][0],
|
||||
"client_id": CLIENT_ID,
|
||||
"redirect_uri": REDIRECT_URI,
|
||||
"code_verifier": "v" * 50,
|
||||
},
|
||||
)
|
||||
assert response.get_json() == {"me": "https://example.com"}
|
||||
|
||||
def test_unsupported_grant(self, client):
|
||||
assert client.post("/auth/token", data={"grant_type": "password"}).status_code == 400
|
||||
|
||||
def test_admin_can_list_and_revoke(self, app, admin_client):
|
||||
with app.test_request_context():
|
||||
token = create_access_token(CLIENT_ID, "create")["access_token"]
|
||||
page = admin_client.get("/admin/tokens")
|
||||
assert page.status_code == 200 and b"quill.example" in page.data
|
||||
|
||||
from starpunk.database import get_db
|
||||
|
||||
with app.app_context():
|
||||
token_id = get_db(app).execute("SELECT id FROM indieauth_tokens").fetchone()["id"]
|
||||
admin_client.post(f"/admin/tokens/{token_id}/revoke")
|
||||
with app.test_request_context():
|
||||
assert verify_access_token(token) is None
|
||||
Loaded 100 of 125 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user