signal-bot/signal_bot/manage/deps.py
Krzysztof kuhy Rudnicki 0b539fc327 feat: the management backend, absorbing the tagger
A second process rather than more routes on the bot's own app. This one can
rewrite .env, and the bot's port already serves /_selftest, which posts for
real into a real group; keeping them apart means the surface that can
reconfigure the bot only listens while somebody has the page open.

The tagger's seven routes move here unchanged, out of tool/ and into the
coverage gate -- which is the reason an unconditional ImportError once sat
in tagger.py through two releases. The media route is still indexed by
integer, and that is not a style choice: a filename from a client is a path,
and the directory holds private photographs. FastAPI refuses a name with 422
before the handler runs, which the tests assert.

guard.py is the part that was not asked for. Binding to loopback stops the
network, not the browser already running on this machine, and two attacks
reach a loopback port from an ordinary web page: a cross-origin form, which
can only send urlencoded, multipart or plain text, so requiring
application/json refuses it with no token to keep; and DNS rebinding, whose
request still carries the attacker's hostname in Host. Forty lines instead
of authentication, which is the right trade for something started by hand
and stopped again a few minutes later.

assets.py branches on whether the page has been built, because StaticFiles
raises at construction on a missing directory -- mounting it unconditionally
would take the app down on a fresh clone, and in CI, where pytest never runs
pnpm build. Both branches are tested; "not built" answers with the command.

The container restart goes through signal_bot.external, wrapped in
asyncio.to_thread: it is synchronous with a 60s default, and blocking the
loop would freeze the page showing the restart.

100% branch coverage across all eleven modules, no omits, no pragmas.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01416TegJ3sa8xL5qGGgrygP
2026-08-31 14:38:24 +02:00

50 lines
1.5 KiB
Python

# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence.
"""Everything the management app is pointed at, as one value.
Every path is a field rather than a module constant. That is what lets the
tests point the whole app at ``tmp_path`` and never go near the real ``.env``
(mode 0600, holding the live account) or the real album (mode 0700, holding
photographs of people who did not agree to be in a test fixture).
"""
from __future__ import annotations
from dataclasses import dataclass
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from pathlib import Path
import httpx
from signal_bot.config import Config
from signal_bot.pack_album import Album
@dataclass(frozen=True, slots=True)
class ManageDeps:
"""What one running management page is bound to."""
config: Config
"""The bot's own configuration, already validated by load_config."""
env_path: Path
"""The .env the page edits. Not derived: the tests point it elsewhere."""
dist_dir: Path
"""Where `pnpm build` puts the page. Absent until it has been run."""
album: Album
"""The one pack this session tags, chosen by --dir."""
client: httpx.AsyncClient
"""Shared, so the proxied /healthz and the group listing pool sockets."""
port: int
"""The port this app is served on, so the Host guard knows what to allow."""
@property
def bot_url(self) -> str:
"""The bot's own loopback base URL, which this app proxies to."""
return f"http://{self.config.bind_host}:{self.config.bind_port}"