Skip to content

Commit

Permalink
encryption: log streamId in "message too long" error (#681)
Browse files Browse the repository at this point in the history
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

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
miguel-nascimento authored Aug 9, 2024
1 parent 11c9147 commit db61ce6
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/encryption/src/encryptionDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ 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')
}

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}`,
)
}
}
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit db61ce6

Please sign in to comment.