From a1e8ec78c3e8a89d37abb55e224713aef206f81e Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:47:23 +0100 Subject: [PATCH] fix(ping): refuse a destination or source that reads as an option Quoting keeps a value one word, but ping still reads a word starting with "-" as one of its options: ping(destination="-f") sent a flood ping option instead of a host, and a source "-f" did the same after -I. No address or hostname starts with "-", so ping_command now refuses such a destination or source with ValueError before anything is sent. Closes #63 --- CHANGELOG.md | 4 +++- napalm_linux/_ping.py | 7 ++++++- napalm_linux/linux.py | 2 +- tests/test_linux.py | 12 ++++++++++++ 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ebac95..52a54c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ping()` passes `destination` and `source` to the host's shell as one quoted word each (#55). A destination such as `192.0.2.1; id` ran the command after the `;` as the login user. `count`, `timeout`, `size` and `ttl` must be numbers; anything else - raises `ValueError` before a command is sent. + raises `ValueError` before a command is sent. A destination or source that starts + with `-` is refused the same way (#63): `ping` would read `-f` as its flood option, + and no address or hostname starts with `-`. - dnf and yum hosts read pending updates and security advisories from the cached metadata only (`--cacheonly`), as apt reads its local index (#59). With an expired cache `check-update` downloaded the repositories' metadata first, and on an Oracle diff --git a/napalm_linux/_ping.py b/napalm_linux/_ping.py index 8bf8416..b358971 100644 --- a/napalm_linux/_ping.py +++ b/napalm_linux/_ping.py @@ -27,8 +27,13 @@ def ping_command( ) -> str: """The shell line that pings *destination*: the names quoted, the numbers numbers (#55). - :raises ValueError: for a count, timeout, size or ttl that is no number. + :raises ValueError: for a count, timeout, size or ttl that is no number, and for a + destination or source that ping would read as an option (#63): no address or + hostname starts with ``-``. """ + for name in (destination, source): + if name.startswith("-"): + raise ValueError(f"{name!r} would be read as a ping option, not an address") numbers = f"-c {int(count)} -W {int(timeout)} -s {int(size)} -t {int(ttl)}" src_opt = f" -I {quote(source)}" if source else "" return f"ping {numbers}{src_opt} {quote(destination)} 2>&1" diff --git a/napalm_linux/linux.py b/napalm_linux/linux.py index f6f3317..cc0e10e 100644 --- a/napalm_linux/linux.py +++ b/napalm_linux/linux.py @@ -1086,7 +1086,7 @@ class LinuxDriver( count: int = 5, vrf: str = "", ) -> dict[str, Any]: - """Ping from the remote host; ValueError for a count, timeout, size or ttl not a number.""" + """Ping from the remote host; ValueError for a bad number or a name starting with -.""" return parse_ping(self._send(ping_command(destination, source, ttl, timeout, size, count))) # ------------------------------------------------------------------ diff --git a/tests/test_linux.py b/tests/test_linux.py index f5f5e3a..0813af4 100644 --- a/tests/test_linux.py +++ b/tests/test_linux.py @@ -540,6 +540,18 @@ class TestPingArguments: assert sent == "ping -c 3 -W 1 -s 56 -t 64 192.0.2.1 2>&1" + @pytest.mark.parametrize( + ("destination", "source"), [("-f", ""), ("192.0.2.1", "-f"), ("-p ff", "")] + ) + def test_an_option_in_place_of_a_name_is_refused_before_anything_is_sent( + self, driver, destination, source + ): + """Quoting keeps a word one word, and ping still reads ``-f`` as its option (#63).""" + with patch.object(driver, "_send") as send, pytest.raises(ValueError, match="option"): + driver.ping(destination, source=source) + + send.assert_not_called() + def test_a_number_that_is_not_one_is_refused_before_anything_is_sent(self, driver): with patch.object(driver, "_send") as send, pytest.raises(ValueError): driver.ping("192.0.2.1", count="5; id")