Skip to content

fix(rewrite): stop rewriting sudo commands (pass them through) - #2810

Merged
KuSh merged 2 commits into
developfrom
fix/no-sudo-rewrite
Sep 4, 2026
Merged

KuSh merged 2 commits into
developfrom
fix/no-sudo-rewrite

Conversation

@pszymkowiak

Copy link
Copy Markdown
Collaborator

Problem (#146)

The env-prefix stripper (added in the original auto-rewrite hook) treats sudo like env / VAR=val and rewrites sudo docker ps → sudo rtk docker ps. Two problems:

  1. It breaks the command. rtk installs to ~/.local/bin, which is not on sudo's secure_path — so sudo rtk docker ps fails with rtk: command not found under root. Reported on Feature Request: Add sudo prefix support for command rewriting #146 by a user.
  2. Where rtk is on secure_path, it runs rtk as root — a large parsing/SQLite binary executing with elevated privileges, an unnecessary-privilege footgun for what is only output filtering.

Fix (option "pass through")

Drop sudo from the env-prefix regex. sudo commands are now left untouched and pass through unchanged.

  • The permission verdict path is unaffected — it never used this regex and already matches sudo commands as-is (sudo:* rules keep working), so least-privilege behaviour is unchanged.
  • env / VAR=val prefixes and transparent builtins (noglob, command, …) still rewrite normally.

Verification

Input Before After
sudo docker ps sudo rtk docker ps (broken under root) (pass through)
sudo -u root docker ps (not handled) (pass through)
sudo noglob git status sudo noglob rtk git status (pass through)
env FOO=bar docker ps env FOO=bar rtk docker ps unchanged ✅
FOO=bar docker ps / noglob git status / docker ps rewritten unchanged ✅

Tests updated (test_classify_sudo_not_stripped, test_rewrite_sudo_passthrough, test_sudo_with_builtin_not_rewritten); cargo fmt / cargo clippy --all-targets clean; 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

@pszymkowiak
pszymkowiak requested a review from aeppling July 4, 2026 10:03
@aeppling

aeppling commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Hey @pszymkowiak

This does not fix the issue but bypass it, sudo should still get rewritten.
Anyway, if you want to drop sudo rewrite to solve this, there are still some blockers to solve before

  1. rtk discover regression. classify_command loses sudo so sudo commands land in the TOP UNHANDLED COMMANDS -- open an issue? table. Observed vs develop: sudo docker / sudo apt / sudo systemctl / sudo RTK_DISABLED=1 / sudo.

  2. RTK_DISABLED detection broken for sudo. strip_disabled_prefix shares the regex -> sudo RTK_DISABLED=1 docker ps no longer flagged. The RTK_DISABLED BYPASS section vanishes from discover, and gain.rs stops warning entirely (develop: [warn] 2 commands (14%) used RTK_DISABLED=1 unnecessarily; branch: nothing). No test covers sudo + RTK_DISABLED, hence green CI.

  3. transparent_prefixes = ["sudo"] still emits sudo rtk docker ps. Verified on the branch binary (also sudo rtk git status, sudo rtk kubectl get pods). PR says rtk "must never run rtk itself as root", that needs a guard, not a default.

@KuSh KuSh left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/discover/registry.rs Resolved
Comment thread src/discover/registry.rs Resolved
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]>
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 KuSh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 listed sudo as a stripped prefix and used A="x y" B=1 sudo git status as its chained example. That is the section a contributor reads before touching ENV_PREFIX, so it invited re-adding exactly what this PR removes. Example swapped to env, verified against the built binary.
  • test_head_tail_honour_exclude_commands — the sudo head -20 src/main.rs assertion had become vacuous: it returns None whether or not head is excluded, because sudo blocks the rewrite outright, so it no longer tested exclusion at all. Dropped it, and corrected the two comments claiming strip_disabled_prefix peels sudo (one in rewrite_segment_inner, one in the test). The RUST_LOG=debug tail case 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants