Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add easy way to determine if the decryption failure is due to "DecryptionError: The sender has disabled encrypting to unverified devices." #3167

Merged
merged 7 commits into from
Feb 21, 2023
21 changes: 21 additions & 0 deletions spec/unit/models/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,30 @@ 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 **",
reason: "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. **",
reason: "DecryptionError: The sender has disabled encrypting to unverified devices.",
});
});

Expand Down
17 changes: 17 additions & 0 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,22 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
return this.clearEvent?.content?.msgtype === "m.bad.encrypted";
}

/**
* Check if this event is an encrypted event which we failed to decrypt, the receiver's device is unverified and
* the sender has disabled encrypting to unverified devices.
*
* (This implies that we might retry decryption at some point in the future)
*
* @returns boolean
*/
public isEncryptedDisabledForUnverifiedDevices(): boolean {
florianduros marked this conversation as resolved.
Show resolved Hide resolved
return (
this.isDecryptionFailure() &&
this.clearEvent?.content?.reason ===
"DecryptionError: The sender has disabled encrypting to unverified devices."
florianduros marked this conversation as resolved.
Show resolved Hide resolved
);
}

public shouldAttemptDecryption(): boolean {
if (this.isRedacted()) return false;
if (this.isBeingDecrypted()) return false;
Expand Down Expand Up @@ -897,6 +913,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
content: {
msgtype: "m.bad.encrypted",
body: "** Unable to decrypt: " + reason + " **",
reason,
},
},
};
Expand Down