-
Notifications
You must be signed in to change notification settings - Fork 6
fix(config): resolve the home and writable dirs like the legacy CLI #197
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
Open
pjcdawkins
wants to merge
7
commits into
main
Choose a base branch
from
claude/config-dir-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+364
−5
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0409140
fix(config): resolve the home and writable dirs like the legacy CLI
pjcdawkins 34d4914
fix(config): choose the writable dir by permissions, like the legacy CLI
pjcdawkins ff17b2b
test(config): cover a file in place of the writable dir
pjcdawkins c5ca72a
test(config): set the temp dir on Windows too
pjcdawkins 22f810d
fix(config): only use temporary and writable dirs private to the user
pjcdawkins c00c0a5
fix(config): check dir ownership only in shared parents, and validate…
pjcdawkins 6e6e69e
docs(config): explain why group-writable parents are not checked
pjcdawkins File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| package config_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/upsun/cli/internal/config" | ||
| ) | ||
|
|
||
| func TestHomeDir(t *testing.T) { | ||
| a, b, c := t.TempDir(), t.TempDir(), t.TempDir() | ||
| link := filepath.Join(t.TempDir(), "link") | ||
| if runtime.GOOS != "windows" { | ||
| require.NoError(t, os.Symlink(a, link)) | ||
| } | ||
| resolved := func(p string) string { | ||
| r, err := filepath.EvalSymlinks(p) | ||
| require.NoError(t, err) | ||
| return r | ||
| } | ||
| cases := []struct { | ||
| name string | ||
| env map[string]string | ||
| want string | ||
| wantErr string | ||
| }{ | ||
| {name: "prefixed var first", env: map[string]string{"EXAMPLE_CLI_HOME": a, "HOME": b, "USERPROFILE": c}, want: a}, | ||
| {name: "then HOME", env: map[string]string{"EXAMPLE_CLI_HOME": "", "HOME": b, "USERPROFILE": c}, want: b}, | ||
| {name: "then USERPROFILE", env: map[string]string{"EXAMPLE_CLI_HOME": "", "HOME": "", "USERPROFILE": c}, want: c}, | ||
| {name: "symlink is resolved", env: map[string]string{"EXAMPLE_CLI_HOME": link}, want: a}, | ||
| { | ||
| name: "not a directory", | ||
| env: map[string]string{"EXAMPLE_CLI_HOME": filepath.Join(a, "missing")}, | ||
| wantErr: "invalid environment variable EXAMPLE_CLI_HOME", | ||
| }, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| if c.env["EXAMPLE_CLI_HOME"] == link && runtime.GOOS == "windows" { | ||
| t.Skip("symlinks need privileges on Windows") | ||
| } | ||
| cnf, err := config.FromYAML([]byte(validConfig)) | ||
| require.NoError(t, err) | ||
| for k, v := range c.env { | ||
| t.Setenv(k, v) | ||
| } | ||
| home, err := cnf.HomeDir() | ||
| if c.wantErr != "" { | ||
| assert.ErrorContains(t, err, c.wantErr) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| assert.Equal(t, resolved(c.want), home) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestHomeDir_Relative(t *testing.T) { | ||
| dir := t.TempDir() | ||
| t.Chdir(dir) | ||
| cnf, err := config.FromYAML([]byte(validConfig)) | ||
| require.NoError(t, err) | ||
| t.Setenv("EXAMPLE_CLI_HOME", ".") | ||
| home, err := cnf.HomeDir() | ||
| require.NoError(t, err) | ||
| want, err := filepath.EvalSymlinks(dir) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, want, home) | ||
| } | ||
|
|
||
| // TestWritableUserDir_TempFallback checks the cases where the legacy CLI uses a temporary directory instead. | ||
| func TestWritableUserDir_TempFallback(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| setup func(t *testing.T, home string) | ||
| }{ | ||
| { | ||
| name: "read-only home", | ||
| setup: func(t *testing.T, home string) { | ||
| if runtime.GOOS == "windows" || os.Geteuid() == 0 { | ||
| t.Skip("needs Unix permissions") | ||
| } | ||
| require.NoError(t, os.Chmod(home, 0o500)) | ||
| t.Cleanup(func() { _ = os.Chmod(home, 0o700) }) | ||
| }, | ||
| }, | ||
| { | ||
| name: "a file in place of the directory", | ||
| setup: func(t *testing.T, home string) { | ||
| require.NoError(t, os.WriteFile(filepath.Join(home, ".example-cli"), nil, 0o600)) | ||
| }, | ||
| }, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| cnf, err := config.FromYAML([]byte(validConfig)) | ||
| require.NoError(t, err) | ||
| home := t.TempDir() | ||
| c.setup(t, home) | ||
| tmp := t.TempDir() | ||
| t.Setenv("EXAMPLE_CLI_HOME", home) | ||
| t.Setenv("TMPDIR", tmp) // Unix | ||
| t.Setenv("TMP", tmp) // Windows | ||
|
|
||
| dir, err := cnf.WritableUserDir() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, filepath.Join(tmp, "example-cli-tmp"), dir) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| //go:build unix | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "syscall" | ||
|
|
||
| "golang.org/x/sys/unix" | ||
| ) | ||
|
|
||
| // isWritable checks write permission, like PHP's is_writable. | ||
| func isWritable(path string, _ os.FileInfo) bool { | ||
| return unix.Access(path, unix.W_OK) == nil | ||
| } | ||
|
|
||
| // checkPrivateDir checks a directory if others could have created it, i.e. if its parent is world-writable, e.g. | ||
| // /tmp. The directory, and any symlink to it, must then be owned by the user (uid), and it is made private. | ||
| // | ||
| // Otherwise, e.g. in a home directory, it can be owned by another user, as with "sudo -E" or an arbitrary UID. | ||
| func checkPrivateDir(path string, uid int) error { | ||
| // G703: the path is the user's own config or temporary directory. | ||
| info, err := os.Lstat(path) //nolint:gosec | ||
| if err != nil { | ||
| return err | ||
| } | ||
| shared, err := hasSharedParent(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if shared && !ownedBy(info, uid) { | ||
| return fmt.Errorf("not owned by the current user: %s", path) | ||
| } | ||
| if info.Mode()&os.ModeSymlink != 0 { | ||
| target, err := filepath.EvalSymlinks(path) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return checkPrivateDir(target, uid) | ||
| } | ||
| if !info.IsDir() { | ||
| return fmt.Errorf("not a directory: %s", path) | ||
| } | ||
| if shared && info.Mode().Perm()&0o077 != 0 { | ||
| return os.Chmod(path, 0o700) //nolint:gosec // G703: as above | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // hasSharedParent reports whether a directory's parent is world-writable. | ||
| // | ||
| // Group-writable parents are not included: they are common with user private groups (umask 002), and in containers | ||
| // with arbitrary UIDs, where the home directory is owned by another user. | ||
| func hasSharedParent(path string) (bool, error) { | ||
| info, err := os.Stat(filepath.Dir(path)) //nolint:gosec // G703: as above | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| return info.Mode().Perm()&0o002 != 0, nil | ||
| } | ||
|
|
||
| func ownedBy(info os.FileInfo, uid int) bool { | ||
| st, ok := info.Sys().(*syscall.Stat_t) | ||
| return ok && int(st.Uid) == uid | ||
| } | ||
|
|
||
| // ensurePrivateDir checks that a directory is private to the current user. | ||
| func ensurePrivateDir(path string) error { | ||
| return checkPrivateDir(path, os.Geteuid()) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.