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
10 changes: 9 additions & 1 deletion winrm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@ def _strip_namespace(self, xml: bytes) -> bytes:

@staticmethod
def _build_url(target: str, transport: str) -> str:
match = re.match(r"(?i)^((?P<scheme>http[s]?)://)?(?P<host>[0-9a-z-_.]+)(:(?P<port>\d+))?(?P<path>(/)?(wsman)?)?", target) # NOQA
# Try IPv6 pattern first (with brackets)
ipv6_pattern = r"(?i)^((?P<scheme>http[s]?)://)?(?P<host>\[[0-9a-f:]+\])(:(?P<port>\d+))?(?P<path>(/)?(wsman)?)?$"
match = re.match(ipv6_pattern, target)

# Fall back to IPv4/hostname pattern
if not match:
ipv4_pattern = r"(?i)^((?P<scheme>http[s]?)://)?(?P<host>[0-9a-z-_.]+)(:(?P<port>\d+))?(?P<path>(/)?(wsman)?)?$"
match = re.match(ipv4_pattern, target)

if not match:
raise ValueError("Invalid target URL: {0}".format(target))

Expand Down
40 changes: 40 additions & 0 deletions winrm/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,43 @@ def test_decode_clixml_invalid_xml():
actual = s._clean_error_msg(msg)

assert actual == msg


def test_target_as_ipv6_address():
s = Session("[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]", auth=("john.smith", "secret"))
assert s.url == "http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985/wsman"


def test_target_as_ipv6_address_with_port():
s = Session("[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:1111", auth=("john.smith", "secret"))
assert s.url == "http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:1111/wsman"


def test_target_as_schema_then_ipv6():
s = Session("http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]", auth=("john.smith", "secret"))
assert s.url == "http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985/wsman"


def test_target_as_schema_then_ipv6_with_port():
s = Session("http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985", auth=("john.smith", "secret"))
assert s.url == "http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985/wsman"


def test_target_as_full_url_with_ipv6():
s = Session("http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985/wsman", auth=("john.smith", "secret"))
assert s.url == "http://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5985/wsman"


def test_target_as_https_ipv6():
s = Session("https://[::1]:5986/wsman", auth=("john.smith", "secret"))
assert s.url == "https://[::1]:5986/wsman"


def test_target_as_ipv6_localhost():
s = Session("[::1]", auth=("john.smith", "secret"))
assert s.url == "http://[::1]:5985/wsman"


def test_target_as_ipv6_with_ssl_transport():
s = Session("[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]", auth=("john.smith", "secret"), transport="ssl")
assert s.url == "https://[2a05:d018:1961:ba00:ff5b:37ba:20c7:726a]:5986/wsman"