Harden the API and the data layer after a security and performance audit
Security - Every route now requires a login and refuses ids outside the caller's workspace with 404: plan, leave-wishes, day-overrides and vacation-days were fully anonymous; every other router trusted client-supplied workspace/child/parent ids. Scraper sessions are bound to their owner. - Invitations can no longer be created for a foreign workspace, and a redeemer who is already a member (or a full workspace) is refused. - Production refuses the default JWT secret; API docs are hidden in production; a failed migration aborts startup instead of serving. - Login and register no longer reveal whether an email exists; inactive accounts are rejected; /auth/refresh is rate limited. - The FK scraper writes debug artefacts only when explicitly enabled, logs no scraped values or page text, clears the BankID QR as soon as it is scanned and purges finished sessions after five minutes. - Real FK page captures are removed from the tree (history rewrite follows); the parser tests use a synthetic fixture. Performance and correctness - Plan apply/sync run inside one locked transaction with batched writes instead of one commit per row, so a failure mid-batch leaves nothing half-applied and concurrent syncs no longer corrupt each other. - SQLite runs in WAL mode with a busy timeout; 14 indexes cover the filters that were full scans; argon2 hashing runs off the event loop; responses are gzipped. - Frontend: a 401 now refreshes the token once and retries, so sessions no longer die silently after 15 minutes; every data hook discards stale responses; logout clears state even when the server call fails. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CsXCCcDfmV4Sps4ZQSvNEY
This commit is contained in:
@@ -19,7 +19,7 @@ from app.dependencies import (
|
||||
ChildRepositoryDep,
|
||||
CurrentUserDep,
|
||||
ParentalLeaveRepositoryDep,
|
||||
PersonalDataRepositoryDep,
|
||||
WorkspaceScopeDep,
|
||||
)
|
||||
from app.entities.parental_leave import ParentalLeaveFetch
|
||||
from app.rate_limit import limiter
|
||||
@@ -55,8 +55,11 @@ _scraper_service = FKScraperService()
|
||||
async def save_manual_entry(
|
||||
payload: ManualFetchRequest,
|
||||
repo: ParentalLeaveRepositoryDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> ParentalLeaveFetchResponse:
|
||||
scope.check_workspace(payload.workspace_id)
|
||||
await scope.check_member(payload.personal_data_uuid)
|
||||
await scope.check_child(payload.child_id)
|
||||
service = ManualFetchService(repo)
|
||||
try:
|
||||
fetch = await service.save_manual_entry(
|
||||
@@ -84,8 +87,10 @@ async def get_latest(
|
||||
personal_data_uuid: UUID,
|
||||
child_id: UUID,
|
||||
repo: ParentalLeaveRepositoryDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> ParentalLeaveFetchResponse | None:
|
||||
await scope.check_member(personal_data_uuid)
|
||||
await scope.check_child(child_id)
|
||||
service = ManualFetchService(repo)
|
||||
fetch = await service.get_latest(personal_data_uuid, child_id)
|
||||
return ParentalLeaveFetchResponse.from_entity(fetch) if fetch else None
|
||||
@@ -98,8 +103,10 @@ async def get_history(
|
||||
personal_data_uuid: UUID,
|
||||
child_id: UUID,
|
||||
repo: ParentalLeaveRepositoryDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> list[ParentalLeaveFetchResponse]:
|
||||
await scope.check_member(personal_data_uuid)
|
||||
await scope.check_child(child_id)
|
||||
service = ManualFetchService(repo)
|
||||
history = await service.list_history(personal_data_uuid, child_id)
|
||||
return [ParentalLeaveFetchResponse.from_entity(f) for f in history]
|
||||
@@ -134,37 +141,33 @@ async def start_fetch(
|
||||
request: Request,
|
||||
payload: _StartRequest,
|
||||
current_user: CurrentUserDep,
|
||||
pd_repo: PersonalDataRepositoryDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
child_repo: ChildRepositoryDep,
|
||||
parental_leave_repo: ParentalLeaveRepositoryDep,
|
||||
) -> _StartResponse:
|
||||
"""`workspace_id` is always the caller's own — `payload.workspace_id` is
|
||||
accepted on the wire (frontend compatibility) but never trusted; the
|
||||
caller's own personal_data_uuid/child_id default when omitted, and any
|
||||
id explicitly supplied is verified against the caller's workspace.
|
||||
"""
|
||||
if not settings.scraper_enabled:
|
||||
raise _SCRAPER_DISABLED
|
||||
|
||||
personal_data_uuid = payload.personal_data_uuid or current_user.personal_data_uuid
|
||||
workspace_id = scope.workspace_id
|
||||
personal_data_uuid = payload.personal_data_uuid or scope.personal_data_uuid
|
||||
await scope.check_member(personal_data_uuid)
|
||||
|
||||
if payload.workspace_id is None or payload.child_id is None:
|
||||
pd = await pd_repo.get_by_id(personal_data_uuid)
|
||||
if pd is None:
|
||||
if payload.child_id is not None:
|
||||
child = await scope.check_child(payload.child_id)
|
||||
child_id = child.id
|
||||
else:
|
||||
children = await child_repo.list_by_workspace(workspace_id)
|
||||
if not children:
|
||||
raise HTTPException(
|
||||
status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
"No parent profile for this account.",
|
||||
"No child in workspace — add one before fetching.",
|
||||
)
|
||||
workspace_id = payload.workspace_id or pd.workspace_id
|
||||
|
||||
if payload.child_id is None:
|
||||
children = await child_repo.list_by_workspace(workspace_id)
|
||||
if not children:
|
||||
raise HTTPException(
|
||||
status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||
"No child in workspace — add one before fetching.",
|
||||
)
|
||||
child_id = children[0].id
|
||||
else:
|
||||
child_id = payload.child_id
|
||||
else:
|
||||
workspace_id = payload.workspace_id
|
||||
child_id = payload.child_id
|
||||
child_id = children[0].id
|
||||
|
||||
# Capture the repo for the background task. The connection lives for the app's
|
||||
# lifetime (app.state.db_connection), so using it from the background coroutine
|
||||
@@ -176,6 +179,7 @@ async def start_fetch(
|
||||
workspace_id=workspace_id,
|
||||
personal_data_uuid=personal_data_uuid,
|
||||
child_id=child_id,
|
||||
owner_user_id=current_user.id,
|
||||
persist_callback=_persist,
|
||||
)
|
||||
return _StartResponse(
|
||||
@@ -192,11 +196,11 @@ async def start_fetch(
|
||||
async def poll_fetch(
|
||||
request: Request,
|
||||
session_id: str,
|
||||
_current_user: CurrentUserDep,
|
||||
current_user: CurrentUserDep,
|
||||
) -> dict:
|
||||
if not settings.scraper_enabled:
|
||||
raise _SCRAPER_DISABLED
|
||||
return await _scraper_service.poll_session(session_id)
|
||||
return await _scraper_service.poll_session(session_id, current_user.id)
|
||||
|
||||
|
||||
@scraper_router.post("/cancel")
|
||||
@@ -204,9 +208,9 @@ async def poll_fetch(
|
||||
async def cancel_fetch(
|
||||
request: Request,
|
||||
payload: _CancelRequest,
|
||||
_current_user: CurrentUserDep,
|
||||
current_user: CurrentUserDep,
|
||||
) -> dict:
|
||||
if not settings.scraper_enabled:
|
||||
raise _SCRAPER_DISABLED
|
||||
await _scraper_service.cancel_session(payload.session_id)
|
||||
await _scraper_service.cancel_session(payload.session_id, current_user.id)
|
||||
return {"status": "cancelled"}
|
||||
|
||||
Reference in New Issue
Block a user