From e661b66d7e255602bb6d8056e12aaf52e12399ec Mon Sep 17 00:00:00 2001 From: JeremyDev87 Date: Fri, 10 Apr 2026 02:32:54 +0900 Subject: [PATCH] feat(plugin): add live AI guardrails for real-time rule violation interception - Add RuleChecker with pattern detection for SQL injection, XSS, hardcoded secrets, eval/exec, and shell injection (strict mode) - Add ViolationRenderer with unicode box formatting and hook output - Wire guardrails into PreToolUse hook for Edit/Write tool calls - Configurable via CODINGBUDDY_GUARDRAIL_LEVEL (strict/normal/off) - False-positive filtering: skip comments, env var refs, placeholders - 50 tests covering all detection patterns and edge cases Closes #1439 --- .../hooks/lib/rule_checker.py | 285 +++++++++++++++ .../hooks/lib/violation_renderer.py | 93 +++++ .../claude-code-plugin/hooks/pre-tool-use.py | 19 +- .../hooks/tests/test_rule_checker.py | 324 ++++++++++++++++++ .../hooks/tests/test_violation_renderer.py | 168 +++++++++ 5 files changed, 887 insertions(+), 2 deletions(-) create mode 100644 packages/claude-code-plugin/hooks/lib/rule_checker.py create mode 100644 packages/claude-code-plugin/hooks/lib/violation_renderer.py create mode 100644 packages/claude-code-plugin/hooks/tests/test_rule_checker.py create mode 100644 packages/claude-code-plugin/hooks/tests/test_violation_renderer.py diff --git a/packages/claude-code-plugin/hooks/lib/rule_checker.py b/packages/claude-code-plugin/hooks/lib/rule_checker.py new file mode 100644 index 00000000..e436c12e --- /dev/null +++ b/packages/claude-code-plugin/hooks/lib/rule_checker.py @@ -0,0 +1,285 @@ +"""Live AI Guardrails — real-time rule violation detection (#1439). + +Intercepts Edit/Write tool calls and checks content against known +security violation patterns (SQL injection, XSS, hardcoded secrets, etc.). +Configurable via CODINGBUDDY_GUARDRAIL_LEVEL env var: strict/normal/off. +""" +import os +import re +from dataclasses import dataclass +from typing import List, Optional + + +@dataclass +class Violation: + """A single rule violation found in code content.""" + + rule_id: str + severity: str + message: str + line_content: str + suggested_fix: str + + +# --- Comment detection --- + +_COMMENT_RE = re.compile(r"^\s*(?:#|//|/\*|\*|