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.
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.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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.SudoHistoryMixinreads the first as a fact and takes both back.Changes
napalm_device_types/sudo_history.py:SudoHistoryMixin, mixed in by a driver that suppliesrun_command,SUDO_HISTORY_MARKER(a fixed string its lines carry) andSUDO_ROOT_FILES.get_history_sudo_lines()counts the lines per history file (~/.bash_history,~/.zsh_history,~/.ash_history,~/.sh_history) withgrep -c -F, as the login user. No line is read back; an unreadable file raises.remove_history_sudo_lines()runs two steps,historyandowners, the second also when the first failed.historydeletes exactly those lines as the login user withsed -i.napalm-bak, overwrites the backup (the old file's blocks) withshred -u(rm -fwithout 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, runschown -h 0:0on eachSUDO_ROOT_FILESentry 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 aFixResultDictplusfiles(before/after per file),removedandowners_restored.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 withshagainst a home directory in a temp dir, with achownthat only records: counting, exact deletion, fixed-string matching, no backup left, links left alone, unreadable files, a failingsed, asedthat 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.py100 %;ruff format --check,ruff check,mypy(strict),bandit,napalm-standard checkandratchetclean.