Skip to content

remove_url_credentials drops brackets from IPv6 hostnames #2551

@tim-hutchinson

Description

@tim-hutchinson

Describe your environment

Python 3.11.9, opentelemetry-instrumentation is auto-injected via the OpenTelemetry operator, opentelemetry_util_http is 0.44b0

Steps to reproduce

from opentelemetry.util.http import remove_url_credentials

literal_ipv6_url = "https://[::1]/somepath?query=foo"
remove_url_credentials(literal_ipv6_url)
# 'https://::1/somepath?query=foo' -- should be 'https://[::1]/somepath?query=foo'

literal_ipv6_url_with_port = "https://[::1]:12345/somepath?query=foo"
remove_url_credentials(literal_ipv6_url_with_port)
# 'https://::1:12345/somepath?query=foo -- should be 'https://[::1]:12345/somepath?query=foo'

literal_ipv6_url_with_auth = "https://someuser:somepass@[::1]:12345/somepath?query=foo"
remove_url_credentials(literal_ipv6_url_with_auth)
# 'https://::1:12345/somepath?query=foo' -- should be https://[::1]:12345/somepath?query=foo

What is the expected behavior?
The ipv6 host should remain inside []

What is the actual behavior?
[] are stripped from the host

Additional context
https://github.com/open-telemetry/opentelemetry-python-contrib/blob/main/util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py#L169 is the causing line. The hostname result on urlparse does not contain the brackets

from urllib.parse import urlparse

parsed = urlparse(literal_ipv6_url_with_auth)

parsed
# ParseResult(scheme='https', netloc='someuser:somepass@[::1]:12345', path='/somepath', params='', query='query=foo', fragment='')

parsed.hostname
# '::1'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions