|
| 1 | +$ErrorActionPreference = "Stop" |
| 2 | + |
| 3 | +function Write-Decision { |
| 4 | + param( |
| 5 | + [Parameter(Mandatory = $true)] |
| 6 | + [ValidateSet("allow", "deny", "ask", "force_ask")] |
| 7 | + [string]$Decision, |
| 8 | + |
| 9 | + [Parameter(Mandatory = $true)] |
| 10 | + [string]$Reason |
| 11 | + ) |
| 12 | + |
| 13 | + [ordered]@{ |
| 14 | + decision = $Decision |
| 15 | + reason = $Reason |
| 16 | + } | ConvertTo-Json -Compress |
| 17 | +} |
| 18 | + |
| 19 | +function Get-ArgumentPath { |
| 20 | + param( |
| 21 | + [object]$Arguments, |
| 22 | + [string[]]$Names |
| 23 | + ) |
| 24 | + |
| 25 | + foreach ($name in $Names) { |
| 26 | + $property = $Arguments.PSObject.Properties[$name] |
| 27 | + if ($null -ne $property -and |
| 28 | + -not [string]::IsNullOrWhiteSpace([string]$property.Value)) { |
| 29 | + return [string]$property.Value |
| 30 | + } |
| 31 | + } |
| 32 | + return $null |
| 33 | +} |
| 34 | + |
| 35 | +function Test-PathWithinRoots { |
| 36 | + param( |
| 37 | + [string]$Candidate, |
| 38 | + [string[]]$Roots, |
| 39 | + [string]$BaseRoot |
| 40 | + ) |
| 41 | + |
| 42 | + if ([string]::IsNullOrWhiteSpace($Candidate) -or $Roots.Count -eq 0) { |
| 43 | + return $false |
| 44 | + } |
| 45 | + if ([string]::IsNullOrWhiteSpace($BaseRoot)) { |
| 46 | + $BaseRoot = [string]$Roots[0] |
| 47 | + } |
| 48 | + if ($Candidate.StartsWith("~")) { |
| 49 | + return $false |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + if ([IO.Path]::IsPathRooted($Candidate)) { |
| 54 | + $candidatePath = [IO.Path]::GetFullPath($Candidate) |
| 55 | + } else { |
| 56 | + $candidatePath = [IO.Path]::GetFullPath( |
| 57 | + (Join-Path $BaseRoot $Candidate) |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($root in $Roots) { |
| 62 | + $rootPath = [IO.Path]::GetFullPath([string]$root).TrimEnd( |
| 63 | + [IO.Path]::DirectorySeparatorChar, |
| 64 | + [IO.Path]::AltDirectorySeparatorChar |
| 65 | + ) |
| 66 | + if ($candidatePath.Equals( |
| 67 | + $rootPath, |
| 68 | + [StringComparison]::OrdinalIgnoreCase |
| 69 | + ) -or $candidatePath.StartsWith( |
| 70 | + $rootPath + [IO.Path]::DirectorySeparatorChar, |
| 71 | + [StringComparison]::OrdinalIgnoreCase |
| 72 | + )) { |
| 73 | + return $true |
| 74 | + } |
| 75 | + } |
| 76 | + } catch { |
| 77 | + return $false |
| 78 | + } |
| 79 | + return $false |
| 80 | +} |
| 81 | + |
| 82 | +try { |
| 83 | + $rawInput = [Console]::In.ReadToEnd() |
| 84 | + if ([string]::IsNullOrWhiteSpace($rawInput)) { |
| 85 | + Write-Decision -Decision "deny" -Reason "WorkflowGuard gate received no tool-call data." |
| 86 | + exit 0 |
| 87 | + } |
| 88 | + |
| 89 | + $payload = $rawInput | ConvertFrom-Json |
| 90 | + $toolName = [string]$payload.toolCall.name |
| 91 | + $arguments = $payload.toolCall.args |
| 92 | + $workspacePaths = @( |
| 93 | + $payload.workspacePaths | |
| 94 | + ForEach-Object { [string]$_ } | |
| 95 | + Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
| 96 | + ) |
| 97 | + if ($workspacePaths.Count -eq 0 -and |
| 98 | + -not [string]::IsNullOrWhiteSpace($env:WORKFLOWGUARD_AGY_WORKSPACE)) { |
| 99 | + try { |
| 100 | + $fallbackRoot = [IO.Path]::GetFullPath( |
| 101 | + $env:WORKFLOWGUARD_AGY_WORKSPACE |
| 102 | + ) |
| 103 | + $requiredMarkers = @( |
| 104 | + (Join-Path $fallbackRoot "agy\engagements\active.json"), |
| 105 | + (Join-Path $fallbackRoot ".agents\plugins\workflowguard-qa\plugin.json") |
| 106 | + ) |
| 107 | + if ([IO.Path]::IsPathRooted($env:WORKFLOWGUARD_AGY_WORKSPACE) -and |
| 108 | + (Test-Path -LiteralPath $fallbackRoot -PathType Container) -and |
| 109 | + @($requiredMarkers | Where-Object { |
| 110 | + -not (Test-Path -LiteralPath $_ -PathType Leaf) |
| 111 | + }).Count -eq 0) { |
| 112 | + $workspacePaths = @($fallbackRoot) |
| 113 | + } |
| 114 | + } catch { |
| 115 | + $workspacePaths = @() |
| 116 | + } |
| 117 | + } |
| 118 | + if ([string]::IsNullOrWhiteSpace($toolName)) { |
| 119 | + Write-Decision -Decision "deny" -Reason "WorkflowGuard gate could not identify the proposed tool." |
| 120 | + exit 0 |
| 121 | + } |
| 122 | + |
| 123 | + if ($workspacePaths.Count -eq 0) { |
| 124 | + Write-Decision -Decision "deny" -Reason "WorkflowGuard gate received no workspace boundary." |
| 125 | + exit 0 |
| 126 | + } |
| 127 | + |
| 128 | + if ($toolName -eq "list_permissions") { |
| 129 | + Write-Decision -Decision "allow" -Reason "Reading the current permission state is allowed." |
| 130 | + exit 0 |
| 131 | + } |
| 132 | + |
| 133 | + $readOnlyTools = @( |
| 134 | + "view_file", |
| 135 | + "list_dir", |
| 136 | + "find_by_name", |
| 137 | + "grep_search" |
| 138 | + ) |
| 139 | + if ($readOnlyTools -contains $toolName) { |
| 140 | + $readRoots = @($workspacePaths) |
| 141 | + $installedPluginRoot = Join-Path ` |
| 142 | + $env:USERPROFILE ` |
| 143 | + ".gemini\config\plugins\workflowguard-qa" |
| 144 | + if (Test-Path -LiteralPath $installedPluginRoot -PathType Container) { |
| 145 | + $readRoots += $installedPluginRoot |
| 146 | + } |
| 147 | + $readPath = Get-ArgumentPath -Arguments $arguments -Names @( |
| 148 | + "AbsolutePath", |
| 149 | + "DirectoryPath", |
| 150 | + "SearchDirectory", |
| 151 | + "SearchPath", |
| 152 | + "Path" |
| 153 | + ) |
| 154 | + if (-not (Test-PathWithinRoots -Candidate $readPath -Roots $readRoots -BaseRoot ([string]$workspacePaths[0]))) { |
| 155 | + Write-Decision -Decision "deny" -Reason "Read-only inspection is restricted to the current WorkflowGuard workspace and installed QA plugin." |
| 156 | + exit 0 |
| 157 | + } |
| 158 | + Write-Decision -Decision "allow" -Reason "Read-only inspection inside the workspace is allowed." |
| 159 | + exit 0 |
| 160 | + } |
| 161 | + |
| 162 | + $writeTools = @( |
| 163 | + "write_to_file", |
| 164 | + "replace_file_content", |
| 165 | + "multi_replace_file_content" |
| 166 | + ) |
| 167 | + if ($writeTools -contains $toolName) { |
| 168 | + $writePath = Get-ArgumentPath -Arguments $arguments -Names @( |
| 169 | + "TargetFile", |
| 170 | + "AbsolutePath", |
| 171 | + "Path" |
| 172 | + ) |
| 173 | + $outputRoot = Join-Path ([string]$workspacePaths[0]) "agy\output" |
| 174 | + if (-not (Test-PathWithinRoots -Candidate $writePath -Roots @($outputRoot) -BaseRoot ([string]$workspacePaths[0]))) { |
| 175 | + Write-Decision -Decision "deny" -Reason "The QA agent may write only sanitized evidence under agy/output." |
| 176 | + exit 0 |
| 177 | + } |
| 178 | + Write-Decision -Decision "force_ask" -Reason "Writing sanitized evidence requires explicit human review." |
| 179 | + exit 0 |
| 180 | + } |
| 181 | + |
| 182 | + if ($toolName -eq "run_command") { |
| 183 | + $commandLine = [string]$arguments.CommandLine |
| 184 | + $workingDirectory = Get-ArgumentPath -Arguments $arguments -Names @( |
| 185 | + "Cwd", |
| 186 | + "WorkingDirectory", |
| 187 | + "DirectoryPath" |
| 188 | + ) |
| 189 | + if (-not [string]::IsNullOrWhiteSpace($workingDirectory) -and |
| 190 | + -not (Test-PathWithinRoots -Candidate $workingDirectory -Roots $workspacePaths -BaseRoot ([string]$workspacePaths[0]))) { |
| 191 | + Write-Decision -Decision "deny" -Reason "Commands may run only inside the current workspace." |
| 192 | + exit 0 |
| 193 | + } |
| 194 | + $allowedPatterns = @( |
| 195 | + '^\s*(\.\\)?gradlew\.bat\s+(clean\s+)?test(\s+jar)?\s*$', |
| 196 | + '^\s*git\s+status(\s+(--short|--porcelain(=v1)?|--branch)){0,2}\s*$', |
| 197 | + '^\s*git\s+diff(\s+(--check|--stat|--cached))?\s*$', |
| 198 | + '^\s*powershell(\.exe)?\s+-NoProfile\s+-ExecutionPolicy\s+Bypass\s+-File\s+["'']?scripts[\\/]agy[\\/]Validate-WorkflowGuardEngagement\.ps1["'']?\s+-Manifest\s+["'']?agy[\\/]engagements[\\/](active|engagement\.example)\.json["'']?(\s+-RequireActive)?\s*$', |
| 199 | + '^\s*powershell(\.exe)?\s+-NoProfile\s+-ExecutionPolicy\s+Bypass\s+-File\s+["'']?scripts[\\/]agy[\\/]Test-WorkflowGuardAgyConfiguration\.ps1["'']?\s*$' |
| 200 | + ) |
| 201 | + foreach ($pattern in $allowedPatterns) { |
| 202 | + if ($commandLine -match $pattern) { |
| 203 | + Write-Decision -Decision "force_ask" -Reason "The command is locally allowlisted but still requires explicit approval." |
| 204 | + exit 0 |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + Write-Decision -Decision "deny" -Reason "Arbitrary shell commands and network clients are disabled for the WorkflowGuard QA agent." |
| 209 | + exit 0 |
| 210 | + } |
| 211 | + |
| 212 | + $interactiveTools = @("ask_question") |
| 213 | + if ($interactiveTools -contains $toolName) { |
| 214 | + Write-Decision -Decision "allow" -Reason "Human clarification is allowed." |
| 215 | + exit 0 |
| 216 | + } |
| 217 | + |
| 218 | + $blockedPatterns = @( |
| 219 | + '^browser_', |
| 220 | + '^mcp', |
| 221 | + '^search_web$', |
| 222 | + '^read_url_content$', |
| 223 | + '^invoke_subagent$', |
| 224 | + '^define_subagent$', |
| 225 | + '^send_message$', |
| 226 | + '^manage_subagents$', |
| 227 | + '^schedule$', |
| 228 | + '^manage_task$', |
| 229 | + '^ask_permission$', |
| 230 | + '^generate_image$' |
| 231 | + ) |
| 232 | + foreach ($pattern in $blockedPatterns) { |
| 233 | + if ($toolName -match $pattern) { |
| 234 | + Write-Decision -Decision "deny" -Reason "Network, delegation, scheduling, MCP, and permission-escalation tools are disabled." |
| 235 | + exit 0 |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + Write-Decision -Decision "deny" -Reason "Unknown tools are denied by default." |
| 240 | +} catch { |
| 241 | + Write-Decision -Decision "deny" -Reason ("WorkflowGuard gate failed closed: " + $_.Exception.Message) |
| 242 | +} |
0 commit comments