Skip to content

Commit

Permalink
Merge pull request #3346 from candlepin/mhorky/CCT-10_ipv6-format
Browse files Browse the repository at this point in the history
CCT-10: Ensure IPv6-based URLs are properly formatted
  • Loading branch information
jirihnidek authored Oct 18, 2023
2 parents 75eb078 + 9e002d1 commit a45afb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/subscription_manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def format_baseurl(hostname: str, port: str, prefix: str) -> str:
if prefix == DEFAULT_CDN_PREFIX:
prefix = prefix[:-1]

# Handle raw IPv6 addresses.
# RFC 1035 only allows characters `a-zA-Z0-9.`, we can do this.
if ":" in hostname:
hostname = f"[{hostname}]"

# just so we match how we format this by
# default
if port == DEFAULT_CDN_PORT:
Expand Down
16 changes: 16 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,22 @@ def test_format_not_fqdn_with_port(self):
self.assertEqual(prefix, DEFAULT_CDN_PREFIX)
self.assertEqual("https://foo-bar:8088", format_baseurl(hostname, port, prefix))

def test_ipv4(self):
result = format_baseurl(hostname="127.0.0.1", port="8080", prefix="/foo")
self.assertEqual("https://127.0.0.1:8080/foo", result)

def test_ipv6(self):
result = format_baseurl(hostname="::1", port="8080", prefix="/foo")
self.assertEqual("https://[::1]:8080/foo", result)

def test_ipv6_default_port(self):
result = format_baseurl(hostname="::1", port="443", prefix="/foo")
self.assertEqual("https://[::1]/foo", result)

def test_ipv6_no_prefix(self):
result = format_baseurl(hostname="::1", port="8080", prefix="/")
self.assertEqual("https://[::1]:8080", result)


class TestUrlBaseJoinEmptyBase(fixture.SubManFixture):
def test_blank_base_blank_url(self):
Expand Down

0 comments on commit a45afb7

Please sign in to comment.