Repository navigation
fix(trace): refuse to install hooks when the root is a user-level directory #3813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: String equality can miss the same directory on case-insensitive filesystems because symlink evaluation does not canonicalize case. A case-variant spelling of the home directory can therefore bypass this refusal and install hooks globally; compare the existing directories by filesystem identity, such as Prompt for AI agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+88
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: This guard runs too late to guarantee refusal before prompting or other setup: control-plane Prompt for AI agents |
||
| 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. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: The new
trace initrefusal has no command-level regression test. Add a test that runsnewTraceInitCmdin a Git repository whose root isHOME(and checks that no hooks or.chainloop.ymlare written), so this safety guard cannot be bypassed without detection.(Based on your team's feedback about covering the real path and the step under test.) [4783126a-1127-49bd-9b99-f3d2848cd202]
Prompt for AI agents