1
0
Fork 0
Commit Graph

27 Commits

Author SHA1 Message Date
Jonny 2efce894fa feat: neue Spalten fuer Empfaenger, Unterschrift und Schadensbeschreibung
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-03 12:10:33 +02:00
Jonny 59ddfe0b68 fix: service_role Grant fuer profiles-Lookup beim Username-Login + Fehlerbehandlung
Task 7's E2E-Verifikation von Task 6 fand: service_role hatte kein SELECT-Grant
auf public.profiles. Der Username-Login-Lookup in web/app/login/actions.ts laeuft
zwangslaeufig ueber den Service-Role-Client (RLS laesst anonyme profiles-Zugriffe
nicht zu), bekam aber "42501 permission denied for table profiles" -- BYPASSRLS
umgeht nur RLS, nicht die darunterliegende GRANT-Ebene. Der Fehler wurde zusaetzlich
komplett verschluckt (nur `data` destrukturiert, `error` ignoriert), sodass Login
per Benutzername fuer JEDEN Benutzernamen fehlschlug, aber mit der irrefuehrend
plausiblen generischen "nicht gefunden"-Meldung.

- Neue Migration grantet service_role SELECT (und nur SELECT) auf public.profiles.
- login() prueft jetzt den Lookup-Fehler und loggt ihn serverseitig, damit ein
  kuenftiger Regressionsfall (z.B. versehentlich entzogenes Grant) nicht wieder
  lautlos als "Benutzername nicht gefunden" maskiert wird. Die Nutzer-Antwort
  (generische LOGIN_FEHLER-Meldung) bleibt fuer beide Faelle byte-identisch, um
  die Anti-Enumeration-Eigenschaft nicht zu gefaehrden.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HCHRPWuV3bd37NJhW2NK6P
2026-08-21 01:15:24 +02:00
Jonny 0cce446e27 fix: enforce minimum length for auto-generated usernames 2026-08-20 23:41:56 +02:00
Jonny a6cbbbbc89 feat: add profiles.username column with backfill and trigger support 2026-08-20 23:41:56 +02:00
Jonny 9c00e0bb14 feat: Zeitplanung-Spalten und Check-Constraint auf auftraege 2026-08-20 19:42:06 +02:00
Jonny 9b7dc111e7 fix: RLS auf auftragsnummer_zaehler aktivieren (Defense-in-Depth)
Jede andere Tabelle in diesem Projekt aktiviert RLS konsequent, auch wenn
(noch) kein Grant an authenticated/anon besteht. auftragsnummer_zaehler
folgte dieser Konvention nicht -- aktuell harmlos, weil der fehlende
Grant den Zugriff schon auf Privilegien-Ebene blockt, aber ohne RLS wuerde
ein spaeter versehentlich ergaenzter Grant (z.B. jemand "behebt" einen
Permission-Fehler naiv per GRANT statt security definer) die Tabelle
sofort komplett offenlegen. RLS ohne Policies faengt das ab (fail closed)
und aendert nichts am bestehenden Verhalten, da die Tabelle ausschliesslich
vom security-definer-Trigger generiere_auftragsnummer() angefasst wird
(Owner-Zugriff umgeht RLS per Default, FORCE ROW LEVEL SECURITY wird
bewusst nicht gesetzt).
2026-08-20 10:27:09 +02:00
Jonny d11d23feaa feat: Auftragsnummer-Trigger, Zaehler und Backfill
security definer + set search_path auf generiere_auftragsnummer()
ergaenzt (Abweichung vom Task-Brief): ohne das schlaegt jeder Insert als
"authenticated" mit "permission denied for table
auftragsnummer_zaehler" fehl, da die Funktion sonst invoker-mode laeuft
und der Zaehler-Tabelle absichtlich kein Grant an authenticated erteilt
wird (sonst waere der Tageszaehler ueber die REST-API frei
lesbar/schreibbar). Gleiches Muster wie
check_auftrag_status_transition() in
20260705145543_auftrag_status_trigger.sql.
2026-08-20 10:21:51 +02:00
Jonny b623805530 feat: telefon/email Spalten auf orte ergaenzen 2026-08-19 12:11:01 +02:00
Jonny 038e2e422b feat: Fahrer/Readonly sehen Admin- und Disponent-Profile (RLS)
Neue Policy fuer Ersteller-Anzeige im Auftrag (Task 2/3 folgen). Bestehende
Assertion "Fahrer sieht nur das eigene Profil" testete eine Einschraenkung,
die dieses Feature bewusst aufhebt -- Erwartungswert und Titel angepasst,
statt der Policy, auf Anweisung des Plan-Autors.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HCHRPWuV3bd37NJhW2NK6P
2026-08-19 10:24:36 +02:00
Jonny eaa5faad7b fix: restore dropped explanatory comment in trigger function
Restore the security-review context comment that explains why the
storniert status check is necessary. This comment was part of the
original trigger function body and should be preserved for future
maintainers who inspect the function directly in the database.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HCHRPWuV3bd37NJhW2NK6P
2026-08-18 16:52:39 +02:00
Jonny 9f410cbe80 fix: remove photo-count check from DB trigger, photos now optional at Fahrtbeginn
- Add migration 20260818120000_auftrag_fotos_optional_trigger.sql that replaces
  check_auftrag_status_transition() function, removing ONLY the photo-count check
  while preserving all other validation (start_km, end_km, uebergabe_bestaetigt_at,
  status transitions, column lockdown, role-based restrictions)
- Update pgTAP test: plan(13) -> plan(12), replace photo-mandatory test with
  test verifying transition succeeds without photos
- Completes DB-level optional photos feature (app-level optional already done in Task 1)

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HCHRPWuV3bd37NJhW2NK6P
2026-08-18 16:32:34 +02:00
Jonny 90ed4d764f fix: unify Zeiterfassung timezone display and add DB guard against double clock-in
Page and CSV export now both render shift timestamps in Europe/Berlin
consistently (previously CSV used UTC, page used server-local time,
causing the same shift to show different clock times in each place).
Also adds a partial unique index so two near-simultaneous Kommen
requests for the same fahrer can no longer create two open shifts --
the existing app-layer check was check-then-act with no DB backstop.
2026-07-14 10:27:50 +02:00
Jonny b49314e67f fix: close arbeitszeiten INSERT audit-trail gap and policy name length
INSERT policy now requires a fahrer's own shift to start open and
uncorrected (geht_um/korrigiert_von/korrigiert_am all null) — closes a
gap where a fahrer could forge a pre-corrected row. Also shortens a
policy name that exceeded Postgres' 63-byte identifier limit and was
being silently truncated.
2026-07-13 16:24:53 +02:00
Jonny 8d76230f02 feat: add arbeitszeiten table with RLS and fahrer column-lockdown trigger 2026-07-13 16:11:38 +02:00
Jonny bb9ac2afe6 feat: allow disponent to see fahrer profiles for Kanban assignment 2026-07-07 15:35:40 +02:00
Jonny c758dd2305 fix: allow schaden photos during unterwegs and close final-review gaps
Final whole-branch review before merging feature/auftragsverwaltung found
two Important and three Minor issues, all fixed in this round:

- auftrag_fotos and storage.objects INSERT policies now split the
  condition: Pflicht-positions still require status = 'zugewiesen', but
  schaden photos may additionally be uploaded while status = 'unterwegs',
  matching the spec's "jederzeit waehrend der Fahrt nachtragen" requirement.
- Added schadenFotoNachtragen server action + a page form so drivers can
  actually use the new mid-trip upload window.
- Added supabase/tests/auftrag_fotos_rls_test.sql (12 pgTAP tests) covering
  SELECT/INSERT RLS on both auftrag_fotos and storage.objects, including the
  new unterwegs-schaden-allowed / unterwegs-pflicht-blocked cases.
- fahrtBeginnen now checks affected row count like fahrtBeenden/stornieren
  instead of only checking `error`.
- /auftraege/neu now gates on role (admin/disponent) via loadProfile(),
  redirecting non-privileged roles back to /auftraege with a visible error
  (which now renders the error query param).
- Renamed the over-long storage INSERT policy name to stay under the 63
  byte Postgres identifier limit.

Migrations were edited in place rather than layered with a new migration,
following the established precedent (55c4ac5) for pre-merge, not-yet-deployed
RLS fixes on this branch.

Verified: npx supabase db reset clean, npx supabase test db 33/33 passing
(was 21), npm run build and npm test green in web/, and a live rolled-back
SQL probe confirming a driver can insert a schaden photo but not a Pflicht
photo while status = 'unterwegs'.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-06 06:57:22 +02:00
Jonny 58e34a14a6 fix: close stornieren RLS gap and harden fahrtBeenden/stornieren
Security review (automated scan + Task 11 reviewer) found a fahrer could
storniere their own assigned order because Task 2's fahrer UPDATE policy
only constrains zugewiesen_an, never status. Adds a role-gated check to
the status trigger (only admin/disponent may set status='storniert') plus
a pgTAP regression test. Also hardens fahrtBeenden/stornieren to check
affected-row counts instead of silently redirecting to a success page on
a blocked update.

Live-verified: fahrer storno attempt now rejected by the trigger; normal
fahrer fahrtBeenden flow unaffected. db reset + test db (21/21), build,
and npm test (17/17) all pass.
2026-07-06 06:22:53 +02:00
Jonny 4407c6de0c feat: add auftrag-fotos storage bucket with RLS policies 2026-07-05 17:12:18 +02:00
Jonny dd725b5027 feat: add auftrag status transition trigger with pgTAP coverage 2026-07-05 17:01:43 +02:00
Jonny bb4f276c72 feat: add auftrag_fotos table with RLS and required-photo constraint 2026-07-05 16:47:26 +02:00
Jonny 55c4ac5949 fix: gate auftraege RLS policies on user_role() to close readonly claim gap
Security review found two RLS gaps in the auftraege migration that exactly
followed the (flawed) plan:

- The fahrer UPDATE policy had no public.user_role() = 'fahrer' predicate,
  so any authenticated user -- including a readonly-role account (the
  default role for new signups) -- could self-assign any open order via
  the "status = 'offen'" branch of USING, since all app roles share the
  same Postgres role and only user_role() distinguishes them.
- The disponent/admin INSERT policy did not bind erstellt_von to
  auth.uid(), allowing a spoofed creator id on insert.

Adds public.user_role() = 'fahrer' to both USING and WITH CHECK on the
fahrer UPDATE policy, and erstellt_von = auth.uid() to the INSERT policy's
WITH CHECK. Column-level lockdown (fahrer changing abholort_id etc.) is
intentionally deferred to Task 4's trigger, not addressed here.

Verified behaviorally: with the pre-fix policy, a readonly-role user could
claim an open order (UPDATE 1); with the fix, the same attempt affects 0
rows. Migration amended in place since it was never applied outside this
local dev stack.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-05 11:27:27 +02:00
Jonny 8ed34cfa96 feat: add auftraege table with status enum and RLS 2026-07-05 11:08:47 +02:00
Jonny 0850f0ccfc feat: add orte and fahrzeuge lookup tables with RLS 2026-07-05 10:59:55 +02:00
Jonny d03052ca73 test: cover profiles UPDATE RLS boundary and privilege-escalation trigger
Final review found the RLS test suite only covered SELECT, never the
actual security-relevant path: a non-admin changing a profile's role.
Add pgTAP coverage for a fahrer's own-role UPDATE being blocked by RLS
(0 rows affected, role unchanged), the positive admin-can-update-others
case, and a regression test for handle_new_user() ignoring
raw_user_meta_data.role so a future edit reintroducing that read fails
loudly instead of quietly reopening the escalation hole.

Also add a migration giving the admin UPDATE policy an explicit
WITH CHECK (behavior unchanged, since Postgres already falls back to
USING for UPDATE policies without one) to document the pattern for
future sub-projects copying this RLS setup.
2026-07-05 00:45:27 +02:00
Jonny d13994b386 feat: add RLS policies and pgTAP tests for profiles 2026-07-04 11:58:09 +02:00
Jonny 92adebd710 fix: never derive new user role from raw_user_meta_data (privilege escalation)
Fixes a CRITICAL finding from automated security review of a9e67ce: the
handle_new_user() trigger read the role from
new.raw_user_meta_data ->> 'role', which is attacker-controllable in
certain GoTrue flows (OAuth, invite links). A user could set
role: admin in their own signup metadata and self-escalate.

Every new user now always gets role = 'readonly' on creation; an admin
must explicitly promote via SQL/Table Editor afterwards (enforced later
by the "Admins aktualisieren alle Profile" RLS policy in Task 3).

Edited the already-applied migration file directly rather than adding a
new one, since this project has no deployment/production data yet.

Verified: inserting a user with raw_user_meta_data = {"role": "admin"}
still results in role = readonly in public.profiles.
2026-07-04 11:48:26 +02:00
Jonny a9e67ce04d feat: add profiles table, role enum and auto-create trigger 2026-07-04 11:30:13 +02:00