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 /internal/crud/cli_new.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 'internal/crud/cli_new.go')
| -rw-r--r-- | internal/crud/cli_new.go | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/internal/crud/cli_new.go b/internal/crud/cli_new.go index a7408d7..6a2787d 100644 --- a/internal/crud/cli_new.go +++ b/internal/crud/cli_new.go @@ -1,7 +1,6 @@ package crud import ( - "bufio" "bytes" "fmt" "os" @@ -29,12 +28,12 @@ func NewNotePass(output string) error { crypt.TryMlock(passwordConfirm) defer clear(passwordConfirm) - if string(password) != string(passwordConfirm) { + if !bytes.Equal(password, passwordConfirm) { return fmt.Errorf("passphrases do not match") } encrypted, err := crypt.EncryptBytesPass( - []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^"), + append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...), password, ) if err != nil { @@ -46,6 +45,7 @@ func NewNotePass(output string) error { if err != nil { return err } + defer file.Close() _, err = file.Write(encrypted) if err != nil { @@ -77,14 +77,14 @@ func NewNotePassEdit(output, editorString string) error { crypt.TryMlock(passwordConfirm) defer clear(passwordConfirm) - if string(password) != string(passwordConfirm) { + if !bytes.Equal(password, passwordConfirm) { return fmt.Errorf("passphrases do not match") } noteInfo, err := os.Stat(output) preExisting := err == nil - initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, err := crypt.EncryptBytesPass(initialContent, password) if err != nil { return err @@ -140,7 +140,9 @@ func NewNotePassEdit(output, editorString string) error { crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, err = crypt.EncryptBytesPass(newContent, password) if err != nil { @@ -183,13 +185,12 @@ func PromptRecipientKeys(defaultPubKeyBytes []byte) ([][]byte, bool, error) { if addMore { for { fmt.Print("Enter a public key, or path to a public key file (comma-separated for multiple): ") - scanner := bufio.NewScanner(os.Stdin) - scanner.Scan() - if err := scanner.Err(); err != nil { + utils.StdinScanner.Scan() + if err := utils.StdinScanner.Err(); err != nil { return nil, false, fmt.Errorf("failed to read public key: %v", err) } - input := strings.TrimSpace(scanner.Text()) + input := strings.TrimSpace(utils.StdinScanner.Text()) if input == "" { continue } @@ -236,7 +237,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { return fmt.Errorf("at least one recipient public key is required") } - tmptxt := []byte(string(utils.RandomLettersNumbers(37))+"\nYou can Delete all of this ^^^") + tmptxt := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, _, err := crypt.EncryptBytesSSH(tmptxt, pubKeys) if err != nil { @@ -248,6 +249,7 @@ func NewNoteSSH(output string, defaultPubKeyBytes []byte) error { if err != nil { return err } + defer file.Close() _, err = file.Write(encrypted) if err != nil { @@ -274,7 +276,7 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath noteInfo, err := os.Stat(output) preExisting := err == nil - initialContent := []byte(string(utils.RandomLettersNumbers(37)) + "\nYou can Delete all of this ^^^") + initialContent := append(utils.RandomLettersNumbers(37), "\nYou can Delete all of this ^^^"...) encrypted, noteKey, err := crypt.EncryptBytesSSH(initialContent, pubKeys) if err != nil { return err @@ -286,6 +288,9 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath if err != nil { return err } + if err != nil { + return err + } _, err = file.Write(encrypted) if err != nil { @@ -337,7 +342,9 @@ func NewNoteSSHEdit(output string, defaultPubKeyBytes []byte, defaultPrivKeyPath crypt.TryMlock(newContent) defer clear(newContent) - os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600) + if err := os.WriteFile(tmpFile.Name(), make([]byte, len(newContent)), 0600); err != nil { + fmt.Fprintf(os.Stderr, "warning: failed to wipe temp file %s: %v\n", tmpFile.Name(), err) + } encrypted, _, err = crypt.EncryptBytesSSH(newContent, pubKeys) if err != nil { |