From db61ce69a53dbbb49cc79a774149d3d033d95a48 Mon Sep 17 00:00:00 2001 From: Miguel Gomes Date: Fri, 9 Aug 2024 17:35:53 -0300 Subject: [PATCH] encryption: log streamId in "message too long" error (#681) I wasnt able to reproduce #647 anymore, but if we even hit it again at least we will have something in the log to inspect ## Summary by CodeRabbit - **New Features** - Enhanced logging capabilities for improved error reporting during encryption processes. - Added contextual information to error messages for payload length checks, improving traceability. - **Bug Fixes** - Improved error handling by catching exceptions in the payload length check and logging detailed error messages. --- packages/encryption/src/encryptionDevice.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/encryption/src/encryptionDevice.ts b/packages/encryption/src/encryptionDevice.ts index 05f3cfd17..ed20e6e5a 100644 --- a/packages/encryption/src/encryptionDevice.ts +++ b/packages/encryption/src/encryptionDevice.ts @@ -40,7 +40,10 @@ export type EncryptionDeviceInitOpts = { pickleKey?: string } -function checkPayloadLength(payloadString: string): void { +function checkPayloadLength( + payloadString: string, + opts: { streamId?: string; source: string }, +): void { if (payloadString === undefined) { throw new Error('payloadString undefined') } @@ -48,10 +51,10 @@ function checkPayloadLength(payloadString: string): void { if (payloadString.length > MAX_PLAINTEXT_LENGTH) { // might as well fail early here rather than letting the olm library throw // a cryptic memory allocation error. - // throw new Error( `Message too long (${payloadString.length} bytes). ` + - `The maximum for an encrypted message is ${MAX_PLAINTEXT_LENGTH} bytes.`, + `The maximum for an encrypted message is ${MAX_PLAINTEXT_LENGTH} bytes.` + + `streamId: ${opts.streamId}, source: ${opts.source}`, ) } } @@ -567,7 +570,7 @@ export class EncryptionDevice { return await this.cryptoStore.withGroupSessions(async () => { log(`encrypting msg with group session for stream id ${streamId}`) - checkPayloadLength(payloadString) + checkPayloadLength(payloadString, { streamId, source: 'encryptGroupMessage' }) const session = await this.getOutboundGroupSession(streamId) const ciphertext = session.encrypt(payloadString) const sessionId = session.session_id() @@ -582,7 +585,7 @@ export class EncryptionDevice { fallbackKey: string, payload: string, ): Promise<{ type: 0 | 1; body: string }> { - checkPayloadLength(payload) + checkPayloadLength(payload, { source: 'encryptUsingFallbackKey' }) return this.cryptoStore.withAccountTx(async () => { const session = this.delegate.createSession() try { @@ -618,7 +621,7 @@ export class EncryptionDevice { throw new Error('Only pre-key messages supported') } - checkPayloadLength(ciphertext) + checkPayloadLength(ciphertext, { source: 'decryptMessage' }) return await this.cryptoStore.withAccountTx(async () => { const account = await this.getAccount() const session = this.delegate.createSession()