fix(hooks): apply host key identity to Git environment filters (#6321)
* fix(hooks): use host key identity when filtering child environments Use execenv for the chained-hook guard and export Git routing filters. Keep their existing per-consumer denylist and valueless GIT_CONFIG prefix behavior while matching Windows os/exec key identity. Preserve the input slice and exercise mixed case, Unicode, malformed keys and value handling. The worktree-removal site from issue6198 remains owned by PR5623. This stack depends on PR5672 and must not close6198 before both parents land. * test(hooks): exercise both host key policies through actual filters (saa-cgh.89) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * refactor(hooks): share environment entry parsing and clarify serial seam (saa-cgh.89) Agent-Signature: unknown-model/unknown-reasoning on behalf of Ewen Cuthiell * build(bazel): list hook_env_test.go in cmd/bd go_test srcs The PR's new cmd/bd/hook_env_test.go is untagged but absent from the gazelle-managed go_test srcs list, so its two pins are invisible to `bazel test //cmd/bd:bd_test`. Both review lanes held this at minor on the grounds that untagged srcs drift is tolerated in this package: three untagged test files (config_leaked_secret_test.go, init_git_bootstrap_routing_test.go, init_role_environment_test.go) are likewise unlisted at this PR's base. That precedent is gone at the merge target. At origin/main147047e15the census is 23 omissions, all 23 //go:build-gated, zero untagged -- main landed all three rows -- so this file would be the merge target's only untagged omission. Two advisory steps in .github/workflows/bazel.yml (absent at this base, present at the merge target) act on that: `Go test equivalence` (tools/bazel/equivalence.py, which fails on any Go test `go test` runs and Bazel did not, including "missing target"; neither new test name is in equivalence_allowlist.txt) and the `make bazel-sync` drift check. The job's `if` includes head.repo.fork == true, so it always runs for this fork PR. Generated by `bazel run //:gazelle`, which is what the drift step compares the tree against. Gazelle also emitted the three base-owned cmd/bd rows and an internal/notion change; all four are already present at the merge target and were reverted so they merge from main's side. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> * build(bazel): sync scripts go_test srcs (adopted base debt, not this PR's change) scripts/upgrade_smoke_loop_test.go is untagged and absent from the gazelle-managed go_test srcs in scripts/BUILD.bazel, which reds the advisory Bazel lane two ways: `Go test equivalence` reports `TestUpgradeSmokeMultiVersionDispatch` as a test `go test` runs and Bazel did not, and the `BUILD files in sync` step's own `make bazel-sync` adds the row and then fails on the drift. This is NOT introduced by this PR, which touches zero files under scripts/. The gap is identical at this PR's basefc2351a51cand at HEAD, and the Bazel lane already fails on main's own pushes atfc2351a51c,147047e15,90f8e42f1and8c889c1f2for this same reason -- it currently reds for every PR in the repo. Carried here because main is unprotected, so the pre-approval CI gate takes the strict path in which any failing run blocks, with no base-preexisting bypass. Generated by `bazel run //:gazelle`; the local drift is byte-identical to the drift CI reported (9345584->5dfc23e). Kept as its own commit so the contributor's hook-env work stays separable from adopted base debt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
6daa06244b
commit
13150ba360
4 changed files with 106 additions and 31 deletions
|
|
@ -754,6 +754,7 @@ go_test(
|
|||
"history_full_hash_test.go",
|
||||
"history_proxied_integration_test.go",
|
||||
"history_remote_frontdoor_matrix_test.go",
|
||||
"hook_env_test.go",
|
||||
"hooks_embedded_test.go",
|
||||
"hooks_exec_composition_test.go",
|
||||
"hooks_export_staged_deletion_test.go",
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
|
|
@ -18,6 +19,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/execenv"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
|
@ -1072,6 +1074,10 @@ func gitIndexLockPath(path string, env []string) (string, error) {
|
|||
return filepath.Join(gitDir, "index.lock"), nil
|
||||
}
|
||||
|
||||
// hookEnvGOOS lets both hook filters exercise either key policy on every test host.
|
||||
// Process-wide seam: tests that swap it must not run in parallel.
|
||||
var hookEnvGOOS = runtime.GOOS
|
||||
|
||||
// scrubGitHookEnv returns env with the GIT_* variables that can poison
|
||||
// git's repo/worktree auto-discovery or object-store resolution removed,
|
||||
// so git falls back to auto-discovery from cwd. The scrub is
|
||||
|
|
@ -1090,34 +1096,28 @@ func gitIndexLockPath(path string, env []string) (string, error) {
|
|||
// GIT_CONFIG namespace, which includes _COUNT, _KEY_n, _VALUE_n,
|
||||
// _GLOBAL, _SYSTEM, _NOSYSTEM, and the legacy GIT_CONFIG itself.
|
||||
func scrubGitHookEnv(env []string) []string {
|
||||
return scrubGitHookEnvForOS(env, hookEnvGOOS)
|
||||
}
|
||||
|
||||
func scrubGitHookEnvForOS(env []string, goos string) []string {
|
||||
// The GIT_CONFIG prefix (no trailing "=") is intentional: it matches
|
||||
// GIT_CONFIG=, GIT_CONFIG_COUNT=, GIT_CONFIG_KEY_n=, GIT_CONFIG_VALUE_n=,
|
||||
// GIT_CONFIG_PARAMETERS=, GIT_CONFIG_GLOBAL=, GIT_CONFIG_SYSTEM=, and
|
||||
// GIT_CONFIG_NOSYSTEM= — the whole family — in one entry. No standard
|
||||
// git env var starts with GIT_CONFIG that we want to preserve.
|
||||
prefixes := []string{
|
||||
"GIT_DIR=",
|
||||
"GIT_WORK_TREE=",
|
||||
"GIT_INDEX_FILE=",
|
||||
"GIT_COMMON_DIR=",
|
||||
"GIT_PREFIX=",
|
||||
"GIT_OBJECT_DIRECTORY=",
|
||||
"GIT_ALTERNATE_OBJECT_DIRECTORIES=",
|
||||
"GIT_CEILING_DIRECTORIES=",
|
||||
"GIT_DISCOVERY_ACROSS_FILESYSTEM=",
|
||||
"GIT_CONFIG",
|
||||
}
|
||||
out := make([]string, 0, len(env))
|
||||
for _, e := range env {
|
||||
skip := false
|
||||
for _, p := range prefixes {
|
||||
if strings.HasPrefix(e, p) {
|
||||
skip = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skip {
|
||||
out = append(out, e)
|
||||
cleaned := execenv.WithoutForOS(env, goos,
|
||||
"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR",
|
||||
"GIT_PREFIX", "GIT_OBJECT_DIRECTORY", "GIT_ALTERNATE_OBJECT_DIRECTORIES",
|
||||
"GIT_CEILING_DIRECTORIES", "GIT_DISCOVERY_ACROSS_FILESYSTEM",
|
||||
)
|
||||
// Without owns a fresh slice, so this second filter cannot mutate env.
|
||||
out := cleaned[:0]
|
||||
for _, entry := range cleaned {
|
||||
// Prefix policy also drops valueless GIT_CONFIG entries. Without
|
||||
// intentionally preserves valueless exact keys such as GIT_DIR.
|
||||
key := execenv.EntryKey(entry)
|
||||
if !execenv.KeyHasPrefixForOS(key, "GIT_CONFIG", goos) {
|
||||
out = append(out, entry)
|
||||
}
|
||||
}
|
||||
return out
|
||||
|
|
|
|||
76
cmd/bd/hook_env_test.go
Normal file
76
cmd/bd/hook_env_test.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFilterChainedHookEnvUsesHostKeyIdentity(t *testing.T) {
|
||||
checkHookEnvForBothHosts(t, checkChainedHookEnv)
|
||||
}
|
||||
|
||||
// Callers and subtests must remain serial while they swap the process-wide seam.
|
||||
func checkHookEnvForBothHosts(t *testing.T, check func(*testing.T)) {
|
||||
t.Helper()
|
||||
for _, goos := range []string{"linux", "windows"} {
|
||||
t.Run(goos, func(t *testing.T) {
|
||||
previous := hookEnvGOOS
|
||||
hookEnvGOOS = goos
|
||||
t.Cleanup(func() { hookEnvGOOS = previous })
|
||||
check(t)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func checkChainedHookEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
env := []string{
|
||||
"BD_GIT_HOOK=canonical", "bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case",
|
||||
"BD_GIT_HOOK=unicode-case", "BD_GIT_HOOK=",
|
||||
"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1",
|
||||
"BD_GIT_HOOK", `=C:=C:\work`,
|
||||
}
|
||||
original := slices.Clone(env)
|
||||
want := []string{"BD_GıT_HOOK=distinct", "KEEP=first", "KEEP=BD_GIT_HOOK=1", "BD_GIT_HOOK", `=C:=C:\work`}
|
||||
if hookEnvGOOS != "windows" {
|
||||
want = append([]string{"bd_git_hook=mixed", "BD_GİT_HOOK=unicode-case", "BD_GIT_HOOK=unicode-case"}, want...)
|
||||
}
|
||||
if got := filterEnv(env, "BD_GIT_HOOK"); !slices.Equal(got, want) {
|
||||
t.Errorf("chained-hook environment = %q, want %q", got, want)
|
||||
}
|
||||
if !slices.Equal(env, original) {
|
||||
t.Errorf("input environment mutated: %q", env)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScrubGitHookEnvUsesHostKeyIdentity(t *testing.T) {
|
||||
checkHookEnvForBothHosts(t, checkGitHookEnv)
|
||||
}
|
||||
|
||||
func checkGitHookEnv(t *testing.T) {
|
||||
t.Helper()
|
||||
env := []string{
|
||||
"GIT_DIR=canonical", "git_dir=mixed", "GİT_DİR=unicode-case", "GIT_DıR=distinct",
|
||||
"GIT_WORK_TREE=unicode-case", "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct", "GIT_CONFIG=",
|
||||
"GIT_CONFIG_COUNT=1", "git_config_parameters=mixed", "gİt_config_count=unicode-case",
|
||||
"GIT_CONFIG_UNSET", "git_config_unset", "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1",
|
||||
"GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`,
|
||||
}
|
||||
original := slices.Clone(env)
|
||||
want := []string{"GIT_DıR=distinct"}
|
||||
if hookEnvGOOS != "windows" {
|
||||
want = append([]string{"git_dir=mixed", "GİT_DİR=unicode-case"}, want...)
|
||||
want = append(want, "GIT_WORK_TREE=unicode-case")
|
||||
}
|
||||
want = append(want, "GIT_DIſCOVERY_ACROSS_FILESYSTEM=distinct")
|
||||
if hookEnvGOOS != "windows" {
|
||||
want = append(want, "git_config_parameters=mixed", "gİt_config_count=unicode-case", "git_config_unset")
|
||||
}
|
||||
want = append(want, "KEEP=first", "KEEP=GIT_CONFIG_COUNT=1", "GIT_AUTHOR_NAME=author", "GIT_OPTIONAL_LOCKS=1", "GIT_DIR", "MALFORMED", `=C:=C:\work`)
|
||||
if got := scrubGitHookEnv(env); !slices.Equal(got, want) {
|
||||
t.Errorf("Git-hook environment = %q, want %q", got, want)
|
||||
}
|
||||
if !slices.Equal(env, original) {
|
||||
t.Errorf("input environment mutated: %q", env)
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/execenv"
|
||||
"github.com/steveyegge/beads/internal/git"
|
||||
"github.com/steveyegge/beads/internal/gitenv"
|
||||
"github.com/steveyegge/beads/internal/metrics"
|
||||
|
|
@ -2100,14 +2101,11 @@ func warnJSONLWithoutDoltRemote(reason string) {
|
|||
|
||||
// filterEnv returns a copy of env with entries matching the given key removed.
|
||||
func filterEnv(env []string, key string) []string {
|
||||
prefix := key + "="
|
||||
out := make([]string, 0, len(env))
|
||||
for _, e := range env {
|
||||
if !strings.HasPrefix(e, prefix) {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
return filterEnvForOS(env, key, hookEnvGOOS)
|
||||
}
|
||||
|
||||
func filterEnvForOS(env []string, key, goos string) []string {
|
||||
return execenv.WithoutForOS(env, goos, key)
|
||||
}
|
||||
|
||||
// runPostMergeHook runs chained hooks after merge, then runs the legacy
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue