Files
MABEA/backend/alembic/versions/0007_indizes_und_unique_constraints.py
T
patrickandClaude Sonnet 5 ceddeb1bf4
CI / backend-tests (push) Failing after 19s
CI / frontend-build (push) Successful in 25s
Multi-Agent-Review: Race Conditions, DB-Indizes, TLS/Security-Härtung
Konsolidierte Funde aus postgres-/sql-/jwt-/owasp-top10-expert-Review:

- Race Conditions gefixt: doppelte aktive Kontrolle (SAVEPOINT + partieller
  Unique-Index), doppelte Mindermengen-Genehmigung (FOR UPDATE + Unique-Index),
  Lost-Update bei Nachfüllung (FOR UPDATE auf Fehlbestand/Objektposition).
- Migration 0007: partielle Unique-Indizes als DB-Sicherheitsnetz + fehlende
  FK-Indizes (fehlbestand.material_id, kontrolle(objekt_id,status),
  zustaendigkeit, benutzer_rolle.rolle, objektposition.ablaufdatum u.a.).
- Connection-Pool explizit begrenzt (pool_size=5, max_overflow=5) - ohne das
  könnte jeder uvicorn-Worker den Postgres max_connections-Wert sprengen.
- Timing-Angriff bei Login-Enumeration gefixt (konstante Antwortzeit über
  Dummy-Hash), JWT-Decode verlangt jetzt exp/sub-Claims.
- App-seitiges Rate-Limiting (slowapi, 5/min) auf /auth/login als Verteidigung
  in der Tiefe zusätzlich zum nginx-Limit.
- nginx: TLS mit selbstsigniertem Zertifikat (LAN-Betrieb, keine Domain),
  HSTS, Content-Security-Policy, Permissions-Policy ergänzt.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVgbozhYmuEhiEJHffRXCV
2026-09-04 18:47:06 +02:00

64 lines
2.8 KiB
Python

"""Review-Funde (postgres-/sql-expert): fehlende FK-Indizes + partielle Unique-
Constraints gegen Race Conditions (doppelte aktive Kontrolle/Mindermenge)
Revision ID: 0007_indizes_und_unique_constraints
Revises: 0006_add_eskalation_felder
Create Date: 2026-09-04
Zwei unabhängige Fixes aus dem Agenten-Review dieser Session:
1. Partielle Unique-Indizes als DB-Sicherheitsnetz gegen die TOCTOU-Race
zwischen Check und Insert in starte_kontrolle()/genehmigen() - Anwendungscode
fängt die resultierende IntegrityError bereits ab (lifecycle.py, mindermenge.py).
2. Fehlende Indizes auf häufig gefilterten Fremdschlüsseln (Eskalations-Cron,
Kontroll-Sperre-Check, Dashboard-Ablaufdatum-Abfrage u.a.).
"""
from typing import Sequence, Union
from alembic import op
revision: str = "0007_indizes_und_unique_constraints"
down_revision: Union[str, None] = "0006_add_eskalation_felder"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute(
"""
CREATE UNIQUE INDEX idx_kontrolle_eine_aktive_je_objekt
ON kontrolle (objekt_id) WHERE status = 'in_bearbeitung';
CREATE UNIQUE INDEX idx_mindermenge_eine_aktive_je_fehlbestand
ON mindermengen_genehmigung (fehlbestand_id) WHERE status = 'aktiv';
CREATE INDEX idx_fehlbestand_material ON fehlbestand (material_id);
CREATE INDEX idx_fehlbestand_status_entstanden ON fehlbestand (status, entstanden_am);
CREATE INDEX idx_kontrolle_objekt_status ON kontrolle (objekt_id, status);
CREATE INDEX idx_kontrollposition_kontrolle ON kontrollposition (kontrolle_id);
CREATE INDEX idx_zustaendigkeit_objekt ON zustaendigkeit (objekt_id);
CREATE INDEX idx_zustaendigkeit_standort ON zustaendigkeit (standort_id);
CREATE INDEX idx_benutzer_rolle_rolle ON benutzer_rolle (rolle);
CREATE INDEX idx_objektposition_material ON objektposition (material_id);
CREATE INDEX idx_nachfuellung_fehlbestand ON nachfuellung (fehlbestand_id);
CREATE INDEX idx_objektposition_ablauf ON objektposition (ablaufdatum) WHERE ablaufdatum IS NOT NULL;
"""
)
def downgrade() -> None:
op.execute(
"""
DROP INDEX idx_objektposition_ablauf;
DROP INDEX idx_nachfuellung_fehlbestand;
DROP INDEX idx_objektposition_material;
DROP INDEX idx_benutzer_rolle_rolle;
DROP INDEX idx_zustaendigkeit_standort;
DROP INDEX idx_zustaendigkeit_objekt;
DROP INDEX idx_kontrollposition_kontrolle;
DROP INDEX idx_kontrolle_objekt_status;
DROP INDEX idx_fehlbestand_status_entstanden;
DROP INDEX idx_fehlbestand_material;
DROP INDEX idx_mindermenge_eine_aktive_je_fehlbestand;
DROP INDEX idx_kontrolle_eine_aktive_je_objekt;
"""
)