Skip to content

fix(aap): properly manage large async request bodies #13921

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 3 additions & 71 deletions .gitlab/benchmarks/bp-runner.microbenchmarks.fail-on-breach.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,80 +26,12 @@ experiments:
- name: flasksimple-appsec-get
thresholds:
- execution_time < 4.75 ms
- max_rss_usage < 62.10 MB
- max_rss_usage < 63.30 MB
- name: flasksimple-appsec-post
thresholds:
- execution_time < 6.75 ms
- max_rss_usage < 62.40 MB
- max_rss_usage < 63.40 MB
- name: flasksimple-appsec-telemetry
thresholds:
- execution_time < 4.75 ms
- max_rss_usage < 62.40 MB

# span
- name: span-add-event
thresholds:
- execution_time < 26.20 ms
- max_rss_usage < 49.00 MB
- name: span-add-metrics
thresholds:
- execution_time < 98.35 ms
- max_rss_usage < 961.00 MB
- name: span-add-tags
thresholds:
- execution_time < 168.55 ms
- max_rss_usage < 962.50 MB
- name: span-get-context
thresholds:
- execution_time < 23.70 ms
- max_rss_usage < 47.50 MB
- name: span-is-recording
thresholds:
- execution_time < 23.90 ms
- max_rss_usage < 47.50 MB
- name: span-record-exception
thresholds:
- execution_time < 44.50 ms
- max_rss_usage < 40.50 MB
- name: span-set-status
thresholds:
- execution_time < 26.00 ms
- max_rss_usage < 47.50 MB
- name: span-start
thresholds:
- execution_time < 23.50 ms
- max_rss_usage < 47.50 MB
- name: span-start-finish
thresholds:
- execution_time < 52.50 ms
- max_rss_usage < 29.50 MB
- name: span-start-finish-telemetry
thresholds:
- execution_time < 55.30 ms
- max_rss_usage < 29.50 MB
- name: span-start-finish-traceid128
thresholds:
- execution_time < 56.05 ms
- max_rss_usage < 29.50 MB
- name: span-start-traceid128
thresholds:
- execution_time < 24.60 ms
- max_rss_usage < 47.50 MB
- name: span-update-name
thresholds:
- execution_time < 24.10 ms
- max_rss_usage < 48.00 MB

# tracer
- name: tracer-large
thresholds:
- execution_time < 32.95 ms
- max_rss_usage < 30.50 MB
- name: tracer-medium
thresholds:
- execution_time < 3.20 ms
- max_rss_usage < 29.50 MB
- name: tracer-small
thresholds:
- execution_time < 0.37 ms
- max_rss_usage < 29.50 MB
- max_rss_usage < 63.40 MB
17 changes: 14 additions & 3 deletions ddtrace/appsec/_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import io
import json
from typing import Any
Expand Down Expand Up @@ -161,19 +162,29 @@ def _on_lambda_start_response(

async def _on_asgi_request_parse_body(receive, headers):
if asm_config._asm_enabled:
more_body = True
body_parts = []
try:
data_received = await receive()
while more_body:
data_received = await asyncio.wait_for(receive(), asm_config._fast_api_async_body_timeout)
if data_received is None:
more_body = False
if isinstance(data_received, dict):
more_body = data_received.get("more_body", False)
body_parts.append(data_received.get("body", b""))
except asyncio.TimeoutError:
pass
except Exception:
return receive, None
body = b"".join(body_parts)

async def receive_wrapped(once=[True]):
if once[0]:
once[0] = False
return data_received
return {"type": "http.request", "body": body, "more_body": more_body}
return await receive()

try:
body = data_received.get("body", b"")
content_type = headers.get("content-type") or headers.get("Content-Type")
if content_type in ("application/json", "text/json"):
if body is None or body == b"":
Expand Down
4 changes: 4 additions & 0 deletions ddtrace/settings/asm.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class ASMConfig(DDConfig):
_django_include_user_login = DDConfig.var(bool, "DD_DJANGO_INCLUDE_USER_LOGIN", default=True)
_django_include_user_realname = DDConfig.var(bool, "DD_DJANGO_INCLUDE_USER_REALNAME", default=False)

# FASTAPI ASYNC
# Timeout for the request body reading in seconds.
_fast_api_async_body_timeout = DDConfig.var(float, "DD_FASTAPI_ASYNC_BODY_TIMEOUT_SECONDS", default=0.1)

# for tests purposes
_asm_config_keys = [
"_asm_enabled",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
AAP: This fix resolves an issue where the FastAPI body extraction was not functioning correctly in asynchronous contexts for large bodies, leading to missing security events.
The timeout for reading request body chunks has been set to 0.1 seconds to ensure timely processing without blocking the event loop.
This can be configured using the ``DD_FASTAPI_ASYNC_BODY_TIMEOUT_SECONDS`` environment variable.
2 changes: 2 additions & 0 deletions tests/telemetry/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def test_app_started_event(telemetry_writer, test_agent_session, mock_time):
{"name": "DD_DOGSTATSD_URL", "origin": "unknown", "value": None},
{"name": "DD_DYNAMIC_INSTRUMENTATION_ENABLED", "origin": "unknown", "value": False},
{"name": "DD_EXCEPTION_REPLAY_ENABLED", "origin": "unknown", "value": False},
{"name": "DD_FASTAPI_ASYNC_BODY_TIMEOUT_SECONDS", "origin": "default", "value": 0.1},
{"name": "DD_INSTRUMENTATION_TELEMETRY_ENABLED", "origin": "unknown", "value": True},
{"name": "DD_PROFILING_STACK_ENABLED", "origin": "unknown", "value": True},
{"name": "DD_PROFILING_MEMORY_ENABLED", "origin": "unknown", "value": True},
Expand Down Expand Up @@ -377,6 +378,7 @@ def test_app_started_event_configuration_override(test_agent_session, run_python
{"name": "DD_ERROR_TRACKING_HANDLED_ERRORS_INCLUDE", "origin": "default", "value": ""},
{"name": "DD_EXCEPTION_REPLAY_CAPTURE_MAX_FRAMES", "origin": "default", "value": 8},
{"name": "DD_EXCEPTION_REPLAY_ENABLED", "origin": "env_var", "value": True},
{"name": "DD_FASTAPI_ASYNC_BODY_TIMEOUT_SECONDS", "origin": "default", "value": 0.1},
{"name": "DD_IAST_DEDUPLICATION_ENABLED", "origin": "default", "value": True},
{"name": "DD_IAST_ENABLED", "origin": "default", "value": False},
{"name": "DD_IAST_MAX_CONCURRENT_REQUESTS", "origin": "default", "value": 2},
Expand Down
Loading