Commit Graph

29 Commits

Author SHA1 Message Date
Hartmut 23c6e0e04b security: sanitise Prisma error leaks in AI-tool helpers (#53)
Five helper error mappers (timeline / project-creation / resource-creation
/ vacation-creation / task-action-execution) fell through to
`return { error: error.message }` for BAD_REQUEST and CONFLICT cases. When
the TRPCError wrapped a Prisma error, the message contained column names,
relation paths, and the offending unique-constraint value — all of which
would reach the LLM in chat context and, via audit_log.changes JSONB, the DB.

Add `sanitizeAssistantErrorMessage()` that regex-detects Prisma and raw
Postgres signatures (P2002/P2003/P2025, not-null, FK, check-constraint,
duplicate-key) and replaces them with a generic "Invalid input". Also caps
messages at 500 chars to defend against stack-trace-like payloads. Wire
the helper into all five call-sites; the developer-constructed
`AssistantVisibleError` branch in `normalizeAssistantExecutionError` is
left untouched since those strings are hand-written.

Coverage: 11 new tests in assistant-tools-error-sanitiser.test.ts; existing
vacation / task-action / resource-creation / project-creation error tests
(12 tests, 5 files) all remain green.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 09:40:01 +02:00
Hartmut 1ff5c3377c security: block raw/tx escape hatches on read-only AI DB proxy (#47)
The read-only proxy previously wrapped model delegates to block writes,
but left client-level raw/escape hatches ($transaction, $executeRaw,
$executeRawUnsafe, $queryRawUnsafe, $runCommandRaw) intact. A read-tool
could smuggle DML via raw SQL, or open an interactive $transaction whose
tx-scoped client (unproxied by construction) accepts writes.

- read-only-prisma: block $transaction, $executeRaw, $executeRawUnsafe,
  $queryRawUnsafe, $runCommandRaw at the client level. Template-tagged
  $queryRaw stays allowed (read-only by API contract).
- assistant-tools: add create_estimate to MUTATION_TOOLS — it uses
  $transaction internally and was previously bypassing the proxy only
  because $transaction wasn't blocked.
- shared: document isReadOnly flag on ToolContext so any scoped tRPC
  caller a tool spawns keeps the proxied client.
- helpers: note the runtime wrap at assistant-tools.ts:739 is
  authoritative; forwarding ctx.db verbatim is correct.
- tests: cover model writes, raw escapes, and the allowed $queryRaw
  path (7 cases, all pass).
- loosen one estimate-detail test that compared the exact db instance
  (fails once that instance is a proxy; the assertion's intent is the
  estimate id).

Covers EGAI 4.1.1.2 / IAAI 3.6.22.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 08:38:05 +02:00
Hartmut 3c5d1d37f7 security: rate-limit IP-keyed, fail-closed on empty key (#37)
Rate-limiter now accepts string | string[] so callers can key on
multiple buckets simultaneously. If any bucket is exhausted the
request is denied, which lets login/TOTP/reset-password throttle on
BOTH user identifier and source IP without either becoming a bypass.

Fail-closed: empty/whitespace-only keys now deny by default instead
of silently allowing unbounded attempts (was CWE-307 gap).

Degraded-fallback divisor reduced from /10 to /2 — the old aggressive
clamp forced-logged-out legitimate users during brief Redis outages;
/2 still meaningfully slows distributed brute-force.

Callers updated:
- auth.ts (login): both email: and ip: buckets
- auth router requestPasswordReset: email + IP
- auth router resetPassword: IP before lookup, email-reset after
- invite router getInvite/acceptInvite: IP
- user-self-service verifyTotp: userId + IP

TRPCContext now carries clientIp; web tRPC route extracts it from
X-Forwarded-For / X-Real-IP.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-17 08:19:33 +02:00
Hartmut a9a580b8f5 fix(api): add resultSchema field to ToolDef interface
CI / Architecture Guardrails (push) Successful in 1m12s
CI / Typecheck (push) Failing after 1m41s
CI / Build (push) Has been skipped
CI / E2E Tests (push) Has been skipped
CI / Fresh-Linux Docker Deploy (push) Has been skipped
CI / Release Images (push) Has been cancelled
CI / Assistant Split Regression (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Unit Tests (push) Has been cancelled
Committed assistant-tools.ts already references toolDefinition?.resultSchema
for EGAI 4.3.1.2 result validation, but the ToolDef interface in shared.ts
was missing the field declaration, breaking typecheck.
2026-04-12 18:17:42 +02:00
Hartmut 5a4836d292 perf(api): eliminate 3 N+1 query patterns
- timeline-holiday-load-support: deduplicate getResolvedCalendarHolidays
  by location key so resources sharing the same country/state/city resolve
  holidays once instead of per-resource
- rate-card-lookup: add lookupRatesBatch that loads rate card lines once
  and scores locally per demand line, replacing per-line DB round-trips
  in estimate-demand-lines autoFillDemandLineRates
- config-readmodels: include _count in utilization-category list query
  instead of calling getById per category for project counts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-11 22:59:45 +02:00
Hartmut dd2c9c0f88 perf(api,web,db): refactor and optimize for enterprise readiness
- Add missing @@index([userId]) on Account and Session models (auth query perf)
- Batch holiday-auto-import to eliminate N+1 query pattern (O(n) → O(1))
- Reduce SessionProvider refetchInterval from 5min to 15min
- Fix Cache-Control catch-all to stop blocking static asset caching
- Decompose assistant-tools.ts (2,562 → 809 lines) into callers, helpers, access-control modules
- Add @next/bundle-analyzer for data-driven bundle optimization
- Add @react-pdf/renderer to optimizePackageImports
- Add safety caps (take limits) on unbounded findMany queries

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-11 22:34:41 +02:00
Hartmut 8c5be51251 feat(platform): checkpoint current implementation state 2026-04-01 07:42:03 +02:00
Hartmut a0c98cf24d test(api): close assistant split regression gaps 2026-04-01 07:33:00 +02:00
Hartmut f2d511ebc8 feat(api): include skill gaps in dashboard detail 2026-03-31 23:46:07 +02:00
Hartmut 2de5a0eede feat(api): include project health in dashboard detail 2026-03-31 23:36:29 +02:00
Hartmut bcfb18393e refactor(api): extract assistant vacation entitlement slice 2026-03-30 23:09:32 +02:00
Hartmut 45c25b17c1 refactor(api): extract assistant country read slice 2026-03-30 22:53:59 +02:00
Hartmut 0cc7b9805a refactor(api): extract assistant planning navigation slice 2026-03-30 22:51:39 +02:00
Hartmut aed99cb894 refactor(api): extract assistant import export dispo slice 2026-03-30 22:45:00 +02:00
Hartmut 4d8c91d705 refactor(api): extract assistant scenario rate-analysis slice 2026-03-30 22:38:01 +02:00
Hartmut d55ab67e04 refactor(api): extract assistant audit-history slice 2026-03-30 22:30:51 +02:00
Hartmut ab32c7804b refactor(api): extract assistant comments slice 2026-03-30 22:29:07 +02:00
Hartmut 73fdf1c6ab refactor(api): extract assistant dashboard insights slice 2026-03-30 22:23:05 +02:00
Hartmut 6c6afdd059 refactor(api): extract assistant blueprint rate-card slice 2026-03-30 22:17:41 +02:00
Hartmut e1496064e0 refactor(api): extract assistant resource slice 2026-03-30 22:13:42 +02:00
Hartmut 279eb24e5a refactor(api): extract assistant staffing demand slice 2026-03-30 22:07:44 +02:00
Hartmut 1568efab30 refactor(api): extract assistant project slice 2026-03-30 22:04:28 +02:00
Hartmut 91ab7898e9 refactor(api): extract assistant estimate slice 2026-03-30 21:57:16 +02:00
Hartmut 18ba6fff9a refactor(api): extract assistant notifications slice 2026-03-30 21:49:49 +02:00
Hartmut fec4aa2e23 refactor(api): extract assistant user admin slice 2026-03-30 21:33:49 +02:00
Hartmut 7d3c6d978e refactor(api): extract assistant self-service slice 2026-03-30 21:31:06 +02:00
Hartmut 72394747f9 refactor(api): extract assistant config readmodels 2026-03-30 21:27:23 +02:00
Hartmut 9571d454d4 refactor(api): extract assistant chargeability and country slices 2026-03-30 21:19:16 +02:00
Hartmut 447d42acb8 refactor(api): extract assistant tool admin slices 2026-03-30 20:56:00 +02:00