feat: SudoHistoryMixin, the sudo password left in shell histories (NetOrk/netork#1090) #60

Merged
christianmanivong merged 2 commits from feat/sudo-history-cleanup into main 2026-10-30 20:47:48 +00:00
Owner

Summary

A driver that typed echo '<password>' | sudo -S -p "" <command> into an interactive shell left that line, password and all, in the login user's shell history once per command (napalm-linux until NetOrk/netork#1039: 121 lines on one host). Where only the part before the first && ran as root, files root should own were left to the login user. SudoHistoryMixin reads the first as a fact and takes both back.

Changes

  • napalm_device_types/sudo_history.py: SudoHistoryMixin, mixed in by a driver that supplies run_command, SUDO_HISTORY_MARKER (a fixed string its lines carry) and SUDO_ROOT_FILES.
    • get_history_sudo_lines() counts the lines per history file (~/.bash_history, ~/.zsh_history, ~/.ash_history, ~/.sh_history) with grep -c -F, as the login user. No line is read back; an unreadable file raises.
    • remove_history_sudo_lines() runs two steps, history and owners, the second also when the first failed. history deletes exactly those lines as the login user with sed -i.napalm-bak, overwrites the backup (the old file's blocks) with shred -u (rm -f without shred), and puts the backup back if sed left no file. A history that is a symbolic link is left alone and fails the step. owners, as root and without stdin, runs chown -h 0:0 on each SUDO_ROOT_FILES entry that is a regular file, not a link, owned by the login user (find -prune -user); a root login has nothing to give back. The result is a FixResultDict plus files (before/after per file), removed and owners_restored.
  • Exported from the package; README and CHANGELOG (Unreleased).
  • The backup suffix carries no application's name.

Related Issue

NetOrk/netork#1090 (napalm-linux uses it in NAPALM/napalm-linux, same branch name).

Testing

  • tests/test_sudo_history.py (26 tests) runs the scripts for real with sh against a home directory in a temp dir, with a chown that only records: counting, exact deletion, fixed-string matching, no backup left, links left alone, unreadable files, a failing sed, a sed that lost the file, root's step only privileged and without stdin, files owned by someone else, root logins, refused sudo.
  • pytest --cov: 885 passed, total 95 %, sudo_history.py 100 %; ruff format --check, ruff check, mypy (strict), bandit, napalm-standard check and ratchet clean.
## Summary A driver that typed `echo '<password>' | sudo -S -p "" <command>` into an interactive shell left that line, password and all, in the login user's shell history once per command (napalm-linux until NetOrk/netork#1039: 121 lines on one host). Where only the part before the first `&&` ran as root, files root should own were left to the login user. `SudoHistoryMixin` reads the first as a fact and takes both back. ## Changes - `napalm_device_types/sudo_history.py`: `SudoHistoryMixin`, mixed in by a driver that supplies `run_command`, `SUDO_HISTORY_MARKER` (a fixed string its lines carry) and `SUDO_ROOT_FILES`. - `get_history_sudo_lines()` counts the lines per history file (`~/.bash_history`, `~/.zsh_history`, `~/.ash_history`, `~/.sh_history`) with `grep -c -F`, as the login user. No line is read back; an unreadable file raises. - `remove_history_sudo_lines()` runs two steps, `history` and `owners`, the second also when the first failed. `history` deletes exactly those lines as the login user with `sed -i.napalm-bak`, overwrites the backup (the old file's blocks) with `shred -u` (`rm -f` without shred), and puts the backup back if sed left no file. A history that is a symbolic link is left alone and fails the step. `owners`, as root and without stdin, runs `chown -h 0:0` on each `SUDO_ROOT_FILES` entry that is a regular file, not a link, owned by the login user (`find -prune -user`); a root login has nothing to give back. The result is a `FixResultDict` plus `files` (before/after per file), `removed` and `owners_restored`. - Exported from the package; README and CHANGELOG (Unreleased). - The backup suffix carries no application's name. ## Related Issue NetOrk/netork#1090 (napalm-linux uses it in NAPALM/napalm-linux, same branch name). ## Testing - `tests/test_sudo_history.py` (26 tests) runs the scripts for real with `sh` against a home directory in a temp dir, with a `chown` that only records: counting, exact deletion, fixed-string matching, no backup left, links left alone, unreadable files, a failing `sed`, a `sed` that lost the file, root's step only privileged and without stdin, files owned by someone else, root logins, refused sudo. - `pytest --cov`: 885 passed, total 95 %, `sudo_history.py` 100 %; `ruff format --check`, `ruff check`, `mypy` (strict), `bandit`, `napalm-standard check` and `ratchet` clean.
christianmanivong added 1 commit 2026-10-30 20:09:55 +00:00
feat: SudoHistoryMixin, the sudo password left in shell histories (NetOrk/netork#1090)
driver / standard (pull_request) Failing after 7s
driver / lint (pull_request) Successful in 20s
driver / types (pull_request) Successful in 1m8s
driver / test (3.12) (pull_request) Failing after 1m9s
driver / test (3.13) (pull_request) Failing after 1m3s
driver / lowest (pull_request) Failing after 33s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m8s
CI / driver (pull_request) Failing after 2m24s
driver / publish (pull_request) Skipped
cb90fa9835
A driver that typed `echo '<password>' | sudo -S -p "" <command>` into an
interactive shell left that line, password and all, in the login user's
shell history once per command, and where only the part before the first
`&&` ran as root, files root should own were left to the login user.

get_history_sudo_lines() counts the lines per history file with grep -c,
as the login user, without reading any back. remove_history_sudo_lines()
deletes exactly those lines with sed -i, overwrites the backup with
shred -u, and as root gives the driver's SUDO_ROOT_FILES back to root:root
where each is a regular file, not a link, owned by the login user. It
reports each file's lines before and after. Nothing goes to stdin.

The driver names the marker and the files and supplies run_command.
christianmanivong added 1 commit 2026-10-30 20:44:40 +00:00
test: the fake host never answers with this machine's uid
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m14s
driver / test (3.12) (pull_request) Successful in 1m14s
driver / test (3.13) (pull_request) Successful in 1m16s
driver / lowest (pull_request) Successful in 36s
driver / build (pull_request) Successful in 30s
driver / security (pull_request) Successful in 1m2s
CI / driver (pull_request) Successful in 2m32s
driver / publish (pull_request) Skipped
0f0064c2d7
CI runs as root, which the owner step skips, so three owner tests failed there
while they passed for a normal user.
christianmanivong merged commit 888a7229dc into main 2026-10-30 20:47:48 +00:00
christianmanivong deleted branch feat/sudo-history-cleanup 2026-10-30 20:47:48 +00:00
Sign in to join this conversation.