-
Notifications
You must be signed in to change notification settings - Fork 7
sec: relax fastapi upper bound + floor-pin tornado (CVE-2025-62727, CVE-2026-31958) #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
e5a6e15
427ab61
e26b11d
7efcf99
1b04fae
408b1a0
548a016
a816bfd
03fadf7
6fa9d54
264a8f4
0bcda2f
8f007bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,8 +11,8 @@ | |||||
| import uvicorn | ||||||
| from fastapi import FastAPI, Request | ||||||
| from pydantic import TypeAdapter, ValidationError | ||||||
| from starlette.types import Send, Scope, ASGIApp, Receive | ||||||
| from fastapi.responses import StreamingResponse | ||||||
| from starlette.middleware.base import BaseHTTPMiddleware | ||||||
|
|
||||||
| from agentex.lib.types.acp import ( | ||||||
| RPC_SYNC_METHODS, | ||||||
|
|
@@ -43,17 +43,25 @@ | |||||
| task_message_update_adapter = TypeAdapter(TaskMessageUpdate) | ||||||
|
|
||||||
|
|
||||||
| class RequestIDMiddleware(BaseHTTPMiddleware): | ||||||
| """Middleware to extract or generate request IDs and add them to logs and response headers""" | ||||||
| class RequestIDMiddleware: | ||||||
| """Pure ASGI middleware to extract or generate request IDs and set them in the logging context. | ||||||
|
|
||||||
| async def dispatch(self, request: Request, call_next): # type: ignore[override] | ||||||
| # Extract request ID from header or generate a new one if there isn't one | ||||||
| request_id = request.headers.get("x-request-id") or uuid.uuid4().hex | ||||||
| # Store request ID in request state for access in handlers | ||||||
| ctx_var_request_id.set(request_id) | ||||||
| # Process request | ||||||
| response = await call_next(request) | ||||||
| return response | ||||||
| Implemented as a pure ASGI middleware (rather than BaseHTTPMiddleware) so that it never | ||||||
| buffers the response body. BaseHTTPMiddleware's call_next() silently swallows | ||||||
| StreamingResponse bodies in several starlette versions, which caused message/send handlers | ||||||
| to return result=null through the Agentex server proxy. | ||||||
| """ | ||||||
|
|
||||||
| def __init__(self, app: ASGIApp) -> None: | ||||||
| self.app = app | ||||||
|
|
||||||
| async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||||||
| if scope["type"] == "http": | ||||||
| headers = dict(scope.get("headers", [])) | ||||||
| raw_request_id = headers.get(b"x-request-id", b"") | ||||||
| request_id = raw_request_id.decode() if raw_request_id else uuid.uuid4().hex | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The HTTP/1.1 spec (RFC 7230) specifies that header field values are ISO-8859-1 / Latin-1, so
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agentex/lib/sdk/fastacp/base/base_acp_server.py
Line: 62
Comment:
**`decode()` can raise `UnicodeDecodeError` on malformed headers**
`raw_request_id.decode()` uses UTF-8 by default. If a client (or an upstream proxy) sends a non-UTF-8 byte sequence in the `x-request-id` header, this will raise an unhandled `UnicodeDecodeError` that propagates through the ASGI stack, causing a 500 for the request.
The HTTP/1.1 spec (RFC 7230) specifies that header field values are ISO-8859-1 / Latin-1, so `decode('latin-1')` is both spec-correct and will never raise an exception (every byte sequence is valid Latin-1).
```suggestion
request_id = raw_request_id.decode("latin-1") if raw_request_id else uuid.uuid4().hex
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||
| ctx_var_request_id.set(request_id) | ||||||
| await self.app(scope, receive, send) | ||||||
|
|
||||||
|
|
||||||
| class BaseACPServer(FastAPI): | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.