Skip to content

Commit 1884a1f

Browse files
committed
Updated the aiohttp-server implementation and the query redaction logic
1 parent c215bff commit 1884a1f

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,6 @@ def collect_request_attributes(request: web.Request) -> Dict:
149149
str(request.url),
150150
)
151151

152-
user_info = request.headers.get("Authorization")
153-
if user_info and http_url and "@" not in http_url:
154-
# If there are credentials in Authorization header but not in URL
155-
# Add dummy credentials that will be redacted
156-
parsed = urllib.parse.urlparse(http_url)
157-
netloc_with_auth = f"username:password@{parsed.netloc}"
158-
http_url = urllib.parse.urlunparse(
159-
(parsed.scheme, netloc_with_auth, parsed.path, parsed.params, parsed.query, parsed.fragment)
160-
)
161-
162152
query_string = request.query_string
163153
if query_string and http_url:
164154
if isinstance(query_string, bytes):

instrumentation/opentelemetry-instrumentation-aiohttp-server/tests/test_aiohttp_server_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ async def handler(request):
185185
span = spans[0]
186186
assert span.attributes[HTTP_METHOD] == "GET"
187187
assert span.attributes[HTTP_STATUS_CODE] == 200
188-
assert span.attributes[HTTP_URL] == f"http://REDACTED:REDACTED@{server.host}:{server.port}/status/200?Signature=REDACTED"
188+
assert span.attributes[HTTP_URL] == f"http://{server.host}:{server.port}/status/200?Signature=REDACTED"
189189

190190
# Clean up
191191
AioHttpServerInstrumentor().uninstrument()

util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from re import compile as re_compile
2121
from re import search
2222
from typing import Callable, Iterable, overload
23-
from urllib.parse import urlparse, urlunparse
23+
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
2424

2525
from opentelemetry.semconv.trace import SpanAttributes
2626

@@ -263,33 +263,19 @@ def redact_query_parameters(url: str) -> str:
263263
parsed = urlparse(url)
264264
if not parsed.query: # No query parameters to redact
265265
return url
266-
267-
# Check if any of the sensitive parameters are in the query
268-
has_sensitive_params = any(param + "=" in parsed.query for param in PARAMS_TO_REDACT)
269-
if not has_sensitive_params:
266+
query_params = parse_qs(parsed.query)
267+
if not any(param in query_params for param in PARAMS_TO_REDACT):
270268
return url
271-
272-
# Process query parameters
273-
query_parts: list[str] = []
274-
for query_part in parsed.query.split("&"):
275-
if "=" in query_part:
276-
param_name, _ = query_part.split("=", 1) # Parameter name and value
277-
if param_name in PARAMS_TO_REDACT:
278-
query_parts.append(f"{param_name}=REDACTED")
279-
else:
280-
query_parts.append(query_part)
281-
else:
282-
query_parts.append(query_part) # Handle params with no value
283-
284-
# Reconstruct the URL with redacted query parameters
285-
redacted_query = "&".join(query_parts)
269+
for param in PARAMS_TO_REDACT:
270+
if param in query_params:
271+
query_params[param] = ["REDACTED"]
286272
return urlunparse(
287273
(
288274
parsed.scheme,
289275
parsed.netloc,
290276
parsed.path,
291277
parsed.params,
292-
redacted_query,
278+
urlencode(query_params, doseq=True),
293279
parsed.fragment,
294280
)
295281
)

0 commit comments

Comments
 (0)