From a7496627fcc327601a2973497064ccd8fa32263d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Tue, 28 Nov 2023 13:14:53 +0000 Subject: [PATCH] Explicitly free some Rust-side objects (#3911) * Explicitly `free` stuff returned by `OlmMachine.getIdentity()` * Explicitly `free` stuff returned by `OlmMachine.getDevice()` * one more --- spec/unit/rust-crypto/rust-crypto.spec.ts | 3 +- src/rust-crypto/CrossSigningIdentity.ts | 11 +- src/rust-crypto/rust-crypto.ts | 175 ++++++++++++++-------- 3 files changed, 120 insertions(+), 69 deletions(-) diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 1044c71a0..7685f811d 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -645,6 +645,7 @@ describe("RustCrypto", () => { it("should call getDevice", async () => { olmMachine.getDevice.mockResolvedValue({ + free: jest.fn(), isCrossSigningTrusted: jest.fn().mockReturnValue(false), isLocallyTrusted: jest.fn().mockReturnValue(false), isCrossSignedByOwner: jest.fn().mockReturnValue(false), @@ -871,7 +872,7 @@ describe("RustCrypto", () => { }); it("returns a verified UserVerificationStatus when the UserIdentity is verified", async () => { - olmMachine.getIdentity.mockResolvedValue({ isVerified: jest.fn().mockReturnValue(true) }); + olmMachine.getIdentity.mockResolvedValue({ free: jest.fn(), isVerified: jest.fn().mockReturnValue(true) }); const userVerificationStatus = await rustCrypto.getUserVerificationStatus(testData.TEST_USER_ID); expect(userVerificationStatus.isVerified()).toBeTruthy(); diff --git a/src/rust-crypto/CrossSigningIdentity.ts b/src/rust-crypto/CrossSigningIdentity.ts index 8598dab61..c0bccec10 100644 --- a/src/rust-crypto/CrossSigningIdentity.ts +++ b/src/rust-crypto/CrossSigningIdentity.ts @@ -91,10 +91,13 @@ export class CrossSigningIdentity { this.olmMachine.userId, this.olmMachine.deviceId, ); - - // Sign the device with our cross-signing key and upload the signature - const request: RustSdkCryptoJs.SignatureUploadRequest = await device.verify(); - await this.outgoingRequestProcessor.makeOutgoingRequest(request); + try { + // Sign the device with our cross-signing key and upload the signature + const request: RustSdkCryptoJs.SignatureUploadRequest = await device.verify(); + await this.outgoingRequestProcessor.makeOutgoingRequest(request); + } finally { + device.free(); + } } else { logger.log( "bootStrapCrossSigning: Cross-signing private keys not found locally or in secret storage, creating new keys", diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index f0c3c8973..dd90a44b6 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -425,6 +425,7 @@ export class RustCrypto extends TypedEventEmitter - verificationMethodIdentifierToMethod(method), - ); - // Get the request content to send to the DM room - const verificationEventContent: string = await userIdentity.verificationRequestContent(methods); + try { + // Transform the verification methods into rust objects + const methods = this._supportedVerificationMethods.map((method) => + verificationMethodIdentifierToMethod(method), + ); + // Get the request content to send to the DM room + const verificationEventContent: string = await userIdentity.verificationRequestContent(methods); - // Send the request content to send to the DM room - const eventId = await this.sendVerificationRequestContent(roomId, verificationEventContent); + // Send the request content to send to the DM room + const eventId = await this.sendVerificationRequestContent(roomId, verificationEventContent); - // Get a verification request - const request: RustSdkCryptoJs.VerificationRequest = await userIdentity.requestVerification( - new RustSdkCryptoJs.RoomId(roomId), - new RustSdkCryptoJs.EventId(eventId), - methods, - ); - return new RustVerificationRequest(request, this.outgoingRequestProcessor, this._supportedVerificationMethods); + // Get a verification request + const request: RustSdkCryptoJs.VerificationRequest = await userIdentity.requestVerification( + new RustSdkCryptoJs.RoomId(roomId), + new RustSdkCryptoJs.EventId(eventId), + methods, + ); + return new RustVerificationRequest( + request, + this.outgoingRequestProcessor, + this._supportedVerificationMethods, + ); + } finally { + userIdentity.free(); + } } /** @@ -995,12 +1026,20 @@ export class RustCrypto extends TypedEventEmitter