Repository navigation
fix(rewrite): stop rewriting sudo commands (pass them through) - #2810
Conversation
|
Hey @pszymkowiak This does not fix the issue but bypass it, sudo should still get rewritten.
|
There was a problem hiding this comment.
Reviewed with a code-review high pass on a locally built binary: a 17-command rewrite matrix, the rtk hook claude path, and all three ENV_PREFIX consumers.
The behaviour is exactly right. sudo docker ps, sudo -u root …, sudo -E …, FOO=1 sudo …, env FOO=1 sudo …, bare sudo, sudoedit … and sudo noglob … all pass through untouched, while env FOO=1 docker ps, FOO=1 git status, noglob git status and docker ps still rewrite normally. Compound commands split correctly (ls && sudo docker ps → rtk ls && sudo docker ps). And this is the fix that unblocks a real rtk sudo: removing sudo\s+ from ENV_PREFIX is the prerequisite for a ^sudo rule ever matching — a guard placed inside rewrite_segment_inner instead (as in #3569) would leave sudo stripped before any rule is consulted. Thanks for taking the right approach here.
Three requested changes, three of them mechanical.
1. Rebase — blocking
The PR is CONFLICTING against develop, which migrated lazy_static! { … } to individual LazyLock statics. See the inline comment on the ENV_PREFIX hunk for the exact resolution — I built and tested it, 2651 tests green.
2. Two doc lines are made false by this PR
Neither file is in the diff, so GitHub won't let me anchor these inline:
docs/guide/getting-started/configuration.md:94
-Patterns match against the full command after stripping env prefixes (`sudo`, `VAR=val`), so `"psql"` excludes both …
+Patterns match against the full command after stripping env prefixes (`VAR=val`), so `"psql"` excludes both …src/discover/README.md:30
-3. Classify the command — strip env prefixes (`sudo`, `FOO="bar baz"`), normalize paths …
+3. Classify the command — strip env prefixes (`FOO="bar baz"`), normalize paths …3. Missing coverage for the cases the passthrough depends on
FOO=1 sudo …, env FOO=1 sudo …, bare sudo, and sudoedit … are all correct today but pinned by nothing — and they're precisely #3569's motivating cases. Suggestion inline, rustfmt-clean, all four verified passing against this branch.
Two pre-existing problems surfaced while reviewing. Neither is yours to fix and neither blocks this PR — filed separately as #3756 (sudo RTK_DISABLED=1 … stops being counted as a bypass in rtk discover/rtk gain, an analytics-only hole) and #3757 (an unanchored match_command in liquibase.toml, which timeout/nohup already trigger on develop today).
Also note the CI benchmark job is red on develop itself (last five runs) — not from this PR.
The env-prefix stripper treated `sudo` like `env` / `VAR=val` and rewrote `sudo docker ps` into `sudo rtk docker ps`. That breaks at runtime: `rtk` lives in ~/.local/bin, which is not on sudo's secure_path, so the rewritten command fails with "rtk: command not found" under root (reported in #146). And where rtk *is* on secure_path, `sudo rtk` would run the whole rtk binary as root — an unnecessary-privilege footgun. Drop `sudo` from the env-prefix regex so sudo commands pass through untouched. The permission verdict path is unaffected (it never used this regex and already matches sudo commands as-is, e.g. `sudo:*` rules). env / VAR= prefixes and transparent builtins (noglob, command, …) still rewrite normally. Verified: `sudo docker ps` / `sudo -u root docker ps` / `sudo noglob git status` are no longer rewritten; `env FOO=bar docker ps`, `FOO=bar docker ps`, `noglob git status` still are. fmt/clippy clean, full test suite green. Refs #146 Co-Authored-By: Claude Opus 4.8 <[email protected]>
713b448 to
a8bb67a
Compare
The "Env Prefix Handling" section of src/discover/README.md still listed `sudo` as a stripped prefix and used it in the chained example; two comments in registry.rs made the same claim. The `sudo head` case in test_head_tail_honour_exclude_commands no longer tested exclusion — it returns None whether or not `head` is excluded, because sudo blocks the rewrite outright. Dropped it; the `RUST_LOG=debug tail` case still covers the env-prefix path. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
KuSh
left a comment
There was a problem hiding this comment.
Verified and approved.
Rebase, both doc lines, and the four passthrough cases all landed as requested — thanks for the quick turnaround. I pushed one follow-up commit (b811434) for two stale-documentation leftovers my earlier pass had missed, so nothing further is needed from you:
src/discover/README.md— the dedicated "Env Prefix Handling" section still listedsudoas a stripped prefix and usedA="x y" B=1 sudo git statusas its chained example. That is the section a contributor reads before touchingENV_PREFIX, so it invited re-adding exactly what this PR removes. Example swapped toenv, verified against the built binary.test_head_tail_honour_exclude_commands— thesudo head -20 src/main.rsassertion had become vacuous: it returnsNonewhether or notheadis excluded, because sudo blocks the rewrite outright, so it no longer tested exclusion at all. Dropped it, and corrected the two comments claimingstrip_disabled_prefixpeelssudo(one inrewrite_segment_inner, one in the test). TheRUST_LOG=debug tailcase still covers the env-prefix path.
Verification, in a clean worktree and separately merged onto current develop (which has since picked up #3704's lexer refactor in this same file): cargo fmt --check clean, cargo clippy --all-targets zero warnings, cargo test --all green on both. Behaviour re-checked on the built binary rather than inferred from the assertions — every sudo form passes through (including sudo -E, a double-space sudo docker ps, and sudoedit), non-sudo rewrites are unaffected, ls && sudo docker ps becomes rtk ls && sudo docker ps, and the hook path emits nothing for sudo docker ps.
Two known consequences are tracked separately and are deliberately not blockers: sudo commands now surface under TOP UNHANDLED in rtk discover (accepted — #146 collapses them into a single Elevation row), and #3756 covers the sudo RTK_DISABLED=1 … bypass accounting.
Problem (#146)
The env-prefix stripper (added in the original auto-rewrite hook) treats
sudolikeenv/VAR=valand rewritessudo docker ps→sudo rtk docker ps. Two problems:rtkinstalls to~/.local/bin, which is not on sudo'ssecure_path— sosudo rtk docker psfails withrtk: command not foundunder root. Reported on Feature Request: Add sudo prefix support for command rewriting #146 by a user.Fix (option "pass through")
Drop
sudofrom the env-prefix regex. sudo commands are now left untouched and pass through unchanged.sudo:*rules keep working), so least-privilege behaviour is unchanged.env/VAR=valprefixes and transparent builtins (noglob,command, …) still rewrite normally.Verification
sudo docker pssudo rtk docker ps(broken under root)sudo -u root docker pssudo noglob git statussudo noglob rtk git statusenv FOO=bar docker psenv FOO=bar rtk docker psFOO=bar docker ps/noglob git status/docker psTests updated (
test_classify_sudo_not_stripped,test_rewrite_sudo_passthrough,test_sudo_with_builtin_not_rewritten);cargo fmt/cargo clippy --all-targetsclean; full test suite green (2361 passed).Note
This is the safe, minimal resolution. A future "real" sudo feature (rtk orchestrating
sudo <cmd>while staying a non-root process) could be revisited separately if the docker/kubectl-under-sudo savings justify it — but it must never run rtk itself as root.Refs #146
🤖 Generated with Claude Code