Why #
The audit found the backend working but structured against FastAPI conventions:
- every protected handler repeated
access_token: str | None = Cookie(default=None)plus a manualrequire_*call, and the current user came back in three different shapes (ORMUser, two different dicts); utils/was really the service layer (users, pages, files, email defense, mailer…), mixed with true helpers;- request models were defined inside route files;
[project].dependenciespinned ~25 transitive packages,passlibwas kept alive only by pinningbcrypt==4.0.1, and ruff ran with only its default rules.
No behaviour changes are intended. This is a mechanical restructuring, covered by the existing suite.
What changed #
- Shared auth dependencies (
core/deps.py)get_current_userresolves the session cookie to an activeUser;require_adminbuilds on it; newRoleIntEnum(NONE/USER/ADMIN/OWNER).- All handlers in
users,keys,notifications,pages,filesandadminnow declareuser: User = Depends(get_current_user)(orrequire_admin). - The cookie name now honours
AUTH_COOKIE_NAMEin one place instead of 36 hard-codedaccess_tokenparameters. - Removed
require_authenticated_user,_extract_user_authandadmin._require_admin;utils/fileshelpers take aUser.current_admin["id"]becamecurrent_admin.id. - Two tests in
files_test.pythat built dict users now buildUserobjects.
- Dependencies and tooling
pyproject.tomllists only direct dependencies (the lockfile pins the rest); dropped thedotenvshim.- Replaced
passlibwithbcryptdirectly. Hashes keep the$2b$format, and passwords are explicitly truncated to 72 bytes, so existing hashes keep verifying. New test covers round-trip, hashes written by another bcrypt implementation, malformed hashes (fail closed) and long passwords. - Ruff now selects
E4,E7,E9,F,I,B,UP,ASYNC,RUF100(ignoringB904andASYNC240, with reasons inpyproject.toml).
utils/→services/:users,pages,files,notifications,email_defense,mailer,access_keys(and their tests) moved withgit mv;utils/keepserrorsandnetwork. Imports and monkeypatch strings updated.schemas/: request models fromroutes/files.py,routes/admin.pyandroutes/users.pymoved intoschemas/files.py,schemas/admin.py,schemas/users.py.
Each commit is self-contained and passes the checks on its own, so it can be reviewed commit by commit.
Review notes #
- Large diff, but almost all of it is moves and import changes;
git diff -M/ per-commit review is much easier than the combined diff. - Path-depth-sensitive code (
srv_root(), mailer custom dir) moved fromutils/toservices/at the same depth, and its tests pass unchanged. - FastAPI caches
get_dbper request, soget_current_userand the handler share one session; tests that overrideget_dbkeep working.
Not in this PR #
Response models for every endpoint, pydantic-settings for config, domain exceptions instead of HTTPException in services, and the model/migration cleanup (relationships, timezone-aware datetimes, ondelete, naming convention). The migration work needs a MariaDB to test against.
Testing #
uv run ruff format --check .,uv run ruff check .,uv run basedpyright .,uv run pytest— all clean.- Not tested: running the app against MariaDB via
make dev.
Merge notes #
Based on dev after the security and CI commits. pyproject.toml and uv.lock change, so the prod image build (uv sync --frozen --no-dev) pulls a different dependency set; worth a make build before deploying.