Class DestructiveCommandDetector

java.lang.Object
com.tnsai.quality.security.DestructiveCommandDetector
All Implemented Interfaces:
com.tnsai.agents.execution.ToolCallFilter

public final class DestructiveCommandDetector extends Object implements com.tnsai.agents.execution.ToolCallFilter
Content-level ToolCallFilter that scans tool arguments for destructive shell / git / filesystem commands hidden inside generic tools like terminal_execute("rm -rf /") (issue #36).

Complements Tool.requiresConfirmation() which is a tool-identity flag — this detector inspects the argument payload of any tool call. Both are intended to be chained: tool-identity confirmation gates shells in general; this detector spots specific dangerous payloads regardless of which shell-like tool dispatches them.

The default pattern set covers the common destructive families: rm -rf, git reset --hard / git clean -fd, dd-to-device, mkfs, world-permissive chmod, force-kill broadcasts, and writes to system / SSH config locations. Each pattern carries a DestructiveCommandDetector.Severity; the detector returns ToolCallAction.block() for DestructiveCommandDetector.Severity.CRITICAL matches and ToolCallAction.guide(java.lang.String) (with corrective feedback) for DestructiveCommandDetector.Severity.HIGH matches by default. The mapping is configurable via DestructiveCommandDetector.Builder.severityAction(Severity, java.util.function.Function).

Default patterns load from tnsai-quality/src/main/resources/security/destructive-patterns.txt — operators ship custom patterns by passing additional resources to DestructiveCommandDetector.Builder.additionalPatternsResource(String) or registering inline via DestructiveCommandDetector.Builder.pattern(DestructivePattern).

Performance: per-call overhead is dominated by a small number of compiled regexes evaluated against concatenated argument strings. Patterns are compiled once at builder time. Target < 1ms for a typical payload of a few hundred chars on commodity hardware.

Since:
0.5.0 (issue #36)
  • Field Details

    • DEFAULT_PATTERNS_RESOURCE

      public static final String DEFAULT_PATTERNS_RESOURCE
      Bundled default-patterns resource. Format: one pattern per line, blank lines and lines starting with # are ignored. Each non-comment line has 3 tab-separated fields: <name>\t<severity>\t<regex>.
      See Also:
  • Method Details