kimi-code icon indicating copy to clipboard operation
kimi-code copied to clipboard

Feature request: Structured policy API for security hooks (PreToolUse)

Open ploteddie-bit opened this issue 3 months ago • 1 comments

Feature request: Structured policy API for security hooks (PreToolUse)

Context

Security hooks (e.g. the local permission hook, Aegis) decide allow/deny by parsing the command string received from the runtime. Documented on 2026-08-29:

Command Actual intent Hook verdict Failure mode
printf '...' >> ~/.bashrc legitimate append blocked ("dangerous pattern") false positive: judged on shape, not effect
python3 aegis-mode.py status read-only status query blocked false positive: the string contains a protected path but never modifies it
cmd1 && cmd2 chains two operations approved false negative: chaining decomposes the command out of reach of the analysis

Net effect: friction on safe commands AND a bypass on compound commands — the worst of both worlds for a security layer.

Problem or Gap

Hooks receive a raw command string. Substring matching cannot distinguish "mentions a protected path" from "writes to a protected path", and it cannot see the individual operations of a &&/||/; chain. Policy decisions therefore produce both false positives and false negatives.

Proposal

  1. Structured call representation: PreToolUse hooks receive a structured representation of the call instead of a string — the tool invoked, typed arguments, and for shell commands the decomposed operations (each segment of a chain presented as a distinct, individually analyzable operation).
  2. Structured decision object: the hook returns {action: allow|deny|ask, reason, scope} instead of an exit code/string — scope allowing the decision to target a single operation of a chain.
  3. Queryable audit trail: every decision (allow/deny/ask) is logged in a queryable audit trail: timestamp, concerned call, triggering rule, verdict.

Prior art (open source): deepseek-harness monorepo 0.1.2-alpha.1, packages/guard/command-policy/ — a declarative policy engine.

Impact

  • Fewer false positives: verdicts on operation semantics instead of substrings.
  • No cheap bypass: each chain operation is individually analyzed.
  • Auditability: decisions are recorded with their rule and reason, queryable after the fact.

Acceptance criteria

  1. A PreToolUse hook receives the decomposed operations of a && chain (one entry per operation, typed arguments).
  2. The hook decision is consumed by the runtime as a structured object; an ask on a single operation of a chain only gates that operation.
  3. Every decision is queryable in the audit trail (command, rule, verdict, timestamp).
  4. The two 2026-08-29 false positives (printf >> ~/.bashrc, python3 aegis-mode.py status) are no longer blocked by substring matching.
  5. Commands that cannot be safely decomposed (complex quoting/substitutions) fall back conservatively to ask on the whole command.
  6. A malformed hook decision falls back to the safe default (ask) and logs the error.
  7. Hooks for tools without a structured representation receive the current string fallback (no crash).

ploteddie-bit avatar Aug 29 '26 05:08 ploteddie-bit

The false-positive/false-negative table here is exactly the failure mode addressed natively (not via hooks) in #4115: the exec-policy layer evaluates the parsed AST per command segment rather than the command string — cmd1 && cmd2 produces two segments, protected-path writes are matched on argv tokens not substring shape, and unanalyzable segments fail conservative. The .rules format (prefix_rule/network_rule over managed/user/project layers) is effectively a structured policy API for commands. See #4114.

Ram-Pavith avatar Oct 03 '26 08:10 Ram-Pavith