Skip to content

Unanchored match_command in liquibase.toml rewrites any command containing /liquibase #3757

Closed

Description

@KuSh

Summary

src/filters/liquibase.toml:3 is the only one of the 63 shipped filters whose match_command is not anchored at ^:

match_command = "(?:^|/)liquibase(?:\\s|$)"

Because the TOML branch matches against the whole command string, any unrecognised leading word followed by a path containing /liquibase gets rewritten to rtk <whole thing>.

Reproduction

Verified against a locally built binary (rtk rewrite — exit 3 + stdout means "rewritten", exit 1 means "passed through"):

timeout 5 /usr/bin/liquibase update    → 3  rtk timeout 5 /usr/bin/liquibase update
nohup /opt/tools/liquibase update      → 3  rtk nohup /opt/tools/liquibase update
liquibase update                       → 3  rtk liquibase update                     ← correct
echo /liquibase update                 → 1                                           ← `echo` is ignored, so it escapes

This is pre-existing and independent of any sudo work — timeout and nohup reproduce it on develop today. #2810 simply adds sudo to the set of leading words that expose it:

sudo /usr/bin/liquibase update   → 3  rtk sudo /usr/bin/liquibase update
sudo ./liquibase update          → 3  rtk sudo ./liquibase update
sudo liquibase update            → 1                                     ← correct

Cause

src/discover/registry.rs:1398, in the Classification::Unsupported (TOML-only) branch:

let normalized = strip_absolute_path(cmd_part.trim());
…
if crate::core::toml_filter::command_matches_filter(&normalized) {
    return Some(format!("rtk {}{}", cmd_part, redirect_suffix));
}

strip_absolute_path only peels a leading path. When the first word is timeout / nohup / sudo, normalized is the untouched full string, and the filter's (?:^|/)liquibase alternation happily matches the /liquibase in the middle of it. The is_rtk_reserved_command(base) check just above (:1403) only inspects the first word, so it doesn't help here.

Note that RULES cannot misfire the same way: all 86 patterns in rules.rs are ^-anchored. This is specific to TOML filters, and today specific to this one file.

Not a security issue

Confirmed, so this can be triaged as correctness rather than as a vulnerability:

  • rtk sudo X (and rtk timeout X) has no handler, so it falls through run_fallback and execs the original command verbatim as the invoking user — sudo still prompts for a password normally.
  • With {"permissions": {"deny": ["Bash(sudo:*)"]}} in ~/.claude/settings.json, both the patched and unpatched binaries return exit 2 (Deny) identically, because rewrite_cmd.rs:48 runs check_command(cmd) on the original string before any rewrite.

The real cost is a bogus rtk prefix on a command RTK cannot filter, plus a misleading entry in the savings analytics.

Suggested fix

Either (or both):

  1. Anchor the pattern in src/filters/liquibase.toml — "^(?:[^\\s]*/)?liquibase(?:\\s|$)" — bringing it in line with the other 62 filters.
  2. Gate the TOML branch on the first word: match command_matches_filter against the base command rather than the full string, so an unanchored user-supplied pattern can never reach past argv[0]. This is the durable fix, since project-local and global TOML filters (.rtk/filters/, ~/.config/rtk/filters/) are user-authored and will hit this again.

Test coverage (required)

Nothing currently pins this — there is no test for a TOML filter behind a leading wrapper word. A fix should add:

  • timeout 5 /usr/bin/liquibase update and nohup /opt/tools/liquibase update → not rewritten
  • liquibase update and /usr/bin/liquibase update → still rewritten to rtk liquibase update (regression guard for the legitimate path-prefixed case that motivated the (?:^|/) alternation)
  • a repo-level assertion that every match_command in src/filters/*.toml starts with ^, so filter 64 can't silently reintroduce this

Context

Found while reviewing #2810.


Status after #3807

Fixed. Suggested fix 1 landed in cab2283: src/filters/liquibase.toml:3 is now ^liquibase(?:\s|$), and the three wrapper repros above (timeout, nohup, sudo) no longer rewrite.

Suggested fix 2, by a different mechanism. Rather than gating the TOML branch on the first word, #3807 enforces the anchoring rule at load time: a shared is_fully_anchored predicate splits match_command on top-level | and requires every branch to be ^-anchored, applied in both match_patterns_in and TomlFilterRegistry::parse_and_compile. An unanchored user-supplied pattern is therefore dropped with a warning and never reaches MATCH_SET, so it cannot match past argv[0]. This covers the project-local and global filter case this issue called out as the durable fix. Matching against the base command instead of the full string remains an option if the stricter containment is wanted later.

Test coverage — two of three bullets delivered:

  • ✅ timeout 5 /usr/bin/liquibase update and nohup /opt/tools/liquibase update → not rewritten.
  • ✅ Repo-level assertion that every built-in match_command is anchored, reading raw patterns from BUILTIN_TOML so the load-time gate cannot hide an unanchored filter from the assertion. It rejects (?:^|/)liquibase(?:\s|$) and ^liquibase\b|/liquibase\b, and accepts per-branch forms such as ^pnpm\b|^npm\b.
  • ❌ /usr/bin/liquibase update → still rewritten. Not delivered. test(filters): assert all builtin toml match_command patterns are anchored #3807 pins the current None as a characterization test, with a comment recording the divergence. That behavior is a bug in its own right — see below.

Still open: path-qualified commands are classified as supported but never rewritten

This is the remaining bug, and it is the requested behavior of the third test bullet above.

classify_command("/usr/bin/liquibase update")
// => Supported { rtk_equivalent: "rtk liquibase", .. }

rewrite_command_no_prefixes("/usr/bin/liquibase update", &[])
// => None

classify_command is right: /usr/bin/liquibase update is a liquibase invocation, RTK has a filter for it, and it should be rewritten to rtk liquibase update. The rewrite path is what is wrong — it silently drops a command it is capable of filtering, so rtk discover reports an optimization opportunity that the hook then declines to take. That is lost savings on every path-qualified invocation, plus a misleading discover report.

Not liquibase-specific: /usr/bin/grep -rn foo src and /usr/bin/make build are dropped the same way, so this affects every command RTK supports whenever argv[0] is written as a path.

Cause: classify_command basenames argv[0] via strip_absolute_path (src/discover/registry.rs:133) and correctly reports Supported. The rewrite branch then matches RULES.rewrite_prefixes (src/discover/rules.rs:950) against the un-normalized cmd_part (src/discover/registry.rs:1473), finds no prefix, and falls through to None.

Requested fix: normalize cmd_part the same way classify_command already does, before the rewrite_prefixes match, so path-qualified argv[0] rewrites. The wrapper misfire this issue was opened for does not come back: the anchor now sits on argv[0] rather than on the whole command line, which is exactly the legitimate case the original (?:^|/) alternation was reaching for.

Implementation note: decide what the rewritten form should be. rtk liquibase update drops the explicit path, so the filter resolves liquibase from PATH — potentially a different binary than the /usr/bin/liquibase the user asked for. Preserving the path through the rewrite avoids that; the original bullet above assumed the basename form.

On merge: fixing this flips test_path_qualified_liquibase_is_not_rewritten (added in #3807), which pins the current None. That test should be updated to assert the rewrite rather than deleted, so the path-qualified case stays covered.

Activity

  1. added
    bug
    P3-nice-to-have
    effort-small
    filter-quality
    on Aug 29, 2026
  2. amandeavor commented on Sep 1, 2026

    @amandeavor
    Contributor

    I have submitted PR #3807 adding \ est_builtin_all_filters_match_command_anchored\ to verify all 63 built-in filters start with ^, along with \ est_rewrite_liquibase_wrapper_and_paths\ verifying wrapper isolation and bare invocations.

  3. KuSh commented on Sep 4, 2026

    @KuSh
    CollaboratorAuthor

    Already fixed on develop — closing.

    Both suggested fixes landed independently:

    1. Pattern anchored — src/filters/liquibase.toml:3 is now "^liquibase(?:\\s|$)" (b6257c7e fix(filters): tighten overly broad spring-boot/liquibase/ssh matching, refined by cab22839).
    2. Load-time guard — 35224779 fix(filters): reject unanchored TOML match_command at load added is_fully_anchored (src/core/toml_filter.rs:521), applied at :238, so an unanchored match_command can no longer reach command_matches_filter at all. This covers the durable half of the report: user-authored filters in ~/.config/rtk/filters/ and .rtk/filters/ can't reintroduce the same shape.

    The test coverage requested in the issue is present too — toml_filter.rs:2131-2136 pins the anchoring rules and asserts the exact reported pattern is rejected:

    assert!(!is_fully_anchored(r"(?:^|/)liquibase(?:\s|$)"));

    Re-ran the original repro against a build of current develop; all three now pass through untouched instead of being rewritten:

    timeout 5 /usr/bin/liquibase update    → exit 1 (was: rtk timeout 5 /usr/bin/liquibase update)
    sudo /usr/bin/liquibase update         → exit 1 (was: rtk sudo /usr/bin/liquibase update)
    liquibase update                       → exit 3  rtk liquibase update      ← still correct
    

    Note this was never dependent on #2810 — timeout and nohup triggered it on develop all along.

  4. added
    resolved-pending-close
    and removed
    resolved-pending-close
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3-nice-to-havebugeffort-smallfilter-quality

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions