Files
poorBird/server/deploy/poorbird.env.example
Thomas KarnerandClaude Opus 5 735cbcc5a0 Add the captures API and auth-checked image serving (SPEC 3.3)
The server half of the gallery. Listing and delivery land together because
neither is useful alone: a listing with no way to fetch bytes renders empty
tiles, and image serving with nothing to enumerate has no entry point.

Delivery follows the design deploy/Caddyfile already committed to. The app
authorises, then answers empty with X-Accel-Redirect and Caddy reads the file,
so a 3 MB JPEG never occupies a Python worker thread. There is no Caddy in
development, though, and that header reaching a browser is an empty 200 - every
image in the gallery blank, with nothing to explain it. So
POORBIRD_SERVE_IMAGES_DIRECTLY makes the app send the bytes instead.

The important part is what that flag does NOT do: the authorisation is the same
code either way, and the flag only changes how an already-authorised response
is finished. A separate unauthenticated file route "only reachable in dev" is
exactly the kind of thing that survives into production because it works, and
this deliberately is not one.

Both delivery paths refuse a path that escapes its root. captures.path is
written by our own ingest code so it should never happen, but "should never" is
not a security control and the cost of being wrong is serving an arbitrary file
off the server. The handoff path needs it just as much as the direct one - a
traversal in the header makes *Caddy* read the file, where the app never opens
it at all.

Keyset paging, not OFFSET. At 5000 captures a day OFFSET gets slower the
further back you scroll, which is the direction a gallery scrolls. The cursor
carries (received_at, id), and the id is in there for a specific reason: a
burst arrives inside the same fraction of a second, so ties are normal rather
than exotic, and a timestamp-only cursor would either repeat rows or skip them.
A gallery that loses a frame while you scroll is one you cannot trust.

Page size is fetched as limit + 1 rather than counted. It answers "is there
another page" without a COUNT over the whole filtered set, and the test for a
full final page covers the off-by-one that trick invites.

Filters cover the columns that have data: device, received-date range, kept,
reviewed, pre_trigger, trigger. Taxon and tag filters are in TODO Phase 3 and
deliberately absent - both need a join, and neither has anything to join to
until the observation editor and a seeded taxa table exist. A filter that can
only ever return nothing is worse than an absent one, because the UI offers it.

Dates filter on received_at and never captured_at_device, which is NULL
whenever the camera's clock was unsynced (API.md, X-Captured-At: 0). Filtering
on it would silently drop exactly those frames.

A missing thumbnail is 404 rather than a fallback to the full JPEG. The grid
needs to tell "queued" from "never decoded" and show a placeholder; a silent
fallback would make 200 tiles fetch a hundred times the bytes and still look
like it worked.

`poorbird check` now verifies the layout the handoff depends on: Caddy serves
the PARENT of both roots, so they have to be siblings named images/ and
thumbs/. Getting it wrong produces 404s from Caddy with nothing in the app's
log, which is the kind of thing worth failing a preflight check over rather
than discovering in a browser.

One test-quality fix worth naming: two delivery tests were inheriting their
mode from whatever server/.env happened to say, and broke the moment
SERVE_IMAGES_DIRECTLY=true was added there for the dev server's benefit. Both
modes are now named explicitly by a fixture, and conftest pins the production
default - a test that changes its answer because of an untracked file is not
testing the code.

Verified against the real server and the six real captures already in the dev
database: 401 before signing in, two pages of three with no overlap and correct
termination, device_name garden-feeder on the detail, a 1572-byte WebP with
RIFF magic and a 3469-byte JPEG with FFD8FF magic, and 401 on the image again
immediately after logout.

Mutation-tested the two claims that matter: removing the traversal guard and
reducing the cursor to a timestamp each failed exactly the tests written for
them. The traversal test escapes to a canary file that really exists just
outside the root rather than to /etc/passwd, which does not exist on this
machine and would have let the test pass for the wrong reason.

Tests: 257 -> 293.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-21 19:06:59 +02:00

97 lines
4.6 KiB
Bash

# poorBird configuration.
#
# Install to /etc/poorbird/poorbird.env, owned by root:poorbird, mode 0640.
# systemd loads it via EnvironmentFile. This same file becomes a compose .env
# unchanged if containers ever happen (TODO.md Phase 1.2).
#
# Note: systemd's EnvironmentFile does NOT do shell quoting or expansion.
# Write VALUES literally, with no surrounding quotes.
# --- database ---------------------------------------------------------------
# Loopback TCP with scram-sha-256, deliberately not a Unix socket.
# Required: the app has no built-in default and refuses to start without it.
# The literal CHANGE_ME below is rejected at startup, so you cannot copy this
# file and forget to edit it.
POORBIRD_DATABASE_URL=postgresql+psycopg://poorbird:CHANGE_ME@127.0.0.1:5432/poorbird
#
# Connection pool. The API and the worker are separate processes and each gets
# its own pool, so the most either can open is POOL_SIZE + MAX_OVERFLOW and the
# most both together can open is twice that -- keep it under PostgreSQL's
# max_connections (100 by default). The worker opens connections lazily and
# needs about three, so the size below is really sizing the API.
POORBIRD_DB_POOL_SIZE=15
POORBIRD_DB_MAX_OVERFLOW=10
# --- http -------------------------------------------------------------------
# Bind to loopback only; Caddy is the only thing that talks to it.
# Caddy itself takes :8080 for the camera and :443 for humans, so uvicorn sits
# behind on 8090. See deploy/Caddyfile.
POORBIRD_API_HOST=127.0.0.1
POORBIRD_API_PORT=8090
# --- storage ----------------------------------------------------------------
# Read by the app AND referenced by the Caddyfile: the auth-checked image
# handoff needs both to agree on this path (SPEC 3.3).
POORBIRD_IMAGE_ROOT=/srv/poorbird/images
POORBIRD_THUMB_ROOT=/srv/poorbird/thumbs
POORBIRD_STATE_DIR=/var/lib/poorbird
# Who copies the image bytes (SPEC 3.3).
#
# Leave this FALSE in production. The app authorises the request and answers
# with X-Accel-Redirect, and Caddy reads the file, so a 3 MB JPEG never
# occupies a Python worker thread.
#
# Set it true ONLY for local development, where there is no Caddy to honour
# that header and every image in the gallery would otherwise arrive as an
# empty 200. It does NOT relax the authorisation -- the same check runs either
# way; it only changes how an already-authorised response is finished.
#
# NOTE: with this false, IMAGE_ROOT and THUMB_ROOT must be siblings named
# images/ and thumbs/ under the directory the Caddyfile's file_server serves
# (deploy/Caddyfile, the protected_files snippet). `poorbird check` verifies
# that, because getting it wrong produces 404s from Caddy and nothing in the
# app's log.
POORBIRD_SERVE_IMAGES_DIRECTLY=false
# --- devices ----------------------------------------------------------------
# Shared bootstrap secret the firmware presents in X-Enroll-Secret.
# Generate with: openssl rand -hex 32
#
# WARNING (firmware/API.md): this value is compiled into the firmware image as
# the last-resort recovery path for a camera that cannot be reached over USB.
# Rotating it here WITHOUT shipping new firmware removes that escape hatch.
POORBIRD_ENROLL_SECRET=
# --- user sessions ----------------------------------------------------------
# How long a signed-in browser stays signed in. SPEC 3.4 makes this a
# UI-editable server setting eventually; for now it is here.
POORBIRD_SESSION_LIFETIME_H=336
# Leave this TRUE in production. Caddy always terminates HTTPS, so the cookie
# can and must be Secure.
#
# Set it false ONLY for local development against the Vite dev server, which is
# plain http on localhost. A Secure cookie is silently discarded by the browser
# over http, and the symptom is "login returns 200 but I am never signed in",
# with nothing in any log to explain it. That is the only reason this is a
# setting rather than a constant.
POORBIRD_SESSION_COOKIE_SECURE=true
# --- rate limiting ------------------------------------------------------------
# In-process token buckets. Sized for ONE uvicorn process: if you ever run the
# API with workers, each gets its own buckets and the effective limit
# multiplies. Set false only for local development.
POORBIRD_RATE_LIMIT_ENABLED=true
# --- worker -----------------------------------------------------------------
# How long to wait after finding the queue empty. Does not delay a queue that
# already has work: the worker drains that without pausing.
POORBIRD_WORKER_POLL_INTERVAL_S=5
POORBIRD_JOB_MAX_ATTEMPTS=3
# --- logging ----------------------------------------------------------------
POORBIRD_LOG_LEVEL=INFO
# text for journalctl by eye, json once you are shipping logs somewhere.
POORBIRD_LOG_FORMAT=text