Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 33bbd45f1e | |||
| fd91a534c7 | |||
| c622e9260f | |||
| 8bf53d6f90 | |||
| 95f7d1d347 | |||
| 72d70bb929 | |||
| 4f67e59692 | |||
| d40d5c8a39 | |||
| 87398ac555 | |||
| de3d5ead42 | |||
| 1e1b571b28 | |||
| f400a7b1b2 | |||
| a0bcb5777f | |||
| f8a625eddb | |||
| 69a2a15b95 | |||
| b9d0596dd7 | |||
| 72af8c193c | |||
| ed8c326856 | |||
| f5bf6b1be6 | |||
| 0e19f8dc69 | |||
| 6049c0bf37 | |||
| a102253f30 | |||
| f71d86f005 | |||
| 70e34ffb76 | |||
| 91aa7b26e6 | |||
| 7d45947fb3 | |||
| 6e174328e8 | |||
| 5fb97fcce4 | |||
| 3d1a450129 | |||
| a58c5aacdf | |||
| d7e165a279 | |||
| a57ee803f1 | |||
| 170a52b09f | |||
| 2be5889d18 | |||
| ca6b574bee | |||
| 57b0172a2d | |||
| 53260ee25d | |||
| 964281322f | |||
| 7c3f483396 | |||
| 59784aa9fe | |||
| 72a2b6d571 | |||
| 5854af0eae | |||
| acd3d3a804 | |||
| 1b8c04a430 | |||
| 378b73f8b8 | |||
| c482a6ab15 | |||
| 2daa429b77 | |||
| 6ebbc15359 | |||
| 9a840d484c | |||
| e89467c9fb | |||
| 0b396c005c | |||
| d05313f95e |
@@ -8,7 +8,7 @@ on:
|
||||
secrets:
|
||||
ELEMENT_BOT_TOKEN:
|
||||
required: true
|
||||
concurrency: ${{ github.workflow }}-${{ github.event.pull_request.head.ref }}
|
||||
concurrency: ${{ github.workflow }}-${{ github.event.pull_request.head.ref || github.head_ref || github.ref }}
|
||||
jobs:
|
||||
changelog:
|
||||
name: Preview Changelog
|
||||
|
||||
@@ -46,7 +46,7 @@ jobs:
|
||||
fi
|
||||
|
||||
- name: 🚀 Deploy
|
||||
uses: peaceiris/actions-gh-pages@bd8c6b06eba6b3d25d72b7a1767993c0aeee42e7 # v3
|
||||
uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 # v3
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
keep_files: true
|
||||
|
||||
@@ -65,7 +65,7 @@ jobs:
|
||||
run: "yarn install"
|
||||
|
||||
- name: Generate Docs
|
||||
run: "yarn run gendoc"
|
||||
run: "yarn run gendoc --treatWarningsAsErrors"
|
||||
|
||||
- name: Upload Artifact
|
||||
uses: actions/upload-artifact@v3
|
||||
|
||||
+19
-27
@@ -8,6 +8,8 @@ on:
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
env:
|
||||
ENABLE_COVERAGE: ${{ github.event_name != 'merge_group' }}
|
||||
jobs:
|
||||
jest:
|
||||
name: "Jest [${{ matrix.specs }}] (Node ${{ matrix.node }})"
|
||||
@@ -38,28 +40,22 @@ jobs:
|
||||
id: cpu-cores
|
||||
uses: SimenB/github-actions-cpu-cores@410541432439795d30db6501fb1d8178eb41e502 # v1
|
||||
|
||||
- name: Load metrics reporter
|
||||
id: metrics
|
||||
if: github.ref == 'refs/heads/develop'
|
||||
run: |
|
||||
echo "extra-reporter='--reporters=<rootDir>/spec/slowReporter.js'" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
yarn ${{ github.event_name == 'merge_group' && 'test' || 'coverage' }} \
|
||||
yarn test \
|
||||
--coverage=${{ env.ENABLE_COVERAGE }} \
|
||||
--ci \
|
||||
--reporters github-actions ${{ steps.metrics.outputs.extra-reporter }} \
|
||||
--max-workers ${{ steps.cpu-cores.outputs.count }} \
|
||||
./spec/${{ matrix.specs }}
|
||||
env:
|
||||
JEST_SONAR_UNIQUE_OUTPUT_NAME: true
|
||||
|
||||
- name: Move coverage files into place
|
||||
if: github.event_name != 'merge_group'
|
||||
if: env.ENABLE_COVERAGE == 'true'
|
||||
run: mv coverage/lcov.info coverage/${{ matrix.node }}-${{ matrix.specs }}.lcov.info
|
||||
|
||||
- name: Upload Artifact
|
||||
if: github.event_name != 'merge_group'
|
||||
if: env.ENABLE_COVERAGE == 'true'
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: coverage
|
||||
@@ -67,22 +63,6 @@ jobs:
|
||||
coverage
|
||||
!coverage/lcov-report
|
||||
|
||||
skip_sonar:
|
||||
name: Skip SonarCloud on merge_queue
|
||||
if: github.event_name == 'merge_group'
|
||||
runs-on: ubuntu-latest
|
||||
needs: jest
|
||||
steps:
|
||||
- name: Skip SonarCloud
|
||||
uses: Sibz/github-status-action@faaa4d96fecf273bd762985e0e7f9f933c774918 # v1
|
||||
with:
|
||||
authToken: ${{ secrets.GITHUB_TOKEN }}
|
||||
state: success
|
||||
description: SonarCloud skipped
|
||||
context: SonarCloud Code Analysis
|
||||
sha: ${{ github.sha }}
|
||||
target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
|
||||
matrix-react-sdk:
|
||||
name: Downstream test matrix-react-sdk
|
||||
if: github.event_name == 'merge_group'
|
||||
@@ -91,7 +71,8 @@ jobs:
|
||||
disable_coverage: true
|
||||
matrix-js-sdk-sha: ${{ github.sha }}
|
||||
|
||||
# Hook for branch protection to work outside merge queues
|
||||
# Hook for branch protection to skip downstream testing outside of merge queues
|
||||
# and skip sonarcloud coverage within merge queues
|
||||
downstream:
|
||||
name: Downstream tests
|
||||
runs-on: ubuntu-latest
|
||||
@@ -99,5 +80,16 @@ jobs:
|
||||
needs:
|
||||
- matrix-react-sdk
|
||||
steps:
|
||||
- name: Skip SonarCloud on merge queues
|
||||
if: env.ENABLE_COVERAGE == 'false'
|
||||
uses: Sibz/github-status-action@faaa4d96fecf273bd762985e0e7f9f933c774918 # v1
|
||||
with:
|
||||
authToken: ${{ secrets.GITHUB_TOKEN }}
|
||||
state: success
|
||||
description: SonarCloud skipped
|
||||
context: SonarCloud Code Analysis
|
||||
sha: ${{ github.sha }}
|
||||
target_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
||||
|
||||
- if: needs.matrix-react-sdk.result != 'skipped' && needs.matrix-react-sdk.result != 'success'
|
||||
run: exit 1
|
||||
|
||||
@@ -20,7 +20,7 @@ jobs:
|
||||
|
||||
- name: Create Pull Request
|
||||
id: cpr
|
||||
uses: peter-evans/create-pull-request@2b011faafdcbc9ceb11414d64d0573f37c774b04 # v4
|
||||
uses: peter-evans/create-pull-request@38e0b6e68b4c852a5500a94740f0e535e0d7ba54 # v4
|
||||
with:
|
||||
token: ${{ secrets.ELEMENT_BOT_TOKEN }}
|
||||
branch: actions/upgrade-deps
|
||||
@@ -31,8 +31,8 @@ jobs:
|
||||
T-Task
|
||||
|
||||
- name: Enable automerge
|
||||
uses: peter-evans/enable-pull-request-automerge@684fed02ccc9b5eefcf7d40b65b3cd44255bd5bc # v2
|
||||
run: gh pr merge --merge --auto "$PR_NUMBER"
|
||||
if: steps.cpr.outputs.pull-request-operation == 'created'
|
||||
with:
|
||||
token: ${{ secrets.ELEMENT_BOT_TOKEN }}
|
||||
pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
|
||||
env:
|
||||
GH_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
|
||||
PR_NUMBER: ${{ steps.cpr.outputs.pull-request-number }}
|
||||
|
||||
+19
-2
@@ -1,5 +1,22 @@
|
||||
Changes in [24.1.0-rc.1](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v24.1.0-rc.1) (2023-04-04)
|
||||
============================================================================================================
|
||||
Changes in [25.0.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v25.0.0) (2023-04-25)
|
||||
==================================================================================================
|
||||
|
||||
## 🚨 BREAKING CHANGES
|
||||
* Change `Store.save()` to return a `Promise` ([\#3221](https://github.com/matrix-org/matrix-js-sdk/pull/3221)). Contributed by @texuf.
|
||||
|
||||
## ✨ Features
|
||||
* Add typedoc-plugin-mdn-links ([\#3292](https://github.com/matrix-org/matrix-js-sdk/pull/3292)).
|
||||
* Annotate events with executed push rule ([\#3284](https://github.com/matrix-org/matrix-js-sdk/pull/3284)). Contributed by @kerryarchibald.
|
||||
* Element-R: pass device list change notifications into rust crypto-sdk ([\#3254](https://github.com/matrix-org/matrix-js-sdk/pull/3254)). Fixes vector-im/element-web#24795. Contributed by @florianduros.
|
||||
* Support for MSC3882 revision 1 ([\#3228](https://github.com/matrix-org/matrix-js-sdk/pull/3228)). Contributed by @hughns.
|
||||
|
||||
## 🐛 Bug Fixes
|
||||
* Fix screen sharing on Firefox 113 ([\#3282](https://github.com/matrix-org/matrix-js-sdk/pull/3282)). Contributed by @tulir.
|
||||
* Retry processing potential poll events after decryption ([\#3246](https://github.com/matrix-org/matrix-js-sdk/pull/3246)). Fixes vector-im/element-web#24568.
|
||||
* Element-R: handle events which arrive before their keys ([\#3230](https://github.com/matrix-org/matrix-js-sdk/pull/3230)). Fixes vector-im/element-web#24489.
|
||||
|
||||
Changes in [24.1.0](https://github.com/matrix-org/matrix-js-sdk/releases/tag/v24.1.0) (2023-04-11)
|
||||
==================================================================================================
|
||||
|
||||
## ✨ Features
|
||||
* Allow via_servers property in findPredecessor (update to MSC3946) ([\#3240](https://github.com/matrix-org/matrix-js-sdk/pull/3240)). Contributed by @andybalaam.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/* Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import type { Config } from "jest";
|
||||
import { env } from "process";
|
||||
|
||||
const config: Config = {
|
||||
testEnvironment: "node",
|
||||
testMatch: ["<rootDir>/spec/**/*.spec.{js,ts}"],
|
||||
setupFilesAfterEnv: ["<rootDir>/spec/setupTests.ts"],
|
||||
collectCoverageFrom: ["<rootDir>/src/**/*.{js,ts}"],
|
||||
coverageReporters: ["text-summary", "lcov"],
|
||||
testResultsProcessor: "@casualbot/jest-sonar-reporter",
|
||||
};
|
||||
|
||||
// if we're running under GHA, enable the GHA reporter
|
||||
if (env["GITHUB_ACTIONS"] !== undefined) {
|
||||
const reporters: Config["reporters"] = [["github-actions", { silent: false }], "summary"];
|
||||
|
||||
// if we're running against the develop branch, also enable the slow test reporter
|
||||
if (env["GITHUB_REF"] == "refs/heads/develop") {
|
||||
reporters.push("<rootDir>/spec/slowReporter.js");
|
||||
}
|
||||
config.reporters = reporters;
|
||||
}
|
||||
|
||||
export default config;
|
||||
+7
-22
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "matrix-js-sdk",
|
||||
"version": "24.1.0-rc.1",
|
||||
"version": "25.0.0",
|
||||
"description": "Matrix Client-Server SDK for Javascript",
|
||||
"engines": {
|
||||
"node": ">=16.0.0"
|
||||
@@ -55,7 +55,7 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.12.5",
|
||||
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.5",
|
||||
"@matrix-org/matrix-sdk-crypto-js": "^0.1.0-alpha.6",
|
||||
"another-json": "^0.2.0",
|
||||
"bs58": "^5.0.0",
|
||||
"content-type": "^1.0.4",
|
||||
@@ -101,7 +101,7 @@
|
||||
"debug": "^4.3.4",
|
||||
"docdash": "^2.0.0",
|
||||
"domexception": "^4.0.0",
|
||||
"eslint": "8.35.0",
|
||||
"eslint": "8.37.0",
|
||||
"eslint-config-google": "^0.14.0",
|
||||
"eslint-config-prettier": "^8.5.0",
|
||||
"eslint-import-resolver-typescript": "^3.5.1",
|
||||
@@ -119,30 +119,15 @@
|
||||
"jest-localstorage-mock": "^2.4.6",
|
||||
"jest-mock": "^29.0.0",
|
||||
"matrix-mock-request": "^2.5.0",
|
||||
"prettier": "2.8.4",
|
||||
"prettier": "2.8.7",
|
||||
"rimraf": "^4.0.0",
|
||||
"terser": "^5.5.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"tsify": "^5.0.2",
|
||||
"typedoc": "^0.23.20",
|
||||
"typedoc-plugin-mdn-links": "^3.0.3",
|
||||
"typedoc-plugin-missing-exports": "^1.0.0",
|
||||
"typescript": "^4.5.3"
|
||||
},
|
||||
"jest": {
|
||||
"testEnvironment": "node",
|
||||
"testMatch": [
|
||||
"<rootDir>/spec/**/*.spec.{js,ts}"
|
||||
],
|
||||
"setupFilesAfterEnv": [
|
||||
"<rootDir>/spec/setupTests.ts"
|
||||
],
|
||||
"collectCoverageFrom": [
|
||||
"<rootDir>/src/**/*.{js,ts}"
|
||||
],
|
||||
"coverageReporters": [
|
||||
"text-summary",
|
||||
"lcov"
|
||||
],
|
||||
"testResultsProcessor": "@casualbot/jest-sonar-reporter"
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"@casualbot/jest-sonar-reporter": {
|
||||
"outputDirectory": "coverage",
|
||||
|
||||
+32
-38
@@ -624,10 +624,8 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
|
||||
expect(decryptedEvent.getContent().body).toEqual("42");
|
||||
});
|
||||
|
||||
oldBackendOnly("Alice receives a megolm message before the session keys", async () => {
|
||||
it("Alice receives a megolm message before the session keys", async () => {
|
||||
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
|
||||
|
||||
// https://github.com/vector-im/element-web/issues/2273
|
||||
await startClientAndAwaitFirstSync();
|
||||
|
||||
// if we're using the old crypto impl, stub out some methods in the device manager.
|
||||
@@ -667,7 +665,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
|
||||
await syncPromise(aliceClient);
|
||||
|
||||
const room = aliceClient.getRoom(ROOM_ID)!;
|
||||
expect(room.getLiveTimeline().getEvents()[0].getContent().msgtype).toEqual("m.bad.encrypted");
|
||||
const event = room.getLiveTimeline().getEvents()[0];
|
||||
|
||||
// wait for a first attempt at decryption: should fail
|
||||
await testUtils.awaitDecryption(event);
|
||||
expect(event.getContent().msgtype).toEqual("m.bad.encrypted");
|
||||
|
||||
// now she gets the room_key event
|
||||
syncResponder.sendOrQueueSyncResponse({
|
||||
@@ -678,20 +680,8 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
|
||||
});
|
||||
await syncPromise(aliceClient);
|
||||
|
||||
const event = room.getLiveTimeline().getEvents()[0];
|
||||
|
||||
let decryptedEvent: MatrixEvent;
|
||||
if (event.getContent().msgtype != "m.bad.encrypted") {
|
||||
decryptedEvent = event;
|
||||
} else {
|
||||
decryptedEvent = await new Promise<MatrixEvent>((resolve) => {
|
||||
event.once(MatrixEventEvent.Decrypted, (ev) => {
|
||||
logger.log(`${Date.now()} event ${event.getId()} now decrypted`);
|
||||
resolve(ev);
|
||||
});
|
||||
});
|
||||
}
|
||||
expect(decryptedEvent.getContent().body).toEqual("42");
|
||||
await testUtils.awaitDecryption(event, { waitOnDecryptionFailure: true });
|
||||
expect(event.getContent().body).toEqual("42");
|
||||
});
|
||||
|
||||
it("Alice gets a second room_key message", async () => {
|
||||
@@ -1947,51 +1937,51 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
|
||||
jest.useRealTimers();
|
||||
});
|
||||
|
||||
function listenToUpload(): Promise<number> {
|
||||
function awaitKeyUploadRequest(): Promise<{ keysCount: number; fallbackKeysCount: number }> {
|
||||
return new Promise((resolve) => {
|
||||
const listener = (url: string, options: RequestInit) => {
|
||||
const content = JSON.parse(options.body as string);
|
||||
const keysCount = Object.keys(content?.one_time_keys || {}).length;
|
||||
if (keysCount) resolve(keysCount);
|
||||
const fallbackKeysCount = Object.keys(content?.fallback_keys || {}).length;
|
||||
if (keysCount) resolve({ keysCount, fallbackKeysCount });
|
||||
return {
|
||||
one_time_key_counts: {
|
||||
// The matrix client does `/upload` requests until 50 keys are uploaded
|
||||
// We return here 60 to avoid the `/upload` request loop
|
||||
signed_curve25519: keysCount ? 60 : keysCount,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
// catch both r0 and v3 variants
|
||||
fetchMock.post(
|
||||
new URL("/_matrix/client/r0/keys/upload", aliceClient.getHomeserverUrl()).toString(),
|
||||
listener,
|
||||
{
|
||||
for (const path of ["/_matrix/client/r0/keys/upload", "/_matrix/client/v3/keys/upload"]) {
|
||||
fetchMock.post(new URL(path, aliceClient.getHomeserverUrl()).toString(), listener, {
|
||||
// These routes are already defined in the E2EKeyReceiver
|
||||
// We want to overwrite the behaviour of the E2EKeyReceiver
|
||||
overwriteRoutes: true,
|
||||
},
|
||||
);
|
||||
fetchMock.post(
|
||||
new URL("/_matrix/client/v3/keys/upload", aliceClient.getHomeserverUrl()).toString(),
|
||||
listener,
|
||||
{
|
||||
overwriteRoutes: true,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
it("should make key upload request after sync", async () => {
|
||||
let uploadPromise = listenToUpload();
|
||||
let uploadPromise = awaitKeyUploadRequest();
|
||||
expectAliceKeyQuery({ device_keys: { "@alice:localhost": {} }, failures: {} });
|
||||
await startClientAndAwaitFirstSync();
|
||||
|
||||
syncResponder.sendOrQueueSyncResponse(getSyncResponse([]));
|
||||
|
||||
await syncPromise(aliceClient);
|
||||
expect(await uploadPromise).toBeGreaterThan(0);
|
||||
|
||||
uploadPromise = listenToUpload();
|
||||
// Verify that `/upload` is called on Alice's homesever
|
||||
const { keysCount, fallbackKeysCount } = await uploadPromise;
|
||||
expect(keysCount).toBeGreaterThan(0);
|
||||
expect(fallbackKeysCount).toBe(0);
|
||||
|
||||
uploadPromise = awaitKeyUploadRequest();
|
||||
syncResponder.sendOrQueueSyncResponse({
|
||||
next_batch: 2,
|
||||
device_one_time_keys_count: { signed_curve25519: 0 },
|
||||
device_unused_fallback_key_types: [],
|
||||
});
|
||||
|
||||
// Advance local date to 2 minutes
|
||||
@@ -2000,7 +1990,11 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("crypto (%s)", (backend: string,
|
||||
|
||||
await syncPromise(aliceClient);
|
||||
|
||||
expect(await uploadPromise).toBeGreaterThan(0);
|
||||
// After we set device_one_time_keys_count to 0
|
||||
// a `/upload` is expected
|
||||
const res = await uploadPromise;
|
||||
expect(res.keysCount).toBeGreaterThan(0);
|
||||
expect(res.fallbackKeysCount).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1127,22 +1127,51 @@ describe("MatrixClient", function () {
|
||||
|
||||
describe("requestLoginToken", () => {
|
||||
it("should hit the expected API endpoint with UIA", async () => {
|
||||
httpBackend!
|
||||
.when("GET", "/capabilities")
|
||||
.respond(200, { capabilities: { "org.matrix.msc3882.get_login_token": { enabled: true } } });
|
||||
const response = {};
|
||||
const uiaData = {};
|
||||
const prom = client!.requestLoginToken(uiaData);
|
||||
httpBackend!
|
||||
.when("POST", "/unstable/org.matrix.msc3882/login/token", { auth: uiaData })
|
||||
.when("POST", "/unstable/org.matrix.msc3882/login/get_token", { auth: uiaData })
|
||||
.respond(200, response);
|
||||
await httpBackend!.flush("");
|
||||
expect(await prom).toStrictEqual(response);
|
||||
});
|
||||
|
||||
it("should hit the expected API endpoint without UIA", async () => {
|
||||
const response = {};
|
||||
httpBackend!
|
||||
.when("GET", "/capabilities")
|
||||
.respond(200, { capabilities: { "org.matrix.msc3882.get_login_token": { enabled: true } } });
|
||||
const response = { login_token: "xyz", expires_in_ms: 5000 };
|
||||
const prom = client!.requestLoginToken();
|
||||
httpBackend!.when("POST", "/unstable/org.matrix.msc3882/login/get_token", {}).respond(200, response);
|
||||
await httpBackend!.flush("");
|
||||
// check that expires_in has been populated for compatibility with r0
|
||||
expect(await prom).toStrictEqual({ ...response, expires_in: 5 });
|
||||
});
|
||||
|
||||
it("should hit the r1 endpoint when capability is disabled", async () => {
|
||||
httpBackend!
|
||||
.when("GET", "/capabilities")
|
||||
.respond(200, { capabilities: { "org.matrix.msc3882.get_login_token": { enabled: false } } });
|
||||
const response = { login_token: "xyz", expires_in_ms: 5000 };
|
||||
const prom = client!.requestLoginToken();
|
||||
httpBackend!.when("POST", "/unstable/org.matrix.msc3882/login/get_token", {}).respond(200, response);
|
||||
await httpBackend!.flush("");
|
||||
// check that expires_in has been populated for compatibility with r0
|
||||
expect(await prom).toStrictEqual({ ...response, expires_in: 5 });
|
||||
});
|
||||
|
||||
it("should hit the r0 endpoint for fallback", async () => {
|
||||
httpBackend!.when("GET", "/capabilities").respond(200, {});
|
||||
const response = { login_token: "xyz", expires_in: 5 };
|
||||
const prom = client!.requestLoginToken();
|
||||
httpBackend!.when("POST", "/unstable/org.matrix.msc3882/login/token", {}).respond(200, response);
|
||||
await httpBackend!.flush("");
|
||||
expect(await prom).toStrictEqual(response);
|
||||
// check that expires_in has been populated for compatibility with r1
|
||||
expect(await prom).toStrictEqual({ ...response, expires_in_ms: 5000 });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -662,41 +662,30 @@ describe("SlidingSyncSdk", () => {
|
||||
});
|
||||
|
||||
it("can update device lists", () => {
|
||||
client!.crypto!.processDeviceLists = jest.fn();
|
||||
ext.onResponse({
|
||||
device_lists: {
|
||||
changed: ["@alice:localhost"],
|
||||
left: ["@bob:localhost"],
|
||||
},
|
||||
});
|
||||
// TODO: more assertions?
|
||||
expect(client!.crypto!.processDeviceLists).toHaveBeenCalledWith({
|
||||
changed: ["@alice:localhost"],
|
||||
left: ["@bob:localhost"],
|
||||
});
|
||||
});
|
||||
|
||||
it("can update OTK counts", () => {
|
||||
client!.crypto!.updateOneTimeKeyCount = jest.fn();
|
||||
it("can update OTK counts and unused fallback keys", () => {
|
||||
client!.crypto!.processKeyCounts = jest.fn();
|
||||
ext.onResponse({
|
||||
device_one_time_keys_count: {
|
||||
signed_curve25519: 42,
|
||||
},
|
||||
});
|
||||
expect(client!.crypto!.updateOneTimeKeyCount).toHaveBeenCalledWith(42);
|
||||
ext.onResponse({
|
||||
device_one_time_keys_count: {
|
||||
not_signed_curve25519: 42,
|
||||
// missing field -> default to 0
|
||||
},
|
||||
});
|
||||
expect(client!.crypto!.updateOneTimeKeyCount).toHaveBeenCalledWith(0);
|
||||
});
|
||||
|
||||
it("can update fallback keys", () => {
|
||||
ext.onResponse({
|
||||
device_unused_fallback_key_types: ["signed_curve25519"],
|
||||
});
|
||||
expect(client!.crypto!.getNeedsNewFallback()).toEqual(false);
|
||||
ext.onResponse({
|
||||
device_unused_fallback_key_types: ["not_signed_curve25519"],
|
||||
});
|
||||
expect(client!.crypto!.getNeedsNewFallback()).toEqual(true);
|
||||
expect(client!.crypto!.processKeyCounts).toHaveBeenCalledWith({ signed_curve25519: 42 }, [
|
||||
"signed_curve25519",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -16,7 +16,7 @@ limitations under the License.
|
||||
|
||||
import DOMException from "domexception";
|
||||
|
||||
global.DOMException = DOMException;
|
||||
global.DOMException = DOMException as typeof global.DOMException;
|
||||
|
||||
jest.mock("../src/http-api/utils", () => ({
|
||||
...jest.requireActual("../src/http-api/utils"),
|
||||
|
||||
@@ -375,17 +375,17 @@ export async function awaitDecryption(
|
||||
// already
|
||||
if (event.getClearContent() !== null) {
|
||||
if (waitOnDecryptionFailure && event.isDecryptionFailure()) {
|
||||
logger.log(`${Date.now()} event ${event.getId()} got decryption error; waiting`);
|
||||
logger.log(`${Date.now()}: event ${event.getId()} got decryption error; waiting`);
|
||||
} else {
|
||||
return event;
|
||||
}
|
||||
} else {
|
||||
logger.log(`${Date.now()} event ${event.getId()} is not yet decrypted; waiting`);
|
||||
logger.log(`${Date.now()}: event ${event.getId()} is not yet decrypted; waiting`);
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
event.once(MatrixEventEvent.Decrypted, (ev) => {
|
||||
logger.log(`${Date.now()} event ${event.getId()} now decrypted`);
|
||||
event.once(MatrixEventEvent.Decrypted, (ev, err) => {
|
||||
logger.log(`${Date.now()}: MatrixEventEvent.Decrypted for event ${event.getId()}: ${err ?? "success"}`);
|
||||
resolve(ev);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1011,7 +1011,6 @@ describe("Crypto", function () {
|
||||
jest.setTimeout(10000);
|
||||
const client = new TestClient("@a:example.com", "dev").client;
|
||||
await client.initCrypto();
|
||||
client.crypto!.getSecretStorageKey = jest.fn().mockResolvedValue(null);
|
||||
client.crypto!.isCrossSigningReady = async () => false;
|
||||
client.crypto!.baseApis.uploadDeviceSigningKeys = jest.fn().mockResolvedValue(null);
|
||||
client.crypto!.baseApis.setAccountData = jest.fn().mockResolvedValue(null);
|
||||
|
||||
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
import "../../olm-loader";
|
||||
import * as olmlib from "../../../src/crypto/olmlib";
|
||||
import { IObject } from "../../../src/crypto/olmlib";
|
||||
import { SECRET_STORAGE_ALGORITHM_V1_AES } from "../../../src/crypto/SecretStorage";
|
||||
import { MatrixEvent } from "../../../src/models/event";
|
||||
import { TestClient } from "../../TestClient";
|
||||
import { makeTestClients } from "./verification/util";
|
||||
@@ -28,7 +27,7 @@ import { ClientEvent, ICreateClientOpts, ICrossSigningKey, MatrixClient } from "
|
||||
import { DeviceInfo } from "../../../src/crypto/deviceinfo";
|
||||
import { ISignatures } from "../../../src/@types/signed";
|
||||
import { ICurve25519AuthData } from "../../../src/crypto/keybackup";
|
||||
import { SecretStorageKeyDescription } from "../../../src/secret-storage";
|
||||
import { SecretStorageKeyDescription, SECRET_STORAGE_ALGORITHM_V1_AES } from "../../../src/secret-storage";
|
||||
|
||||
async function makeTestClient(
|
||||
userInfo: { userId: string; deviceId: string },
|
||||
|
||||
@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import DOMException from "domexception";
|
||||
import { mocked } from "jest-mock";
|
||||
|
||||
import { ClientPrefix, MatrixHttpApi, Method, UploadResponse } from "../../../src";
|
||||
@@ -33,8 +32,6 @@ describe("MatrixHttpApi", () => {
|
||||
|
||||
const DONE = 0;
|
||||
|
||||
global.DOMException = DOMException;
|
||||
|
||||
beforeEach(() => {
|
||||
xhr = {
|
||||
upload: {} as XMLHttpRequestUpload,
|
||||
|
||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { Mocked, mocked } from "jest-mock";
|
||||
|
||||
import { logger } from "../../src/logger";
|
||||
import { ClientEvent, IMatrixClientCreateOpts, ITurnServerResponse, MatrixClient, Store } from "../../src/client";
|
||||
@@ -50,6 +50,12 @@ import {
|
||||
MatrixScheduler,
|
||||
Method,
|
||||
Room,
|
||||
EventTimelineSet,
|
||||
PushRuleActionName,
|
||||
TweakName,
|
||||
RuleId,
|
||||
IPushRule,
|
||||
ConditionKind,
|
||||
} from "../../src";
|
||||
import { supportsMatrixCall } from "../../src/webrtc/call";
|
||||
import { makeBeaconEvent } from "../test-utils/beacon";
|
||||
@@ -63,6 +69,7 @@ import { QueryDict } from "../../src/utils";
|
||||
import { SyncState } from "../../src/sync";
|
||||
import * as featureUtils from "../../src/feature";
|
||||
import { StubStore } from "../../src/store/stub";
|
||||
import { SecretStorageKeyDescriptionAesV1, ServerSideSecretStorageImpl } from "../../src/secret-storage";
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
@@ -2704,4 +2711,202 @@ describe("MatrixClient", function () {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// these wrappers are deprecated, but we need coverage of them to pass the quality gate
|
||||
describe("SecretStorage wrappers", () => {
|
||||
let mockSecretStorage: Mocked<ServerSideSecretStorageImpl>;
|
||||
|
||||
beforeEach(() => {
|
||||
mockSecretStorage = {
|
||||
getDefaultKeyId: jest.fn(),
|
||||
hasKey: jest.fn(),
|
||||
isStored: jest.fn(),
|
||||
} as unknown as Mocked<ServerSideSecretStorageImpl>;
|
||||
client["_secretStorage"] = mockSecretStorage;
|
||||
});
|
||||
|
||||
it("hasSecretStorageKey", async () => {
|
||||
mockSecretStorage.hasKey.mockResolvedValue(false);
|
||||
expect(await client.hasSecretStorageKey("mykey")).toBe(false);
|
||||
expect(mockSecretStorage.hasKey).toHaveBeenCalledWith("mykey");
|
||||
});
|
||||
|
||||
it("isSecretStored", async () => {
|
||||
const mockResult = { key: {} as SecretStorageKeyDescriptionAesV1 };
|
||||
mockSecretStorage.isStored.mockResolvedValue(mockResult);
|
||||
expect(await client.isSecretStored("mysecret")).toBe(mockResult);
|
||||
expect(mockSecretStorage.isStored).toHaveBeenCalledWith("mysecret");
|
||||
});
|
||||
|
||||
it("getDefaultSecretStorageKeyId", async () => {
|
||||
mockSecretStorage.getDefaultKeyId.mockResolvedValue("bzz");
|
||||
expect(await client.getDefaultSecretStorageKeyId()).toEqual("bzz");
|
||||
});
|
||||
|
||||
it("isKeyBackupKeyStored", async () => {
|
||||
mockSecretStorage.isStored.mockResolvedValue(null);
|
||||
expect(await client.isKeyBackupKeyStored()).toBe(null);
|
||||
expect(mockSecretStorage.isStored).toHaveBeenCalledWith("m.megolm_backup.v1");
|
||||
});
|
||||
});
|
||||
|
||||
describe("paginateEventTimeline()", () => {
|
||||
describe("notifications timeline", () => {
|
||||
const unsafeNotification = {
|
||||
actions: ["notify"],
|
||||
room_id: "__proto__",
|
||||
event: testUtils.mkMessage({
|
||||
user: "@villain:server.org",
|
||||
room: "!roomId:server.org",
|
||||
msg: "I am nefarious",
|
||||
}),
|
||||
profile_tag: null,
|
||||
read: true,
|
||||
ts: 12345,
|
||||
};
|
||||
|
||||
const goodNotification = {
|
||||
actions: ["notify"],
|
||||
room_id: "!favouriteRoom:server.org",
|
||||
event: new MatrixEvent({
|
||||
sender: "@bob:server.org",
|
||||
room_id: "!roomId:server.org",
|
||||
type: "m.call.invite",
|
||||
content: {},
|
||||
}),
|
||||
profile_tag: null,
|
||||
read: true,
|
||||
ts: 12345,
|
||||
};
|
||||
|
||||
const highlightNotification = {
|
||||
actions: ["notify", { set_tweak: "highlight", value: true }],
|
||||
room_id: "!roomId:server.org",
|
||||
event: testUtils.mkMessage({
|
||||
user: "@bob:server.org",
|
||||
room: "!roomId:server.org",
|
||||
msg: "I am highlighted banana",
|
||||
}),
|
||||
profile_tag: null,
|
||||
read: true,
|
||||
ts: 12345,
|
||||
};
|
||||
|
||||
const setNotifsResponse = (notifications: any[] = []): void => {
|
||||
const response: HttpLookup = {
|
||||
method: "GET",
|
||||
path: "/notifications",
|
||||
data: { notifications: JSON.parse(JSON.stringify(notifications)) },
|
||||
};
|
||||
httpLookups = [response];
|
||||
};
|
||||
|
||||
const callRule: IPushRule = {
|
||||
actions: [PushRuleActionName.Notify],
|
||||
conditions: [
|
||||
{
|
||||
kind: ConditionKind.EventMatch,
|
||||
key: "type",
|
||||
pattern: "m.call.invite",
|
||||
},
|
||||
],
|
||||
default: true,
|
||||
enabled: true,
|
||||
rule_id: ".m.rule.call",
|
||||
};
|
||||
const masterRule: IPushRule = {
|
||||
actions: [PushRuleActionName.DontNotify],
|
||||
conditions: [],
|
||||
default: true,
|
||||
enabled: false,
|
||||
rule_id: RuleId.Master,
|
||||
};
|
||||
const bananaRule = {
|
||||
actions: [PushRuleActionName.Notify, { set_tweak: TweakName.Highlight, value: true }],
|
||||
pattern: "banana",
|
||||
rule_id: "banana",
|
||||
default: false,
|
||||
enabled: true,
|
||||
} as IPushRule;
|
||||
const pushRules = {
|
||||
global: {
|
||||
underride: [callRule],
|
||||
override: [masterRule],
|
||||
content: [bananaRule],
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
makeClient();
|
||||
|
||||
// this is how notif timeline is set up in react-sdk
|
||||
const notifTimelineSet = new EventTimelineSet(undefined, {
|
||||
timelineSupport: true,
|
||||
pendingEvents: false,
|
||||
});
|
||||
notifTimelineSet.getLiveTimeline().setPaginationToken("", EventTimeline.BACKWARDS);
|
||||
client.setNotifTimelineSet(notifTimelineSet);
|
||||
|
||||
setNotifsResponse();
|
||||
|
||||
client.setPushRules(pushRules);
|
||||
});
|
||||
|
||||
it("should throw when trying to paginate forwards", async () => {
|
||||
const timeline = client.getNotifTimelineSet()!.getLiveTimeline();
|
||||
await expect(
|
||||
async () => await client.paginateEventTimeline(timeline, { backwards: false }),
|
||||
).rejects.toThrow("paginateNotifTimeline can only paginate backwards");
|
||||
});
|
||||
|
||||
it("defaults limit to 30 events", async () => {
|
||||
jest.spyOn(client.http, "authedRequest");
|
||||
const timeline = client.getNotifTimelineSet()!.getLiveTimeline();
|
||||
await client.paginateEventTimeline(timeline, { backwards: true });
|
||||
|
||||
expect(client.http.authedRequest).toHaveBeenCalledWith(Method.Get, "/notifications", {
|
||||
limit: "30",
|
||||
only: "highlight",
|
||||
});
|
||||
});
|
||||
|
||||
it("filters out unsafe notifications", async () => {
|
||||
setNotifsResponse([unsafeNotification, goodNotification, highlightNotification]);
|
||||
|
||||
const timelineSet = client.getNotifTimelineSet()!;
|
||||
const timeline = timelineSet.getLiveTimeline();
|
||||
await client.paginateEventTimeline(timeline, { backwards: true });
|
||||
|
||||
// badNotification not added to timeline
|
||||
const timelineEvents = timeline.getEvents();
|
||||
expect(timelineEvents.length).toEqual(2);
|
||||
});
|
||||
|
||||
it("sets push details on events and add to timeline", async () => {
|
||||
setNotifsResponse([goodNotification, highlightNotification]);
|
||||
|
||||
const timelineSet = client.getNotifTimelineSet()!;
|
||||
const timeline = timelineSet.getLiveTimeline();
|
||||
await client.paginateEventTimeline(timeline, { backwards: true });
|
||||
|
||||
const [highlightEvent, goodEvent] = timeline.getEvents();
|
||||
expect(highlightEvent.getPushActions()).toEqual({
|
||||
notify: true,
|
||||
tweaks: {
|
||||
highlight: true,
|
||||
},
|
||||
});
|
||||
expect(highlightEvent.getPushDetails().rule).toEqual({
|
||||
...bananaRule,
|
||||
kind: "content",
|
||||
});
|
||||
expect(goodEvent.getPushActions()).toEqual({
|
||||
notify: true,
|
||||
tweaks: {
|
||||
highlight: false,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,6 +17,7 @@ limitations under the License.
|
||||
import { MatrixEvent, MatrixEventEvent } from "../../../src/models/event";
|
||||
import { emitPromise } from "../../test-utils/test-utils";
|
||||
import { Crypto, IEventDecryptionResult } from "../../../src/crypto";
|
||||
import { IAnnotatedPushRule, PushRuleActionName, TweakName } from "../../../src";
|
||||
|
||||
describe("MatrixEvent", () => {
|
||||
it("should create copies of itself", () => {
|
||||
@@ -216,4 +217,95 @@ describe("MatrixEvent", () => {
|
||||
expect(encryptedEvent.replyEventId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("push details", () => {
|
||||
const pushRule = {
|
||||
actions: [PushRuleActionName.Notify, { set_tweak: TweakName.Highlight, value: true }],
|
||||
pattern: "banana",
|
||||
rule_id: "banana",
|
||||
kind: "override",
|
||||
default: false,
|
||||
enabled: true,
|
||||
} as IAnnotatedPushRule;
|
||||
describe("setPushActions()", () => {
|
||||
it("sets actions on event", () => {
|
||||
const actions = { notify: false, tweaks: {} };
|
||||
const event = new MatrixEvent({
|
||||
type: "com.example.test",
|
||||
content: {
|
||||
isTest: true,
|
||||
},
|
||||
});
|
||||
event.setPushActions(actions);
|
||||
|
||||
expect(event.getPushActions()).toBe(actions);
|
||||
});
|
||||
|
||||
it("sets actions to undefined", () => {
|
||||
const event = new MatrixEvent({
|
||||
type: "com.example.test",
|
||||
content: {
|
||||
isTest: true,
|
||||
},
|
||||
});
|
||||
event.setPushActions(null);
|
||||
|
||||
// undefined is set on state
|
||||
expect(event.getPushDetails().actions).toBe(undefined);
|
||||
// but pushActions getter returns null when falsy
|
||||
expect(event.getPushActions()).toBe(null);
|
||||
});
|
||||
|
||||
it("clears existing push rule", () => {
|
||||
const prevActions = { notify: true, tweaks: { highlight: true } };
|
||||
const actions = { notify: false, tweaks: {} };
|
||||
const event = new MatrixEvent({
|
||||
type: "com.example.test",
|
||||
content: {
|
||||
isTest: true,
|
||||
},
|
||||
});
|
||||
event.setPushDetails(prevActions, pushRule);
|
||||
|
||||
event.setPushActions(actions);
|
||||
|
||||
// rule is not in event push cache
|
||||
expect(event.getPushDetails()).toEqual({ actions });
|
||||
});
|
||||
});
|
||||
|
||||
describe("setPushDetails()", () => {
|
||||
it("sets actions and rule on event", () => {
|
||||
const actions = { notify: false, tweaks: {} };
|
||||
const event = new MatrixEvent({
|
||||
type: "com.example.test",
|
||||
content: {
|
||||
isTest: true,
|
||||
},
|
||||
});
|
||||
event.setPushDetails(actions, pushRule);
|
||||
|
||||
expect(event.getPushDetails()).toEqual({
|
||||
actions,
|
||||
rule: pushRule,
|
||||
});
|
||||
});
|
||||
it("clears existing push rule", () => {
|
||||
const prevActions = { notify: true, tweaks: { highlight: true } };
|
||||
const actions = { notify: false, tweaks: {} };
|
||||
const event = new MatrixEvent({
|
||||
type: "com.example.test",
|
||||
content: {
|
||||
isTest: true,
|
||||
},
|
||||
});
|
||||
event.setPushDetails(prevActions, pushRule);
|
||||
|
||||
event.setPushActions(actions);
|
||||
|
||||
// rule is not in event push cache
|
||||
expect(event.getPushDetails()).toEqual({ actions });
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,13 +14,16 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { IEvent, MatrixEvent, PollEvent, Room } from "../../../src";
|
||||
import { M_POLL_START } from "matrix-events-sdk";
|
||||
|
||||
import { EventType, IEvent, MatrixEvent, PollEvent, Room } from "../../../src";
|
||||
import { REFERENCE_RELATION } from "../../../src/@types/extensible_events";
|
||||
import { M_POLL_END, M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE } from "../../../src/@types/polls";
|
||||
import { PollStartEvent } from "../../../src/extensible_events_v1/PollStartEvent";
|
||||
import { Poll } from "../../../src/models/poll";
|
||||
import { isPollEvent, Poll } from "../../../src/models/poll";
|
||||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../test-utils/client";
|
||||
import { flushPromises } from "../../test-utils/flushPromises";
|
||||
import { mkEvent } from "../../test-utils/test-utils";
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
@@ -453,4 +456,31 @@ describe("Poll", () => {
|
||||
expect(responses.getRelations()).toEqual([responseEvent]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("isPollEvent", () => {
|
||||
it("should return »false« for a non-poll event", () => {
|
||||
const messageEvent = mkEvent({
|
||||
event: true,
|
||||
type: EventType.RoomMessage,
|
||||
content: {},
|
||||
user: mockClient.getSafeUserId(),
|
||||
room: room.roomId,
|
||||
});
|
||||
expect(isPollEvent(messageEvent)).toBe(false);
|
||||
});
|
||||
|
||||
it.each([[M_POLL_START.name], [M_POLL_RESPONSE.name], [M_POLL_END.name]])(
|
||||
"should return »true« for a »%s« event",
|
||||
(type: string) => {
|
||||
const pollEvent = mkEvent({
|
||||
event: true,
|
||||
type,
|
||||
content: {},
|
||||
user: mockClient.getSafeUserId(),
|
||||
room: room.roomId,
|
||||
});
|
||||
expect(isPollEvent(pollEvent)).toBe(true);
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import * as utils from "../test-utils/test-utils";
|
||||
import { IActionsObject, PushProcessor } from "../../src/pushprocessor";
|
||||
import { ConditionKind, EventType, IContent, MatrixClient, MatrixEvent, PushRuleActionName, RuleId } from "../../src";
|
||||
import { mockClientMethodsUser } from "../test-utils/client";
|
||||
|
||||
describe("NotificationService", function () {
|
||||
const testUserId = "@ali:matrix.org";
|
||||
@@ -45,9 +46,7 @@ describe("NotificationService", function () {
|
||||
},
|
||||
};
|
||||
},
|
||||
credentials: {
|
||||
userId: testUserId,
|
||||
},
|
||||
...mockClientMethodsUser(testUserId),
|
||||
supportsIntentionalMentions: () => true,
|
||||
pushRules: {
|
||||
device: {},
|
||||
|
||||
@@ -38,6 +38,7 @@ function makeMockClient(opts: {
|
||||
deviceId: string;
|
||||
deviceKey?: string;
|
||||
msc3882Enabled: boolean;
|
||||
msc3882r0Only: boolean;
|
||||
msc3886Enabled: boolean;
|
||||
devices?: Record<string, Partial<DeviceInfo>>;
|
||||
verificationFunction?: (
|
||||
@@ -58,6 +59,17 @@ function makeMockClient(opts: {
|
||||
},
|
||||
};
|
||||
},
|
||||
getCapabilities() {
|
||||
return opts.msc3882r0Only
|
||||
? {}
|
||||
: {
|
||||
capabilities: {
|
||||
"org.matrix.msc3882.get_login_token": {
|
||||
enabled: opts.msc3882Enabled,
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
getUserId() {
|
||||
return opts.userId;
|
||||
},
|
||||
@@ -111,6 +123,7 @@ describe("Rendezvous", function () {
|
||||
deviceId: "DEVICEID",
|
||||
msc3886Enabled: false,
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: true,
|
||||
});
|
||||
httpBackend.when("POST", "https://fallbackserver/rz").response = {
|
||||
body: null,
|
||||
@@ -166,7 +179,13 @@ describe("Rendezvous", function () {
|
||||
await aliceRz.close();
|
||||
});
|
||||
|
||||
it("no protocols", async function () {
|
||||
async function testNoProtocols({
|
||||
msc3882Enabled,
|
||||
msc3882r0Only,
|
||||
}: {
|
||||
msc3882Enabled: boolean;
|
||||
msc3882r0Only: boolean;
|
||||
}) {
|
||||
const aliceTransport = makeTransport("Alice");
|
||||
const bobTransport = makeTransport("Bob", "https://test.rz/999999");
|
||||
transports.push(aliceTransport, bobTransport);
|
||||
@@ -178,8 +197,9 @@ describe("Rendezvous", function () {
|
||||
const alice = makeMockClient({
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: false,
|
||||
msc3886Enabled: false,
|
||||
msc3882Enabled,
|
||||
msc3882r0Only,
|
||||
});
|
||||
const aliceEcdh = new MSC3903ECDHRendezvousChannel(aliceTransport, undefined, aliceOnFailure);
|
||||
const aliceRz = new MSC3906Rendezvous(aliceEcdh, alice);
|
||||
@@ -218,6 +238,14 @@ describe("Rendezvous", function () {
|
||||
|
||||
await aliceStartProm;
|
||||
await bobStartPromise;
|
||||
}
|
||||
|
||||
it("no protocols - r0", async function () {
|
||||
await testNoProtocols({ msc3882Enabled: false, msc3882r0Only: true });
|
||||
});
|
||||
|
||||
it("no protocols - r1", async function () {
|
||||
await testNoProtocols({ msc3882Enabled: false, msc3882r0Only: false });
|
||||
});
|
||||
|
||||
it("new device declines protocol with outcome unsupported", async function () {
|
||||
@@ -233,6 +261,7 @@ describe("Rendezvous", function () {
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: false,
|
||||
msc3886Enabled: false,
|
||||
});
|
||||
const aliceEcdh = new MSC3903ECDHRendezvousChannel(aliceTransport, undefined, aliceOnFailure);
|
||||
@@ -291,6 +320,7 @@ describe("Rendezvous", function () {
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: false,
|
||||
msc3886Enabled: false,
|
||||
});
|
||||
const aliceEcdh = new MSC3903ECDHRendezvousChannel(aliceTransport, undefined, aliceOnFailure);
|
||||
@@ -349,6 +379,7 @@ describe("Rendezvous", function () {
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: false,
|
||||
msc3886Enabled: false,
|
||||
});
|
||||
const aliceEcdh = new MSC3903ECDHRendezvousChannel(aliceTransport, undefined, aliceOnFailure);
|
||||
@@ -409,6 +440,7 @@ describe("Rendezvous", function () {
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: false,
|
||||
msc3886Enabled: false,
|
||||
});
|
||||
const aliceEcdh = new MSC3903ECDHRendezvousChannel(aliceTransport, undefined, aliceOnFailure);
|
||||
@@ -477,6 +509,7 @@ describe("Rendezvous", function () {
|
||||
userId: "alice",
|
||||
deviceId: "ALICE",
|
||||
msc3882Enabled: true,
|
||||
msc3882r0Only: false,
|
||||
msc3886Enabled: false,
|
||||
devices,
|
||||
deviceKey: "aaaa",
|
||||
|
||||
+46
-3
@@ -19,7 +19,7 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
import { mocked } from "jest-mock";
|
||||
import { M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE, PollStartEvent } from "matrix-events-sdk";
|
||||
import { M_POLL_KIND_DISCLOSED, M_POLL_RESPONSE, M_POLL_START, PollStartEvent } from "matrix-events-sdk";
|
||||
|
||||
import * as utils from "../test-utils/test-utils";
|
||||
import { emitPromise } from "../test-utils/test-utils";
|
||||
@@ -53,6 +53,7 @@ import { FeatureSupport, Thread, THREAD_RELATION_TYPE, ThreadEvent } from "../..
|
||||
import { Crypto } from "../../src/crypto";
|
||||
import { mkThread } from "../test-utils/thread";
|
||||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../test-utils/client";
|
||||
import { logger } from "../../src/logger";
|
||||
|
||||
describe("Room", function () {
|
||||
const roomId = "!foo:bar";
|
||||
@@ -171,6 +172,8 @@ describe("Room", function () {
|
||||
room.oldState = room.getLiveTimeline().startState = utils.mock(RoomState, "oldState");
|
||||
// @ts-ignore
|
||||
room.currentState = room.getLiveTimeline().endState = utils.mock(RoomState, "currentState");
|
||||
|
||||
jest.spyOn(logger, "warn");
|
||||
});
|
||||
|
||||
describe("getCreator", () => {
|
||||
@@ -3261,7 +3264,7 @@ describe("Room", function () {
|
||||
expect(room.emit).toHaveBeenCalledWith(PollEvent.New, pollInstance);
|
||||
});
|
||||
|
||||
it("adds related events to poll models", async () => {
|
||||
it("adds related events to poll models and log errors", async () => {
|
||||
const pollStartEvent = makePollStart("1");
|
||||
const pollStartEvent2 = makePollStart("2");
|
||||
const events = [pollStartEvent, pollStartEvent2];
|
||||
@@ -3274,6 +3277,7 @@ describe("Room", function () {
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const messageEvent = new MatrixEvent({
|
||||
type: "m.room.messsage",
|
||||
content: {
|
||||
@@ -3281,6 +3285,19 @@ describe("Room", function () {
|
||||
},
|
||||
});
|
||||
|
||||
const errorEvent = new MatrixEvent({
|
||||
type: M_POLL_START.name,
|
||||
content: {
|
||||
text: "Error!!!!",
|
||||
},
|
||||
});
|
||||
|
||||
const error = new Error("Test error");
|
||||
|
||||
mocked(client.decryptEventIfNeeded).mockImplementation(async (event: MatrixEvent) => {
|
||||
if (event === errorEvent) throw error;
|
||||
});
|
||||
|
||||
// init poll
|
||||
await room.processPollEvents(events);
|
||||
|
||||
@@ -3289,7 +3306,7 @@ describe("Room", function () {
|
||||
jest.spyOn(poll, "onNewRelation");
|
||||
jest.spyOn(poll2, "onNewRelation");
|
||||
|
||||
await room.processPollEvents([pollResponseEvent, messageEvent]);
|
||||
await room.processPollEvents([errorEvent, messageEvent, pollResponseEvent]);
|
||||
|
||||
// only called for relevant event
|
||||
expect(poll.onNewRelation).toHaveBeenCalledTimes(1);
|
||||
@@ -3297,6 +3314,32 @@ describe("Room", function () {
|
||||
|
||||
// only called on poll with relation
|
||||
expect(poll2.onNewRelation).not.toHaveBeenCalled();
|
||||
|
||||
expect(logger.warn).toHaveBeenCalledWith("Error processing poll event", errorEvent.getId(), error);
|
||||
});
|
||||
|
||||
it("should retry on decryption", async () => {
|
||||
const pollStartEventId = "poll1";
|
||||
const pollStartEvent = makePollStart(pollStartEventId);
|
||||
// simulate decryption failure
|
||||
const isDecryptionFailureSpy = jest.spyOn(pollStartEvent, "isDecryptionFailure").mockReturnValue(true);
|
||||
|
||||
await room.processPollEvents([pollStartEvent]);
|
||||
// do not expect a poll to show up for the room
|
||||
expect(room.polls.get(pollStartEventId)).toBeUndefined();
|
||||
|
||||
// now emit a Decrypted event but keep the decryption failure
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent);
|
||||
// still do not expect a poll to show up for the room
|
||||
expect(room.polls.get(pollStartEventId)).toBeUndefined();
|
||||
|
||||
// clear decryption failure and emit a Decrypted event again
|
||||
isDecryptionFailureSpy.mockRestore();
|
||||
pollStartEvent.emit(MatrixEventEvent.Decrypted, pollStartEvent);
|
||||
|
||||
// the poll should now show up in the room's polls
|
||||
const poll = room.polls.get(pollStartEventId);
|
||||
expect(poll?.pollId).toBe(pollStartEventId);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -92,16 +92,6 @@ describe("RustCrypto", () => {
|
||||
const res = await rustCrypto.preprocessToDeviceMessages(inputs);
|
||||
expect(res).toEqual(inputs);
|
||||
});
|
||||
|
||||
it("should pass through one time key counts", async () => {
|
||||
const oneTimeKeyCounts = new Map<string, number>([["signed_curve25519", 50]]);
|
||||
await expect(rustCrypto.preprocessOneTimeKeyCounts(oneTimeKeyCounts)).resolves.not.toBeDefined();
|
||||
});
|
||||
|
||||
it("should pass through unused fallback keys", async () => {
|
||||
const unusedFallbackKeys = new Set(["signed_curve25519"]);
|
||||
await expect(rustCrypto.preprocessUnusedFallbackKeys(unusedFallbackKeys)).resolves.not.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("outgoing requests", () => {
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
/*
|
||||
Copyright 2019, 2022-2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { Mocked } from "jest-mock";
|
||||
|
||||
import {
|
||||
AccountDataClient,
|
||||
PassphraseInfo,
|
||||
SecretStorageCallbacks,
|
||||
SecretStorageKeyDescriptionAesV1,
|
||||
SecretStorageKeyDescriptionCommon,
|
||||
ServerSideSecretStorageImpl,
|
||||
trimTrailingEquals,
|
||||
} from "../../src/secret-storage";
|
||||
import { calculateKeyCheck } from "../../src/crypto/aes";
|
||||
import { randomString } from "../../src/randomstring";
|
||||
|
||||
describe("ServerSideSecretStorageImpl", function () {
|
||||
describe(".addKey", function () {
|
||||
it("should allow storing a default key", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
const result = await secretStorage.addKey("m.secret_storage.v1.aes-hmac-sha2");
|
||||
|
||||
// it should have made up a 32-character key id
|
||||
expect(result.keyId.length).toEqual(32);
|
||||
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith(
|
||||
`m.secret_storage.key.${result.keyId}`,
|
||||
result.keyInfo,
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow storing a key with an explicit id", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
const result = await secretStorage.addKey("m.secret_storage.v1.aes-hmac-sha2", {}, "myKeyId");
|
||||
|
||||
// it should have made up a 32-character key id
|
||||
expect(result.keyId).toEqual("myKeyId");
|
||||
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith(
|
||||
"m.secret_storage.key.myKeyId",
|
||||
result.keyInfo,
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow storing a key with a name", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
const result = await secretStorage.addKey("m.secret_storage.v1.aes-hmac-sha2", { name: "mykey" });
|
||||
|
||||
expect(result.keyInfo.name).toEqual("mykey");
|
||||
|
||||
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith(
|
||||
`m.secret_storage.key.${result.keyId}`,
|
||||
result.keyInfo,
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow storing a key with a passphrase", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
const passphrase: PassphraseInfo = {
|
||||
algorithm: "m.pbkdf2",
|
||||
iterations: 125,
|
||||
salt: "saltygoodness",
|
||||
bits: 256,
|
||||
};
|
||||
const result = await secretStorage.addKey("m.secret_storage.v1.aes-hmac-sha2", {
|
||||
passphrase,
|
||||
});
|
||||
|
||||
expect(result.keyInfo.passphrase).toEqual(passphrase);
|
||||
|
||||
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith(
|
||||
`m.secret_storage.key.${result.keyId}`,
|
||||
result.keyInfo,
|
||||
);
|
||||
});
|
||||
|
||||
it("should complain about invalid algorithm", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
await expect(() => secretStorage.addKey("bad_alg")).rejects.toThrow("Unknown key algorithm");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getKey", function () {
|
||||
it("should return the specified key", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
|
||||
const storedKey = { iv: "iv", mac: "mac" } as SecretStorageKeyDescriptionAesV1;
|
||||
async function mockGetAccountData<T extends Record<string, any>>(eventType: string): Promise<T | null> {
|
||||
if (eventType === "m.secret_storage.key.my_key") {
|
||||
return storedKey as unknown as T;
|
||||
} else {
|
||||
throw new Error(`unexpected eventType ${eventType}`);
|
||||
}
|
||||
}
|
||||
accountDataAdapter.getAccountDataFromServer.mockImplementation(mockGetAccountData);
|
||||
|
||||
const result = await secretStorage.getKey("my_key");
|
||||
expect(result).toEqual(["my_key", storedKey]);
|
||||
});
|
||||
|
||||
it("should return the default key if none is specified", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
|
||||
const storedKey = { iv: "iv", mac: "mac" } as SecretStorageKeyDescriptionAesV1;
|
||||
async function mockGetAccountData<T extends Record<string, any>>(eventType: string): Promise<T | null> {
|
||||
if (eventType === "m.secret_storage.default_key") {
|
||||
return { key: "default_key_id" } as unknown as T;
|
||||
} else if (eventType === "m.secret_storage.key.default_key_id") {
|
||||
return storedKey as unknown as T;
|
||||
} else {
|
||||
throw new Error(`unexpected eventType ${eventType}`);
|
||||
}
|
||||
}
|
||||
accountDataAdapter.getAccountDataFromServer.mockImplementation(mockGetAccountData);
|
||||
|
||||
const result = await secretStorage.getKey();
|
||||
expect(result).toEqual(["default_key_id", storedKey]);
|
||||
});
|
||||
|
||||
it("should return null if the key is not found", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, {});
|
||||
// @ts-ignore
|
||||
accountDataAdapter.getAccountDataFromServer.mockResolvedValue(null);
|
||||
|
||||
const result = await secretStorage.getKey("my_key");
|
||||
expect(result).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe("checkKey", function () {
|
||||
it("should return true for a correct key check", async function () {
|
||||
const secretStorage = new ServerSideSecretStorageImpl({} as AccountDataClient, {});
|
||||
|
||||
const myKey = new TextEncoder().encode(randomString(32));
|
||||
const { iv, mac } = await calculateKeyCheck(myKey);
|
||||
|
||||
const keyInfo: SecretStorageKeyDescriptionAesV1 = {
|
||||
name: "my key",
|
||||
passphrase: {} as PassphraseInfo,
|
||||
algorithm: "m.secret_storage.v1.aes-hmac-sha2",
|
||||
iv,
|
||||
mac,
|
||||
};
|
||||
|
||||
const result = await secretStorage.checkKey(myKey, keyInfo);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for an incorrect key check", async function () {
|
||||
const secretStorage = new ServerSideSecretStorageImpl({} as AccountDataClient, {});
|
||||
|
||||
const { iv, mac } = await calculateKeyCheck(new TextEncoder().encode("badkey"));
|
||||
|
||||
const keyInfo: SecretStorageKeyDescriptionAesV1 = {
|
||||
name: "my key",
|
||||
passphrase: {} as PassphraseInfo,
|
||||
algorithm: "m.secret_storage.v1.aes-hmac-sha2",
|
||||
iv,
|
||||
mac,
|
||||
};
|
||||
|
||||
const result = await secretStorage.checkKey(new TextEncoder().encode("goodkey"), keyInfo);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("should raise for an unknown algorithm", async function () {
|
||||
const secretStorage = new ServerSideSecretStorageImpl({} as AccountDataClient, {});
|
||||
const keyInfo: SecretStorageKeyDescriptionAesV1 = {
|
||||
name: "my key",
|
||||
passphrase: {} as PassphraseInfo,
|
||||
algorithm: "bad_alg",
|
||||
iv: "iv",
|
||||
mac: "mac",
|
||||
};
|
||||
|
||||
await expect(() => secretStorage.checkKey(new TextEncoder().encode("goodkey"), keyInfo)).rejects.toThrow(
|
||||
"Unknown algorithm",
|
||||
);
|
||||
});
|
||||
|
||||
// XXX: really???
|
||||
it("should return true for an absent mac", async function () {
|
||||
const secretStorage = new ServerSideSecretStorageImpl({} as AccountDataClient, {});
|
||||
const keyInfo: SecretStorageKeyDescriptionAesV1 = {
|
||||
name: "my key",
|
||||
passphrase: {} as PassphraseInfo,
|
||||
algorithm: "m.secret_storage.v1.aes-hmac-sha2",
|
||||
iv: "iv",
|
||||
mac: "",
|
||||
};
|
||||
|
||||
const result = await secretStorage.checkKey(new TextEncoder().encode("goodkey"), keyInfo);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("store", () => {
|
||||
it("should ignore keys with unknown algorithm", async function () {
|
||||
const accountDataAdapter = mockAccountDataClient();
|
||||
const mockCallbacks = { getSecretStorageKey: jest.fn() } as Mocked<SecretStorageCallbacks>;
|
||||
const secretStorage = new ServerSideSecretStorageImpl(accountDataAdapter, mockCallbacks);
|
||||
|
||||
// stub out getAccountData to return a key with an unknown algorithm
|
||||
const storedKey = { algorithm: "badalg" } as SecretStorageKeyDescriptionCommon;
|
||||
async function mockGetAccountData<T extends Record<string, any>>(eventType: string): Promise<T | null> {
|
||||
if (eventType === "m.secret_storage.key.keyid") {
|
||||
return storedKey as unknown as T;
|
||||
} else {
|
||||
throw new Error(`unexpected eventType ${eventType}`);
|
||||
}
|
||||
}
|
||||
accountDataAdapter.getAccountDataFromServer.mockImplementation(mockGetAccountData);
|
||||
|
||||
// suppress the expected warning on the console
|
||||
jest.spyOn(console, "warn").mockImplementation();
|
||||
|
||||
// now attempt the store
|
||||
await secretStorage.store("mysecret", "supersecret", ["keyid"]);
|
||||
|
||||
// we should have stored... nothing
|
||||
expect(accountDataAdapter.setAccountData).toHaveBeenCalledWith("mysecret", { encrypted: {} });
|
||||
|
||||
// ... and emitted a warning.
|
||||
// eslint-disable-next-line no-console
|
||||
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining("unknown algorithm"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("trimTrailingEquals", () => {
|
||||
it("should strip trailing =", () => {
|
||||
expect(trimTrailingEquals("ab=c===")).toEqual("ab=c");
|
||||
});
|
||||
|
||||
it("should leave strings without trailing = alone", () => {
|
||||
expect(trimTrailingEquals("ab=c")).toEqual("ab=c");
|
||||
});
|
||||
|
||||
it("should leave the empty string alone", () => {
|
||||
const result = trimTrailingEquals("");
|
||||
expect(result).toEqual("");
|
||||
});
|
||||
});
|
||||
|
||||
function mockAccountDataClient(): Mocked<AccountDataClient> {
|
||||
return {
|
||||
getAccountDataFromServer: jest.fn().mockResolvedValue(null),
|
||||
setAccountData: jest.fn().mockResolvedValue({}),
|
||||
} as unknown as Mocked<AccountDataClient>;
|
||||
}
|
||||
@@ -678,14 +678,14 @@ describe("utils", function () {
|
||||
|
||||
describe("safeSet", () => {
|
||||
it("should set a value", () => {
|
||||
const obj = {};
|
||||
const obj: Record<string, string> = {};
|
||||
safeSet(obj, "testProp", "test value");
|
||||
expect(obj).toEqual({ testProp: "test value" });
|
||||
});
|
||||
|
||||
it.each(["__proto__", "prototype", "constructor"])("should raise an error when setting »%s«", (prop) => {
|
||||
expect(() => {
|
||||
safeSet({}, prop, "teset value");
|
||||
safeSet(<Record<string, string>>{}, prop, "teset value");
|
||||
}).toThrow("Trying to modify prototype or constructor");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import { GroupCallStats } from "../../../../src/webrtc/stats/groupCallStats";
|
||||
import { SummaryStats } from "../../../../src/webrtc/stats/summaryStats";
|
||||
|
||||
const GROUP_CALL_ID = "GROUP_ID";
|
||||
const LOCAL_USER_ID = "LOCAL_USER_ID";
|
||||
@@ -79,9 +80,17 @@ describe("GroupCallStats", () => {
|
||||
it("starting processing and calling the collectors", async () => {
|
||||
stats.addStatsReportGatherer("CALL_ID", "USER_ID", mockRTCPeerConnection());
|
||||
const collector = stats.getStatsReportGatherer("CALL_ID");
|
||||
stats.reports.emitSummaryStatsReport = jest.fn();
|
||||
const summaryStats = {
|
||||
receivedMedia: 0,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 0, muted: 0 },
|
||||
videoTrackSummary: { count: 0, muted: 0 },
|
||||
} as SummaryStats;
|
||||
let processStatsSpy;
|
||||
if (collector) {
|
||||
processStatsSpy = jest.spyOn(collector, "processStats");
|
||||
processStatsSpy = jest.spyOn(collector, "processStats").mockResolvedValue(summaryStats);
|
||||
stats.start();
|
||||
jest.advanceTimersByTime(TIME_INTERVAL);
|
||||
} else {
|
||||
|
||||
@@ -26,14 +26,14 @@ describe("MediaSsrcHandler", () => {
|
||||
handler = new MediaSsrcHandler();
|
||||
});
|
||||
describe("should parse description", () => {
|
||||
it("and build mid ssrc map", () => {
|
||||
it("and build mid ssrc map", async () => {
|
||||
handler.parse(REMOTE_SFU_DESCRIPTION, "remote");
|
||||
expect(handler.getSsrcToMidMap("remote")).toEqual(remoteMap);
|
||||
});
|
||||
});
|
||||
|
||||
describe("should on find mid by ssrc", () => {
|
||||
it("and return mid if mapping exists.", () => {
|
||||
it("and return mid if mapping exists.", async () => {
|
||||
handler.parse(REMOTE_SFU_DESCRIPTION, "remote");
|
||||
expect(handler.findMidBySsrc("2963372119", "remote")).toEqual("0");
|
||||
});
|
||||
|
||||
@@ -25,7 +25,7 @@ describe("TrackHandler", () => {
|
||||
handler = new MediaTrackHandler(pc);
|
||||
});
|
||||
describe("should get local tracks", () => {
|
||||
it("returns video track", () => {
|
||||
it("returns video track", async () => {
|
||||
expect(handler.getLocalTracks("video")).toEqual([
|
||||
{
|
||||
id: `sender-track-2`,
|
||||
@@ -34,7 +34,7 @@ describe("TrackHandler", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("returns audio track", () => {
|
||||
it("returns audio track", async () => {
|
||||
expect(handler.getLocalTracks("audio")).toEqual([
|
||||
{
|
||||
id: `sender-track-1`,
|
||||
@@ -45,58 +45,72 @@ describe("TrackHandler", () => {
|
||||
});
|
||||
|
||||
describe("should get local track by mid", () => {
|
||||
it("returns video track", () => {
|
||||
it("returns video track", async () => {
|
||||
expect(handler.getLocalTrackIdByMid("2")).toEqual("sender-track-2");
|
||||
});
|
||||
|
||||
it("returns audio track", () => {
|
||||
it("returns audio track", async () => {
|
||||
expect(handler.getLocalTrackIdByMid("1")).toEqual("sender-track-1");
|
||||
});
|
||||
|
||||
it("returns undefined if not exists", () => {
|
||||
it("returns undefined if not exists", async () => {
|
||||
expect(handler.getLocalTrackIdByMid("3")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should get remote track by mid", () => {
|
||||
it("returns video track", () => {
|
||||
it("returns video track", async () => {
|
||||
expect(handler.getRemoteTrackIdByMid("2")).toEqual("receiver-track-2");
|
||||
});
|
||||
|
||||
it("returns audio track", () => {
|
||||
it("returns audio track", async () => {
|
||||
expect(handler.getRemoteTrackIdByMid("1")).toEqual("receiver-track-1");
|
||||
});
|
||||
|
||||
it("returns undefined if not exists", () => {
|
||||
it("returns undefined if not exists", async () => {
|
||||
expect(handler.getRemoteTrackIdByMid("3")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should get track by id", () => {
|
||||
it("returns remote track", () => {
|
||||
it("returns remote track", async () => {
|
||||
expect(handler.getTackById("receiver-track-2")).toEqual({
|
||||
id: `receiver-track-2`,
|
||||
kind: "video",
|
||||
} as MediaStreamTrack);
|
||||
});
|
||||
|
||||
it("returns local track", () => {
|
||||
it("returns local track", async () => {
|
||||
expect(handler.getTackById("sender-track-1")).toEqual({
|
||||
id: `sender-track-1`,
|
||||
kind: "audio",
|
||||
} as MediaStreamTrack);
|
||||
});
|
||||
|
||||
it("returns undefined if not exists", () => {
|
||||
it("returns undefined if not exists", async () => {
|
||||
expect(handler.getTackById("sender-track-3")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should get simulcast track count", () => {
|
||||
it("returns 2", () => {
|
||||
it("returns 2", async () => {
|
||||
expect(handler.getActiveSimulcastStreams()).toEqual(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("should get Transceiver by Track ID", () => {
|
||||
it("and returns remote Transceiver if exits", async () => {
|
||||
expect(handler.getTransceiverByTrackId(`receiver-track-1`)?.mid).toEqual("1");
|
||||
});
|
||||
|
||||
it("and returns local Transceiver if exits", async () => {
|
||||
expect(handler.getTransceiverByTrackId(`sender-track-2`)?.mid).toEqual("2");
|
||||
});
|
||||
|
||||
it("returns undefined if Transceiver not exits", async () => {
|
||||
expect(handler.getTransceiverByTrackId("22")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const mockTransceiver = (mid: string, kind: "video" | "audio"): RTCRtpTransceiver => {
|
||||
|
||||
@@ -62,7 +62,7 @@ describe("MediaTrackStatsHandler", () => {
|
||||
});
|
||||
});
|
||||
describe("should find local video track stats", () => {
|
||||
it("and returns stats if `trackIdentifier` exists in report", () => {
|
||||
it("and returns stats if `trackIdentifier` exists in report", async () => {
|
||||
const report = { trackIdentifier: "2222" };
|
||||
expect(statsHandler.findLocalVideoTrackStats(report)?.trackId).toEqual("2222");
|
||||
});
|
||||
@@ -75,9 +75,22 @@ describe("MediaTrackStatsHandler", () => {
|
||||
ssrcHandler.findMidBySsrc = jest.fn().mockReturnValue("2");
|
||||
expect(statsHandler.findTrack2Stats(report, "local")?.trackId).toEqual("2222");
|
||||
});
|
||||
it("and returns undefined if needed property not existing", () => {
|
||||
it("and returns undefined if needed property not existing", async () => {
|
||||
const report = {};
|
||||
expect(statsHandler.findTrack2Stats(report, "remote")?.trackId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should find a Transceiver by Track id", () => {
|
||||
it("and returns undefined if Transceiver not existing", async () => {
|
||||
trackHandler.getTransceiverByTrackId = jest.fn().mockReturnValue(undefined);
|
||||
expect(statsHandler.findTransceiverByTrackId("12")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("and returns Transceiver if existing", async () => {
|
||||
const ts = {} as RTCRtpTransceiver;
|
||||
trackHandler.getTransceiverByTrackId = jest.fn().mockReturnValue(ts);
|
||||
expect(statsHandler.findTransceiverByTrackId("12")).toEqual(ts);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -87,6 +87,10 @@ describe("StatsReportBuilder", () => {
|
||||
["REMOTE_VIDEO_TRACK_ID", { height: 960, width: 1080 }],
|
||||
]),
|
||||
},
|
||||
jitter: new Map([
|
||||
["REMOTE_AUDIO_TRACK_ID", 0.1],
|
||||
["REMOTE_VIDEO_TRACK_ID", 50],
|
||||
]),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -99,6 +103,7 @@ describe("StatsReportBuilder", () => {
|
||||
remoteAudioTrack.setCodec("opus");
|
||||
remoteAudioTrack.setLoss({ packetsTotal: 20, packetsLost: 0, isDownloadStream: true });
|
||||
remoteAudioTrack.setBitrate({ download: 4000, upload: 0 });
|
||||
remoteAudioTrack.setJitter(0.1);
|
||||
|
||||
localVideoTrack.setCodec("v8");
|
||||
localVideoTrack.setLoss({ packetsTotal: 30, packetsLost: 6, isDownloadStream: false });
|
||||
@@ -111,5 +116,6 @@ describe("StatsReportBuilder", () => {
|
||||
remoteVideoTrack.setBitrate({ download: 5000000, upload: 0 });
|
||||
remoteVideoTrack.setFramerate(60);
|
||||
remoteVideoTrack.setResolution({ width: 1080, height: 960 });
|
||||
remoteVideoTrack.setJitter(50);
|
||||
};
|
||||
});
|
||||
|
||||
@@ -14,7 +14,12 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import { StatsReportEmitter } from "../../../../src/webrtc/stats/statsReportEmitter";
|
||||
import { ByteSentStatsReport, ConnectionStatsReport, StatsReport } from "../../../../src/webrtc/stats/statsReport";
|
||||
import {
|
||||
ByteSentStatsReport,
|
||||
ConnectionStatsReport,
|
||||
StatsReport,
|
||||
SummaryStatsReport,
|
||||
} from "../../../../src/webrtc/stats/statsReport";
|
||||
|
||||
describe("StatsReportEmitter", () => {
|
||||
let emitter: StatsReportEmitter;
|
||||
@@ -45,4 +50,16 @@ describe("StatsReportEmitter", () => {
|
||||
emitter.emitConnectionStatsReport(report);
|
||||
});
|
||||
});
|
||||
|
||||
it("should emit and receive SummaryStatsReport", async () => {
|
||||
const report = {} as SummaryStatsReport;
|
||||
return new Promise((resolve, _) => {
|
||||
emitter.on(StatsReport.SUMMARY_STATS, (r) => {
|
||||
expect(r).toBe(report);
|
||||
resolve(null);
|
||||
return;
|
||||
});
|
||||
emitter.emitSummaryStatsReport(report);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -34,9 +34,19 @@ describe("StatsReportGatherer", () => {
|
||||
describe("on process stats", () => {
|
||||
it("if active calculate stats reports", async () => {
|
||||
const getStats = jest.spyOn(rtcSpy, "getStats");
|
||||
getStats.mockResolvedValue({} as RTCStatsReport);
|
||||
await collector.processStats("GROUP_CALL_ID", "LOCAL_USER_ID");
|
||||
const report = {} as RTCStatsReport;
|
||||
report.forEach = jest.fn().mockReturnValue([]);
|
||||
getStats.mockResolvedValue(report);
|
||||
const actual = await collector.processStats("GROUP_CALL_ID", "LOCAL_USER_ID");
|
||||
expect(getStats).toHaveBeenCalled();
|
||||
expect(actual).toEqual({
|
||||
receivedMedia: 0,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 0, muted: 0 },
|
||||
videoTrackSummary: { count: 0, muted: 0 },
|
||||
});
|
||||
expect(collector.getActive()).toBeTruthy();
|
||||
});
|
||||
|
||||
it("if not active do not calculate stats reports", async () => {
|
||||
@@ -60,7 +70,13 @@ describe("StatsReportGatherer", () => {
|
||||
// @ts-ignore
|
||||
getStats.mockReturnValue({});
|
||||
const actual = await collector.processStats("GROUP_CALL_ID", "LOCAL_USER_ID");
|
||||
expect(actual).toBeFalsy();
|
||||
expect(actual).toEqual({
|
||||
receivedMedia: 0,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 0, muted: 0 },
|
||||
videoTrackSummary: { count: 0, muted: 0 },
|
||||
});
|
||||
expect(getStats).toHaveBeenCalled();
|
||||
expect(collector.getActive()).toBeFalsy();
|
||||
});
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import { SummaryStatsReporter } from "../../../../src/webrtc/stats/summaryStatsReporter";
|
||||
import { StatsReportEmitter } from "../../../../src/webrtc/stats/statsReportEmitter";
|
||||
|
||||
describe("SummaryStatsReporter", () => {
|
||||
let reporter: SummaryStatsReporter;
|
||||
let emitter: StatsReportEmitter;
|
||||
beforeEach(() => {
|
||||
emitter = new StatsReportEmitter();
|
||||
emitter.emitSummaryStatsReport = jest.fn();
|
||||
reporter = new SummaryStatsReporter(emitter);
|
||||
});
|
||||
|
||||
describe("build Summary Stats Report", () => {
|
||||
it("should do nothing if summary list empty", async () => {
|
||||
reporter.build([]);
|
||||
expect(emitter.emitSummaryStatsReport).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should trigger new summary report", async () => {
|
||||
const summary = [
|
||||
{
|
||||
receivedMedia: 10,
|
||||
receivedAudioMedia: 4,
|
||||
receivedVideoMedia: 6,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 1, muted: 0 },
|
||||
},
|
||||
{
|
||||
receivedMedia: 13,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 13,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 1, muted: 0 },
|
||||
},
|
||||
{
|
||||
receivedMedia: 0,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 1, muted: 0 },
|
||||
},
|
||||
{
|
||||
receivedMedia: 15,
|
||||
receivedAudioMedia: 6,
|
||||
receivedVideoMedia: 9,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 1, muted: 0 },
|
||||
},
|
||||
];
|
||||
reporter.build(summary);
|
||||
expect(emitter.emitSummaryStatsReport).toHaveBeenCalledWith({
|
||||
percentageReceivedMedia: 0.5,
|
||||
percentageReceivedAudioMedia: 0.5,
|
||||
percentageReceivedVideoMedia: 0.75,
|
||||
});
|
||||
});
|
||||
|
||||
it("as received video Media, although video was not received, but because video muted", async () => {
|
||||
const summary = [
|
||||
{
|
||||
receivedMedia: 10,
|
||||
receivedAudioMedia: 10,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 1, muted: 1 },
|
||||
},
|
||||
];
|
||||
reporter.build(summary);
|
||||
expect(emitter.emitSummaryStatsReport).toHaveBeenCalledWith({
|
||||
percentageReceivedMedia: 1,
|
||||
percentageReceivedAudioMedia: 1,
|
||||
percentageReceivedVideoMedia: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("as received no video Media, because only on video was muted", async () => {
|
||||
const summary = [
|
||||
{
|
||||
receivedMedia: 10,
|
||||
receivedAudioMedia: 10,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 1, muted: 0 },
|
||||
videoTrackSummary: { count: 2, muted: 1 },
|
||||
},
|
||||
];
|
||||
reporter.build(summary);
|
||||
expect(emitter.emitSummaryStatsReport).toHaveBeenCalledWith({
|
||||
percentageReceivedMedia: 0,
|
||||
percentageReceivedAudioMedia: 1,
|
||||
percentageReceivedVideoMedia: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("as received no audio Media, although audio not received and audio muted", async () => {
|
||||
const summary = [
|
||||
{
|
||||
receivedMedia: 100,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 100,
|
||||
audioTrackSummary: { count: 1, muted: 1 },
|
||||
videoTrackSummary: { count: 1, muted: 0 },
|
||||
},
|
||||
];
|
||||
reporter.build(summary);
|
||||
expect(emitter.emitSummaryStatsReport).toHaveBeenCalledWith({
|
||||
percentageReceivedMedia: 0,
|
||||
percentageReceivedAudioMedia: 0,
|
||||
percentageReceivedVideoMedia: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -129,4 +129,147 @@ describe("TrackStatsReporter", () => {
|
||||
expect(trackStats.getLoss()).toEqual({ packetsTotal: 280, packetsLost: 80, isDownloadStream: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("should set state of a TrackStats", () => {
|
||||
it("to not alive if Transceiver undefined", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, undefined);
|
||||
expect(trackStats.alive).toBeFalsy();
|
||||
});
|
||||
|
||||
it("to not alive if Transceiver has no local track", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "local", "video");
|
||||
const ts = {
|
||||
sender: {
|
||||
track: null,
|
||||
} as RTCRtpSender,
|
||||
} as RTCRtpTransceiver;
|
||||
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
expect(trackStats.alive).toBeFalsy();
|
||||
});
|
||||
|
||||
it("to alive if Transceiver remote and track is alive", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
trackStats.alive = false;
|
||||
const ts = {
|
||||
receiver: {
|
||||
track: {
|
||||
readyState: "live",
|
||||
enabled: false,
|
||||
muted: false,
|
||||
} as MediaStreamTrack,
|
||||
} as RTCRtpReceiver,
|
||||
} as RTCRtpTransceiver;
|
||||
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
expect(trackStats.alive).toBeTruthy();
|
||||
});
|
||||
|
||||
it("to alive if Transceiver local and track is live", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "local", "video");
|
||||
trackStats.alive = false;
|
||||
const ts = {
|
||||
sender: {
|
||||
track: {
|
||||
readyState: "live",
|
||||
enabled: false,
|
||||
muted: false,
|
||||
} as MediaStreamTrack,
|
||||
} as RTCRtpSender,
|
||||
} as RTCRtpTransceiver;
|
||||
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
expect(trackStats.alive).toBeTruthy();
|
||||
});
|
||||
|
||||
it("to not alive if Transceiver track is ended", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
const ts = {
|
||||
receiver: {
|
||||
track: {
|
||||
readyState: "ended",
|
||||
enabled: false,
|
||||
muted: false,
|
||||
} as MediaStreamTrack,
|
||||
} as RTCRtpReceiver,
|
||||
} as RTCRtpTransceiver;
|
||||
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
expect(trackStats.alive).toBeFalsy();
|
||||
});
|
||||
|
||||
it("to not alive and muted if Transceiver track is live and muted", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
const ts = {
|
||||
receiver: {
|
||||
track: {
|
||||
readyState: "live",
|
||||
enabled: false,
|
||||
muted: true,
|
||||
} as MediaStreamTrack,
|
||||
} as RTCRtpReceiver,
|
||||
} as RTCRtpTransceiver;
|
||||
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
expect(trackStats.alive).toBeTruthy();
|
||||
expect(trackStats.muted).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("should build Track Summary", () => {
|
||||
it("and returns empty summary if stats list empty", async () => {
|
||||
const summary = TrackStatsReporter.buildTrackSummary([]);
|
||||
expect(summary).toEqual({
|
||||
audioTrackSummary: {
|
||||
count: 0,
|
||||
muted: 0,
|
||||
},
|
||||
videoTrackSummary: {
|
||||
count: 0,
|
||||
muted: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("and returns summary if stats list not empty", async () => {
|
||||
const summary = TrackStatsReporter.buildTrackSummary([]);
|
||||
expect(summary).toEqual({
|
||||
audioTrackSummary: {
|
||||
count: 0,
|
||||
muted: 0,
|
||||
},
|
||||
videoTrackSummary: {
|
||||
count: 0,
|
||||
muted: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("should build jitter value in Track Stats", () => {
|
||||
it("and returns track stats without jitter if report not 'inbound-rtp'", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
TrackStatsReporter.buildJitter(trackStats, { jitter: 0.01 });
|
||||
expect(trackStats.getJitter()).toEqual(0);
|
||||
});
|
||||
|
||||
it("and returns track stats with jitter", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
TrackStatsReporter.buildJitter(trackStats, { type: "inbound-rtp", jitter: 0.01 });
|
||||
expect(trackStats.getJitter()).toEqual(10);
|
||||
});
|
||||
|
||||
it("and returns negative jitter if stats has no jitter value", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
TrackStatsReporter.buildJitter(trackStats, { type: "inbound-rtp" });
|
||||
expect(trackStats.getJitter()).toEqual(-1);
|
||||
});
|
||||
|
||||
it("and returns jitter as number", async () => {
|
||||
const trackStats = new MediaTrackStats("1", "remote", "video");
|
||||
TrackStatsReporter.buildJitter(trackStats, { type: "inbound-rtp", jitter: "0.5" });
|
||||
expect(trackStats.getJitter()).toEqual(500);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -112,6 +112,12 @@ export interface LoginTokenPostResponse {
|
||||
login_token: string;
|
||||
/**
|
||||
* Expiration in seconds.
|
||||
*
|
||||
* @deprecated this is only provided for compatibility with original revision of the MSC.
|
||||
*/
|
||||
expires_in: number;
|
||||
/**
|
||||
* Expiration in milliseconds.
|
||||
*/
|
||||
expires_in_ms: number;
|
||||
}
|
||||
|
||||
+114
-54
@@ -30,6 +30,7 @@ import {
|
||||
MatrixEvent,
|
||||
MatrixEventEvent,
|
||||
MatrixEventHandlerMap,
|
||||
PushDetails,
|
||||
} from "./models/event";
|
||||
import { StubStore } from "./store/stub";
|
||||
import { CallEvent, CallEventHandlerMap, createNewMatrixCall, MatrixCall, supportsMatrixCall } from "./webrtc/call";
|
||||
@@ -37,7 +38,7 @@ import { Filter, IFilterDefinition, IRoomEventFilter } from "./filter";
|
||||
import { CallEventHandlerEvent, CallEventHandler, CallEventHandlerEventHandlerMap } from "./webrtc/callEventHandler";
|
||||
import { GroupCallEventHandlerEvent, GroupCallEventHandlerEventHandlerMap } from "./webrtc/groupCallEventHandler";
|
||||
import * as utils from "./utils";
|
||||
import { replaceParam, QueryDict, sleep, noUnsafeEventProps } from "./utils";
|
||||
import { replaceParam, QueryDict, sleep, noUnsafeEventProps, safeSet } from "./utils";
|
||||
import { Direction, EventTimeline } from "./models/event-timeline";
|
||||
import { IActionsObject, PushProcessor } from "./pushprocessor";
|
||||
import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery";
|
||||
@@ -101,7 +102,6 @@ import { IAuthData, IAuthDict } from "./interactive-auth";
|
||||
import { IMinimalEvent, IRoomEvent, IStateEvent } from "./sync-accumulator";
|
||||
import {
|
||||
CrossSigningKey,
|
||||
IAddSecretStorageKeyOpts,
|
||||
ICreateSecretStorageOpts,
|
||||
IEncryptedEventInfo,
|
||||
IImportRoomKeysOpts,
|
||||
@@ -207,7 +207,12 @@ import { CryptoBackend } from "./common-crypto/CryptoBackend";
|
||||
import { RUST_SDK_STORE_PREFIX } from "./rust-crypto/constants";
|
||||
import { CryptoApi } from "./crypto-api";
|
||||
import { DeviceInfoMap } from "./crypto/DeviceList";
|
||||
import { SecretStorageKeyDescription } from "./secret-storage";
|
||||
import {
|
||||
AddSecretStorageKeyOpts,
|
||||
SecretStorageKeyDescription,
|
||||
ServerSideSecretStorage,
|
||||
ServerSideSecretStorageImpl,
|
||||
} from "./secret-storage";
|
||||
|
||||
export type Store = IStore;
|
||||
|
||||
@@ -489,11 +494,21 @@ export interface IChangePasswordCapability extends ICapability {}
|
||||
|
||||
export interface IThreadsCapability extends ICapability {}
|
||||
|
||||
interface ICapabilities {
|
||||
export interface IMSC3882GetLoginTokenCapability extends ICapability {}
|
||||
|
||||
export const UNSTABLE_MSC3882_CAPABILITY = new UnstableValue("m.get_login_token", "org.matrix.msc3882.get_login_token");
|
||||
|
||||
/**
|
||||
* A representation of the capabilities advertised by a homeserver as defined by
|
||||
* [Capabilities negotiation](https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3capabilities).
|
||||
*/
|
||||
export interface Capabilities {
|
||||
[key: string]: any;
|
||||
"m.change_password"?: IChangePasswordCapability;
|
||||
"m.room_versions"?: IRoomVersionsCapability;
|
||||
"io.element.thread"?: IThreadsCapability;
|
||||
[UNSTABLE_MSC3882_CAPABILITY.name]?: IMSC3882GetLoginTokenCapability;
|
||||
[UNSTABLE_MSC3882_CAPABILITY.altName]?: IMSC3882GetLoginTokenCapability;
|
||||
}
|
||||
|
||||
/* eslint-disable camelcase */
|
||||
@@ -1226,7 +1241,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
protected serverVersionsPromise?: Promise<IServerVersions>;
|
||||
|
||||
public cachedCapabilities?: {
|
||||
capabilities: ICapabilities;
|
||||
capabilities: Capabilities;
|
||||
expiration: number;
|
||||
};
|
||||
protected clientWellKnown?: IClientWellKnown;
|
||||
@@ -1243,6 +1258,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
private useE2eForGroupCall = true;
|
||||
private toDeviceMessageQueue: ToDeviceMessageQueue;
|
||||
|
||||
private _secretStorage: ServerSideSecretStorageImpl;
|
||||
|
||||
// A manager for determining which invites should be ignored.
|
||||
public readonly ignoredInvites: IgnoredInvites;
|
||||
|
||||
@@ -1408,6 +1425,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
});
|
||||
|
||||
this.ignoredInvites = new IgnoredInvites(this);
|
||||
this._secretStorage = new ServerSideSecretStorageImpl(this, opts.cryptoCallbacks ?? {});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2045,7 +2063,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @returns Promise which resolves to the capabilities of the homeserver
|
||||
* @returns Rejects: with an error response.
|
||||
*/
|
||||
public getCapabilities(fresh = false): Promise<ICapabilities> {
|
||||
public getCapabilities(fresh = false): Promise<Capabilities> {
|
||||
const now = new Date().getTime();
|
||||
|
||||
if (this.cachedCapabilities && !fresh) {
|
||||
@@ -2056,7 +2074,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
}
|
||||
|
||||
type Response = {
|
||||
capabilities?: ICapabilities;
|
||||
capabilities?: Capabilities;
|
||||
};
|
||||
return this.http
|
||||
.authedRequest<Response>(Method.Get, "/capabilities")
|
||||
@@ -2217,6 +2235,13 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
this.on(RoomMemberEvent.Membership, rustCrypto.onRoomMembership.bind(rustCrypto));
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the server-side secret storage API for this client.
|
||||
*/
|
||||
public get secretStorage(): ServerSideSecretStorage {
|
||||
return this._secretStorage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the crypto API for this client.
|
||||
*
|
||||
@@ -2269,7 +2294,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @param userIds - The users to fetch.
|
||||
* @param forceDownload - Always download the keys even if cached.
|
||||
*
|
||||
* @returns A promise which resolves to a map userId-\>deviceId-\>{@link DeviceInfo}
|
||||
* @returns A promise which resolves to a map userId-\>deviceId-\>`DeviceInfo`
|
||||
*/
|
||||
public downloadKeys(userIds: string[], forceDownload?: boolean): Promise<DeviceInfoMap> {
|
||||
if (!this.crypto) {
|
||||
@@ -2463,11 +2488,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
return this.crypto.beginKeyVerification(method, userId, deviceId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#checkKey}.
|
||||
*/
|
||||
public checkSecretStorageKey(key: Uint8Array, info: SecretStorageKeyDescription): Promise<boolean> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.checkSecretStorageKey(key, info);
|
||||
return this.secretStorage.checkKey(key, info);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2858,16 +2883,15 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @returns An object with:
|
||||
* keyId: the ID of the key
|
||||
* keyInfo: details about the key (iv, mac, passphrase)
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#addKey}.
|
||||
*/
|
||||
public addSecretStorageKey(
|
||||
algorithm: string,
|
||||
opts: IAddSecretStorageKeyOpts,
|
||||
opts: AddSecretStorageKeyOpts,
|
||||
keyName?: string,
|
||||
): Promise<{ keyId: string; keyInfo: SecretStorageKeyDescription }> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.addSecretStorageKey(algorithm, opts, keyName);
|
||||
return this.secretStorage.addKey(algorithm, opts, keyName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2878,12 +2902,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns Whether we have the key.
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#hasKey}.
|
||||
*/
|
||||
public hasSecretStorageKey(keyId?: string): Promise<boolean> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.hasSecretStorageKey(keyId);
|
||||
return this.secretStorage.hasKey(keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2895,12 +2918,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @param secret - The secret contents.
|
||||
* @param keys - The IDs of the keys to use to encrypt the secret or null/undefined
|
||||
* to use the default (will throw if no default key is set).
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#store}.
|
||||
*/
|
||||
public storeSecret(name: string, secret: string, keys?: string[]): Promise<void> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.storeSecret(name, secret, keys);
|
||||
return this.secretStorage.store(name, secret, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2911,12 +2933,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @param name - the name of the secret
|
||||
*
|
||||
* @returns the contents of the secret
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#get}.
|
||||
*/
|
||||
public getSecret(name: string): Promise<string | undefined> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.getSecret(name);
|
||||
return this.secretStorage.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2928,12 +2949,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @returns map of key name to key info the secret is encrypted
|
||||
* with, or null if it is not present or not encrypted with a trusted
|
||||
* key
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#isStored}.
|
||||
*/
|
||||
public isSecretStored(name: string): Promise<Record<string, SecretStorageKeyDescription> | null> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.isSecretStored(name);
|
||||
return this.secretStorage.isStored(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2959,12 +2979,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @returns The default key ID or null if no default key ID is set
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#getDefaultKeyId}.
|
||||
*/
|
||||
public getDefaultSecretStorageKeyId(): Promise<string | null> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.getDefaultSecretStorageKeyId();
|
||||
return this.secretStorage.getDefaultKeyId();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2973,12 +2992,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* The Secure Secret Storage API is currently UNSTABLE and may change without notice.
|
||||
*
|
||||
* @param keyId - The new default key ID
|
||||
*
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#setDefaultKeyId}.
|
||||
*/
|
||||
public setDefaultSecretStorageKeyId(keyId: string): Promise<void> {
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
return this.crypto.setDefaultSecretStorageKeyId(keyId);
|
||||
return this.secretStorage.setDefaultKeyId(keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2991,6 +3009,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @param privateKey - The private key
|
||||
* @param expectedPublicKey - The public key
|
||||
* @returns true if the key matches, otherwise false
|
||||
*
|
||||
* @deprecated The use of asymmetric keys for SSSS is deprecated.
|
||||
* Use {@link SecretStorage.ServerSideSecretStorage#checkKey} for symmetric keys.
|
||||
*/
|
||||
public checkSecretStoragePrivateKey(privateKey: Uint8Array, expectedPublicKey: string): boolean {
|
||||
if (!this.crypto) {
|
||||
@@ -3287,7 +3308,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
await this.crypto.backupManager.prepareKeyBackupVersion(password);
|
||||
|
||||
if (opts.secureSecretStorage) {
|
||||
await this.storeSecret("m.megolm_backup.v1", encodeBase64(privateKey));
|
||||
await this.secretStorage.store("m.megolm_backup.v1", encodeBase64(privateKey));
|
||||
logger.info("Key backup private key stored in secret storage");
|
||||
}
|
||||
|
||||
@@ -3307,7 +3328,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* trusted key
|
||||
*/
|
||||
public isKeyBackupKeyStored(): Promise<Record<string, SecretStorageKeyDescription> | null> {
|
||||
return Promise.resolve(this.isSecretStored("m.megolm_backup.v1"));
|
||||
return Promise.resolve(this.secretStorage.isStored("m.megolm_backup.v1"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3572,14 +3593,14 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
if (!this.crypto) {
|
||||
throw new Error("End-to-end encryption disabled");
|
||||
}
|
||||
const storedKey = await this.getSecret("m.megolm_backup.v1");
|
||||
const storedKey = await this.secretStorage.get("m.megolm_backup.v1");
|
||||
|
||||
// ensure that the key is in the right format. If not, fix the key and
|
||||
// store the fixed version
|
||||
const fixedKey = fixBackupKey(storedKey);
|
||||
if (fixedKey) {
|
||||
const keys = await this.crypto.getSecretStorageKey();
|
||||
await this.storeSecret("m.megolm_backup.v1", fixedKey, [keys![0]]);
|
||||
const keys = await this.secretStorage.getKey();
|
||||
await this.secretStorage.store("m.megolm_backup.v1", fixedKey, [keys![0]]);
|
||||
}
|
||||
|
||||
const privKey = decodeBase64(fixedKey || storedKey!);
|
||||
@@ -5365,11 +5386,28 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
*/
|
||||
public getPushActionsForEvent(event: MatrixEvent, forceRecalculate = false): IActionsObject | null {
|
||||
if (!event.getPushActions() || forceRecalculate) {
|
||||
event.setPushActions(this.pushProcessor.actionsForEvent(event));
|
||||
const { actions, rule } = this.pushProcessor.actionsAndRuleForEvent(event);
|
||||
event.setPushDetails(actions, rule);
|
||||
}
|
||||
return event.getPushActions();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtain a dict of actions which should be performed for this event according
|
||||
* to the push rules for this user. Caches the dict on the event.
|
||||
* @param event - The event to get push actions for.
|
||||
* @param forceRecalculate - forces to recalculate actions for an event
|
||||
* Useful when an event just got decrypted
|
||||
* @returns A dict of actions to perform.
|
||||
*/
|
||||
public getPushDetailsForEvent(event: MatrixEvent, forceRecalculate = false): PushDetails | null {
|
||||
if (!event.getPushDetails() || forceRecalculate) {
|
||||
const { actions, rule } = this.pushProcessor.actionsAndRuleForEvent(event);
|
||||
event.setPushDetails(actions, rule);
|
||||
}
|
||||
return event.getPushDetails();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param info - The kind of info to set (e.g. 'avatar_url')
|
||||
* @param data - The JSON object to set.
|
||||
@@ -6044,7 +6082,11 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
for (let i = 0; i < res.notifications.length; i++) {
|
||||
const notification = res.notifications[i];
|
||||
const event = this.getEventMapper()(notification.event);
|
||||
event.setPushActions(PushProcessor.actionListToActionsObject(notification.actions));
|
||||
|
||||
// @TODO(kerrya) reprocessing every notification is ugly
|
||||
// remove if we get server MSC3994 support
|
||||
this.getPushDetailsForEvent(event, true);
|
||||
|
||||
event.event.room_id = notification.room_id; // XXX: gutwrenching
|
||||
matrixEvents[i] = event;
|
||||
}
|
||||
@@ -7462,7 +7504,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* Set the identity server URL of this client
|
||||
* @param url - New identity server URL
|
||||
*/
|
||||
public setIdentityServerUrl(url: string): void {
|
||||
public setIdentityServerUrl(url?: string): void {
|
||||
this.idBaseUrl = utils.ensureNoTrailingSlash(url);
|
||||
this.http.setIdBaseUrl(this.idBaseUrl);
|
||||
}
|
||||
@@ -7809,15 +7851,33 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
* @returns Promise which resolves: On success, the token response
|
||||
* or UIA auth data.
|
||||
*/
|
||||
public requestLoginToken(auth?: IAuthData): Promise<UIAResponse<LoginTokenPostResponse>> {
|
||||
public async requestLoginToken(auth?: IAuthData): Promise<UIAResponse<LoginTokenPostResponse>> {
|
||||
// use capabilities to determine which revision of the MSC is being used
|
||||
const capabilities = await this.getCapabilities();
|
||||
// use r1 endpoint if capability is exposed otherwise use old r0 endpoint
|
||||
const endpoint = UNSTABLE_MSC3882_CAPABILITY.findIn(capabilities)
|
||||
? "/org.matrix.msc3882/login/get_token" // r1 endpoint
|
||||
: "/org.matrix.msc3882/login/token"; // r0 endpoint
|
||||
|
||||
const body: UIARequest<{}> = { auth };
|
||||
return this.http.authedRequest(
|
||||
const res = await this.http.authedRequest<UIAResponse<LoginTokenPostResponse>>(
|
||||
Method.Post,
|
||||
"/org.matrix.msc3882/login/token",
|
||||
endpoint,
|
||||
undefined, // no query params
|
||||
body,
|
||||
{ prefix: ClientPrefix.Unstable },
|
||||
);
|
||||
|
||||
// the representation of expires_in changed from revision 0 to revision 1 so we populate
|
||||
if ("login_token" in res) {
|
||||
if (typeof res.expires_in_ms === "number") {
|
||||
res.expires_in = Math.floor(res.expires_in_ms / 1000);
|
||||
} else if (typeof res.expires_in === "number") {
|
||||
res.expires_in_ms = res.expires_in * 1000;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -8749,8 +8809,8 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
|
||||
|
||||
for (const [userId, deviceId] of devices) {
|
||||
const query = queries[userId] || {};
|
||||
queries[userId] = query;
|
||||
query[deviceId] = keyAlgorithm;
|
||||
safeSet(queries, userId, query);
|
||||
safeSet(query, deviceId, keyAlgorithm);
|
||||
}
|
||||
const content: IClaimKeysRequest = { one_time_keys: queries };
|
||||
if (timeout) {
|
||||
|
||||
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import type { IToDeviceEvent } from "../sync-accumulator";
|
||||
import type { IDeviceLists, IToDeviceEvent } from "../sync-accumulator";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import { Room } from "../models/room";
|
||||
import { CryptoApi } from "../crypto-api";
|
||||
@@ -106,32 +106,20 @@ export interface SyncCryptoCallbacks {
|
||||
preprocessToDeviceMessages(events: IToDeviceEvent[]): Promise<IToDeviceEvent[]>;
|
||||
|
||||
/**
|
||||
* Called by the /sync loop whenever there are incoming to-device messages.
|
||||
*
|
||||
* The implementation may preprocess the received messages (eg, decrypt them) and return an
|
||||
* updated list of messages for dispatch to the rest of the system.
|
||||
*
|
||||
* Note that, unlike {@link ClientEvent.ToDeviceEvent} events, this is called on the raw to-device
|
||||
* messages, rather than the results of any decryption attempts.
|
||||
* Called by the /sync loop when one time key counts and unused fallback key details are received.
|
||||
*
|
||||
* @param oneTimeKeysCounts - the received one time key counts
|
||||
* @returns A list of preprocessed to-device messages.
|
||||
* @param unusedFallbackKeys - the received unused fallback keys
|
||||
*/
|
||||
preprocessOneTimeKeyCounts(oneTimeKeysCounts: Map<string, number>): Promise<void>;
|
||||
processKeyCounts(oneTimeKeysCounts?: Record<string, number>, unusedFallbackKeys?: string[]): Promise<void>;
|
||||
|
||||
/**
|
||||
* Called by the /sync loop whenever there are incoming to-device messages.
|
||||
* Handle the notification from /sync that device lists have
|
||||
* been changed.
|
||||
*
|
||||
* The implementation may preprocess the received messages (eg, decrypt them) and return an
|
||||
* updated list of messages for dispatch to the rest of the system.
|
||||
*
|
||||
* Note that, unlike {@link ClientEvent.ToDeviceEvent} events, this is called on the raw to-device
|
||||
* messages, rather than the results of any decryption attempts.
|
||||
*
|
||||
* @param unusedFallbackKeys - the received unused fallback keys
|
||||
* @returns A list of preprocessed to-device messages.
|
||||
* @param deviceLists - device_lists field from /sync
|
||||
*/
|
||||
preprocessUnusedFallbackKeys(unusedFallbackKeys: Set<string>): Promise<void>;
|
||||
processDeviceLists(deviceLists: IDeviceLists): Promise<void>;
|
||||
|
||||
/**
|
||||
* Called by the /sync loop whenever an m.room.encryption event is received.
|
||||
|
||||
@@ -25,13 +25,12 @@ import { logger } from "../logger";
|
||||
import { IndexedDBCryptoStore } from "../crypto/store/indexeddb-crypto-store";
|
||||
import { decryptAES, encryptAES } from "./aes";
|
||||
import { DeviceInfo } from "./deviceinfo";
|
||||
import { SecretStorage } from "./SecretStorage";
|
||||
import { ICrossSigningKey, ISignedKey, MatrixClient } from "../client";
|
||||
import { OlmDevice } from "./OlmDevice";
|
||||
import { ICryptoCallbacks } from ".";
|
||||
import { ISignatures } from "../@types/signed";
|
||||
import { CryptoStore, SecretStorePrivateKeys } from "./store/base";
|
||||
import { SecretStorageKeyDescription } from "../secret-storage";
|
||||
import { ServerSideSecretStorage, SecretStorageKeyDescription } from "../secret-storage";
|
||||
|
||||
const KEY_REQUEST_TIMEOUT_MS = 1000 * 60;
|
||||
|
||||
@@ -164,7 +163,7 @@ export class CrossSigningInfo {
|
||||
* key
|
||||
*/
|
||||
public async isStoredInSecretStorage(
|
||||
secretStorage: SecretStorage<MatrixClient | undefined>,
|
||||
secretStorage: ServerSideSecretStorage,
|
||||
): Promise<Record<string, object> | null> {
|
||||
// check what SSSS keys have encrypted the master key (if any)
|
||||
const stored = (await secretStorage.isStored("m.cross_signing.master")) || {};
|
||||
@@ -192,7 +191,7 @@ export class CrossSigningInfo {
|
||||
*/
|
||||
public static async storeInSecretStorage(
|
||||
keys: Map<string, Uint8Array>,
|
||||
secretStorage: SecretStorage<undefined>,
|
||||
secretStorage: ServerSideSecretStorage,
|
||||
): Promise<void> {
|
||||
for (const [type, privateKey] of keys) {
|
||||
const encodedKey = encodeBase64(privateKey);
|
||||
@@ -209,7 +208,10 @@ export class CrossSigningInfo {
|
||||
* @param secretStorage - The secret store using account data
|
||||
* @returns The private key
|
||||
*/
|
||||
public static async getFromSecretStorage(type: string, secretStorage: SecretStorage): Promise<Uint8Array | null> {
|
||||
public static async getFromSecretStorage(
|
||||
type: string,
|
||||
secretStorage: ServerSideSecretStorage,
|
||||
): Promise<Uint8Array | null> {
|
||||
const encodedKey = await secretStorage.get(`m.cross_signing.${type}`);
|
||||
if (!encodedKey) {
|
||||
return null;
|
||||
|
||||
@@ -15,7 +15,7 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
import { logger } from "../logger";
|
||||
import { IContent, MatrixEvent } from "../models/event";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import { createCryptoStoreCacheCallbacks, ICacheCallbacks } from "./CrossSigning";
|
||||
import { IndexedDBCryptoStore } from "./store/indexeddb-crypto-store";
|
||||
import { Method, ClientPrefix } from "../http-api";
|
||||
@@ -30,8 +30,7 @@ import {
|
||||
} from "../client";
|
||||
import { IKeyBackupInfo } from "./keybackup";
|
||||
import { TypedEventEmitter } from "../models/typed-event-emitter";
|
||||
import { IAccountDataClient } from "./SecretStorage";
|
||||
import { SecretStorageKeyDescription } from "../secret-storage";
|
||||
import { AccountDataClient, SecretStorageKeyDescription } from "../secret-storage";
|
||||
|
||||
interface ICrossSigningKeys {
|
||||
authUpload: IBootstrapCrossSigningOpts["authUploadDeviceSigningKeys"];
|
||||
@@ -238,7 +237,7 @@ export class EncryptionSetupOperation {
|
||||
*/
|
||||
class AccountDataClientAdapter
|
||||
extends TypedEventEmitter<ClientEvent.AccountData, ClientEventHandlerMap>
|
||||
implements IAccountDataClient
|
||||
implements AccountDataClient
|
||||
{
|
||||
//
|
||||
public readonly values = new Map<string, MatrixEvent>();
|
||||
@@ -253,21 +252,21 @@ class AccountDataClientAdapter
|
||||
/**
|
||||
* @returns the content of the account data
|
||||
*/
|
||||
public getAccountDataFromServer<T extends { [k: string]: any }>(type: string): Promise<T> {
|
||||
return Promise.resolve(this.getAccountData(type) as T);
|
||||
public getAccountDataFromServer<T extends { [k: string]: any }>(type: string): Promise<T | null> {
|
||||
return Promise.resolve(this.getAccountData(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns the content of the account data
|
||||
*/
|
||||
public getAccountData(type: string): IContent | null {
|
||||
public getAccountData<T extends { [k: string]: any }>(type: string): T | null {
|
||||
const modifiedValue = this.values.get(type);
|
||||
if (modifiedValue) {
|
||||
return modifiedValue;
|
||||
return modifiedValue as unknown as T;
|
||||
}
|
||||
const existingValue = this.existingValues.get(type);
|
||||
if (existingValue) {
|
||||
return existingValue.getContent();
|
||||
return existingValue.getContent<T>();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
Copyright 2019-2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
|
||||
import { MatrixClient } from "../client";
|
||||
import { ICryptoCallbacks, IEncryptedContent } from "./index";
|
||||
import { defer, IDeferred } from "../utils";
|
||||
import { ToDeviceMessageId } from "../@types/event";
|
||||
import { logger } from "../logger";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import * as olmlib from "./olmlib";
|
||||
|
||||
export interface ISecretRequest {
|
||||
requestId: string;
|
||||
promise: Promise<string>;
|
||||
cancel: (reason: string) => void;
|
||||
}
|
||||
|
||||
interface ISecretRequestInternal {
|
||||
name: string;
|
||||
devices: string[];
|
||||
deferred: IDeferred<string>;
|
||||
}
|
||||
|
||||
export class SecretSharing {
|
||||
private requests = new Map<string, ISecretRequestInternal>();
|
||||
|
||||
public constructor(private readonly baseApis: MatrixClient, private readonly cryptoCallbacks: ICryptoCallbacks) {}
|
||||
|
||||
/**
|
||||
* Request a secret from another device
|
||||
*
|
||||
* @param name - the name of the secret to request
|
||||
* @param devices - the devices to request the secret from
|
||||
*/
|
||||
public request(name: string, devices: string[]): ISecretRequest {
|
||||
const requestId = this.baseApis.makeTxnId();
|
||||
|
||||
const deferred = defer<string>();
|
||||
this.requests.set(requestId, { name, devices, deferred });
|
||||
|
||||
const cancel = (reason: string): void => {
|
||||
// send cancellation event
|
||||
const cancelData = {
|
||||
action: "request_cancellation",
|
||||
requesting_device_id: this.baseApis.deviceId,
|
||||
request_id: requestId,
|
||||
};
|
||||
const toDevice: Map<string, typeof cancelData> = new Map();
|
||||
for (const device of devices) {
|
||||
toDevice.set(device, cancelData);
|
||||
}
|
||||
this.baseApis.sendToDevice("m.secret.request", new Map([[this.baseApis.getUserId()!, toDevice]]));
|
||||
|
||||
// and reject the promise so that anyone waiting on it will be
|
||||
// notified
|
||||
deferred.reject(new Error(reason || "Cancelled"));
|
||||
};
|
||||
|
||||
// send request to devices
|
||||
const requestData = {
|
||||
name,
|
||||
action: "request",
|
||||
requesting_device_id: this.baseApis.deviceId,
|
||||
request_id: requestId,
|
||||
[ToDeviceMessageId]: uuidv4(),
|
||||
};
|
||||
const toDevice: Map<string, typeof requestData> = new Map();
|
||||
for (const device of devices) {
|
||||
toDevice.set(device, requestData);
|
||||
}
|
||||
logger.info(`Request secret ${name} from ${devices}, id ${requestId}`);
|
||||
this.baseApis.sendToDevice("m.secret.request", new Map([[this.baseApis.getUserId()!, toDevice]]));
|
||||
|
||||
return {
|
||||
requestId,
|
||||
promise: deferred.promise,
|
||||
cancel,
|
||||
};
|
||||
}
|
||||
|
||||
public async onRequestReceived(event: MatrixEvent): Promise<void> {
|
||||
const sender = event.getSender();
|
||||
const content = event.getContent();
|
||||
if (
|
||||
sender !== this.baseApis.getUserId() ||
|
||||
!(content.name && content.action && content.requesting_device_id && content.request_id)
|
||||
) {
|
||||
// ignore requests from anyone else, for now
|
||||
return;
|
||||
}
|
||||
const deviceId = content.requesting_device_id;
|
||||
// check if it's a cancel
|
||||
if (content.action === "request_cancellation") {
|
||||
/*
|
||||
Looks like we intended to emit events when we got cancelations, but
|
||||
we never put anything in the _incomingRequests object, and the request
|
||||
itself doesn't use events anyway so if we were to wire up cancellations,
|
||||
they probably ought to use the same callback interface. I'm leaving them
|
||||
disabled for now while converting this file to typescript.
|
||||
if (this._incomingRequests[deviceId]
|
||||
&& this._incomingRequests[deviceId][content.request_id]) {
|
||||
logger.info(
|
||||
"received request cancellation for secret (" + sender +
|
||||
", " + deviceId + ", " + content.request_id + ")",
|
||||
);
|
||||
this.baseApis.emit("crypto.secrets.requestCancelled", {
|
||||
user_id: sender,
|
||||
device_id: deviceId,
|
||||
request_id: content.request_id,
|
||||
});
|
||||
}
|
||||
*/
|
||||
} else if (content.action === "request") {
|
||||
if (deviceId === this.baseApis.deviceId) {
|
||||
// no point in trying to send ourself the secret
|
||||
return;
|
||||
}
|
||||
|
||||
// check if we have the secret
|
||||
logger.info("received request for secret (" + sender + ", " + deviceId + ", " + content.request_id + ")");
|
||||
if (!this.cryptoCallbacks.onSecretRequested) {
|
||||
return;
|
||||
}
|
||||
const secret = await this.cryptoCallbacks.onSecretRequested(
|
||||
sender,
|
||||
deviceId,
|
||||
content.request_id,
|
||||
content.name,
|
||||
this.baseApis.checkDeviceTrust(sender, deviceId),
|
||||
);
|
||||
if (secret) {
|
||||
logger.info(`Preparing ${content.name} secret for ${deviceId}`);
|
||||
const payload = {
|
||||
type: "m.secret.send",
|
||||
content: {
|
||||
request_id: content.request_id,
|
||||
secret: secret,
|
||||
},
|
||||
};
|
||||
const encryptedContent: IEncryptedContent = {
|
||||
algorithm: olmlib.OLM_ALGORITHM,
|
||||
sender_key: this.baseApis.crypto!.olmDevice.deviceCurve25519Key!,
|
||||
ciphertext: {},
|
||||
[ToDeviceMessageId]: uuidv4(),
|
||||
};
|
||||
await olmlib.ensureOlmSessionsForDevices(
|
||||
this.baseApis.crypto!.olmDevice,
|
||||
this.baseApis,
|
||||
new Map([[sender, [this.baseApis.getStoredDevice(sender, deviceId)!]]]),
|
||||
);
|
||||
await olmlib.encryptMessageForDevice(
|
||||
encryptedContent.ciphertext,
|
||||
this.baseApis.getUserId()!,
|
||||
this.baseApis.deviceId!,
|
||||
this.baseApis.crypto!.olmDevice,
|
||||
sender,
|
||||
this.baseApis.getStoredDevice(sender, deviceId)!,
|
||||
payload,
|
||||
);
|
||||
const contentMap = new Map([[sender, new Map([[deviceId, encryptedContent]])]]);
|
||||
|
||||
logger.info(`Sending ${content.name} secret for ${deviceId}`);
|
||||
this.baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||
} else {
|
||||
logger.info(`Request denied for ${content.name} secret for ${deviceId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public onSecretReceived(event: MatrixEvent): void {
|
||||
if (event.getSender() !== this.baseApis.getUserId()) {
|
||||
// we shouldn't be receiving secrets from anyone else, so ignore
|
||||
// because someone could be trying to send us bogus data
|
||||
return;
|
||||
}
|
||||
|
||||
if (!olmlib.isOlmEncrypted(event)) {
|
||||
logger.error("secret event not properly encrypted");
|
||||
return;
|
||||
}
|
||||
|
||||
const content = event.getContent();
|
||||
|
||||
const senderKeyUser = this.baseApis.crypto!.deviceList.getUserByIdentityKey(
|
||||
olmlib.OLM_ALGORITHM,
|
||||
event.getSenderKey() || "",
|
||||
);
|
||||
if (senderKeyUser !== event.getSender()) {
|
||||
logger.error("sending device does not belong to the user it claims to be from");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log("got secret share for request", content.request_id);
|
||||
const requestControl = this.requests.get(content.request_id);
|
||||
if (requestControl) {
|
||||
// make sure that the device that sent it is one of the devices that
|
||||
// we requested from
|
||||
const deviceInfo = this.baseApis.crypto!.deviceList.getDeviceByIdentityKey(
|
||||
olmlib.OLM_ALGORITHM,
|
||||
event.getSenderKey()!,
|
||||
);
|
||||
if (!deviceInfo) {
|
||||
logger.log("secret share from unknown device with key", event.getSenderKey());
|
||||
return;
|
||||
}
|
||||
if (!requestControl.devices.includes(deviceInfo.deviceId)) {
|
||||
logger.log("unsolicited secret share from device", deviceInfo.deviceId);
|
||||
return;
|
||||
}
|
||||
// unsure that the sender is trusted. In theory, this check is
|
||||
// unnecessary since we only accept secret shares from devices that
|
||||
// we requested from, but it doesn't hurt.
|
||||
const deviceTrust = this.baseApis.crypto!.checkDeviceInfoTrust(event.getSender()!, deviceInfo);
|
||||
if (!deviceTrust.isVerified()) {
|
||||
logger.log("secret share from unverified device");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log(`Successfully received secret ${requestControl.name} ` + `from ${deviceInfo.deviceId}`);
|
||||
requestControl.deferred.resolve(content.secret);
|
||||
}
|
||||
}
|
||||
}
|
||||
+59
-502
@@ -14,570 +14,127 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { ICryptoCallbacks } from ".";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import { MatrixClient } from "../client";
|
||||
import {
|
||||
SecretStorageKeyDescription,
|
||||
SecretStorageKeyTuple,
|
||||
SecretStorageKeyObject,
|
||||
AddSecretStorageKeyOpts,
|
||||
AccountDataClient,
|
||||
ServerSideSecretStorage,
|
||||
ServerSideSecretStorageImpl,
|
||||
} from "../secret-storage";
|
||||
import { ISecretRequest, SecretSharing } from "./SecretSharing";
|
||||
|
||||
import { logger } from "../logger";
|
||||
import * as olmlib from "./olmlib";
|
||||
import { randomString } from "../randomstring";
|
||||
import { calculateKeyCheck, decryptAES, encryptAES, IEncryptedPayload } from "./aes";
|
||||
import { ICryptoCallbacks, IEncryptedContent } from ".";
|
||||
import { IContent, MatrixEvent } from "../models/event";
|
||||
import { ClientEvent, ClientEventHandlerMap, MatrixClient } from "../client";
|
||||
import { IAddSecretStorageKeyOpts } from "./api";
|
||||
import { TypedEventEmitter } from "../models/typed-event-emitter";
|
||||
import { defer, IDeferred } from "../utils";
|
||||
import { ToDeviceMessageId } from "../@types/event";
|
||||
import { SecretStorageKeyDescription, SecretStorageKeyDescriptionAesV1 } from "../secret-storage";
|
||||
/* re-exports for backwards compatibility */
|
||||
export type {
|
||||
AccountDataClient as IAccountDataClient,
|
||||
SecretStorageKeyTuple,
|
||||
SecretStorageKeyObject,
|
||||
SECRET_STORAGE_ALGORITHM_V1_AES,
|
||||
} from "../secret-storage";
|
||||
|
||||
export const SECRET_STORAGE_ALGORITHM_V1_AES = "m.secret_storage.v1.aes-hmac-sha2";
|
||||
|
||||
// Some of the key functions use a tuple and some use an object...
|
||||
export type SecretStorageKeyTuple = [keyId: string, keyInfo: SecretStorageKeyDescription];
|
||||
export type SecretStorageKeyObject = { keyId: string; keyInfo: SecretStorageKeyDescription };
|
||||
|
||||
export interface ISecretRequest {
|
||||
requestId: string;
|
||||
promise: Promise<string>;
|
||||
cancel: (reason: string) => void;
|
||||
}
|
||||
|
||||
export interface IAccountDataClient extends TypedEventEmitter<ClientEvent.AccountData, ClientEventHandlerMap> {
|
||||
// Subset of MatrixClient (which also uses any for the event content)
|
||||
getAccountDataFromServer: <T extends { [k: string]: any }>(eventType: string) => Promise<T>;
|
||||
getAccountData: (eventType: string) => IContent | null;
|
||||
setAccountData: (eventType: string, content: any) => Promise<{}>;
|
||||
}
|
||||
|
||||
interface ISecretRequestInternal {
|
||||
name: string;
|
||||
devices: string[];
|
||||
deferred: IDeferred<string>;
|
||||
}
|
||||
|
||||
interface IDecryptors {
|
||||
encrypt: (plaintext: string) => Promise<IEncryptedPayload>;
|
||||
decrypt: (ciphertext: IEncryptedPayload) => Promise<string>;
|
||||
}
|
||||
|
||||
interface ISecretInfo {
|
||||
encrypted: {
|
||||
[keyId: string]: IEncryptedPayload;
|
||||
};
|
||||
}
|
||||
export type { ISecretRequest } from "./SecretSharing";
|
||||
|
||||
/**
|
||||
* Implements Secure Secret Storage and Sharing (MSC1946)
|
||||
*
|
||||
* @deprecated This is just a backwards-compatibility hack which will be removed soon.
|
||||
* Use {@link SecretStorage.ServerSideSecretStorageImpl} from `../secret-storage` and/or {@link SecretSharing} from `./SecretSharing`.
|
||||
*/
|
||||
export class SecretStorage<B extends MatrixClient | undefined = MatrixClient> {
|
||||
private requests = new Map<string, ISecretRequestInternal>();
|
||||
export class SecretStorage<B extends MatrixClient | undefined = MatrixClient> implements ServerSideSecretStorage {
|
||||
private readonly storageImpl: ServerSideSecretStorageImpl;
|
||||
private readonly sharingImpl: SecretSharing;
|
||||
|
||||
// In it's pure javascript days, this was relying on some proper Javascript-style
|
||||
// In its pure javascript days, this was relying on some proper Javascript-style
|
||||
// type-abuse where sometimes we'd pass in a fake client object with just the account
|
||||
// data methods implemented, which is all this class needs unless you use the secret
|
||||
// sharing code, so it was fine. As a low-touch TypeScript migration, this now has
|
||||
// sharing code, so it was fine. As a low-touch TypeScript migration, we added
|
||||
// an extra, optional param for a real matrix client, so you can not pass it as long
|
||||
// as you don't request any secrets.
|
||||
// A better solution would probably be to split this class up into secret storage and
|
||||
// secret sharing which are really two separate things, even though they share an MSC.
|
||||
public constructor(
|
||||
private readonly accountDataAdapter: IAccountDataClient,
|
||||
private readonly cryptoCallbacks: ICryptoCallbacks,
|
||||
private readonly baseApis: B,
|
||||
) {}
|
||||
//
|
||||
// Nowadays, the whole class is scheduled for destruction, once we get rid of the legacy
|
||||
// Crypto impl that exposes it.
|
||||
public constructor(accountDataAdapter: AccountDataClient, cryptoCallbacks: ICryptoCallbacks, baseApis: B) {
|
||||
this.storageImpl = new ServerSideSecretStorageImpl(accountDataAdapter, cryptoCallbacks);
|
||||
this.sharingImpl = new SecretSharing(baseApis as MatrixClient, cryptoCallbacks);
|
||||
}
|
||||
|
||||
public async getDefaultKeyId(): Promise<string | null> {
|
||||
const defaultKey = await this.accountDataAdapter.getAccountDataFromServer<{ key: string }>(
|
||||
"m.secret_storage.default_key",
|
||||
);
|
||||
if (!defaultKey) return null;
|
||||
return defaultKey.key;
|
||||
public getDefaultKeyId(): Promise<string | null> {
|
||||
return this.storageImpl.getDefaultKeyId();
|
||||
}
|
||||
|
||||
public setDefaultKeyId(keyId: string): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const listener = (ev: MatrixEvent): void => {
|
||||
if (ev.getType() === "m.secret_storage.default_key" && ev.getContent().key === keyId) {
|
||||
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
this.accountDataAdapter.on(ClientEvent.AccountData, listener);
|
||||
|
||||
this.accountDataAdapter.setAccountData("m.secret_storage.default_key", { key: keyId }).catch((e) => {
|
||||
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
return this.storageImpl.setDefaultKeyId(keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a key for encrypting secrets.
|
||||
*
|
||||
* @param algorithm - the algorithm used by the key.
|
||||
* @param opts - the options for the algorithm. The properties used
|
||||
* depend on the algorithm given.
|
||||
* @param keyId - the ID of the key. If not given, a random
|
||||
* ID will be generated.
|
||||
*
|
||||
* @returns An object with:
|
||||
* keyId: the ID of the key
|
||||
* keyInfo: details about the key (iv, mac, passphrase)
|
||||
*/
|
||||
public async addKey(
|
||||
public addKey(
|
||||
algorithm: string,
|
||||
opts: IAddSecretStorageKeyOpts = {},
|
||||
opts: AddSecretStorageKeyOpts = {},
|
||||
keyId?: string,
|
||||
): Promise<SecretStorageKeyObject> {
|
||||
if (algorithm !== SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
throw new Error(`Unknown key algorithm ${algorithm}`);
|
||||
}
|
||||
|
||||
const keyInfo = { algorithm } as SecretStorageKeyDescriptionAesV1;
|
||||
|
||||
if (opts.name) {
|
||||
keyInfo.name = opts.name;
|
||||
}
|
||||
|
||||
if (opts.passphrase) {
|
||||
keyInfo.passphrase = opts.passphrase;
|
||||
}
|
||||
if (opts.key) {
|
||||
const { iv, mac } = await calculateKeyCheck(opts.key);
|
||||
keyInfo.iv = iv;
|
||||
keyInfo.mac = mac;
|
||||
}
|
||||
|
||||
if (!keyId) {
|
||||
do {
|
||||
keyId = randomString(32);
|
||||
} while (
|
||||
await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
`m.secret_storage.key.${keyId}`,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
await this.accountDataAdapter.setAccountData(`m.secret_storage.key.${keyId}`, keyInfo);
|
||||
|
||||
return {
|
||||
keyId,
|
||||
keyInfo,
|
||||
};
|
||||
return this.storageImpl.addKey(algorithm, opts, keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the key information for a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns If the key was found, the return value is an array of
|
||||
* the form [keyId, keyInfo]. Otherwise, null is returned.
|
||||
* XXX: why is this an array when addKey returns an object?
|
||||
*/
|
||||
public async getKey(keyId?: string | null): Promise<SecretStorageKeyTuple | null> {
|
||||
if (!keyId) {
|
||||
keyId = await this.getDefaultKeyId();
|
||||
}
|
||||
if (!keyId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
return keyInfo ? [keyId, keyInfo] : null;
|
||||
public getKey(keyId?: string | null): Promise<SecretStorageKeyTuple | null> {
|
||||
return this.storageImpl.getKey(keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether we have a key with a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns Whether we have the key.
|
||||
*/
|
||||
public async hasKey(keyId?: string): Promise<boolean> {
|
||||
return Boolean(await this.getKey(keyId));
|
||||
public hasKey(keyId?: string): Promise<boolean> {
|
||||
return this.storageImpl.hasKey(keyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a key matches what we expect based on the key info
|
||||
*
|
||||
* @param key - the key to check
|
||||
* @param info - the key info
|
||||
*
|
||||
* @returns whether or not the key matches
|
||||
*/
|
||||
public async checkKey(key: Uint8Array, info: SecretStorageKeyDescription): Promise<boolean> {
|
||||
if (info.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (info.mac) {
|
||||
const { mac } = await calculateKeyCheck(key, info.iv);
|
||||
return info.mac.replace(/=+$/g, "") === mac.replace(/=+$/g, "");
|
||||
} else {
|
||||
// if we have no information, we have to assume the key is right
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
throw new Error("Unknown algorithm");
|
||||
}
|
||||
public checkKey(key: Uint8Array, info: SecretStorageKeyDescription): Promise<boolean> {
|
||||
return this.storageImpl.checkKey(key, info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an encrypted secret on the server
|
||||
*
|
||||
* @param name - The name of the secret
|
||||
* @param secret - The secret contents.
|
||||
* @param keys - The IDs of the keys to use to encrypt the secret
|
||||
* or null/undefined to use the default key.
|
||||
*/
|
||||
public async store(name: string, secret: string, keys?: string[] | null): Promise<void> {
|
||||
const encrypted: Record<string, IEncryptedPayload> = {};
|
||||
|
||||
if (!keys) {
|
||||
const defaultKeyId = await this.getDefaultKeyId();
|
||||
if (!defaultKeyId) {
|
||||
throw new Error("No keys specified and no default key present");
|
||||
}
|
||||
keys = [defaultKeyId];
|
||||
}
|
||||
|
||||
if (keys.length === 0) {
|
||||
throw new Error("Zero keys given to encrypt with!");
|
||||
}
|
||||
|
||||
for (const keyId of keys) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
if (!keyInfo) {
|
||||
throw new Error("Unknown key: " + keyId);
|
||||
}
|
||||
|
||||
// encrypt secret, based on the algorithm
|
||||
if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
const keys = { [keyId]: keyInfo };
|
||||
const [, encryption] = await this.getSecretStorageKey(keys, name);
|
||||
encrypted[keyId] = await encryption.encrypt(secret);
|
||||
} else {
|
||||
logger.warn("unknown algorithm for secret storage key " + keyId + ": " + keyInfo.algorithm);
|
||||
// do nothing if we don't understand the encryption algorithm
|
||||
}
|
||||
}
|
||||
|
||||
// save encrypted secret
|
||||
await this.accountDataAdapter.setAccountData(name, { encrypted });
|
||||
public store(name: string, secret: string, keys?: string[] | null): Promise<void> {
|
||||
return this.storageImpl.store(name, secret, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a secret from storage.
|
||||
*
|
||||
* @param name - the name of the secret
|
||||
*
|
||||
* @returns the contents of the secret
|
||||
*/
|
||||
public async get(name: string): Promise<string | undefined> {
|
||||
const secretInfo = await this.accountDataAdapter.getAccountDataFromServer<ISecretInfo>(name);
|
||||
if (!secretInfo) {
|
||||
return;
|
||||
}
|
||||
if (!secretInfo.encrypted) {
|
||||
throw new Error("Content is not encrypted!");
|
||||
}
|
||||
|
||||
// get possible keys to decrypt
|
||||
const keys: Record<string, SecretStorageKeyDescription> = {};
|
||||
for (const keyId of Object.keys(secretInfo.encrypted)) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
// only use keys we understand the encryption algorithm of
|
||||
if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
|
||||
keys[keyId] = keyInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(keys).length === 0) {
|
||||
throw new Error(
|
||||
`Could not decrypt ${name} because none of ` +
|
||||
`the keys it is encrypted with are for a supported algorithm`,
|
||||
);
|
||||
}
|
||||
|
||||
// fetch private key from app
|
||||
const [keyId, decryption] = await this.getSecretStorageKey(keys, name);
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
|
||||
return decryption.decrypt(encInfo);
|
||||
public get(name: string): Promise<string | undefined> {
|
||||
return this.storageImpl.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a secret is stored on the server.
|
||||
*
|
||||
* @param name - the name of the secret
|
||||
*
|
||||
* @returns map of key name to key info the secret is encrypted
|
||||
* with, or null if it is not present or not encrypted with a trusted
|
||||
* key
|
||||
*/
|
||||
public async isStored(name: string): Promise<Record<string, SecretStorageKeyDescription> | null> {
|
||||
// check if secret exists
|
||||
const secretInfo = await this.accountDataAdapter.getAccountDataFromServer<ISecretInfo>(name);
|
||||
if (!secretInfo?.encrypted) return null;
|
||||
|
||||
const ret: Record<string, SecretStorageKeyDescription> = {};
|
||||
|
||||
// filter secret encryption keys with supported algorithm
|
||||
for (const keyId of Object.keys(secretInfo.encrypted)) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
if (!keyInfo) continue;
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
|
||||
// only use keys we understand the encryption algorithm of
|
||||
if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
|
||||
ret[keyId] = keyInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.keys(ret).length ? ret : null;
|
||||
return this.storageImpl.isStored(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Request a secret from another device
|
||||
*
|
||||
* @param name - the name of the secret to request
|
||||
* @param devices - the devices to request the secret from
|
||||
*/
|
||||
public request(this: SecretStorage<MatrixClient>, name: string, devices: string[]): ISecretRequest {
|
||||
const requestId = this.baseApis.makeTxnId();
|
||||
|
||||
const deferred = defer<string>();
|
||||
this.requests.set(requestId, { name, devices, deferred });
|
||||
|
||||
const cancel = (reason: string): void => {
|
||||
// send cancellation event
|
||||
const cancelData = {
|
||||
action: "request_cancellation",
|
||||
requesting_device_id: this.baseApis.deviceId,
|
||||
request_id: requestId,
|
||||
};
|
||||
const toDevice: Map<string, typeof cancelData> = new Map();
|
||||
for (const device of devices) {
|
||||
toDevice.set(device, cancelData);
|
||||
}
|
||||
this.baseApis.sendToDevice("m.secret.request", new Map([[this.baseApis.getUserId()!, toDevice]]));
|
||||
|
||||
// and reject the promise so that anyone waiting on it will be
|
||||
// notified
|
||||
deferred.reject(new Error(reason || "Cancelled"));
|
||||
};
|
||||
|
||||
// send request to devices
|
||||
const requestData = {
|
||||
name,
|
||||
action: "request",
|
||||
requesting_device_id: this.baseApis.deviceId,
|
||||
request_id: requestId,
|
||||
[ToDeviceMessageId]: uuidv4(),
|
||||
};
|
||||
const toDevice: Map<string, typeof requestData> = new Map();
|
||||
for (const device of devices) {
|
||||
toDevice.set(device, requestData);
|
||||
}
|
||||
logger.info(`Request secret ${name} from ${devices}, id ${requestId}`);
|
||||
this.baseApis.sendToDevice("m.secret.request", new Map([[this.baseApis.getUserId()!, toDevice]]));
|
||||
|
||||
return {
|
||||
requestId,
|
||||
promise: deferred.promise,
|
||||
cancel,
|
||||
};
|
||||
public request(name: string, devices: string[]): ISecretRequest {
|
||||
return this.sharingImpl.request(name, devices);
|
||||
}
|
||||
|
||||
public async onRequestReceived(this: SecretStorage<MatrixClient>, event: MatrixEvent): Promise<void> {
|
||||
const sender = event.getSender();
|
||||
const content = event.getContent();
|
||||
if (
|
||||
sender !== this.baseApis.getUserId() ||
|
||||
!(content.name && content.action && content.requesting_device_id && content.request_id)
|
||||
) {
|
||||
// ignore requests from anyone else, for now
|
||||
return;
|
||||
}
|
||||
const deviceId = content.requesting_device_id;
|
||||
// check if it's a cancel
|
||||
if (content.action === "request_cancellation") {
|
||||
/*
|
||||
Looks like we intended to emit events when we got cancelations, but
|
||||
we never put anything in the _incomingRequests object, and the request
|
||||
itself doesn't use events anyway so if we were to wire up cancellations,
|
||||
they probably ought to use the same callback interface. I'm leaving them
|
||||
disabled for now while converting this file to typescript.
|
||||
if (this._incomingRequests[deviceId]
|
||||
&& this._incomingRequests[deviceId][content.request_id]) {
|
||||
logger.info(
|
||||
"received request cancellation for secret (" + sender +
|
||||
", " + deviceId + ", " + content.request_id + ")",
|
||||
);
|
||||
this.baseApis.emit("crypto.secrets.requestCancelled", {
|
||||
user_id: sender,
|
||||
device_id: deviceId,
|
||||
request_id: content.request_id,
|
||||
});
|
||||
}
|
||||
*/
|
||||
} else if (content.action === "request") {
|
||||
if (deviceId === this.baseApis.deviceId) {
|
||||
// no point in trying to send ourself the secret
|
||||
return;
|
||||
}
|
||||
|
||||
// check if we have the secret
|
||||
logger.info("received request for secret (" + sender + ", " + deviceId + ", " + content.request_id + ")");
|
||||
if (!this.cryptoCallbacks.onSecretRequested) {
|
||||
return;
|
||||
}
|
||||
const secret = await this.cryptoCallbacks.onSecretRequested(
|
||||
sender,
|
||||
deviceId,
|
||||
content.request_id,
|
||||
content.name,
|
||||
this.baseApis.checkDeviceTrust(sender, deviceId),
|
||||
);
|
||||
if (secret) {
|
||||
logger.info(`Preparing ${content.name} secret for ${deviceId}`);
|
||||
const payload = {
|
||||
type: "m.secret.send",
|
||||
content: {
|
||||
request_id: content.request_id,
|
||||
secret: secret,
|
||||
},
|
||||
};
|
||||
const encryptedContent: IEncryptedContent = {
|
||||
algorithm: olmlib.OLM_ALGORITHM,
|
||||
sender_key: this.baseApis.crypto!.olmDevice.deviceCurve25519Key!,
|
||||
ciphertext: {},
|
||||
[ToDeviceMessageId]: uuidv4(),
|
||||
};
|
||||
await olmlib.ensureOlmSessionsForDevices(
|
||||
this.baseApis.crypto!.olmDevice,
|
||||
this.baseApis,
|
||||
new Map([[sender, [this.baseApis.getStoredDevice(sender, deviceId)!]]]),
|
||||
);
|
||||
await olmlib.encryptMessageForDevice(
|
||||
encryptedContent.ciphertext,
|
||||
this.baseApis.getUserId()!,
|
||||
this.baseApis.deviceId!,
|
||||
this.baseApis.crypto!.olmDevice,
|
||||
sender,
|
||||
this.baseApis.getStoredDevice(sender, deviceId)!,
|
||||
payload,
|
||||
);
|
||||
const contentMap = new Map([[sender, new Map([[deviceId, encryptedContent]])]]);
|
||||
|
||||
logger.info(`Sending ${content.name} secret for ${deviceId}`);
|
||||
this.baseApis.sendToDevice("m.room.encrypted", contentMap);
|
||||
} else {
|
||||
logger.info(`Request denied for ${content.name} secret for ${deviceId}`);
|
||||
}
|
||||
}
|
||||
public onRequestReceived(event: MatrixEvent): Promise<void> {
|
||||
return this.sharingImpl.onRequestReceived(event);
|
||||
}
|
||||
|
||||
public onSecretReceived(this: SecretStorage<MatrixClient>, event: MatrixEvent): void {
|
||||
if (event.getSender() !== this.baseApis.getUserId()) {
|
||||
// we shouldn't be receiving secrets from anyone else, so ignore
|
||||
// because someone could be trying to send us bogus data
|
||||
return;
|
||||
}
|
||||
|
||||
if (!olmlib.isOlmEncrypted(event)) {
|
||||
logger.error("secret event not properly encrypted");
|
||||
return;
|
||||
}
|
||||
|
||||
const content = event.getContent();
|
||||
|
||||
const senderKeyUser = this.baseApis.crypto!.deviceList.getUserByIdentityKey(
|
||||
olmlib.OLM_ALGORITHM,
|
||||
event.getSenderKey() || "",
|
||||
);
|
||||
if (senderKeyUser !== event.getSender()) {
|
||||
logger.error("sending device does not belong to the user it claims to be from");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log("got secret share for request", content.request_id);
|
||||
const requestControl = this.requests.get(content.request_id);
|
||||
if (requestControl) {
|
||||
// make sure that the device that sent it is one of the devices that
|
||||
// we requested from
|
||||
const deviceInfo = this.baseApis.crypto!.deviceList.getDeviceByIdentityKey(
|
||||
olmlib.OLM_ALGORITHM,
|
||||
event.getSenderKey()!,
|
||||
);
|
||||
if (!deviceInfo) {
|
||||
logger.log("secret share from unknown device with key", event.getSenderKey());
|
||||
return;
|
||||
}
|
||||
if (!requestControl.devices.includes(deviceInfo.deviceId)) {
|
||||
logger.log("unsolicited secret share from device", deviceInfo.deviceId);
|
||||
return;
|
||||
}
|
||||
// unsure that the sender is trusted. In theory, this check is
|
||||
// unnecessary since we only accept secret shares from devices that
|
||||
// we requested from, but it doesn't hurt.
|
||||
const deviceTrust = this.baseApis.crypto!.checkDeviceInfoTrust(event.getSender()!, deviceInfo);
|
||||
if (!deviceTrust.isVerified()) {
|
||||
logger.log("secret share from unverified device");
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log(`Successfully received secret ${requestControl.name} ` + `from ${deviceInfo.deviceId}`);
|
||||
requestControl.deferred.resolve(content.secret);
|
||||
}
|
||||
}
|
||||
|
||||
private async getSecretStorageKey(
|
||||
keys: Record<string, SecretStorageKeyDescription>,
|
||||
name: string,
|
||||
): Promise<[string, IDecryptors]> {
|
||||
if (!this.cryptoCallbacks.getSecretStorageKey) {
|
||||
throw new Error("No getSecretStorageKey callback supplied");
|
||||
}
|
||||
|
||||
const returned = await this.cryptoCallbacks.getSecretStorageKey({ keys }, name);
|
||||
|
||||
if (!returned) {
|
||||
throw new Error("getSecretStorageKey callback returned falsey");
|
||||
}
|
||||
if (returned.length < 2) {
|
||||
throw new Error("getSecretStorageKey callback returned invalid data");
|
||||
}
|
||||
|
||||
const [keyId, privateKey] = returned;
|
||||
if (!keys[keyId]) {
|
||||
throw new Error("App returned unknown key from getSecretStorageKey!");
|
||||
}
|
||||
|
||||
if (keys[keyId].algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
const decryption = {
|
||||
encrypt: function (secret: string): Promise<IEncryptedPayload> {
|
||||
return encryptAES(secret, privateKey, name);
|
||||
},
|
||||
decrypt: function (encInfo: IEncryptedPayload): Promise<string> {
|
||||
return decryptAES(encInfo, privateKey, name);
|
||||
},
|
||||
};
|
||||
return [keyId, decryption];
|
||||
} else {
|
||||
throw new Error("Unknown key type: " + keys[keyId].algorithm);
|
||||
}
|
||||
public onSecretReceived(event: MatrixEvent): void {
|
||||
this.sharingImpl.onSecretReceived(event);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-10
@@ -16,10 +16,11 @@ limitations under the License.
|
||||
|
||||
import { DeviceInfo } from "./deviceinfo";
|
||||
import { IKeyBackupInfo } from "./keybackup";
|
||||
import { PassphraseInfo } from "../secret-storage";
|
||||
import type { AddSecretStorageKeyOpts } from "../secret-storage";
|
||||
|
||||
/* re-exports for backwards compatibility. */
|
||||
export {
|
||||
export type {
|
||||
AddSecretStorageKeyOpts as IAddSecretStorageKeyOpts,
|
||||
PassphraseInfo as IPassphraseInfo,
|
||||
SecretStorageKeyDescription as ISecretStorageKeyInfo,
|
||||
} from "../secret-storage";
|
||||
@@ -65,7 +66,7 @@ export interface IEncryptedEventInfo {
|
||||
}
|
||||
|
||||
export interface IRecoveryKey {
|
||||
keyInfo?: IAddSecretStorageKeyOpts;
|
||||
keyInfo?: AddSecretStorageKeyOpts;
|
||||
privateKey: Uint8Array;
|
||||
encodedPrivateKey?: string;
|
||||
}
|
||||
@@ -105,13 +106,6 @@ export interface ICreateSecretStorageOpts {
|
||||
getKeyBackupPassphrase?: () => Promise<Uint8Array>;
|
||||
}
|
||||
|
||||
export interface IAddSecretStorageKeyOpts {
|
||||
pubkey?: string;
|
||||
passphrase?: PassphraseInfo;
|
||||
name?: string;
|
||||
key?: Uint8Array;
|
||||
}
|
||||
|
||||
export interface IImportOpts {
|
||||
stage: string; // TODO: Enum
|
||||
successes: number;
|
||||
|
||||
+90
-82
@@ -34,21 +34,8 @@ import type { DecryptionAlgorithm, EncryptionAlgorithm } from "./algorithms";
|
||||
import * as algorithms from "./algorithms";
|
||||
import { createCryptoStoreCacheCallbacks, CrossSigningInfo, DeviceTrustLevel, UserTrustLevel } from "./CrossSigning";
|
||||
import { EncryptionSetupBuilder } from "./EncryptionSetup";
|
||||
import {
|
||||
IAccountDataClient,
|
||||
ISecretRequest,
|
||||
SECRET_STORAGE_ALGORITHM_V1_AES,
|
||||
SecretStorage,
|
||||
SecretStorageKeyObject,
|
||||
SecretStorageKeyTuple,
|
||||
} from "./SecretStorage";
|
||||
import {
|
||||
IAddSecretStorageKeyOpts,
|
||||
ICreateSecretStorageOpts,
|
||||
IEncryptedEventInfo,
|
||||
IImportRoomKeysOpts,
|
||||
IRecoveryKey,
|
||||
} from "./api";
|
||||
import { SecretStorage as LegacySecretStorage } from "./SecretStorage";
|
||||
import { ICreateSecretStorageOpts, IEncryptedEventInfo, IImportRoomKeysOpts, IRecoveryKey } from "./api";
|
||||
import { OutgoingRoomKeyRequestManager } from "./OutgoingRoomKeyRequestManager";
|
||||
import { IndexedDBCryptoStore } from "./store/indexeddb-crypto-store";
|
||||
import { VerificationBase } from "./verification/Base";
|
||||
@@ -84,13 +71,23 @@ import { CryptoStore } from "./store/base";
|
||||
import { IVerificationChannel } from "./verification/request/Channel";
|
||||
import { TypedEventEmitter } from "../models/typed-event-emitter";
|
||||
import { IContent } from "../models/event";
|
||||
import { ISyncResponse, IToDeviceEvent } from "../sync-accumulator";
|
||||
import { IDeviceLists, ISyncResponse, IToDeviceEvent } from "../sync-accumulator";
|
||||
import { ISignatures } from "../@types/signed";
|
||||
import { IMessage } from "./algorithms/olm";
|
||||
import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend";
|
||||
import { RoomState, RoomStateEvent } from "../models/room-state";
|
||||
import { MapWithDefault, recursiveMapToObject } from "../utils";
|
||||
import { SecretStorageKeyDescription } from "../secret-storage";
|
||||
import {
|
||||
AccountDataClient,
|
||||
AddSecretStorageKeyOpts,
|
||||
SecretStorageKeyDescription,
|
||||
SecretStorageKeyObject,
|
||||
SecretStorageKeyTuple,
|
||||
SECRET_STORAGE_ALGORITHM_V1_AES,
|
||||
SecretStorageCallbacks,
|
||||
ServerSideSecretStorageImpl,
|
||||
} from "../secret-storage";
|
||||
import { ISecretRequest } from "./SecretSharing";
|
||||
|
||||
const DeviceVerification = DeviceInfo.DeviceVerification;
|
||||
|
||||
@@ -137,14 +134,10 @@ export interface IBootstrapCrossSigningOpts {
|
||||
authUploadDeviceSigningKeys?(makeRequest: (authData: any) => Promise<{}>): Promise<void>;
|
||||
}
|
||||
|
||||
export interface ICryptoCallbacks {
|
||||
export interface ICryptoCallbacks extends SecretStorageCallbacks {
|
||||
getCrossSigningKey?: (keyType: string, pubKey: string) => Promise<Uint8Array | null>;
|
||||
saveCrossSigningKeys?: (keys: Record<string, Uint8Array>) => void;
|
||||
shouldUpgradeDeviceVerifications?: (users: Record<string, any>) => Promise<string[]>;
|
||||
getSecretStorageKey?: (
|
||||
keys: { keys: Record<string, SecretStorageKeyDescription> },
|
||||
name: string,
|
||||
) => Promise<[string, Uint8Array] | null>;
|
||||
cacheSecretStorageKey?: (keyId: string, keyInfo: SecretStorageKeyDescription, key: Uint8Array) => void;
|
||||
onSecretRequested?: (
|
||||
userId: string,
|
||||
@@ -252,8 +245,8 @@ export enum CryptoEvent {
|
||||
export type CryptoEventHandlerMap = {
|
||||
/**
|
||||
* Fires when a device is marked as verified/unverified/blocked/unblocked by
|
||||
* {@link MatrixClient#setDeviceVerified|MatrixClient.setDeviceVerified} or
|
||||
* {@link MatrixClient#setDeviceBlocked|MatrixClient.setDeviceBlocked}.
|
||||
* {@link MatrixClient#setDeviceVerified | MatrixClient.setDeviceVerified} or
|
||||
* {@link MatrixClient#setDeviceBlocked | MatrixClient.setDeviceBlocked}.
|
||||
*
|
||||
* @param userId - the owner of the verified device
|
||||
* @param deviceId - the id of the verified device
|
||||
@@ -356,7 +349,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
public readonly olmDevice: OlmDevice;
|
||||
public readonly deviceList: DeviceList;
|
||||
public readonly dehydrationManager: DehydrationManager;
|
||||
public readonly secretStorage: SecretStorage;
|
||||
public readonly secretStorage: LegacySecretStorage;
|
||||
|
||||
private readonly reEmitter: TypedReEmitter<CryptoEvent, CryptoEventHandlerMap>;
|
||||
private readonly verificationMethods: Map<VerificationMethod, typeof VerificationBase>;
|
||||
@@ -483,15 +476,15 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
}
|
||||
|
||||
// try to get key from secret storage
|
||||
const storedKey = await this.getSecret("m.megolm_backup.v1");
|
||||
const storedKey = await this.secretStorage.get("m.megolm_backup.v1");
|
||||
|
||||
if (storedKey) {
|
||||
// ensure that the key is in the right format. If not, fix the key and
|
||||
// store the fixed version
|
||||
const fixedKey = fixBackupKey(storedKey);
|
||||
if (fixedKey) {
|
||||
const keys = await this.getSecretStorageKey();
|
||||
await this.storeSecret("m.megolm_backup.v1", fixedKey, [keys![0]]);
|
||||
const keys = await this.secretStorage.getKey();
|
||||
await this.secretStorage.store("m.megolm_backup.v1", fixedKey, [keys![0]]);
|
||||
}
|
||||
|
||||
return olmlib.decodeBase64(fixedKey || storedKey);
|
||||
@@ -529,7 +522,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
|
||||
this.crossSigningInfo = new CrossSigningInfo(userId, cryptoCallbacks, cacheCallbacks);
|
||||
// Yes, we pass the client twice here: see SecretStorage
|
||||
this.secretStorage = new SecretStorage(baseApis as IAccountDataClient, cryptoCallbacks, baseApis);
|
||||
this.secretStorage = new LegacySecretStorage(baseApis as AccountDataClient, cryptoCallbacks, baseApis);
|
||||
this.dehydrationManager = new DehydrationManager(this);
|
||||
|
||||
// Assuming no app-supplied callback, default to getting from SSSS.
|
||||
@@ -834,10 +827,9 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
// done as part of setting up secret storage later.
|
||||
const crossSigningPrivateKeys = builder.crossSigningCallbacks.privateKeys;
|
||||
if (crossSigningPrivateKeys.size && !this.baseApis.cryptoCallbacks.saveCrossSigningKeys) {
|
||||
const secretStorage = new SecretStorage(
|
||||
const secretStorage = new ServerSideSecretStorageImpl(
|
||||
builder.accountDataClientAdapter,
|
||||
builder.ssssCryptoCallbacks,
|
||||
undefined,
|
||||
);
|
||||
if (await secretStorage.hasKey()) {
|
||||
logger.log("Storing new cross-signing private keys in secret storage");
|
||||
@@ -900,17 +892,16 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
logger.log("Bootstrapping Secure Secret Storage");
|
||||
const delegateCryptoCallbacks = this.baseApis.cryptoCallbacks;
|
||||
const builder = new EncryptionSetupBuilder(this.baseApis.store.accountData, delegateCryptoCallbacks);
|
||||
const secretStorage = new SecretStorage(
|
||||
const secretStorage = new ServerSideSecretStorageImpl(
|
||||
builder.accountDataClientAdapter,
|
||||
builder.ssssCryptoCallbacks,
|
||||
undefined,
|
||||
);
|
||||
|
||||
// the ID of the new SSSS key, if we create one
|
||||
let newKeyId: string | null = null;
|
||||
|
||||
// create a new SSSS key and set it as default
|
||||
const createSSSS = async (opts: IAddSecretStorageKeyOpts, privateKey?: Uint8Array): Promise<string> => {
|
||||
const createSSSS = async (opts: AddSecretStorageKeyOpts, privateKey?: Uint8Array): Promise<string> => {
|
||||
if (privateKey) {
|
||||
opts.key = privateKey;
|
||||
}
|
||||
@@ -959,7 +950,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
}
|
||||
};
|
||||
|
||||
const oldSSSSKey = await this.getSecretStorageKey();
|
||||
const oldSSSSKey = await this.secretStorage.getKey();
|
||||
const [oldKeyId, oldKeyInfo] = oldSSSSKey || [null, null];
|
||||
const storageExists =
|
||||
!setupNewSecretStorage && oldKeyInfo && oldKeyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES;
|
||||
@@ -984,7 +975,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
// secrets using it, in theory. We could move them to the new key but a)
|
||||
// that would mean we'd need to prompt for the old passphrase, and b)
|
||||
// it's not clear that would be the right thing to do anyway.
|
||||
const { keyInfo = {} as IAddSecretStorageKeyOpts, privateKey } = await createSecretStorageKey();
|
||||
const { keyInfo = {} as AddSecretStorageKeyOpts, privateKey } = await createSecretStorageKey();
|
||||
newKeyId = await createSSSS(keyInfo, privateKey);
|
||||
} else if (!storageExists && keyBackupInfo) {
|
||||
// we have an existing backup, but no SSSS
|
||||
@@ -995,7 +986,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
const backupKey = (await this.getSessionBackupPrivateKey()) || (await getKeyBackupPassphrase?.());
|
||||
|
||||
// create a new SSSS key and use the backup key as the new SSSS key
|
||||
const opts = {} as IAddSecretStorageKeyOpts;
|
||||
const opts = {} as AddSecretStorageKeyOpts;
|
||||
|
||||
if (keyBackupInfo.auth_data.private_key_salt && keyBackupInfo.auth_data.private_key_iterations) {
|
||||
// FIXME: ???
|
||||
@@ -1109,30 +1100,48 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
logger.log("Secure Secret Storage ready");
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#addKey}.
|
||||
*/
|
||||
public addSecretStorageKey(
|
||||
algorithm: string,
|
||||
opts: IAddSecretStorageKeyOpts,
|
||||
opts: AddSecretStorageKeyOpts,
|
||||
keyID?: string,
|
||||
): Promise<SecretStorageKeyObject> {
|
||||
return this.secretStorage.addKey(algorithm, opts, keyID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#hasKey}.
|
||||
*/
|
||||
public hasSecretStorageKey(keyID?: string): Promise<boolean> {
|
||||
return this.secretStorage.hasKey(keyID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#getKey}.
|
||||
*/
|
||||
public getSecretStorageKey(keyID?: string): Promise<SecretStorageKeyTuple | null> {
|
||||
return this.secretStorage.getKey(keyID);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#store}.
|
||||
*/
|
||||
public storeSecret(name: string, secret: string, keys?: string[]): Promise<void> {
|
||||
return this.secretStorage.store(name, secret, keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#get}.
|
||||
*/
|
||||
public getSecret(name: string): Promise<string | undefined> {
|
||||
return this.secretStorage.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#isStored}.
|
||||
*/
|
||||
public isSecretStored(name: string): Promise<Record<string, SecretStorageKeyDescription> | null> {
|
||||
return this.secretStorage.isStored(name);
|
||||
}
|
||||
@@ -1144,14 +1153,23 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
return this.secretStorage.request(name, devices);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#getDefaultKeyId}.
|
||||
*/
|
||||
public getDefaultSecretStorageKeyId(): Promise<string | null> {
|
||||
return this.secretStorage.getDefaultKeyId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#setDefaultKeyId}.
|
||||
*/
|
||||
public setDefaultSecretStorageKeyId(k: string): Promise<void> {
|
||||
return this.secretStorage.setDefaultKeyId(k);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link MatrixClient#secretStorage} and {@link SecretStorage.ServerSideSecretStorage#checkKey}.
|
||||
*/
|
||||
public checkSecretStorageKey(key: Uint8Array, info: SecretStorageKeyDescription): Promise<boolean> {
|
||||
return this.secretStorage.checkKey(key, info);
|
||||
}
|
||||
@@ -1787,8 +1805,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
*
|
||||
* @param value - whether to blacklist all unverified devices by default
|
||||
*
|
||||
* @deprecated For external code, use {@link MatrixClient#setGlobalBlacklistUnverifiedDevices}. For
|
||||
* internal code, set {@link MatrixClient#globalBlacklistUnverifiedDevices} directly.
|
||||
* @deprecated Set {@link CryptoApi#globalBlacklistUnverifiedDevices | CryptoApi.globalBlacklistUnverifiedDevices} directly.
|
||||
*/
|
||||
public setGlobalBlacklistUnverifiedDevices(value: boolean): void {
|
||||
this.globalBlacklistUnverifiedDevices = value;
|
||||
@@ -1797,8 +1814,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
/**
|
||||
* @returns whether to blacklist all unverified devices by default
|
||||
*
|
||||
* @deprecated For external code, use {@link MatrixClient#getGlobalBlacklistUnverifiedDevices}. For
|
||||
* internal code, reference {@link MatrixClient#globalBlacklistUnverifiedDevices} directly.
|
||||
* @deprecated Reference {@link CryptoApi#globalBlacklistUnverifiedDevices | CryptoApi.globalBlacklistUnverifiedDevices} directly.
|
||||
*/
|
||||
public getGlobalBlacklistUnverifiedDevices(): boolean {
|
||||
return this.globalBlacklistUnverifiedDevices;
|
||||
@@ -1823,24 +1839,6 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores the current one_time_key count which will be handled later (in a call of
|
||||
* onSyncCompleted). The count is e.g. coming from a /sync response.
|
||||
*
|
||||
* @param currentCount - The current count of one_time_keys to be stored
|
||||
*/
|
||||
public updateOneTimeKeyCount(currentCount: number): void {
|
||||
if (isFinite(currentCount)) {
|
||||
this.oneTimeKeyCount = currentCount;
|
||||
} else {
|
||||
throw new TypeError("Parameter for updateOneTimeKeyCount has to be a number");
|
||||
}
|
||||
}
|
||||
|
||||
public setNeedsNewFallback(needsNewFallback: boolean): void {
|
||||
this.needsNewFallback = needsNewFallback;
|
||||
}
|
||||
|
||||
public getNeedsNewFallback(): boolean {
|
||||
return !!this.needsNewFallback;
|
||||
}
|
||||
@@ -1976,7 +1974,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
fallbackJson["signed_curve25519:" + keyId] = k;
|
||||
promises.push(this.signObject(k));
|
||||
}
|
||||
this.setNeedsNewFallback(false);
|
||||
this.needsNewFallback = false;
|
||||
}
|
||||
|
||||
const oneTimeKeys = await this.olmDevice.getOneTimeKeys();
|
||||
@@ -2693,7 +2691,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
*
|
||||
* @returns resolves once the sessions are complete, to
|
||||
* an Object mapping from userId to deviceId to
|
||||
* {@link OlmSessionResult}
|
||||
* `IOlmSessionResult`
|
||||
*/
|
||||
public ensureOlmSessionsForUsers(
|
||||
users: string[],
|
||||
@@ -2917,21 +2915,12 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the notification from /sync or /keys/changes that device lists have
|
||||
* Handle the notification from /sync that device lists have
|
||||
* been changed.
|
||||
*
|
||||
* @param syncData - Object containing sync tokens associated with this sync
|
||||
* @param syncDeviceLists - device_lists field from /sync, or response from
|
||||
* /keys/changes
|
||||
* @param deviceLists - device_lists field from /sync
|
||||
*/
|
||||
public async handleDeviceListChanges(
|
||||
syncData: ISyncStateData,
|
||||
syncDeviceLists: Required<ISyncResponse>["device_lists"],
|
||||
): Promise<void> {
|
||||
// Initial syncs don't have device change lists. We'll either get the complete list
|
||||
// of changes for the interval or will have invalidated everything in willProcessSync
|
||||
if (!syncData.oldSyncToken) return;
|
||||
|
||||
public async processDeviceLists(deviceLists: IDeviceLists): Promise<void> {
|
||||
// Here, we're relying on the fact that we only ever save the sync data after
|
||||
// sucessfully saving the device list data, so we're guaranteed that the device
|
||||
// list store is at least as fresh as the sync token from the sync store, ie.
|
||||
@@ -2940,7 +2929,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
// If we didn't make this assumption, we'd have to use the /keys/changes API
|
||||
// to get key changes between the sync token in the device list and the 'old'
|
||||
// sync token used here to make sure we didn't miss any.
|
||||
await this.evalDeviceListChanges(syncDeviceLists);
|
||||
await this.evalDeviceListChanges(deviceLists);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3221,14 +3210,33 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
});
|
||||
}
|
||||
|
||||
public preprocessOneTimeKeyCounts(oneTimeKeysCounts: Map<string, number>): Promise<void> {
|
||||
const currentCount = oneTimeKeysCounts.get("signed_curve25519") || 0;
|
||||
this.updateOneTimeKeyCount(currentCount);
|
||||
return Promise.resolve();
|
||||
/**
|
||||
* Stores the current one_time_key count which will be handled later (in a call of
|
||||
* onSyncCompleted).
|
||||
*
|
||||
* @param currentCount - The current count of one_time_keys to be stored
|
||||
*/
|
||||
private updateOneTimeKeyCount(currentCount: number): void {
|
||||
if (isFinite(currentCount)) {
|
||||
this.oneTimeKeyCount = currentCount;
|
||||
} else {
|
||||
throw new TypeError("Parameter for updateOneTimeKeyCount has to be a number");
|
||||
}
|
||||
}
|
||||
|
||||
public preprocessUnusedFallbackKeys(unusedFallbackKeys: Set<string>): Promise<void> {
|
||||
this.setNeedsNewFallback(!unusedFallbackKeys.has("signed_curve25519"));
|
||||
public processKeyCounts(oneTimeKeysCounts?: Record<string, number>, unusedFallbackKeys?: string[]): Promise<void> {
|
||||
if (oneTimeKeysCounts !== undefined) {
|
||||
this.updateOneTimeKeyCount(oneTimeKeysCounts["signed_curve25519"] || 0);
|
||||
}
|
||||
|
||||
if (unusedFallbackKeys !== undefined) {
|
||||
// If `unusedFallbackKeys` is defined, that means `device_unused_fallback_key_types`
|
||||
// is present in the sync response, which indicates that the server supports fallback keys.
|
||||
//
|
||||
// If there's no unused signed_curve25519 fallback key, we need a new one.
|
||||
this.needsNewFallback = !unusedFallbackKeys.includes("signed_curve25519");
|
||||
}
|
||||
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
@@ -3784,7 +3792,7 @@ export class Crypto extends TypedEventEmitter<CryptoEvent, CryptoEventHandlerMap
|
||||
*
|
||||
* @param algorithm - crypto algorithm
|
||||
*
|
||||
* @throws {@link DecryptionError} if the algorithm is unknown
|
||||
* @throws `DecryptionError` if the algorithm is unknown
|
||||
*/
|
||||
public getRoomDecryptor(roomId: string | null, algorithm: string): DecryptionAlgorithm {
|
||||
let decryptors: Map<string, DecryptionAlgorithm> | undefined;
|
||||
|
||||
@@ -211,7 +211,7 @@ export interface OutgoingRoomKeyRequest {
|
||||
*/
|
||||
requestBody: IRoomKeyRequestBody;
|
||||
/**
|
||||
* current state of this request (states are defined in {@link OutgoingRoomKeyRequestManager})
|
||||
* current state of this request
|
||||
*/
|
||||
state: RoomKeyRequestState;
|
||||
}
|
||||
|
||||
@@ -338,7 +338,7 @@ export class MemoryCryptoStore implements CryptoStore {
|
||||
deviceSessions = {};
|
||||
this.sessions[deviceKey] = deviceSessions;
|
||||
}
|
||||
deviceSessions[sessionId] = sessionInfo;
|
||||
safeSet(deviceSessions, sessionId, sessionInfo);
|
||||
}
|
||||
|
||||
public async storeEndToEndSessionProblem(deviceKey: string, type: string, fixed: boolean): Promise<void> {
|
||||
|
||||
@@ -25,6 +25,9 @@ export enum ServerSupport {
|
||||
export enum Feature {
|
||||
Thread = "Thread",
|
||||
ThreadUnreadNotifications = "ThreadUnreadNotifications",
|
||||
/**
|
||||
* @deprecated this is now exposed as a capability not a feature
|
||||
*/
|
||||
LoginTokenRequest = "LoginTokenRequest",
|
||||
RelationBasedRedactions = "RelationBasedRedactions",
|
||||
AccountDataDeletion = "AccountDataDeletion",
|
||||
|
||||
@@ -66,7 +66,7 @@ export class FetchHttpApi<O extends IHttpOpts> {
|
||||
* Sets the base URL for the identity server
|
||||
* @param url - The new base url
|
||||
*/
|
||||
public setIdBaseUrl(url: string): void {
|
||||
public setIdBaseUrl(url?: string): void {
|
||||
this.opts.idBaseUrl = url;
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ export interface IAuthDict {
|
||||
token?: string;
|
||||
}
|
||||
|
||||
class NoAuthFlowFoundError extends Error {
|
||||
export class NoAuthFlowFoundError extends Error {
|
||||
public name = "NoAuthFlowFoundError";
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention, camelcase
|
||||
|
||||
+3
-1
@@ -47,6 +47,7 @@ export * from "./store/memory";
|
||||
export * from "./store/indexeddb";
|
||||
export * from "./crypto/store/memory-crypto-store";
|
||||
export * from "./crypto/store/indexeddb-crypto-store";
|
||||
export type { OutgoingRoomKeyRequest } from "./crypto/store/base";
|
||||
export * from "./content-repo";
|
||||
export * from "./@types/event";
|
||||
export * from "./@types/PushRules";
|
||||
@@ -62,13 +63,14 @@ export type { MatrixCall } from "./webrtc/call";
|
||||
export { GroupCallEvent, GroupCallIntent, GroupCallState, GroupCallType } from "./webrtc/groupCall";
|
||||
export type { GroupCall } from "./webrtc/groupCall";
|
||||
export type { CryptoApi } from "./crypto-api";
|
||||
export { CryptoEvent } from "./crypto";
|
||||
|
||||
let cryptoStoreFactory = (): CryptoStore => new MemoryCryptoStore();
|
||||
|
||||
/**
|
||||
* Configure a different factory to be used for creating crypto stores
|
||||
*
|
||||
* @param fac - a function which will return a new {@link CryptoStore}
|
||||
* @param fac - a function which will return a new `CryptoStore`
|
||||
*/
|
||||
export function setCryptoStoreFactory(fac: () => CryptoStore): void {
|
||||
cryptoStoreFactory = fac;
|
||||
|
||||
+37
-4
@@ -37,6 +37,7 @@ import { EventStatus } from "./event-status";
|
||||
import { DecryptionError } from "../crypto/algorithms";
|
||||
import { CryptoBackend } from "../common-crypto/CryptoBackend";
|
||||
import { WITHHELD_MESSAGES } from "../crypto/OlmDevice";
|
||||
import { IAnnotatedPushRule } from "../@types/PushRules";
|
||||
|
||||
export { EventStatus } from "./event-status";
|
||||
|
||||
@@ -121,6 +122,11 @@ export interface IMentions {
|
||||
room?: boolean;
|
||||
}
|
||||
|
||||
export interface PushDetails {
|
||||
rule?: IAnnotatedPushRule;
|
||||
actions?: IActionsObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* When an event is a visibility change event, as per MSC3531,
|
||||
* the visibility change implied by the event.
|
||||
@@ -220,7 +226,8 @@ export type MatrixEventHandlerMap = {
|
||||
} & Pick<ThreadEventHandlerMap, ThreadEvent.Update>;
|
||||
|
||||
export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, MatrixEventHandlerMap> {
|
||||
private pushActions: IActionsObject | null = null;
|
||||
// applied push rule and action for this event
|
||||
private pushDetails: PushDetails = {};
|
||||
private _replacingEvent: MatrixEvent | null = null;
|
||||
private _localRedactionEvent: MatrixEvent | null = null;
|
||||
private _isCancelled = false;
|
||||
@@ -888,7 +895,7 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
|
||||
// highlighting when the user's name is mentioned rely on this happening. We also want
|
||||
// to set the push actions before emitting so that any notification listeners don't
|
||||
// pick up the wrong contents.
|
||||
this.setPushActions(null);
|
||||
this.setPushDetails();
|
||||
|
||||
if (options.emit !== false) {
|
||||
this.emit(MatrixEventEvent.Decrypted, this, err);
|
||||
@@ -1241,16 +1248,42 @@ export class MatrixEvent extends TypedEventEmitter<MatrixEventEmittedEvents, Mat
|
||||
* @returns push actions
|
||||
*/
|
||||
public getPushActions(): IActionsObject | null {
|
||||
return this.pushActions;
|
||||
return this.pushDetails.actions || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the push details, if known, for this event
|
||||
*
|
||||
* @returns push actions
|
||||
*/
|
||||
public getPushDetails(): PushDetails {
|
||||
return this.pushDetails;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the push actions for this event.
|
||||
* Clears rule from push details if present
|
||||
* @deprecated use `setPushDetails`
|
||||
*
|
||||
* @param pushActions - push actions
|
||||
*/
|
||||
public setPushActions(pushActions: IActionsObject | null): void {
|
||||
this.pushActions = pushActions;
|
||||
this.pushDetails = {
|
||||
actions: pushActions || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the push details for this event.
|
||||
*
|
||||
* @param pushActions - push actions
|
||||
* @param rule - the executed push rule
|
||||
*/
|
||||
public setPushDetails(pushActions?: IActionsObject, rule?: IAnnotatedPushRule): void {
|
||||
this.pushDetails = {
|
||||
actions: pushActions,
|
||||
rule,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
import { M_POLL_START } from "matrix-events-sdk";
|
||||
|
||||
import { M_POLL_END, M_POLL_RESPONSE } from "../@types/polls";
|
||||
import { MatrixClient } from "../client";
|
||||
import { PollStartEvent } from "../extensible_events_v1/PollStartEvent";
|
||||
@@ -266,3 +268,14 @@ export class Poll extends TypedEventEmitter<Exclude<PollEvent, PollEvent.New>, P
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether the event is a start, response or end poll event.
|
||||
*
|
||||
* @param event - Event to test
|
||||
* @returns true if the event is a poll event, else false
|
||||
*/
|
||||
export const isPollEvent = (event: MatrixEvent): boolean => {
|
||||
const eventType = event.getType();
|
||||
return M_POLL_START.matches(eventType) || M_POLL_RESPONSE.matches(eventType) || M_POLL_END.matches(eventType);
|
||||
};
|
||||
|
||||
+60
-23
@@ -64,7 +64,7 @@ import {
|
||||
import { IStateEventWithRoomId } from "../@types/search";
|
||||
import { RelationsContainer } from "./relations-container";
|
||||
import { ReadReceipt, synthesizeReceipt } from "./read-receipt";
|
||||
import { Poll, PollEvent } from "./poll";
|
||||
import { isPollEvent, Poll, PollEvent } from "./poll";
|
||||
|
||||
// These constants are used as sane defaults when the homeserver doesn't support
|
||||
// the m.room_versions capability. In practice, KNOWN_SAFE_ROOM_VERSION should be
|
||||
@@ -324,7 +324,14 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
||||
private unthreadedReceipts = new Map<string, Receipt>();
|
||||
private readonly timelineSets: EventTimelineSet[];
|
||||
public readonly polls: Map<string, Poll> = new Map<string, Poll>();
|
||||
public readonly threadsTimelineSets: EventTimelineSet[] = [];
|
||||
|
||||
/**
|
||||
* Empty array if the timeline sets have not been initialised. After initialisation:
|
||||
* 0: All threads
|
||||
* 1: Threads the current user has participated in
|
||||
*/
|
||||
public readonly threadsTimelineSets: [] | [EventTimelineSet, EventTimelineSet] = [];
|
||||
|
||||
// any filtered timeline sets we're maintaining for this room
|
||||
private readonly filteredTimelineSets: Record<string, EventTimelineSet> = {}; // filter_id: timelineSet
|
||||
private timelineNeedsRefresh = false;
|
||||
@@ -490,7 +497,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
||||
this.createThreadTimelineSet(ThreadFilterType.My),
|
||||
]);
|
||||
const timelineSets = await this.threadTimelineSetsPromise;
|
||||
this.threadsTimelineSets.push(...timelineSets);
|
||||
this.threadsTimelineSets[0] = timelineSets[0];
|
||||
this.threadsTimelineSets[1] = timelineSets[1];
|
||||
return timelineSets;
|
||||
} catch (e) {
|
||||
this.threadTimelineSetsPromise = null;
|
||||
@@ -1897,35 +1905,62 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
||||
this.threadsReady = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls {@link processPollEvent} for a list of events.
|
||||
*
|
||||
* @param events - List of events
|
||||
*/
|
||||
public async processPollEvents(events: MatrixEvent[]): Promise<void> {
|
||||
const processPollStartEvent = (event: MatrixEvent): void => {
|
||||
if (!M_POLL_START.matches(event.getType())) return;
|
||||
for (const event of events) {
|
||||
try {
|
||||
// Continue if the event is a clear text, non-poll event.
|
||||
if (!event.isEncrypted() && !isPollEvent(event)) continue;
|
||||
|
||||
/**
|
||||
* Try to decrypt the event. Promise resolution does not guarantee a successful decryption.
|
||||
* Retry is handled in {@link processPollEvent}.
|
||||
*/
|
||||
await this.client.decryptEventIfNeeded(event);
|
||||
this.processPollEvent(event);
|
||||
} catch (err) {
|
||||
logger.warn("Error processing poll event", event.getId(), err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes poll events:
|
||||
* If the event has a decryption failure, it will listen for decryption and tries again.
|
||||
* If it is a poll start event (`m.poll.start`),
|
||||
* it creates and stores a Poll model and emits a PollEvent.New event.
|
||||
* If the event is related to a poll, it will add it to the poll.
|
||||
* Noop for other cases.
|
||||
*
|
||||
* @param event - Event that could be a poll event
|
||||
*/
|
||||
private async processPollEvent(event: MatrixEvent): Promise<void> {
|
||||
if (event.isDecryptionFailure()) {
|
||||
event.once(MatrixEventEvent.Decrypted, (maybeDecryptedEvent: MatrixEvent) => {
|
||||
this.processPollEvent(maybeDecryptedEvent);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (M_POLL_START.matches(event.getType())) {
|
||||
try {
|
||||
const poll = new Poll(event, this.client, this);
|
||||
this.polls.set(event.getId()!, poll);
|
||||
this.emit(PollEvent.New, poll);
|
||||
} catch {}
|
||||
// poll creation can fail for malformed poll start events
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
const processPollRelationEvent = (event: MatrixEvent): void => {
|
||||
const relationEventId = event.relationEventId;
|
||||
if (relationEventId && this.polls.has(relationEventId)) {
|
||||
const poll = this.polls.get(relationEventId);
|
||||
poll?.onNewRelation(event);
|
||||
}
|
||||
};
|
||||
const relationEventId = event.relationEventId;
|
||||
|
||||
const processPollEvent = (event: MatrixEvent): void => {
|
||||
processPollStartEvent(event);
|
||||
processPollRelationEvent(event);
|
||||
};
|
||||
|
||||
for (const event of events) {
|
||||
try {
|
||||
await this.client.decryptEventIfNeeded(event);
|
||||
processPollEvent(event);
|
||||
} catch {}
|
||||
if (relationEventId && this.polls.has(relationEventId)) {
|
||||
const poll = this.polls.get(relationEventId);
|
||||
poll?.onNewRelation(event);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1934,6 +1969,8 @@ export class Room extends ReadReceipt<RoomEmittedEvents, RoomEventHandlerMap> {
|
||||
* @internal
|
||||
*/
|
||||
private async fetchRoomThreadList(filter?: ThreadFilterType): Promise<void> {
|
||||
if (this.threadsTimelineSets.length === 0) return;
|
||||
|
||||
const timelineSet = filter === ThreadFilterType.My ? this.threadsTimelineSets[1] : this.threadsTimelineSets[0];
|
||||
|
||||
const { chunk: events, end } = await this.client.createThreadListMessagesRequest(
|
||||
|
||||
@@ -59,10 +59,6 @@ export class TypedEventEmitter<
|
||||
return super.emit(event, ...args);
|
||||
}
|
||||
|
||||
public eventNames(): (Events | EventEmitterEvents)[] {
|
||||
return super.eventNames() as Array<Events | EventEmitterEvents>;
|
||||
}
|
||||
|
||||
public listenerCount(event: Events | EventEmitterEvents): number {
|
||||
return super.listenerCount(event);
|
||||
}
|
||||
|
||||
+20
-5
@@ -589,7 +589,7 @@ export class PushProcessor {
|
||||
* @internal
|
||||
*/
|
||||
public static partsForDottedKey(str: string): string[] {
|
||||
const result = [];
|
||||
const result: string[] = [];
|
||||
|
||||
// The current field and whether the previous character was the escape
|
||||
// character (a backslash).
|
||||
@@ -688,17 +688,24 @@ export class PushProcessor {
|
||||
if (!rulesets) {
|
||||
return null;
|
||||
}
|
||||
if (ev.getSender() === this.client.credentials.userId) {
|
||||
|
||||
if (ev.getSender() === this.client.getSafeUserId()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.matchingRuleFromKindSet(ev, rulesets.global);
|
||||
}
|
||||
|
||||
private pushActionsForEventAndRulesets(ev: MatrixEvent, rulesets?: IPushRules): IActionsObject {
|
||||
private pushActionsForEventAndRulesets(
|
||||
ev: MatrixEvent,
|
||||
rulesets?: IPushRules,
|
||||
): {
|
||||
actions?: IActionsObject;
|
||||
rule?: IAnnotatedPushRule;
|
||||
} {
|
||||
const rule = this.matchingRuleForEventWithRulesets(ev, rulesets);
|
||||
if (!rule) {
|
||||
return {} as IActionsObject;
|
||||
return {};
|
||||
}
|
||||
|
||||
const actionObj = PushProcessor.actionListToActionsObject(rule.actions);
|
||||
@@ -710,7 +717,7 @@ export class PushProcessor {
|
||||
actionObj.tweaks.highlight = rule.kind == PushRuleKind.ContentSpecific;
|
||||
}
|
||||
|
||||
return actionObj;
|
||||
return { actions: actionObj, rule };
|
||||
}
|
||||
|
||||
public ruleMatchesEvent(rule: Partial<IPushRule> & Pick<IPushRule, "conditions">, ev: MatrixEvent): boolean {
|
||||
@@ -732,6 +739,14 @@ export class PushProcessor {
|
||||
* Get the user's push actions for the given event
|
||||
*/
|
||||
public actionsForEvent(ev: MatrixEvent): IActionsObject {
|
||||
const { actions } = this.pushActionsForEventAndRulesets(ev, this.client.pushRules);
|
||||
return actions || ({} as IActionsObject);
|
||||
}
|
||||
|
||||
public actionsAndRuleForEvent(ev: MatrixEvent): {
|
||||
actions?: IActionsObject;
|
||||
rule?: IAnnotatedPushRule;
|
||||
} {
|
||||
return this.pushActionsForEventAndRulesets(ev, this.client.pushRules);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ limitations under the License.
|
||||
import { UnstableValue } from "matrix-events-sdk";
|
||||
|
||||
import { RendezvousChannel, RendezvousFailureListener, RendezvousFailureReason, RendezvousIntent } from ".";
|
||||
import { MatrixClient } from "../client";
|
||||
import { IMSC3882GetLoginTokenCapability, MatrixClient, UNSTABLE_MSC3882_CAPABILITY } from "../client";
|
||||
import { CrossSigningInfo } from "../crypto/CrossSigning";
|
||||
import { DeviceInfo } from "../crypto/deviceinfo";
|
||||
import { buildFeatureSupportMap, Feature, ServerSupport } from "../feature";
|
||||
@@ -100,9 +100,14 @@ export class MSC3906Rendezvous {
|
||||
|
||||
logger.info(`Connected to secure channel with checksum: ${checksum} our intent is ${this.ourIntent}`);
|
||||
|
||||
// in r1 of MSC3882 the availability is exposed as a capability
|
||||
const capabilities = await this.client.getCapabilities();
|
||||
// in r0 of MSC3882 the availability is exposed as a feature flag
|
||||
const features = await buildFeatureSupportMap(await this.client.getVersions());
|
||||
const capability = UNSTABLE_MSC3882_CAPABILITY.findIn<IMSC3882GetLoginTokenCapability>(capabilities);
|
||||
|
||||
// determine available protocols
|
||||
if (features.get(Feature.LoginTokenRequest) === ServerSupport.Unsupported) {
|
||||
if (!capability?.enabled && features.get(Feature.LoginTokenRequest) === ServerSupport.Unsupported) {
|
||||
logger.info("Server doesn't support MSC3882");
|
||||
await this.send({ type: PayloadType.Finish, outcome: Outcome.Unsupported });
|
||||
await this.cancel(RendezvousFailureReason.HomeserverLacksSupport);
|
||||
|
||||
@@ -39,6 +39,9 @@ export async function initRustCrypto(
|
||||
// TODO: use the pickle key for the passphrase
|
||||
const olmMachine = await RustSdkCryptoJs.OlmMachine.initialize(u, d, RUST_SDK_STORE_PREFIX, "test pass");
|
||||
const rustCrypto = new RustCrypto(olmMachine, http, userId, deviceId);
|
||||
await olmMachine.registerRoomKeyUpdatedCallback((sessions: RustSdkCryptoJs.RoomKeyInfo[]) =>
|
||||
rustCrypto.onRoomKeysUpdated(sessions),
|
||||
);
|
||||
|
||||
logger.info("Completed rust crypto-sdk setup");
|
||||
return rustCrypto;
|
||||
|
||||
+172
-29
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
Copyright 2022-2023 The Matrix.org Foundation C.I.C.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
@@ -17,7 +17,7 @@ limitations under the License.
|
||||
import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-js";
|
||||
|
||||
import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto";
|
||||
import type { IToDeviceEvent } from "../sync-accumulator";
|
||||
import type { IDeviceLists, IToDeviceEvent } from "../sync-accumulator";
|
||||
import type { IEncryptedEventInfo } from "../crypto/api";
|
||||
import { MatrixEvent } from "../models/event";
|
||||
import { Room } from "../models/room";
|
||||
@@ -29,6 +29,7 @@ import { DeviceTrustLevel, UserTrustLevel } from "../crypto/CrossSigning";
|
||||
import { RoomEncryptor } from "./RoomEncryptor";
|
||||
import { OutgoingRequest, OutgoingRequestProcessor } from "./OutgoingRequestProcessor";
|
||||
import { KeyClaimManager } from "./KeyClaimManager";
|
||||
import { MapWithDefault } from "../utils";
|
||||
|
||||
/**
|
||||
* An implementation of {@link CryptoBackend} using the Rust matrix-sdk-crypto.
|
||||
@@ -45,6 +46,7 @@ export class RustCrypto implements CryptoBackend {
|
||||
/** mapping of roomId → encryptor class */
|
||||
private roomEncryptors: Record<string, RoomEncryptor> = {};
|
||||
|
||||
private eventDecryptor: EventDecryptor;
|
||||
private keyClaimManager: KeyClaimManager;
|
||||
private outgoingRequestProcessor: OutgoingRequestProcessor;
|
||||
|
||||
@@ -56,6 +58,7 @@ export class RustCrypto implements CryptoBackend {
|
||||
) {
|
||||
this.outgoingRequestProcessor = new OutgoingRequestProcessor(olmMachine, http);
|
||||
this.keyClaimManager = new KeyClaimManager(olmMachine, this.outgoingRequestProcessor);
|
||||
this.eventDecryptor = new EventDecryptor(olmMachine);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -101,23 +104,7 @@ export class RustCrypto implements CryptoBackend {
|
||||
// through decryptEvent and hence get rid of this case.
|
||||
throw new Error("to-device event was not decrypted in preprocessToDeviceMessages");
|
||||
}
|
||||
const res = (await this.olmMachine.decryptRoomEvent(
|
||||
JSON.stringify({
|
||||
event_id: event.getId(),
|
||||
type: event.getWireType(),
|
||||
sender: event.getSender(),
|
||||
state_key: event.getStateKey(),
|
||||
content: event.getWireContent(),
|
||||
origin_server_ts: event.getTs(),
|
||||
}),
|
||||
new RustSdkCryptoJs.RoomId(event.getRoomId()!),
|
||||
)) as RustSdkCryptoJs.DecryptedRoomEvent;
|
||||
return {
|
||||
clearEvent: JSON.parse(res.event),
|
||||
claimedEd25519Key: res.senderClaimedEd25519Key,
|
||||
senderCurve25519Key: res.senderCurve25519Key,
|
||||
forwardingCurve25519KeyChain: res.forwardingCurve25519KeyChain,
|
||||
};
|
||||
return await this.eventDecryptor.attemptEventDecryption(event);
|
||||
}
|
||||
|
||||
public getEventEncryptionInfo(event: MatrixEvent): IEncryptedEventInfo {
|
||||
@@ -189,20 +176,23 @@ export class RustCrypto implements CryptoBackend {
|
||||
* @param events - the received to-device messages
|
||||
* @param oneTimeKeysCounts - the received one time key counts
|
||||
* @param unusedFallbackKeys - the received unused fallback keys
|
||||
* @param devices - the received device list updates
|
||||
* @returns A list of preprocessed to-device messages.
|
||||
*/
|
||||
private async receiveSyncChanges({
|
||||
events,
|
||||
oneTimeKeysCounts = new Map<string, number>(),
|
||||
unusedFallbackKeys = new Set<string>(),
|
||||
devices = new RustSdkCryptoJs.DeviceLists(),
|
||||
}: {
|
||||
events?: IToDeviceEvent[];
|
||||
oneTimeKeysCounts?: Map<string, number>;
|
||||
unusedFallbackKeys?: Set<string>;
|
||||
devices?: RustSdkCryptoJs.DeviceLists;
|
||||
}): Promise<IToDeviceEvent[]> {
|
||||
const result = await this.olmMachine.receiveSyncChanges(
|
||||
events ? JSON.stringify(events) : "[]",
|
||||
new RustSdkCryptoJs.DeviceLists(),
|
||||
devices,
|
||||
oneTimeKeysCounts,
|
||||
unusedFallbackKeys,
|
||||
);
|
||||
@@ -222,22 +212,37 @@ export class RustCrypto implements CryptoBackend {
|
||||
return this.receiveSyncChanges({ events });
|
||||
}
|
||||
|
||||
/** called by the sync loop to preprocess one time key counts
|
||||
/** called by the sync loop to process one time key counts and unused fallback keys
|
||||
*
|
||||
* @param oneTimeKeysCounts - the received one time key counts
|
||||
* @returns A list of preprocessed to-device messages.
|
||||
* @param unusedFallbackKeys - the received unused fallback keys
|
||||
*/
|
||||
public async preprocessOneTimeKeyCounts(oneTimeKeysCounts: Map<string, number>): Promise<void> {
|
||||
await this.receiveSyncChanges({ oneTimeKeysCounts });
|
||||
public async processKeyCounts(
|
||||
oneTimeKeysCounts?: Record<string, number>,
|
||||
unusedFallbackKeys?: string[],
|
||||
): Promise<void> {
|
||||
const mapOneTimeKeysCount = oneTimeKeysCounts && new Map<string, number>(Object.entries(oneTimeKeysCounts));
|
||||
const setUnusedFallbackKeys = unusedFallbackKeys && new Set<string>(unusedFallbackKeys);
|
||||
|
||||
if (mapOneTimeKeysCount !== undefined || setUnusedFallbackKeys !== undefined) {
|
||||
await this.receiveSyncChanges({
|
||||
oneTimeKeysCounts: mapOneTimeKeysCount,
|
||||
unusedFallbackKeys: setUnusedFallbackKeys,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** called by the sync loop to preprocess unused fallback keys
|
||||
/** called by the sync loop to process the notification that device lists have
|
||||
* been changed.
|
||||
*
|
||||
* @param unusedFallbackKeys - the received unused fallback keys
|
||||
* @returns A list of preprocessed to-device messages.
|
||||
* @param deviceLists - device_lists field from /sync
|
||||
*/
|
||||
public async preprocessUnusedFallbackKeys(unusedFallbackKeys: Set<string>): Promise<void> {
|
||||
await this.receiveSyncChanges({ unusedFallbackKeys });
|
||||
public async processDeviceLists(deviceLists: IDeviceLists): Promise<void> {
|
||||
const devices = new RustSdkCryptoJs.DeviceLists(
|
||||
deviceLists.changed?.map((userId) => new RustSdkCryptoJs.UserId(userId)),
|
||||
deviceLists.left?.map((userId) => new RustSdkCryptoJs.UserId(userId)),
|
||||
);
|
||||
await this.receiveSyncChanges({ devices });
|
||||
}
|
||||
|
||||
/** called by the sync loop on m.room.encrypted events
|
||||
@@ -303,6 +308,43 @@ export class RustCrypto implements CryptoBackend {
|
||||
enc.onRoomMembership(member);
|
||||
}
|
||||
|
||||
/** Callback for OlmMachine.registerRoomKeyUpdatedCallback
|
||||
*
|
||||
* Called by the rust-sdk whenever there is an update to (megolm) room keys. We
|
||||
* check if we have any events waiting for the given keys, and schedule them for
|
||||
* a decryption retry if so.
|
||||
*
|
||||
* @param keys - details of the updated keys
|
||||
*/
|
||||
public async onRoomKeysUpdated(keys: RustSdkCryptoJs.RoomKeyInfo[]): Promise<void> {
|
||||
for (const key of keys) {
|
||||
this.onRoomKeyUpdated(key);
|
||||
}
|
||||
}
|
||||
|
||||
private onRoomKeyUpdated(key: RustSdkCryptoJs.RoomKeyInfo): void {
|
||||
logger.debug(`Got update for session ${key.senderKey.toBase64()}|${key.sessionId} in ${key.roomId.toString()}`);
|
||||
const pendingList = this.eventDecryptor.getEventsPendingRoomKey(key);
|
||||
if (pendingList.length === 0) return;
|
||||
|
||||
logger.debug(
|
||||
"Retrying decryption on events:",
|
||||
pendingList.map((e) => `${e.getId()}`),
|
||||
);
|
||||
|
||||
// Have another go at decrypting events with this key.
|
||||
//
|
||||
// We don't want to end up blocking the callback from Rust, which could otherwise end up dropping updates,
|
||||
// so we don't wait for the decryption to complete. In any case, there is no need to wait:
|
||||
// MatrixEvent.attemptDecryption ensures that there is only one decryption attempt happening at once,
|
||||
// and deduplicates repeated attempts for the same event.
|
||||
for (const ev of pendingList) {
|
||||
ev.attemptDecryption(this, { isRetry: true }).catch((_e) => {
|
||||
logger.info(`Still unable to decrypt event ${ev.getId()} after receiving key`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Outgoing requests
|
||||
@@ -332,3 +374,104 @@ export class RustCrypto implements CryptoBackend {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EventDecryptor {
|
||||
/**
|
||||
* Events which we couldn't decrypt due to unknown sessions / indexes.
|
||||
*
|
||||
* Map from senderKey to sessionId to Set of MatrixEvents
|
||||
*/
|
||||
private eventsPendingKey = new MapWithDefault<string, MapWithDefault<string, Set<MatrixEvent>>>(
|
||||
() => new MapWithDefault<string, Set<MatrixEvent>>(() => new Set()),
|
||||
);
|
||||
|
||||
public constructor(private readonly olmMachine: RustSdkCryptoJs.OlmMachine) {}
|
||||
|
||||
public async attemptEventDecryption(event: MatrixEvent): Promise<IEventDecryptionResult> {
|
||||
logger.info("Attempting decryption of event", event);
|
||||
// add the event to the pending list *before* attempting to decrypt.
|
||||
// then, if the key turns up while decryption is in progress (and
|
||||
// decryption fails), we will schedule a retry.
|
||||
// (fixes https://github.com/vector-im/element-web/issues/5001)
|
||||
this.addEventToPendingList(event);
|
||||
|
||||
const res = (await this.olmMachine.decryptRoomEvent(
|
||||
JSON.stringify({
|
||||
event_id: event.getId(),
|
||||
type: event.getWireType(),
|
||||
sender: event.getSender(),
|
||||
state_key: event.getStateKey(),
|
||||
content: event.getWireContent(),
|
||||
origin_server_ts: event.getTs(),
|
||||
}),
|
||||
new RustSdkCryptoJs.RoomId(event.getRoomId()!),
|
||||
)) as RustSdkCryptoJs.DecryptedRoomEvent;
|
||||
|
||||
// Success. We can remove the event from the pending list, if
|
||||
// that hasn't already happened.
|
||||
this.removeEventFromPendingList(event);
|
||||
|
||||
return {
|
||||
clearEvent: JSON.parse(res.event),
|
||||
claimedEd25519Key: res.senderClaimedEd25519Key,
|
||||
senderCurve25519Key: res.senderCurve25519Key,
|
||||
forwardingCurve25519KeyChain: res.forwardingCurve25519KeyChain,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Look for events which are waiting for a given megolm session
|
||||
*
|
||||
* Returns a list of events which were encrypted by `session` and could not be decrypted
|
||||
*
|
||||
* @param session -
|
||||
*/
|
||||
public getEventsPendingRoomKey(session: RustSdkCryptoJs.RoomKeyInfo): MatrixEvent[] {
|
||||
const senderPendingEvents = this.eventsPendingKey.get(session.senderKey.toBase64());
|
||||
if (!senderPendingEvents) return [];
|
||||
|
||||
const sessionPendingEvents = senderPendingEvents.get(session.sessionId);
|
||||
if (!sessionPendingEvents) return [];
|
||||
|
||||
const roomId = session.roomId.toString();
|
||||
return [...sessionPendingEvents].filter((ev) => ev.getRoomId() === roomId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an event to the list of those awaiting their session keys.
|
||||
*/
|
||||
private addEventToPendingList(event: MatrixEvent): void {
|
||||
const content = event.getWireContent();
|
||||
const senderKey = content.sender_key;
|
||||
const sessionId = content.session_id;
|
||||
|
||||
const senderPendingEvents = this.eventsPendingKey.getOrCreate(senderKey);
|
||||
const sessionPendingEvents = senderPendingEvents.getOrCreate(sessionId);
|
||||
sessionPendingEvents.add(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an event from the list of those awaiting their session keys.
|
||||
*/
|
||||
private removeEventFromPendingList(event: MatrixEvent): void {
|
||||
const content = event.getWireContent();
|
||||
const senderKey = content.sender_key;
|
||||
const sessionId = content.session_id;
|
||||
|
||||
const senderPendingEvents = this.eventsPendingKey.get(senderKey);
|
||||
if (!senderPendingEvents) return;
|
||||
|
||||
const sessionPendingEvents = senderPendingEvents.get(sessionId);
|
||||
if (!sessionPendingEvents) return;
|
||||
|
||||
sessionPendingEvents.delete(event);
|
||||
|
||||
// also clean up the higher-level maps if they are now empty
|
||||
if (sessionPendingEvents.size === 0) {
|
||||
senderPendingEvents.delete(sessionId);
|
||||
if (senderPendingEvents.size === 0) {
|
||||
this.eventsPendingKey.delete(senderKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,15 @@ limitations under the License.
|
||||
* @see https://spec.matrix.org/v1.6/client-server-api/#storage
|
||||
*/
|
||||
|
||||
import { TypedEventEmitter } from "./models/typed-event-emitter";
|
||||
import { ClientEvent, ClientEventHandlerMap } from "./client";
|
||||
import { MatrixEvent } from "./models/event";
|
||||
import { calculateKeyCheck, decryptAES, encryptAES, IEncryptedPayload } from "./crypto/aes";
|
||||
import { randomString } from "./randomstring";
|
||||
import { logger } from "./logger";
|
||||
|
||||
export const SECRET_STORAGE_ALGORITHM_V1_AES = "m.secret_storage.v1.aes-hmac-sha2";
|
||||
|
||||
/**
|
||||
* Common base interface for Secret Storage Keys.
|
||||
*
|
||||
@@ -86,3 +95,580 @@ export interface PassphraseInfo {
|
||||
/** The number of bits to generate. Defaults to 256. */
|
||||
bits?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for {@link ServerSideSecretStorageImpl#addKey}.
|
||||
*/
|
||||
export interface AddSecretStorageKeyOpts {
|
||||
pubkey?: string;
|
||||
passphrase?: PassphraseInfo;
|
||||
name?: string;
|
||||
key?: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return type for {@link ServerSideSecretStorageImpl#getKey}.
|
||||
*/
|
||||
export type SecretStorageKeyTuple = [keyId: string, keyInfo: SecretStorageKeyDescription];
|
||||
|
||||
/**
|
||||
* Return type for {@link ServerSideSecretStorageImpl#addKey}.
|
||||
*/
|
||||
export type SecretStorageKeyObject = {
|
||||
/** The ID of the key */
|
||||
keyId: string;
|
||||
/** details about the key */
|
||||
keyInfo: SecretStorageKeyDescription;
|
||||
};
|
||||
|
||||
/** Interface for managing account data on the server.
|
||||
*
|
||||
* A subset of {@link MatrixClient}.
|
||||
*/
|
||||
export interface AccountDataClient extends TypedEventEmitter<ClientEvent.AccountData, ClientEventHandlerMap> {
|
||||
/**
|
||||
* Get account data event of given type for the current user. This variant
|
||||
* gets account data directly from the homeserver if the local store is not
|
||||
* ready, which can be useful very early in startup before the initial sync.
|
||||
*
|
||||
* @param eventType - The type of account data
|
||||
* @returns The contents of the given account data event, or `null` if the event is not found
|
||||
*/
|
||||
getAccountDataFromServer: <T extends Record<string, any>>(eventType: string) => Promise<T | null>;
|
||||
|
||||
/**
|
||||
* Set account data event for the current user, with retries
|
||||
*
|
||||
* @param eventType - The type of account data
|
||||
* @param content - the content object to be set
|
||||
* @returns an empty object
|
||||
*/
|
||||
setAccountData: (eventType: string, content: any) => Promise<{}>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Application callbacks for use with {@link SecretStorage.ServerSideSecretStorageImpl}
|
||||
*/
|
||||
export interface SecretStorageCallbacks {
|
||||
/**
|
||||
* Called to retrieve a secret storage encryption key
|
||||
*
|
||||
* Before a secret can be stored in server-side storage, it must be encrypted with one or more
|
||||
* keys. Similarly, after it has been retrieved from storage, it must be decrypted with one of
|
||||
* the keys it was encrypted with. These encryption keys are known as "secret storage keys".
|
||||
*
|
||||
* Descriptions of the secret storage keys are also stored in server-side storage, per the
|
||||
* [matrix specification](https://spec.matrix.org/v1.6/client-server-api/#key-storage), so
|
||||
* before a key can be used in this way, it must have been stored on the server. This is
|
||||
* done via {@link SecretStorage.ServerSideSecretStorage#addKey}.
|
||||
*
|
||||
* Obviously the keys themselves are not stored server-side, so the js-sdk calls this callback
|
||||
* in order to retrieve a secret storage key from the application.
|
||||
*
|
||||
* @param keys - An options object, containing only the property `keys`.
|
||||
*
|
||||
* @param name - the name of the *secret* (NB: not the encryption key) being stored or retrieved.
|
||||
* This is the "event type" stored in account data.
|
||||
*
|
||||
* @returns a pair [`keyId`, `privateKey`], where `keyId` is one of the keys from the `keys` parameter,
|
||||
* and `privateKey` is the raw private encryption key, as appropriate for the encryption algorithm.
|
||||
* (For `m.secret_storage.v1.aes-hmac-sha2`, it is the input to an HKDF as defined in the
|
||||
* [specification](https://spec.matrix.org/v1.6/client-server-api/#msecret_storagev1aes-hmac-sha2).)
|
||||
*
|
||||
* Alternatively, if none of the keys are known, may return `null` — in which case the original
|
||||
* storage/retrieval operation will fail with an exception.
|
||||
*/
|
||||
getSecretStorageKey?: (
|
||||
keys: {
|
||||
/**
|
||||
* details of the secret storage keys required: a map from the key ID
|
||||
* (excluding the `m.secret_storage.key.` prefix) to details of the key.
|
||||
*
|
||||
* When storing a secret, `keys` will contain exactly one entry; this method will be called
|
||||
* once for each secret storage key to be used for encryption.
|
||||
*
|
||||
* For secret retrieval, `keys` may contain several entries, and the application can return
|
||||
* any one of the requested keys.
|
||||
*/
|
||||
keys: Record<string, SecretStorageKeyDescription>;
|
||||
},
|
||||
name: string,
|
||||
) => Promise<[string, Uint8Array] | null>;
|
||||
}
|
||||
|
||||
interface SecretInfo {
|
||||
encrypted: {
|
||||
[keyId: string]: IEncryptedPayload;
|
||||
};
|
||||
}
|
||||
|
||||
interface Decryptors {
|
||||
encrypt: (plaintext: string) => Promise<IEncryptedPayload>;
|
||||
decrypt: (ciphertext: IEncryptedPayload) => Promise<string>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface provided by SecretStorage implementations
|
||||
*
|
||||
* Normally this will just be an {@link ServerSideSecretStorageImpl}, but for backwards
|
||||
* compatibility some methods allow other implementations.
|
||||
*/
|
||||
export interface ServerSideSecretStorage {
|
||||
/**
|
||||
* Add a key for encrypting secrets.
|
||||
*
|
||||
* @param algorithm - the algorithm used by the key.
|
||||
* @param opts - the options for the algorithm. The properties used
|
||||
* depend on the algorithm given.
|
||||
* @param keyId - the ID of the key. If not given, a random
|
||||
* ID will be generated.
|
||||
*
|
||||
* @returns details about the key.
|
||||
*/
|
||||
addKey(algorithm: string, opts: AddSecretStorageKeyOpts, keyId?: string): Promise<SecretStorageKeyObject>;
|
||||
|
||||
/**
|
||||
* Get the key information for a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns If the key was found, the return value is an array of
|
||||
* the form [keyId, keyInfo]. Otherwise, null is returned.
|
||||
* XXX: why is this an array when addKey returns an object?
|
||||
*/
|
||||
getKey(keyId?: string | null): Promise<SecretStorageKeyTuple | null>;
|
||||
|
||||
/**
|
||||
* Check whether we have a key with a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns Whether we have the key.
|
||||
*/
|
||||
hasKey(keyId?: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Check whether a key matches what we expect based on the key info
|
||||
*
|
||||
* @param key - the key to check
|
||||
* @param info - the key info
|
||||
*
|
||||
* @returns whether or not the key matches
|
||||
*/
|
||||
checkKey(key: Uint8Array, info: SecretStorageKeyDescriptionAesV1): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Store an encrypted secret on the server.
|
||||
*
|
||||
* Details of the encryption keys to be used must previously have been stored in account data
|
||||
* (for example, via {@link ServerSideSecretStorage#addKey}.
|
||||
*
|
||||
* @param name - The name of the secret - i.e., the "event type" to be stored in the account data
|
||||
* @param secret - The secret contents.
|
||||
* @param keys - The IDs of the keys to use to encrypt the secret, or null/undefined to use the default key
|
||||
* (will throw if no default key is set).
|
||||
*/
|
||||
store(name: string, secret: string, keys?: string[] | null): Promise<void>;
|
||||
|
||||
/**
|
||||
* Get a secret from storage, and decrypt it.
|
||||
*
|
||||
* @param name - the name of the secret - i.e., the "event type" stored in the account data
|
||||
*
|
||||
* @returns the decrypted contents of the secret, or "undefined" if `name` is not found in
|
||||
* the user's account data.
|
||||
*/
|
||||
get(name: string): Promise<string | undefined>;
|
||||
|
||||
/**
|
||||
* Check if a secret is stored on the server.
|
||||
*
|
||||
* @param name - the name of the secret
|
||||
*
|
||||
* @returns map of key name to key info the secret is encrypted
|
||||
* with, or null if it is not present or not encrypted with a trusted
|
||||
* key
|
||||
*/
|
||||
isStored(name: string): Promise<Record<string, SecretStorageKeyDescriptionAesV1> | null>;
|
||||
|
||||
/**
|
||||
* Get the current default key ID for encrypting secrets.
|
||||
*
|
||||
* @returns The default key ID or null if no default key ID is set
|
||||
*/
|
||||
getDefaultKeyId(): Promise<string | null>;
|
||||
|
||||
/**
|
||||
* Set the default key ID for encrypting secrets.
|
||||
*
|
||||
* @param keyId - The new default key ID
|
||||
*/
|
||||
setDefaultKeyId(keyId: string): Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of Server-side secret storage.
|
||||
*
|
||||
* Secret *sharing* is *not* implemented here: this class is strictly about the storage component of
|
||||
* SSSS.
|
||||
*
|
||||
* @see https://spec.matrix.org/v1.6/client-server-api/#storage
|
||||
*/
|
||||
export class ServerSideSecretStorageImpl implements ServerSideSecretStorage {
|
||||
/**
|
||||
* Construct a new `SecretStorage`.
|
||||
*
|
||||
* Normally, it is unnecessary to call this directly, since MatrixClient automatically constructs one.
|
||||
* However, it may be useful to construct a new `SecretStorage`, if custom `callbacks` are required, for example.
|
||||
*
|
||||
* @param accountDataAdapter - interface for fetching and setting account data on the server. Normally an instance
|
||||
* of {@link MatrixClient}.
|
||||
* @param callbacks - application level callbacks for retrieving secret keys
|
||||
*/
|
||||
public constructor(
|
||||
private readonly accountDataAdapter: AccountDataClient,
|
||||
private readonly callbacks: SecretStorageCallbacks,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get the current default key ID for encrypting secrets.
|
||||
*
|
||||
* @returns The default key ID or null if no default key ID is set
|
||||
*/
|
||||
public async getDefaultKeyId(): Promise<string | null> {
|
||||
const defaultKey = await this.accountDataAdapter.getAccountDataFromServer<{ key: string }>(
|
||||
"m.secret_storage.default_key",
|
||||
);
|
||||
if (!defaultKey) return null;
|
||||
return defaultKey.key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default key ID for encrypting secrets.
|
||||
*
|
||||
* @param keyId - The new default key ID
|
||||
*/
|
||||
public setDefaultKeyId(keyId: string): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const listener = (ev: MatrixEvent): void => {
|
||||
if (ev.getType() === "m.secret_storage.default_key" && ev.getContent().key === keyId) {
|
||||
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
this.accountDataAdapter.on(ClientEvent.AccountData, listener);
|
||||
|
||||
this.accountDataAdapter.setAccountData("m.secret_storage.default_key", { key: keyId }).catch((e) => {
|
||||
this.accountDataAdapter.removeListener(ClientEvent.AccountData, listener);
|
||||
reject(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a key for encrypting secrets.
|
||||
*
|
||||
* @param algorithm - the algorithm used by the key.
|
||||
* @param opts - the options for the algorithm. The properties used
|
||||
* depend on the algorithm given.
|
||||
* @param keyId - the ID of the key. If not given, a random
|
||||
* ID will be generated.
|
||||
*
|
||||
* @returns An object with:
|
||||
* keyId: the ID of the key
|
||||
* keyInfo: details about the key (iv, mac, passphrase)
|
||||
*/
|
||||
public async addKey(
|
||||
algorithm: string,
|
||||
opts: AddSecretStorageKeyOpts = {},
|
||||
keyId?: string,
|
||||
): Promise<SecretStorageKeyObject> {
|
||||
if (algorithm !== SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
throw new Error(`Unknown key algorithm ${algorithm}`);
|
||||
}
|
||||
|
||||
const keyInfo = { algorithm } as SecretStorageKeyDescriptionAesV1;
|
||||
|
||||
if (opts.name) {
|
||||
keyInfo.name = opts.name;
|
||||
}
|
||||
|
||||
if (opts.passphrase) {
|
||||
keyInfo.passphrase = opts.passphrase;
|
||||
}
|
||||
if (opts.key) {
|
||||
const { iv, mac } = await calculateKeyCheck(opts.key);
|
||||
keyInfo.iv = iv;
|
||||
keyInfo.mac = mac;
|
||||
}
|
||||
|
||||
// Create a unique key id. XXX: this is racey.
|
||||
if (!keyId) {
|
||||
do {
|
||||
keyId = randomString(32);
|
||||
} while (
|
||||
await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescription>(
|
||||
`m.secret_storage.key.${keyId}`,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
await this.accountDataAdapter.setAccountData(`m.secret_storage.key.${keyId}`, keyInfo);
|
||||
|
||||
return {
|
||||
keyId,
|
||||
keyInfo,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the key information for a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns If the key was found, the return value is an array of
|
||||
* the form [keyId, keyInfo]. Otherwise, null is returned.
|
||||
* XXX: why is this an array when addKey returns an object?
|
||||
*/
|
||||
public async getKey(keyId?: string | null): Promise<SecretStorageKeyTuple | null> {
|
||||
if (!keyId) {
|
||||
keyId = await this.getDefaultKeyId();
|
||||
}
|
||||
if (!keyId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescriptionAesV1>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
return keyInfo ? [keyId, keyInfo] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether we have a key with a given ID.
|
||||
*
|
||||
* @param keyId - The ID of the key to check
|
||||
* for. Defaults to the default key ID if not provided.
|
||||
* @returns Whether we have the key.
|
||||
*/
|
||||
public async hasKey(keyId?: string): Promise<boolean> {
|
||||
const key = await this.getKey(keyId);
|
||||
return Boolean(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a key matches what we expect based on the key info
|
||||
*
|
||||
* @param key - the key to check
|
||||
* @param info - the key info
|
||||
*
|
||||
* @returns whether or not the key matches
|
||||
*/
|
||||
public async checkKey(key: Uint8Array, info: SecretStorageKeyDescriptionAesV1): Promise<boolean> {
|
||||
if (info.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (info.mac) {
|
||||
const { mac } = await calculateKeyCheck(key, info.iv);
|
||||
return trimTrailingEquals(info.mac) === trimTrailingEquals(mac);
|
||||
} else {
|
||||
// if we have no information, we have to assume the key is right
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
throw new Error("Unknown algorithm");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an encrypted secret on the server.
|
||||
*
|
||||
* Details of the encryption keys to be used must previously have been stored in account data
|
||||
* (for example, via {@link ServerSideSecretStorageImpl#addKey}. {@link SecretStorageCallbacks#getSecretStorageKey} will be called to obtain a secret storage
|
||||
* key to decrypt the secret.
|
||||
*
|
||||
* @param name - The name of the secret - i.e., the "event type" to be stored in the account data
|
||||
* @param secret - The secret contents.
|
||||
* @param keys - The IDs of the keys to use to encrypt the secret, or null/undefined to use the default key.
|
||||
*/
|
||||
public async store(name: string, secret: string, keys?: string[] | null): Promise<void> {
|
||||
const encrypted: Record<string, IEncryptedPayload> = {};
|
||||
|
||||
if (!keys) {
|
||||
const defaultKeyId = await this.getDefaultKeyId();
|
||||
if (!defaultKeyId) {
|
||||
throw new Error("No keys specified and no default key present");
|
||||
}
|
||||
keys = [defaultKeyId];
|
||||
}
|
||||
|
||||
if (keys.length === 0) {
|
||||
throw new Error("Zero keys given to encrypt with!");
|
||||
}
|
||||
|
||||
for (const keyId of keys) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescriptionAesV1>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
if (!keyInfo) {
|
||||
throw new Error("Unknown key: " + keyId);
|
||||
}
|
||||
|
||||
// encrypt secret, based on the algorithm
|
||||
if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
const keys = { [keyId]: keyInfo };
|
||||
const [, encryption] = await this.getSecretStorageKey(keys, name);
|
||||
encrypted[keyId] = await encryption.encrypt(secret);
|
||||
} else {
|
||||
logger.warn("unknown algorithm for secret storage key " + keyId + ": " + keyInfo.algorithm);
|
||||
// do nothing if we don't understand the encryption algorithm
|
||||
}
|
||||
}
|
||||
|
||||
// save encrypted secret
|
||||
await this.accountDataAdapter.setAccountData(name, { encrypted });
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a secret from storage, and decrypt it.
|
||||
*
|
||||
* {@link SecretStorageCallbacks#getSecretStorageKey} will be called to obtain a secret storage
|
||||
* key to decrypt the secret.
|
||||
*
|
||||
* @param name - the name of the secret - i.e., the "event type" stored in the account data
|
||||
*
|
||||
* @returns the decrypted contents of the secret, or "undefined" if `name` is not found in
|
||||
* the user's account data.
|
||||
*/
|
||||
public async get(name: string): Promise<string | undefined> {
|
||||
const secretInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretInfo>(name);
|
||||
if (!secretInfo) {
|
||||
return;
|
||||
}
|
||||
if (!secretInfo.encrypted) {
|
||||
throw new Error("Content is not encrypted!");
|
||||
}
|
||||
|
||||
// get possible keys to decrypt
|
||||
const keys: Record<string, SecretStorageKeyDescriptionAesV1> = {};
|
||||
for (const keyId of Object.keys(secretInfo.encrypted)) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescriptionAesV1>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
// only use keys we understand the encryption algorithm of
|
||||
if (keyInfo?.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
|
||||
keys[keyId] = keyInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(keys).length === 0) {
|
||||
throw new Error(
|
||||
`Could not decrypt ${name} because none of ` +
|
||||
`the keys it is encrypted with are for a supported algorithm`,
|
||||
);
|
||||
}
|
||||
|
||||
// fetch private key from app
|
||||
const [keyId, decryption] = await this.getSecretStorageKey(keys, name);
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
|
||||
return decryption.decrypt(encInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a secret is stored on the server.
|
||||
*
|
||||
* @param name - the name of the secret
|
||||
*
|
||||
* @returns map of key name to key info the secret is encrypted
|
||||
* with, or null if it is not present or not encrypted with a trusted
|
||||
* key
|
||||
*/
|
||||
public async isStored(name: string): Promise<Record<string, SecretStorageKeyDescriptionAesV1> | null> {
|
||||
// check if secret exists
|
||||
const secretInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretInfo>(name);
|
||||
if (!secretInfo?.encrypted) return null;
|
||||
|
||||
const ret: Record<string, SecretStorageKeyDescriptionAesV1> = {};
|
||||
|
||||
// filter secret encryption keys with supported algorithm
|
||||
for (const keyId of Object.keys(secretInfo.encrypted)) {
|
||||
// get key information from key storage
|
||||
const keyInfo = await this.accountDataAdapter.getAccountDataFromServer<SecretStorageKeyDescriptionAesV1>(
|
||||
"m.secret_storage.key." + keyId,
|
||||
);
|
||||
if (!keyInfo) continue;
|
||||
const encInfo = secretInfo.encrypted[keyId];
|
||||
|
||||
// only use keys we understand the encryption algorithm of
|
||||
if (keyInfo.algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
if (encInfo.iv && encInfo.ciphertext && encInfo.mac) {
|
||||
ret[keyId] = keyInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.keys(ret).length ? ret : null;
|
||||
}
|
||||
|
||||
private async getSecretStorageKey(
|
||||
keys: Record<string, SecretStorageKeyDescriptionAesV1>,
|
||||
name: string,
|
||||
): Promise<[string, Decryptors]> {
|
||||
if (!this.callbacks.getSecretStorageKey) {
|
||||
throw new Error("No getSecretStorageKey callback supplied");
|
||||
}
|
||||
|
||||
const returned = await this.callbacks.getSecretStorageKey({ keys }, name);
|
||||
|
||||
if (!returned) {
|
||||
throw new Error("getSecretStorageKey callback returned falsey");
|
||||
}
|
||||
if (returned.length < 2) {
|
||||
throw new Error("getSecretStorageKey callback returned invalid data");
|
||||
}
|
||||
|
||||
const [keyId, privateKey] = returned;
|
||||
if (!keys[keyId]) {
|
||||
throw new Error("App returned unknown key from getSecretStorageKey!");
|
||||
}
|
||||
|
||||
if (keys[keyId].algorithm === SECRET_STORAGE_ALGORITHM_V1_AES) {
|
||||
const decryption = {
|
||||
encrypt: function (secret: string): Promise<IEncryptedPayload> {
|
||||
return encryptAES(secret, privateKey, name);
|
||||
},
|
||||
decrypt: function (encInfo: IEncryptedPayload): Promise<string> {
|
||||
return decryptAES(encInfo, privateKey, name);
|
||||
},
|
||||
};
|
||||
return [keyId, decryption];
|
||||
} else {
|
||||
throw new Error("Unknown key type: " + keys[keyId].algorithm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** trim trailing instances of '=' from a string
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @param input - input string
|
||||
*/
|
||||
export function trimTrailingEquals(input: string): string {
|
||||
// according to Sonar and CodeQL, a regex such as /=+$/ is superlinear.
|
||||
// Not sure I believe it, but it's easy enough to work around.
|
||||
|
||||
// find the number of characters before the trailing =
|
||||
let i = input.length;
|
||||
while (i >= 1 && input.charCodeAt(i - 1) == 0x3d) i--;
|
||||
|
||||
// trim to the calculated length
|
||||
if (i < input.length) {
|
||||
return input.substring(0, i);
|
||||
} else {
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
+8
-22
@@ -85,30 +85,16 @@ class ExtensionE2EE implements Extension<ExtensionE2EERequest, ExtensionE2EEResp
|
||||
|
||||
public async onResponse(data: ExtensionE2EEResponse): Promise<void> {
|
||||
// Handle device list updates
|
||||
if (data["device_lists"]) {
|
||||
await this.crypto.handleDeviceListChanges(
|
||||
{
|
||||
oldSyncToken: "yep", // XXX need to do this so the device list changes get processed :(
|
||||
},
|
||||
data["device_lists"],
|
||||
);
|
||||
if (data.device_lists) {
|
||||
await this.crypto.processDeviceLists(data.device_lists);
|
||||
}
|
||||
|
||||
// Handle one_time_keys_count
|
||||
if (data["device_one_time_keys_count"]) {
|
||||
const currentCount = data["device_one_time_keys_count"].signed_curve25519 || 0;
|
||||
this.crypto.updateOneTimeKeyCount(currentCount);
|
||||
}
|
||||
if (data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"]) {
|
||||
// The presence of device_unused_fallback_key_types indicates that the
|
||||
// server supports fallback keys. If there's no unused
|
||||
// signed_curve25519 fallback key we need a new one.
|
||||
const unusedFallbackKeys =
|
||||
data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"];
|
||||
this.crypto.setNeedsNewFallback(
|
||||
Array.isArray(unusedFallbackKeys) && !unusedFallbackKeys.includes("signed_curve25519"),
|
||||
);
|
||||
}
|
||||
// Handle one_time_keys_count and unused_fallback_key_types
|
||||
await this.crypto.processKeyCounts(
|
||||
data.device_one_time_keys_count,
|
||||
data["device_unused_fallback_key_types"] || data["org.matrix.msc2732.device_unused_fallback_key_types"],
|
||||
);
|
||||
|
||||
this.crypto.onSyncCompleted({});
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -176,7 +176,7 @@ export interface IStore {
|
||||
/**
|
||||
* Save does nothing as there is no backing data store.
|
||||
*/
|
||||
save(force?: boolean): void;
|
||||
save(force?: boolean): Promise<void>;
|
||||
|
||||
/**
|
||||
* Startup does nothing.
|
||||
|
||||
+3
-1
@@ -324,7 +324,9 @@ export class MemoryStore implements IStore {
|
||||
* @param force - True to force a save (but the memory
|
||||
* store still can't save anything)
|
||||
*/
|
||||
public save(force: boolean): void {}
|
||||
public save(force: boolean): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
* Startup does nothing as this store doesn't require starting up.
|
||||
|
||||
+3
-1
@@ -189,7 +189,9 @@ export class StubStore implements IStore {
|
||||
/**
|
||||
* Save does nothing as there is no backing data store.
|
||||
*/
|
||||
public save(): void {}
|
||||
public save(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
/**
|
||||
* Startup does nothing.
|
||||
|
||||
@@ -132,7 +132,7 @@ interface IToDevice {
|
||||
events: IToDeviceEvent[];
|
||||
}
|
||||
|
||||
interface IDeviceLists {
|
||||
export interface IDeviceLists {
|
||||
changed?: string[];
|
||||
left?: string[];
|
||||
}
|
||||
|
||||
+8
-16
@@ -953,7 +953,7 @@ export class SyncApi {
|
||||
}
|
||||
|
||||
// tell databases that everything is now in a consistent state and can be saved.
|
||||
this.client.store.save();
|
||||
await this.client.store.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1515,8 +1515,8 @@ export class SyncApi {
|
||||
|
||||
// Handle device list updates
|
||||
if (data.device_lists) {
|
||||
if (this.syncOpts.crypto) {
|
||||
await this.syncOpts.crypto.handleDeviceListChanges(syncEventData, data.device_lists);
|
||||
if (this.syncOpts.cryptoCallbacks) {
|
||||
await this.syncOpts.cryptoCallbacks.processDeviceLists(data.device_lists);
|
||||
} else {
|
||||
// FIXME if we *don't* have a crypto module, we still need to
|
||||
// invalidate the device lists. But that would require a
|
||||
@@ -1524,19 +1524,11 @@ export class SyncApi {
|
||||
}
|
||||
}
|
||||
|
||||
// Handle one_time_keys_count
|
||||
if (data.device_one_time_keys_count) {
|
||||
const map = new Map<string, number>(Object.entries(data.device_one_time_keys_count));
|
||||
this.syncOpts.cryptoCallbacks?.preprocessOneTimeKeyCounts(map);
|
||||
}
|
||||
if (data.device_unused_fallback_key_types || data["org.matrix.msc2732.device_unused_fallback_key_types"]) {
|
||||
// The presence of device_unused_fallback_key_types indicates that the
|
||||
// server supports fallback keys. If there's no unused
|
||||
// signed_curve25519 fallback key we need a new one.
|
||||
const unusedFallbackKeys =
|
||||
data.device_unused_fallback_key_types || data["org.matrix.msc2732.device_unused_fallback_key_types"];
|
||||
this.syncOpts.cryptoCallbacks?.preprocessUnusedFallbackKeys(new Set<string>(unusedFallbackKeys || null));
|
||||
}
|
||||
// Handle one_time_keys_count and unused fallback keys
|
||||
this.syncOpts.cryptoCallbacks?.processKeyCounts(
|
||||
data.device_one_time_keys_count,
|
||||
data.device_unused_fallback_key_types ?? data["org.matrix.msc2732.device_unused_fallback_key_types"],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-2
@@ -720,7 +720,7 @@ function processMapToObjectValue(value: any): any {
|
||||
* Recursively converts Maps to plain objects.
|
||||
* Also supports sub-lists of Maps.
|
||||
*/
|
||||
export function recursiveMapToObject(map: Map<any, any>): any {
|
||||
export function recursiveMapToObject(map: Map<any, any>): Record<any, any> {
|
||||
const targetMap = new Map();
|
||||
|
||||
for (const [key, value] of map) {
|
||||
@@ -734,7 +734,7 @@ export function unsafeProp<K extends keyof any | undefined>(prop: K): boolean {
|
||||
return prop === "__proto__" || prop === "prototype" || prop === "constructor";
|
||||
}
|
||||
|
||||
export function safeSet<K extends keyof any>(obj: Record<any, any>, prop: K, value: any): void {
|
||||
export function safeSet<O extends Record<any, any>, K extends keyof O>(obj: O, prop: K, value: O[K]): void {
|
||||
if (unsafeProp(prop)) {
|
||||
throw new Error("Trying to modify prototype or constructor");
|
||||
}
|
||||
|
||||
+10
-2
@@ -152,6 +152,10 @@ export enum CallEvent {
|
||||
DataChannel = "datachannel",
|
||||
|
||||
SendVoipEvent = "send_voip_event",
|
||||
|
||||
// When the call instantiates its peer connection
|
||||
// For apps that want to access the underlying peer connection, eg for debugging
|
||||
PeerConnectionCreated = "peer_connection_created",
|
||||
}
|
||||
|
||||
export enum CallErrorCode {
|
||||
@@ -325,6 +329,7 @@ export type CallEventHandlerMap = {
|
||||
/* @deprecated */
|
||||
[CallEvent.HoldUnhold]: (onHold: boolean) => void;
|
||||
[CallEvent.SendVoipEvent]: (event: VoipEvent, call: MatrixCall) => void;
|
||||
[CallEvent.PeerConnectionCreated]: (peerConn: RTCPeerConnection, call: MatrixCall) => void;
|
||||
};
|
||||
|
||||
// The key of the transceiver map (purpose + media type, separated by ':')
|
||||
@@ -951,6 +956,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
|
||||
}
|
||||
|
||||
this.peerConn = this.createPeerConnection();
|
||||
this.emit(CallEvent.PeerConnectionCreated, this.peerConn, this);
|
||||
// we must set the party ID before await-ing on anything: the call event
|
||||
// handler will start giving us more call events (eg. candidates) so if
|
||||
// we haven't set the party ID, we'll ignore them.
|
||||
@@ -2309,7 +2315,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
|
||||
* [96685:23:0518/162603.933430:ERROR:sdp_offer_answer.cc(4302)] Failed to set local video description recv parameters for m-section with mid='2'. (INVALID_PARAMETER)
|
||||
*/
|
||||
private getRidOfRTXCodecs(): void {
|
||||
// RTCRtpReceiver.getCapabilities and RTCRtpSender.getCapabilities don't seem to be supported on FF
|
||||
// RTCRtpReceiver.getCapabilities and RTCRtpSender.getCapabilities don't seem to be supported on FF before v113
|
||||
if (!RTCRtpReceiver.getCapabilities || !RTCRtpSender.getCapabilities) return;
|
||||
|
||||
const recvCodecs = RTCRtpReceiver.getCapabilities("video")!.codecs;
|
||||
@@ -2326,7 +2332,8 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
|
||||
const screenshareVideoTransceiver = this.transceivers.get(
|
||||
getTransceiverKey(SDPStreamMetadataPurpose.Screenshare, "video"),
|
||||
);
|
||||
if (screenshareVideoTransceiver) screenshareVideoTransceiver.setCodecPreferences(codecs);
|
||||
// setCodecPreferences isn't supported on FF (as of v113)
|
||||
screenshareVideoTransceiver?.setCodecPreferences?.(codecs);
|
||||
}
|
||||
|
||||
private onNegotiationNeeded = async (): Promise<void> => {
|
||||
@@ -2785,6 +2792,7 @@ export class MatrixCall extends TypedEventEmitter<CallEvent, CallEventHandlerMap
|
||||
// create the peer connection now so it can be gathering candidates while we get user
|
||||
// media (assuming a candidate pool size is configured)
|
||||
this.peerConn = this.createPeerConnection();
|
||||
this.emit(CallEvent.PeerConnectionCreated, this.peerConn, this);
|
||||
this.gotCallFeedsForInvite(callFeeds, requestScreenshareFeed);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import { GroupCallEventHandlerEvent } from "./groupCallEventHandler";
|
||||
import { IScreensharingOpts } from "./mediaHandler";
|
||||
import { mapsEqual } from "../utils";
|
||||
import { GroupCallStats } from "./stats/groupCallStats";
|
||||
import { ByteSentStatsReport, ConnectionStatsReport, StatsReport } from "./stats/statsReport";
|
||||
import { ByteSentStatsReport, ConnectionStatsReport, StatsReport, SummaryStatsReport } from "./stats/statsReport";
|
||||
|
||||
export enum GroupCallIntent {
|
||||
Ring = "m.ring",
|
||||
@@ -95,11 +95,13 @@ export type GroupCallEventHandlerMap = {
|
||||
export enum GroupCallStatsReportEvent {
|
||||
ConnectionStats = "GroupCall.connection_stats",
|
||||
ByteSentStats = "GroupCall.byte_sent_stats",
|
||||
SummaryStats = "GroupCall.summary_stats",
|
||||
}
|
||||
|
||||
export type GroupCallStatsReportEventHandlerMap = {
|
||||
[GroupCallStatsReportEvent.ConnectionStats]: (report: GroupCallStatsReport<ConnectionStatsReport>) => void;
|
||||
[GroupCallStatsReportEvent.ByteSentStats]: (report: GroupCallStatsReport<ByteSentStatsReport>) => void;
|
||||
[GroupCallStatsReportEvent.SummaryStats]: (report: GroupCallStatsReport<SummaryStatsReport>) => void;
|
||||
};
|
||||
|
||||
export enum GroupCallErrorCode {
|
||||
@@ -108,7 +110,7 @@ export enum GroupCallErrorCode {
|
||||
PlaceCallFailed = "place_call_failed",
|
||||
}
|
||||
|
||||
export interface GroupCallStatsReport<T extends ConnectionStatsReport | ByteSentStatsReport> {
|
||||
export interface GroupCallStatsReport<T extends ConnectionStatsReport | ByteSentStatsReport | SummaryStatsReport> {
|
||||
report: T;
|
||||
}
|
||||
|
||||
@@ -264,18 +266,21 @@ export class GroupCall extends TypedEventEmitter<
|
||||
this.stats = new GroupCallStats(this.groupCallId, userID);
|
||||
this.stats.reports.on(StatsReport.CONNECTION_STATS, this.onConnectionStats);
|
||||
this.stats.reports.on(StatsReport.BYTE_SENT_STATS, this.onByteSentStats);
|
||||
this.stats.reports.on(StatsReport.SUMMARY_STATS, this.onSummaryStats);
|
||||
}
|
||||
|
||||
private onConnectionStats = (report: ConnectionStatsReport): void => {
|
||||
// @TODO: Implement data argumentation
|
||||
this.emit(GroupCallStatsReportEvent.ConnectionStats, { report });
|
||||
};
|
||||
|
||||
private onByteSentStats = (report: ByteSentStatsReport): void => {
|
||||
// @TODO: Implement data argumentation
|
||||
this.emit(GroupCallStatsReportEvent.ByteSentStats, { report });
|
||||
};
|
||||
|
||||
private onSummaryStats = (report: SummaryStatsReport): void => {
|
||||
this.emit(GroupCallStatsReportEvent.SummaryStats, { report });
|
||||
};
|
||||
|
||||
public async create(): Promise<GroupCall> {
|
||||
this.creationTs = Date.now();
|
||||
this.client.groupCallEventHandler!.groupCalls.set(this.room.roomId, this);
|
||||
|
||||
@@ -15,11 +15,14 @@ limitations under the License.
|
||||
*/
|
||||
import { StatsReportGatherer } from "./statsReportGatherer";
|
||||
import { StatsReportEmitter } from "./statsReportEmitter";
|
||||
import { SummaryStats } from "./summaryStats";
|
||||
import { SummaryStatsReporter } from "./summaryStatsReporter";
|
||||
|
||||
export class GroupCallStats {
|
||||
private timer: undefined | ReturnType<typeof setTimeout>;
|
||||
private readonly gatherers: Map<string, StatsReportGatherer> = new Map<string, StatsReportGatherer>();
|
||||
public readonly reports = new StatsReportEmitter();
|
||||
private readonly summaryStatsReporter = new SummaryStatsReporter(this.reports);
|
||||
|
||||
public constructor(private groupCallId: string, private userId: string, private interval: number = 10000) {}
|
||||
|
||||
@@ -59,6 +62,11 @@ export class GroupCallStats {
|
||||
}
|
||||
|
||||
private processStats(): void {
|
||||
this.gatherers.forEach((c) => c.processStats(this.groupCallId, this.userId));
|
||||
const summary: Promise<SummaryStats>[] = [];
|
||||
this.gatherers.forEach((c) => {
|
||||
summary.push(c.processStats(this.groupCallId, this.userId));
|
||||
});
|
||||
|
||||
Promise.all(summary).then((s: Awaited<SummaryStats>[]) => this.summaryStatsReporter.build(s));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,4 +68,10 @@ export class MediaTrackHandler {
|
||||
//@TODO implement this right.. Check how many layer configured
|
||||
return 3;
|
||||
}
|
||||
|
||||
public getTransceiverByTrackId(trackId: TrackId): RTCRtpTransceiver | undefined {
|
||||
return this.pc.getTransceivers().find((t) => {
|
||||
return t.receiver.track.id === trackId || (t.sender.track !== null && t.sender.track.id === trackId);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,11 @@ export class MediaTrackStats {
|
||||
private bitrate: Bitrate = { download: 0, upload: 0 };
|
||||
private resolution: Resolution = { width: -1, height: -1 };
|
||||
private framerate = 0;
|
||||
private jitter = 0;
|
||||
private codec = "";
|
||||
private isAlive = true;
|
||||
private isMuted = false;
|
||||
private isEnabled = true;
|
||||
|
||||
public constructor(
|
||||
public readonly trackId: TrackId,
|
||||
@@ -101,4 +105,51 @@ export class MediaTrackStats {
|
||||
public resetBitrate(): void {
|
||||
this.bitrate = { download: 0, upload: 0 };
|
||||
}
|
||||
|
||||
public set alive(isAlive: boolean) {
|
||||
this.isAlive = isAlive;
|
||||
}
|
||||
|
||||
/**
|
||||
* A MediaTrackState is alive if the corresponding MediaStreamTrack track bound to a transceiver and the
|
||||
* MediaStreamTrack is in state MediaStreamTrack.readyState === live
|
||||
*/
|
||||
public get alive(): boolean {
|
||||
return this.isAlive;
|
||||
}
|
||||
|
||||
public set muted(isMuted: boolean) {
|
||||
this.isMuted = isMuted;
|
||||
}
|
||||
|
||||
/**
|
||||
* A MediaTrackState.isMuted corresponding to MediaStreamTrack.muted.
|
||||
* But these values only match if MediaTrackState.isAlive.
|
||||
*/
|
||||
public get muted(): boolean {
|
||||
return this.isMuted;
|
||||
}
|
||||
|
||||
public set enabled(isEnabled: boolean) {
|
||||
this.isEnabled = isEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* A MediaTrackState.isEnabled corresponding to MediaStreamTrack.enabled.
|
||||
* But these values only match if MediaTrackState.isAlive.
|
||||
*/
|
||||
public get enabled(): boolean {
|
||||
return this.isEnabled;
|
||||
}
|
||||
|
||||
public setJitter(jitter: number): void {
|
||||
this.jitter = jitter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Jitter in milliseconds
|
||||
*/
|
||||
public getJitter(): number {
|
||||
return this.jitter;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ limitations under the License.
|
||||
*/
|
||||
import { TrackID } from "../statsReport";
|
||||
import { MediaTrackStats } from "./mediaTrackStats";
|
||||
import { MediaTrackHandler } from "./mediaTrackHandler";
|
||||
import { MediaTrackHandler, TrackId } from "./mediaTrackHandler";
|
||||
import { MediaSsrcHandler } from "./mediaSsrcHandler";
|
||||
|
||||
export class MediaTrackStatsHandler {
|
||||
@@ -83,4 +83,8 @@ export class MediaTrackStatsHandler {
|
||||
public getTrack2stats(): Map<TrackID, MediaTrackStats> {
|
||||
return this.track2stats;
|
||||
}
|
||||
|
||||
public findTransceiverByTrackId(trackID: TrackId): undefined | RTCRtpTransceiver {
|
||||
return this.mediaTrackHandler.getTransceiverByTrackId(trackID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import { Resolution } from "./media/mediaTrackStats";
|
||||
export enum StatsReport {
|
||||
CONNECTION_STATS = "StatsReport.connection_stats",
|
||||
BYTE_SENT_STATS = "StatsReport.byte_sent_stats",
|
||||
SUMMARY_STATS = "StatsReport.summary_stats",
|
||||
}
|
||||
|
||||
export type TrackID = string;
|
||||
@@ -37,6 +38,7 @@ export interface ConnectionStatsReport {
|
||||
resolution: ResolutionMap;
|
||||
framerate: FramerateMap;
|
||||
codec: CodecMap;
|
||||
jitter: Map<TrackID, number>;
|
||||
transport: TransportStats[];
|
||||
}
|
||||
|
||||
@@ -54,3 +56,16 @@ export interface CodecMap {
|
||||
local: Map<TrackID, string>;
|
||||
remote: Map<TrackID, string>;
|
||||
}
|
||||
|
||||
export interface SummaryStatsReport {
|
||||
/**
|
||||
* Aggregated the information for percentage of received media
|
||||
*
|
||||
* This measure whether the current user receive data from a call participants.
|
||||
* As soon as a participant sends at least a byte media to this user, this counts as one measurement unit.
|
||||
* The units of measure divided by the total number of participants is a value between 0 and 1.
|
||||
*/
|
||||
percentageReceivedMedia: number;
|
||||
percentageReceivedAudioMedia: number;
|
||||
percentageReceivedVideoMedia: number;
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ export class StatsReportBuilder {
|
||||
};
|
||||
const framerates: FramerateMap = { local: new Map<TrackID, number>(), remote: new Map<TrackID, number>() };
|
||||
const codecs: CodecMap = { local: new Map<TrackID, string>(), remote: new Map<TrackID, string>() };
|
||||
const jitter = new Map<TrackID, number>();
|
||||
|
||||
let audioBitrateDownload = 0;
|
||||
let audioBitrateUpload = 0;
|
||||
@@ -67,6 +68,9 @@ export class StatsReportBuilder {
|
||||
resolutions[trackStats.getType()].set(trackId, trackStats.getResolution());
|
||||
framerates[trackStats.getType()].set(trackId, trackStats.getFramerate());
|
||||
codecs[trackStats.getType()].set(trackId, trackStats.getCodec());
|
||||
if (trackStats.getType() === "remote") {
|
||||
jitter.set(trackId, trackStats.getJitter());
|
||||
}
|
||||
|
||||
trackStats.resetBitrate();
|
||||
}
|
||||
@@ -97,6 +101,7 @@ export class StatsReportBuilder {
|
||||
report.framerate = framerates;
|
||||
report.resolution = resolutions;
|
||||
report.codec = codecs;
|
||||
report.jitter = jitter;
|
||||
return report;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,12 @@ limitations under the License.
|
||||
*/
|
||||
|
||||
import { TypedEventEmitter } from "../../models/typed-event-emitter";
|
||||
import { ByteSentStatsReport, ConnectionStatsReport, StatsReport } from "./statsReport";
|
||||
import { ByteSentStatsReport, ConnectionStatsReport, StatsReport, SummaryStatsReport } from "./statsReport";
|
||||
|
||||
export type StatsReportHandlerMap = {
|
||||
[StatsReport.BYTE_SENT_STATS]: (report: ByteSentStatsReport) => void;
|
||||
[StatsReport.CONNECTION_STATS]: (report: ConnectionStatsReport) => void;
|
||||
[StatsReport.SUMMARY_STATS]: (report: SummaryStatsReport) => void;
|
||||
};
|
||||
|
||||
export class StatsReportEmitter extends TypedEventEmitter<StatsReport, StatsReportHandlerMap> {
|
||||
@@ -30,4 +31,8 @@ export class StatsReportEmitter extends TypedEventEmitter<StatsReport, StatsRepo
|
||||
public emitConnectionStatsReport(report: ConnectionStatsReport): void {
|
||||
this.emit(StatsReport.CONNECTION_STATS, report);
|
||||
}
|
||||
|
||||
public emitSummaryStatsReport(report: SummaryStatsReport): void {
|
||||
this.emit(StatsReport.SUMMARY_STATS, report);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import { MediaTrackStatsHandler } from "./media/mediaTrackStatsHandler";
|
||||
import { TrackStatsReporter } from "./trackStatsReporter";
|
||||
import { StatsReportBuilder } from "./statsReportBuilder";
|
||||
import { StatsValueFormatter } from "./statsValueFormatter";
|
||||
import { SummaryStats } from "./summaryStats";
|
||||
|
||||
export class StatsReportGatherer {
|
||||
private isActive = true;
|
||||
@@ -47,7 +48,14 @@ export class StatsReportGatherer {
|
||||
this.trackStats = new MediaTrackStatsHandler(new MediaSsrcHandler(), new MediaTrackHandler(pc));
|
||||
}
|
||||
|
||||
public async processStats(groupCallId: string, localUserId: string): Promise<boolean> {
|
||||
public async processStats(groupCallId: string, localUserId: string): Promise<SummaryStats> {
|
||||
const summary = {
|
||||
receivedMedia: 0,
|
||||
receivedAudioMedia: 0,
|
||||
receivedVideoMedia: 0,
|
||||
audioTrackSummary: { count: 0, muted: 0 },
|
||||
videoTrackSummary: { count: 0, muted: 0 },
|
||||
} as SummaryStats;
|
||||
if (this.isActive) {
|
||||
const statsPromise = this.pc.getStats();
|
||||
if (typeof statsPromise?.then === "function") {
|
||||
@@ -59,20 +67,30 @@ export class StatsReportGatherer {
|
||||
this.processStatsReport(groupCallId, localUserId);
|
||||
} catch (error) {
|
||||
this.isActive = false;
|
||||
return false;
|
||||
return summary;
|
||||
}
|
||||
|
||||
this.previousStatsReport = this.currentStatsReport;
|
||||
return true;
|
||||
summary.receivedMedia = this.connectionStats.bitrate.download;
|
||||
summary.receivedAudioMedia = this.connectionStats.bitrate.audio?.download || 0;
|
||||
summary.receivedVideoMedia = this.connectionStats.bitrate.video?.download || 0;
|
||||
const trackSummary = TrackStatsReporter.buildTrackSummary(
|
||||
Array.from(this.trackStats.getTrack2stats().values()),
|
||||
);
|
||||
return {
|
||||
...summary,
|
||||
audioTrackSummary: trackSummary.audioTrackSummary,
|
||||
videoTrackSummary: trackSummary.videoTrackSummary,
|
||||
};
|
||||
})
|
||||
.catch((error) => {
|
||||
this.handleError(error);
|
||||
return false;
|
||||
return summary;
|
||||
});
|
||||
}
|
||||
this.isActive = false;
|
||||
}
|
||||
return Promise.resolve(false);
|
||||
return Promise.resolve(summary);
|
||||
}
|
||||
|
||||
private processStatsReport(groupCallId: string, localUserId: string): void {
|
||||
@@ -117,6 +135,9 @@ export class StatsReportGatherer {
|
||||
if (before) {
|
||||
TrackStatsReporter.buildBitrateReceived(trackStats, now, before);
|
||||
}
|
||||
const ts = this.trackStats.findTransceiverByTrackId(trackStats.trackId);
|
||||
TrackStatsReporter.setTrackStatsState(trackStats, ts);
|
||||
TrackStatsReporter.buildJitter(trackStats, now);
|
||||
} else if (before) {
|
||||
byteSentStats.set(trackStats.trackId, StatsValueFormatter.getNonNegativeValue(now.bytesSent));
|
||||
TrackStatsReporter.buildBitrateSend(trackStats, now, before);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
export interface SummaryStats {
|
||||
receivedMedia: number;
|
||||
receivedAudioMedia: number;
|
||||
receivedVideoMedia: number;
|
||||
audioTrackSummary: TrackSummary;
|
||||
videoTrackSummary: TrackSummary;
|
||||
}
|
||||
|
||||
export interface TrackSummary {
|
||||
count: number;
|
||||
muted: number;
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
import { StatsReportEmitter } from "./statsReportEmitter";
|
||||
import { SummaryStats } from "./summaryStats";
|
||||
import { SummaryStatsReport } from "./statsReport";
|
||||
|
||||
export class SummaryStatsReporter {
|
||||
public constructor(private emitter: StatsReportEmitter) {}
|
||||
|
||||
public build(summary: SummaryStats[]): void {
|
||||
const entirety = summary.length;
|
||||
if (entirety === 0) {
|
||||
return;
|
||||
}
|
||||
let receivedMedia = 0;
|
||||
let receivedVideoMedia = 0;
|
||||
let receivedAudioMedia = 0;
|
||||
|
||||
summary.forEach((stats) => {
|
||||
let hasReceivedAudio = false;
|
||||
let hasReceivedVideo = false;
|
||||
if (stats.receivedAudioMedia > 0) {
|
||||
receivedAudioMedia++;
|
||||
hasReceivedAudio = true;
|
||||
}
|
||||
if (stats.receivedVideoMedia > 0) {
|
||||
receivedVideoMedia++;
|
||||
hasReceivedVideo = true;
|
||||
} else {
|
||||
if (
|
||||
stats.videoTrackSummary.muted > 0 &&
|
||||
stats.videoTrackSummary.muted === stats.videoTrackSummary.count
|
||||
) {
|
||||
receivedVideoMedia++;
|
||||
hasReceivedVideo = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (stats.receivedMedia > 0 && hasReceivedVideo && hasReceivedAudio) {
|
||||
receivedMedia++;
|
||||
}
|
||||
});
|
||||
|
||||
const report = {
|
||||
percentageReceivedMedia: Math.round((receivedMedia / entirety) * 100) / 100,
|
||||
percentageReceivedVideoMedia: Math.round((receivedVideoMedia / entirety) * 100) / 100,
|
||||
percentageReceivedAudioMedia: Math.round((receivedAudioMedia / entirety) * 100) / 100,
|
||||
} as SummaryStatsReport;
|
||||
this.emitter.emitSummaryStatsReport(report);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { MediaTrackStats } from "./media/mediaTrackStats";
|
||||
import { StatsValueFormatter } from "./statsValueFormatter";
|
||||
import { TrackSummary } from "./summaryStats";
|
||||
|
||||
export class TrackStatsReporter {
|
||||
public static buildFramerateResolution(trackStats: MediaTrackStats, now: any): void {
|
||||
@@ -108,10 +109,62 @@ export class TrackStatsReporter {
|
||||
let bitrateKbps = 0;
|
||||
|
||||
if (timeMs > 0) {
|
||||
// TODO is there any reason to round here?
|
||||
bitrateKbps = Math.round((bytesProcessed * 8) / timeMs);
|
||||
}
|
||||
|
||||
return bitrateKbps;
|
||||
}
|
||||
|
||||
public static setTrackStatsState(trackStats: MediaTrackStats, transceiver: RTCRtpTransceiver | undefined): void {
|
||||
if (transceiver === undefined) {
|
||||
trackStats.alive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const track = trackStats.getType() === "remote" ? transceiver.receiver.track : transceiver?.sender?.track;
|
||||
if (track === undefined || track === null) {
|
||||
trackStats.alive = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (track.readyState === "ended") {
|
||||
trackStats.alive = false;
|
||||
return;
|
||||
}
|
||||
trackStats.muted = track.muted;
|
||||
trackStats.enabled = track.enabled;
|
||||
trackStats.alive = true;
|
||||
}
|
||||
|
||||
public static buildTrackSummary(trackStatsList: MediaTrackStats[]): {
|
||||
audioTrackSummary: TrackSummary;
|
||||
videoTrackSummary: TrackSummary;
|
||||
} {
|
||||
const audioTrackSummary = { count: 0, muted: 0 };
|
||||
const videoTrackSummary = { count: 0, muted: 0 };
|
||||
trackStatsList
|
||||
.filter((t) => t.getType() === "remote")
|
||||
.forEach((stats) => {
|
||||
const trackSummary = stats.kind === "video" ? videoTrackSummary : audioTrackSummary;
|
||||
trackSummary.count++;
|
||||
if (stats.alive && stats.muted) {
|
||||
trackSummary.muted++;
|
||||
}
|
||||
});
|
||||
return { audioTrackSummary, videoTrackSummary };
|
||||
}
|
||||
|
||||
public static buildJitter(trackStats: MediaTrackStats, statsReport: any): void {
|
||||
if (statsReport.type !== "inbound-rtp") {
|
||||
return;
|
||||
}
|
||||
|
||||
const jitterStr = statsReport?.jitter;
|
||||
if (jitterStr !== undefined) {
|
||||
const jitter = StatsValueFormatter.getNonNegativeValue(jitterStr);
|
||||
trackStats.setJitter(Math.round(jitter * 1000));
|
||||
} else {
|
||||
trackStats.setJitter(-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user