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:
@@ -14,9 +14,10 @@ from fastapi import APIRouter, HTTPException, status
|
||||
|
||||
from app.dependencies import (
|
||||
ChildServiceDep,
|
||||
CurrentUserDep,
|
||||
PersonalDataServiceDep,
|
||||
TipsServiceDep,
|
||||
WorkspaceIdDep,
|
||||
WorkspaceScopeDep,
|
||||
)
|
||||
from app.models.child import ChildCreate, ChildResponse, ChildUpdate
|
||||
from app.models.personal_data import PersonalDataResponse, PersonalDataUpdate
|
||||
@@ -35,9 +36,8 @@ tips_router = APIRouter(prefix="/api/tips", tags=["tips"])
|
||||
|
||||
@tips_router.get("/{workspace_id}", response_model=list[TipResponse])
|
||||
async def get_tips(
|
||||
workspace_id: UUID,
|
||||
workspace_id: WorkspaceIdDep,
|
||||
service: TipsServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
) -> list[TipResponse]:
|
||||
tips = await service.get_tips(workspace_id)
|
||||
return [TipResponse.from_domain(tip) for tip in tips]
|
||||
@@ -45,10 +45,9 @@ async def get_tips(
|
||||
|
||||
@tips_router.post("/{workspace_id}/dismissals", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def dismiss_tip(
|
||||
workspace_id: UUID,
|
||||
workspace_id: WorkspaceIdDep,
|
||||
payload: TipDismissalCreate,
|
||||
service: TipsServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
) -> None:
|
||||
try:
|
||||
await service.dismiss(workspace_id, payload.tip_id)
|
||||
@@ -71,8 +70,9 @@ def _child_to_response(child) -> ChildResponse:
|
||||
async def create_child(
|
||||
payload: ChildCreate,
|
||||
service: ChildServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> ChildResponse:
|
||||
scope.check_workspace(payload.workspace_id)
|
||||
try:
|
||||
child = await service.create(
|
||||
workspace_id=payload.workspace_id,
|
||||
@@ -90,10 +90,10 @@ async def create_child(
|
||||
async def get_child(
|
||||
child_id: UUID,
|
||||
service: ChildServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> ChildResponse:
|
||||
try:
|
||||
child = await service.get(child_id)
|
||||
child = await service.get(child_id, workspace_id=scope.workspace_id)
|
||||
return _child_to_response(child)
|
||||
except NotFoundError as exc:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
||||
@@ -101,9 +101,8 @@ async def get_child(
|
||||
|
||||
@child_router.get("/workspace/{workspace_id}", response_model=list[ChildResponse])
|
||||
async def list_children(
|
||||
workspace_id: UUID,
|
||||
workspace_id: WorkspaceIdDep,
|
||||
service: ChildServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
) -> list[ChildResponse]:
|
||||
children = await service.list_by_workspace(workspace_id)
|
||||
return [_child_to_response(c) for c in children]
|
||||
@@ -118,7 +117,7 @@ async def update_child(
|
||||
child_id: UUID,
|
||||
payload: ChildUpdate,
|
||||
service: ChildServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> ChildResponse:
|
||||
try:
|
||||
child = await service.update(
|
||||
@@ -127,6 +126,7 @@ async def update_child(
|
||||
birth_date=payload.birth_date,
|
||||
preschool_start_date=payload.preschool_start_date,
|
||||
preschool_planning_days_per_year=payload.preschool_planning_days_per_year,
|
||||
workspace_id=scope.workspace_id,
|
||||
)
|
||||
return _child_to_response(child)
|
||||
except ValidationError as exc:
|
||||
@@ -139,10 +139,10 @@ async def update_child(
|
||||
async def delete_child(
|
||||
child_id: UUID,
|
||||
service: ChildServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> None:
|
||||
try:
|
||||
await service.delete(child_id)
|
||||
await service.delete(child_id, workspace_id=scope.workspace_id)
|
||||
except NotFoundError as exc:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, str(exc)) from exc
|
||||
|
||||
@@ -152,7 +152,7 @@ async def update_parent_settings(
|
||||
personal_data_uuid: UUID,
|
||||
payload: PersonalDataUpdate,
|
||||
service: PersonalDataServiceDep,
|
||||
_current_user: CurrentUserDep,
|
||||
scope: WorkspaceScopeDep,
|
||||
) -> PersonalDataResponse:
|
||||
try:
|
||||
updated = await service.update(
|
||||
@@ -160,6 +160,7 @@ async def update_parent_settings(
|
||||
salary_type=payload.salary_type,
|
||||
employment_status=payload.employment_status,
|
||||
min_vacation_days_per_year=payload.min_vacation_days_per_year,
|
||||
workspace_id=scope.workspace_id,
|
||||
)
|
||||
return PersonalDataResponse.from_entity(updated)
|
||||
except ValidationError as exc:
|
||||
|
||||
Reference in New Issue
Block a user