Skip to content

Commit 002abb0

Browse files
florentinlalyshawang
authored andcommitted
feat(appsec): enable api security for lambda (#14118)
## Description Add a handler for lambda to send a response body to parse. Additionally fix one typo ## Additional Notes This is enabled in datadog_lambda by this PR: DataDog/datadog-lambda-python#636 ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - Backport labels are set in a manner that is consistent with the [release branch maintenance policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
1 parent faea547 commit 002abb0

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

ddtrace/appsec/_handlers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
from typing import Any
55
from typing import Dict
66
from typing import Optional
7+
from typing import Union
78

89
import xmltodict
910

1011
from ddtrace._trace.span import Span
1112
from ddtrace.appsec._asm_request_context import _call_waf
1213
from ddtrace.appsec._asm_request_context import _call_waf_first
1314
from ddtrace.appsec._asm_request_context import get_blocked
15+
from ddtrace.appsec._asm_request_context import set_body_response
1416
from ddtrace.appsec._constants import SPAN_DATA_NAMES
1517
from ddtrace.appsec._http_utils import extract_cookies_from_headers
1618
from ddtrace.appsec._http_utils import normalize_headers
@@ -157,6 +159,14 @@ def _on_lambda_start_response(
157159
_call_waf(("aws_lambda",))
158160

159161

162+
def _on_lambda_parse_body(
163+
response_body: Optional[Union[str, Dict[str, Any]]],
164+
):
165+
if asm_config._api_security_feature_active:
166+
if response_body:
167+
set_body_response(response_body)
168+
169+
160170
# ASGI
161171

162172

@@ -408,6 +418,7 @@ def listen():
408418

409419
core.on("aws_lambda.start_request", _on_lambda_start_request)
410420
core.on("aws_lambda.start_response", _on_lambda_start_response)
421+
core.on("aws_lambda.parse_body", _on_lambda_parse_body)
411422

412423
core.on("grpc.server.response.message", _on_grpc_server_response)
413424
core.on("grpc.server.data", _on_grpc_server_data)

ddtrace/appsec/_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def on_span_start(self, span: Span) -> None:
189189
if skip_event:
190190
core.discard_item("appsec_skip_next_lambda_event")
191191
log.debug(
192-
"appsec: ignoring unsupported lamdba event",
192+
"appsec: ignoring unsupported lambda event",
193193
)
194194
span.set_metric(APPSEC.UNSUPPORTED_EVENT_TYPE, 1.0)
195195
return

ddtrace/settings/asm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,8 @@ def __init__(self):
246246
self._asm_processed_span_types.add(SpanTypes.SERVERLESS)
247247
self._asm_http_span_types.add(SpanTypes.SERVERLESS)
248248

249-
# As a first step, only Threat Management in monitoring mode should be enabled in AWS Lambda
249+
# Disable all features that are not supported in Lambda
250250
tracer_config._remote_config_enabled = False
251-
self._api_security_enabled = False
252251
self._ep_enabled = False
253252
self._iast_supported = False
254253

0 commit comments

Comments
 (0)