From 9177cfd5b01117b05322ab76f3bf0941244af5ec Mon Sep 17 00:00:00 2001 From: Faye Duxovni Date: Thu, 25 Aug 2022 18:47:46 -0400 Subject: [PATCH 1/3] Use deep equality comparisons when searching for outgoing key requests by target --- .../crypto/outgoing-room-key-requests.spec.ts | 20 +++++++++++++++++++ .../store/indexeddb-crypto-store-backend.ts | 6 ++++-- src/crypto/store/memory-crypto-store.ts | 4 +++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/spec/unit/crypto/outgoing-room-key-requests.spec.ts b/spec/unit/crypto/outgoing-room-key-requests.spec.ts index c572a63ebef..f6b3cefd6c4 100644 --- a/spec/unit/crypto/outgoing-room-key-requests.spec.ts +++ b/spec/unit/crypto/outgoing-room-key-requests.spec.ts @@ -28,16 +28,27 @@ const requests = [ requestId: "A", requestBody: { session_id: "A", room_id: "A" }, state: RoomKeyRequestState.Sent, + recipients: [ + { userId: "@alice:example.com", deviceId: "*" }, + { userId: "@becca:example.com", deviceId: "foobarbaz" }, + ], }, { requestId: "B", requestBody: { session_id: "B", room_id: "B" }, state: RoomKeyRequestState.Sent, + recipients: [ + { userId: "@alice:example.com", deviceId: "*" }, + { userId: "@carrie:example.com", deviceId: "barbazquux" }, + ], }, { requestId: "C", requestBody: { session_id: "C", room_id: "C" }, state: RoomKeyRequestState.Unsent, + recipients: [ + { userId: "@becca:example.com", deviceId: "foobarbaz" }, + ], }, ]; @@ -75,6 +86,15 @@ describe.each([ }); }); + it("getOutgoingRoomKeyRequestsByTarget retrieves all entries with a given target", + async () => { + const r = await store.getOutgoingRoomKeyRequestsByTarget( + "@becca:example.com", "foobarbaz", [RoomKeyRequestState.Sent], + ); + expect(r).toHaveLength(1); + expect(r[0]).toEqual(requests[0]); + }); + test("getOutgoingRoomKeyRequestByState retrieves any entry in a given state", async () => { const r = diff --git a/src/crypto/store/indexeddb-crypto-store-backend.ts b/src/crypto/store/indexeddb-crypto-store-backend.ts index 6666fcf0165..2611a5f2a35 100644 --- a/src/crypto/store/indexeddb-crypto-store-backend.ts +++ b/src/crypto/store/indexeddb-crypto-store-backend.ts @@ -26,7 +26,7 @@ import { Mode, OutgoingRoomKeyRequest, } from "./base"; -import { IRoomKeyRequestBody } from "../index"; +import { IRoomKeyRequestBody, IRoomKeyRequestRecipient } from "../index"; import { ICrossSigningKey } from "../../client"; import { IOlmDevice } from "../algorithms/megolm"; import { IRoomEncryption } from "../RoomList"; @@ -261,7 +261,9 @@ export class Backend implements CryptoStore { const cursor = this.result; if (cursor) { const keyReq = cursor.value; - if (keyReq.recipients.includes({ userId, deviceId })) { + if (keyReq.recipients.some((recipient: IRoomKeyRequestRecipient) => + recipient.userId === userId && recipient.deviceId === deviceId, + )) { results.push(keyReq); } cursor.continue(); diff --git a/src/crypto/store/memory-crypto-store.ts b/src/crypto/store/memory-crypto-store.ts index 2e441908e3c..0acad891c2b 100644 --- a/src/crypto/store/memory-crypto-store.ts +++ b/src/crypto/store/memory-crypto-store.ts @@ -195,7 +195,9 @@ export class MemoryCryptoStore implements CryptoStore { for (const req of this.outgoingRoomKeyRequests) { for (const state of wantedStates) { - if (req.state === state && req.recipients.includes({ userId, deviceId })) { + if (req.state === state && req.recipients.some( + (recipient) => recipient.userId === userId && recipient.deviceId === deviceId, + )) { results.push(req); } } From 9ae4e35912febd966a0c83a890fbb5a63c9c1278 Mon Sep 17 00:00:00 2001 From: Faye Duxovni Date: Thu, 25 Aug 2022 19:07:14 -0400 Subject: [PATCH 2/3] Fix typescript strict mode errors --- .../crypto/outgoing-room-key-requests.spec.ts | 26 +++++++------------ src/crypto/store/memory-crypto-store.ts | 2 +- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/spec/unit/crypto/outgoing-room-key-requests.spec.ts b/spec/unit/crypto/outgoing-room-key-requests.spec.ts index f6b3cefd6c4..d5ba1328191 100644 --- a/spec/unit/crypto/outgoing-room-key-requests.spec.ts +++ b/spec/unit/crypto/outgoing-room-key-requests.spec.ts @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { - IndexedDBCryptoStore, -} from '../../../src/crypto/store/indexeddb-crypto-store'; +import { CryptoStore } from '../../../src/crypto/store/base'; +import { IndexedDBCryptoStore } from '../../../src/crypto/store/indexeddb-crypto-store'; import { MemoryCryptoStore } from '../../../src/crypto/store/memory-crypto-store'; +import { LocalStorageCryptoStore } from '../../../src/crypto/store/localStorage-crypto-store'; import { RoomKeyRequestState } from '../../../src/crypto/OutgoingRoomKeyRequestManager'; import 'fake-indexeddb/auto'; @@ -26,7 +26,7 @@ import 'jest-localstorage-mock'; const requests = [ { requestId: "A", - requestBody: { session_id: "A", room_id: "A" }, + requestBody: { session_id: "A", room_id: "A", sender_key: "A", algorithm: "m.megolm.v1.aes-sha2" }, state: RoomKeyRequestState.Sent, recipients: [ { userId: "@alice:example.com", deviceId: "*" }, @@ -35,7 +35,7 @@ const requests = [ }, { requestId: "B", - requestBody: { session_id: "B", room_id: "B" }, + requestBody: { session_id: "B", room_id: "B", sender_key: "B", algorithm: "m.megolm.v1.aes-sha2" }, state: RoomKeyRequestState.Sent, recipients: [ { userId: "@alice:example.com", deviceId: "*" }, @@ -44,7 +44,7 @@ const requests = [ }, { requestId: "C", - requestBody: { session_id: "C", room_id: "C" }, + requestBody: { session_id: "C", room_id: "C", sender_key: "B", algorithm: "m.megolm.v1.aes-sha2" }, state: RoomKeyRequestState.Unsent, recipients: [ { userId: "@becca:example.com", deviceId: "foobarbaz" }, @@ -55,18 +55,10 @@ const requests = [ describe.each([ ["IndexedDBCryptoStore", () => new IndexedDBCryptoStore(global.indexedDB, "tests")], - ["LocalStorageCryptoStore", - () => new IndexedDBCryptoStore(undefined, "tests")], - ["MemoryCryptoStore", () => { - const store = new IndexedDBCryptoStore(undefined, "tests"); - // @ts-ignore set private properties - store.backend = new MemoryCryptoStore(); - // @ts-ignore - store.backendPromise = Promise.resolve(store.backend); - return store; - }], + ["LocalStorageCryptoStore", () => new LocalStorageCryptoStore(localStorage)], + ["MemoryCryptoStore", () => new MemoryCryptoStore()], ])("Outgoing room key requests [%s]", function(name, dbFactory) { - let store; + let store: CryptoStore; beforeAll(async () => { store = dbFactory(); diff --git a/src/crypto/store/memory-crypto-store.ts b/src/crypto/store/memory-crypto-store.ts index 0acad891c2b..f62f52250be 100644 --- a/src/crypto/store/memory-crypto-store.ts +++ b/src/crypto/store/memory-crypto-store.ts @@ -191,7 +191,7 @@ export class MemoryCryptoStore implements CryptoStore { deviceId: string, wantedStates: number[], ): Promise { - const results = []; + const results: OutgoingRoomKeyRequest[] = []; for (const req of this.outgoingRoomKeyRequests) { for (const state of wantedStates) { From 8754d066c66b6aa88206b767b9e49f06a76edc09 Mon Sep 17 00:00:00 2001 From: Faye Duxovni Date: Fri, 26 Aug 2022 23:07:45 -0400 Subject: [PATCH 3/3] Re-alphabetize imports as no-op change to un-wedge github actions --- spec/unit/crypto/outgoing-room-key-requests.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/crypto/outgoing-room-key-requests.spec.ts b/spec/unit/crypto/outgoing-room-key-requests.spec.ts index d5ba1328191..1b1a4f57a7d 100644 --- a/spec/unit/crypto/outgoing-room-key-requests.spec.ts +++ b/spec/unit/crypto/outgoing-room-key-requests.spec.ts @@ -16,8 +16,8 @@ limitations under the License. import { CryptoStore } from '../../../src/crypto/store/base'; import { IndexedDBCryptoStore } from '../../../src/crypto/store/indexeddb-crypto-store'; -import { MemoryCryptoStore } from '../../../src/crypto/store/memory-crypto-store'; import { LocalStorageCryptoStore } from '../../../src/crypto/store/localStorage-crypto-store'; +import { MemoryCryptoStore } from '../../../src/crypto/store/memory-crypto-store'; import { RoomKeyRequestState } from '../../../src/crypto/OutgoingRoomKeyRequestManager'; import 'fake-indexeddb/auto';