()
| 202 | |
| 203 | |
| 204 | def main(): |
| 205 | # Read JSON input from stdin |
| 206 | try: |
| 207 | input_data = json.load(sys.stdin) |
| 208 | except json.JSONDecodeError: |
| 209 | # If we can't parse input, allow the command |
| 210 | sys.exit(0) |
| 211 | |
| 212 | # Only check Bash commands |
| 213 | tool_name = input_data.get("tool_name", "") |
| 214 | if tool_name != "Bash": |
| 215 | sys.exit(0) |
| 216 | |
| 217 | # Extract command |
| 218 | tool_input = input_data.get("tool_input", {}) |
| 219 | command = extract_command(tool_input) |
| 220 | |
| 221 | if not command: |
| 222 | sys.exit(0) |
| 223 | |
| 224 | # Type narrowing: command is guaranteed to be str here |
| 225 | assert command is not None |
| 226 | |
| 227 | # Check for override environment variable |
| 228 | # First check if it's set in the command string (e.g., FL_AGENT_ALLOW_ALL_CMDS=1 ninja) |
| 229 | command_env_vars = parse_env_vars_from_command(command) |
| 230 | override_in_command = OVERRIDE_ENV_VAR in command_env_vars and command_env_vars[ |
| 231 | OVERRIDE_ENV_VAR |
| 232 | ] in ("1", "true", "True", "TRUE") |
| 233 | |
| 234 | # Then check if it's set in the process environment |
| 235 | override_in_env = bool(os.environ.get(OVERRIDE_ENV_VAR)) |
| 236 | |
| 237 | # Override is enabled if set in either location |
| 238 | override_enabled = override_in_command or override_in_env |
| 239 | |
| 240 | # Check 1: Forbidden commands |
| 241 | forbidden_result = is_forbidden_command(command) |
| 242 | if forbidden_result: |
| 243 | forbidden_cmd, recommendation = forbidden_result |
| 244 | |
| 245 | if override_enabled: |
| 246 | if DRY_RUN: |
| 247 | print( |
| 248 | f"[DRY-RUN] Would allow '{forbidden_cmd}' due to {OVERRIDE_ENV_VAR}", |
| 249 | file=sys.stderr, |
| 250 | ) |
| 251 | sys.exit(0) |
| 252 | |
| 253 | # Comprehensive error message: what's forbidden, why, alternative, and override |
| 254 | error_msg = ( |
| 255 | f"{forbidden_cmd} is forbidden - {recommendation}. " |
| 256 | f"To override this check, use: {OVERRIDE_ENV_VAR}=1 {command}" |
| 257 | ) |
| 258 | |
| 259 | if DRY_RUN: |
| 260 | print(f"[DRY-RUN] Would block: {error_msg}", file=sys.stderr) |
| 261 | sys.exit(0) |
no test coverage detected