Skip to content
This repository has been archived by the owner on Jan 17, 2021. It is now read-only.

Commit

Permalink
add ~/.ssh directory sanity check before starting
Browse files Browse the repository at this point in the history
Checks:
- if it exists
- if it's a directory (if not warn and disable reuse connection feature)
- if it has safe permissions (not writable by anyone except the owner,
  if not then warn)
  • Loading branch information
deansheather committed Jun 15, 2019
1 parent 4d64fc0 commit eee34f5
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions sshcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
)

const codeServerPath = "~/.cache/sshcode/sshcode-server"
const sshControlPath = "~/.ssh/control-%h-%p-%r"
const sshDirectory = "~/.ssh"
const sshDirectoryUnsafeModeMask = 0022
const sshControlPath = sshDirectory + "/control-%h-%p-%r"

type options struct {
skipSync bool
Expand All @@ -34,8 +36,6 @@ type options struct {
}

func sshCode(host, dir string, o options) error {
flog.Info("ensuring code-server is updated...")

host, extraSSHFlags, err := parseHost(host)
if err != nil {
return xerrors.Errorf("failed to parse host IP: %w", err)
Expand All @@ -56,6 +56,28 @@ func sshCode(host, dir string, o options) error {
return xerrors.Errorf("failed to find available remote port: %w", err)
}

// Check the SSH directory's permissions and warn the user if it is not safe.
sshDirectoryMode, err := os.Lstat(expandPath(sshDirectory))
if err != nil {
if !o.noReuseConnection {
flog.Info("failed to stat %v directory, disabling connection reuse feature: %v", sshDirectory, err)
o.noReuseConnection = true
}
} else {
if !sshDirectoryMode.IsDir() {
if !o.noReuseConnection {
flog.Info("%v is not a directory, disabling connection reuse feature", sshDirectory)
o.noReuseConnection = true
} else {
flog.Info("warning: %v is not a directory", sshDirectory)
}
}
if sshDirectoryMode.Mode().Perm()&sshDirectoryUnsafeModeMask != 0 {
flog.Info("warning: the %v directory has unsafe permissions, they should only be writable by "+
"the owner (and files inside should be set to 0600)", sshDirectory)
}
}

// Start SSH master connection socket. This prevents multiple password prompts from appearing as authentication
// only happens on the initial connection.
if !o.noReuseConnection {
Expand Down Expand Up @@ -100,6 +122,7 @@ func sshCode(host, dir string, o options) error {
}
}

flog.Info("ensuring code-server is updated...")
dlScript := downloadScript(codeServerPath)

// Downloads the latest code-server and allows it to be executed.
Expand Down Expand Up @@ -214,6 +237,23 @@ func sshCode(host, dir string, o options) error {
return nil
}

// expandPath returns an expanded version of path.
func expandPath(path string) string {
path = filepath.Clean(os.ExpandEnv(path))

// Replace tilde notation in path with the home directory.
homedir := os.Getenv("HOME")
if homedir != "" {
if path == "~" {
path = homedir
} else if strings.HasPrefix(path, "~/") {
path = filepath.Join(homedir, path[2:])
}
}

return filepath.Clean(path)
}

func parseBindAddr(bindAddr string) (string, error) {
if bindAddr == "" {
bindAddr = ":"
Expand Down

0 comments on commit eee34f5

Please sign in to comment.