From 1f0c6a6dc9f14137b8b7348f0d33aff5b92a725f Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Tue, 21 Feb 2023 17:13:43 +0100 Subject: [PATCH] Add easy way to determine if the decryption failure is due to "DecryptionError: The sender has disabled encrypting to unverified devices." (#3167) * Add isEncryptedDisabledForUnverifiedDevices in event.ts * Add Tests * Add isEncryptedDisabledForUnverifiedDevices properties to event * Use WITHHELD_MESSAGES instead of hardcoded string * Use getter instead of function * Add documentation --- spec/unit/models/event.spec.ts | 19 +++++++++++++++++++ src/@types/crypto.ts | 4 ++++ src/models/event.ts | 17 +++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/spec/unit/models/event.spec.ts b/spec/unit/models/event.spec.ts index 244f9214521..dd21893a52c 100644 --- a/spec/unit/models/event.spec.ts +++ b/spec/unit/models/event.spec.ts @@ -126,12 +126,31 @@ describe("MatrixEvent", () => { expect(encryptedEvent.isEncrypted()).toBeTruthy(); expect(encryptedEvent.isBeingDecrypted()).toBeFalsy(); expect(encryptedEvent.isDecryptionFailure()).toBeTruthy(); + expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices).toBeFalsy(); expect(encryptedEvent.getContent()).toEqual({ msgtype: "m.bad.encrypted", body: "** Unable to decrypt: Error: test error **", }); }); + it(`should report "DecryptionError: The sender has disabled encrypting to unverified devices."`, async () => { + const crypto = { + decryptEvent: jest + .fn() + .mockRejectedValue("DecryptionError: The sender has disabled encrypting to unverified devices."), + } as unknown as Crypto; + + await encryptedEvent.attemptDecryption(crypto); + expect(encryptedEvent.isEncrypted()).toBeTruthy(); + expect(encryptedEvent.isBeingDecrypted()).toBeFalsy(); + expect(encryptedEvent.isDecryptionFailure()).toBeTruthy(); + expect(encryptedEvent.isEncryptedDisabledForUnverifiedDevices).toBeTruthy(); + expect(encryptedEvent.getContent()).toEqual({ + msgtype: "m.bad.encrypted", + body: "** Unable to decrypt: DecryptionError: The sender has disabled encrypting to unverified devices. **", + }); + }); + it("should retry decryption if a retry is queued", async () => { const eventAttemptDecryptionSpy = jest.spyOn(encryptedEvent, "attemptDecryption"); diff --git a/src/@types/crypto.ts b/src/@types/crypto.ts index a81ea57da0c..77118401436 100644 --- a/src/@types/crypto.ts +++ b/src/@types/crypto.ts @@ -44,6 +44,10 @@ export interface IEventDecryptionResult { */ claimedEd25519Key?: string; untrusted?: boolean; + /** + * The sender doesn't authorize the unverified devices to decrypt his messages + */ + encryptedDisabledForUnverifiedDevices?: boolean; } interface Extensible { diff --git a/src/models/event.ts b/src/models/event.ts index 5e0c1276123..d4baa5b224b 100644 --- a/src/models/event.ts +++ b/src/models/event.ts @@ -36,6 +36,7 @@ import { TypedEventEmitter } from "./typed-event-emitter"; import { EventStatus } from "./event-status"; import { DecryptionError } from "../crypto/algorithms"; import { CryptoBackend } from "../common-crypto/CryptoBackend"; +import { WITHHELD_MESSAGES } from "../crypto/OlmDevice"; export { EventStatus } from "./event-status"; @@ -272,6 +273,12 @@ export class MatrixEvent extends TypedEventEmitter