forked from kenzuya/unshackle
fix(api): take 256 cache files and an 8 MiB body on the login routes
This commit is contained in:
@@ -179,14 +179,21 @@ def categorized(handler: Handler) -> Handler:
|
||||
return wrapper
|
||||
|
||||
|
||||
async def json_body(request: web.Request) -> Any:
|
||||
"""The request's JSON object; raises INVALID_INPUT when it does not parse or is not an object."""
|
||||
MAX_LOGIN_BODY_BYTES = 8 * 1024 * 1024
|
||||
|
||||
|
||||
async def json_body(request: web.Request, max_size: Optional[int] = None) -> Any:
|
||||
"""The request's JSON object; raises INVALID_INPUT when it does not parse or is not an object.
|
||||
|
||||
``max_size`` replaces the body limit of the application for this read.
|
||||
"""
|
||||
try:
|
||||
data = await request.json()
|
||||
data = await (request.clone(client_max_size=max_size) if max_size else request).json()
|
||||
except Exception as e:
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, "Invalid JSON request body", details={"error": str(e)}) from e
|
||||
if not isinstance(data, dict):
|
||||
raise APIError(APIErrorCode.INVALID_INPUT, "The request body must be a JSON object")
|
||||
request["json_body"] = data
|
||||
return data
|
||||
|
||||
|
||||
@@ -688,7 +695,7 @@ async def search(request: web.Request) -> web.Response:
|
||||
'400':
|
||||
description: Invalid request
|
||||
"""
|
||||
data = await json_body(request)
|
||||
data = await json_body(request, MAX_LOGIN_BODY_BYTES)
|
||||
return await search_handler(data, request)
|
||||
|
||||
|
||||
@@ -2112,7 +2119,7 @@ async def session_create(request: web.Request) -> web.Response:
|
||||
'401':
|
||||
description: Authentication failed
|
||||
"""
|
||||
data = await json_body(request)
|
||||
data = await json_body(request, MAX_LOGIN_BODY_BYTES)
|
||||
return await session_create_handler(data, request)
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from pathlib import PureWindowsPath
|
||||
from typing import Optional
|
||||
|
||||
MAX_CACHE_KEY_DEPTH = 8
|
||||
MAX_SESSION_CACHE_KEYS = 64
|
||||
MAX_SESSION_CACHE_KEYS = 256
|
||||
|
||||
|
||||
CONTROL_CHARS = re.compile(r"[\x00-\x1f\x7f-\x9f]")
|
||||
|
||||
@@ -388,12 +388,16 @@ def _reply_json(response: Any) -> Dict[str, Any]:
|
||||
|
||||
|
||||
async def _consumed_json(request: web.Request) -> Dict[str, Any]:
|
||||
"""The JSON object of the request body when the handler consumed it (aiohttp caches that read), else ``{}``.
|
||||
"""The JSON object of the request body when the handler consumed it, else ``{}``.
|
||||
|
||||
``json_body`` keeps the object on the request, and aiohttp caches a direct read.
|
||||
|
||||
A body nobody read belongs to a call refused before the handler, a 401 or a 429. It stays
|
||||
unread: reading it would spend time and memory on an unauthenticated caller, and wait on a
|
||||
body that never finishes arriving.
|
||||
"""
|
||||
if "json_body" in request:
|
||||
return request["json_body"]
|
||||
if not request.content.at_eof():
|
||||
return {}
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user