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
+45 -1
View File
@@ -217,7 +217,12 @@ describe('WorkspaceContext', () => {
expect(result.current.selectedChildId).toBe('c2')
})
it('setWorkspaceId stores the id and updates state', async () => {
it('setWorkspaceId stores the id, updates state, and fetches that workspace directly', async () => {
// P2 in the frontend audit: this used to rely on a reactive effect that
// only fired while `workspace` was still null, so switching workspaces
// after the initial load silently never fetched the new one. Fetching
// explicitly here fixes that without reintroducing the cold-load
// duplicate GET /workspace the same effect used to cause.
vi.mocked(useAuth).mockReturnValue(authenticated)
vi.mocked(api.getUserContext).mockResolvedValue(userContext)
vi.mocked(api.getWorkspace).mockResolvedValue(workspace)
@@ -225,12 +230,19 @@ describe('WorkspaceContext', () => {
const { result } = renderHook(() => useWorkspace(), { wrapper })
await waitFor(() => expect(result.current.workspace).not.toBeNull())
const otherWorkspace: Workspace = { ...workspace, id: 'ws2', name: 'Other workspace' }
vi.mocked(api.getWorkspace).mockResolvedValue(otherWorkspace)
vi.mocked(api.getWorkspace).mockClear()
act(() => {
result.current.setWorkspaceId('ws2')
})
expect(result.current.workspaceId).toBe('ws2')
expect(localStorage.getItem('workspaceId')).toBe('ws2')
await waitFor(() => expect(api.getWorkspace).toHaveBeenCalledWith('ws2'))
await waitFor(() => expect(result.current.workspace).toEqual(otherWorkspace))
})
it('refetch re-fetches the current workspace', async () => {
@@ -322,6 +334,38 @@ describe('WorkspaceContext', () => {
expect(result.current.workspace).toEqual(otherWorkspace)
})
it('discards a refreshContext response that resolves after logout (S4)', async () => {
vi.mocked(useAuth).mockReturnValue(authenticated)
let resolveContext: (value: api.UserContext) => void = () => {}
const pending = new Promise<api.UserContext>((resolve) => {
resolveContext = resolve
})
vi.mocked(api.getUserContext).mockReturnValue(pending)
const { result, rerender } = renderHook(() => useWorkspace(), { wrapper })
// Logout happens while the initial refreshContext() call is still
// in flight (e.g. a slow /auth/context response).
vi.mocked(useAuth).mockReturnValue(unauthenticated)
rerender()
await waitFor(() => expect(result.current.workspace).toBeNull())
expect(result.current.workspaceId).toBeNull()
// The stale response arrives after logout — it must be discarded
// rather than repopulating this account's context.
await act(async () => {
resolveContext(userContext)
await pending
})
expect(result.current.workspaceId).toBeNull()
expect(result.current.parentName).toBeNull()
expect(result.current.personalDataUuid).toBeNull()
expect(result.current.children).toEqual([])
expect(localStorage.getItem('workspaceId')).toBeNull()
})
it('throws when useWorkspace is used outside a WorkspaceProvider', () => {
vi.mocked(useAuth).mockReturnValue(unauthenticated)