diff --git a/app/cli/cmd/trace_init.go b/app/cli/cmd/trace_init.go index 7937bb0e6..d5f45c815 100644 --- a/app/cli/cmd/trace_init.go +++ b/app/cli/cmd/trace_init.go @@ -103,6 +103,20 @@ cannot use them with --local. You can use --require-trace with --local.`, return err } + // The harness hooks are written relative to the repository root, + // so a root that is the user's home directory would rewrite the + // agent's user-level configuration (for example + // ~/.claude/settings.json) and the hooks would load in every + // session of that agent, in any project. Refuse before asking + // anything or writing anything. + userLevel, err := trace.IsUserLevelRoot(repoRoot) + if err != nil { + return fmt.Errorf("check repository root: %w", err) + } + if userLevel { + return fmt.Errorf("refusing to initialize tracing in %q: that is a user-level directory, so the agent hooks would load in every session; run `chainloop trace init` inside a project repository instead", repoRoot) + } + // Local mode contacts no control plane, so the control-plane // identity and workflow flags are contradictions rather than extra // configuration. --require-trace is orthogonal and allowed. diff --git a/app/cli/internal/trace/userlevel.go b/app/cli/internal/trace/userlevel.go new file mode 100644 index 000000000..937cf882d --- /dev/null +++ b/app/cli/internal/trace/userlevel.go @@ -0,0 +1,88 @@ +// +// Copyright 2026 The Chainloop Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package trace + +import ( + "os" + "path/filepath" +) + +// IsUserLevelRoot reports whether root is a user-level directory: the user's +// home directory, or an agent's user-level configuration folder ($CODEX_HOME, +// $COPILOT_HOME, defaulting to ~/.codex and ~/.copilot). +// +// Trace installs agent hooks as files relative to the repository root, so a +// root that is the home directory would write the agent's user-level +// configuration (for example ~/.claude/settings.json) and the hooks would +// load in every session of that agent, in any project, instead of only in +// this repository. Callers should refuse such a root before installing +// anything. Symlinks are resolved on both sides, so a symlinked home +// directory is still caught. +func IsUserLevelRoot(root string) (bool, error) { + resolvedRoot, err := resolveDir(root) + if err != nil { + return false, err + } + + for _, candidate := range userLevelDirs() { + resolved, err := resolveDir(candidate) + if err != nil { + continue + } + if resolvedRoot == resolved { + return true, nil + } + } + + return false, nil +} + +// resolveDir returns the absolute path of dir with symlinks evaluated. When +// the path does not exist yet, EvalSymlinks fails and the cleaned absolute +// path is used instead, so callers can still compare it. +func resolveDir(path string) (string, error) { + abs, err := filepath.Abs(path) + if err != nil { + return "", err + } + if resolved, err := filepath.EvalSymlinks(abs); err == nil { + return resolved, nil + } + return abs, nil +} + +// userLevelDirs lists the directories whose contents an agent loads for every +// session: the user's home directory first, then the agent-specific home +// folders where they apply. +func userLevelDirs() []string { + home, err := os.UserHomeDir() + if err != nil || home == "" { + return nil + } + + dirs := []string{home} + if codexHome := os.Getenv("CODEX_HOME"); codexHome != "" { + dirs = append(dirs, codexHome) + } else { + dirs = append(dirs, filepath.Join(home, ".codex")) + } + if copilotHome := os.Getenv("COPILOT_HOME"); copilotHome != "" { + dirs = append(dirs, copilotHome) + } else { + dirs = append(dirs, filepath.Join(home, ".copilot")) + } + return dirs +} diff --git a/app/cli/internal/trace/userlevel_test.go b/app/cli/internal/trace/userlevel_test.go new file mode 100644 index 000000000..d820248a9 --- /dev/null +++ b/app/cli/internal/trace/userlevel_test.go @@ -0,0 +1,81 @@ +// +// Copyright 2026 The Chainloop Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package trace + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIsUserLevelRoot(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("CODEX_HOME", "") + t.Setenv("COPILOT_HOME", "") + + t.Run("home directory itself", func(t *testing.T) { + isUserLevel, err := IsUserLevelRoot(home) + require.NoError(t, err) + assert.True(t, isUserLevel) + }) + + t.Run("home directory through a symlink", func(t *testing.T) { + link := filepath.Join(t.TempDir(), "home-link") + require.NoError(t, os.Symlink(home, link)) + isUserLevel, err := IsUserLevelRoot(link) + require.NoError(t, err) + assert.True(t, isUserLevel, "a symlinked home directory is still the home directory") + }) + + t.Run("project below home", func(t *testing.T) { + project := filepath.Join(home, "work", "myrepo") + require.NoError(t, os.MkdirAll(project, 0o755)) + isUserLevel, err := IsUserLevelRoot(project) + require.NoError(t, err) + assert.False(t, isUserLevel) + }) + + t.Run("unrelated directory", func(t *testing.T) { + isUserLevel, err := IsUserLevelRoot(t.TempDir()) + require.NoError(t, err) + assert.False(t, isUserLevel) + }) + + t.Run("agent home folders", func(t *testing.T) { + codexHome := filepath.Join(t.TempDir(), "codex-home") + copilotHome := filepath.Join(t.TempDir(), "copilot-home") + t.Setenv("CODEX_HOME", codexHome) + t.Setenv("COPILOT_HOME", copilotHome) + + for _, dir := range []string{codexHome, copilotHome} { + isUserLevel, err := IsUserLevelRoot(dir) + require.NoError(t, err) + assert.True(t, isUserLevel, "%s is an agent user-level folder", dir) + } + }) + + t.Run("agent home defaults under home", func(t *testing.T) { + for _, dir := range []string{filepath.Join(home, ".codex"), filepath.Join(home, ".copilot")} { + isUserLevel, err := IsUserLevelRoot(dir) + require.NoError(t, err) + assert.True(t, isUserLevel, "%s is an agent user-level folder", dir) + } + }) +} diff --git a/app/cli/pkg/action/trace_run.go b/app/cli/pkg/action/trace_run.go index 0ff219b73..b18672419 100644 --- a/app/cli/pkg/action/trace_run.go +++ b/app/cli/pkg/action/trace_run.go @@ -104,6 +104,19 @@ func TraceRun(ctx context.Context, log zerolog.Logger, opts TraceRunOpts) error return fmt.Errorf("no trace providers selected") } + // The agent hooks are written relative to the repository root, so a root + // that is the user's home directory would rewrite the agent's user-level + // configuration (for example ~/.claude/settings.json) and the hooks would + // load in every session of that agent, in any project. Refuse before + // anything is installed, snapshotted, or run. + userLevel, err := trace.IsUserLevelRoot(opts.RepoRoot) + if err != nil { + return fmt.Errorf("check repository root: %w", err) + } + if userLevel { + return fmt.Errorf("refusing to trace in %q: that is a user-level directory, so the agent hooks would load in every session; run `chainloop trace run` inside a project repository instead", opts.RepoRoot) + } + // Local mode reaches no control plane, so it skips the credential check and // the up-front workflow creation: the session is recorded and written to // disk either way. diff --git a/app/cli/pkg/action/trace_run_test.go b/app/cli/pkg/action/trace_run_test.go index 476af109f..c004e4b03 100644 --- a/app/cli/pkg/action/trace_run_test.go +++ b/app/cli/pkg/action/trace_run_test.go @@ -16,6 +16,7 @@ package action import ( + "context" "os" "path/filepath" "testing" @@ -29,6 +30,43 @@ import ( "github.com/stretchr/testify/require" ) +// TestTraceRunRefusesUserLevelRoot covers both ways `trace run` can end up +// with the home directory as its root: run outside a git repository with the +// working directory at $HOME, and a git repository rooted at $HOME. Either +// way the agent hooks would land in the user-level configuration and load in +// every session, so the run must fail before anything is installed. +func TestTraceRunRefusesUserLevelRoot(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + + run := func(t *testing.T, store *state.Store) { + t.Helper() + err := TraceRun(context.Background(), zerolog.Nop(), TraceRunOpts{ + Store: store, + RepoRoot: home, + Providers: []string{"claude-code"}, + Command: []string{"true"}, + Local: true, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "user-level directory") + } + + t.Run("no git repository, working directory at home", func(t *testing.T) { + run(t, state.NewOutOfTreeStore(filepath.Join(t.TempDir(), "state"))) + }) + + t.Run("git repository rooted at home", func(t *testing.T) { + run(t, state.NewGitStore(filepath.Join(home, ".git"))) + }) + + // Nothing was written to the home directory in either case. + _, err := os.Stat(filepath.Join(home, ".claude", "settings.json")) + assert.True(t, os.IsNotExist(err), "no agent hooks may be installed under home") + _, err = os.Stat(filepath.Join(home, ".git", "chainloop-trace")) + assert.True(t, os.IsNotExist(err), "no trace state may be created under home") +} + func TestTraceRunOwnsState(t *testing.T) { t.Run("clean repo", func(t *testing.T) { assert.True(t, traceRunOwnsState(state.NewGitStore(t.TempDir())))