PGP decryption always fails on server-side encrypted mail (raw armor passed to pgpmail.Read)
Summary
PGP decryption of incoming mail fails with
failed to decrypt PGP message: message: malformed MIME header line: -----BEGIN PGP MESSAGE-
whenever the encryption was applied server-side (e.g. mailbox.org's "Encrypted Inbox" Sieve rule). Decryption of matcha-sent PGP mail works; server-encrypted mail never works. The root cause is in decryptPGPMessage in fetcher/fetcher.go.
Environment
- matcha 0.44.0 (also reproduced on 0.39.0; still present in v1.0.0-rc2)
- macOS (Apple Silicon), Homebrew cask
- Provider: mailbox.org with "PGP encryption for incoming mails" (Sieve auto-encrypts inbound mail to the account's public key, RFC 3156 PGP/MIME)
Reproduction
-
Enable mailbox.org Encrypted Inbox (Sieve encrypts inbound mail to your public key).
-
Receive any email. On the server it is stored as
multipart/encryptedwith anapplication/octet-streampart containing the ASCII-armored-----BEGIN PGP MESSAGE-----block. -
Open the message in matcha. It always shows:
PGP Decryption Failed: failed to decrypt PGP message: message: malformed MIME header line: -----BEGIN PGP MESSAGE- -
The same message decrypts cleanly with
gpg --decryptusing the same private key, so the key, passphrase, and ciphertext are fine.
Root cause
decryptPGPMessage (fetcher/fetcher.go, ~L2091-2134) is called with the bare armored payload extracted from the application/octet-stream part (see the part-walk in checkPart, ~L1042-1102). It then passes those raw bytes straight to:
mr, err := pgpmail.Read(bytes.NewReader(encryptedData), openpgp.EntityList{entityList[0]}, nil, nil)
pgpmail.Read (emersion/go-pgpmail v0.2.2) expects a complete RFC 822 MIME entity — it runs textproto.ReadHeader on the input first. The armor's first line -----BEGIN PGP MESSAGE----- contains no colon, so go-message returns malformed MIME header line: -----BEGIN PGP MESSAGE----- and decryption aborts before the private key is ever used.
Suggested fix (either approach)
- Armor path: if the payload starts with
-----BEGIN PGP MESSAGE-----, decode withopenpgp.ReadArmoredMessageand decrypt withopenpgp.Decryptdirectly, then feed the plaintext tomail.CreateReader(the caller already does this parse); or - MIME path: synthesize a minimal RFC 3156 wrapper around the extracted part (
multipart/encrypted+application/pgp-encryptedversion part + the octet-stream part) and pass that topgpmail.Read.
Why this matters
Server-side encryption (mailbox.org Sieve) is currently the only practical way to get encrypted mail at rest over open standards, since it requires no sender-side PGP. Any matcha user with mailbox.org's Encrypted Inbox hits this on every single message.
Diagnosis and root-cause analysis assisted by Crush (CLI AI agent, model GLM by Z.ai). Reproduction environment and verification (gpg round-trip test on the same key pair) performed on the reporter's machine.
Fixed on master!