The E22 redactor is a safety net over addresses and credential params, and every one of these call sites was leaning on it to do the whole job. What it does not cover went in whole, and what it does cover came out as a constant. Not covered, so a real leak: - routes/search.py logged the raw search term at ERROR. The privacy policy shipped in 0.88.0 says a search string is kept only at Teljes/TRACE, with the user's own consent. The ERROR line now carries the shape of the failure (source, candidate and collected counts) and the term follows on a TRACE line, which nothing emits unless that consent is in force. - downloads/backfill.py logged the full source URL at INFO — a row of the user's download history. Now the asset id and the host. - youtube/client.py logged the upstream error body, which is prose that quotes the request back. Every failing YouTube call now goes through one _upstream_failure() that logs the API's `reason` code, keeps the body for the same consented TRACE line, and raises a YouTubeError carrying `reason` as a field — callers put that error in log lines of their own (routes/channels.py) and in a 422 answered to the user, so the body must not travel in it either. Covered, so merely useless: routes/auth.py named the account by an address that reached both sinks as `[email]`, and app/email.py printed recipient lists that read as `['[email]']`. Both now carry the uid (or a count) and a reason code, which is also the first half of E22.5 S4's failed-sign-in coverage. routes/channels.py branched on the text of that error message to recognise an already-existing subscription. It now branches on `exc.reason`, with the substring check kept beside it for a body that carried no reason at all. tests/test_log_pii.py pins the call sites and the family: a source-level contract test walks every log call in app/ and fails on an argument naming user-typed content unless the line says why it is safe (`# log-pii-ok: <why>`). Eight sites carry that marker today, each with its reason. Gate 8/8 green; all five cases mutation-checked.
350 lines
16 KiB
Python
350 lines
16 KiB
Python
import asyncio
|
|
import logging
|
|
import math
|
|
from contextlib import asynccontextmanager, suppress
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI, HTTPException, Request
|
|
from fastapi.encoders import jsonable_encoder
|
|
from fastapi.exceptions import RequestValidationError
|
|
|
|
# Logging is configured HERE, before the route modules are imported, so every logger they create at
|
|
# import time is already covered. ONE format for the api, the worker, the download child and alembic
|
|
# alike (app/logs.py) — this replaces the old uvicorn --log-config + the hand-rolled fallback that
|
|
# used to live here, and uvicorn's access log is replaced by the `http.request` middleware below.
|
|
from app.logs import configure_logging, current_level, log_requests, set_level
|
|
|
|
configure_logging("api")
|
|
|
|
log = logging.getLogger("siftlode.app")
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
from sqlalchemy.exc import DataError
|
|
from starlette.middleware.sessions import SessionMiddleware
|
|
|
|
from app import logsink
|
|
from app import state
|
|
from app import sysconfig
|
|
from app.config import settings
|
|
from app.db import SessionLocal
|
|
from app.plex import stream as plex_stream
|
|
from app.plex import subtitles as plex_subtitles
|
|
from app.routes import (
|
|
admin,
|
|
audit as audit_routes,
|
|
auth as auth_routes,
|
|
channels,
|
|
config as config_routes,
|
|
diagnostics as diagnostics_routes,
|
|
discover as discover_routes,
|
|
downloads,
|
|
feed,
|
|
health,
|
|
me,
|
|
messages,
|
|
notifications,
|
|
playlists,
|
|
plex as plex_routes,
|
|
public as public_routes,
|
|
quota,
|
|
saved_views,
|
|
scheduler as scheduler_routes,
|
|
search as search_routes,
|
|
setup as setup_routes,
|
|
sync,
|
|
tags,
|
|
version,
|
|
)
|
|
from app.scheduler import shutdown_scheduler, start_scheduler
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
log.info("Siftlode starting up")
|
|
# The stored copy of the log (log_events). The entrypoint has already migrated the schema.
|
|
logsink.start()
|
|
# First-run: if the instance isn't configured yet, mint a fresh one-time setup token and print
|
|
# the wizard URL so the operator can open it (the app runs in setup mode until they finish).
|
|
with SessionLocal() as db:
|
|
# The admin's runtime level (sysconfig `log_level`) wins over the LOG_LEVEL bootstrap
|
|
# default, so a level chosen in the UI survives a restart without touching the environment.
|
|
# Best-effort on purpose: a LOGGING knob must never be the thing that stops the app from
|
|
# booting — an unreachable or not-yet-migrated database has to start into the env level.
|
|
try:
|
|
set_level(sysconfig.get_str(db, "log_level"))
|
|
except Exception as exc: # noqa: BLE001 — any DB/config failure keeps the bootstrap level
|
|
log.warning(
|
|
"could not read the stored log level (%s); staying at %s", exc, current_level()
|
|
)
|
|
if not state.is_configured(db):
|
|
token = state.rotate_setup_token(db)
|
|
url = f"{settings.app_base}/setup?token={token}"
|
|
log.warning(
|
|
"FIRST-RUN SETUP REQUIRED — this instance isn't configured yet.\n"
|
|
" Open the install wizard (internal access only):\n %s",
|
|
url, # log-pii-ok: this instance's OWN setup URL, and `?token=` is redacted
|
|
)
|
|
# No HLS session survives a restart (they live in this process), so anything still on disk is
|
|
# a crashed run's segments — sweep them or they accumulate forever. FIRE AND FORGET, not
|
|
# awaited: uvicorn does not serve a single request until lifespan startup returns, so awaiting
|
|
# the sweep would hold the port shut for as long as the rmtree runs (gigabytes of `.ts` after a
|
|
# long uptime) — the 502 + failed post-deploy health probe this is meant to avoid. A thread
|
|
# alone doesn't buy that; not waiting does. The sweep only touches directories no live session
|
|
# owns, so racing it against early playback is safe.
|
|
sweep = asyncio.create_task(_sweep_hls_orphans())
|
|
reaper = asyncio.create_task(_reap_hls_sessions())
|
|
start_scheduler()
|
|
try:
|
|
yield
|
|
finally:
|
|
log.info("Siftlode shutting down")
|
|
# Awaited, not just cancelled: a bare `.cancel()` lets the loop close with the task still
|
|
# pending ("Task was destroyed but it is pending!" — noise that reads like a bug during
|
|
# triage). Cancelling does NOT interrupt the rmtree thread itself; this only makes our own
|
|
# bookkeeping honest.
|
|
for task in (sweep, reaper):
|
|
task.cancel()
|
|
with suppress(asyncio.CancelledError):
|
|
await task
|
|
shutdown_scheduler()
|
|
# Kills a running subtitle extraction: its worker would otherwise hold interpreter exit for
|
|
# the rest of the read (see plex_subtitles.shutdown).
|
|
plex_subtitles.shutdown()
|
|
logsink.stop() # last: write what the shutdown itself just logged
|
|
|
|
|
|
# How often idle HLS sessions are reaped (see plex/stream.py `reap_idle`).
|
|
_HLS_REAP_EVERY_S = 60.0
|
|
|
|
|
|
async def _reap_hls_sessions() -> None:
|
|
"""The periodic reaper plex/stream.py's docstring promises. `reap_idle` used to run only inside
|
|
the NEXT session request, so after the last playback of the day an abandoned session — its
|
|
ffmpeg still working through the film, its directory a whole segmented copy of it — lived until
|
|
somebody pressed play again or the process restarted.
|
|
|
|
A FINISHED session is reaped on the short rule here, not kept for the full idle window: an open
|
|
player checkpoints every 10 s and that route touches the session, so a paused viewer is no longer
|
|
invisible to this pass (see `reap_idle`). Keeping every finished session for ten minutes instead
|
|
held one whole segmented copy of a film per item on the download volume (round 5)."""
|
|
while True:
|
|
await asyncio.sleep(_HLS_REAP_EVERY_S)
|
|
try:
|
|
await asyncio.to_thread(plex_stream.reap_idle)
|
|
except Exception:
|
|
log.exception("HLS session reap failed")
|
|
|
|
|
|
async def _sweep_hls_orphans() -> None:
|
|
"""Background half of the startup HLS sweep (see lifespan). Owns its own errors: a failure here
|
|
must never take the app down, and a cancelled shutdown is not one."""
|
|
try:
|
|
swept = await asyncio.to_thread(plex_stream.sweep_orphans)
|
|
except asyncio.CancelledError:
|
|
raise
|
|
except Exception:
|
|
log.exception("HLS orphan sweep failed")
|
|
return
|
|
if swept:
|
|
log.info("swept %d orphaned HLS segment directories", swept)
|
|
|
|
|
|
app = FastAPI(title=settings.app_name, lifespan=lifespan)
|
|
|
|
|
|
@app.exception_handler(RequestValidationError)
|
|
async def _validation_error(request: Request, exc: RequestValidationError) -> JSONResponse:
|
|
"""FastAPI's own 422, in its own shape — except that it can be ENCODED. The JSON parser accepts
|
|
`NaN`/`Infinity`, a float field refuses them, and the refusal echoes the value back: the default
|
|
handler then failed to serialise its own error (JSON has no NaN) and the request became a 500
|
|
with a traceback. A non-finite value is echoed as its name instead."""
|
|
finite = {float: lambda x: x if math.isfinite(x) else str(x)}
|
|
return JSONResponse(
|
|
status_code=422, content={"detail": jsonable_encoder(exc.errors(), custom_encoder=finite)}
|
|
)
|
|
|
|
|
|
@app.exception_handler(DataError)
|
|
async def _nul_in_text(request: Request, exc: DataError) -> JSONResponse:
|
|
"""A NUL byte in a string the request sent is a 422, on every route at once. Postgres cannot hold
|
|
NUL in text, so the driver refuses to even send the query — and a `%00` in a query parameter or
|
|
an escaped NUL in a JSON body (a playlist's `contains=`, a reorder's key list, a collection
|
|
search) was a 500 with a traceback wherever it reached a comparison. Guarding each site did not
|
|
hold: round 3 guarded the by-rating_key lookup and missed the rest. Any other DataError is left
|
|
alone — re-raised, it stays the 500 it always was."""
|
|
if "NUL (0x00)" not in str(exc.orig):
|
|
raise exc
|
|
return JSONResponse(
|
|
status_code=422, content={"detail": {"code": "invalid_input", "reason": "nul"}}
|
|
)
|
|
|
|
|
|
app.add_middleware(
|
|
SessionMiddleware,
|
|
secret_key=settings.secret_key,
|
|
same_site="lax", # required so the cookie rides the OAuth redirect back from Google
|
|
https_only=settings.session_https_only, # Secure flag when served over HTTPS (prod)
|
|
)
|
|
|
|
if settings.frontend_origin:
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=[settings.frontend_origin],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Paths reachable while the instance is unconfigured (setup mode). Everything else under /api or
|
|
# /auth is locked until the wizard finishes; static/SPA always loads so the wizard page can render.
|
|
_SETUP_OPEN_PREFIXES = ("/api/setup", "/api/version")
|
|
|
|
|
|
@app.middleware("http")
|
|
async def setup_gate(request, call_next):
|
|
"""Lock the app to the install wizard until it's configured. Skips the DB entirely once setup
|
|
completed in this process (the cached fast-path), so there's no steady-state overhead."""
|
|
if not state.setup_complete():
|
|
path = request.url.path
|
|
if path.startswith(("/api/", "/auth/")) and not path.startswith(_SETUP_OPEN_PREFIXES):
|
|
with SessionLocal() as db:
|
|
configured = state.is_configured_cached(db)
|
|
if not configured:
|
|
return JSONResponse({"detail": "This instance isn't set up yet."}, status_code=503)
|
|
return await call_next(request)
|
|
|
|
|
|
@app.middleware("http")
|
|
async def static_cache_headers(request, call_next):
|
|
"""Long-cache the content-hashed SPA bundles. Vite hashes their filename, so a given
|
|
/assets URL never changes content and a browser can hold it forever — this is what makes
|
|
repeat loads instant and clears the Lighthouse "efficient cache lifetimes" audit. index.html
|
|
stays no-cache (set on its own FileResponse) so a deploy is picked up at once; other
|
|
SPA-root static files (welcome images, favicon, robots.txt) get a moderate TTL in the SPA
|
|
fallback below."""
|
|
response = await call_next(request)
|
|
if request.url.path.startswith("/assets/"):
|
|
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
|
|
return response
|
|
|
|
|
|
# Baseline security response headers (SEC-1). A minimal, safe set that needs no per-route tuning:
|
|
# nosniff stops MIME-confusion, the Referrer-Policy keeps the (private) app URL from leaking to
|
|
# external sites the user follows, `frame-ancestors 'none'` blocks click-jacking by refusing to be
|
|
# iframed anywhere (we never embed our own pages; we embed OTHERS, which this does not affect), and
|
|
# Permissions-Policy turns off powerful features the app never uses. `X-Frame-Options: DENY` duplicates
|
|
# the frame-ancestors intent for legacy engines that never implemented CSP frame-ancestors (belt and
|
|
# suspenders — modern browsers use the CSP directive and ignore the header). A full Content-Security-Policy
|
|
# (script/style/connect sources) is deferred — the SPA's inline styles + the YouTube/Plex embeds make
|
|
# it real work — and HSTS belongs on the TLS-terminating edge (the Caddy front door), not here.
|
|
_SECURITY_HEADERS = {
|
|
"X-Content-Type-Options": "nosniff",
|
|
"Referrer-Policy": "strict-origin-when-cross-origin",
|
|
"Content-Security-Policy": "frame-ancestors 'none'",
|
|
"X-Frame-Options": "DENY",
|
|
"Permissions-Policy": "camera=(), microphone=(), geolocation=()",
|
|
}
|
|
|
|
|
|
@app.middleware("http")
|
|
async def security_headers(request, call_next):
|
|
response = await call_next(request)
|
|
for name, value in _SECURITY_HEADERS.items():
|
|
response.headers.setdefault(name, value)
|
|
return response
|
|
|
|
|
|
# The access log (app/logs.py): request id, route template, status, duration — one line, never a
|
|
# query string. Registered LAST so it is the OUTERMOST http middleware and therefore times the whole
|
|
# request, including what the three above do. Being outermost also puts it OUTSIDE SessionMiddleware,
|
|
# which is why the account comes from `request.state.user_id` (set by the auth dependency, the one
|
|
# place a session is actually resolved into an account) rather than from `request.session`.
|
|
app.middleware("http")(log_requests)
|
|
|
|
|
|
app.include_router(health.router)
|
|
app.include_router(auth_routes.router)
|
|
app.include_router(sync.router)
|
|
app.include_router(tags.router)
|
|
app.include_router(feed.router)
|
|
app.include_router(discover_routes.router)
|
|
app.include_router(search_routes.router)
|
|
app.include_router(me.router)
|
|
app.include_router(diagnostics_routes.router)
|
|
app.include_router(notifications.router)
|
|
app.include_router(messages.router)
|
|
app.include_router(channels.router)
|
|
app.include_router(playlists.router)
|
|
app.include_router(saved_views.router)
|
|
app.include_router(plex_routes.router)
|
|
app.include_router(downloads.router)
|
|
app.include_router(downloads.admin_router)
|
|
app.include_router(public_routes.router)
|
|
app.include_router(admin.router)
|
|
app.include_router(config_routes.router)
|
|
app.include_router(scheduler_routes.router)
|
|
app.include_router(audit_routes.router)
|
|
app.include_router(quota.router)
|
|
app.include_router(version.router)
|
|
app.include_router(setup_routes.router)
|
|
|
|
# Ensure modern image types resolve to the right Content-Type when FileResponse guesses from the
|
|
# filename (the runtime's mimetypes db doesn't always know .webp → it'd fall back to octet-stream).
|
|
import mimetypes
|
|
|
|
mimetypes.add_type("image/webp", ".webp")
|
|
mimetypes.add_type("image/avif", ".avif")
|
|
|
|
# The built SPA (populated by the Docker frontend build stage).
|
|
STATIC_DIR = Path(__file__).parent / "static_spa"
|
|
# index.html is unhashed and references the content-hashed /assets bundles, so it MUST NOT be
|
|
# heuristically cached — otherwise, after a deploy, a browser keeps serving the old index.html
|
|
# (pointing at the previous bundle) and runs stale code until a hard refresh. `no-cache` lets it
|
|
# stay cached but forces revalidation (cheap 304 via the FileResponse ETag) on every load. The
|
|
# hashed bundles under /assets can cache forever (a new build changes their filename).
|
|
INDEX_HTML = STATIC_DIR / "index.html"
|
|
INDEX_HEADERS = {"Cache-Control": "no-cache"}
|
|
app.mount(
|
|
"/assets",
|
|
StaticFiles(directory=STATIC_DIR / "assets", check_dir=False),
|
|
name="assets",
|
|
)
|
|
|
|
|
|
@app.get("/")
|
|
async def index() -> FileResponse:
|
|
return FileResponse(INDEX_HTML, headers=INDEX_HEADERS)
|
|
|
|
|
|
@app.get("/watch/{token}")
|
|
async def watch_page(token: str):
|
|
"""Serve the SPA for a public share link, but with per-video Open Graph tags injected so the
|
|
link unfurls richly (title/channel/thumbnail) in Messenger/social crawlers. A real browser
|
|
ignores the extra tags and hydrates WatchPage as usual; invalid/expired/password links fall
|
|
back to the generic card."""
|
|
from app.downloads import og
|
|
|
|
with SessionLocal() as db:
|
|
rendered = og.render_watch_html(token, INDEX_HTML, db)
|
|
if rendered is None:
|
|
return FileResponse(INDEX_HTML, headers=INDEX_HEADERS)
|
|
return HTMLResponse(rendered, headers=INDEX_HEADERS)
|
|
|
|
|
|
@app.get("/{full_path:path}")
|
|
async def spa_fallback(full_path: str) -> FileResponse:
|
|
# Client-side routes fall back to index.html; real API/asset paths are matched above.
|
|
if full_path.startswith(("api/", "auth/", "healthz", "assets/")):
|
|
raise HTTPException(status_code=404)
|
|
# Serve real files that live at the SPA root (Vite copies public/ there — e.g. the landing
|
|
# screenshots under /welcome/, favicon). /assets is already mounted above; everything else
|
|
# that isn't a real file is a client-side route → index.html. Guard against path traversal.
|
|
if full_path:
|
|
candidate = (STATIC_DIR / full_path).resolve()
|
|
if candidate.is_file() and STATIC_DIR.resolve() in candidate.parents:
|
|
# Real SPA-root assets (welcome images, favicon, robots.txt) rarely change and have
|
|
# stable names, so a moderate cache is safe and satisfies the cache-lifetime audit.
|
|
return FileResponse(candidate, headers={"Cache-Control": "public, max-age=604800"})
|
|
return FileResponse(INDEX_HTML, headers=INDEX_HEADERS)
|