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:
2026-09-18 07:54:21 +02:00
parent ae16d71970
commit 02db5430eb
81 changed files with 3616 additions and 600 deletions
+47 -10
View File
@@ -24,10 +24,11 @@ import pytest_asyncio
from dateutil.relativedelta import relativedelta
from httpx import ASGITransport, AsyncClient
from app.dependencies import get_connection
from app.dependencies import get_connection, get_current_user
from app.entities.child import Child
from app.entities.parental_leave import ParentalLeaveFetch, TierData
from app.entities.personal_data import PersonalData
from app.entities.user import User
from app.entities.vacation_days import VacationDays
from app.entities.workspace import Workspace
from app.main import app
@@ -38,6 +39,14 @@ from app.repositories_impl.sqlite.vacation_days_repository import SQLiteVacation
from app.repositories_impl.sqlite.workspace_repository import SQLiteWorkspaceRepository
from app.services.allocation_service import PlanProposal, WindowOutcome
# Fixed identity for the authenticated caller in every test in this file.
# Each test creates its own workspace + "parent" PersonalData row — that row
# is created with THIS uuid (instead of a random one) so the caller's
# `get_current_personal_data` -> `workspace_id` resolves to the workspace the
# test just seeded. See CLAUDE.md T1 authz work / audit §4 blast radius.
_CURRENT_USER_PD_UUID = uuid4()
_CURRENT_USER_ID = uuid4()
def _make_fetch(
personal_data_uuid,
@@ -82,9 +91,17 @@ async def client(connection: aiosqlite.Connection) -> AsyncClient:
on-disk database via the (never-run) lifespan.
"""
app.dependency_overrides[get_connection] = lambda: connection
app.dependency_overrides[get_current_user] = lambda: User(
id=_CURRENT_USER_ID,
personal_data_uuid=_CURRENT_USER_PD_UUID,
email="alex@example.com",
hashed_password="x",
created_at=datetime.now(),
)
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
yield ac
app.dependency_overrides.pop(get_connection, None)
app.dependency_overrides.pop(get_current_user, None)
class TestProposeSurplusAdviceOverTheWire:
@@ -107,7 +124,7 @@ class TestProposeSurplusAdviceOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -178,7 +195,7 @@ class TestProposeAge4ProjectionsOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -254,7 +271,7 @@ class TestProposeMessageKeysOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -369,7 +386,7 @@ class TestProposeWishNameMessageKeyOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -451,7 +468,7 @@ class TestProposeWishNameMessageKeyOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -528,7 +545,7 @@ class TestApplyRejectionsOverTheWireMessageKeys:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
@@ -598,7 +615,13 @@ class TestLeaveWishWindowModeOverTheWire:
) -> None:
now = datetime.now()
workspace_repo = SQLiteWorkspaceRepository(connection)
personal_data_repo = SQLitePersonalDataRepository(connection)
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
await personal_data_repo.create(
PersonalData(
uuid=_CURRENT_USER_PD_UUID, workspace_id=ws.id, name="Alex", created_at=now
)
)
response = await client.post(
"/api/leave-wishes",
@@ -632,7 +655,13 @@ class TestLeaveWishWindowModeOverTheWire:
) -> None:
now = datetime.now()
workspace_repo = SQLiteWorkspaceRepository(connection)
personal_data_repo = SQLitePersonalDataRepository(connection)
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
await personal_data_repo.create(
PersonalData(
uuid=_CURRENT_USER_PD_UUID, workspace_id=ws.id, name="Alex", created_at=now
)
)
response = await client.post(
"/api/leave-wishes",
@@ -668,7 +697,11 @@ class TestLeaveWishWindowModeOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(), workspace_id=ws.id, name="Alex", salary_type="monthly", created_at=now
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
created_at=now,
)
)
child = await child_repo.create(
@@ -740,7 +773,11 @@ class TestLeaveWishWindowModeOverTheWire:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(), workspace_id=ws.id, name="Alex", salary_type="monthly", created_at=now
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",
created_at=now,
)
)
child = await child_repo.create(
@@ -924,7 +961,7 @@ class TestPlanProposalFieldParityWithDomain:
ws = await workspace_repo.create(Workspace(id=uuid4(), name="Familj", created_at=now))
parent = await personal_data_repo.create(
PersonalData(
uuid=uuid4(),
uuid=_CURRENT_USER_PD_UUID,
workspace_id=ws.id,
name="Alex",
salary_type="monthly",