Skip to content

fix(trace): refuse to install hooks when the root is a user-level directory - #3813

Open
Sanjays2402 wants to merge 1 commit into
chainloop-dev:mainfrom
Sanjays2402:fix-trace-user-level-root
Open

Sanjays2402 wants to merge 1 commit into
chainloop-dev:mainfrom
Sanjays2402:fix-trace-user-level-root

Conversation

@Sanjays2402

@Sanjays2402 Sanjays2402 commented Nov 16, 2026 •

Copy link
Copy Markdown

Fixes #3812

trace run outside a git repository uses the working directory as the root
and still installs the agent hooks there. Run from $HOME, that writes the
user-level configuration (~/.claude/settings.json, ~/.codex/hooks.json,
~/.cursor/hooks.json), so the hooks load in every session of that agent in
any 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 symlinks
evaluated and compares it against the home directory and the agent home
folders ($CODEX_HOME, $COPILOT_HOME, defaulting to ~/.codex and
~/.copilot). Both entry points refuse such a root before anything is
installed or asked:

  • trace run fails in TraceRun, before snapshotting, installing, or
    running the wrapped command.
  • trace init fails right after resolving the repository root, before any
    prompt or write.

Test plan:

  • TestIsUserLevelRoot in app/cli/internal/trace/userlevel_test.go: home
    itself, home through a symlink, a project below home, an unrelated
    directory, and both agent home folders (explicit and default).
  • TestTraceRunRefusesUserLevelRoot in
    app/cli/pkg/action/trace_run_test.go: both paths from the issue, no git
    repository with the working directory at $HOME and a git repository
    rooted at $HOME. Both fail with the user-level error, and neither writes
    agent hooks or trace state under home.
  • Ran go test ./app/cli/internal/trace/ -run TestIsUserLevelRoot and
    go test ./app/cli/pkg/action/ -run 'TestTraceRun|TestCleanupTrace|TestSnapshotAndRestore|TestPiTraceRun', all passing.

…ectory

Signed-off-by: Sanjay Santhanam <51058514+Sanjays2402@users.noreply.github.com>
@chainloop-platform

chainloop-platform Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

PR validation — ⚠️ 1 failing

Status Policy Material Messages
⚠️ Failed 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 -

View attestation ↗

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

View attestation ↗


Powered by Chainloop and Chainloop Trace

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 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>

Comment on lines +72 to +88
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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Suggested change
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 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>

Comment thread app/cli/cmd/trace_init.go
// ~/.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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@javirln
javirln requested a review from a team November 18, 2026 06:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

trace: never install agent hooks at the user level (home directory)

1 participant