diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-15 07:51:06 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-15 07:51:06 +0530 |
| commit | d369e5611df643bd83f951a94b468ef272c1bf3e (patch) | |
| tree | e9b12ca81bafc53cfad7933578f2665e183bf4f2 /cmd/eno/cobra_ssh.go | |
| parent | 8ebc072fb0ceae229196e8d3d8ff15bbbc049fdd (diff) | |
bunch of fixes
defer file.Close() after os.OpenFile
clear(sensitive) before each os.Exit(1)
Shared utils.StdinScanner with 512KB buffer
bytes.Equal for password comparison, append instead of string() + []byte()
Diffstat (limited to 'cmd/eno/cobra_ssh.go')
| -rw-r--r-- | cmd/eno/cobra_ssh.go | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/cmd/eno/cobra_ssh.go b/cmd/eno/cobra_ssh.go index f684e95..084960a 100644 --- a/cmd/eno/cobra_ssh.go +++ b/cmd/eno/cobra_ssh.go @@ -1,7 +1,6 @@ package main import ( - "bufio" "crypto" "errors" "fmt" @@ -39,8 +38,6 @@ var sshInitCmd = &cobra.Command{ Short: "setup ssh for Eno notes", Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { - scanner := bufio.NewScanner(os.Stdin) - if Conf.SSHInitialized { cmd.PrintErrln("SSH is already initialized") ok, err := utils.AskUserYesNo("Reinitialize SSH?", false) @@ -70,22 +67,22 @@ var sshInitCmd = &cobra.Command{ if privateKeyPath == "" { fmt.Print("Enter path to SSH private key: ") - if !scanner.Scan() { + if !utils.StdinScanner.Scan() { cmd.PrintErrln("failed to read input") os.Exit(1) } - privateKeyPath = expandPath(strings.TrimSpace(scanner.Text())) + privateKeyPath = expandPath(strings.TrimSpace(utils.StdinScanner.Text())) } publicKeyPath := privateKeyPath + ".pub" if _, err := os.Stat(publicKeyPath); os.IsNotExist(err) { cmd.PrintErrln("public key not found at " + publicKeyPath) fmt.Print("Enter path to SSH public key: ") - if !scanner.Scan() { + if !utils.StdinScanner.Scan() { cmd.PrintErrln("failed to read input") os.Exit(1) } - publicKeyPath = strings.TrimSpace(scanner.Text()) + publicKeyPath = strings.TrimSpace(utils.StdinScanner.Text()) publicKeyPath = expandPath(publicKeyPath) } @@ -110,14 +107,16 @@ var sshInitCmd = &cobra.Command{ fmt.Printf("Enter the ssh key passphrase: ") sshPassword, err := utils.TakeSecureUserInput() if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } crypt.TryMlock(sshPassword) defer clear(sshPassword) - privKey, err = ssh.ParseRawPrivateKeyWithPassphrase(privKeyBytes, []byte(sshPassword)) + privKey, err = ssh.ParseRawPrivateKeyWithPassphrase(privKeyBytes, sshPassword) } else { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } @@ -125,6 +124,7 @@ var sshInitCmd = &cobra.Command{ signer, ok := privKey.(crypto.Signer) if !ok { + clear(privKeyBytes) cmd.PrintErrln("failed to extract public key from private key") os.Exit(1) } @@ -132,30 +132,36 @@ var sshInitCmd = &cobra.Command{ givenPubKey, _, _, _, err := ssh.ParseAuthorizedKey(givenPubKeyBytes) if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } extractedPubKey, err := ssh.NewPublicKey(extractedPubKeyRaw) if err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if ssh.FingerprintSHA256(extractedPubKey) != ssh.FingerprintSHA256(givenPubKey) { + clear(privKeyBytes) cmd.PrintErrln("public key file does not match private key") os.Exit(1) } if err := updateConfig("DefaultSSHPrivPath", privateKeyPath); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if err := updateConfig("DefaultSSHPubPath", publicKeyPath); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } if err := updateConfig("SSHInitialized", true); err != nil { + clear(privKeyBytes) cmd.PrintErrln(err) os.Exit(1) } |