myst/web/app/config.py
ZTatman 18897362df fix(web): security hardening, rename cleanup, and error pages
- Move TOKEN_ENCRYPTION_KEY from DB to env var (no longer stored alongside encrypted data)
- Add one-time setup token printed to server logs to prevent anonymous setup takeover
- Setup token persists until first successful login, allowing config retries
- Remove setup_complete flag in favor of app_settings nullability check
- Fix stale Myst→Firebreak references in crypto.py, dev.sh, pyproject.toml
- Add dynamic OAuth callback URL in setup wizard
- Fix global heading color override to use foreground instead of danger
- Add aria-label to brand link for accessibility
- Remove forced redirect to /settings after first login
- Remove reconfigure link from landing page
- Add styled error page with HTML/JSON content negotiation
- Add 401 guard dog easter egg

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-06-05 00:27:51 -04:00

61 lines
1.9 KiB
Python

from __future__ import annotations
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING
from cryptography.fernet import Fernet
from fastapi import Request
from pydantic import SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict
from app.auth.crypto import fernet_from_encryption_key
if TYPE_CHECKING:
from app.models.app_settings import AppSettings
_env_file = Path(__file__).parent.parent / ".env"
class Settings(BaseSettings):
"""Bootstrap settings loaded from environment variables.
Only contains fields needed before the database is available.
Forgejo config lives in the AppSettings DB table after setup.
"""
app_name: str = "Firebreak"
database_url: str = "postgresql+asyncpg://firebreak:firebreak@db:5432/firebreak_db"
token_encryption_key: str = ""
model_config = SettingsConfigDict(env_file=str(_env_file))
@lru_cache
def get_settings() -> Settings:
"""Cached settings instance."""
return Settings()
@lru_cache
def get_fernet() -> Fernet:
"""Return a cached Fernet cipher from the TOKEN_ENCRYPTION_KEY env var."""
key = get_settings().token_encryption_key
if not key:
raise ValueError(
"TOKEN_ENCRYPTION_KEY is not set. Add it to your .env file or deployment "
"environment. Generate one with: uv run python -c "
"'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'"
)
fernet = fernet_from_encryption_key(SecretStr(key))
if fernet is None:
raise ValueError(
"TOKEN_ENCRYPTION_KEY is invalid. Generate a valid key with: uv run python -c "
"'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'"
)
return fernet
def get_app_settings(request: Request) -> AppSettings:
"""FastAPI dependency returning the AppSettings row from app state."""
return request.app.state.app_settings