fix(ping): quote the destination and source in the ping command

ping() typed its arguments into the interactive shell as they came, so a
destination such as "1.2.3.4; id" ran the command after the semicolon as
the login user. destination and source now go through shlex.quote, and
count, timeout, size and ttl are coerced with int(), which refuses
anything that is not a number before a command is sent.

The command is built in _ping.py, next to its parser, which keeps
linux.py within its recorded length.

Closes #55
This commit is contained in:
Christian Manivong committed 2026-11-02 08:37:15 +01:00
1 parent 5687f5d9a1
commit c2d7c4d1b4
5 files changed
+51 -6

No files matched your search

+1 -1
View File
@@ -21,7 +21,7 @@
"napalm_linux.linux"
],
"python_file_lines": {
"napalm_linux/linux.py": 2142
"napalm_linux/linux.py": 2140
},
"untyped_public": []
}
+30
View File
@@ -516,6 +516,36 @@ class TestPingFormats:
assert driver.ping("nosuch") == {"error": "ping: unknown host nosuch"}
class TestPingArguments:
"""``ping`` types its arguments into a shell: each one stays one word (#55)."""
def _sent(self, driver, *args, **kwargs):
with patch.object(driver, "_send", return_value=PING_OUTPUT) as send:
driver.ping(*args, **kwargs)
return send.call_args[0][0]
def test_the_destination_is_one_quoted_word(self, driver):
sent = self._sent(driver, "1.2.3.4; id")
assert sent == "ping -c 5 -W 2 -s 100 -t 255 '1.2.3.4; id' 2>&1"
def test_the_source_is_one_quoted_word(self, driver):
sent = self._sent(driver, "192.0.2.1", source="$(id)")
assert sent == "ping -c 5 -W 2 -s 100 -t 255 -I '$(id)' 192.0.2.1 2>&1"
def test_the_numbers_are_numbers(self, driver):
sent = self._sent(driver, "192.0.2.1", count="3", timeout=1.0, size="56", ttl=64)
assert sent == "ping -c 3 -W 1 -s 56 -t 64 192.0.2.1 2>&1"
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")
send.assert_not_called()
# ---------------------------------------------------------------------------
# apply_updates – input validation
# ---------------------------------------------------------------------------