-
Notifications
You must be signed in to change notification settings - Fork 704
WIP - Redact specific url query string values and url credentials in instrumentations #3508
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
Open
rads-1996
wants to merge
12
commits into
open-telemetry:main
Choose a base branch
from
rads-1996:rads-1996/redact-sensitive-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−32
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
09156b9
Redact specific url query string values and url credentials by defaul…
rads-1996 bc1ef33
Merge branch 'main' into rads-1996/redact-sensitive-params
rads-1996 cd8da98
Updated the CHANGELOG and updated the description of remove_url_crede…
rads-1996 501f916
Merge branch 'rads-1996/redact-sensitive-params' of https://github.co…
rads-1996 b09ccd9
Removed extra space in CHANGELOG
rads-1996 3e5a66a
Removed extra space in CHANGELOG
rads-1996 25525b3
Merge branch 'main' into rads-1996/redact-sensitive-params
rads-1996 49da997
Merge branch 'main' into rads-1996/redact-sensitive-params
rads-1996 b359524
Merge branch 'main' into rads-1996/redact-sensitive-params
rads-1996 0983337
Added the redaction capability in the instrumentations
rads-1996 b0c01ea
Merge branch 'rads-1996/redact-sensitive-params' of https://github.co…
rads-1996 e499841
Merge branch 'main' into rads-1996/redact-sensitive-params
rads-1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,7 @@ | |
SpanAttributes.HTTP_SERVER_NAME, | ||
} | ||
|
||
PARAMS_TO_REDACT = ["AWSAccessKeyId", "Signature", "sig", "X-Goog-Signature"] | ||
|
||
class ExcludeList: | ||
"""Class to exclude certain paths (given as a list of regexes) from tracing requests""" | ||
|
@@ -159,23 +160,23 @@ def parse_excluded_urls(excluded_urls: str) -> ExcludeList: | |
|
||
|
||
def remove_url_credentials(url: str) -> str: | ||
"""Given a string url, remove the username and password only if it is a valid url""" | ||
|
||
""" Given a string url, replace the username and password with the keyword "REDACTED "only if it is a valid url""" | ||
try: | ||
parsed = urlparse(url) | ||
if all([parsed.scheme, parsed.netloc]): # checks for valid url | ||
parsed_url = urlparse(url) | ||
_, _, netloc = parsed.netloc.rpartition("@") | ||
return urlunparse( | ||
( | ||
parsed_url.scheme, | ||
netloc, | ||
parsed_url.path, | ||
parsed_url.params, | ||
parsed_url.query, | ||
parsed_url.fragment, | ||
if '@' in parsed.netloc: | ||
_, _, host = parsed.netloc.rpartition("@") | ||
new_netloc = "REDACTED:REDACTED@" + host | ||
return urlunparse( | ||
( | ||
parsed.scheme, | ||
new_netloc, | ||
parsed.path, | ||
parsed.params, | ||
parsed.query, | ||
parsed.fragment, | ||
) | ||
) | ||
) | ||
except ValueError: # an unparsable url was passed | ||
pass | ||
return url | ||
|
@@ -255,3 +256,43 @@ def _parse_url_query(url: str): | |
path = parsed_url.path | ||
query_params = parsed_url.query | ||
return path, query_params | ||
|
||
def redact_query_parameters(url: str) -> str: | ||
"""Given a string url, redact sensitive query parameter values""" | ||
try: | ||
parsed = urlparse(url) | ||
if not parsed.query: # No query parameters to redact | ||
return url | ||
|
||
# Check if any of the sensitive parameters are in the query | ||
has_sensitive_params = any(param + "=" in parsed.query for param in PARAMS_TO_REDACT) | ||
if not has_sensitive_params: | ||
return url | ||
|
||
# Process query parameters | ||
query_parts: list[str] = [] | ||
for query_part in parsed.query.split("&"): | ||
if "=" in query_part: | ||
param_name, _ = query_part.split("=", 1) # Parameter name and value | ||
if param_name in PARAMS_TO_REDACT: | ||
query_parts.append(f"{param_name}=REDACTED") | ||
else: | ||
query_parts.append(query_part) | ||
else: | ||
query_parts.append(query_part) # Handle params with no value | ||
|
||
# Reconstruct the URL with redacted query parameters | ||
redacted_query = "&".join(query_parts) | ||
return urlunparse( | ||
( | ||
parsed.scheme, | ||
parsed.netloc, | ||
parsed.path, | ||
parsed.params, | ||
redacted_query, | ||
parsed.fragment, | ||
) | ||
) | ||
except ValueError: # an unparsable url was passed | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is just for urlparse, maybe do an early return instead so we save one indentation level? |
||
pass | ||
return url |
51 changes: 51 additions & 0 deletions
51
util/opentelemetry-util-http/tests/test_redact_query_parameters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
from opentelemetry.util.http import redact_query_parameters | ||
|
||
class TestRedactSensitiveInfo(unittest.TestCase): | ||
def test_redact_goog_signature(self): | ||
url = "https://www.example.com/path?color=blue&X-Goog-Signature=secret" | ||
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&X-Goog-Signature=REDACTED") | ||
|
||
def test_no_redaction_needed(self): | ||
url = "https://www.example.com/path?color=blue&query=secret" | ||
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&query=secret") | ||
|
||
def test_no_query_parameters(self): | ||
url = "https://www.example.com/path" | ||
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path") | ||
|
||
def test_empty_query_string(self): | ||
url = "https://www.example.com/path?" | ||
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?") | ||
|
||
def test_empty_url(self): | ||
url = "" | ||
self.assertEqual(redact_query_parameters(url), "") | ||
|
||
def test_redact_aws_access_key_id(self): | ||
url = "https://www.example.com/path?color=blue&AWSAccessKeyId=secrets" | ||
self.assertEqual(redact_query_parameters(url), "https://www.example.com/path?color=blue&AWSAccessKeyId=REDACTED") | ||
|
||
def test_api_key_not_in_redact_list(self): | ||
url = "https://www.example.com/path?api_key=secret%20key&user=john" | ||
self.assertNotEqual(redact_query_parameters(url), "https://www.example.com/path?api_key=REDACTED&user=john") | ||
|
||
def test_password_key_not_in_redact_list(self): | ||
url = "https://api.example.com?key=abc&password=123&user=admin" | ||
self.assertNotEqual(redact_query_parameters(url), "https://api.example.com?key=REDACTED&password=REDACTED&user=admin") | ||
|
||
def test_url_with_at_symbol_in_path_and_query(self): | ||
url = "https://github.com/p@th?foo=b@r" | ||
self.assertEqual(redact_query_parameters(url), "https://github.com/p@th?foo=b@r") | ||
|
||
def test_aws_access_key_with_real_format(self): | ||
url = "https://microsoft.com?AWSAccessKeyId=AKIAIOSFODNN7" | ||
self.assertEqual(redact_query_parameters(url), "https://microsoft.com?AWSAccessKeyId=REDACTED") | ||
|
||
def test_signature_parameter(self): | ||
url = "https://service.com?sig=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0" | ||
self.assertEqual(redact_query_parameters(url), "https://service.com?sig=REDACTED") | ||
|
||
def test_signature_with_url_encoding(self): | ||
url = "https://service.com?Signature=39Up9jzHkxhuIhFE9594DJxe7w6cIRCg0V6ICGS0%3A377" | ||
self.assertEqual(redact_query_parameters(url), "https://service.com?Signature=REDACTED") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,22 +10,28 @@ def test_remove_no_credentials(self): | |
self.assertEqual(cleaned_url, url) | ||
|
||
def test_remove_credentials(self): | ||
url = "http://someuser:[email protected]:8080/test/path?query=value" | ||
url = "http://someuser:[email protected]:8080/test/path?sig=value" | ||
cleaned_url = remove_url_credentials(url) | ||
self.assertEqual( | ||
cleaned_url, "http://opentelemetry.io:8080/test/path?query=value" | ||
cleaned_url, "http://REDACTED:REDACTED@opentelemetry.io:8080/test/path?sig=value" | ||
) | ||
|
||
def test_remove_credentials_ipv4_literal(self): | ||
url = "http://someuser:[email protected]:8080/test/path?query=value" | ||
cleaned_url = remove_url_credentials(url) | ||
self.assertEqual( | ||
cleaned_url, "http://127.0.0.1:8080/test/path?query=value" | ||
cleaned_url, "http://REDACTED:REDACTED@127.0.0.1:8080/test/path?query=value" | ||
) | ||
|
||
def test_remove_credentials_ipv6_literal(self): | ||
url = "http://someuser:somepass@[::1]:8080/test/path?query=value" | ||
cleaned_url = remove_url_credentials(url) | ||
self.assertEqual( | ||
cleaned_url, "http://[::1]:8080/test/path?query=value" | ||
cleaned_url, "http://REDACTED:REDACTED@[::1]:8080/test/path?query=value" | ||
) | ||
|
||
def test_empty_url(self): | ||
url = "" | ||
cleaned_url = remove_url_credentials(url) | ||
self.assertEqual(cleaned_url, url) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think on creating a
redact_url
function that calls both instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that does make sense. I will make changes for it.