# Without -Command this runs as a PreToolUse hook: it reads the event from stdin and exits 2 to block. # With -Command it checks one command and also rejects absolute paths outside -Workspace. # Pattern checks are a guard rail against accidents, not a security boundary. # -Shell (bash, powershell, unknown) overrides the dialect taken from the hook's tool name. param([string]$Command, [string]$Workspace = (Get-Location).Path, [ValidateSet('', 'bash', 'powershell', 'unknown')][string]$Shell = '') . (Join-Path $PSScriptRoot 'Resolve-Python.ps1') $python = Get-PythonCommand $script = Join-Path $PSScriptRoot 'command-safety.py' if ($PSBoundParameters.ContainsKey('Command')) { if (-not $python) { Write-Error (Get-PythonMissingMessage); exit 1 } # Pass the command on stdin: Windows PowerShell 5.1 does not escape embedded quotes in native arguments. $Command | & $python.Path @($python.Arguments) $script --command - --workspace $Workspace exit $LASTEXITCODE } # Registered hooks fail open without Python; doctor reports that case. if (-not $python) { exit 0 } $shellArguments = if ($Shell) { @('--shell', $Shell) } else { @() } & $python.Path @($python.Arguments) $script --hook @shellArguments exit $LASTEXITCODE