Repository navigation
fix(trace): refuse to install hooks when the root is a user-level directory - #3813
Sanjays2402 wants to merge 1 commit into
Conversation
…ectory Signed-off-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
PR validation —
|
| Status | Policy | Material | Messages |
|---|---|---|---|
pr-min-approvals |
pr-info |
PR/MR #3813 has 0 approving reviews, 1 required. | |
| ✅ Passed | pr-description-required |
pr-info |
- |
| ✅ Passed | pr-user-story-linked |
pr-info |
- |
AI Session Checks — ⚠️ no AI session found
Missing AI Coding Sessions
This organization requires every PR to be backed by a Chainloop Trace AI coding session, and none was found for this one.
Please make sure the AI coding session evidence has been sent by the Chainloop CLI, or add the skip-ai-session label to this PR to bypass this check.
Learn more about Chainloop Trace.
Security Checks — ✅ 5 passing
✅ secret-scan
| Status | Policy | Messages |
|---|---|---|
| ✅ Passed | secrets-detection |
- |
✅ sast-scan
| Status | Policy | Messages |
|---|---|---|
| ✅ Passed | owasp-top10-2025 |
- |
| ✅ Passed | sast |
- |
| ✅ Passed | cwe-top25 |
- |
| ✅ Passed | cwe-top26-40-cusp |
- |
Scans not applied (3)
| Scan | Reason |
|---|---|
vulnerability-scan |
no manifest/lockfile changed |
github-actions-scan |
no workflow files changed |
iac-scan |
no IaC files changed |
Powered by Chainloop and Chainloop Trace
There was a problem hiding this comment.
4 issues found across 5 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="app/cli/cmd/trace_init.go">
<violation number="1" location="app/cli/cmd/trace_init.go:112">
P3: The new `trace init` refusal has no command-level regression test. Add a test that runs `newTraceInitCmd` in a Git repository whose root is `HOME` (and checks that no hooks or `.chainloop.yml` are 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]</violation>
</file>
<file name="app/cli/internal/trace/userlevel.go">
<violation number="1" location="app/cli/internal/trace/userlevel.go:45">
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 `os.SameFile`, instead.</violation>
<violation number="2" location="app/cli/internal/trace/userlevel.go:72">
P1: `userLevelDirs` stops before examining custom homes when `os.UserHomeDir` is unavailable. With `CODEX_HOME` or `COPILOT_HOME` set but no discoverable user home, a root equal to that configured user-level folder passes the guard and can receive hooks; add custom candidates independently and use defaults only when the home is known.</violation>
</file>
<file name="app/cli/pkg/action/trace_run.go">
<violation number="1" location="app/cli/pkg/action/trace_run.go:112">
P2: This guard runs too late to guarantee refusal before prompting or other setup: control-plane `trace run` calls `ensureActionOpts` before `TraceRun`, and that path can prompt for confirmation and save organization state. Validate the resolved root in the command before `ensureActionOpts` (or otherwise move this check ahead of that setup).</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| if err != nil { | ||
| continue | ||
| } | ||
| if resolvedRoot == resolved { |
There was a problem hiding this comment.
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 os.SameFile, instead.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At app/cli/internal/trace/userlevel.go, line 45:
<comment>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 `os.SameFile`, instead.</comment>
<file context>
@@ -0,0 +1,88 @@
+ if err != nil {
+ continue
+ }
+ if resolvedRoot == resolved {
+ return true, nil
+ }
</file context>
| 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 | ||
| } |
There was a problem hiding this comment.
P1: userLevelDirs stops before examining custom homes when os.UserHomeDir is unavailable. With CODEX_HOME or COPILOT_HOME set but no discoverable user home, a root equal to that configured user-level folder passes the guard and can receive hooks; add custom candidates independently and use defaults only when the home is known.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At app/cli/internal/trace/userlevel.go, line 72:
<comment>`userLevelDirs` stops before examining custom homes when `os.UserHomeDir` is unavailable. With `CODEX_HOME` or `COPILOT_HOME` set but no discoverable user home, a root equal to that configured user-level folder passes the guard and can receive hooks; add custom candidates independently and use defaults only when the home is known.</comment>
<file context>
@@ -0,0 +1,88 @@
+// folders where they apply.
+func userLevelDirs() []string {
+ home, err := os.UserHomeDir()
+ if err != nil || home == "" {
+ return nil
+ }
</file context>
| 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 | |
| } | |
| home, err := os.UserHomeDir() | |
| if err != nil { | |
| home = "" | |
| } | |
| dirs := make([]string, 0, 3) | |
| if home != "" { | |
| dirs = append(dirs, home) | |
| } | |
| if codexHome := os.Getenv("CODEX_HOME"); codexHome != "" { | |
| dirs = append(dirs, codexHome) | |
| } else if home != "" { | |
| dirs = append(dirs, filepath.Join(home, ".codex")) | |
| } | |
| if copilotHome := os.Getenv("COPILOT_HOME"); copilotHome != "" { | |
| dirs = append(dirs, copilotHome) | |
| } else if home != "" { | |
| dirs = append(dirs, filepath.Join(home, ".copilot")) | |
| } | |
| return dirs |
| // 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) |
There was a problem hiding this comment.
P2: This guard runs too late to guarantee refusal before prompting or other setup: control-plane trace run calls ensureActionOpts before TraceRun, and that path can prompt for confirmation and save organization state. Validate the resolved root in the command before ensureActionOpts (or otherwise move this check ahead of that setup).
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At app/cli/pkg/action/trace_run.go, line 112:
<comment>This guard runs too late to guarantee refusal before prompting or other setup: control-plane `trace run` calls `ensureActionOpts` before `TraceRun`, and that path can prompt for confirmation and save organization state. Validate the resolved root in the command before `ensureActionOpts` (or otherwise move this check ahead of that setup).</comment>
<file context>
@@ -104,6 +104,19 @@ func TraceRun(ctx context.Context, log zerolog.Logger, opts TraceRunOpts) error
+ // 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)
</file context>
| // ~/.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) |
There was a problem hiding this comment.
P3: The new trace init refusal has no command-level regression test. Add a test that runs newTraceInitCmd in a Git repository whose root is HOME (and checks that no hooks or .chainloop.yml are 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
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At app/cli/cmd/trace_init.go, line 112:
<comment>The new `trace init` refusal has no command-level regression test. Add a test that runs `newTraceInitCmd` in a Git repository whose root is `HOME` (and checks that no hooks or `.chainloop.yml` are 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]</comment>
<file context>
@@ -103,6 +103,20 @@ cannot use them with --local. You can use --require-trace with --local.`,
+ // ~/.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)
</file context>
Fixes #3812
trace runoutside a git repository uses the working directory as the rootand still installs the agent hooks there. Run from
$HOME, that writes theuser-level configuration (
~/.claude/settings.json,~/.codex/hooks.json,~/.cursor/hooks.json), so the hooks load in every session of that agent inany project, and a killed run leaves them there. The same applies to a git
repository rooted at
$HOME.This adds
trace.IsUserLevelRoot, which resolves the root with symlinksevaluated and compares it against the home directory and the agent home
folders (
$CODEX_HOME,$COPILOT_HOME, defaulting to~/.codexand~/.copilot). Both entry points refuse such a root before anything isinstalled or asked:
trace runfails inTraceRun, before snapshotting, installing, orrunning the wrapped command.
trace initfails right after resolving the repository root, before anyprompt or write.
Test plan:
TestIsUserLevelRootinapp/cli/internal/trace/userlevel_test.go: homeitself, home through a symlink, a project below home, an unrelated
directory, and both agent home folders (explicit and default).
TestTraceRunRefusesUserLevelRootinapp/cli/pkg/action/trace_run_test.go: both paths from the issue, no gitrepository with the working directory at
$HOMEand a git repositoryrooted at
$HOME. Both fail with the user-level error, and neither writesagent hooks or trace state under home.
go test ./app/cli/internal/trace/ -run TestIsUserLevelRootandgo test ./app/cli/pkg/action/ -run 'TestTraceRun|TestCleanupTrace|TestSnapshotAndRestore|TestPiTraceRun', all passing.