|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Cross-platform auto-approval hook for the lrc review flow. |
| 4 | +
|
| 5 | +Reads a Claude Code PreToolUse JSON payload from stdin and emits an allow |
| 6 | +decision when the command matches a known-safe lrc plugin operation. |
| 7 | +Exits with no output if no approval rule matches (normal permission flow |
| 8 | +continues). |
| 9 | +
|
| 10 | +Used by both the Bash-matcher and PowerShell-matcher hooks in hooks.json so |
| 11 | +that the same approval rules apply regardless of which shell tool Claude |
| 12 | +chose to run the command with. |
| 13 | +""" |
| 14 | +import json |
| 15 | +import os |
| 16 | +import shlex |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | + |
| 21 | +def emit_allow(reason: str) -> None: |
| 22 | + payload = json.dumps( |
| 23 | + { |
| 24 | + "hookSpecificOutput": { |
| 25 | + "hookEventName": "PreToolUse", |
| 26 | + "permissionDecision": "allow", |
| 27 | + "permissionDecisionReason": reason, |
| 28 | + } |
| 29 | + } |
| 30 | + ) |
| 31 | + print(payload) |
| 32 | + |
| 33 | + |
| 34 | +def _normalize_source_prefix(tokens: list) -> list: |
| 35 | + """Strip `source ~/.lrc/env [2>/dev/null || true] &&` prepended by some model runs.""" |
| 36 | + if len(tokens) >= 4 and tokens[0] == "source" and tokens[1] == "~/.lrc/env" and tokens[2] == "&&": |
| 37 | + return tokens[3:] |
| 38 | + if ( |
| 39 | + len(tokens) >= 7 |
| 40 | + and tokens[0] == "source" |
| 41 | + and tokens[1] == "~/.lrc/env" |
| 42 | + and tokens[2] == "2>/dev/null" |
| 43 | + and tokens[3] == "||" |
| 44 | + and tokens[4] == "true" |
| 45 | + and tokens[5] == "&&" |
| 46 | + ): |
| 47 | + return tokens[6:] |
| 48 | + return tokens |
| 49 | + |
| 50 | + |
| 51 | +def _paths_equal(a: Path, b: Path) -> bool: |
| 52 | + """Compare two paths, resolving if both exist; otherwise compare normalized strings.""" |
| 53 | + try: |
| 54 | + return a.resolve() == b.resolve() |
| 55 | + except Exception: |
| 56 | + pass |
| 57 | + # Fallback: compare as strings after normalizing separators (handles mixed / and \) |
| 58 | + return str(a).replace("\\", "/").lower() == str(b).replace("\\", "/").lower() |
| 59 | + |
| 60 | + |
| 61 | +def main() -> None: |
| 62 | + plugin_root = os.environ.get("CLAUDE_PLUGIN_ROOT", "").strip() |
| 63 | + plugin_scripts = Path(plugin_root, "scripts") if plugin_root else None |
| 64 | + |
| 65 | + try: |
| 66 | + payload = json.load(sys.stdin) |
| 67 | + except Exception: |
| 68 | + return |
| 69 | + |
| 70 | + command = (payload.get("tool_input", {}).get("command") or "").strip() |
| 71 | + if not command: |
| 72 | + return |
| 73 | + |
| 74 | + # Parse the command into tokens using POSIX mode (handles both bash and |
| 75 | + # PowerShell invocations reasonably well for our whitelist patterns). |
| 76 | + try: |
| 77 | + tokens = shlex.split(command, posix=True) |
| 78 | + except ValueError: |
| 79 | + return |
| 80 | + |
| 81 | + tokens = _normalize_source_prefix(tokens) |
| 82 | + if not tokens: |
| 83 | + return |
| 84 | + |
| 85 | + # ── Rule: bash .../ensure-lrc.sh --for-review ──────────────────────────── |
| 86 | + # Emitted by review/SKILL.md and hooks/SKILL.md on Unix. |
| 87 | + if tokens[0] == "bash" and len(tokens) >= 3 and tokens[-1] == "--for-review": |
| 88 | + if plugin_scripts: |
| 89 | + try: |
| 90 | + if _paths_equal(Path(tokens[1]), plugin_scripts / "ensure-lrc.sh"): |
| 91 | + emit_allow("Auto-allow plugin ensure-lrc review readiness command") |
| 92 | + return |
| 93 | + except Exception: |
| 94 | + pass |
| 95 | + |
| 96 | + # ── Rule: PowerShell & .../ensure-lrc.ps1 [-ForReview] ─────────────────── |
| 97 | + # Emitted by review/SKILL.md and hooks/SKILL.md on Windows via PowerShell tool. |
| 98 | + # Handles both: |
| 99 | + # & "C:\...\ensure-lrc.ps1" -ForReview |
| 100 | + # & "C:\...\ensure-lrc.ps1" |
| 101 | + # "C:\...\ensure-lrc.ps1" -ForReview (direct path invocation) |
| 102 | + if plugin_scripts: |
| 103 | + path_token_idx = None |
| 104 | + if tokens[0] == "&" and len(tokens) >= 2: |
| 105 | + path_token_idx = 1 |
| 106 | + elif len(tokens) >= 1: |
| 107 | + # Direct invocation without & operator |
| 108 | + first = tokens[0].strip("\"'") |
| 109 | + if first.lower().endswith("ensure-lrc.ps1"): |
| 110 | + path_token_idx = 0 |
| 111 | + |
| 112 | + if path_token_idx is not None: |
| 113 | + raw_path = tokens[path_token_idx].strip("\"'") |
| 114 | + try: |
| 115 | + if _paths_equal(Path(raw_path), plugin_scripts / "ensure-lrc.ps1"): |
| 116 | + emit_allow("Auto-allow plugin ensure-lrc PowerShell command") |
| 117 | + return |
| 118 | + except Exception: |
| 119 | + pass |
| 120 | + |
| 121 | + # ── Rule: python3 .../setup-session.py start ───────────────────────────── |
| 122 | + if tokens[0] == "python3" and len(tokens) >= 3: |
| 123 | + if plugin_scripts: |
| 124 | + try: |
| 125 | + if _paths_equal(Path(tokens[1]), plugin_scripts / "setup-session.py"): |
| 126 | + if tokens[2] == "start" and len(tokens) == 3: |
| 127 | + emit_allow("Auto-allow plugin setup-session start command") |
| 128 | + return |
| 129 | + if tokens[2] == "submit-key" and len(tokens) >= 5 and tokens[3] == "--key": |
| 130 | + emit_allow("Auto-allow plugin setup-session submit-key command") |
| 131 | + return |
| 132 | + except Exception: |
| 133 | + pass |
| 134 | + |
| 135 | + # ── Rule: lrc review --staged [--blocking-review] ──────────────────────── |
| 136 | + if tokens[0] == "lrc" and len(tokens) >= 3 and tokens[1] == "review": |
| 137 | + if tokens[2] == "--staged": |
| 138 | + if len(tokens) == 3: |
| 139 | + emit_allow("Auto-allow lrc review staged command") |
| 140 | + return |
| 141 | + if len(tokens) == 4 and tokens[3] == "--blocking-review": |
| 142 | + emit_allow("Auto-allow lrc review blocking command") |
| 143 | + return |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + main() |
0 commit comments