forked from mtusi2026/mtusi2026lab4
Serve attachment upload, metadata and download through the API
Add the adapters behind the attachments ports: PostgreSQL for the pending and ready rows, with a foreign key violation on the chat member turned into 404; Pillow that opens only PNG and JPEG, refuses a type that differs from Content-Type, checks the pixel count from the header before any decoding, verifies PNG chunks and decodes JPEG at 1/8 scale in a worker thread under a semaphore; and S3 on aiobotocore with path style, SigV4, short timeouts and checksums only when required. botocore sends Expect: 100-continue for streamed bodies and RustFS behind the Docker port proxy never answers it, so the PUT hung until the read timeout; the handler that adds the header is unregistered. The HTTP layer streams the raw body into the use case, returns the contract Attachment and streams the original back with its content type and length and no redirect. The request pipeline takes a body limit per route, so uploads may carry 10 MiB while JSON stays at 1 MiB. main.py wires the zone and answers its membership port from the messaging chat directory. Tests run the API with an in-memory object store: download by another member, 404 for strangers, the exact 10 MiB limit and one byte over it with Content-Length and chunked, 40 MP and above, spoofed and truncated images, unsupported types, dangerous names, the variant, a failing store and a lost object. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
76c9673ab3
commit
00826a40d3
@@ -1,4 +1,5 @@
|
||||
import asyncio
|
||||
import re
|
||||
import time
|
||||
from collections.abc import Awaitable, Callable
|
||||
from dataclasses import dataclass
|
||||
@@ -22,6 +23,7 @@ class Rejection:
|
||||
|
||||
|
||||
type Guard = Callable[[Scope], Awaitable[Rejection | None]]
|
||||
type BodyLimits = tuple[tuple[re.Pattern[str], int], ...]
|
||||
|
||||
HEADER_BYTES = REQUEST_ID_HEADER.lower().encode()
|
||||
INSTANCE_BYTES = INSTANCE_ID_HEADER.lower().encode()
|
||||
@@ -56,7 +58,7 @@ async def send_error(
|
||||
|
||||
|
||||
class RequestPipeline:
|
||||
__slots__ = ("_app", "_guard", "_instance", "_logger", "_max_bytes", "_timeout")
|
||||
__slots__ = ("_app", "_guard", "_instance", "_limits", "_logger", "_max_bytes", "_timeout")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -66,10 +68,12 @@ class RequestPipeline:
|
||||
timeout: float,
|
||||
guard: Guard,
|
||||
instance: str,
|
||||
limits: BodyLimits = (),
|
||||
) -> None:
|
||||
self._app = app
|
||||
self._logger = logger
|
||||
self._max_bytes = max_bytes
|
||||
self._limits = limits
|
||||
self._timeout = timeout
|
||||
self._guard = guard
|
||||
self._instance = instance.encode()
|
||||
@@ -103,7 +107,7 @@ class RequestPipeline:
|
||||
await send(message)
|
||||
|
||||
declared = header(scope, b"content-length")
|
||||
if declared.isdigit() and int(declared) > self._max_bytes:
|
||||
if declared.isdigit() and int(declared) > self._limit(scope["path"]):
|
||||
await send_error(wrapped_send, 413, "payload_too_large", request_id)
|
||||
self._log(scope, status_holder[0], request_id, started)
|
||||
return
|
||||
@@ -150,6 +154,12 @@ class RequestPipeline:
|
||||
)
|
||||
self._log(scope, status_holder[0], request_id, started)
|
||||
|
||||
def _limit(self, path: str) -> int:
|
||||
for pattern, limit in self._limits:
|
||||
if pattern.fullmatch(path):
|
||||
return limit
|
||||
return self._max_bytes
|
||||
|
||||
def _route(self, scope: Scope) -> str:
|
||||
route = scope.get("route")
|
||||
return getattr(route, "path", scope["path"])
|
||||
|
||||
Reference in New Issue
Block a user