From 8afa68af2b160de448ee21dfe5e2d9fa5912167b Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:29:53 +0000 Subject: [PATCH 1/3] aitools: skip auto-install when the CLI runs as root in another user's home ## Changes The automatic installation of Databricks AI tools now skips a run where the CLI is root in a home directory that another user owns. - `libs/aitools/autoinstall`: new `RootInUserHome(ctx)`. On Unix it returns true when the effective UID is 0 and the home directory (from `env.UserHomeDir`) belongs to a non-root UID. If the home can't be stat'ed, it returns false. On Windows it always returns false, because an elevated process keeps the user's own profile. - `Decide` gets a new input, `RootInUserHome`, and a new reason, `root_in_user_home`. The check runs right after the development-build check and before anything that reads the state. The reason is not due, so it doesn't settle the version. - `cmd/aitools`: the auto-install hook passes `RootInUserHome` into `Decide` and adds the new reason to the state-free skips. Such a run returns before it takes the lock, reads or writes the state, looks up the gate, installs anything or sends telemetry. ```diff Decide(in) skipped command / on runtime / cache disabled / development build + root in a user's home -> root_in_user_home (no lock, no state, no telemetry) retries exhausted / already ran / CI / opt-out / gate / holdout / run ``` ## Why `sudo databricks ...` keeps `HOME` pointing at the invoking user's home by default on macOS, and on Linux with `sudo -E`. The auto-install hook then ran as root in that home. It created a root-owned `~/.databricks` (the state and lock files) and could install the agent skills as root. After that, `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` failed for the user, because they could no longer write to that directory. Skipping the run without touching state leaves the home as it was, so the user's next normal run installs as usual. Root in its own home (for example `sudo -H`, or a root login) still installs as before. ## Tests - `TestRootInUserHome` (Unix): returns false for a non-root UID, for root in a root-owned home, for a home that doesn't exist and for an empty home. `TestRootInUserHomeOwnedByAnotherUser` returns true for root in a directory the test user owns. It is skipped when the tests themselves run as root. - `TestDecide`: the new reason, and its precedence over the persisted state and over CI. - `TestReasonDue`: the new reason is not due. - `TestAutoInstallSkipsRootInAUserHome`: with the check forced on, the hook creates no `~/.databricks`, installs no skills, does no gate lookup and buffers no telemetry. With the check off, the same setup installs and records the state. Co-authored-by: Isaac --- .../cli/aitools-auto-install-sudo-home.md | 1 + cmd/aitools/autoinstall_run.go | 5 +++ cmd/aitools/autoinstall_run_test.go | 43 +++++++++++++++++++ libs/aitools/autoinstall/decide.go | 11 ++++- libs/aitools/autoinstall/decide_test.go | 8 +++- libs/aitools/autoinstall/home_unix.go | 36 ++++++++++++++++ libs/aitools/autoinstall/home_unix_test.go | 42 ++++++++++++++++++ libs/aitools/autoinstall/home_windows.go | 12 ++++++ 8 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 .nextchanges/cli/aitools-auto-install-sudo-home.md create mode 100644 libs/aitools/autoinstall/home_unix.go create mode 100644 libs/aitools/autoinstall/home_unix_test.go create mode 100644 libs/aitools/autoinstall/home_windows.go diff --git a/.nextchanges/cli/aitools-auto-install-sudo-home.md b/.nextchanges/cli/aitools-auto-install-sudo-home.md new file mode 100644 index 00000000000..2a15738f6d2 --- /dev/null +++ b/.nextchanges/cli/aitools-auto-install-sudo-home.md @@ -0,0 +1 @@ +* The automatic installation of Databricks AI tools no longer runs when the CLI runs as root in a home directory another user owns, as under `sudo` with `HOME` kept (the macOS default). Such a run used to leave a root-owned `~/.databricks` that broke `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` for the user, and could install the tools as root. diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index c071ab29b96..9b066660d3c 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -49,6 +49,9 @@ const ( // gateOn is autoinstall.GateOn; tests replace it. var gateOn = autoinstall.GateOn +// rootInUserHome is autoinstall.RootInUserHome; tests replace it. +var rootInUserHome = autoinstall.RootInUserHome + // notifyInterrupt returns a context that is canceled when the CLI receives // Ctrl+C or a request to terminate; tests replace it. var notifyInterrupt = func(ctx context.Context) (context.Context, context.CancelFunc) { @@ -102,6 +105,7 @@ func autoInstall(ctx context.Context, skippedCommand bool) { OnRuntime: dbr.RunsOnRuntime(ctx), CacheDisabled: autoinstall.CacheDisabled(ctx), Version: version, + RootInUserHome: rootInUserHome(ctx), CI: agents.InCI(ctx), // A "no" earlier in this process counts even if storing it failed. EnvOptOut: autoinstall.EnvOptedOut(ctx) || autoinstall.Declined(), @@ -403,6 +407,7 @@ var stateFreeSkips = []autoinstall.Reason{ autoinstall.ReasonOnRuntime, autoinstall.ReasonCacheDisabled, autoinstall.ReasonDevelopmentBuild, + autoinstall.ReasonRootInUserHome, } // skipOutcomes are the reported outcomes of the skips that settle a version. diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go index f0393e4c23f..ab6173cb60f 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -365,6 +365,49 @@ func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) { assert.Equal(t, autoinstall.State{ID: "treatment-machine"}, state, "the version is not settled") } +// Root running in a user's home (sudo with HOME kept) leaves the home alone: +// it takes no lock, reads and writes no state, looks no gate up and installs +// nothing, so the user's own runs can still write there. The same run as the +// user installs. +func TestAutoInstallSkipsRootInAUserHome(t *testing.T) { + run := func(t *testing.T, rootInHome bool) (ctx context.Context, home string, lookups *int) { + home = setupAutoInstallHome(t, "") + if rootInHome { + // The state the setup seeds would hide a state file or lock file + // the run creates. + require.NoError(t, os.RemoveAll(filepath.Join(home, ".databricks"))) + } + t.Cleanup(func() { rootInUserHome = autoinstall.RootInUserHome }) + rootInUserHome = func(context.Context) bool { return rootInHome } + lookups = new(int) + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + *lookups++ + return true, nil + } + ctx = cmdio.MockDiscard(t.Context()) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + return ctx, home, lookups + } + + t.Run("root in a user's home", func(t *testing.T) { + ctx, home, lookups := run(t, true) + assert.NoDirExists(t, filepath.Join(home, ".databricks")) + assert.NoDirExists(t, filepath.Join(home, ".cursor", "skills")) + assert.Zero(t, *lookups) + assert.Empty(t, telemetry.BufferedEvents(ctx)) + }) + t.Run("the user", func(t *testing.T) { + ctx, home, lookups := run(t, false) + assert.FileExists(t, filepath.Join(home, ".cursor", "skills", "databricks-test", "SKILL.md")) + assert.Equal(t, 1, *lookups) + state, err := autoinstall.Load(ctx) + require.NoError(t, err) + assert.Equal(t, autoinstall.State{LastVersion: "1.30.0", ID: "treatment-machine"}, state) + }) +} + func TestToolsPlanNewAgents(t *testing.T) { claude := &agents.Agent{DisplayName: "Claude Code"} cursor := &agents.Agent{DisplayName: "Cursor"} diff --git a/libs/aitools/autoinstall/decide.go b/libs/aitools/autoinstall/decide.go index 0b4d2832a48..e782a593986 100644 --- a/libs/aitools/autoinstall/decide.go +++ b/libs/aitools/autoinstall/decide.go @@ -25,6 +25,7 @@ const ( ReasonOnRuntime Reason = "on_runtime" ReasonCacheDisabled Reason = "cache_disabled" ReasonDevelopmentBuild Reason = "development_build" + ReasonRootInUserHome Reason = "root_in_user_home" ReasonAlreadyRan Reason = "already_ran" ReasonRetriesExhausted Reason = "retries_exhausted" @@ -43,7 +44,7 @@ const ( // settles the version. A run that is not due changes nothing. func (r Reason) Due() bool { switch r { - case ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonAlreadyRan, ReasonRetriesExhausted: + case ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonRootInUserHome, ReasonAlreadyRan, ReasonRetriesExhausted: return false case ReasonSkippedCI, ReasonSkippedOptOut, ReasonGateOff, ReasonSkippedHoldout, ReasonRun: return true @@ -66,6 +67,10 @@ type Inputs struct { // Version is the running CLI version. Version string + // RootInUserHome is set when the CLI runs as root in a home directory + // that another user owns (see RootInUserHome). + RootInUserHome bool + // State is the persisted state: last version, attempts and opt-out. State State @@ -100,6 +105,10 @@ func Decide(in Inputs) Reason { // they would trigger on every build. case build.IsDevelopmentVersion(in.Version): return ReasonDevelopmentBuild + // Root under sudo with HOME kept would leave files the user cannot + // change in the user's home. + case in.RootInUserHome: + return ReasonRootInUserHome case in.State.LastVersion == in.Version && in.State.Attempts >= MaxAttempts: return ReasonRetriesExhausted case in.State.LastVersion == in.Version && in.State.Attempts == 0: diff --git a/libs/aitools/autoinstall/decide_test.go b/libs/aitools/autoinstall/decide_test.go index 352f78637b1..736684788b3 100644 --- a/libs/aitools/autoinstall/decide_test.go +++ b/libs/aitools/autoinstall/decide_test.go @@ -26,6 +26,12 @@ func TestDecide(t *testing.T) { {name: "cache disabled", modify: func(in *Inputs) { in.CacheDisabled = true }, want: ReasonCacheDisabled}, {name: "development build", modify: func(in *Inputs) { in.Version = "1.31.0-dev+abcdef123456" }, want: ReasonDevelopmentBuild}, {name: "snapshot build", modify: func(in *Inputs) { in.Version = "1.30.1-dev+abcdef1" }, want: ReasonDevelopmentBuild}, + {name: "root in a user's home", modify: func(in *Inputs) { in.RootInUserHome = true }, want: ReasonRootInUserHome}, + {name: "root in a user's home before the state", modify: func(in *Inputs) { + in.RootInUserHome = true + in.State = State{LastVersion: "1.30.0", Attempts: 3} + in.CI = true + }, want: ReasonRootInUserHome}, {name: "same version, settled", modify: func(in *Inputs) { in.State.LastVersion = "1.30.0" }, want: ReasonAlreadyRan}, {name: "same version, failed before", modify: func(in *Inputs) { in.State = State{LastVersion: "1.30.0", Attempts: 3} }, want: ReasonRun}, @@ -65,7 +71,7 @@ func TestDecide(t *testing.T) { } func TestReasonDue(t *testing.T) { - notDue := []Reason{ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonAlreadyRan, ReasonRetriesExhausted} + notDue := []Reason{ReasonSkippedCommand, ReasonOnRuntime, ReasonCacheDisabled, ReasonDevelopmentBuild, ReasonRootInUserHome, ReasonAlreadyRan, ReasonRetriesExhausted} due := []Reason{ReasonSkippedCI, ReasonSkippedOptOut, ReasonGateOff, ReasonSkippedHoldout, ReasonRun} for _, r := range notDue { assert.False(t, r.Due(), r) diff --git a/libs/aitools/autoinstall/home_unix.go b/libs/aitools/autoinstall/home_unix.go new file mode 100644 index 00000000000..9fe991270e1 --- /dev/null +++ b/libs/aitools/autoinstall/home_unix.go @@ -0,0 +1,36 @@ +//go:build unix + +package autoinstall + +import ( + "context" + "os" + + "github.com/databricks/cli/libs/env" + "golang.org/x/sys/unix" +) + +// RootInUserHome reports whether the CLI runs as root in a home directory +// that another user owns, as under sudo with HOME kept (the macOS default, +// and `sudo -E` on Linux). What auto-install writes there, its state and the +// agents' files, would belong to root, and the user could no longer change +// it. A home that cannot be inspected counts as not. +func RootInUserHome(ctx context.Context) bool { + return rootInUserHome(ctx, os.Geteuid()) +} + +// rootInUserHome is RootInUserHome for the effective user ID euid. +func rootInUserHome(ctx context.Context, euid int) bool { + if euid != 0 { + return false + } + home, err := env.UserHomeDir(ctx) + if err != nil { + return false + } + var st unix.Stat_t + if err := unix.Stat(home, &st); err != nil { + return false + } + return st.Uid != 0 +} diff --git a/libs/aitools/autoinstall/home_unix_test.go b/libs/aitools/autoinstall/home_unix_test.go new file mode 100644 index 00000000000..27e075ce402 --- /dev/null +++ b/libs/aitools/autoinstall/home_unix_test.go @@ -0,0 +1,42 @@ +//go:build unix + +package autoinstall + +import ( + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" +) + +func TestRootInUserHome(t *testing.T) { + userHome := t.TempDir() + tests := []struct { + name string + euid int + home string + }{ + {name: "the user in their home", euid: os.Geteuid(), home: userHome}, + {name: "another user in the home", euid: os.Geteuid() + 1, home: userHome}, + // The root directory is owned by root on every Unix system. + {name: "root in a root-owned home", euid: 0, home: "/"}, + {name: "root with a home that does not exist", euid: 0, home: filepath.Join(userHome, "missing")}, + {name: "root without a home", euid: 0, home: ""}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), tc.home) + assert.False(t, rootInUserHome(ctx, tc.euid)) + }) + } +} + +func TestRootInUserHomeOwnedByAnotherUser(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("a home the test creates is owned by root") + } + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + assert.True(t, rootInUserHome(ctx, 0)) +} diff --git a/libs/aitools/autoinstall/home_windows.go b/libs/aitools/autoinstall/home_windows.go new file mode 100644 index 00000000000..db826d8df27 --- /dev/null +++ b/libs/aitools/autoinstall/home_windows.go @@ -0,0 +1,12 @@ +//go:build windows + +package autoinstall + +import "context" + +// RootInUserHome reports whether the CLI runs as root in a home directory +// that another user owns. Windows has no root user that the CLI switches to: +// an elevated process keeps the user's own profile, so it reports false. +func RootInUserHome(context.Context) bool { + return false +} From a06d7192a69b2af694649414f93416dd511e8c17 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:38:33 +0000 Subject: [PATCH 2/3] Link the changelog entry to #7828 Co-authored-by: Isaac --- .nextchanges/cli/aitools-auto-install-sudo-home.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/aitools-auto-install-sudo-home.md b/.nextchanges/cli/aitools-auto-install-sudo-home.md index 2a15738f6d2..923ae5574e0 100644 --- a/.nextchanges/cli/aitools-auto-install-sudo-home.md +++ b/.nextchanges/cli/aitools-auto-install-sudo-home.md @@ -1 +1 @@ -* The automatic installation of Databricks AI tools no longer runs when the CLI runs as root in a home directory another user owns, as under `sudo` with `HOME` kept (the macOS default). Such a run used to leave a root-owned `~/.databricks` that broke `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` for the user, and could install the tools as root. +* The automatic installation of Databricks AI tools no longer runs when the CLI runs as root in a home directory another user owns, as under `sudo` with `HOME` kept (the macOS default). Such a run used to leave a root-owned `~/.databricks` that broke `databricks auth login`, `databricks aitools install` and `databricks aitools auto-install disable` for the user, and could install the tools as root. ([#7828](https://github.com/databricks/cli/pull/7828)) From ad19ad788beeb9bcb1535b000f7d6135fdb97de6 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:05:59 +0000 Subject: [PATCH 3/3] Address review comments on the root-in-user-home auto-install skip Log the root_in_user_home skip at debug level, note in the doc comment that agent directories set through environment variables are out of scope, skip the root-owned home test where / is not owned by UID 0, make the Decide precedence case start from a state that skips on its own, and turn the test closure into a named helper. Co-authored-by: Isaac --- cmd/aitools/autoinstall_run.go | 9 +++- cmd/aitools/autoinstall_run_test.go | 48 +++++++++++----------- libs/aitools/autoinstall/decide_test.go | 2 +- libs/aitools/autoinstall/home_unix.go | 4 +- libs/aitools/autoinstall/home_unix_test.go | 14 ++++++- 5 files changed, 48 insertions(+), 29 deletions(-) diff --git a/cmd/aitools/autoinstall_run.go b/cmd/aitools/autoinstall_run.go index 9b066660d3c..8fe979938e9 100644 --- a/cmd/aitools/autoinstall_run.go +++ b/cmd/aitools/autoinstall_run.go @@ -111,8 +111,13 @@ func autoInstall(ctx context.Context, skippedCommand bool) { EnvOptOut: autoinstall.EnvOptedOut(ctx) || autoinstall.Declined(), } // What never auto-installs skips before the state is read, and without a - // log line: these are most runs. - if slices.Contains(stateFreeSkips, autoinstall.Decide(in)) { + // log line: these are most runs. Root in a user's home is rare, and a user + // who always runs the CLI under sudo needs a way to see why nothing + // installs, so it logs. + if reason := autoinstall.Decide(in); slices.Contains(stateFreeSkips, reason) { + if reason == autoinstall.ReasonRootInUserHome { + log.Debugf(ctx, "auto-install: skipped: %s", reason) + } return } var err error diff --git a/cmd/aitools/autoinstall_run_test.go b/cmd/aitools/autoinstall_run_test.go index ab6173cb60f..376fa91ebe4 100644 --- a/cmd/aitools/autoinstall_run_test.go +++ b/cmd/aitools/autoinstall_run_test.go @@ -370,36 +370,15 @@ func TestAutoInstallSkipsTheGateLookupWithoutAgents(t *testing.T) { // nothing, so the user's own runs can still write there. The same run as the // user installs. func TestAutoInstallSkipsRootInAUserHome(t *testing.T) { - run := func(t *testing.T, rootInHome bool) (ctx context.Context, home string, lookups *int) { - home = setupAutoInstallHome(t, "") - if rootInHome { - // The state the setup seeds would hide a state file or lock file - // the run creates. - require.NoError(t, os.RemoveAll(filepath.Join(home, ".databricks"))) - } - t.Cleanup(func() { rootInUserHome = autoinstall.RootInUserHome }) - rootInUserHome = func(context.Context) bool { return rootInHome } - lookups = new(int) - t.Cleanup(func() { gateOn = autoinstall.GateOn }) - gateOn = func(context.Context, string) (bool, error) { - *lookups++ - return true, nil - } - ctx = cmdio.MockDiscard(t.Context()) - ctx = telemetry.WithNewLogger(ctx) - runAutoInstallHook(ctx) - return ctx, home, lookups - } - t.Run("root in a user's home", func(t *testing.T) { - ctx, home, lookups := run(t, true) + ctx, home, lookups := runRootInUserHome(t, true) assert.NoDirExists(t, filepath.Join(home, ".databricks")) assert.NoDirExists(t, filepath.Join(home, ".cursor", "skills")) assert.Zero(t, *lookups) assert.Empty(t, telemetry.BufferedEvents(ctx)) }) t.Run("the user", func(t *testing.T) { - ctx, home, lookups := run(t, false) + ctx, home, lookups := runRootInUserHome(t, false) assert.FileExists(t, filepath.Join(home, ".cursor", "skills", "databricks-test", "SKILL.md")) assert.Equal(t, 1, *lookups) state, err := autoinstall.Load(ctx) @@ -408,6 +387,29 @@ func TestAutoInstallSkipsRootInAUserHome(t *testing.T) { }) } +// runRootInUserHome runs the hook in a fresh home, as root in a user's home +// when rootInHome is set, and counts the gate lookups. +func runRootInUserHome(t *testing.T, rootInHome bool) (ctx context.Context, home string, lookups *int) { + home = setupAutoInstallHome(t, "") + if rootInHome { + // The state the setup seeds would hide a state file or lock file + // the run creates. + require.NoError(t, os.RemoveAll(filepath.Join(home, ".databricks"))) + } + t.Cleanup(func() { rootInUserHome = autoinstall.RootInUserHome }) + rootInUserHome = func(context.Context) bool { return rootInHome } + lookups = new(int) + t.Cleanup(func() { gateOn = autoinstall.GateOn }) + gateOn = func(context.Context, string) (bool, error) { + *lookups++ + return true, nil + } + ctx = cmdio.MockDiscard(t.Context()) + ctx = telemetry.WithNewLogger(ctx) + runAutoInstallHook(ctx) + return ctx, home, lookups +} + func TestToolsPlanNewAgents(t *testing.T) { claude := &agents.Agent{DisplayName: "Claude Code"} cursor := &agents.Agent{DisplayName: "Cursor"} diff --git a/libs/aitools/autoinstall/decide_test.go b/libs/aitools/autoinstall/decide_test.go index 736684788b3..d0aa08c26fb 100644 --- a/libs/aitools/autoinstall/decide_test.go +++ b/libs/aitools/autoinstall/decide_test.go @@ -29,7 +29,7 @@ func TestDecide(t *testing.T) { {name: "root in a user's home", modify: func(in *Inputs) { in.RootInUserHome = true }, want: ReasonRootInUserHome}, {name: "root in a user's home before the state", modify: func(in *Inputs) { in.RootInUserHome = true - in.State = State{LastVersion: "1.30.0", Attempts: 3} + in.State = State{LastVersion: "1.30.0", Attempts: MaxAttempts} in.CI = true }, want: ReasonRootInUserHome}, diff --git a/libs/aitools/autoinstall/home_unix.go b/libs/aitools/autoinstall/home_unix.go index 9fe991270e1..f935baf4e2f 100644 --- a/libs/aitools/autoinstall/home_unix.go +++ b/libs/aitools/autoinstall/home_unix.go @@ -14,7 +14,9 @@ import ( // that another user owns, as under sudo with HOME kept (the macOS default, // and `sudo -E` on Linux). What auto-install writes there, its state and the // agents' files, would belong to root, and the user could no longer change -// it. A home that cannot be inspected counts as not. +// it. A home that cannot be inspected counts as not. Only the home is +// checked: an agent directory set through an environment variable, such as +// CLAUDE_CONFIG_DIR kept by `sudo -E -H`, is out of scope. func RootInUserHome(ctx context.Context) bool { return rootInUserHome(ctx, os.Geteuid()) } diff --git a/libs/aitools/autoinstall/home_unix_test.go b/libs/aitools/autoinstall/home_unix_test.go index 27e075ce402..0967326737d 100644 --- a/libs/aitools/autoinstall/home_unix_test.go +++ b/libs/aitools/autoinstall/home_unix_test.go @@ -9,6 +9,8 @@ import ( "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/sys/unix" ) func TestRootInUserHome(t *testing.T) { @@ -20,8 +22,6 @@ func TestRootInUserHome(t *testing.T) { }{ {name: "the user in their home", euid: os.Geteuid(), home: userHome}, {name: "another user in the home", euid: os.Geteuid() + 1, home: userHome}, - // The root directory is owned by root on every Unix system. - {name: "root in a root-owned home", euid: 0, home: "/"}, {name: "root with a home that does not exist", euid: 0, home: filepath.Join(userHome, "missing")}, {name: "root without a home", euid: 0, home: ""}, } @@ -33,6 +33,16 @@ func TestRootInUserHome(t *testing.T) { } } +func TestRootInUserHomeOwnedByRoot(t *testing.T) { + var st unix.Stat_t + require.NoError(t, unix.Stat("/", &st)) + if st.Uid != 0 { + t.Skip("the root directory is not owned by root, as in a user namespace that does not map root") + } + ctx := env.WithUserHomeDir(t.Context(), "/") + assert.False(t, rootInUserHome(ctx, 0)) +} + func TestRootInUserHomeOwnedByAnotherUser(t *testing.T) { if os.Geteuid() == 0 { t.Skip("a home the test creates is owned by root")