Skip to content

Commit

Permalink
validate that to url contains a hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-sahlmann committed Jan 4, 2024
1 parent 2c58c01 commit b0b5516
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mb_netmgmt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# along with mb-netmgmt. If not, see <https://www.gnu.org/licenses/

"""Network Management Protocols for Mountebank"""
__version__ = "0.0.77"
__version__ = "0.0.78"

import os
import subprocess
Expand Down
9 changes: 8 additions & 1 deletion mb_netmgmt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_to(self):
if proxy:
self.save_key(proxy)
disable_algorithms(proxy.get("disabled_algorithms", {}))
return urlparse(proxy["to"])
return parse_to(proxy["to"])
except (IndexError, AttributeError):
pass

Expand All @@ -124,6 +124,13 @@ def get_proxy(self, stub):
return stub["responses"][0].get("proxy")


def parse_to(url: str):
to = urlparse(url)
if not to.hostname:
raise ValueError("No hostname")
return to


def get_cli_patterns():
patterns = []
patterns.append(
Expand Down
11 changes: 9 additions & 2 deletions test/test_mb_netmgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import ncclient.manager
import paramiko
import pytest
from ncclient.transport.session import BASE_NS_1_0, MSG_DELIM, to_ele
from ncclient.devices.default import DefaultDeviceHandler
from ncclient.transport.session import BASE_NS_1_0, MSG_DELIM, to_ele
from scapy.layers.snmp import ASN1_NULL, SNMPvarbind

from mb_netmgmt import mb, netconf, snmp, ssh, use_scalar_strings, yaml
from mb_netmgmt.__main__ import create_server, get_cli_patterns
from mb_netmgmt.__main__ import create_server, get_cli_patterns, parse_to

port = 8081
prompt = b"prompt#"
Expand Down Expand Up @@ -280,3 +280,10 @@ def test_to_varbind_no_such_instance():
assert result == SNMPvarbind(
oid="1.3", value=None, noSuchObject=None, noSuchInstance=0, endOfMibView=None
)


def test_parse_to():
assert parse_to("telnet://localhost").hostname == "localhost"
assert parse_to("telnet://localhost:23").port == 23
with pytest.raises(ValueError):
parse_to("localhost")

0 comments on commit b0b5516

Please sign in to comment.