gwoe-antragspruefer/app/validators.py
Dotty Dotter 565849bd84 feat(#139,#129,#138,#141): v2-Frontend (ECOnGOOD-CD), Login-Modal, Auto-DL, OG-Cards
v2-Frontend (#139, ECOnGOOD CD Manual Juni 2024):
- app/static/v2/: tokens.css, fonts.css, v2.css, Nunito-Sans woff2, Phosphor-Icons (21 SVGs)
- app/templates/v2/: base.html + 11 Screens + 8 Component-Macros
- AppShell mit Sidebar (Lesen/Pruefen/Daten/Admin), v2-Detail mit allen Features
  (ScoreHero, MatrixMini, QuoteCard, Redline, Fraktions-Scores)
- v2 ist jetzt Default unter / — classic unter /classic
- Login-Modal in v2-Topbar mit Tabs Anmelden/Registrieren (#129)
- Phosphor-Icons in Sidebar + Topbar mit dynamischem Theme-Toggle
- Keyboard-Shortcuts (j/k/Enter/Esc/?/path), Landtag-Suche, Antrag-Historie,
  Sort-Dropdown, Matrix-Feld-Info-Modal, Bookmarks/Comments/Voting/Share/Re-Analyze

Backend-Erweiterungen:
- main.py: ~30 neue Routes (/v2/*, /antrag/{ds}, /api/auth/{login,refresh,logout},
  /api/me/merkliste/*, /api/admin/*, /v2/admin/*, OG-Cards, etc.)
- og_card.py + og_template: Open-Graph-Bilder via Playwright (#141)
- wahlprogramm_fetch.py + wahlprogramm-links.yaml: SHA-Gate Auto-DL (#138)
- auswertungen.py: BL-Filter + get_wahlperioden Helper (#137)
- auth.py: Direct-Access-Grant + Refresh-Token-Cookie

Classic-Updates:
- Header-DRY via _header.html, Auswertungen redirected, Batch-Inline raus

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-25 20:55:57 +02:00

48 lines
1.6 KiB
Python

"""Input-Validators für die FastAPI-Endpoints.
Eigenes Modul, damit die Unit-Tests die Helper direkt importieren können
ohne den vollen FastAPI-/slowapi-Stack aus app.main mitzuziehen. Issue
#57 Befunde #3 (Path-Traversal via drucksache) und #7 (Search-Query-DoS).
"""
from __future__ import annotations
import re
from fastapi import HTTPException
# Drucksache-Format: erlaubt sind alle bisher beobachteten Schreibweisen:
# "8/6390", "18/12345", "8/6390(neu)", "23/3700-A", "21/754S" (HB: S=Stadtbürgerschaft).
# Restriktiv genug für Path-Traversal-Schutz (#57 Befund #3).
_DRUCKSACHE_RE = re.compile(r"^\d{1,3}/\d{1,7}[A-Z]?([-(].{1,20})?$")
def validate_drucksache(drucksache: str) -> str:
"""Lehnt jede Drucksache-ID ab, die nicht dem erwarteten Format
entspricht. Gemeinsamer Validation-Funnel für alle Endpoints, die
``drucksache`` als Parameter nehmen. Issue #57 Befund #3.
"""
if not drucksache or not _DRUCKSACHE_RE.match(drucksache):
raise HTTPException(
status_code=400,
detail=f"Ungültige Drucksache-ID: {drucksache!r}",
)
return drucksache
# Längen-Limit für freie Search-Queries — verhindert, dass jemand mit einem
# 10-MB-Query die SQL-LIKE-Pattern oder den HTTP-Adapter aushungert.
# Issue #57 Befund #7.
MAX_SEARCH_QUERY_LEN = 200
def validate_search_query(q: str) -> str:
if q is None:
raise HTTPException(status_code=400, detail="Suchbegriff fehlt")
if len(q) > MAX_SEARCH_QUERY_LEN:
raise HTTPException(
status_code=400,
detail=f"Suchbegriff zu lang (max {MAX_SEARCH_QUERY_LEN} Zeichen, war {len(q)})",
)
return q