fix(email): enclose PGP encryption logic in try/catch (#2519)

This commit is contained in:
TheCatLady
2022-02-09 21:48:53 -05:00
committed by GitHub
parent 61681857b1
commit a76b608ab7

View File

@@ -1,6 +1,7 @@
import { randomBytes } from 'crypto'; import { randomBytes } from 'crypto';
import * as openpgp from 'openpgp'; import * as openpgp from 'openpgp';
import { Transform, TransformCallback } from 'stream'; import { Transform, TransformCallback } from 'stream';
import logger from '../../logger';
interface EncryptorOptions { interface EncryptorOptions {
signingKey?: string; signingKey?: string;
@@ -36,10 +37,14 @@ class PGPEncryptor extends Transform {
// Actually do stuff // Actually do stuff
_flush = async (callback: TransformCallback): Promise<void> => { _flush = async (callback: TransformCallback): Promise<void> => {
// Reconstruct message as buffer
const message = Buffer.concat(this._messageChunks, this._messageLength); const message = Buffer.concat(this._messageChunks, this._messageLength);
try {
// Reconstruct message as buffer
const validPublicKeys = await Promise.all( const validPublicKeys = await Promise.all(
this._encryptionKeys.map((armoredKey) => openpgp.readKey({ armoredKey })) this._encryptionKeys.map((armoredKey) =>
openpgp.readKey({ armoredKey })
)
); );
let privateKey: openpgp.PrivateKey | undefined; let privateKey: openpgp.PrivateKey | undefined;
@@ -163,6 +168,18 @@ class PGPEncryptor extends Transform {
this.push(Buffer.from(emailHeadersRaw + emailPartDelimiter + body)); this.push(Buffer.from(emailHeadersRaw + emailPartDelimiter + body));
callback(); callback();
} catch (e) {
logger.error(
'Something went wrong while encrypting email message with OpenPGP. Sending email without encryption',
{
label: 'Notifications',
errorMessage: e.message,
}
);
this.push(message);
callback();
}
}; };
} }