1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-06 12:02:40 +03:00

Element-R: ensure that userHasCrossSigningKeys uses up-to-date data (#3599)

* Element-R: ensure that `userHasCrossSigningKeys` uses up-to-date data

* Bump matrix-sdk-crypto-js
This commit is contained in:
Richard van der Hoff
2023-07-13 11:46:56 +01:00
committed by GitHub
parent 008294cfc6
commit 13fec49e74
4 changed files with 43 additions and 54 deletions

View File

@@ -55,7 +55,7 @@
], ],
"dependencies": { "dependencies": {
"@babel/runtime": "^7.12.5", "@babel/runtime": "^7.12.5",
"@matrix-org/matrix-sdk-crypto-js": "^0.1.3", "@matrix-org/matrix-sdk-crypto-js": "^0.1.4",
"another-json": "^0.2.0", "another-json": "^0.2.0",
"bs58": "^5.0.0", "bs58": "^5.0.0",
"content-type": "^1.0.4", "content-type": "^1.0.4",

View File

@@ -34,7 +34,7 @@ import {
import { mkEvent } from "../../test-utils/test-utils"; import { mkEvent } from "../../test-utils/test-utils";
import { CryptoBackend } from "../../../src/common-crypto/CryptoBackend"; import { CryptoBackend } from "../../../src/common-crypto/CryptoBackend";
import { IEventDecryptionResult } from "../../../src/@types/crypto"; import { IEventDecryptionResult } from "../../../src/@types/crypto";
import { OutgoingRequest, OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor"; import { OutgoingRequestProcessor } from "../../../src/rust-crypto/OutgoingRequestProcessor";
import { ServerSideSecretStorage } from "../../../src/secret-storage"; import { ServerSideSecretStorage } from "../../../src/secret-storage";
import { CryptoCallbacks, ImportRoomKeysOpts, VerificationRequest } from "../../../src/crypto-api"; import { CryptoCallbacks, ImportRoomKeysOpts, VerificationRequest } from "../../../src/crypto-api";
import * as testData from "../../test-utils/test-data"; import * as testData from "../../test-utils/test-data";
@@ -42,6 +42,10 @@ import * as testData from "../../test-utils/test-data";
const TEST_USER = "@alice:example.com"; const TEST_USER = "@alice:example.com";
const TEST_DEVICE_ID = "TEST_DEVICE"; const TEST_DEVICE_ID = "TEST_DEVICE";
afterEach(() => {
fetchMock.reset();
});
describe("RustCrypto", () => { describe("RustCrypto", () => {
describe(".importRoomKeys and .exportRoomKeys", () => { describe(".importRoomKeys and .exportRoomKeys", () => {
let rustCrypto: RustCrypto; let rustCrypto: RustCrypto;
@@ -390,60 +394,39 @@ describe("RustCrypto", () => {
let rustCrypto: RustCrypto; let rustCrypto: RustCrypto;
beforeEach(async () => { beforeEach(async () => {
rustCrypto = await makeTestRustCrypto(undefined, testData.TEST_USER_ID); rustCrypto = await makeTestRustCrypto(
}); new MatrixHttpApi(new TypedEventEmitter<HttpApiEvent, HttpApiEventHandlerMap>(), {
baseUrl: "http://server/",
afterEach(() => { prefix: "",
jest.useRealTimers(); onlyData: true,
});
it("returns false initially", async () => {
jest.useFakeTimers();
const prom = rustCrypto.userHasCrossSigningKeys();
// the getIdentity() request should wait for a /keys/query request to complete, but times out after 1500ms
await jest.advanceTimersByTimeAsync(2000);
await expect(prom).resolves.toBe(false);
});
it("returns false if there is no cross-signing identity", async () => {
// @ts-ignore private field
const olmMachine = rustCrypto.olmMachine;
const outgoingRequests: OutgoingRequest[] = await olmMachine.outgoingRequests();
// pick out the KeysQueryRequest, and respond to it with the device keys but *no* cross-signing keys.
const req = outgoingRequests.find((r) => r instanceof KeysQueryRequest)!;
await olmMachine.markRequestAsSent(
req.id!,
req.type,
JSON.stringify({
device_keys: {
[testData.TEST_USER_ID]: { [testData.TEST_DEVICE_ID]: testData.SIGNED_TEST_DEVICE_DATA },
},
}), }),
testData.TEST_USER_ID,
); );
});
it("throws an error if the fetch fails", async () => {
fetchMock.post("path:/_matrix/client/v3/keys/query", 400);
await expect(rustCrypto.userHasCrossSigningKeys()).rejects.toThrow("400 error");
});
it("returns false if the user has no cross-signing keys", async () => {
fetchMock.post("path:/_matrix/client/v3/keys/query", {
device_keys: {
[testData.TEST_USER_ID]: { [testData.TEST_DEVICE_ID]: testData.SIGNED_TEST_DEVICE_DATA },
},
});
await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(false); await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(false);
}); });
it("returns true if OlmMachine has a cross-signing identity", async () => { it("returns true if the user has cross-signing keys", async () => {
// @ts-ignore private field fetchMock.post("path:/_matrix/client/v3/keys/query", {
const olmMachine = rustCrypto.olmMachine; device_keys: {
[testData.TEST_USER_ID]: { [testData.TEST_DEVICE_ID]: testData.SIGNED_TEST_DEVICE_DATA },
},
...testData.SIGNED_CROSS_SIGNING_KEYS_DATA,
});
const outgoingRequests: OutgoingRequest[] = await olmMachine.outgoingRequests();
// pick out the KeysQueryRequest, and respond to it with the cross-signing keys
const req = outgoingRequests.find((r) => r instanceof KeysQueryRequest)!;
await olmMachine.markRequestAsSent(
req.id!,
req.type,
JSON.stringify({
device_keys: {
[testData.TEST_USER_ID]: { [testData.TEST_DEVICE_ID]: testData.SIGNED_TEST_DEVICE_DATA },
},
...testData.SIGNED_CROSS_SIGNING_KEYS_DATA,
}),
);
// ... and we should now have cross-signing keys.
await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(true); await expect(rustCrypto.userHasCrossSigningKeys()).resolves.toBe(true);
}); });
}); });

View File

@@ -206,7 +206,13 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
* Implementation of {@link CryptoApi.userHasCrossSigningKeys}. * Implementation of {@link CryptoApi.userHasCrossSigningKeys}.
*/ */
public async userHasCrossSigningKeys(): Promise<boolean> { public async userHasCrossSigningKeys(): Promise<boolean> {
const userIdentity = await this.olmMachine.getIdentity(new RustSdkCryptoJs.UserId(this.userId)); const userId = new RustSdkCryptoJs.UserId(this.userId);
/* make sure we have an *up-to-date* idea of the user's cross-signing keys. This is important, because if we
* return "false" here, we will end up generating new cross-signing keys and replacing the existing ones.
*/
const request = this.olmMachine.queryKeysForUsers([userId]);
await this.outgoingRequestProcessor.makeOutgoingRequest(request);
const userIdentity = await this.olmMachine.getIdentity(userId);
return userIdentity !== undefined; return userIdentity !== undefined;
} }

View File

@@ -1530,10 +1530,10 @@
dependencies: dependencies:
lodash "^4.17.21" lodash "^4.17.21"
"@matrix-org/matrix-sdk-crypto-js@^0.1.3": "@matrix-org/matrix-sdk-crypto-js@^0.1.4":
version "0.1.3" version "0.1.4"
resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-js/-/matrix-sdk-crypto-js-0.1.3.tgz#19981e7613d3673d07c885a98d39276b5fe74ef0" resolved "https://registry.yarnpkg.com/@matrix-org/matrix-sdk-crypto-js/-/matrix-sdk-crypto-js-0.1.4.tgz#c13c7c8c3a1d8da08e6ad195d25e5e61cc402df7"
integrity sha512-RcRlE3wcMnE5ijACHIHmhXFogEEJdIcb/CbJ4rK1PCMduQ4yvxycVpMxwh7aKxFNitZbHZLCK7TfRzUpzjU2tw== integrity sha512-OxG84iSeR89zYLFeb+DCaFtZT+DDiIu+kTkqY8OYfhE5vpGLFX2sDVBRrAdos1IUqEoboDloDBR9+yU7hNRyog==
"@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.14.tgz": "@matrix-org/olm@https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.14.tgz":
version "3.2.14" version "3.2.14"