Skip to content

Commit 65b4f85

Browse files
Preserve brackets around literal IPv6 hosts (#2552)
1 parent e640956 commit 65b4f85

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5656
([#2420](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2420))
5757
- `opentelemetry-instrumentation-asyncio` Check for __name__ attribute in the coroutine
5858
([#2521](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2521))
59+
- `opentelemetry-util-http` Preserve brackets around literal IPv6 hosts ([#2552](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2552))
5960

6061
## Version 1.24.0/0.45b0 (2024-03-28)
6162

Diff for: util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,7 @@ def remove_url_credentials(url: str) -> str:
166166
parsed = urlparse(url)
167167
if all([parsed.scheme, parsed.netloc]): # checks for valid url
168168
parsed_url = urlparse(url)
169-
netloc = (
170-
(":".join(((parsed_url.hostname or ""), str(parsed_url.port))))
171-
if parsed_url.port
172-
else (parsed_url.hostname or "")
173-
)
169+
_, _, netloc = parsed.netloc.rpartition("@")
174170
return urlunparse(
175171
(
176172
parsed_url.scheme,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
3+
from opentelemetry.util.http import remove_url_credentials
4+
5+
6+
class TestRemoveUrlCredentials(unittest.TestCase):
7+
def test_remove_no_credentials(self):
8+
url = "http://opentelemetry.io:8080/test/path?query=value"
9+
cleaned_url = remove_url_credentials(url)
10+
self.assertEqual(cleaned_url, url)
11+
12+
def test_remove_credentials(self):
13+
url = "http://someuser:[email protected]:8080/test/path?query=value"
14+
cleaned_url = remove_url_credentials(url)
15+
self.assertEqual(
16+
cleaned_url, "http://opentelemetry.io:8080/test/path?query=value"
17+
)
18+
19+
def test_remove_credentials_ipv4_literal(self):
20+
url = "http://someuser:[email protected]:8080/test/path?query=value"
21+
cleaned_url = remove_url_credentials(url)
22+
self.assertEqual(cleaned_url, "http://127.0.0.1:8080/test/path?query=value")
23+
24+
def test_remove_credentials_ipv6_literal(self):
25+
url = "http://someuser:somepass@[::1]:8080/test/path?query=value"
26+
cleaned_url = remove_url_credentials(url)
27+
self.assertEqual(cleaned_url, "http://[::1]:8080/test/path?query=value")

0 commit comments

Comments
 (0)