Dippy icon indicating copy to clipboard operation
Dippy copied to clipboard

Fix config rules not matching env-var-prefixed commands

Open powli opened this issue 6 months ago • 2 comments

Summary

Config rules like allow symfony fail to match commands with a leading environment-variable assignment such as COMPOSE_PROJECT_NAME=app symfony console cache:clear. Dippy displays this command in the permission prompt as "symfony" (it strips the assignment for description/classification), but the config matcher checks the raw tokens — where COMPOSE_PROJECT_NAME=app sits before the base command and breaks the prefix match. So the user sees "symfony", writes allow symfony, and Dippy still asks.

This is the environment-variable variant of #136. Built-in handlers (git, docker, …) already work with env prefixes because they receive the env-stripped tokens; only commands matched purely by user config rules are affected. It's the case @Djaler raised in a comment on #136 (DOCKER_IMAGE=foo docker compose up not matching allow docker compose up).

Reproduction

from dippy.core.analyzer import analyze
from dippy.core.config import Config, Rule
from pathlib import Path

cfg = Config(rules=[Rule("allow", "symfony")])
print(analyze("symfony php foo", cfg, Path("/tmp")).action)           # allow
print(analyze("FOO=bar symfony php foo", cfg, Path("/tmp")).action)   # ask  <-- bug

Approach: two-pass config matching

Mirrors the shape proposed in #137 for global flags:

  • Step 1 (unchanged): match the raw tokens. Preserves explicit rules that include the assignment, e.g. deny FOO=* symfony *.
  • Step 1.5 (new): if step 1 finds no match and the command has leading env assignments (i > 0), retry match_command against the env-stripped tokens.

Also extracts a small _config_match_decision() helper (same name/shape as #137) to deduplicate the match→decision conversion, so the two changes converge cleanly regardless of merge order.

Tests

New class TestConfigEnvVarPrefixMatching in tests/test_analyzer_bugs.py:

  • allow matches an env-prefixed command (the bug)
  • baseline without prefix unchanged
  • multiple leading assignments
  • explicit raw-token rule still wins in step 1 (deny FOO=* symfony *)
  • no spurious match when no rule applies (falls through to default ask)
  • a stripped deny rule surfaces through the retry path

Full suite passes (10,904 tests) in an isolated HOME.

Relationship to #136 / #137

#137 fixes the global-flags case (git -C /path commit) but does not cover env assignments: its strip_global_flags() requires a handler (so symfony is skipped), and its retry rebuilds the match input as list(words[:i]) + stripped, which re-prepends the FOO=bar tokens. This PR is complementary and focuses solely on the env-assignment case. If both land, they merge with only trivial conflict (shared helper + adjacent step-1.5 block).

powli avatar May 31 '26 16:05 powli

Thanks for splitting this out and mirroring #137's two-pass so the explicit deny FOO=* cmd form still matches first — that part is right.

My concern is that env assignments aren't inert the way a stray flag is, so stripping them before matching is riskier than the global-flags case. FOO=bar cmd is part of the literal command, and the prefix can change what actually executes:

  • LD_PRELOAD=/tmp/evil.so ls would match allow ls
  • DOCKER_HOST=tcp://other docker ps would match allow docker ps
  • GIT_SSH_COMMAND='…' git fetch would match allow git fetch

The security model is explicit on this one: variables "stay literal … we match the literal string," expanded by the shell only after approval. Stripping the assignment to match allow cmd matches a string the shell will never run, and lets an unexamined prefix ride in under the allow. The two-pass preserves an explicit deny LD_PRELOAD=* *, but realistically nobody writes that, so the default outcome is that the assignment is ignored.

Rather than transparently stripping any FOO=bar prefix for the allow retry, could we hold the literal-match guarantee here — e.g. not strip env assignments for allow decisions at all, or only a known-inert subset? That keeps the env-prefixed command from silently inheriting a plain allow cmd.

ldayton avatar Jun 08 '26 10:06 ldayton

The concern about non-inert prefixes is fair — LD_PRELOAD=… cmd shouldn't silently inherit allow cmd, and the two-pass alone doesn't prevent that.

One data point worth factoring in, though: those examples are already auto-allowed on current main, without this PR:

from dippy.core.analyzer import analyze
from dippy.core.config import Config
from pathlib import Path

cfg = Config(rules=[])
analyze("LD_PRELOAD=/tmp/evil.so ls", cfg, Path("/tmp")).action         # allow (ls)
analyze("GIT_SSH_COMMAND=evil git fetch", cfg, Path("/tmp")).action     # allow (git fetch)
analyze("DOCKER_HOST=tcp://other docker ps", cfg, Path("/tmp")).action  # allow (docker ps)

_analyze_simple_command strips leading assignments before consulting SIMPLE_SAFE, wrapper commands, and the built-in handlers — only config rules currently see the raw tokens. So the literal-match guarantee is already not held on the built-in path; this PR as written would just have extended the existing behavior to config rules (including its hole).

Proposal: take your "known-inert subset" suggestion, with a user escape hatch.

  1. A curated INERT_ENV_VARS frozenset in allowlists.py (alongside SIMPLE_SAFE / WRAPPER_COMMANDS): display/locale/project-naming vars only — COMPOSE_PROJECT_NAME, NODE_ENV, CI, TZ, LANG, LC_*, TERM, NO_COLOR, FORCE_COLOR, … Nothing that affects executable resolution, library loading, or connection targets.
  2. A new config directive allow-env NAME (parallel to allow-redirect / allow-mcp) so users can extend it for vars their workflow needs — explicit, written-down opt-in, same trust model as any other allow rule.
  3. The step-1.5 retry then becomes asymmetric:
    • a deny/ask match from the stripped retry always applies (stripping in that direction only widens what gets blocked),
    • an allow match is honored only when every leading assignment's variable name is in INERT_ENV_VARS ∪ user allow-env. Otherwise fall through to the default ask.

This fails closed: a variable nobody thought about isn't in the list, so the command falls back to ask — unlike a denylist of dangerous vars, which fails open on anything not enumerated. Your three examples all hit ask under an unmodified config, while the reported case (COMPOSE_PROJECT_NAME=app symfony … + allow symfony) works out of the box.

It also gives a migration path for the built-in-path behavior shown above: once the list exists, the same gate can be applied before SIMPLE_SAFE/handler classification in a follow-up, which would restore the literal-match guarantee everywhere rather than only for config rules. I'd keep that out of this PR to limit scope, but happy to file it.

If this direction works for you I'll update the PR: gate + directive + tests (parsing/merge in test_config.py, behavior in TestConfigEnvVarPrefixMatching), plus the VS Code grammar/snippets and a Configuration wiki blurb for allow-env.

powli avatar Jun 09 '26 22:06 powli

Implemented the inert-subset approach in 687cf0b (rebased onto current main, single commit). The allow retry no longer strips arbitrary prefixes:

  • deny/ask retries are unconditional (stripping only ever blocks more);
  • an allow retry applies only when every leading assignment names an inert variable — a curated INERT_ENV_VARS (locale/color/project-name), the LC_* family, or a user allow-env NAME opt-in. PATH, LD_*, BASH_ENV, GIT_SSH_COMMAND, DOCKER_HOST, PYTHON*, PAGER, EDITOR are deliberately excluded, so your three examples now resolve to ask under an unmodified config. It fails closed: an unenumerated variable is treated as non-inert.

allow-env mirrors the existing python-allow-module directive; a bare * is rejected so the allowlist can't be silently nullified, and globs match case-sensitively.

One honest scoping note: this gate is on the config-rule path only. The SIMPLE_SAFE / version-help / wrapper paths already strip env prefixes before matching on main (e.g. LD_PRELOAD=… ls is allowed today), so a non-inert prefix on those still rides in. I kept that out of this PR to stay on the reported bug, but INERT_ENV_VARS is in allowlists.py so the same gate can be applied there — happy to file a follow-up (or fold it in here if you'd prefer the guarantee to be uniform before merge).

powli avatar Jun 30 '26 11:06 powli