Skip to content

Commit 32233be

Browse files
committed
feat(appsec): enable api security
1 parent c7739eb commit 32233be

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

datadog_lambda/asm.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,19 @@ def asm_start_response(
185185
"content-type": "application/json",
186186
}
187187

188+
if isinstance(response, dict) and "statusCode" in response:
189+
body = response.get("body")
190+
else:
191+
body = response
192+
188193
core.dispatch(
189194
# The matching listener is registered in ddtrace.appsec._handlers
190195
"aws_lambda.start_response",
191196
(
192197
span,
193198
status_code,
194199
response_headers,
200+
body,
195201
),
196202
)
197203

datadog_lambda/wrapper.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ def _after(self, event, context):
318318
if status_code:
319319
self.inferred_span.set_tag("http.status_code", status_code)
320320

321+
if self.trigger_tags and (route := self.trigger_tags.get("http.route")):
322+
self.inferred_span.set_tag("http.route", route)
323+
321324
if config.service:
322325
self.inferred_span.set_tag("peer.service", config.service)
323326

tests/test_asm.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
},
107107
"200",
108108
{"Content-Type": "text/html"},
109+
None,
109110
True,
110111
),
111112
(
@@ -123,6 +124,7 @@
123124
"Content-Type": "text/plain",
124125
"X-Error": "Not Found",
125126
},
127+
None,
126128
True,
127129
),
128130
(
@@ -140,6 +142,7 @@
140142
"Location": "/user/123",
141143
"Content-Type": "application/json",
142144
},
145+
None,
143146
True,
144147
),
145148
(
@@ -158,6 +161,7 @@
158161
"Content-Type": "application/json",
159162
"X-Custom-Header": "test-value",
160163
},
164+
'{"message": "success"}',
161165
True,
162166
),
163167
(
@@ -169,6 +173,7 @@
169173
},
170174
"200",
171175
{"Content-Type": "application/json"},
176+
None,
172177
True,
173178
),
174179
(
@@ -180,6 +185,7 @@
180185
},
181186
"200",
182187
{"Content-Type": "text/plain"},
188+
None,
183189
True,
184190
),
185191
(
@@ -188,6 +194,7 @@
188194
{"statusCode": 200},
189195
"200",
190196
{},
197+
None,
191198
False, # Should not dispatch for non-HTTP events
192199
),
193200
(
@@ -196,6 +203,7 @@
196203
"Hello, World!",
197204
"200",
198205
{"content-type": "application/json"},
206+
"Hello, World!",
199207
True,
200208
),
201209
(
@@ -204,6 +212,7 @@
204212
{"message": "Hello, World!"},
205213
"200",
206214
{"content-type": "application/json"},
215+
{"message": "Hello, World!"},
207216
True,
208217
),
209218
]
@@ -326,7 +335,7 @@ def test_asm_start_request_parametrized(
326335

327336

328337
@pytest.mark.parametrize(
329-
"name,event_file,response,status_code,expected_headers,should_dispatch",
338+
"name,event_file,response,status_code,expected_headers,expected_body,should_dispatch",
330339
ASM_START_RESPONSE_TEST_CASES,
331340
)
332341
@patch("datadog_lambda.asm.core")
@@ -337,6 +346,7 @@ def test_asm_start_response_parametrized(
337346
response,
338347
status_code,
339348
expected_headers,
349+
expected_body,
340350
should_dispatch,
341351
):
342352
"""Test ASM start response for various HTTP event types using parametrization"""
@@ -362,11 +372,12 @@ def test_asm_start_response_parametrized(
362372

363373
# Extract the dispatched arguments
364374
dispatch_args = call_args[0][1]
365-
span, response_status_code, response_headers = dispatch_args
375+
span, response_status_code, response_headers, response_body = dispatch_args
366376

367377
assert span == mock_span
368378
assert response_status_code == status_code
369379
assert response_headers == expected_headers
380+
assert response_body == expected_body
370381
else:
371382
# Verify core.dispatch was not called for non-HTTP events
372383
mock_core.dispatch.assert_not_called()

0 commit comments

Comments
 (0)