From ddda2b403e4b686579cb2a9fb173e8072aaa2c68 Mon Sep 17 00:00:00 2001 From: Yshuro Date: Tue, 24 Nov 2026 16:19:28 +0100 Subject: [PATCH 1/7] fix(sandbox): classify gh subcommands as network by default (#1237) Treat gh subcommands as network access by default unless they are on an explicit local list (help, version, completion, config, alias, and extension list). Account for value-taking options in front of and between subcommand words so options like -R/--repo or --host do not shift subcommand matching. Synchronize unparseable command handling in risk.go to evaluate gh commands through the same classifier logic. Signed-off-by: Yshuro --- internal/sandbox/analyzer.go | 107 +++++++++++++++++++++-- internal/sandbox/analyzer_test.go | 36 ++++++++ internal/sandbox/risk.go | 111 ++++++++++++++++++------ internal/sandbox/risk_hardening_test.go | 41 +++++++++ 4 files changed, 262 insertions(+), 33 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index add5a1da8c..4036946ac9 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -570,14 +570,111 @@ func pythonModuleRunsLocalServer(words []string) bool { return false } +var ghValueOptions = map[string]bool{ + "-r": true, + "--repo": true, + "--host": true, + "--hostname": true, + "-s": true, + "--shell": true, + "-t": true, + "--template": true, + "-q": true, + "--jq": true, + "-l": true, + "--limit": true, + "-x": true, + "--method": true, + "-f": true, + "--field": true, + "--raw-field": true, + "-p": true, + "--pattern": true, + "-d": true, + "--dir": true, + "-o": true, + "--output": true, + "--owner": true, + "-a": true, + "--archive": true, + "--custom-agent": true, + "-b": true, + "--base": true, + "--ref": true, + "-e": true, + "--env": true, + "-i": true, + "--input": true, + "--cache": true, +} + +func ghSubcommands(words []string) (string, string) { + var subs []string + for i := 0; i < len(words); i++ { + w := words[i] + if w == "" { + continue + } + if strings.HasPrefix(w, "-") { + if !strings.Contains(w, "=") && ghValueOptions[w] && i+1 < len(words) { + i++ + } + continue + } + subs = append(subs, w) + if len(subs) == 2 { + break + } + } + var first, second string + if len(subs) > 0 { + first = subs[0] + } + if len(subs) > 1 { + second = subs[1] + } + return first, second +} + +func ghHasHelpOrVersionFlag(words []string) bool { + for i := 0; i < len(words); i++ { + w := words[i] + if w == "" { + continue + } + if w == "--" { + break + } + if strings.HasPrefix(w, "-") { + if !strings.Contains(w, "=") && ghValueOptions[w] && i+1 < len(words) { + i++ + continue + } + switch w { + case "-h", "--help", "--version": + return true + } + } + } + return false +} + func ghUsesNetwork(words []string) bool { - first := firstSubcommand(words, nil) - if first == "api" { + if ghHasHelpOrVersionFlag(words) { + return false + } + first, second := ghSubcommands(words) + if first == "" { + return false + } + switch first { + case "help", "version", "completion", "config", "alias": + return false + case "extension": + return second != "" && second != "list" + 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 fbafe3c3aa..e82a49e85a 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -100,6 +100,42 @@ 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 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 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 bare is offline", script: "gh", network: false}, + {name: "gh help is offline", script: "gh help", network: false}, + {name: "gh help topic is offline", script: "gh help pr", network: false}, + {name: "gh version is offline", script: "gh version", network: false}, + {name: "gh help flag is offline", script: "gh --help", network: false}, + {name: "gh version flag is offline", script: "gh --version", network: false}, + {name: "gh subcommand help flag after is offline", script: "gh pr list --help", network: false}, + {name: "gh subcommand help flag before is offline", script: "gh --help pr list", network: false}, + {name: "gh subcommand short help flag is offline", script: "gh pr -h", network: false}, + {name: "gh subcommand help flag with repo flag is offline", script: "gh -R owner/repo pr list --help", network: false}, + {name: "gh subcommand version flag is offline", script: "gh pr list --version", network: false}, + {name: "gh repo value named --help stays network", script: "gh -R --help pr list", network: true}, + {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 config set is offline", script: "gh config set editor vim", network: false}, + {name: "gh config with host flag is offline", script: "gh config --host github.com get editor", network: false}, + {name: "gh config with repo flag in front is offline", script: "gh -R owner/repo config get git_protocol", network: false}, + {name: "gh alias list is offline", script: "gh alias list", network: false}, + {name: "gh alias set is offline", script: "gh alias set pv 'pr view'", network: false}, + {name: "gh alias with repo flag in front is offline", script: "gh -R owner/repo alias list", network: false}, + {name: "gh alias with repo flag between is offline", script: "gh alias -R owner/repo list", network: false}, + {name: "gh extension bare is offline", script: "gh extension", network: false}, + {name: "gh extension list is offline", script: "gh extension list", network: false}, + {name: "gh extension list with repo flag between is offline", script: "gh extension -R owner/repo list", network: false}, + {name: "gh extension list with repo flag in front is offline", script: "gh -R owner/repo extension list", 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 751d3f0bd2..b2c808b7b2 100644 --- a/internal/sandbox/risk.go +++ b/internal/sandbox/risk.go @@ -50,32 +50,6 @@ var ( // unparseableNetworkPattern is used only after the shell parser fails. At // that point the command is already marked too complex, so this intentionally // favors catching obvious network programs over proving exact shell syntax. - // - // IT MUST BE A SUPERSET OF WHAT THE AST PATH FLAGS FOR NETWORK, not a mirror - // of the analyzer's category names. The serving forms were dropped from here - // on the reasoning that they bind rather than fetch, but the analyzer sets - // Network for every recognized LocalServer anyway, deliberately and for two - // reasons it documents: nothing consumes LocalServer yet, so the approval IS - // the protection, and `npm run dev` is matched by SCRIPT NAME, where the - // repository decides what `dev` and `predev` actually do and either can reach - // out before a port is bound. - // - // So dropping them here did not align the two paths, it split them. The POSIX - // parser rejects Windows shell syntax the invoked shell accepts, and the - // analyzer names that as a fallback case, so: - // - // if "%OS%"=="Windows_NT" (npm run dev) else (npm start) - // -> parsed=false network=false categories=[shell unparseable_command] - // - // while every parseable spelling of the same command gets network. That is - // the approval gate disappearing on exactly the platform where it is the only - // egress control. Restored until a runner actually provides a scoped listener - // capability and the AST path stops setting Network for these itself. - // - // The fetch alternative is compiled from packageManagerNetworkSubcommands - // rather than a second list. The shorter list omitted ci, create, view, and - // search, so `npm --prefix ./web ci` kept network when it parsed and lost - // it inside a Windows conditional. unparseableNetworkPattern *regexp.Regexp // destructiveExtraPatterns hold high-severity patterns that the legacy // destructiveCommandPattern does not already cover. Folded in from the @@ -114,7 +88,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` } func matchesDestructive(command string) bool { @@ -129,8 +103,89 @@ func matchesDestructive(command string) bool { return false } +func isGHExecutableToken(token string) bool { + if idx := strings.LastIndexAny(token, `/\`); idx >= 0 { + token = token[idx+1:] + } + return token == "gh" || token == "gh.exe" +} + +// extractGHInvocations tokenizes an unparseable command string while preserving +// quoted executable paths and option values, splitting on unquoted shell/cmd +// separators (&, |, ;, (, ), CR, LF), and returns the lowercased argument +// slices for each gh/gh.exe invocation found. +func extractGHInvocations(command string) [][]string { + type tok struct { + val string + sep bool + } + var tokens []tok + var cur strings.Builder + hasCur := false + var quote byte + + flush := func() { + if hasCur { + tokens = append(tokens, tok{val: strings.ToLower(cur.String())}) + cur.Reset() + hasCur = false + } + } + + for i := 0; i < len(command); i++ { + c := command[i] + if quote != 0 { + if c == quote { + quote = 0 + } else { + cur.WriteByte(c) + } + continue + } + switch c { + case '"', '\'': + quote = c + hasCur = true + case ' ', '\t': + flush() + case '&', '|', ';', '(', ')', '\r', '\n': + flush() + tokens = append(tokens, tok{sep: true}) + default: + cur.WriteByte(c) + hasCur = true + } + } + flush() + + var invocations [][]string + for i := 0; i < len(tokens); i++ { + if tokens[i].sep || !isGHExecutableToken(tokens[i].val) { + continue + } + var args []string + for j := i + 1; j < len(tokens) && !tokens[j].sep; j++ { + if tokens[j].val != "" { + args = append(args, tokens[j].val) + } + i = j + } + invocations = append(invocations, args) + } + return invocations +} + +func unparseableGHUsesNetwork(command string) bool { + for _, words := range extractGHInvocations(command) { + if ghUsesNetwork(words) { + return true + } + } + return false +} + func matchesUnparseableNetwork(command string) bool { - return unparseableNetworkPattern.MatchString(command) + return unparseableNetworkPattern.MatchString(command) || unparseableGHUsesNetwork(command) } func Classify(request Request) Risk { diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index ce2f89994e..e23c0d6a81 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -327,3 +327,44 @@ 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 gh release download with flag", command: `if %ERRORLEVEL% == 0 gh release -R owner/repo download v1`, network: true}, + {name: "unparseable gh with flag in front", command: `if %ERRORLEVEL% == 0 gh -R owner/repo pr list`, network: true}, + {name: "unparseable gh with quoted repo flag", command: `if %ERRORLEVEL% == 0 gh -R "owner/repo" pr list`, 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 quoted gh.exe path with quoted repo flag", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" -R "owner/repo" pr list`, network: true}, + {name: "unparseable gh extension install", command: `if %ERRORLEVEL% == 0 gh extension install owner/ext`, network: true}, + {name: "unparseable gh pr list unterminated quote", command: `gh pr list && "unterminated`, network: true}, + {name: "unparseable gh --help", command: `if %ERRORLEVEL% == 0 gh --help`, network: false}, + {name: "unparseable gh pr list --help", command: `if %ERRORLEVEL% == 0 gh pr list --help`, network: false}, + {name: "unparseable gh --help pr list", command: `if %ERRORLEVEL% == 0 gh --help pr list`, network: false}, + {name: "unparseable gh help", command: `if %ERRORLEVEL% == 0 gh help`, network: false}, + {name: "unparseable gh config", command: `if %ERRORLEVEL% == 0 gh config get git_protocol`, network: false}, + {name: "unparseable gh alias list", command: `if %ERRORLEVEL% == 0 gh alias list`, network: false}, + {name: "unparseable gh alias list with flag", command: `if %ERRORLEVEL% == 0 gh -R owner/repo alias list`, network: false}, + {name: "unparseable gh extension list", command: `if %ERRORLEVEL% == 0 gh extension list`, network: false}, + {name: "unparseable gh extension list with flag", command: `if %ERRORLEVEL% == 0 gh extension -R owner/repo list`, network: false}, + } + 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) + } + }) + } +} From f78520eaa452f8d7971e5409f5c08c3cea721601 Mon Sep 17 00:00:00 2001 From: Yshuro Date: Wed, 25 Nov 2026 21:02:26 +0100 Subject: [PATCH 2/7] fix(sandbox): address review feedback on gh flag casing, shell wrappers, and redirections --- internal/sandbox/analyzer.go | 83 ++++++++++---- internal/sandbox/analyzer_test.go | 3 + internal/sandbox/risk.go | 145 ++++++++++++++++++++++-- internal/sandbox/risk_hardening_test.go | 6 + 4 files changed, 203 insertions(+), 34 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index 4036946ac9..0f5ad0f335 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -269,40 +269,40 @@ func commandUsesNetwork(prog string, args []*syntax.Word) bool { return true } if pythonLauncherPrograms[prog] { - return pythonModuleUsesNetwork(words) + return pythonModuleUsesNetwork(lowerWords(words)) } switch prog { case "npm": - return packageManagerUsesNetwork(words, map[string]string{ + return packageManagerUsesNetwork(lowerWords(words), map[string]string{ "run": "run", "exec": "exec", "x": "exec", }) case "pnpm": - return packageManagerUsesNetwork(words, map[string]string{ + return packageManagerUsesNetwork(lowerWords(words), map[string]string{ "run": "run", "exec": "exec", "dlx": "exec", }) case "yarn": - return packageManagerUsesNetwork(words, map[string]string{ + return packageManagerUsesNetwork(lowerWords(words), map[string]string{ "run": "run", "exec": "exec", "dlx": "exec", }) case "bun": - return packageManagerUsesNetwork(words, map[string]string{ + return packageManagerUsesNetwork(lowerWords(words), map[string]string{ "run": "run", "x": "exec", }) case "npx": - return npxUsesNetwork(words) + return npxUsesNetwork(lowerWords(words)) case "pip", "pip2", "pip3": - return firstSubcommand(words, nil) == "install" + return firstSubcommand(lowerWords(words), nil) == "install" case "go": - return firstSubcommand(words, nil) == "get" + return firstSubcommand(lowerWords(words), nil) == "get" case "git": - return gitUsesNetwork(words) + return gitUsesNetwork(lowerWords(words)) case "gh": return ghUsesNetwork(words) default: @@ -340,7 +340,7 @@ func packageManagerUsesNetwork(words []string, aliases map[string]string) bool { } operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - subcommand := operands[index] + subcommand := strings.ToLower(operands[index]) if alias, ok := aliases[subcommand]; ok { subcommand = alias } @@ -377,11 +377,19 @@ func npxUsesNetwork(_ []string) bool { func literalWordTexts(args []*syntax.Word) []string { words := make([]string, 0, len(args)) for _, arg := range args { - words = append(words, strings.ToLower(strings.TrimSpace(wordText(arg)))) + words = append(words, strings.TrimSpace(wordText(arg))) } return words } +func lowerWords(words []string) []string { + lowered := make([]string, len(words)) + for i, w := range words { + lowered[i] = strings.ToLower(w) + } + return lowered +} + func pythonModuleUsesNetwork(words []string) bool { for index := 0; index < len(words); index++ { if words[index] != "-m" || index+1 >= len(words) { @@ -408,7 +416,7 @@ func pythonModuleUsesNetwork(words []string) bool { // is not EGRESS, not that dev tooling is inert. Anything that actually fetches // still matches commandUsesNetwork through its own program or subcommand. func commandRunsLocalServer(prog string, args []*syntax.Word) bool { - words := literalWordTexts(args) + words := lowerWords(literalWordTexts(args)) if localServerPrograms[prog] { return frameworkSubcommandRunsLocalServer(prog, words) } @@ -446,7 +454,7 @@ func frameworkSubcommandRunsLocalServer(prog string, words []string) bool { // the same way the package managers do; see subcommandPositions. operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - switch operands[index] { + switch strings.ToLower(operands[index]) { case "dev", "start", "serve", "preview": return true case "build", "optimize", "generate", "check", "lint", "export": @@ -467,10 +475,11 @@ func frameworkSubcommandRunsLocalServer(prog string, words []string) bool { func packageManagerRunsLocalServer(words []string) bool { operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - if packageManagerServingScripts[operands[index]] { + op := strings.ToLower(operands[index]) + if packageManagerServingScripts[op] { return true } - if operands[index] == "run" && index+1 < len(operands) && packageManagerServingScripts[operands[index+1]] { + if op == "run" && index+1 < len(operands) && packageManagerServingScripts[strings.ToLower(operands[index+1])] { return true } } @@ -571,7 +580,7 @@ func pythonModuleRunsLocalServer(words []string) bool { } var ghValueOptions = map[string]bool{ - "-r": true, + "-R": true, "--repo": true, "--host": true, "--hostname": true, @@ -581,13 +590,16 @@ var ghValueOptions = map[string]bool{ "--template": true, "-q": true, "--jq": true, - "-l": true, + "-L": true, "--limit": true, - "-x": true, + "-X": true, "--method": true, "-f": true, + "-F": true, "--field": true, "--raw-field": true, + "-H": true, + "--header": true, "-p": true, "--pattern": true, "-d": true, @@ -608,6 +620,16 @@ var ghValueOptions = map[string]bool{ "--cache": true, } +func ghOptionTakesValue(flag string, subcommand string) bool { + if strings.HasPrefix(flag, "--") { + flag = strings.ToLower(flag) + } + if ghValueOptions[flag] { + return true + } + return subcommand == "auth" && flag == "-h" +} + func ghSubcommands(words []string) (string, string) { var subs []string for i := 0; i < len(words); i++ { @@ -615,13 +637,20 @@ func ghSubcommands(words []string) (string, string) { if w == "" { continue } + if w == "--" { + break + } if strings.HasPrefix(w, "-") { - if !strings.Contains(w, "=") && ghValueOptions[w] && i+1 < len(words) { + first := "" + if len(subs) > 0 { + first = subs[0] + } + if !strings.Contains(w, "=") && ghOptionTakesValue(w, first) && i+1 < len(words) { i++ } continue } - subs = append(subs, w) + subs = append(subs, strings.ToLower(w)) if len(subs) == 2 { break } @@ -636,7 +665,7 @@ func ghSubcommands(words []string) (string, string) { return first, second } -func ghHasHelpOrVersionFlag(words []string) bool { +func ghHasHelpOrVersionFlag(words []string, subcommand string) bool { for i := 0; i < len(words); i++ { w := words[i] if w == "" { @@ -646,13 +675,17 @@ func ghHasHelpOrVersionFlag(words []string) bool { break } if strings.HasPrefix(w, "-") { - if !strings.Contains(w, "=") && ghValueOptions[w] && i+1 < len(words) { + if !strings.Contains(w, "=") && ghOptionTakesValue(w, subcommand) && i+1 < len(words) { i++ continue } switch w { - case "-h", "--help", "--version": + case "--help", "--version": return true + case "-h": + if subcommand != "auth" { + return true + } } } } @@ -660,10 +693,10 @@ func ghHasHelpOrVersionFlag(words []string) bool { } func ghUsesNetwork(words []string) bool { - if ghHasHelpOrVersionFlag(words) { + first, second := ghSubcommands(words) + if ghHasHelpOrVersionFlag(words, first) { return false } - first, second := ghSubcommands(words) if first == "" { return false } diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index e82a49e85a..ecf8581f46 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -108,7 +108,10 @@ func TestAnalyzeCommand(t *testing.T) { {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 header flag -H stays network", script: `gh api -H "Accept: application/vnd.github+json" /user`, network: true}, {name: "gh auth status", script: "gh auth status", network: true}, + {name: "gh auth status with hostname flag -h stays network", script: "gh auth status -h github.com", network: true}, + {name: "gh auth status --help is offline", script: "gh auth status --help", network: false}, {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 bare is offline", script: "gh", network: false}, diff --git a/internal/sandbox/risk.go b/internal/sandbox/risk.go index b2c808b7b2..a2d229b31b 100644 --- a/internal/sandbox/risk.go +++ b/internal/sandbox/risk.go @@ -103,21 +103,53 @@ func matchesDestructive(command string) bool { return false } -func isGHExecutableToken(token string) bool { +func executableBaseName(token string) string { if idx := strings.LastIndexAny(token, `/\`); idx >= 0 { token = token[idx+1:] } - return token == "gh" || token == "gh.exe" + return strings.ToLower(token) +} + +func isGHExecutableToken(token string) bool { + base := executableBaseName(token) + return base == "gh" || base == "gh.exe" +} + +func isShellExecutableToken(token string) bool { + base := strings.TrimSuffix(executableBaseName(token), ".exe") + return shellPrograms[base] +} + +func isFDPrefix(s string) bool { + if s == "" { + return true + } + if s == "&" { + return true + } + for i := 0; i < len(s); i++ { + if s[i] < '0' || s[i] > '9' { + return false + } + } + return true } // extractGHInvocations tokenizes an unparseable command string while preserving -// quoted executable paths and option values, splitting on unquoted shell/cmd -// separators (&, |, ;, (, ), CR, LF), and returns the lowercased argument -// slices for each gh/gh.exe invocation found. +// token case, quoted executable paths, and option values, splitting on unquoted +// shell/cmd separators (&, |, ;, (, ), CR, LF), stripping redirection operators +// and their filename operands, and recursing (up to maxAnalyzerDepth) into +// recognized shell wrappers (sh/bash/zsh/ksh/dash -c ...). func extractGHInvocations(command string) [][]string { + return extractGHInvocationsDepth(command, 0) +} + +func extractGHInvocationsDepth(command string, depth int) [][]string { type tok struct { - val string - sep bool + val string + sep bool + redir bool + consumesNext bool } var tokens []tok var cur strings.Builder @@ -126,7 +158,7 @@ func extractGHInvocations(command string) [][]string { flush := func() { if hasCur { - tokens = append(tokens, tok{val: strings.ToLower(cur.String())}) + tokens = append(tokens, tok{val: cur.String()}) cur.Reset() hasCur = false } @@ -151,6 +183,57 @@ func extractGHInvocations(command string) [][]string { case '&', '|', ';', '(', ')', '\r', '\n': flush() tokens = append(tokens, tok{sep: true}) + case '<', '>': + if hasCur && !isFDPrefix(cur.String()) { + flush() + } + var redir strings.Builder + if hasCur { + redir.WriteString(cur.String()) + cur.Reset() + hasCur = false + } + for i < len(command) && (command[i] == '<' || command[i] == '>') { + redir.WriteByte(command[i]) + i++ + } + if i < len(command) && command[i] == '&' { + redir.WriteByte('&') + i++ + for i < len(command) && command[i] >= '0' && command[i] <= '9' { + redir.WriteByte(command[i]) + i++ + } + i-- + tokens = append(tokens, tok{val: redir.String(), redir: true, consumesNext: false}) + continue + } + hasAttachedTarget := false + for i < len(command) && command[i] != ' ' && command[i] != '\t' && + command[i] != '&' && command[i] != '|' && command[i] != ';' && + command[i] != '(' && command[i] != ')' && command[i] != '\r' && command[i] != '\n' { + hasAttachedTarget = true + if command[i] == '"' || command[i] == '\'' { + q := command[i] + i++ + for i < len(command) && command[i] != q { + redir.WriteByte(command[i]) + i++ + } + if i < len(command) { + i++ + } + continue + } + redir.WriteByte(command[i]) + i++ + } + i-- + tokens = append(tokens, tok{ + val: redir.String(), + redir: true, + consumesNext: !hasAttachedTarget, + }) default: cur.WriteByte(c) hasCur = true @@ -160,11 +243,55 @@ func extractGHInvocations(command string) [][]string { var invocations [][]string for i := 0; i < len(tokens); i++ { - if tokens[i].sep || !isGHExecutableToken(tokens[i].val) { + if tokens[i].sep || tokens[i].redir { + continue + } + if isShellExecutableToken(tokens[i].val) { + end := i + 1 + for end < len(tokens) && !tokens[end].sep { + end++ + } + if depth < maxAnalyzerDepth { + for j := i + 1; j < end; j++ { + if tokens[j].redir { + if tokens[j].consumesNext && j+1 < end && !tokens[j+1].redir { + j++ + } + continue + } + if tokens[j].val == "--" { + break + } + if shellCommandFlag(tokens[j].val) { + k := j + 1 + for k < end && tokens[k].redir { + if tokens[k].consumesNext && k+1 < end && !tokens[k+1].redir { + k++ + } + k++ + } + if k < end && tokens[k].val != "" { + invocations = append(invocations, extractGHInvocationsDepth(tokens[k].val, depth+1)...) + } + break + } + } + } + i = end - 1 + continue + } + if !isGHExecutableToken(tokens[i].val) { continue } var args []string for j := i + 1; j < len(tokens) && !tokens[j].sep; j++ { + if tokens[j].redir { + if tokens[j].consumesNext && j+1 < len(tokens) && !tokens[j+1].sep && !tokens[j+1].redir { + j++ + } + i = j + continue + } if tokens[j].val != "" { args = append(args, tokens[j].val) } diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index e23c0d6a81..882646d57c 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -343,6 +343,12 @@ func TestClassifyUnparseableGHCommands(t *testing.T) { {name: "unparseable quoted gh.exe path", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" pr list`, network: true}, {name: "unparseable quoted gh.exe path with quoted repo flag", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" -R "owner/repo" pr list`, network: true}, {name: "unparseable gh extension install", command: `if %ERRORLEVEL% == 0 gh extension install owner/ext`, network: true}, + {name: "unparseable gh api with uppercase -H header flag", command: `if %ERRORLEVEL% == 0 gh api -H "Accept: application/vnd.github+json" /user`, network: true}, + {name: "unparseable gh auth status with -h hostname", command: `if %ERRORLEVEL% == 0 gh auth status -h github.com`, network: true}, + {name: "unparseable gh auth status --help", command: `if %ERRORLEVEL% == 0 gh auth status --help`, network: false}, + {name: "unparseable bash -c quoted gh api payload", command: `if %ERRORLEVEL% == 0 bash -c "gh api /user"`, network: true}, + {name: "unparseable gh api redirected to --help filename", command: `if %ERRORLEVEL% == 0 gh api /user > --help`, network: true}, + {name: "unparseable gh api --help with redirection", command: `if %ERRORLEVEL% == 0 gh api > out.txt --help`, network: false}, {name: "unparseable gh pr list unterminated quote", command: `gh pr list && "unterminated`, network: true}, {name: "unparseable gh --help", command: `if %ERRORLEVEL% == 0 gh --help`, network: false}, {name: "unparseable gh pr list --help", command: `if %ERRORLEVEL% == 0 gh pr list --help`, network: false}, From 0ca02167273145f0a783be971505a51800e414bd Mon Sep 17 00:00:00 2001 From: Yshuro Date: Thu, 26 Nov 2026 17:04:49 +0100 Subject: [PATCH 3/7] fix(sandbox): treat gh --preview as value-taking option Signed-off-by: Yshuro --- internal/sandbox/analyzer.go | 1 + internal/sandbox/analyzer_test.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index 0f5ad0f335..12c3c4f090 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -602,6 +602,7 @@ var ghValueOptions = map[string]bool{ "--header": true, "-p": true, "--pattern": true, + "--preview": true, "-d": true, "--dir": true, "-o": true, diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index ecf8581f46..40c05a9e26 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -109,6 +109,8 @@ func TestAnalyzeCommand(t *testing.T) { {name: "gh repo view", script: "gh repo view", network: true}, {name: "gh api", script: "gh api /user", network: true}, {name: "gh api with header flag -H stays network", script: `gh api -H "Accept: application/vnd.github+json" /user`, network: true}, + {name: "gh api with preview value --help stays network", script: "gh api /user --preview --help", network: true}, + {name: "gh api --help is offline", script: "gh api --help", network: false}, {name: "gh auth status", script: "gh auth status", network: true}, {name: "gh auth status with hostname flag -h stays network", script: "gh auth status -h github.com", network: true}, {name: "gh auth status --help is offline", script: "gh auth status --help", network: false}, From ed18749d436b0ea83b4741f3c583288a7fbbf937 Mon Sep 17 00:00:00 2001 From: Yshuro Date: Fri, 27 Nov 2026 11:55:57 +0100 Subject: [PATCH 4/7] fix(sandbox): handle gh extension exec boundary and subcommand value flags Signed-off-by: Yshuro --- internal/sandbox/analyzer.go | 37 +++++++++++++++++++++++-- internal/sandbox/analyzer_test.go | 5 ++++ internal/sandbox/risk_hardening_test.go | 3 ++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index 12c3c4f090..30ab50cdea 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -616,11 +616,33 @@ var ghValueOptions = map[string]bool{ "--ref": true, "-e": true, "--env": true, - "-i": true, "--input": true, "--cache": true, } +var ghSubcommandValueOptions = map[string]map[string]bool{ + "auth": { + "-h": true, + }, + "issue": { + "--title": true, + "--body": true, + }, + "pr": { + "--title": true, + "--body": true, + }, + "release": { + "--title": true, + }, + "project": { + "--title": true, + }, + "run": { + "-i": true, + }, +} + func ghOptionTakesValue(flag string, subcommand string) bool { if strings.HasPrefix(flag, "--") { flag = strings.ToLower(flag) @@ -628,7 +650,10 @@ func ghOptionTakesValue(flag string, subcommand string) bool { if ghValueOptions[flag] { return true } - return subcommand == "auth" && flag == "-h" + if opts, ok := ghSubcommandValueOptions[subcommand]; ok { + return opts[flag] + } + return false } func ghSubcommands(words []string) (string, string) { @@ -667,6 +692,7 @@ func ghSubcommands(words []string) (string, string) { } func ghHasHelpOrVersionFlag(words []string, subcommand string) bool { + var subs []string for i := 0; i < len(words); i++ { w := words[i] if w == "" { @@ -688,6 +714,13 @@ func ghHasHelpOrVersionFlag(words []string, subcommand string) bool { return true } } + continue + } + subs = append(subs, strings.ToLower(w)) + // Everything after `gh extension exec` (or `gh ext exec`) is forwarded to + // the extension runner, so `--help`/`--version`/`-h` there are not gh's own. + if len(subs) >= 2 && (subs[0] == "extension" || subs[0] == "ext") && subs[1] == "exec" { + break } } return false diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index 40c05a9e26..e9ae6c2748 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -110,12 +110,17 @@ func TestAnalyzeCommand(t *testing.T) { {name: "gh api", script: "gh api /user", network: true}, {name: "gh api with header flag -H stays network", script: `gh api -H "Accept: application/vnd.github+json" /user`, network: true}, {name: "gh api with preview value --help stays network", script: "gh api /user --preview --help", network: true}, + {name: "gh api with boolean -i and -- before --help endpoint stays network", script: "gh api -i -- --help", network: true}, {name: "gh api --help is offline", script: "gh api --help", network: false}, + {name: "gh issue create with title --help stays network", script: "gh issue create --title --help --body test --repo owner/repo", network: true}, {name: "gh auth status", script: "gh auth status", network: true}, {name: "gh auth status with hostname flag -h stays network", script: "gh auth status -h github.com", network: true}, {name: "gh auth status --help is offline", script: "gh auth status --help", network: false}, {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 stays network", script: "gh extension exec myext --help", network: true}, + {name: "gh extension exec with --help directly after exec stays network", script: "gh extension exec --help", network: true}, + {name: "gh extension --help before exec is offline", script: "gh extension --help exec myext", network: false}, {name: "gh bare is offline", script: "gh", network: false}, {name: "gh help is offline", script: "gh help", network: false}, {name: "gh help topic is offline", script: "gh help pr", network: false}, diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index 882646d57c..34b39cc978 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -343,6 +343,9 @@ func TestClassifyUnparseableGHCommands(t *testing.T) { {name: "unparseable quoted gh.exe path", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" pr list`, network: true}, {name: "unparseable quoted gh.exe path with quoted repo flag", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" -R "owner/repo" pr list`, network: true}, {name: "unparseable gh extension install", command: `if %ERRORLEVEL% == 0 gh extension install owner/ext`, network: true}, + {name: "unparseable gh extension exec forwarded --help", command: `if %ERRORLEVEL% == 0 gh extension exec myext --help`, network: true}, + {name: "unparseable gh issue create --title --help", command: `if %ERRORLEVEL% == 0 gh issue create --title --help --body test --repo owner/repo`, network: true}, + {name: "unparseable gh api -i -- --help", command: `if %ERRORLEVEL% == 0 gh api -i -- --help`, network: true}, {name: "unparseable gh api with uppercase -H header flag", command: `if %ERRORLEVEL% == 0 gh api -H "Accept: application/vnd.github+json" /user`, network: true}, {name: "unparseable gh auth status with -h hostname", command: `if %ERRORLEVEL% == 0 gh auth status -h github.com`, network: true}, {name: "unparseable gh auth status --help", command: `if %ERRORLEVEL% == 0 gh auth status --help`, network: false}, From aecddca38a5e11cf6cebc64b73d8c13ea08c06eb Mon Sep 17 00:00:00 2001 From: Yshuro Date: Sat, 28 Nov 2026 09:47:27 +0100 Subject: [PATCH 5/7] fix(sandbox): address PR #1244 review feedback --- internal/sandbox/analyzer.go | 26 ++++++++++++++++++++----- internal/sandbox/analyzer_test.go | 6 ++++++ internal/sandbox/risk_hardening_test.go | 1 + 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index 30ab50cdea..01812ee9f6 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -629,8 +629,10 @@ var ghSubcommandValueOptions = map[string]map[string]bool{ "--body": true, }, "pr": { - "--title": true, - "--body": true, + "--title": true, + "--body": true, + "--search": true, + "-S": true, }, "release": { "--title": true, @@ -643,7 +645,7 @@ var ghSubcommandValueOptions = map[string]map[string]bool{ }, } -func ghOptionTakesValue(flag string, subcommand string) bool { +func ghSingleOptionTakesValue(flag string, subcommand string) bool { if strings.HasPrefix(flag, "--") { flag = strings.ToLower(flag) } @@ -656,6 +658,20 @@ func ghOptionTakesValue(flag string, subcommand string) bool { return false } +func ghOptionTakesValue(flag string, subcommand string) bool { + if strings.HasPrefix(flag, "-") && !strings.HasPrefix(flag, "--") && len(flag) > 2 { + for idx := 1; idx < len(flag); idx++ { + if ghSingleOptionTakesValue("-"+string(flag[idx]), subcommand) { + // Only consumes the next token when the value-taking flag is last in the cluster; + // otherwise the remainder of this token is its attached value. + return idx == len(flag)-1 + } + } + return false + } + return ghSingleOptionTakesValue(flag, subcommand) +} + func ghSubcommands(words []string) (string, string) { var subs []string for i := 0; i < len(words); i++ { @@ -737,8 +753,8 @@ func ghUsesNetwork(words []string) bool { switch first { case "help", "version", "completion", "config", "alias": return false - case "extension": - return second != "" && second != "list" + case "extension", "ext", "extensions": + return second != "" && second != "list" && second != "ls" default: return true } diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index e9ae6c2748..a7c2429dce 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -103,6 +103,8 @@ func TestAnalyzeCommand(t *testing.T) { {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 with --search --help stays network", script: "gh pr list --search --help", network: true}, + {name: "gh pr list with -S --help stays network", script: "gh pr list -S --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}, @@ -111,6 +113,7 @@ func TestAnalyzeCommand(t *testing.T) { {name: "gh api with header flag -H stays network", script: `gh api -H "Accept: application/vnd.github+json" /user`, network: true}, {name: "gh api with preview value --help stays network", script: "gh api /user --preview --help", network: true}, {name: "gh api with boolean -i and -- before --help endpoint stays network", script: "gh api -i -- --help", network: true}, + {name: "gh api with short-option cluster -it --help user stays network", script: "gh api -it --help user", network: true}, {name: "gh api --help is offline", script: "gh api --help", network: false}, {name: "gh issue create with title --help stays network", script: "gh issue create --title --help --body test --repo owner/repo", network: true}, {name: "gh auth status", script: "gh auth status", network: true}, @@ -144,6 +147,9 @@ func TestAnalyzeCommand(t *testing.T) { {name: "gh alias with repo flag between is offline", script: "gh alias -R owner/repo list", network: false}, {name: "gh extension bare is offline", script: "gh extension", 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: "gh extension list with repo flag between is offline", script: "gh extension -R owner/repo list", network: false}, {name: "gh extension list with repo flag in front is offline", script: "gh -R owner/repo extension list", network: false}, {name: "no network", script: "ls -la && echo done", network: false}, diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index 34b39cc978..30d0413125 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -346,6 +346,7 @@ func TestClassifyUnparseableGHCommands(t *testing.T) { {name: "unparseable gh extension exec forwarded --help", command: `if %ERRORLEVEL% == 0 gh extension exec myext --help`, network: true}, {name: "unparseable gh issue create --title --help", command: `if %ERRORLEVEL% == 0 gh issue create --title --help --body test --repo owner/repo`, network: true}, {name: "unparseable gh api -i -- --help", command: `if %ERRORLEVEL% == 0 gh api -i -- --help`, network: true}, + {name: "unparseable gh api -it --help user", command: `if %ERRORLEVEL% == 0 gh api -it --help user`, network: true}, {name: "unparseable gh api with uppercase -H header flag", command: `if %ERRORLEVEL% == 0 gh api -H "Accept: application/vnd.github+json" /user`, network: true}, {name: "unparseable gh auth status with -h hostname", command: `if %ERRORLEVEL% == 0 gh auth status -h github.com`, network: true}, {name: "unparseable gh auth status --help", command: `if %ERRORLEVEL% == 0 gh auth status --help`, network: false}, From 3816e97c1b470ee3770fb36a5bf9fc0dc02618cf Mon Sep 17 00:00:00 2001 From: Yshuro Date: Sun, 29 Nov 2026 14:08:53 +0100 Subject: [PATCH 6/7] Simplify gh network classification per review feedback --- internal/sandbox/analyzer.go | 212 ++------------------- internal/sandbox/analyzer_test.go | 43 ++--- internal/sandbox/risk.go | 238 +++--------------------- internal/sandbox/risk_hardening_test.go | 33 +--- 4 files changed, 66 insertions(+), 460 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index 01812ee9f6..dd8a75341e 100644 --- a/internal/sandbox/analyzer.go +++ b/internal/sandbox/analyzer.go @@ -269,40 +269,40 @@ func commandUsesNetwork(prog string, args []*syntax.Word) bool { return true } if pythonLauncherPrograms[prog] { - return pythonModuleUsesNetwork(lowerWords(words)) + return pythonModuleUsesNetwork(words) } switch prog { case "npm": - return packageManagerUsesNetwork(lowerWords(words), map[string]string{ + return packageManagerUsesNetwork(words, map[string]string{ "run": "run", "exec": "exec", "x": "exec", }) case "pnpm": - return packageManagerUsesNetwork(lowerWords(words), map[string]string{ + return packageManagerUsesNetwork(words, map[string]string{ "run": "run", "exec": "exec", "dlx": "exec", }) case "yarn": - return packageManagerUsesNetwork(lowerWords(words), map[string]string{ + return packageManagerUsesNetwork(words, map[string]string{ "run": "run", "exec": "exec", "dlx": "exec", }) case "bun": - return packageManagerUsesNetwork(lowerWords(words), map[string]string{ + return packageManagerUsesNetwork(words, map[string]string{ "run": "run", "x": "exec", }) case "npx": - return npxUsesNetwork(lowerWords(words)) + return npxUsesNetwork(words) case "pip", "pip2", "pip3": - return firstSubcommand(lowerWords(words), nil) == "install" + return firstSubcommand(words, nil) == "install" case "go": - return firstSubcommand(lowerWords(words), nil) == "get" + return firstSubcommand(words, nil) == "get" case "git": - return gitUsesNetwork(lowerWords(words)) + return gitUsesNetwork(words) case "gh": return ghUsesNetwork(words) default: @@ -340,7 +340,7 @@ func packageManagerUsesNetwork(words []string, aliases map[string]string) bool { } operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - subcommand := strings.ToLower(operands[index]) + subcommand := operands[index] if alias, ok := aliases[subcommand]; ok { subcommand = alias } @@ -377,19 +377,11 @@ func npxUsesNetwork(_ []string) bool { func literalWordTexts(args []*syntax.Word) []string { words := make([]string, 0, len(args)) for _, arg := range args { - words = append(words, strings.TrimSpace(wordText(arg))) + words = append(words, strings.ToLower(strings.TrimSpace(wordText(arg)))) } return words } -func lowerWords(words []string) []string { - lowered := make([]string, len(words)) - for i, w := range words { - lowered[i] = strings.ToLower(w) - } - return lowered -} - func pythonModuleUsesNetwork(words []string) bool { for index := 0; index < len(words); index++ { if words[index] != "-m" || index+1 >= len(words) { @@ -416,7 +408,7 @@ func pythonModuleUsesNetwork(words []string) bool { // is not EGRESS, not that dev tooling is inert. Anything that actually fetches // still matches commandUsesNetwork through its own program or subcommand. func commandRunsLocalServer(prog string, args []*syntax.Word) bool { - words := lowerWords(literalWordTexts(args)) + words := literalWordTexts(args) if localServerPrograms[prog] { return frameworkSubcommandRunsLocalServer(prog, words) } @@ -454,7 +446,7 @@ func frameworkSubcommandRunsLocalServer(prog string, words []string) bool { // the same way the package managers do; see subcommandPositions. operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - switch strings.ToLower(operands[index]) { + switch operands[index] { case "dev", "start", "serve", "preview": return true case "build", "optimize", "generate", "check", "lint", "export": @@ -475,11 +467,10 @@ func frameworkSubcommandRunsLocalServer(prog string, words []string) bool { func packageManagerRunsLocalServer(words []string) bool { operands, eatable := commandOperands(words) for index := 0; index < subcommandPositions(operands, eatable); index++ { - op := strings.ToLower(operands[index]) - if packageManagerServingScripts[op] { + if packageManagerServingScripts[operands[index]] { return true } - if op == "run" && index+1 < len(operands) && packageManagerServingScripts[strings.ToLower(operands[index+1])] { + if operands[index] == "run" && index+1 < len(operands) && packageManagerServingScripts[operands[index+1]] { return true } } @@ -579,182 +570,15 @@ func pythonModuleRunsLocalServer(words []string) bool { return false } -var ghValueOptions = map[string]bool{ - "-R": true, - "--repo": true, - "--host": true, - "--hostname": true, - "-s": true, - "--shell": true, - "-t": true, - "--template": true, - "-q": true, - "--jq": true, - "-L": true, - "--limit": true, - "-X": true, - "--method": true, - "-f": true, - "-F": true, - "--field": true, - "--raw-field": true, - "-H": true, - "--header": true, - "-p": true, - "--pattern": true, - "--preview": true, - "-d": true, - "--dir": true, - "-o": true, - "--output": true, - "--owner": true, - "-a": true, - "--archive": true, - "--custom-agent": true, - "-b": true, - "--base": true, - "--ref": true, - "-e": true, - "--env": true, - "--input": true, - "--cache": true, -} - -var ghSubcommandValueOptions = map[string]map[string]bool{ - "auth": { - "-h": true, - }, - "issue": { - "--title": true, - "--body": true, - }, - "pr": { - "--title": true, - "--body": true, - "--search": true, - "-S": true, - }, - "release": { - "--title": true, - }, - "project": { - "--title": true, - }, - "run": { - "-i": true, - }, -} - -func ghSingleOptionTakesValue(flag string, subcommand string) bool { - if strings.HasPrefix(flag, "--") { - flag = strings.ToLower(flag) - } - if ghValueOptions[flag] { - return true - } - if opts, ok := ghSubcommandValueOptions[subcommand]; ok { - return opts[flag] - } - return false -} - -func ghOptionTakesValue(flag string, subcommand string) bool { - if strings.HasPrefix(flag, "-") && !strings.HasPrefix(flag, "--") && len(flag) > 2 { - for idx := 1; idx < len(flag); idx++ { - if ghSingleOptionTakesValue("-"+string(flag[idx]), subcommand) { - // Only consumes the next token when the value-taking flag is last in the cluster; - // otherwise the remainder of this token is its attached value. - return idx == len(flag)-1 - } - } - return false - } - return ghSingleOptionTakesValue(flag, subcommand) -} - -func ghSubcommands(words []string) (string, string) { - var subs []string - for i := 0; i < len(words); i++ { - w := words[i] - if w == "" { - continue - } - if w == "--" { - break - } - if strings.HasPrefix(w, "-") { - first := "" - if len(subs) > 0 { - first = subs[0] - } - if !strings.Contains(w, "=") && ghOptionTakesValue(w, first) && i+1 < len(words) { - i++ - } - continue - } - subs = append(subs, strings.ToLower(w)) - if len(subs) == 2 { - break - } - } - var first, second string - if len(subs) > 0 { - first = subs[0] - } - if len(subs) > 1 { - second = subs[1] - } - return first, second -} - -func ghHasHelpOrVersionFlag(words []string, subcommand string) bool { - var subs []string - for i := 0; i < len(words); i++ { - w := words[i] - if w == "" { - continue - } - if w == "--" { - break - } - if strings.HasPrefix(w, "-") { - if !strings.Contains(w, "=") && ghOptionTakesValue(w, subcommand) && i+1 < len(words) { - i++ - continue - } - switch w { - case "--help", "--version": - return true - case "-h": - if subcommand != "auth" { - return true - } - } - continue - } - subs = append(subs, strings.ToLower(w)) - // Everything after `gh extension exec` (or `gh ext exec`) is forwarded to - // the extension runner, so `--help`/`--version`/`-h` there are not gh's own. - if len(subs) >= 2 && (subs[0] == "extension" || subs[0] == "ext") && subs[1] == "exec" { - break - } - } - return false -} - func ghUsesNetwork(words []string) bool { - first, second := ghSubcommands(words) - if ghHasHelpOrVersionFlag(words, first) { - return false - } - if first == "" { + if len(words) == 0 { return false } - switch first { + switch words[0] { case "help", "version", "completion", "config", "alias": return false case "extension", "ext", "extensions": - return second != "" && second != "list" && second != "ls" + return len(words) < 2 || (words[1] != "list" && words[1] != "ls") default: return true } diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index a7c2429dce..86fa06904b 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -103,55 +103,36 @@ func TestAnalyzeCommand(t *testing.T) { {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 with --search --help stays network", script: "gh pr list --search --help", network: true}, - {name: "gh pr list with -S --help stays network", script: "gh pr list -S --help", 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 header flag -H stays network", script: `gh api -H "Accept: application/vnd.github+json" /user`, network: true}, - {name: "gh api with preview value --help stays network", script: "gh api /user --preview --help", network: true}, - {name: "gh api with boolean -i and -- before --help endpoint stays network", script: "gh api -i -- --help", network: true}, - {name: "gh api with short-option cluster -it --help user stays network", script: "gh api -it --help user", network: true}, - {name: "gh api --help is offline", script: "gh api --help", network: false}, - {name: "gh issue create with title --help stays network", script: "gh issue create --title --help --body test --repo owner/repo", 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 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 auth status with hostname flag -h stays network", script: "gh auth status -h github.com", network: true}, - {name: "gh auth status --help is offline", script: "gh auth status --help", network: false}, {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 stays network", script: "gh extension exec myext --help", network: true}, - {name: "gh extension exec with --help directly after exec stays network", script: "gh extension exec --help", network: true}, - {name: "gh extension --help before exec is offline", script: "gh extension --help exec myext", network: false}, + {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 help topic is offline", script: "gh help pr", network: false}, {name: "gh version is offline", script: "gh version", network: false}, - {name: "gh help flag is offline", script: "gh --help", network: false}, - {name: "gh version flag is offline", script: "gh --version", network: false}, - {name: "gh subcommand help flag after is offline", script: "gh pr list --help", network: false}, - {name: "gh subcommand help flag before is offline", script: "gh --help pr list", network: false}, - {name: "gh subcommand short help flag is offline", script: "gh pr -h", network: false}, - {name: "gh subcommand help flag with repo flag is offline", script: "gh -R owner/repo pr list --help", network: false}, - {name: "gh subcommand version flag is offline", script: "gh pr list --version", network: false}, - {name: "gh repo value named --help stays network", script: "gh -R --help pr list", network: true}, {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 config set is offline", script: "gh config set editor vim", network: false}, - {name: "gh config with host flag is offline", script: "gh config --host github.com get editor", network: false}, - {name: "gh config with repo flag in front is offline", script: "gh -R owner/repo config get git_protocol", network: false}, {name: "gh alias list is offline", script: "gh alias list", network: false}, - {name: "gh alias set is offline", script: "gh alias set pv 'pr view'", network: false}, - {name: "gh alias with repo flag in front is offline", script: "gh -R owner/repo alias list", network: false}, - {name: "gh alias with repo flag between is offline", script: "gh alias -R owner/repo list", network: false}, - {name: "gh extension bare is offline", script: "gh extension", 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: "gh extension list with repo flag between is offline", script: "gh extension -R owner/repo list", network: false}, - {name: "gh extension list with repo flag in front is offline", script: "gh -R owner/repo extension list", 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 a2d229b31b..466f1c527d 100644 --- a/internal/sandbox/risk.go +++ b/internal/sandbox/risk.go @@ -50,6 +50,32 @@ var ( // unparseableNetworkPattern is used only after the shell parser fails. At // that point the command is already marked too complex, so this intentionally // favors catching obvious network programs over proving exact shell syntax. + // + // IT MUST BE A SUPERSET OF WHAT THE AST PATH FLAGS FOR NETWORK, not a mirror + // of the analyzer's category names. The serving forms were dropped from here + // on the reasoning that they bind rather than fetch, but the analyzer sets + // Network for every recognized LocalServer anyway, deliberately and for two + // reasons it documents: nothing consumes LocalServer yet, so the approval IS + // the protection, and `npm run dev` is matched by SCRIPT NAME, where the + // repository decides what `dev` and `predev` actually do and either can reach + // out before a port is bound. + // + // So dropping them here did not align the two paths, it split them. The POSIX + // parser rejects Windows shell syntax the invoked shell accepts, and the + // analyzer names that as a fallback case, so: + // + // if "%OS%"=="Windows_NT" (npm run dev) else (npm start) + // -> parsed=false network=false categories=[shell unparseable_command] + // + // while every parseable spelling of the same command gets network. That is + // the approval gate disappearing on exactly the platform where it is the only + // egress control. Restored until a runner actually provides a scoped listener + // capability and the AST path stops setting Network for these itself. + // + // The fetch alternative is compiled from packageManagerNetworkSubcommands + // rather than a second list. The shorter list omitted ci, create, view, and + // search, so `npm --prefix ./web ci` kept network when it parsed and lost + // it inside a Windows conditional. unparseableNetworkPattern *regexp.Regexp // destructiveExtraPatterns hold high-severity patterns that the legacy // destructiveCommandPattern does not already cover. Folded in from the @@ -88,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` + `|\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 { @@ -103,216 +129,8 @@ func matchesDestructive(command string) bool { return false } -func executableBaseName(token string) string { - if idx := strings.LastIndexAny(token, `/\`); idx >= 0 { - token = token[idx+1:] - } - return strings.ToLower(token) -} - -func isGHExecutableToken(token string) bool { - base := executableBaseName(token) - return base == "gh" || base == "gh.exe" -} - -func isShellExecutableToken(token string) bool { - base := strings.TrimSuffix(executableBaseName(token), ".exe") - return shellPrograms[base] -} - -func isFDPrefix(s string) bool { - if s == "" { - return true - } - if s == "&" { - return true - } - for i := 0; i < len(s); i++ { - if s[i] < '0' || s[i] > '9' { - return false - } - } - return true -} - -// extractGHInvocations tokenizes an unparseable command string while preserving -// token case, quoted executable paths, and option values, splitting on unquoted -// shell/cmd separators (&, |, ;, (, ), CR, LF), stripping redirection operators -// and their filename operands, and recursing (up to maxAnalyzerDepth) into -// recognized shell wrappers (sh/bash/zsh/ksh/dash -c ...). -func extractGHInvocations(command string) [][]string { - return extractGHInvocationsDepth(command, 0) -} - -func extractGHInvocationsDepth(command string, depth int) [][]string { - type tok struct { - val string - sep bool - redir bool - consumesNext bool - } - var tokens []tok - var cur strings.Builder - hasCur := false - var quote byte - - flush := func() { - if hasCur { - tokens = append(tokens, tok{val: cur.String()}) - cur.Reset() - hasCur = false - } - } - - for i := 0; i < len(command); i++ { - c := command[i] - if quote != 0 { - if c == quote { - quote = 0 - } else { - cur.WriteByte(c) - } - continue - } - switch c { - case '"', '\'': - quote = c - hasCur = true - case ' ', '\t': - flush() - case '&', '|', ';', '(', ')', '\r', '\n': - flush() - tokens = append(tokens, tok{sep: true}) - case '<', '>': - if hasCur && !isFDPrefix(cur.String()) { - flush() - } - var redir strings.Builder - if hasCur { - redir.WriteString(cur.String()) - cur.Reset() - hasCur = false - } - for i < len(command) && (command[i] == '<' || command[i] == '>') { - redir.WriteByte(command[i]) - i++ - } - if i < len(command) && command[i] == '&' { - redir.WriteByte('&') - i++ - for i < len(command) && command[i] >= '0' && command[i] <= '9' { - redir.WriteByte(command[i]) - i++ - } - i-- - tokens = append(tokens, tok{val: redir.String(), redir: true, consumesNext: false}) - continue - } - hasAttachedTarget := false - for i < len(command) && command[i] != ' ' && command[i] != '\t' && - command[i] != '&' && command[i] != '|' && command[i] != ';' && - command[i] != '(' && command[i] != ')' && command[i] != '\r' && command[i] != '\n' { - hasAttachedTarget = true - if command[i] == '"' || command[i] == '\'' { - q := command[i] - i++ - for i < len(command) && command[i] != q { - redir.WriteByte(command[i]) - i++ - } - if i < len(command) { - i++ - } - continue - } - redir.WriteByte(command[i]) - i++ - } - i-- - tokens = append(tokens, tok{ - val: redir.String(), - redir: true, - consumesNext: !hasAttachedTarget, - }) - default: - cur.WriteByte(c) - hasCur = true - } - } - flush() - - var invocations [][]string - for i := 0; i < len(tokens); i++ { - if tokens[i].sep || tokens[i].redir { - continue - } - if isShellExecutableToken(tokens[i].val) { - end := i + 1 - for end < len(tokens) && !tokens[end].sep { - end++ - } - if depth < maxAnalyzerDepth { - for j := i + 1; j < end; j++ { - if tokens[j].redir { - if tokens[j].consumesNext && j+1 < end && !tokens[j+1].redir { - j++ - } - continue - } - if tokens[j].val == "--" { - break - } - if shellCommandFlag(tokens[j].val) { - k := j + 1 - for k < end && tokens[k].redir { - if tokens[k].consumesNext && k+1 < end && !tokens[k+1].redir { - k++ - } - k++ - } - if k < end && tokens[k].val != "" { - invocations = append(invocations, extractGHInvocationsDepth(tokens[k].val, depth+1)...) - } - break - } - } - } - i = end - 1 - continue - } - if !isGHExecutableToken(tokens[i].val) { - continue - } - var args []string - for j := i + 1; j < len(tokens) && !tokens[j].sep; j++ { - if tokens[j].redir { - if tokens[j].consumesNext && j+1 < len(tokens) && !tokens[j+1].sep && !tokens[j+1].redir { - j++ - } - i = j - continue - } - if tokens[j].val != "" { - args = append(args, tokens[j].val) - } - i = j - } - invocations = append(invocations, args) - } - return invocations -} - -func unparseableGHUsesNetwork(command string) bool { - for _, words := range extractGHInvocations(command) { - if ghUsesNetwork(words) { - return true - } - } - return false -} - func matchesUnparseableNetwork(command string) bool { - return unparseableNetworkPattern.MatchString(command) || unparseableGHUsesNetwork(command) + return unparseableNetworkPattern.MatchString(command) } func Classify(request Request) Risk { diff --git a/internal/sandbox/risk_hardening_test.go b/internal/sandbox/risk_hardening_test.go index 30d0413125..7d90fc65d9 100644 --- a/internal/sandbox/risk_hardening_test.go +++ b/internal/sandbox/risk_hardening_test.go @@ -337,32 +337,15 @@ func TestClassifyUnparseableGHCommands(t *testing.T) { {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 gh release download with flag", command: `if %ERRORLEVEL% == 0 gh release -R owner/repo download v1`, network: true}, - {name: "unparseable gh with flag in front", command: `if %ERRORLEVEL% == 0 gh -R owner/repo pr list`, network: true}, - {name: "unparseable gh with quoted repo flag", command: `if %ERRORLEVEL% == 0 gh -R "owner/repo" pr list`, 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 quoted gh.exe path with quoted repo flag", command: `if %ERRORLEVEL% == 0 "C:\Program Files\GitHub CLI\gh.exe" -R "owner/repo" pr list`, network: true}, - {name: "unparseable gh extension install", command: `if %ERRORLEVEL% == 0 gh extension install owner/ext`, network: true}, - {name: "unparseable gh extension exec forwarded --help", command: `if %ERRORLEVEL% == 0 gh extension exec myext --help`, network: true}, - {name: "unparseable gh issue create --title --help", command: `if %ERRORLEVEL% == 0 gh issue create --title --help --body test --repo owner/repo`, network: true}, - {name: "unparseable gh api -i -- --help", command: `if %ERRORLEVEL% == 0 gh api -i -- --help`, network: true}, - {name: "unparseable gh api -it --help user", command: `if %ERRORLEVEL% == 0 gh api -it --help user`, network: true}, - {name: "unparseable gh api with uppercase -H header flag", command: `if %ERRORLEVEL% == 0 gh api -H "Accept: application/vnd.github+json" /user`, network: true}, - {name: "unparseable gh auth status with -h hostname", command: `if %ERRORLEVEL% == 0 gh auth status -h github.com`, network: true}, - {name: "unparseable gh auth status --help", command: `if %ERRORLEVEL% == 0 gh auth status --help`, network: false}, - {name: "unparseable bash -c quoted gh api payload", command: `if %ERRORLEVEL% == 0 bash -c "gh api /user"`, network: true}, - {name: "unparseable gh api redirected to --help filename", command: `if %ERRORLEVEL% == 0 gh api /user > --help`, network: true}, - {name: "unparseable gh api --help with redirection", command: `if %ERRORLEVEL% == 0 gh api > out.txt --help`, network: false}, - {name: "unparseable gh pr list unterminated quote", command: `gh pr list && "unterminated`, network: true}, - {name: "unparseable gh --help", command: `if %ERRORLEVEL% == 0 gh --help`, network: false}, - {name: "unparseable gh pr list --help", command: `if %ERRORLEVEL% == 0 gh pr list --help`, network: false}, - {name: "unparseable gh --help pr list", command: `if %ERRORLEVEL% == 0 gh --help pr list`, network: false}, - {name: "unparseable gh help", command: `if %ERRORLEVEL% == 0 gh help`, network: false}, - {name: "unparseable gh config", command: `if %ERRORLEVEL% == 0 gh config get git_protocol`, network: false}, - {name: "unparseable gh alias list", command: `if %ERRORLEVEL% == 0 gh alias list`, network: false}, - {name: "unparseable gh alias list with flag", command: `if %ERRORLEVEL% == 0 gh -R owner/repo alias list`, network: false}, - {name: "unparseable gh extension list", command: `if %ERRORLEVEL% == 0 gh extension list`, network: false}, - {name: "unparseable gh extension list with flag", command: `if %ERRORLEVEL% == 0 gh extension -R owner/repo list`, network: false}, + {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) { From f4bd6c398c781882c03ceffa798d80a753a2dd16 Mon Sep 17 00:00:00 2001 From: Yshuro Date: Thu, 3 Dec 2026 18:02:28 +0100 Subject: [PATCH 7/7] fix(sandbox): require exact literal words for offline gh subcommands --- internal/sandbox/analyzer.go | 17 ++++++++++++----- internal/sandbox/analyzer_test.go | 4 ++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/internal/sandbox/analyzer.go b/internal/sandbox/analyzer.go index dd8a75341e..8d3922fb28 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,15 +570,22 @@ func pythonModuleRunsLocalServer(words []string) bool { return false } -func ghUsesNetwork(words []string) bool { - if len(words) == 0 { +func ghUsesNetwork(args []*syntax.Word) bool { + if len(args) == 0 { return false } - switch words[0] { + if !isLiteralWord(args[0]) { + return true + } + switch wordText(args[0]) { case "help", "version", "completion", "config", "alias": return false case "extension", "ext", "extensions": - return len(words) < 2 || (words[1] != "list" && words[1] != "ls") + if len(args) < 2 || !isLiteralWord(args[1]) { + return true + } + sub := wordText(args[1]) + return sub != "list" && sub != "ls" default: return true } diff --git a/internal/sandbox/analyzer_test.go b/internal/sandbox/analyzer_test.go index 86fa06904b..fa536fd96a 100644 --- a/internal/sandbox/analyzer_test.go +++ b/internal/sandbox/analyzer_test.go @@ -118,6 +118,10 @@ func TestAnalyzeCommand(t *testing.T) { {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},