From 7da2caf3ca7502f8c34af401215c88f0a6ec5bc8 Mon Sep 17 00:00:00 2001 From: Artu-Rito Date: Wed, 18 Nov 2026 17:37:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D1=83=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D1=80=D0=BE=D0=B2=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index b573473..0c3299d 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,7 @@ from datetime import datetime, timezone import asyncpg from fastapi import FastAPI, Header, Request +from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse, Response from pydantic import BaseModel, ConfigDict, Field, ValidationError @@ -64,6 +65,15 @@ async def api_error_handler(request: Request, exc: ApiError): return _error_response(exc, getattr(request.state, "request_id", uuid.uuid4().hex)) +@app.exception_handler(RequestValidationError) +async def request_validation_error_handler(request: Request, exc: RequestValidationError): + """Keep FastAPI's query/path validation inside the public error contract.""" + return _error_response( + bad_request("invalid request parameters"), + getattr(request.state, "request_id", uuid.uuid4().hex), + ) + + def iso(value: datetime) -> str: return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") @@ -109,7 +119,10 @@ def decode_cursor(value: str) -> dict: try: padded = value + "=" * (-len(value) % 4) raw = base64.urlsafe_b64decode(padded.encode()) - return json.loads(raw) + payload = json.loads(raw) + if not isinstance(payload, dict): + raise bad_request("malformed cursor") + return payload except (binascii.Error, ValueError, UnicodeDecodeError): raise bad_request("malformed cursor") @@ -208,7 +221,7 @@ async def health_ready(): @app.post("/api/v1/users", status_code=201) async def register_user(request: Request): body = await parse_body(request, CreateUser) - if not USERNAME_RE.match(body.username): + if not USERNAME_RE.fullmatch(body.username): raise bad_request("username must match ^[a-z0-9_]{3,32}$") if not body.display_name.strip(): raise bad_request("display_name must not be blank")