diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index add5a1da8..8d3922fb2 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -304,7 +304,7 @@ func commandUsesNetwork(prog string, args []*syntax.Word) bool { case "git": return gitUsesNetwork(words) case "gh": - return ghUsesNetwork(words) + return ghUsesNetwork(args) default: return false } @@ -570,14 +570,25 @@ func pythonModuleRunsLocalServer(words []string) bool { return false } -func ghUsesNetwork(words []string) bool { - first := firstSubcommand(words, nil) - if first == "api" { +func ghUsesNetwork(args []*syntax.Word) bool { + if len(args) == 0 { + return false + } + if !isLiteralWord(args[0]) { + return true + } + switch wordText(args[0]) { + case "help", "version", "completion", "config", "alias": + return false + case "extension", "ext", "extensions": + if len(args) < 2 || !isLiteralWord(args[1]) { + return true + } + sub := wordText(args[1]) + return sub != "list" && sub != "ls" + default: return true } - second := secondSubcommand(words) - return (first == "release" && second == "download") || - (first == "repo" && second == "clone") } func secondSubcommand(words []string) string { diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index fbafe3c3a..fa536fd96 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -100,6 +100,43 @@ func TestAnalyzeCommand(t *testing.T) { {name: "git fetch", script: "git fetch origin", network: true}, {name: "git status is offline", script: "git status", network: false}, {name: "gh release download", script: "gh release download v1.0.0", network: true}, + {name: "gh release download with repo flag between", script: "gh release -R owner/repo download v1", network: true}, + {name: "gh release download with repo flag in front", script: "gh -R owner/repo release download v1", network: true}, + {name: "gh pr list", script: "gh pr list", network: true}, + {name: "gh pr list --help is network", script: "gh pr list --help", network: true}, + {name: "gh issue view", script: "gh issue view 5", network: true}, + {name: "gh run watch", script: "gh run watch", network: true}, + {name: "gh repo clone", script: "gh repo clone owner/repo", network: true}, + {name: "gh repo view", script: "gh repo view", network: true}, + {name: "gh api", script: "gh api /user", network: true}, + {name: "gh api with expansion before --help", script: `gh api "${BASE}--help"`, network: true}, + {name: "gh api with padded --help", script: "gh api ' --help'", network: true}, + {name: "gh gist create --desc --help", script: "gh gist create --desc --help secret.txt", network: true}, + {name: "gh pr comment --body-file --help", script: "gh pr comment 5 --body-file --help", network: true}, + {name: "gh issue create --label --help", script: "gh issue create --label --help --title t", network: true}, + {name: "gh pr create --assignee --help", script: "gh pr create --assignee --help", network: true}, + {name: "gh secret set --body --help", script: "gh secret set NAME --body --help", network: true}, + {name: "gh myext --help", script: "gh myext --help", network: true}, + {name: "gh with expansion before config", script: "gh $X config get y", network: true}, + {name: "gh with expansion inside help", script: `gh "${X}help"`, network: true}, + {name: "gh with padded help", script: "gh ' help'", network: true}, + {name: "gh extension with expansion inside list", script: `gh extension "${X}list"`, network: true}, + {name: "gh extension with padded list", script: "gh extension ' list'", network: true}, + {name: "gh with option before config", script: "gh -R owner/repo config get git_protocol", network: true}, + {name: "gh auth status", script: "gh auth status", network: true}, + {name: "gh extension install", script: "gh extension install owner/gh-ext", network: true}, + {name: "gh extension upgrade", script: "gh extension upgrade --all", network: true}, + {name: "gh extension exec forwarded --help", script: "gh extension exec myext --help", network: true}, + {name: "gh bare is offline", script: "gh", network: false}, + {name: "gh help is offline", script: "gh help", network: false}, + {name: "gh version is offline", script: "gh version", network: false}, + {name: "gh completion is offline", script: "gh completion -s bash", network: false}, + {name: "gh config get is offline", script: "gh config get git_protocol", network: false}, + {name: "gh alias list is offline", script: "gh alias list", network: false}, + {name: "gh extension list is offline", script: "gh extension list", network: false}, + {name: "gh ext list is offline", script: "gh ext list", network: false}, + {name: "gh extensions list is offline", script: "gh extensions list", network: false}, + {name: "gh extension ls is offline", script: "gh extension ls", network: false}, {name: "no network", script: "ls -la && echo done", network: false}, {name: "process pattern is not network", script: `pkill -f "python3 -m http.server 8000"`, network: false}, {name: "process listing is not special-cased", script: "ps aux", network: false}, diff --git a/internal/sandbox/risk.go b/internal/sandbox/risk.go index 751d3f0bd..466f1c527 100644 --- a/internal/sandbox/risk.go +++ b/internal/sandbox/risk.go @@ -114,7 +114,7 @@ func unparseableNetworkPatternSource() string { serving := `\b(npm|pnpm|yarn|bun)` + leadingOptionsPattern + `\s+(run|dev|serve|start|preview)\b` return `(?i)\b(curl|wget|fetch|aria2c|ssh|scp|sftp|rsync|nc|ncat|netcat|telnet|ftp|npx)\b|` + fetch + `|` + serving + - `|\b(http-server|serve|vite|next|nuxt|astro)\b|\b(python(2|3)?|py)\s+-m\s+http\.server\b|\bgo\s+get\b|\bgit\s+clone\b|\b(python(2|3)?|py)\s+-m\s+pip\s+install\b|\bgh\s+(api|repo\s+clone|release\s+download)\b` + `|\b(http-server|serve|vite|next|nuxt|astro)\b|\b(python(2|3)?|py)\s+-m\s+http\.server\b|\bgo\s+get\b|\bgit\s+clone\b|\b(python(2|3)?|py)\s+-m\s+pip\s+install\b|\bgh(\.exe)?\b` } func matchesDestructive(command string) bool { diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index ce2f89994..7d90fc65d 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -327,3 +327,37 @@ func TestClassifyBenignCommandStaysClean(t *testing.T) { } } } + +func TestClassifyUnparseableGHCommands(t *testing.T) { + cases := []struct { + name string + command string + network bool + }{ + {name: "unparseable gh pr list", command: `if %ERRORLEVEL% == 0 gh pr list`, network: true}, + {name: "unparseable gh issue view", command: `if %ERRORLEVEL% == 0 gh issue view 5`, network: true}, + {name: "unparseable gh run watch", command: `if %ERRORLEVEL% == 0 gh run watch`, network: true}, + {name: "unparseable quoted gh.exe path", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" pr list`, network: true}, + {name: "unparseable cmd /c quoted gh api", command: `if %ERRORLEVEL% == 0 cmd /c "gh api /user"`, network: true}, + {name: "unparseable powershell quoted gh api", command: `if %ERRORLEVEL% == 0 powershell -Command "gh api /user"`, network: true}, + {name: "unparseable pwsh quoted gh release download", command: `if %ERRORLEVEL% == 0 pwsh -c "gh release download v1"`, network: true}, + {name: "unparseable echo backtick gh api", command: "if %ERRORLEVEL% == 0 echo `gh api /user`", network: true}, + {name: "unparseable @gh api", command: `@gh api /user & if 1==1 echo done`, network: true}, + {name: "unparseable if defined bash gh api", command: `if defined bash gh api /user`, network: true}, + {name: "unparseable for /f gh api", command: `if %ERRORLEVEL% == 0 for /f %i in ('gh api /user') do echo %i`, network: true}, + {name: "unparseable FOR /F gh release download", command: `if %ERRORLEVEL% == 0 FOR /F "tokens=*" %i IN ('gh release download v1') DO echo %i`, network: true}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + risk := classifyCommand(tc.command) + if !HasRiskCategory(risk, "unparseable_command") { + t.Fatalf("Classify(%q) = categories %v, want unparseable_command", tc.command, risk.Categories) + } + hasNet := HasRiskCategory(risk, "network") + if hasNet != tc.network { + t.Fatalf("Classify(%q) network = %v, want %v (risk = %s, categories %v)", + tc.command, hasNet, tc.network, risk.Level, risk.Categories) + } + }) + } +}