fix(trust): a project's key mappings that can run commands wait for :trust

Merged
KI
21d ago
+966-27

Problem

A project's .vigent.toml holds back everything that runs programs until you :trust it: hooks, [commands] shell steps, engine commands and arguments, and Claude Code allow rules. Its [keys] mappings were applied untrusted, though. A mapping's right-hand side is replayed as keys, so a cloned repository could map j to :!cmd<CR>, and pressing j ran a shell command without :trust. The same was reachable through:

  • raw keys (q:!cmd<CR>);
  • lists ([":", "!cmd", "<CR>"]);
  • <Cmd>…<CR> mappings;
  • [keys.command] lines;
  • insert-mode <Esc>:!cmd<CR>;
  • mappings that define a command or mapping (:command, :nnoremap) or replay keys (:normal).

The registry had the matching gap: it builds runs from the trust gate, so a keymap entry mapping a key to :!cmd<CR> was shown as "data only" in vigent add and on the website.

Change

  • mappingCommandLines and commandLineRisk (src/keys.ts and keys.rs, mirrored):
    • The first simulates a right-hand side the way vigent replays it and returns each command line it would run. Lines it can't follow (cursor moves, history, completion, paste) count as risky.
    • A line is risky if it's a shell command (after any range), a definer or replayer (:command, :delcommand, the map/noremap/unmap family, :normal, :execute, :source), :trust or :permissions allow, or a user command whose expanded steps do one of these.
  • loadConfig: in an untrusted project file, a risky mapping isn't applied and is named in the trust message as keys.<mode>.<lhs>. :trust now applies the mappings that waited to the live keymap, in both builds. Safe mappings and your own config.toml are unchanged.
  • Registry:
    • runsOf lists a keymap's risky command lines, so vigent add and the website show them.
    • The validator rejects keymaps that define, replay, trust, or hide what they run.
    • In the regenerated index, command/land now also lists <leader>L → Land.
  • Docs: docs/config.md (the trust paragraph, and "A project's mappings"), docs/registry.md (safety model), and the registry page.
  • The TypeScript app and the Rust binary behave the same.

Verification

Run on this branch, which contains current main (5dcf358):

SuitePassFailSkipped
npm test470013
cargo test (crates/vigent)1950–
full pty suite through the packaged Rust binary47607
  • Unit tests in both languages:
    • every path above, ranges, user commands, and safe mappings that stay allowed;
    • the gating, and :trust applying the mapping;
    • the user's own config being unaffected;
    • the registry: a keymap with :!x<CR> is listed, and definers are rejected.
  • pty test on both builds: a repository maps j to :!touch pwned<CR>. Before :trust, j creates nothing and the message names keys.normal.j; after :trust, it creates the file. The test fails on the binary from before this change.
  • Differential run: a one-off comparison of 20,000 random right-hand sides across all modes found the TypeScript and Rust results identical.
  • cargo build --release has no warnings; the binary is 2.9 MB (1.3 MB download).

Follow-ups

  • [commands] steps without ! still apply untrusted, so a project command like X = "nnoremap j :!y<CR>" defines a shell mapping when you run :X. Gating definer steps in [commands] the same way would close that.
  • From a mapping, :w! <path> can overwrite a file with the transcript, and :serve opens the session to the network. Consider gating these too.
  • The :trust message is computed without your own config. A key held back only because it calls one of your commands is applied by :trust, but isn't named in the message.