gpg: don't send messages that failed encryption

Add error handling for messages that were unable to be encrypted.
Previously, messages that failed encryption would be sent with no
content. This patch adds error handling - when encryption fails, the
user is returned to the Review screen and instructed to check the public
keys for their recipients.

Reported-by: Moritz Poldrack <moritz@poldrack.dev>
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Moritz Poldrack <moritz@poldrack.dev>
This commit is contained in:
Tim Culverhouse 2022-06-25 09:22:49 -05:00 committed by Robin Jarry
parent 96db50c4f0
commit 6a10123f4a
3 changed files with 11 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package gpgbin
import ( import (
"bytes" "bytes"
"fmt"
"io" "io"
"git.sr.ht/~rjarry/aerc/models" "git.sr.ht/~rjarry/aerc/models"
@ -27,7 +28,10 @@ func Encrypt(r io.Reader, to []string, from string) ([]byte, error) {
g.cmd.Run() g.cmd.Run()
outRdr := bytes.NewReader(g.stdout.Bytes()) outRdr := bytes.NewReader(g.stdout.Bytes())
var md models.MessageDetails var md models.MessageDetails
parse(outRdr, &md) err := parse(outRdr, &md)
if err != nil {
return nil, fmt.Errorf("gpg: failure to encrypt: %v. check public key(s)", err)
}
var buf bytes.Buffer var buf bytes.Buffer
io.Copy(&buf, md.Body) io.Copy(&buf, md.Body)

View file

@ -228,6 +228,8 @@ func parse(r io.Reader, md *models.MessageDetails) error {
md.Micalg = micalgs[micalg] md.Micalg = micalgs[micalg]
case "NODATA": case "NODATA":
md.SignatureError = "gpg: no signature packet found" md.SignatureError = "gpg: no signature packet found"
case "FAILURE":
return fmt.Errorf(strings.TrimPrefix(line, "[GNUPG:] "))
} }
} }
md.Body = bytes.NewReader(msgContent) md.Body = bytes.NewReader(msgContent)

View file

@ -598,7 +598,10 @@ func (c *Composer) WriteMessage(header *mail.Header, writer io.Writer) error {
if err != nil { if err != nil {
return err return err
} }
cleartext.Close() err = cleartext.Close()
if err != nil {
return err
}
io.Copy(writer, &buf) io.Copy(writer, &buf)
return nil return nil