Skip to content

Commit

Permalink
Element-R: Avoid errors in VerificationRequest.generateQRCode when …
Browse files Browse the repository at this point in the history
…QR code is unavailable (#3779)

* Avoid `VerificationRequest.generateQRCode` to crash when QRCode is unavailable

* Add tests `can try to generate a QR code when QR code is not supported`
  • Loading branch information
florianduros authored Oct 5, 2023
1 parent 95baccf commit af63d9b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
23 changes: 23 additions & 0 deletions spec/integ/crypto/verification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,29 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
expect(request.phase).toEqual(VerificationPhase.Done);
});

it("can try to generate a QR code when QR code is not supported", async () => {
aliceClient = await startTestClient();
// we need cross-signing keys for a QR code verification
e2eKeyResponder.addCrossSigningData(SIGNED_CROSS_SIGNING_KEYS_DATA);
await waitForDeviceList();

// Alice sends a m.key.verification.request
const [, request] = await Promise.all([
expectSendToDeviceMessage("m.key.verification.request"),
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
]);
const transactionId = request.transactionId!;

// The dummy device replies with an m.key.verification.ready, indicating it can only use SaS
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
await waitForVerificationRequestChanged(request);
expect(request.phase).toEqual(VerificationPhase.Ready);

// Alice tries to generate a QR Code but it's unavailable
const qrCodeBuffer = await request.generateQRCode();
expect(qrCodeBuffer).toBeUndefined();
});

newBackendOnly("can verify another by scanning their QR code", async () => {
aliceClient = await startTestClient();
// we need cross-signing keys for a QR code verification
Expand Down
5 changes: 4 additions & 1 deletion src/rust-crypto/verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,10 @@ export class RustVerificationRequest
* Implementation of {@link Crypto.VerificationRequest#generateQRCode}.
*/
public async generateQRCode(): Promise<Buffer | undefined> {
const innerVerifier: RustSdkCryptoJs.Qr = await this.inner.generateQrCode();
const innerVerifier: RustSdkCryptoJs.Qr | undefined = await this.inner.generateQrCode();
// If we are unable to generate a QRCode, we return undefined
if (!innerVerifier) return;

return Buffer.from(innerVerifier.toBytes());
}

Expand Down

0 comments on commit af63d9b

Please sign in to comment.