forked from SamohvalovAndreyBPI2302/mtusi2026lab1
Harden request bounds and verify benchmark responses
This commit is contained in:
+6
-2
@@ -30,9 +30,13 @@ def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dic
|
||||
|
||||
|
||||
def parse_limit(value: str) -> int:
|
||||
if not value.isdecimal() or not 1 <= int(value) <= 100:
|
||||
try:
|
||||
number = int(value) if value.isdecimal() else 0
|
||||
except ValueError:
|
||||
raise HTTPException(400) from None
|
||||
if not 1 <= number <= 100:
|
||||
raise HTTPException(400)
|
||||
return int(value)
|
||||
return number
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
|
||||
+8
-1
@@ -10,6 +10,7 @@ from .api import router
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(router)
|
||||
MAX_JSON_BYTES = 1_048_576
|
||||
|
||||
ERRORS = {
|
||||
400: ("invalid_request", "Неверный запрос"),
|
||||
@@ -41,7 +42,13 @@ async def request_contract(request: Request, call_next):
|
||||
if media_type.strip() != "application/json" or (charset and charset.strip() != "charset=utf-8"):
|
||||
return error_response(request, 415)
|
||||
try:
|
||||
(await request.body()).decode("utf-8")
|
||||
body = bytearray()
|
||||
async for chunk in request.stream():
|
||||
if len(body) + len(chunk) > MAX_JSON_BYTES:
|
||||
return error_response(request, 413)
|
||||
body.extend(chunk)
|
||||
request._body = bytes(body)
|
||||
request._body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return error_response(request, 400)
|
||||
response = await call_next(request)
|
||||
|
||||
Reference in New Issue
Block a user