|
| 1 | +/* |
| 2 | +Copyright 2023 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import fetchMock from "fetch-mock-jest"; |
| 18 | +import "fake-indexeddb/auto"; |
| 19 | +import { IDBFactory } from "fake-indexeddb"; |
| 20 | + |
| 21 | +import { CRYPTO_BACKENDS, InitCrypto } from "../test-utils/test-utils"; |
| 22 | +import { createClient, MatrixClient, UIAuthCallback } from "../../src"; |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + // reset fake-indexeddb after each test, to make sure we don't leak connections |
| 26 | + // cf https://github.com/dumbmatter/fakeIndexedDB#wipingresetting-the-indexeddb-for-a-fresh-state |
| 27 | + // eslint-disable-next-line no-global-assign |
| 28 | + indexedDB = new IDBFactory(); |
| 29 | +}); |
| 30 | + |
| 31 | +const TEST_USER_ID = "@alice:localhost"; |
| 32 | +const TEST_DEVICE_ID = "xzcvb"; |
| 33 | + |
| 34 | +/** |
| 35 | + * Integration tests for cross-signing functionality. |
| 36 | + * |
| 37 | + * These tests work by intercepting HTTP requests via fetch-mock rather than mocking out bits of the client, so as |
| 38 | + * to provide the most effective integration tests possible. |
| 39 | + */ |
| 40 | +describe.each(Object.entries(CRYPTO_BACKENDS))("cross-signing (%s)", (backend: string, initCrypto: InitCrypto) => { |
| 41 | + let aliceClient: MatrixClient; |
| 42 | + |
| 43 | + beforeEach(async () => { |
| 44 | + // anything that we don't have a specific matcher for silently returns a 404 |
| 45 | + fetchMock.catch(404); |
| 46 | + fetchMock.config.warnOnFallback = false; |
| 47 | + |
| 48 | + const homeserverUrl = "https://alice-server.com"; |
| 49 | + aliceClient = createClient({ |
| 50 | + baseUrl: homeserverUrl, |
| 51 | + userId: TEST_USER_ID, |
| 52 | + accessToken: "akjgkrgjs", |
| 53 | + deviceId: TEST_DEVICE_ID, |
| 54 | + }); |
| 55 | + |
| 56 | + await initCrypto(aliceClient); |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(async () => { |
| 60 | + await aliceClient.stopClient(); |
| 61 | + fetchMock.mockReset(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("bootstrapCrossSigning (before initialsync completes)", () => { |
| 65 | + it("publishes keys if none were yet published", async () => { |
| 66 | + // have account_data requests return an empty object |
| 67 | + fetchMock.get("express:/_matrix/client/r0/user/:userId/account_data/:type", {}); |
| 68 | + |
| 69 | + // we expect a request to upload signatures for our device ... |
| 70 | + fetchMock.post({ url: "path:/_matrix/client/v3/keys/signatures/upload", name: "upload-sigs" }, {}); |
| 71 | + |
| 72 | + // ... and one to upload the cross-signing keys (with UIA) |
| 73 | + fetchMock.post( |
| 74 | + // legacy crypto uses /unstable/; /v3/ is correct |
| 75 | + { |
| 76 | + url: new RegExp("/_matrix/client/(unstable|v3)/keys/device_signing/upload"), |
| 77 | + name: "upload-keys", |
| 78 | + }, |
| 79 | + {}, |
| 80 | + ); |
| 81 | + |
| 82 | + // provide a UIA callback, so that the cross-signing keys are uploaded |
| 83 | + const authDict = { type: "test" }; |
| 84 | + const uiaCallback: UIAuthCallback<void> = async (makeRequest) => { |
| 85 | + await makeRequest(authDict); |
| 86 | + }; |
| 87 | + |
| 88 | + // now bootstrap cross signing, and check it resolves successfully |
| 89 | + await aliceClient.bootstrapCrossSigning({ |
| 90 | + authUploadDeviceSigningKeys: uiaCallback, |
| 91 | + }); |
| 92 | + |
| 93 | + // check the cross-signing keys upload |
| 94 | + expect(fetchMock.called("upload-keys")).toBeTruthy(); |
| 95 | + const [, keysOpts] = fetchMock.lastCall("upload-keys")!; |
| 96 | + const keysBody = JSON.parse(keysOpts!.body as string); |
| 97 | + expect(keysBody.auth).toEqual(authDict); // check uia dict was passed |
| 98 | + // there should be a key of each type |
| 99 | + // master key is signed by the device |
| 100 | + expect(keysBody).toHaveProperty(`master_key.signatures.[${TEST_USER_ID}].[ed25519:${TEST_DEVICE_ID}]`); |
| 101 | + const masterKeyId = Object.keys(keysBody.master_key.keys)[0]; |
| 102 | + // ssk and usk are signed by the master key |
| 103 | + expect(keysBody).toHaveProperty(`self_signing_key.signatures.[${TEST_USER_ID}].[${masterKeyId}]`); |
| 104 | + expect(keysBody).toHaveProperty(`user_signing_key.signatures.[${TEST_USER_ID}].[${masterKeyId}]`); |
| 105 | + const sskId = Object.keys(keysBody.self_signing_key.keys)[0]; |
| 106 | + |
| 107 | + // check the publish call |
| 108 | + expect(fetchMock.called("upload-sigs")).toBeTruthy(); |
| 109 | + const [, sigsOpts] = fetchMock.lastCall("upload-sigs")!; |
| 110 | + const body = JSON.parse(sigsOpts!.body as string); |
| 111 | + // there should be a signature for our device, by our self-signing key. |
| 112 | + expect(body).toHaveProperty( |
| 113 | + `[${TEST_USER_ID}].[${TEST_DEVICE_ID}].signatures.[${TEST_USER_ID}].[${sskId}]`, |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments