diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 9ece9a485..67f4cb9ed 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -16,10 +16,17 @@ go_binary( # (compact_gc_target_test.go), entirely env-driven at run time, so one # prebuilt copy covers every test case and mode: the test never needs a host # Go toolchain under Bazel (bazeltest.RunfileEnv, BEADS_TEST_COMPACT_GC_FIXTURE). +# pure/race pinned explicitly, like :bd_for_tests: stdlib-only (encoding/json, +# fmt, os), so it needs neither cgo nor the race detector, and nothing about +# it depends on either; pinning keeps that true whatever --config the +# consuming go_test builds under (prcore/embedded/doltserver-*), instead of +# relying on this tiny fixture happening to inherit a compatible default. go_binary( name = "compact_gc_fixture", testonly = True, srcs = ["testdata/compact-gc-fixture.go"], + pure = "on", + race = "off", ) go_library( diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 105ec94c5..97a16d586 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -212,10 +212,17 @@ go_test( deps = ["//internal/testutil/bazeltest"], ) -# check-testing-short.sh over every Go file, test and non-test. +# check-testing-short.sh over every Go file, test and non-test, plus the +# Go-exec half of the host-go-under-Bazel policy (host_tool_exec_policy_test.go +# scans //:repo_go_srcs and //:repo_go_test_srcs for a literal go/gofmt exec). +# The shell-script half (host_tool_shell_policy_test.go) is its own +# :shell_host_tool_policy_test below: it reads a fixed, narrow list of shell +# scripts, not every Go source file, so it would otherwise pay this target's +# full re-run cost for files it never looks at. go_test( name = "go_sources_test", srcs = [ + "host_tool_exec_policy_test.go", "repo_helpers_test.go", "testing_short_tree_test.go", ], @@ -227,6 +234,52 @@ go_test( deps = ["//internal/testutil/bazeltest"], ) +# The shell-script half of the host-go-under-Bazel policy +# (host_tool_shell_policy_test.go: see its package comment for shellPolicyScope +# and the detection tradeoffs). Split out of go_sources_test (which otherwise +# re-ran on every Go-source change to serve only this file) with data limited +# to exactly the shell scripts shellPolicyScope names -- no BUILD.bazel/.bzl +# walk, and so no //:repo_other_files: TestShellPolicyScopeCoversBazelShellData +# (dropped; see that test's old doc comment in version control) could not +# parse a `//pkg:file.sh` label or a glob() either, so paying 758 extra input +# files and ~4 points of cache-rerun rate across the whole repo for a walk +# that demonstrably still missed real in-scope scripts was not worth it. +go_test( + name = "shell_host_tool_policy_test", + srcs = [ + "host_tool_shell_policy_test.go", + "repo_helpers_test.go", + ], + data = [ + "check-build-tags.sh", + "check-doc-freshness.sh", + "check-go-install-guidance.sh", + "check-migration-hygiene.sh", + "check-testing-short.sh", + "check-versions.sh", + "check-winget-portable-alias.sh", + "check-workapi-frontend-boundary.sh", + "ci/fmt-check.sh", + "ci/gofmt-bin.sh", + "ci/pr-lint.sh", + "conformance.sh", + "migrate-legacy-to-current.sh", + "migration-test/historical-dolt-upgrade-test.sh", + "migration-test/legacy-bridge-test.sh", + "migration-test/lib/binary.sh", + "release.sh", + "test.sh", + "upgrade-smoke-test.sh", + "//:.github/scripts/embedded-storage-test-shard.sh", + "//:.github/scripts/embedded-test-shard.sh", + "//:.github/scripts/proxied-test-shard.sh", + "//:.github/scripts/server-storage-test-shard.sh", + "//scripts/repochecks:run_check.sh", + "//scripts/repochecks:types_gen_drift_test.sh", + ], + deps = ["//internal/testutil/bazeltest"], +) + # Scans of every tracked file (//:repo_files): any change re-runs them, so # keep them few and fast. go_test( diff --git a/scripts/host_tool_exec_policy_test.go b/scripts/host_tool_exec_policy_test.go new file mode 100644 index 000000000..03ce7c606 --- /dev/null +++ b/scripts/host_tool_exec_policy_test.go @@ -0,0 +1,1001 @@ +package scripts_test + +// Policy: no new ungated host `go` or `gofmt` exec under Bazel +// (//scripts:go_sources_test, which declares //:repo_go_srcs and +// //:repo_go_test_srcs, so a non-Go change leaves it cached). +// +// Background (f4-spec.md §9, a release-track planning note). Under `bazel test` the +// PATH is hermetic_bin: plus the strict action env +// (tools/bazel/test_env.sh); the hermetic pinned dolt resolves there, but +// `go` and `gofmt` do not — whether a bare `go` or `gofmt` on that PATH finds +// a host toolchain depends on the worker, so a bare exec of either succeeds +// on some workers and exits 127 on others (gastownhall/beads#7478 fixed one: +// TestRunCompactDoltTargetsOnlyAuthorizedActiveDatabase's fixture builder). +// `dolt` is deliberately NOT in scope here: the hermetic copy comes first on +// that PATH (test_env.sh), so a bare `exec.Command("dolt", ...)` or +// `exec.LookPath("dolt")` is not the worker-dependent failure this policy +// guards against, and the repository has on the order of 80 such call sites +// (internal/storage/dolt, cmd/bd, internal/testutil, ...); allowlisting them +// all here for no safety gain would make this policy file itself the +// un-reviewable thing. Extend bannedHostTools if that changes. +// +// Scope: every *_test.go file (repo-wide) plus the non-test .go files of +// internal/testutil, the one package dedicated to test helpers that other +// packages' tests import. Production code (cmd/bd's non-test files, +// internal/storage/..., internal/doltserver, ...) legitimately execs `dolt` +// (and `go`, for `bd preflight`/`bd doctor`) as part of what bd itself does; +// that is not in scope. +// +// A call site is gated, and so exempt, when: +// - its tool-name argument is not a string literal (a runfiles-resolved +// binary: bazeltest.RunfileEnv/Runfile/PrebuiltBD/testGo, or a variable +// a helper returns only after such a check) — the scan below only looks +// at literal arguments, so this case never reaches it; +// - the enclosing function (or func literal) contains an `if` whose +// condition names bazeltest.IsBazel() or tests os.Getenv("TEST_SRCDIR") +// against "", AND whose body aborts the function (return/continue/break/ +// t.Fatal*/t.Skip*/t.FailNow/panic/os.Exit) exactly when that condition +// is true-under-Bazel (see condPolarity/blockAborts) — i.e. the +// enclosing scope provably never reaches the exec while running under +// Bazel. This is deliberately still position-agnostic (it does not +// check that the `if` lexically precedes the call), matching the +// allowlist's own "the mechanism exists somewhere in this function" +// looseness, but it IS polarity-aware: `if !bazeltest.IsBazel() { +// t.Skip() }` (which makes the function run ONLY under Bazel) is not a +// gate, because the abort fires on the opposite condition +// (gastownhall/beads#7485 review point 6); +// - the enclosing function is itself named (not a func literal) and is +// called only from sites that are themselves gated by the rule above — +// checked by scanning every other in-scope file for bare calls to that +// name (helperIsGatedByAllCallers). This is what makes a shared builder +// like goBuildBDCommand (no gate of its own, nine call sites) provably +// safe without an allowlist entry that only asserts one caller's +// behavior in prose (gastownhall/beads#7485 review point 4): a tenth, +// ungated caller added later breaks this check, not just the comment; +// - it is in hostToolExecAllowlist below, each entry naming the gating +// mechanism the above cannot see, keyed by file + enclosing function + +// an exact source-line snippet rather than a line number, so it survives +// unrelated edits that shift lines (gastownhall/beads#7485 review point 5). +// +// Mutation check: TestHostToolExecPolicyScan's fixtures include a bare +// `exec.Command("go", "version")` with no gate and no allowlist entry, and +// assert the scan reports it; a second fixture pins the inverted-polarity +// case (`if !bazeltest.IsBazel() { t.Skip() }`) as still ungated; a third +// pins that a helper with one ungated caller among several is reported. + +import ( + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "strconv" + "strings" + "testing" +) + +// bannedHostTools are the literal exec argument values this policy flags +// when ungated and unallowlisted. See the package comment above for why +// "dolt" is not here. +var bannedHostTools = map[string]bool{"go": true, "gofmt": true} + +// hostToolExecHit is one call this scan found, gated or not. +type hostToolExecHit struct { + line int + tool string + call string // "exec.Command", "exec.CommandContext", "exec.LookPath" + gated bool + funcName string // enclosing top-level func's name, or "" (func literal/none) + snippet string // trimmed source text of the hit's line, for allowlist matching +} + +// funcScopes bundles the per-file lookups findHostToolExecCalls and +// findCallSitesOf share: the smallest function/func-literal body enclosing a +// position, whether that scope is gated, and the top-level function name (if +// any) owning a body. +type funcScopes struct { + enclosing func(pos token.Pos) ast.Node + funcName func(scope ast.Node) string + gated func(scope ast.Node, pos token.Pos) bool +} + +// newFuncScopes builds the body/gating lookups for file. +func newFuncScopes(file *ast.File) *funcScopes { + var bodies []*ast.BlockStmt + owner := map[*ast.BlockStmt]string{} + ast.Inspect(file, func(n ast.Node) bool { + switch fn := n.(type) { + case *ast.FuncDecl: + if fn.Body != nil { + bodies = append(bodies, fn.Body) + owner[fn.Body] = fn.Name.Name + } + case *ast.FuncLit: + bodies = append(bodies, fn.Body) + } + return true + }) + + enclosing := func(pos token.Pos) ast.Node { + var best *ast.BlockStmt + for _, b := range bodies { + if b.Pos() <= pos && pos <= b.End() { + if best == nil || (b.End()-b.Pos()) < (best.End()-best.Pos()) { + best = b + } + } + } + if best != nil { + return best + } + return file + } + + funcName := func(scope ast.Node) string { + if b, ok := scope.(*ast.BlockStmt); ok { + return owner[b] + } + return "" + } + + gated := func(scope ast.Node, pos token.Pos) bool { + prebuiltVars := prebuiltResultVars(scope) + found := false + ast.Inspect(scope, func(n ast.Node) bool { + // Do not descend into a nested func literal: its gate (or lack + // of one) belongs to that closure's own scope, not to pos's + // enclosing scope. Without this, a gate inside a sibling + // t.Run(...) closure (or any other func literal) would be + // treated as covering an exec outside it entirely + // (gastownhall/beads#7485 review, "sibling closures"). + if _, ok := n.(*ast.FuncLit); ok && n != scope { + return false + } + ifStmt, ok := n.(*ast.IfStmt) + if !ok { + return true + } + bazelWhenTrue, matched := condPolarity(ifStmt.Cond, prebuiltVars) + if !matched { + return true + } + // Abort style: the if's body aborts (never falls through to the + // rest of the enclosing scope) exactly when ifStmt.Cond is true. + // If Cond is true precisely when running under Bazel, the rest + // of the scope -- wherever pos sits in it, PROVIDED pos is not + // itself inside this same Bazel-true branch -- is reachable + // only off Bazel: a real gate, independent of whether pos is + // textually before or after this if (deliberately permissive, + // see the package comment). The !within(ifStmt.Body, pos) + // guard makes that provision exact: `if bazeltest.IsBazel() { + // exec.Command("go", ...); return }` has bazelWhenTrue=true + // and an aborting body, but the exec sits INSIDE that same + // true-under-Bazel branch, before its own return -- it runs + // under Bazel every time, so this must not set found + // (gastownhall/beads#7485 review, "M17"). If Cond is true + // precisely when NOT under Bazel (the inverted case), the + // rest of the scope is reachable ONLY under Bazel -- the + // opposite of a gate -- so this must not set found either. + if bazelWhenTrue && blockAborts(ifStmt.Body) && !within(ifStmt.Body, pos) { + found = true + } + // Containment style: pos sits inside a branch that only runs + // off Bazel -- the if's Body when Cond is true off Bazel, or + // its Else when Cond is true under Bazel -- regardless of + // whether that branch aborts (e.g. `if !bazeltest.IsBazel() { + // ...exec... }` with no return at all, a common real pattern: + // cmd/bd/test_repo_beads_guard_test.go's GOCACHE/GOMODCACHE + // probes). An inverted containment (pos inside the branch that + // runs UNDER Bazel) must not set found -- that is review point + // 6's bug. + if !bazelWhenTrue && within(ifStmt.Body, pos) { + found = true + } + // ifStmt.Else is either a *ast.BlockStmt (a plain `else { ... }`) + // or another *ast.IfStmt (an `else if` link in a chain); either + // way its whole span -- including a further-nested else-if's own + // body -- is reached only when ifStmt.Cond is false, so a single + // Pos/End containment check on the Else node covers the chain. + if ifStmt.Else != nil && bazelWhenTrue && within(ifStmt.Else, pos) { + found = true + } + return true + }) + return found + } + + return &funcScopes{enclosing: enclosing, funcName: funcName, gated: gated} +} + +// condPolarity reports, for a condition expr, whether expr is true exactly +// when running under Bazel (bazelWhenTrue), and whether expr is a condition +// this policy recognizes at all (matched). prebuiltVars names local variables +// already known (via prebuiltResultVars) to hold a findPrebuiltBDBinary()/ +// bazeltest.PrebuiltBD() result: under Bazel that call either errors (an +// abort in its own right, wherever the caller checks it) or returns a +// non-empty path (bazeltest.PrebuiltBD, internal/testutil/bazeltest/ +// bazeltest.go), so it is never "" under Bazel. Recognized forms: +// - bazeltest.IsBazel() (or any x.IsBazel()): true under Bazel. +// - ! / (): negation/parens flip/pass through. +// - os.Getenv("TEST_SRCDIR") != "" : true under Bazel; == "" : true off Bazel. +// - prebuiltVar != "" : true under Bazel; == "" : true off Bazel. +// - a && / || of two recognized sub-conditions that agree on polarity (a +// mixed-polarity compound isn't a single-condition gate, so it is left +// unmatched rather than guessed at). +func condPolarity(cond ast.Expr, prebuiltVars map[string]bool) (bazelWhenTrue bool, matched bool) { + switch e := cond.(type) { + case *ast.ParenExpr: + return condPolarity(e.X, prebuiltVars) + case *ast.UnaryExpr: + if e.Op == token.NOT { + pol, ok := condPolarity(e.X, prebuiltVars) + if ok { + return !pol, true + } + } + case *ast.CallExpr: + if sel, ok := e.Fun.(*ast.SelectorExpr); ok && sel.Sel.Name == "IsBazel" { + return true, true + } + case *ast.BinaryExpr: + nonEmptyUnderBazel := isTestSrcdirGetenv(e.X) || isPrebuiltVarIdent(e.X, prebuiltVars) || + isTestSrcdirGetenv(e.Y) || isPrebuiltVarIdent(e.Y, prebuiltVars) + if nonEmptyUnderBazel && (isEmptyStringLit(e.X) || isEmptyStringLit(e.Y)) { + switch e.Op { + case token.NEQ: + return true, true + case token.EQL: + return false, true + } + } + if e.Op == token.LAND || e.Op == token.LOR { + lp, lok := condPolarity(e.X, prebuiltVars) + rp, rok := condPolarity(e.Y, prebuiltVars) + switch { + case lok && rok && lp == rp: + return lp, true + case lok && !rok: + return lp, true + case rok && !lok: + return rp, true + } + } + } + return false, false +} + +// isPrebuiltVarIdent reports whether e is a bare identifier naming a variable +// prebuiltResultVars recognized as a findPrebuiltBDBinary()/ +// bazeltest.PrebuiltBD() result. +func isPrebuiltVarIdent(e ast.Expr, prebuiltVars map[string]bool) bool { + id, ok := e.(*ast.Ident) + return ok && prebuiltVars[id.Name] +} + +// prebuiltResultVars scans scope for assignments whose right-hand side is a +// call to findPrebuiltBDBinary() or (any x.)PrebuiltBD(), returning the set +// of local names bound to the call's first result -- the "prebuilt binary +// path, or \"\"" value. A caller that checks `name != ""` and aborts when +// true is gated: see condPolarity. +func prebuiltResultVars(scope ast.Node) map[string]bool { + vars := map[string]bool{} + ast.Inspect(scope, func(n ast.Node) bool { + assign, ok := n.(*ast.AssignStmt) + if !ok || len(assign.Rhs) != 1 || len(assign.Lhs) < 1 { + return true + } + call, ok := assign.Rhs[0].(*ast.CallExpr) + if !ok { + return true + } + isPrebuiltCall := false + switch fn := call.Fun.(type) { + case *ast.Ident: + isPrebuiltCall = fn.Name == "findPrebuiltBDBinary" + case *ast.SelectorExpr: + isPrebuiltCall = fn.Sel.Name == "PrebuiltBD" + } + if !isPrebuiltCall { + return true + } + if id, ok := assign.Lhs[0].(*ast.Ident); ok && id.Name != "_" { + vars[id.Name] = true + } + return true + }) + return vars +} + +func isTestSrcdirGetenv(e ast.Expr) bool { + call, ok := e.(*ast.CallExpr) + if !ok || len(call.Args) != 1 { + return false + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok || sel.Sel.Name != "Getenv" { + return false + } + lit, ok := call.Args[0].(*ast.BasicLit) + if !ok || lit.Kind != token.STRING { + return false + } + v, err := strconv.Unquote(lit.Value) + return err == nil && v == "TEST_SRCDIR" +} + +// within reports whether pos lies within node's source range. +func within(node ast.Node, pos token.Pos) bool { + return node.Pos() <= pos && pos <= node.End() +} + +func isEmptyStringLit(e ast.Expr) bool { + lit, ok := e.(*ast.BasicLit) + if !ok || lit.Kind != token.STRING { + return false + } + v, err := strconv.Unquote(lit.Value) + return err == nil && v == "" +} + +// blockAborts reports whether body unconditionally stops control flow from +// reaching whatever follows it: a return/break/continue, or a call that +// itself never returns to the caller in a way that matters here +// (t.Fatal*/t.Skip*/t.FailNow, panic, os.Exit). It inspects the whole body, +// not just its top-level statements, so `if x { t.Skip() }` inside a nested +// block still counts -- matching the same permissive, non-control-flow-exact +// spirit the rest of this policy uses (see the package comment). +func blockAborts(body *ast.BlockStmt) bool { + aborts := false + ast.Inspect(body, func(n ast.Node) bool { + switch s := n.(type) { + case *ast.ReturnStmt: + aborts = true + case *ast.BranchStmt: + if s.Tok == token.BREAK || s.Tok == token.CONTINUE { + aborts = true + } + case *ast.CallExpr: + switch fn := s.Fun.(type) { + case *ast.Ident: + if fn.Name == "panic" { + aborts = true + } + case *ast.SelectorExpr: + switch fn.Sel.Name { + case "Skip", "SkipNow", "Skipf", "Fatal", "Fatalf", "FailNow": + aborts = true + case "Exit": + if pkg, ok := fn.X.(*ast.Ident); ok && pkg.Name == "os" { + aborts = true + } + } + } + } + return true + }) + return aborts +} + +func literalArg(e ast.Expr) (string, bool) { + lit, ok := e.(*ast.BasicLit) + if !ok || lit.Kind != token.STRING { + return "", false + } + v, err := strconv.Unquote(lit.Value) + if err != nil { + return "", false + } + return v, true +} + +// findHostToolExecCalls scans a parsed file for exec.Command / CommandContext +// / LookPath calls whose tool-name argument is a string literal naming a +// bannedHostTools entry, and reports, for each, whether its enclosing +// function (or func literal) is gated (see the package comment), its +// enclosing top-level function name, and its line's trimmed source text. +func findHostToolExecCalls(fset *token.FileSet, file *ast.File, lines []string) []hostToolExecHit { + scopes := newFuncScopes(file) + + var hits []hostToolExecHit + ast.Inspect(file, func(n ast.Node) bool { + call, ok := n.(*ast.CallExpr) + if !ok { + return true + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok { + return true + } + pkg, ok := sel.X.(*ast.Ident) + if !ok || pkg.Name != "exec" { + return true + } + argIdx := -1 + switch sel.Sel.Name { + case "Command", "LookPath": + argIdx = 0 + case "CommandContext": + argIdx = 1 + default: + return true + } + if len(call.Args) <= argIdx { + return true + } + tool, ok := literalArg(call.Args[argIdx]) + if !ok || !bannedHostTools[tool] { + return true + } + line := fset.Position(call.Pos()).Line + scope := scopes.enclosing(call.Pos()) + hits = append(hits, hostToolExecHit{ + line: line, + tool: tool, + call: "exec." + sel.Sel.Name, + gated: scopes.gated(scope, call.Pos()), + funcName: scopes.funcName(scope), + snippet: snippetAt(lines, line), + }) + return true + }) + return hits +} + +// findCallSitesOf scans file for bare calls `name(...)` (an *ast.Ident, not a +// selector -- how a same-package helper like goBuildBDCommand is called) and +// reports whether each call site's enclosing function/func-literal is gated. +func findCallSitesOf(file *ast.File, name string) []bool { + scopes := newFuncScopes(file) + var gatedPerSite []bool + ast.Inspect(file, func(n ast.Node) bool { + call, ok := n.(*ast.CallExpr) + if !ok { + return true + } + ident, ok := call.Fun.(*ast.Ident) + if !ok || ident.Name != name { + return true + } + gatedPerSite = append(gatedPerSite, scopes.gated(scopes.enclosing(call.Pos()), call.Pos())) + return true + }) + return gatedPerSite +} + +// helperIsGatedByAllCallers reports whether every bare call to funcName, +// across every file in files, is itself gated -- and how many call sites it +// found. A helper with zero call sites is not considered gated this way (it +// is either dead code or the scan is being asked about the wrong name); the +// caller should fall back to requiring a direct gate or allowlist entry. +func helperIsGatedByAllCallers(files map[string]*ast.File, funcName string) (gated bool, callSites int) { + if funcName == "" { + return false, 0 + } + for _, f := range files { + for _, siteGated := range findCallSitesOf(f, funcName) { + callSites++ + if !siteGated { + return false, callSites + } + } + } + return callSites > 0, callSites +} + +func snippetAt(lines []string, line int) string { + if line < 1 || line > len(lines) { + return "" + } + return strings.TrimSpace(lines[line-1]) +} + +// hostToolAllowlistEntry is one justified exception: the enclosing function +// name (empty for a call at package scope or inside a func literal this +// policy cannot name) and the exact trimmed source line, rather than a line +// number, so an entry survives the file shifting lines around it +// (gastownhall/beads#7485 review point 5). +type hostToolAllowlistEntry struct { + fn string + snippet string + why string +} + +// hostToolExecAllowlist is file (repo-relative, slash-separated) -> the +// entries justifying an ungated literal go/gofmt exec there. Every entry +// here is a helper reached only when bazeltest.PrebuiltBD()/ +// findPrebuiltBDBinary() (which itself calls bazeltest.IsBazel() under the +// hood) already reported no prebuilt bd binary -- under Bazel that call +// never returns "", so the exec below it is unreachable there, just not +// through a mention this scan's textual check can find in the same +// function. goBuildBDCommand itself (nine call sites, cmd/bd/*_test.go) is +// deliberately NOT here: it has no gate of its own, so it is instead +// verified by helperIsGatedByAllCallers, which checks every call site +// directly rather than trusting a comment about them (review point 4). +// Note: internal/storage/uow/doltserver_provider_test.go and +// cmd/bd/migrate_dolt_mode_frontdoor_integration_test.go used to have entries +// here for their `go build` fallbacks; both are now recognized directly +// (prebuiltResultVars/condPolarity: each checks its own +// findPrebuiltBDBinary()/bazeltest.PrebuiltBD() result `!= ""` and returns), +// so they need no allowlist entry. +var hostToolExecAllowlist = map[string][]hostToolAllowlistEntry{ + "internal/storage/embeddeddolt/concurrency_test.go": { + { + fn: "TestConcurrencyMultiProcess", + snippet: `build := exec.CommandContext(ctx, "go", "test",`, + why: "in the else branch of `if testBin := os.Getenv(\"BEADS_TEST_EMBEDDED_TEST_BINARY\"); testBin != \"\"`; unset only off Bazel", + }, + }, + "tests/regression/regression_test.go": { + { + fn: "buildCandidate", + snippet: `cmd := exec.Command("go", "build", "-tags", "gms_pure_go", "-o", outPath, "./cmd/bd")`, + why: "buildCandidate's only caller gates it on bazeltest.PrebuiltBD() returning \"\"", + }, + }, +} + +// hostToolExecInScope reports whether rel (repo-relative, slash-separated) +// is in this policy's scope: see the package comment. +func hostToolExecInScope(rel string) bool { + if strings.HasSuffix(rel, "_test.go") { + return true + } + return strings.HasPrefix(rel, "internal/testutil/") && strings.HasSuffix(rel, ".go") +} + +func TestHostToolExecPolicyScan(t *testing.T) { + parse := func(t *testing.T, src string) (*token.FileSet, *ast.File, []string) { + t.Helper() + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, "fixture.go", src, 0) + if err != nil { + t.Fatalf("parse fixture: %v", err) + } + return fset, f, strings.Split(src, "\n") + } + + t.Run("ungated literal go exec is reported, unqated", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import "os/exec" + +func build() { + exec.Command("go", "version") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit (mutation check)", hits) + } + }) + + t.Run("CommandContext and LookPath also match", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import ( + "context" + "os/exec" +) + +func a(ctx context.Context) { + exec.CommandContext(ctx, "go", "build") +} + +func b() { + exec.LookPath("gofmt") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 2 || hits[0].gated || hits[1].gated { + t.Fatalf("hits = %+v, want two ungated hits", hits) + } + }) + + t.Run("dolt is out of scope", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import "os/exec" + +func d() { + exec.Command("dolt", "version") +} +`) + if hits := findHostToolExecCalls(fset, f, lines); len(hits) != 0 { + t.Fatalf("hits = %+v, want none (dolt is not a bannedHostTools entry)", hits) + } + }) + + t.Run("a variable tool name is not flagged", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import "os/exec" + +func build(goBin string) { + exec.Command(goBin, "build") +} +`) + if hits := findHostToolExecCalls(fset, f, lines); len(hits) != 0 { + t.Fatalf("hits = %+v, want none (not a literal)", hits) + } + }) + + t.Run("same-function IsBazel gate, textually before the exec", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import "os/exec" + +func build() { + if bazeltest.IsBazel() { + return + } + exec.Command("go", "build") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || !hits[0].gated { + t.Fatalf("hits = %+v, want one gated hit", hits) + } + }) + + t.Run("same-function IsBazel gate, textually after the exec, still counts", func(t *testing.T) { + // Deliberately permissive (see the package comment): the check is + // "does the function contain this mechanism", not control-flow aware + // about lexical order. + fset, f, lines := parse(t, `package p + +import "os/exec" + +func build() { + exec.Command("go", "build") + if bazeltest.IsBazel() { + panic("unreachable") + } +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || !hits[0].gated { + t.Fatalf("hits = %+v, want one gated hit", hits) + } + }) + + t.Run("exec inside the Bazel-true branch, before its own abort, is not gated (mutation check, review M17)", func(t *testing.T) { + // `if bazeltest.IsBazel() { exec.Command("go", ...); return }` runs + // the exec every time Bazel is true -- the opposite of safe. The old + // abort-style check only looked at whether ifStmt.Body aborts + // anywhere in it, not at whether pos (the exec) sits inside that + // same true-under-Bazel body ahead of the abort. + fset, f, lines := parse(t, `package p + +import "os/exec" + +func build() { + if bazeltest.IsBazel() { + exec.Command("go", "build") + return + } +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit: the exec runs every time Bazel is true, "+ + "before the branch's own return", hits) + } + }) + + t.Run("a gate inside a sibling closure does not cover an exec outside it (mutation check, review sibling-closures)", func(t *testing.T) { + // The gate lives inside a t.Run func literal; the exec is a sibling + // statement in the outer test function, not reached by descending + // into that closure. + // + // Named siblingClosureFixture, not TestSiblingClosure: see the + // "inverted IsBazel skip" fixture below for why a column-0 + // "func TestXxx(t *testing.T)" inside this raw string would trip + // tools/bazel/equivalence.py's line-based scan. + fset, f, lines := parse(t, `package p + +import "os/exec" + +func siblingClosureFixture(t *testing.T) { + t.Run("sub", func(t *testing.T) { + if bazeltest.IsBazel() { + return + } + }) + exec.Command("go", "build") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit: the gate is inside a sibling closure, "+ + "not the outer function's own scope", hits) + } + }) + + t.Run("TEST_SRCDIR gate", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import ( + "os" + "os/exec" +) + +func build() { + if os.Getenv("TEST_SRCDIR") != "" { + return + } + exec.LookPath("go") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || !hits[0].gated { + t.Fatalf("hits = %+v, want one gated hit", hits) + } + }) + + t.Run("inverted TEST_SRCDIR check is not a gate", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import ( + "os" + "os/exec" +) + +func build() { + if os.Getenv("TEST_SRCDIR") == "" { + return + } + exec.LookPath("go") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit: this function returns OFF Bazel and only "+ + "reaches the exec UNDER Bazel, which is backwards", hits) + } + }) + + t.Run("inverted IsBazel skip is not a gate (mutation check, review point 6)", func(t *testing.T) { + // `if !bazeltest.IsBazel() { t.Skip() }` makes the enclosing test run + // ONLY under Bazel: the exact opposite of a gate. The old scan saw + // "IsBazel" mentioned anywhere in the function and called that + // gated; this fixture pins that it no longer does. + // Named to avoid matching tools/bazel/equivalence.py's + // `^func\s+(Test\w*)\s*\(\s*\w+\s+\*testing\.T\s*\)` line scan: that + // regex reads this file's raw text (it doesn't compile it), so a + // column-0 "func TestXxx(t *testing.T)" inside this fixture string + // would be counted as a real top-level test the equivalence check + // then expects Bazel to have run. + fset, f, lines := parse(t, `package p + +import "os/exec" + +func bazelOnlyFixture(t *testing.T) { + if !bazeltest.IsBazel() { + t.Skip("bazel only") + } + exec.Command("go", "build") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit: this test runs ONLY under Bazel, "+ + "so the exec is never off the hermetic PATH", hits) + } + }) + + t.Run("TEST_SRCDIR gate", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import ( + "os" + "os/exec" +) + +func build() { + if os.Getenv("TEST_SRCDIR") != "" { + return + } + exec.LookPath("go") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || !hits[0].gated { + t.Fatalf("hits = %+v, want one gated hit", hits) + } + }) + + t.Run("a sibling function's gate does not cover this one", func(t *testing.T) { + fset, f, lines := parse(t, `package p + +import "os/exec" + +func caller() { + if bazeltest.IsBazel() { + return + } + helper() +} + +func helper() { + exec.Command("go", "build") +} +`) + hits := findHostToolExecCalls(fset, f, lines) + if len(hits) != 1 || hits[0].gated { + t.Fatalf("hits = %+v, want one ungated hit (the gate is in a different function)", hits) + } + }) + + t.Run("helper gated by call sites when every caller gates it", func(t *testing.T) { + _, helperFile, _ := parse(t, `package p + +import "os/exec" + +func helper() { + exec.Command("go", "build") +} +`) + _, callerFile, _ := parse(t, `package p + +func caller() { + if bazeltest.IsBazel() { + return + } + helper() +} + +func other() { + if bazeltest.IsBazel() { + return + } + helper() +} +`) + files := map[string]*ast.File{"helper.go": helperFile, "caller.go": callerFile} + gated, n := helperIsGatedByAllCallers(files, "helper") + if !gated || n != 2 { + t.Fatalf("helperIsGatedByAllCallers = (%v, %d), want (true, 2)", gated, n) + } + }) + + t.Run("helper NOT gated when any one caller is ungated (mutation check, review point 4)", func(t *testing.T) { + _, helperFile, _ := parse(t, `package p + +import "os/exec" + +func helper() { + exec.Command("go", "build") +} +`) + _, callerFile, _ := parse(t, `package p + +func caller() { + if bazeltest.IsBazel() { + return + } + helper() +} + +func newUngatedCaller() { + helper() +} +`) + files := map[string]*ast.File{"helper.go": helperFile, "caller.go": callerFile} + gated, n := helperIsGatedByAllCallers(files, "helper") + if gated || n != 2 { + t.Fatalf("helperIsGatedByAllCallers = (%v, %d), want (false, 2): a new ungated caller "+ + "must make the helper unsafe again", gated, n) + } + }) + + t.Run("a helper with no call sites found is not considered gated", func(t *testing.T) { + files := map[string]*ast.File{} + gated, n := helperIsGatedByAllCallers(files, "helper") + if gated || n != 0 { + t.Fatalf("helperIsGatedByAllCallers = (%v, %d), want (false, 0)", gated, n) + } + }) + + t.Run("hostToolExecInScope", func(t *testing.T) { + for rel, want := range map[string]bool{ + "cmd/bd/compact_gc_target_test.go": true, + "internal/testutil/localdoltserver.go": true, + "internal/testutil/integration/harness.go": true, + "internal/testutil/bazeltest/bazeltest_test.go": true, + "cmd/bd/compact.go": false, + "internal/storage/dolt/store.go": false, + "internal/doltserver/doltserver.go": false, + "scripts/ci/go-list-test-names/main.go": false, + } { + if got := hostToolExecInScope(rel); got != want { + t.Errorf("hostToolExecInScope(%q) = %v, want %v", rel, got, want) + } + } + }) +} + +// TestNoUngatedHostToolExecsUnderBazel scans the real tree (every *_test.go +// file and internal/testutil's non-test .go files) and fails on an +// unallowlisted, ungated literal go/gofmt exec. See the package comment. +func TestNoUngatedHostToolExecsUnderBazel(t *testing.T) { + root := sourceRepoRoot(t) + relFiles := repoFiles(t, root, 1000) + + type parsed struct { + file *ast.File + hits []hostToolExecHit + } + byFile := map[string]*ast.File{} + results := map[string]*parsed{} + scanned := 0 + for _, rel := range relFiles { + if !hostToolExecInScope(rel) { + continue + } + path := filepath.Join(root, filepath.FromSlash(rel)) + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v", rel, err) + } + fset := token.NewFileSet() + f, err := parser.ParseFile(fset, path, data, 0) + if err != nil { + t.Fatalf("parse %s: %v", rel, err) + } + scanned++ + lines := strings.Split(string(data), "\n") + hits := findHostToolExecCalls(fset, f, lines) + byFile[rel] = f + results[rel] = &parsed{file: f, hits: hits} + } + if scanned < 1000 { + t.Fatalf("scanned only %d in-scope files; the walk is broken", scanned) + } + + seen := map[string]map[int]bool{} // rel -> allowlist entry index -> matched + for rel, p := range results { + for _, hit := range p.hits { + if hit.gated { + continue + } + if idx, ok := matchAllowlist(hostToolExecAllowlist[rel], hit); ok { + if seen[rel] == nil { + seen[rel] = map[int]bool{} + } + seen[rel][idx] = true + continue + } + if gated, n := helperIsGatedByAllCallers(byFile, hit.funcName); gated && n > 0 { + continue + } + t.Errorf("%s:%d: ungated %s(%q): under `bazel test` the hermetic-first PATH may lack a host %s "+ + "toolchain, so this can exit 127 on some workers (f4-spec.md §9); gate it on bazeltest.IsBazel()/"+ + "TEST_SRCDIR, resolve the binary through bazeltest.RunfileEnv/PrebuiltBD, ensure every caller of "+ + "its enclosing function gates it, or add a justified hostToolExecAllowlist entry", rel, hit.line, hit.call, hit.tool, hit.tool) + } + } + for rel, entries := range hostToolExecAllowlist { + if _, scannedFile := results[rel]; !scannedFile { + t.Errorf("hostToolExecAllowlist entry for %s: file was not scanned (not in scope or missing)", rel) + continue + } + for idx, entry := range entries { + if !seen[rel][idx] { + t.Errorf("hostToolExecAllowlist entry %s (func %q, snippet %q) no longer matches an "+ + "ungated host-tool exec; remove it", rel, entry.fn, entry.snippet) + } + } + } +} + +// matchAllowlist finds the allowlist entry (if any) matching hit by enclosing +// function name and exact source-line snippet, returning its index for +// usage-tracking. +func matchAllowlist(entries []hostToolAllowlistEntry, hit hostToolExecHit) (int, bool) { + for i, e := range entries { + if e.fn == hit.funcName && e.snippet == hit.snippet { + return i, true + } + } + return 0, false +} diff --git a/scripts/host_tool_shell_policy_test.go b/scripts/host_tool_shell_policy_test.go new file mode 100644 index 000000000..15e58e662 --- /dev/null +++ b/scripts/host_tool_shell_policy_test.go @@ -0,0 +1,1103 @@ +package scripts_test + +// Policy: no new ungated bare `go`/`gofmt` exec in shell scripts under +// scripts/ or .github/scripts/ that a Bazel test actually runs. This is the +// shell-script half of host_tool_exec_policy_test.go's Go-exec policy (see +// that file's package comment for the full rationale shared by both: under +// `bazel test` PATH is `hermetic_bin:` plus the strict action env, and a host +// `go`/`gofmt` toolchain is not guaranteed to be on it the way the +// hermetic-first `dolt` is -- see f4-spec.md §9). "dolt" is out of scope here +// for the same reason it is out of scope there: it is the one tool that PATH +// ordering already makes safe. +// +// Scope: unlike the Go scanner (which can cheaply scan every *_test.go file +// in the tree), finding every shell script a Bazel test actually executes +// requires reading each candidate script's BUILD.bazel wiring (sh_test +// srcs/args/data, a go_test's data, or a Go test's exec.Command of a fixed +// script path) by hand; there is no existing Starlark-aware tool in this repo +// to do that automatically, and no mechanical backstop here either -- see +// TestShellPolicyScopeCoversBazelShellData's removal note further down for +// why a regex-based one was tried and dropped. In particular this scope +// cannot see the common run_check.sh-as-launcher pattern (most +// scripts/repochecks targets pass the real target script as an `arg`, not as +// `data`/`srcs`, so run_check.sh itself is the only mechanically discoverable +// entry for that whole family) -- those still need hand curation and +// citation below. +// +// shellPolicyScope is that set, as of this writing, with how each entry was +// confirmed to run under `bazel test`: +// +// scripts/ci/fmt-check.sh PROGRAM of //scripts/repochecks:fmt_test +// (run_check.sh execs it); data of +// //scripts/prlintmake:prlintmake_test +// scripts/ci/gofmt-bin.sh data of prlintmake_test, which execs it +// directly (gofmtbin_test.go) and fmt-check.sh +// execs it as its first line +// scripts/ci/pr-lint.sh data of //scripts/prlintmake:prlintmake_test +// AND //scripts/repochecks (doc_freshness_test's +// tracked-file tree); prlintmake_test.go execs it +// directly with gofmt/go shims on PATH +// scripts/check-build-tags.sh PROGRAM of repochecks:build_tags_test +// scripts/check-go-install-guidance.sh PROGRAM of repochecks:go_install_guidance_test +// scripts/check-winget-portable-alias.sh PROGRAM of repochecks:winget_portable_alias_test +// scripts/check-migration-hygiene.sh PROGRAM of repochecks:migration_hygiene_test +// scripts/check-doc-freshness.sh PROGRAM of repochecks:doc_freshness_test +// scripts/check-workapi-frontend-boundary.sh PROGRAM of repochecks:workapi_frontend_boundary_test +// scripts/check-testing-short.sh exec.Command("bash", ...)'d directly by +// testing_short_tree_test.go, in this package +// scripts/check-versions.sh exports_files'd to //scripts/check-versions, +// exec'd directly by pre_push_hook_test.go +// (its "exit-status contract") +// scripts/repochecks/run_check.sh srcs of every repochecks sh_test above +// scripts/repochecks/types_gen_drift_test.sh srcs of repochecks:types_gen_drift_test +// scripts/migration-test/lib/binary.sh data of //tests/migration's +// historical_upgrade_tests and legacy_bridge_test +// scripts/migration-test/historical-dolt-upgrade-test.sh ditto +// scripts/migration-test/legacy-bridge-test.sh ditto +// scripts/migrate-legacy-to-current.sh ditto (//scripts:migration_harness) +// scripts/upgrade-smoke-test.sh data of every upgrade_smoke__test +// sh_test (tests/upgrade_smoke/defs.bzl); IS +// Bazel-executed, unlike what an earlier +// revision of this comment claimed +// scripts/test.sh reached via //:repo_other_files (the +// scripts/** glob) as shell_scripts_test's +// data; test_script_test.go execs it 6 +// times with a fake go/prebuilt-bd driver +// first on PATH +// scripts/conformance.sh ditto; conformance_script_test.go execs +// it 4 times with a fake go on PATH +// scripts/release.sh ditto; release_script_test.go execs it +// with a fake bd first on PATH +// .github/scripts/embedded-test-shard.sh arg of cmd/bd's +// .github/scripts/embedded-storage-test-shard.sh bd_embedded_test/_part2_test, +// .github/scripts/proxied-test-shard.sh embeddeddolt_embedded_test, +// .github/scripts/server-storage-test-shard.sh bd_proxied_test and +// dolt_server_full_test sh_test rules, run via +// tools/bazel/go_test_manifest_shard.sh +// +// Deliberately NOT in scope: scripts/ci/lib/test-env.sh. Its own TEST_SRCDIR +// gate predates this policy and is good practice, but it is not itself wired +// as Bazel test data/srcs/args anywhere -- other scripts `source` it, and +// *their* wiring is what puts it in scope transitively through their own +// scan, not a standalone one for the library file. +// +// scripts/test.sh, scripts/conformance.sh and scripts/release.sh ARE in +// scope: each runs under `bazel test` through the `scripts/**` glob behind +// //:repo_other_files (shell_scripts_test's data), not through an explicit +// per-file label -- test_script_test.go execs test.sh 6 times (with a fake +// `go`/prebuilt-bd driver on PATH), conformance_script_test.go execs +// conformance.sh 4 times (fake `go` on PATH), and release_script_test.go +// execs release.sh with a fake `bd` on PATH. An earlier revision of this +// comment claimed test.sh was only linked-to by //test/docsync and never +// executed, and that conformance.sh had no BUILD.bazel reference at all and +// so was out of policy scope entirely; both claims were wrong (gastownhall/ +// beads#7485 review: "test.sh, conformance.sh and release.sh do run under +// Bazel"). +// +// Detection is line-based, not a real shell parser: a line is scanned unless +// it is a `#` comment, inside a `<<'EOF'`/`<_test. Both always set it. + "CANDIDATE_BIN": true, + // tools/bazel/go_test_manifest_shard.sh's `export "$bin_var=$PWD/$bin"` + // always sets exactly one of these three to an absolute path to a + // declared `data` test binary before exec'ing the matching shard script + // (cmd/bd/BUILD.bazel's bd_embedded_test/_part2_test/bd_proxied_test, + // internal/storage/embeddeddolt/BUILD.bazel's embedded storage sh_test, + // internal/storage/dolt/BUILD.bazel's server storage sh_test). + "BEADS_TEST_CMD_BINARY": true, + "BEADS_TEST_EMBEDDED_TEST_BINARY": true, + "BEADS_TEST_SERVER_TEST_BINARY": true, +} + +// shellAllowlistEntry mirrors host_tool_exec_policy_test.go's +// hostToolAllowlistEntry: keyed by the enclosing function (empty for +// top-level script code, outside any `name() { ... }`/`name() ( ... )`) and +// an exact snippet of the flagged line, not a line number, so the entry +// tracks the code through refactors instead of silently going stale or +// silently covering the wrong line (review point 5). +type shellAllowlistEntry struct { + fn string + snippet string + why string +} + +// shellExecAllowlist is file (repo-relative) -> entries for bare `go`/`gofmt` +// execs that are safe despite findShellHostToolExecs flagging them and +// shellLineGating not proving it structurally -- in every case here the gate +// is a generic `command -v`/PATH-shim precondition a Go test arranges before +// exec'ing the script, not a textual TEST_SRCDIR/shellBazelSetVars check this +// scanner's line-based gate tracker can see. +var shellExecAllowlist = map[string][]shellAllowlistEntry{ + "scripts/ci/gofmt-bin.sh": { + { + fn: "", + snippet: `if [[ "$(go env GOVERSION 2>/dev/null)" == "go$pinned" ]]; then`, + why: "only reached once `command -v go` (above) has already confirmed go is on " + + "PATH; //scripts/prlintmake:prlintmake_test's gofmtbin_test.go exercises every " + + "branch of this script with a real go on PATH", + }, + { + fn: "", + snippet: ` goroot="$(go env GOROOT 2>/dev/null || true)"`, + why: "same `command -v go` gate as the GOVERSION check above", + }, + { + fn: "", + snippet: `elif ! goroot="$(GOTOOLCHAIN="go$pinned" go env GOROOT 2>/dev/null)"; then`, + why: "same `command -v go` gate as the GOVERSION check above", + }, + }, + "scripts/ci/pr-lint.sh": { + { + fn: "", + snippet: `go run -mod=readonly -tags=gms_pure_go ./scripts/pr-lint`, + why: "scripts/prlintmake/prlintmake_test.go execs this script directly with a " + + "shim directory (containing a fake `go`) placed first on PATH", + }, + }, + "scripts/check-versions.sh": { + { + fn: "", + snippet: `if ! go build -tags=gms_pure_go -o "$checker" ./scripts/check-versions; then`, + why: "scripts/check-versions/pre_push_hook_test.go execs this script directly " + + "with fakeGoBuildDir(t) (a stand-in `go` that copies the Bazel-built " + + "BEADS_TEST_CHECK_VERSIONS binary instead of compiling from source) first on PATH", + }, + }, + "scripts/test.sh": { + { + fn: "", + snippet: `PREBUILT_BD_BIN="$PREBUILT_BD_DIR/bd$(go env GOEXE)"`, + why: "test_script_test.go execs test.sh with a fake `go` (and a prebuilt-bd " + + "driver copied into place by that fake go's `build` subcommand) first on PATH", + }, + { + fn: "", + snippet: `if go build -o "$PREBUILT_BD_BIN" "$REPO_ROOT/cmd/bd"; then`, + why: "same fake-go-first-on-PATH gate as the GOEXE line above", + }, + { + // fn is "cleanup_shared_server", not "": these two lines are + // top-level script code, textually after that indented, + // nested `cleanup_shared_server() { ... }` definition (around + // test.sh:191-195). findShellHostToolExecs' end-of-function + // detection only recognizes an UNINDENTED closing `}`/`)` (the + // column-0 convention every other in-scope script's functions + // follow); cleanup_shared_server's own closing brace is + // indented, so the scanner never sees this function end and + // attributes every later line in the file to it. Keying this + // entry by that attribution (rather than fixing the shared + // scanner for one nested-function script) keeps this fix + // scoped to what review item 1 asked for. + fn: "cleanup_shared_server", + snippet: `CMD=(go test -p "$GO_TEST_PKG_PARALLEL" -parallel "$GO_TEST_PARALLEL" -timeout "$TIMEOUT")`, + why: "same fake-go-first-on-PATH gate as the GOEXE line above", + }, + { + fn: "cleanup_shared_server", // see the fn note on the CMD= entry above + snippet: `total=$(go tool cover -func="$COVERPROFILE" | awk '/^total:/ {print $NF}')`, + why: "same fake-go-first-on-PATH gate; test_script_test.go's fake go's `tool` " + + "subcommand (COVERAGE is never requested by the test, so this line only " + + "executes under a real invocation, not under the policy's own harness, but " + + "the fake go sits first on PATH for the whole script run regardless)", + }, + }, + "scripts/conformance.sh": { + { + fn: "", + snippet: `BEADS_TEST_EMBEDDED_DOLT=1 CGO_ENABLED=1 go test -tags "$TAGS" -v \`, + why: "conformance_script_test.go execs conformance.sh with a fake `go` first on PATH", + }, + { + fn: "", + snippet: `CGO_ENABLED=1 go test -tags "$TAGS e2e" -timeout 10m ./test/conformance/`, + why: "same fake-go-first-on-PATH gate as Tier 1 above", + }, + { + fn: "", + snippet: `go test -tags "$TAGS" -timeout=40m ./internal/httpclient/`, + why: "same fake-go-first-on-PATH gate as Tier 1 above", + }, + }, + "scripts/release.sh": { + { + fn: "", + snippet: `BD_CMD=(go run -tags gms_pure_go ./cmd/bd)`, + why: "only reached when none of BD, a `bd` on PATH, or $REPO_ROOT/bd resolves the " + + "repo formula; release_script_test.go's fixtures always place a fake `bd` that " + + "does, on PATH, so this line is never reached by the test's own dataflow", + }, + }, +} + +// shellHostToolVerbs are the `go` subcommands this policy treats as a real +// exec rather than incidental prose (e.g. "go.mod", "go$pinned"). "tool" is +// included for `go tool ` (review point 2). +var shellHostToolVerbs = regexp.MustCompile(`\bgo(?:\s+)(env|build|run|version|vet|mod|install|test|fmt|list|generate|get|work|doc|bug|tool)\b`) + +// shellGofmtExec matches a bare `gofmt` invoked as its own shell word -- +// bounded by whitespace/`;`/`|`/`&`/`(`/“ ` “/line-start/line-end on both +// sides -- so it does not false-positive on an identifier or filename that +// merely contains "gofmt" as a substring, such as "gofmt-bin.sh" or a +// variable named "$GOFMT_BIN" (review point 2's "misses gofmt" plus the +// matching false-positive risk a naive `\bgofmt\b` fix would introduce: that +// pattern alone DOES match inside "gofmt-bin.sh", since `-` is a non-word +// character and `\b` only requires a word/non-word transition). +var shellGofmtExec = regexp.MustCompile("(^|[\\s;|&(`])gofmt([\\s;|&)`]|$)") + +// shellToolLookup matches a `command -v`/`which`/`type` lookup of go or +// gofmt, which is not itself an exec of the tool. +var shellToolLookup = regexp.MustCompile(`\b(command\s+-v|which|type)\s+(go|gofmt)\b`) + +// heredocStart matches a `<<` (optionally `<<-`) redirection naming its +// terminator, with or without quotes: `< 0; j++ { + switch line[j] { + case '(': + depth++ + case ')': + depth-- + } + } + spans = append(spans, [2]int{i, j}) + i = j - 1 + } + return spans +} + +// addNonSubSpans appends the portion(s) of [start,end) that fall outside +// every span in cmdSub to spans, so a command substitution nested inside a +// quoted string is not itself treated as quoted text. +func addNonSubSpans(spans *[][2]int, start, end int, cmdSub [][2]int) { + cur := start + for _, s := range cmdSub { + if s[1] <= cur || s[0] >= end { + continue + } + if s[0] > cur { + *spans = append(*spans, [2]int{cur, s[0]}) + } + if s[1] > cur { + cur = s[1] + } + } + if cur < end { + *spans = append(*spans, [2]int{cur, end}) + } +} + +func insideSpan(spans [][2]int, pos int) bool { + for _, s := range spans { + if pos >= s[0] && pos < s[1] { + return true + } + } + return false +} + +// shellDashC matches a `bash -c`/`sh -c`/`zsh -c` invocation up through the +// opening quote of its command-string argument. +var shellDashC = regexp.MustCompile(`\b(?:bash|sh|zsh)\b(?:\s+-[A-Za-z]+)*\s+-c\s+`) + +// dashCCodeSpans returns the [start,end) span of each quoted command string +// immediately following a bash/sh/zsh -c on line. Unlike an ordinary quoted +// string, this one really executes -- `bash -c 'go build -o out ./cmd/bd'` +// really execs `go build` -- so quotedSpans' default treatment of quoted +// text as message/pattern prose, not a command, must not apply to it (review: +// "bash -c 'go build' regressed" -- an earlier revision of this scanner +// recognized this shape, lost when quotedSpans' quote-aware skip was added). +func dashCCodeSpans(line string) [][2]int { + var spans [][2]int + for _, loc := range shellDashC.FindAllStringIndex(line, -1) { + i := loc[1] + if i >= len(line) { + continue + } + quote := line[i] + if quote != '\'' && quote != '"' { + continue + } + j := i + 1 + for j < len(line) && line[j] != quote { + j++ + } + if j < len(line) { + spans = append(spans, [2]int{i, j + 1}) + } + } + return spans +} + +// quoted reports whether pos lies in a quoted span that is genuinely message +// text, not a bash/sh/zsh -c command string that happens to be quoted +// (dashCCodeSpans overrides quotedSpans for exactly that case). +func quoted(spans, codeSpans [][2]int, pos int) bool { + return insideSpan(spans, pos) && !insideSpan(codeSpans, pos) +} + +// findShellHostToolExecs scans src (one shell script's contents) for bare +// `go `/`gofmt` lines, skipping comments, heredoc bodies, `command -v` +// style lookups, and quoted message/pattern text. It reports every hit +// regardless of any gate or the allowlist -- callers apply those. Each hit +// also records its enclosing function name (empty at top level) and an exact +// snippet of the matched line, for shellExecAllowlist and shellLineGating. +func findShellHostToolExecs(src string) []shellExecHit { + var hits []shellExecHit + lines := strings.Split(src, "\n") + scanner := bufio.NewScanner(strings.NewReader(src)) + lineno := 0 + inHeredoc := false + heredocDelim := "" + currentFn := "" + for scanner.Scan() { + lineno++ + line := scanner.Text() + if inHeredoc { + if strings.TrimSpace(line) == heredocDelim { + inHeredoc = false + } + continue + } + trimmed := strings.TrimSpace(line) + if strings.HasPrefix(trimmed, "#") { + continue + } + if fn, ok := matchFuncStart(trimmed); ok { + currentFn = fn + } else if currentFn != "" && (line == "}" || line == ")") { + // An unindented closing brace/paren alone on a line ends the + // function, mirroring matchFuncStart's own column-0 anchoring: + // every function body line in this policy's scope is indented. + currentFn = "" + } + if m := heredocStart.FindStringSubmatch(line); m != nil { + inHeredoc = true + heredocDelim = m[2] + // The redirection line itself (e.g. `cat >&2 <<'EOF'`) is still + // scanned below; only what follows is heredoc body. + } + spans := quotedSpans(line) + codeSpans := dashCCodeSpans(line) + + for _, m := range shellHostToolVerbs.FindAllStringSubmatchIndex(line, -1) { + if quoted(spans, codeSpans, m[0]) { + continue + } + hits = append(hits, shellExecHit{line: lineno, fn: currentFn, snippet: lines[lineno-1], verb: line[m[2]:m[3]]}) + } + if shellToolLookup.MatchString(line) { + continue + } + for _, m := range shellGofmtExec.FindAllStringSubmatchIndex(line, -1) { + start := strings.Index(line[m[0]:m[1]], "gofmt") + m[0] + if quoted(spans, codeSpans, start) { + continue + } + hits = append(hits, shellExecHit{line: lineno, fn: currentFn, snippet: lines[lineno-1], verb: ""}) + } + } + return hits +} + +// shellFuncStart matches a shell function definition's opening line, either +// style this repo uses: `name() {` or `name() (`. +var shellFuncStart = regexp.MustCompile(`^([A-Za-z_][A-Za-z0-9_]*)\s*\(\)\s*[{(]\s*$`) + +func matchFuncStart(trimmed string) (string, bool) { + m := shellFuncStart.FindStringSubmatch(trimmed) + if m == nil { + return "", false + } + return m[1], true +} + +// condVarCheck matches a condition fragment testing one of TEST_SRCDIR or a +// shellBazelSetVars entry: `-n "${VAR:-...}"`, `-z "${VAR:-...}"`, +// `-x "$VAR"`/`-x "${VAR}"`, or `${VAR:?...}`. It reports the variable name, +// the operator ("-n", "-z" or "-x"; ":?" is normalized to "-n", since a +// required-nonempty substitution has the same "true/reached implies set" +// polarity as an explicit -n check), and whether anything matched. +var condVarCheckN = regexp.MustCompile(`-n\s+"\$\{?(\w+)(:-[^}]*)?\}?"`) +var condVarCheckZ = regexp.MustCompile(`-z\s+"\$\{?(\w+)(:-[^}]*)?\}?"`) +var condVarCheckX = regexp.MustCompile(`-x\s+"\$\{?(\w+)(:-[^}]*)?\}?"`) +var condVarRequired = regexp.MustCompile(`\$\{(\w+):\?`) + +// shellVarAlias matches a simple default-value assignment, `VAR="${OTHER:-...}"` +// or `VAR="${OTHER}"`, the pattern every shellPolicyScope script in this +// policy's scope uses to copy a Bazel-set variable (shellBazelSetVars) into a +// local name before testing it, e.g. .github/scripts/*-test-shard.sh's +// `CMD_BINARY="${BEADS_TEST_CMD_BINARY:-/tmp/bd-cmd-test}"`. +var shellVarAlias = regexp.MustCompile(`^(\w+)=\"\$\{(\w+)(:-[^}]*)?\}\"$`) + +// shellVarAliases returns a map from a script-local variable name to the +// Bazel-set variable name (a shellBazelSetVars key) it was assigned from via +// a `VAR="${BAZEL_VAR:-default}"` default-value assignment, so +// bazelVarPolarity can recognize a later `-x "$VAR"`/`-n "$VAR"` test as +// referring, transitively, to the Bazel-set variable. +func shellVarAliases(src string) map[string]string { + aliases := map[string]string{} + for _, raw := range strings.Split(src, "\n") { + trimmed := strings.TrimSpace(raw) + m := shellVarAlias.FindStringSubmatch(trimmed) + if m == nil { + continue + } + if shellBazelSetVars[m[2]] { + aliases[m[1]] = m[2] + } + } + return aliases +} + +// bazelVarPolarity reports whether cond contains a recognized +// TEST_SRCDIR/shellBazelSetVars check and, if so, whether the condition being +// TRUE implies the script is running under Bazel (bazelWhenTrue), mirroring +// host_tool_exec_policy_test.go's condPolarity. A condition this function +// does not recognize at all (including anything containing `||`, which this +// line-based heuristic does not attempt to reason about) reports matched = +// false, so callers never treat an unrecognized condition as either kind of +// gate. +func bazelVarPolarity(cond string, aliases map[string]string) (bazelWhenTrue bool, matched bool) { + if strings.Contains(cond, "||") { + return false, false + } + isBazelVar := func(name string) bool { + if name == "TEST_SRCDIR" || shellBazelSetVars[name] { + return true + } + return shellBazelSetVars[aliases[name]] + } + if m := condVarRequired.FindStringSubmatch(cond); m != nil && isBazelVar(m[1]) { + return true, true + } + if m := condVarCheckN.FindStringSubmatch(cond); m != nil && isBazelVar(m[1]) { + return true, true + } + if m := condVarCheckX.FindStringSubmatch(cond); m != nil && isBazelVar(m[1]) { + return true, true + } + if m := condVarCheckZ.FindStringSubmatch(cond); m != nil && isBazelVar(m[1]) { + return false, true + } + return false, false +} + +// ifFrame tracks one open if/elif/else block for shellLineGating's per-branch +// containment tracking (review point 1): the shell analogue of +// host_tool_exec_policy_test.go's within()/condPolarity-driven *ast.IfStmt +// walk, done textually over if/then/elif/else/fi keywords instead of an AST. +type ifFrame struct { + matched bool // condVarPolarity recognized this frame's own condition + bodySafe bool // lines directly in this frame's active branch never run under Bazel + thenBazelWhenTrue bool // the (last-set) condition's polarity: true implies under-Bazel + inElse bool + thenHasAbort bool // an abort statement appeared directly in the then-branch +} + +// shellLineGating replaces shellFileIsGated's whole-file, position-agnostic +// TEST_SRCDIR check (review point 1's bug: it exempted an entire file on any +// TEST_SRCDIR mention, anywhere, including in a comment or an unrelated +// branch). It walks src tracking if/elif/else/fi nesting and shell function +// boundaries, and returns the set of line numbers it can prove run only when +// NOT under Bazel -- either because they are lexically inside a branch whose +// condition's truth implies off-Bazel (containment style), or because they +// come after an if-block, in the same function, whose condition implies +// on-Bazel and which unconditionally returns/exits (abort style, the same +// two styles host_tool_exec_policy_test.go's gated() recognizes for Go). +// +// This is deliberately conservative and line-based, not a real shell parser: +// an unrecognized condition (including any `||`) leaves the surrounding code +// ungated rather than guessing, and `elif` is treated as if it reset to a +// fresh `if` rather than ANDing in the negation of earlier branches in the +// same chain (none of this policy's real call sites use elif alongside a +// TEST_SRCDIR/shellBazelSetVars condition, so that simplification does not +// currently cost any precision; a future case that needs it should extend +// this rather than work around it). +func shellLineGating(src string) map[int]bool { + safe := map[int]bool{} + lines := strings.Split(src, "\n") + aliases := shellVarAliases(src) + var stack []*ifFrame + abortSafeFrom := -1 // line after which abort-style safety applies, -1 if none + inFunc := false + for i, raw := range lines { + lineno := i + 1 + trimmed := strings.TrimSpace(raw) + if _, ok := matchFuncStart(trimmed); ok { + stack = nil + abortSafeFrom = -1 + inFunc = true + } else if inFunc && (raw == "}" || raw == ")") { + // An unindented closing brace/paren alone on a line ends the + // function (matchFuncStart's own column-0 anchoring assumes + // every function body line in this policy's scope is indented, + // the same convention findShellHostToolExecs's currentFn + // tracking uses). Without this reset, abort-style safety + // computed inside one function (e.g. upgrade-smoke-test.sh's + // build_candidate) leaked into whatever top-level code follows + // the function, long after it closed (review: "the abort/skip + // gate state isn't reset at function end"). + stack = nil + abortSafeFrom = -1 + inFunc = false + } + switch { + case strings.HasPrefix(trimmed, "if ") || trimmed == "if": + cond := extractCond(trimmed, "if") + bazelWhenTrue, matched := bazelVarPolarity(cond, aliases) + frame := &ifFrame{matched: matched, thenBazelWhenTrue: bazelWhenTrue, bodySafe: matched && !bazelWhenTrue} + stack = append(stack, frame) + case strings.HasPrefix(trimmed, "elif "): + if len(stack) > 0 { + cond := extractCond(trimmed, "elif") + bazelWhenTrue, matched := bazelVarPolarity(cond, aliases) + top := stack[len(stack)-1] + top.matched = matched + top.thenBazelWhenTrue = bazelWhenTrue + top.bodySafe = matched && !bazelWhenTrue + top.thenHasAbort = false + } + case trimmed == "else": + if len(stack) > 0 { + top := stack[len(stack)-1] + top.inElse = true + if top.matched { + top.bodySafe = top.thenBazelWhenTrue + } + } + case trimmed == "fi": + if len(stack) > 0 { + top := stack[len(stack)-1] + stack = stack[:len(stack)-1] + // Abort-style (review point 1/6): if this if-statement's + // then-branch condition implied under-Bazel and the + // then-branch unconditionally aborted, later lines in the + // same function are reached only when that condition was + // false, i.e. only off-Bazel. + if top.matched && top.thenBazelWhenTrue && top.thenHasAbort { + abortSafeFrom = lineno + } + } + } + if isAbortStmt(trimmed) && len(stack) > 0 { + top := stack[len(stack)-1] + if !top.inElse { + top.thenHasAbort = true + } + } + // A line's safety is the OR of every enclosing frame's current + // branch being safe, or lying after a same-function abort. + lineSafe := abortSafeFrom >= 0 && lineno > abortSafeFrom + for _, f := range stack { + if f.bodySafe { + lineSafe = true + } + } + if lineSafe { + safe[lineno] = true + } + } + return safe +} + +var condExtract = regexp.MustCompile(`^(?:if|elif)\s+(.*?)\s*;?\s*then\s*$`) + +// extractCond pulls the condition text out of an `if cond; then` or +// `if cond` line (the `then` may be on the next line, outside this +// function's view; extractCond returns the whole remainder in that case, +// which is fine since bazelVarPolarity only looks for substrings). +func extractCond(trimmed, kw string) string { + if m := condExtract.FindStringSubmatch(trimmed); m != nil { + return m[1] + } + return strings.TrimPrefix(trimmed, kw+" ") +} + +var abortStmt = regexp.MustCompile(`^(return|exit|continue|break)\b`) + +func isAbortStmt(trimmed string) bool { + return abortStmt.MatchString(trimmed) +} + +func shellMatchAllowlist(entries []shellAllowlistEntry, fn, snippet string) bool { + for _, e := range entries { + if e.fn == fn && strings.TrimSpace(e.snippet) == strings.TrimSpace(snippet) { + return true + } + } + return false +} + +func TestShellHostToolExecPolicyScan(t *testing.T) { + t.Run("ungated bare go build is reported", func(t *testing.T) { + src := "#!/usr/bin/env bash\ngo build -o out ./cmd/bd\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].line != 2 || hits[0].verb != "build" { + t.Fatalf("got %+v, want one hit at line 2 (build)", hits) + } + if shellLineGating(src)[2] { + t.Fatal("fixture has no gate; line 2 must not be reported safe") + } + }) + + t.Run("go tool is reported (review point 2)", func(t *testing.T) { + src := "#!/usr/bin/env bash\ngo tool nm ./bd\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].verb != "tool" { + t.Fatalf("got %+v, want one `tool` hit", hits) + } + }) + + t.Run("bare gofmt exec is reported (review point 2)", func(t *testing.T) { + src := "#!/usr/bin/env bash\ngofmt -l .\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 { + t.Fatalf("got %+v, want one gofmt hit", hits) + } + }) + + t.Run("gofmt as a substring of a filename is not reported", func(t *testing.T) { + src := "#!/usr/bin/env bash\nsource scripts/ci/gofmt-bin.sh\nGOFMT_BIN=/tmp/x\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (gofmt-bin.sh/GOFMT_BIN are not a `gofmt` exec)", hits) + } + }) + + t.Run("command -v / which / type lookups are not reported", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "if ! command -v go >/dev/null 2>&1; then exit 1; fi\n" + + "which gofmt >/dev/null\n" + + "type go >/dev/null\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (tool-presence lookups, not execs)", hits) + } + }) + + t.Run("comment lines are not scanned", func(t *testing.T) { + src := "#!/usr/bin/env bash\n# go build is mentioned here only in prose\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits", hits) + } + }) + + t.Run("heredoc bodies are not scanned", func(t *testing.T) { + src := "#!/usr/bin/env bash\ncat >&2 <<'EOF'\nrun `go build|test|run` yourself\nEOF\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (heredoc body)", hits) + } + }) + + t.Run("arithmetic left-shift is not mistaken for a heredoc (review point 2)", func(t *testing.T) { + // A bogus heredoc-start match here would swallow the rest of the + // script (including the real `go build` below) as heredoc body. + src := "#!/usr/bin/env bash\nx=$(( 1 << 2 ))\ngo build -o out ./cmd/bd\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].line != 3 { + t.Fatalf("got %+v, want one hit at line 3 (arithmetic shift must not start a heredoc)", hits) + } + }) + + t.Run("a here-string is not mistaken for a heredoc", func(t *testing.T) { + src := "#!/usr/bin/env bash\ncat <<< \"go build fake\"\ngo build -o out ./cmd/bd\n" + hits := findShellHostToolExecs(src) + found3 := false + for _, h := range hits { + if h.line == 3 { + found3 = true + } + } + if !found3 { + t.Fatalf("got %+v, want a hit at line 3 (here-string must not start a heredoc)", hits) + } + }) + + t.Run("printf and echo quoted message text are not scanned", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "printf 'error: unsupported go install module path\\n' >&2\n" + + "echo \"run go version to check\"\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (printf/echo text)", hits) + } + }) + + t.Run("a real exec joined onto an echo line by && is still reported (review point 2)", func(t *testing.T) { + // A line-wide "contains echo" skip (the previous scanner's bug) would + // hide this real second command. + src := "#!/usr/bin/env bash\n" + + "echo \"building\" && go build -o out ./cmd/bd\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].line != 2 { + t.Fatalf("got %+v, want one hit at line 2 (go build after && is a real exec)", hits) + } + }) + + t.Run("git grep search patterns are not scanned", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "git grep -n -E 'go install github\\.com/x/y@latest' -- . || true\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (git grep pattern)", hits) + } + }) + + t.Run("command substitution exec is reported", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + `if [[ "$(go env GOVERSION 2>/dev/null)" == "go1.25" ]]; then` + "\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].verb != "env" { + t.Fatalf("got %+v, want one `env` hit", hits) + } + }) + + t.Run("dolt is never flagged", func(t *testing.T) { + src := "#!/usr/bin/env bash\ndolt sql -q 'select 1'\n" + if hits := findShellHostToolExecs(src); len(hits) != 0 { + t.Fatalf("got %+v, want no hits (dolt is out of scope; see package comment)", hits) + } + }) + + t.Run("TEST_SRCDIR gates only its own branch, not the whole file (review point 1)", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "if [[ -z \"${TEST_SRCDIR:-}\" ]]; then\n" + + " go build -o out ./cmd/bd\n" + + "fi\n" + + "go build -o out2 ./cmd/bd\n" + gating := shellLineGating(src) + if !gating[3] { + t.Fatal("line 3 is inside the off-Bazel-only branch; must be reported safe") + } + if gating[5] { + t.Fatal("line 5 is NOT inside any TEST_SRCDIR branch; a whole-file gate would " + + "wrongly report it safe (review point 1's bug)") + } + }) + + t.Run("TEST_SRCDIR-true branch is not safe, its else is", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "if [[ -n \"${TEST_SRCDIR:-}\" ]]; then\n" + + " go build -o out ./cmd/bd\n" + + "else\n" + + " go build -o out2 ./cmd/bd\n" + + "fi\n" + gating := shellLineGating(src) + if gating[3] { + t.Fatal("line 3 runs only under Bazel; must not be reported safe") + } + if !gating[5] { + t.Fatal("line 5 runs only off-Bazel (the else of a TEST_SRCDIR-set check); must be safe") + } + }) + + t.Run("abort-style: unset-check that returns makes the rest of the function safe", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "build_candidate() {\n" + + " if [ -n \"${CANDIDATE_BIN:-}\" ] && [ -x \"${CANDIDATE_BIN}\" ]; then\n" + + " echo \"$CANDIDATE_BIN\"\n" + + " return\n" + + " fi\n" + + " go build -o \"$candidate\" ./cmd/bd\n" + + "}\n" + gating := shellLineGating(src) + if !gating[7] { + t.Fatal("line 7 is reached only when CANDIDATE_BIN is unset, i.e. off-Bazel; must be safe") + } + }) + + t.Run("abort-style safety does not leak into the next function (review point 1)", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "build_candidate() {\n" + + " if [ -n \"${CANDIDATE_BIN:-}\" ]; then\n" + + " return\n" + + " fi\n" + + " true\n" + + "}\n" + + "other_fn() {\n" + + " go build -o out ./cmd/bd\n" + + "}\n" + if shellLineGating(src)[9] { + t.Fatal("line 9 is in a different function; must not inherit the previous " + + "function's abort-style safety") + } + }) + + t.Run("abort-style safety does not leak into top-level code after the function ends (mutation check, review: upgrade-smoke-test.sh)", func(t *testing.T) { + // The previous test only pinned that a SECOND function doesn't + // inherit the gate (matchFuncStart resets state when a new function + // starts). This pins the bug the review actually found: top-level + // code AFTER a function closes, with no new function definition in + // between, must not inherit abort-style safety computed inside that + // function either -- the shape scripts/upgrade-smoke-test.sh uses + // (a top-level `go build` after build_candidate()). + src := "#!/usr/bin/env bash\n" + + "build_candidate() {\n" + + " if [ -n \"${CANDIDATE_BIN:-}\" ]; then\n" + + " return\n" + + " fi\n" + + " true\n" + + "}\n" + + "go build -o out ./cmd/bd\n" + if shellLineGating(src)[8] { + t.Fatal("line 8 is top-level code after the function closed; must not inherit " + + "build_candidate's abort-style safety") + } + }) + + t.Run("bash -c 'go build' is detected (mutation check, review regression)", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "bash -c 'go build -o out ./cmd/bd'\n" + hits := findShellHostToolExecs(src) + if len(hits) != 1 || hits[0].line != 2 || hits[0].verb != "build" { + t.Fatalf("got %+v, want one `build` hit at line 2: bash -c's quoted argument really "+ + "execs go build, it is not message text", hits) + } + if shellLineGating(src)[2] { + t.Fatal("fixture has no gate; line 2 must not be reported safe") + } + }) + + t.Run("an alias of a Bazel-set var still gates (review point 1, shard-script pattern)", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "CMD_BINARY=\"${BEADS_TEST_CMD_BINARY:-/tmp/bd-cmd-test}\"\n" + + "if [ -x \"$CMD_BINARY\" ]; then\n" + + " exec \"$CMD_BINARY\" -test.v\n" + + "else\n" + + " exec go test -tags=gms_pure_go ./cmd/bd/\n" + + "fi\n" + if !shellLineGating(src)[6] { + t.Fatal("line 6 runs only when the Bazel-set BEADS_TEST_CMD_BINARY-derived " + + "binary is missing, i.e. off-Bazel; must be safe") + } + }) + + t.Run("an unrecognized condition is not treated as a gate", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "if [ \"$FOO\" = \"1\" ]; then\n" + + " go build -o out ./cmd/bd\n" + + "fi\n" + if shellLineGating(src)[3] { + t.Fatal("an unrelated condition must not be treated as a Bazel gate") + } + }) + + t.Run("a condition with || is not treated as a gate (conservative)", func(t *testing.T) { + src := "#!/usr/bin/env bash\n" + + "if [[ -z \"${TEST_SRCDIR:-}\" ]] || [[ -n \"${FORCE_LOCAL:-}\" ]]; then\n" + + " go build -o out ./cmd/bd\n" + + "fi\n" + if shellLineGating(src)[3] { + t.Fatal("a disjunctive condition is not reasoned about; must not be reported safe") + } + }) +} + +// TestNoUngatedBareGoInBazelReachedShellScripts scans shellPolicyScope and +// fails on an unallowlisted, ungated bare `go`/`gofmt` exec. See the package +// comment for scope and the detection tradeoffs. +func TestNoUngatedBareGoInBazelReachedShellScripts(t *testing.T) { + root := sourceRepoRoot(t) + seenAllow := map[string]map[string]bool{} // file -> "fn\x00snippet" -> true + for _, rel := range shellPolicyScope { + path := filepath.Join(root, filepath.FromSlash(rel)) + data, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s: %v (shellPolicyScope entry no longer exists; update it)", rel, err) + } + src := string(data) + gating := shellLineGating(src) + for _, hit := range findShellHostToolExecs(src) { + if gating[hit.line] { + continue + } + if shellMatchAllowlist(shellExecAllowlist[rel], hit.fn, hit.snippet) { + if seenAllow[rel] == nil { + seenAllow[rel] = map[string]bool{} + } + seenAllow[rel][hit.fn+"\x00"+strings.TrimSpace(hit.snippet)] = true + continue + } + tool := "go " + hit.verb + if hit.verb == "" { + tool = "gofmt" + } + t.Errorf("%s:%d: ungated bare `%s`: under `bazel test` the hermetic-first PATH "+ + "may lack a host toolchain, so this can exit 127 on some workers (f4-spec.md "+ + "§9); gate it on TEST_SRCDIR/a shellBazelSetVars entry, resolve it through a "+ + "Bazel-wired env var, or add a justified shellExecAllowlist entry\n\t%s", + rel, hit.line, tool, strings.TrimSpace(hit.snippet)) + } + } + for rel, entries := range shellExecAllowlist { + for _, e := range entries { + if !seenAllow[rel][e.fn+"\x00"+strings.TrimSpace(e.snippet)] { + t.Errorf("shellExecAllowlist entry %s (fn=%q, snippet=%q) no longer matches "+ + "an ungated bare exec; remove it", rel, e.fn, e.snippet) + } + } + } +} + +// TestShellPolicyScopeFilesExist is a guard against shellPolicyScope rotting +// silently: repoFiles-style tests elsewhere skip missing files quietly, but +// this list is hand-maintained, so a missing entry should fail loudly instead +// of just scanning fewer files than the comment above claims. +func TestShellPolicyScopeFilesExist(t *testing.T) { + root := sourceRepoRoot(t) + for _, rel := range shellPolicyScope { + if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(rel))); err != nil { + t.Errorf("shellPolicyScope entry %s: %v", rel, err) + } + } + if len(shellPolicyScope) < 10 { + t.Fatalf("shellPolicyScope has only %d entries; did a bulk edit truncate it?", len(shellPolicyScope)) + } +} + +// TestShellPolicyScopeCoversBazelShellData (review point 3's mechanical +// backstop: a regex walk over every BUILD.bazel looking for a literal `.sh` +// path under scripts/ or .github/scripts/ in a data/srcs list) was removed +// (gastownhall/beads#7485 review item 4). Its regex could not parse a +// `//pkg:file.sh` label, a `.bzl` file, or a glob() -- the three real +// reference shapes this repo's BUILD files actually use for most scripts in +// shellPolicyScope above -- so even with //:repo_other_files (every tracked +// BUILD/.bzl/script file) as data, it would not have caught +// check-versions.sh, scripts/test.sh, scripts/conformance.sh or +// scripts/release.sh going missing from shellPolicyScope; a hand reviewer +// citing each script's BUILD wiring (as the comment above does) is what +// actually found them. Paying //:repo_other_files' cost on every target that +// used it (758 extra input files, a measured 91.5%->96% rise in this +// package's cache-rerun rate across the whole repo) for a check that was +// already demonstrably incomplete was not worth it; there is no `bazel +// query`-free static check of Starlark label/glob references this policy can +// do honestly, so shellPolicyScope's accuracy depends on the hand curation +// above, same as the run_check.sh-as-launcher family already did. diff --git a/scripts/repochecks/BUILD.bazel b/scripts/repochecks/BUILD.bazel index 7940a4538..7e476796f 100644 --- a/scripts/repochecks/BUILD.bazel +++ b/scripts/repochecks/BUILD.bazel @@ -355,6 +355,17 @@ sh_test( }, ) +# Readable by //scripts:go_sources_test's host_tool_shell_policy_test.go, +# which scans every sh_test PROGRAM/srcs this package runs under Bazel for +# ungated bare host-`go` execs (f4-spec.md §9). +exports_files( + [ + "run_check.sh", + "types_gen_drift_test.sh", + ], + visibility = ["//scripts:__pkg__"], +) + # --- begin repo_files (managed by tools/bazel/go_srcs.py; run `make bazel-sync`) --- filegroup(