Skip to content

Commit 7ac24ea

Browse files
authored
[devbox cloud] require git in home and fs-root dirs to use as devbox project dir (#522)
## Summary Problem: When running `devbox cloud shell` (and other commands) if we don't find a `devbox.json` in the current directory and no `--config` flag is specified, then we walk up the directory tree to find a `devbox.json` file. If we do find one, we use that directory as the `projectDir`. This has the danger that we may sync a sensitive directory like a user's homedir or the filesystem-root dir (i.e. `/`). The syncing may inadvertently delete something in the directories under this sensitive dir. @Lagoja ran into this since he has a devbox.json in his homedir. Solution: This PR protects against this by requiring a `.git` repository to be present in the same dir. We presume this would protect against data loss. Without a `.git` we display a user error. ## How was it tested? ``` ❯ devbox cloud shell Devbox Cloud Remote development environments powered by Nix Error: Found a config (devbox.json) file at /Users/savil, but since it is a sensitive directory we require it to be part of a git repository before we sync it to devbox cloud ```
1 parent 5974862 commit 7ac24ea

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

internal/cloud/cloud.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ func Shell(w io.Writer, projectDir string, githubUsername string) error {
3333
fmt.Fprintln(w, "Remote development environments powered by Nix")
3434
fmt.Fprint(w, "\n")
3535

36+
if err := ensureProjectDirIsNotSensitive(projectDir); err != nil {
37+
return err
38+
}
39+
3640
username, vmHostname := parseVMEnvVar()
3741
// The flag for githubUsername overrides any env-var, since flags are a more
3842
// explicit action compared to an env-var which could be latently present.
@@ -473,3 +477,38 @@ func vmHostnameFromSSHControlPath() string {
473477
func hyphenatePath(path string) string {
474478
return strings.ReplaceAll(path, "/", "-")
475479
}
480+
481+
func ensureProjectDirIsNotSensitive(dir string) error {
482+
483+
// isSensitiveDir checks if the dir is the rootdir or the user's homedir
484+
isSensitiveDir := func(dir string) bool {
485+
dir = filepath.Clean(dir)
486+
if dir == "/" {
487+
return true
488+
}
489+
490+
home, err := os.UserHomeDir()
491+
if err != nil {
492+
return false
493+
}
494+
return dir == filepath.Clean(home)
495+
}
496+
497+
if isSensitiveDir(dir) {
498+
// check for a git repository in this folder before using this project config
499+
// (and potentially syncing all the code to devbox-cloud)
500+
_, err := os.Stat(filepath.Join(dir, ".git"))
501+
if err != nil {
502+
if os.IsNotExist(err) {
503+
return usererr.New(
504+
"Found a config (devbox.json) file at %s, "+
505+
"but since it is a sensitive directory we require it to be part of a git repository "+
506+
"before we sync it to devbox cloud",
507+
dir,
508+
)
509+
}
510+
return errors.WithStack(err)
511+
}
512+
}
513+
return nil
514+
}

0 commit comments

Comments
 (0)