Return client error for oversized pagination limits
This commit is contained in:
+12
-6
@@ -40,6 +40,16 @@ def current_user(request: Request) -> dict:
|
|||||||
return store.authenticated(bearer_token(request.headers.get("authorization")))
|
return store.authenticated(bearer_token(request.headers.get("authorization")))
|
||||||
|
|
||||||
|
|
||||||
|
def parse_limit(value: str) -> int:
|
||||||
|
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 number
|
||||||
|
|
||||||
|
|
||||||
@router.delete("/api/v1/auth/sessions/current", status_code=204)
|
@router.delete("/api/v1/auth/sessions/current", status_code=204)
|
||||||
def revoke_session(request: Request):
|
def revoke_session(request: Request):
|
||||||
store.revoke_session(bearer_token(request.headers.get("authorization")))
|
store.revoke_session(bearer_token(request.headers.get("authorization")))
|
||||||
@@ -60,9 +70,7 @@ def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
|||||||
def list_chats(
|
def list_chats(
|
||||||
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
||||||
):
|
):
|
||||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
return store.list_chats(actor["id"], parse_limit(limit), cursor)
|
||||||
raise HTTPException(400)
|
|
||||||
return store.list_chats(actor["id"], int(limit), cursor)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/api/v1/chats/{chat_id}")
|
@router.get("/api/v1/chats/{chat_id}")
|
||||||
@@ -85,6 +93,4 @@ def list_messages(
|
|||||||
cursor: str | None = Query(None, min_length=1),
|
cursor: str | None = Query(None, min_length=1),
|
||||||
actor: dict = Depends(current_user),
|
actor: dict = Depends(current_user),
|
||||||
):
|
):
|
||||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)
|
||||||
raise HTTPException(400)
|
|
||||||
return store.list_messages(actor["id"], str(chat_id), int(limit), cursor)
|
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
"""Invalid pagination limits must remain client errors."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from src.api import parse_limit
|
||||||
|
|
||||||
|
|
||||||
|
class LimitTests(unittest.TestCase):
|
||||||
|
def test_valid_and_extremely_long_limits(self):
|
||||||
|
self.assertEqual(parse_limit("100"), 100)
|
||||||
|
for value in ("0", "101", "-1", "x", "9" * 4301):
|
||||||
|
with self.assertRaises(HTTPException) as failure:
|
||||||
|
parse_limit(value)
|
||||||
|
self.assertEqual(failure.exception.status_code, 400)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user