Skip to content

Commit ea579ee

Browse files
authored
Add MASK_URL for url sanitization (#1850)
* Add MASK_URL for url sanitization Python 3.11.4 added more strict URL checking when calling `urllib.parse.urlparse`. This caused the url sanitization to fail when the url contained brackets, as with `[REDACTED]` when redacting passwords in URLs * Fix two more tests
1 parent aedd6b1 commit ea579ee

File tree

7 files changed

+8
-9
lines changed

7 files changed

+8
-9
lines changed

elasticapm/conf/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _starmatch_to_regex(pattern: str) -> Pattern:
7676
HTTP_WITH_BODY = {"POST", "PUT", "PATCH", "DELETE"}
7777

7878
MASK = "[REDACTED]"
79+
MASK_URL = "REDACTED"
7980

8081
EXCEPTION_CHAIN_MAX_DEPTH = 50
8182

elasticapm/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def sanitize_url(url: str) -> str:
132132
if "@" not in url:
133133
return url
134134
parts = urllib.parse.urlparse(url)
135-
return url.replace("%s:%s" % (parts.username, parts.password), "%s:%s" % (parts.username, constants.MASK))
135+
return url.replace("%s:%s" % (parts.username, parts.password), "%s:%s" % (parts.username, constants.MASK_URL))
136136

137137

138138
def get_host_from_url(url: str) -> str:

tests/instrumentation/asyncio_tests/httpx_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ async def test_url_sanitization(instrument, elasticapm_client, waiting_httpserve
149149
span = elasticapm_client.spans_for_transaction(transactions[0])[0]
150150

151151
assert "pass" not in span["context"]["http"]["url"]
152-
assert constants.MASK in span["context"]["http"]["url"]
152+
assert constants.MASK_URL in span["context"]["http"]["url"]
153153

154154

155155
@pytest.mark.parametrize("status_code", [400, 500])

tests/instrumentation/httpx_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_url_sanitization(instrument, elasticapm_client, waiting_httpserver):
147147
span = elasticapm_client.spans_for_transaction(transactions[0])[0]
148148

149149
assert "pass" not in span["context"]["http"]["url"]
150-
assert constants.MASK in span["context"]["http"]["url"]
150+
assert constants.MASK_URL in span["context"]["http"]["url"]
151151

152152

153153
@pytest.mark.parametrize("status_code", [400, 500])

tests/instrumentation/requests_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,4 @@ def test_url_sanitization(instrument, elasticapm_client, waiting_httpserver):
199199
span = elasticapm_client.spans_for_transaction(transactions[0])[0]
200200

201201
assert "pass" not in span["context"]["http"]["url"]
202-
assert constants.MASK in span["context"]["http"]["url"]
202+
assert constants.MASK_URL in span["context"]["http"]["url"]

tests/instrumentation/urllib_tests.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def test_urllib(instrument, elasticapm_client, waiting_httpserver):
5050
elasticapm_client.begin_transaction("transaction")
5151
expected_sig = "GET {0}".format(parsed_url.netloc)
5252
with capture_span("test_name", "test_type"):
53-
5453
url = "http://{0}/hello_world".format(parsed_url.netloc)
5554
r = urlopen(url)
5655

@@ -93,7 +92,6 @@ def test_urllib_error(instrument, elasticapm_client, waiting_httpserver, status_
9392
elasticapm_client.begin_transaction("transaction")
9493
expected_sig = "GET {0}".format(parsed_url.netloc)
9594
with capture_span("test_name", "test_type"):
96-
9795
url = "http://{0}/hello_world".format(parsed_url.netloc)
9896
try:
9997
r = urlopen(url)
@@ -257,4 +255,4 @@ def test_url_sanitization(instrument, elasticapm_client, waiting_httpserver):
257255
span = elasticapm_client.spans_for_transaction(transactions[0])[0]
258256

259257
assert "pass" not in span["context"]["http"]["url"]
260-
assert constants.MASK in span["context"]["http"]["url"]
258+
assert constants.MASK_URL in span["context"]["http"]["url"]

tests/utils/tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ def test_starmatch_to_regex(pattern, input, match):
204204

205205
def test_url_sanitization():
206206
sanitized = sanitize_url("http://user:pass@localhost:123/foo?bar=baz#bazzinga")
207-
assert sanitized == "http://user:%s@localhost:123/foo?bar=baz#bazzinga" % constants.MASK
207+
assert sanitized == "http://user:%s@localhost:123/foo?bar=baz#bazzinga" % constants.MASK_URL
208208

209209

210210
def test_url_sanitization_urlencoded_password():
211211
sanitized = sanitize_url("http://user:%F0%9F%9A%B4@localhost:123/foo?bar=baz#bazzinga")
212-
assert sanitized == "http://user:%s@localhost:123/foo?bar=baz#bazzinga" % constants.MASK
212+
assert sanitized == "http://user:%s@localhost:123/foo?bar=baz#bazzinga" % constants.MASK_URL
213213

214214

215215
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)