-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathasm.py
More file actions
253 lines (212 loc) · 8.03 KB
/
asm.py
File metadata and controls
253 lines (212 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import logging
import urllib.parse
from copy import deepcopy
from typing import Any, Dict, List, Optional, Union
from ddtrace.contrib.internal.trace_utils import _get_request_header_client_ip
from ddtrace.internal import core
from ddtrace.internal.utils import get_blocked
from ddtrace.internal.utils import http as http_utils
from ddtrace.trace import Span
from datadog_lambda.trigger import (
EventSubtypes,
EventTypes,
_EventSource,
_http_event_types,
)
logger = logging.getLogger(__name__)
def _to_single_value_headers(headers: Dict[str, List[str]]) -> Dict[str, str]:
"""
Convert multi-value headers to single-value headers.
If a header has multiple values, join them with commas.
"""
single_value_headers = {}
for key, values in headers.items():
single_value_headers[key] = ", ".join(values)
return single_value_headers
def _merge_single_and_multi_value_headers(
single_value_headers: Dict[str, str],
multi_value_headers: Dict[str, List[str]],
):
"""
Merge single-value headers with multi-value headers.
If a header exists in both, we merge them removing duplicates
"""
merged_headers = deepcopy(multi_value_headers)
for key, value in single_value_headers.items():
if key not in merged_headers:
merged_headers[key] = [value]
elif value not in merged_headers[key]:
merged_headers[key].append(value)
return _to_single_value_headers(merged_headers)
def asm_set_context(event_source: _EventSource):
"""Add asm specific items to the ExecutionContext.
This allows the AppSecSpanProcessor to know information about the event
at the moment the span is created and skip it when not relevant.
"""
if event_source.event_type not in _http_event_types:
core.set_item("appsec_skip_next_lambda_event", True)
def asm_start_request(
span: Span,
event: Dict[str, Any],
event_source: _EventSource,
trigger_tags: Dict[str, str],
):
if event_source.event_type not in _http_event_types:
return
request_headers: Dict[str, str] = {}
peer_ip: Optional[str] = None
request_path_parameters: Optional[Dict[str, Any]] = None
route: Optional[str] = None
if event_source.event_type == EventTypes.ALB:
raw_uri = event.get("path")
if event_source.subtype == EventSubtypes.ALB:
request_headers = event.get("headers", {})
parsed_query = event.get("queryStringParameters")
if event_source.subtype == EventSubtypes.ALB_MULTI_VALUE_HEADERS:
request_headers = _to_single_value_headers(
event.get("multiValueHeaders", {})
)
parsed_query = event.get("multiValueQueryStringParameters")
elif event_source.event_type == EventTypes.LAMBDA_FUNCTION_URL:
request_headers = event.get("headers", {})
peer_ip = event.get("requestContext", {}).get("http", {}).get("sourceIp")
raw_uri = event.get("rawPath")
parsed_query = event.get("queryStringParameters")
elif event_source.event_type == EventTypes.API_GATEWAY:
request_context = event.get("requestContext", {})
request_path_parameters = event.get("pathParameters")
route = trigger_tags.get("http.route")
if event_source.subtype == EventSubtypes.API_GATEWAY:
request_headers = event.get("headers", {})
peer_ip = request_context.get("identity", {}).get("sourceIp")
raw_uri = event.get("path")
parsed_query = event.get("multiValueQueryStringParameters")
elif event_source.subtype == EventSubtypes.HTTP_API:
request_headers = event.get("headers", {})
peer_ip = request_context.get("http", {}).get("sourceIp")
raw_uri = event.get("rawPath")
parsed_query = event.get("queryStringParameters")
elif event_source.subtype == EventSubtypes.WEBSOCKET:
request_headers = _to_single_value_headers(
event.get("multiValueHeaders", {})
)
peer_ip = request_context.get("identity", {}).get("sourceIp")
raw_uri = event.get("path")
parsed_query = event.get("multiValueQueryStringParameters")
else:
return
else:
return
body = event.get("body")
is_base64_encoded = event.get("isBase64Encoded", False)
request_ip = _get_request_header_client_ip(request_headers, peer_ip, True)
if request_ip is not None:
span.set_tag("http.client_ip", request_ip)
span.set_tag("network.client.ip", request_ip)
# Encode the parsed query and append it to reconstruct the original raw URI expected by AppSec.
if parsed_query:
try:
encoded_query = urllib.parse.urlencode(parsed_query, doseq=True)
raw_uri += "?" + encoded_query # type: ignore
except Exception:
pass
core.dispatch(
# The matching listener is registered in ddtrace.appsec._handlers
"aws_lambda.start_request",
(
span,
request_headers,
request_ip,
body,
is_base64_encoded,
raw_uri,
route,
trigger_tags.get("http.method"),
parsed_query,
request_path_parameters,
),
)
def asm_start_response(
span: Span,
status_code: str,
event_source: _EventSource,
response: Union[Dict[str, Any], str, None],
):
if event_source.event_type not in _http_event_types:
return
if isinstance(response, dict) and (
"headers" in response or "multiValueHeaders" in response
):
headers = response.get("headers", {})
multi_value_request_headers = response.get("multiValueHeaders")
if isinstance(multi_value_request_headers, dict) and isinstance(headers, dict):
response_headers = _merge_single_and_multi_value_headers(
headers, multi_value_request_headers
)
elif isinstance(headers, dict):
response_headers = headers
else:
response_headers = {
"content-type": "application/json",
}
else:
response_headers = {
"content-type": "application/json",
}
core.dispatch(
# The matching listener is registered in ddtrace.appsec._handlers
"aws_lambda.start_response",
(
span,
status_code,
response_headers,
),
)
if isinstance(response, dict) and "statusCode" in response:
body = response.get("body")
else:
body = response
core.dispatch(
# The matching listener is registered in ddtrace.appsec._handlers
"aws_lambda.parse_body",
(body,),
)
def get_asm_blocked_response(
event_source: _EventSource,
) -> Optional[Dict[str, Any]]:
"""Get the blocked response for the given event source."""
if event_source.event_type not in _http_event_types:
return None
blocked = get_blocked()
if not blocked:
return None
desired_type = blocked.get("type", "auto")
if desired_type == "none":
content_type = "text/plain; charset=utf-8"
content = ""
else:
content_type = blocked.get("content-type", "application/json")
content = http_utils._get_blocked_template(
content_type, blocked.get("block_id", "default")
)
response = {
"statusCode": blocked.get("status_code", 403),
"body": content,
"isBase64Encoded": False,
}
needs_multi_value_headers = event_source.equals(
EventTypes.ALB, EventSubtypes.ALB_MULTI_VALUE_HEADERS
)
if needs_multi_value_headers:
response["multiValueHeaders"] = {
"content-type": [content_type],
}
if "location" in blocked:
response["multiValueHeaders"]["location"] = [blocked["location"]]
else:
response["headers"] = {
"content-type": content_type,
}
if "location" in blocked:
response["headers"]["location"] = blocked["location"]
return response