Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions app/utils/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
environment variables.
"""

import ipaddress
import logging
import os
import socket
Expand All @@ -15,15 +16,45 @@

def _parse_target(target: str, default_port: int) -> tuple[str, int]:
"""Return ``(host, port)`` tuple for ``target``."""
Comment on lines 17 to 18
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The docstring for _parse_target is minimal and doesn't document the complex IPv6 parsing logic. Consider expanding it to explain the supported formats and parsing rules, for example:

"""Return ``(host, port)`` tuple for ``target``.

Supports the following formats:
- hostname:port (e.g., "example.com:8080")
- hostname (uses default_port)
- [ipv6]:port (e.g., "[2001:db8::1]:8443")
- [ipv6] (uses default_port)
- ipv6:port (e.g., "2001:db8::1:8443", parsed heuristically)
- ipv6 (uses default_port, e.g., "2001:db8::1")

For unbracketed IPv6 addresses, the last colon-separated segment is treated
as a port if it's all digits and the remaining part is a valid IPv6 address.
"""

This would help maintainers understand the parsing logic and expected behavior.

Copilot uses AI. Check for mistakes.
if ":" in target:
host, p = target.rsplit(":", 1)
try:
return host, int(p)
except ValueError:

if target.startswith("["):
end = target.find("]")
if end != -1:
host = target[1:end]
port_candidate = target[end + 1 :]
if port_candidate.startswith(":"):
port_str = port_candidate[1:]
if port_str:
try:
return host, int(port_str)
except ValueError:
return host, default_port
return host, default_port

if ":" in target:
host_candidate, port_candidate = target.rsplit(":", 1)
if port_candidate.isdigit():
if ":" not in host_candidate or _is_ipv6_literal(host_candidate):
try:
return host_candidate, int(port_candidate)
Comment on lines +28 to +39
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code doesn't validate that the parsed port number is within the valid range (0-65535). For example, _parse_target("example.com:99999", 80) would return ("example.com", 99999), which is an invalid port.

Consider adding validation:

port = int(port_str)
if 0 <= port <= 65535:
    return host, port
return host, default_port

This validation should be applied at lines 29 and 39 where ports are parsed.

Copilot uses AI. Check for mistakes.
except ValueError:
return host_candidate, default_port
return target, default_port
if ":" not in host_candidate:
return host_candidate, default_port
return target, default_port


def _is_ipv6_literal(value: str) -> bool:
"""Return ``True`` if ``value`` is a valid IPv6 literal."""

try:
ipaddress.IPv6Address(value)
except ValueError:
return False
return True


def _check_url(url: str, timeout: int) -> bool:
"""Return ``True`` if ``url`` responds to a HEAD request."""
try:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_network_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def test_ipv6_host_with_port(self):
network._parse_target("2001:db8::1:8443", 443), ("2001:db8::1", 8443)
)

def test_ipv6_literal_without_port_uses_default(self):
self.assertEqual(
network._parse_target("2001:db8::1", 443), ("2001:db8::1", 443)
)


class TestTryConnect(unittest.TestCase):
@patch("app.utils.network.socket.create_connection")
Expand Down
Loading