2018-06-15 12:33:07 +00:00
|
|
|
/*
|
2021-06-22 16:23:13 +00:00
|
|
|
Copyright 2018 - 2021 The Matrix.org Foundation C.I.C.
|
2018-06-15 12:33:07 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-08-07 08:24:58 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2022-06-07 19:08:36 +00:00
|
|
|
import { Error as ErrorEvent } from "@matrix-org/analytics-events/types/typescript/Error";
|
2024-04-17 12:36:01 +00:00
|
|
|
import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";
|
2022-01-19 19:31:43 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { PosthogAnalytics } from "./PosthogAnalytics";
|
2021-06-22 16:23:13 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
export class DecryptionFailure {
|
2021-06-22 16:23:13 +00:00
|
|
|
public readonly ts: number;
|
|
|
|
|
2024-01-02 18:56:39 +00:00
|
|
|
public constructor(
|
|
|
|
public readonly failedEventId: string,
|
2024-04-17 12:36:01 +00:00
|
|
|
public readonly errorCode: DecryptionFailureCode,
|
2024-01-02 18:56:39 +00:00
|
|
|
) {
|
2018-06-15 12:33:07 +00:00
|
|
|
this.ts = Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 12:36:01 +00:00
|
|
|
type ErrorCode = ErrorEvent["name"];
|
2022-05-05 03:46:03 +00:00
|
|
|
type TrackingFn = (count: number, trackedErrCode: ErrorCode, rawError: string) => void;
|
2024-04-17 12:36:01 +00:00
|
|
|
export type ErrCodeMapFn = (errcode: DecryptionFailureCode) => ErrorCode;
|
2021-06-22 16:23:13 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
export class DecryptionFailureTracker {
|
2022-12-12 11:24:14 +00:00
|
|
|
private static internalInstance = new DecryptionFailureTracker(
|
|
|
|
(total, errorCode, rawError) => {
|
|
|
|
for (let i = 0; i < total; i++) {
|
|
|
|
PosthogAnalytics.instance.trackEvent<ErrorEvent>({
|
|
|
|
eventName: "Error",
|
|
|
|
domain: "E2EE",
|
|
|
|
name: errorCode,
|
|
|
|
context: `mxc_crypto_error_type_${rawError}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(errorCode) => {
|
|
|
|
// Map JS-SDK error codes to tracker codes for aggregation
|
|
|
|
switch (errorCode) {
|
2024-04-17 12:36:01 +00:00
|
|
|
case DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID:
|
2022-12-12 11:24:14 +00:00
|
|
|
return "OlmKeysNotSentError";
|
2024-04-17 12:36:01 +00:00
|
|
|
case DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX:
|
2022-12-12 11:24:14 +00:00
|
|
|
return "OlmIndexError";
|
2024-04-17 12:36:01 +00:00
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_NO_KEY_BACKUP:
|
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_BACKUP_UNCONFIGURED:
|
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_WORKING_BACKUP:
|
|
|
|
return "HistoricalMessage";
|
2024-04-29 17:18:57 +00:00
|
|
|
case DecryptionFailureCode.HISTORICAL_MESSAGE_USER_NOT_JOINED:
|
|
|
|
return "ExpectedDueToMembership";
|
2022-12-12 11:24:14 +00:00
|
|
|
default:
|
|
|
|
return "UnknownError";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2022-01-19 19:31:43 +00:00
|
|
|
|
|
|
|
// Map of event IDs to DecryptionFailure items.
|
|
|
|
public failures: Map<string, DecryptionFailure> = new Map();
|
|
|
|
|
|
|
|
// Set of event IDs that have been visible to the user.
|
|
|
|
public visibleEvents: Set<string> = new Set();
|
|
|
|
|
|
|
|
// Map of visible event IDs to `DecryptionFailure`s. Every
|
|
|
|
// `CHECK_INTERVAL_MS`, this map is checked for failures that
|
|
|
|
// happened > `GRACE_PERIOD_MS` ago. Those that did are
|
|
|
|
// accumulated in `failureCounts`.
|
|
|
|
public visibleFailures: Map<string, DecryptionFailure> = new Map();
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2024-04-17 12:36:01 +00:00
|
|
|
/**
|
|
|
|
* A histogram of the number of failures that will be tracked at the next tracking
|
|
|
|
* interval, split by failure error code.
|
|
|
|
*/
|
|
|
|
private failureCounts: Map<DecryptionFailureCode, number> = new Map();
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-06-15 13:48:20 +00:00
|
|
|
// Event IDs of failures that were tracked previously
|
2022-01-19 19:31:43 +00:00
|
|
|
public trackedEvents: Set<string> = new Set();
|
2018-06-15 13:48:20 +00:00
|
|
|
|
2018-06-15 16:58:43 +00:00
|
|
|
// Set to an interval ID when `start` is called
|
2023-02-03 15:27:47 +00:00
|
|
|
public checkInterval: number | null = null;
|
|
|
|
public trackInterval: number | null = null;
|
2018-06-15 16:58:43 +00:00
|
|
|
|
2018-06-28 14:03:47 +00:00
|
|
|
// Spread the load on `Analytics` by tracking at a low frequency, `TRACK_INTERVAL_MS`.
|
2022-12-16 12:29:59 +00:00
|
|
|
public static TRACK_INTERVAL_MS = 60000;
|
2018-06-15 12:33:07 +00:00
|
|
|
|
|
|
|
// Call `checkFailures` every `CHECK_INTERVAL_MS`.
|
2024-02-29 15:29:59 +00:00
|
|
|
public static CHECK_INTERVAL_MS = 40000;
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
// Give events a chance to be decrypted by waiting `GRACE_PERIOD_MS` before counting
|
|
|
|
// the failure in `failureCounts`.
|
2024-02-29 15:29:59 +00:00
|
|
|
public static GRACE_PERIOD_MS = 30000;
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
/**
|
|
|
|
* Create a new DecryptionFailureTracker.
|
|
|
|
*
|
|
|
|
* Call `eventDecrypted(event, err)` on this instance when an event is decrypted.
|
|
|
|
*
|
|
|
|
* Call `start()` to start the tracker, and `stop()` to stop tracking.
|
|
|
|
*
|
|
|
|
* @param {function} fn The tracking function, which will be called when failures
|
|
|
|
* are tracked. The function should have a signature `(count, trackedErrorCode) => {...}`,
|
2024-04-17 12:36:01 +00:00
|
|
|
* where `count` is the number of failures and `errorCode` matches the output of `errorCodeMapFn`.
|
|
|
|
*
|
|
|
|
* @param {function} errorCodeMapFn The function used to map decryption failure reason codes to the
|
|
|
|
* `trackedErrorCode`.
|
2018-07-05 12:54:44 +00:00
|
|
|
*/
|
2024-01-02 18:56:39 +00:00
|
|
|
private constructor(
|
|
|
|
private readonly fn: TrackingFn,
|
|
|
|
private readonly errorCodeMapFn: ErrCodeMapFn,
|
|
|
|
) {
|
2022-12-12 11:24:14 +00:00
|
|
|
if (!fn || typeof fn !== "function") {
|
|
|
|
throw new Error("DecryptionFailureTracker requires tracking function");
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
if (typeof errorCodeMapFn !== "function") {
|
|
|
|
throw new Error("DecryptionFailureTracker second constructor argument should be a function");
|
2018-07-05 12:54:44 +00:00
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
public static get instance(): DecryptionFailureTracker {
|
|
|
|
return DecryptionFailureTracker.internalInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadTrackedEvents() {
|
|
|
|
// this.trackedEvents = new Set(JSON.parse(localStorage.getItem('mx-decryption-failure-event-ids')) || []);
|
2018-06-15 16:08:11 +00:00
|
|
|
// }
|
2018-06-15 14:26:53 +00:00
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
// saveTrackedEvents() {
|
|
|
|
// localStorage.setItem('mx-decryption-failure-event-ids', JSON.stringify([...this.trackedEvents]));
|
2018-06-15 16:08:11 +00:00
|
|
|
// }
|
2018-06-15 14:26:53 +00:00
|
|
|
|
2024-04-17 12:36:01 +00:00
|
|
|
public eventDecrypted(e: MatrixEvent): void {
|
|
|
|
// for now we only track megolm decryption failures
|
2022-06-30 07:55:05 +00:00
|
|
|
if (e.getWireContent().algorithm != "m.megolm.v1.aes-sha2") {
|
|
|
|
return;
|
|
|
|
}
|
2024-04-17 12:36:01 +00:00
|
|
|
|
|
|
|
const errCode = e.decryptionFailureReason;
|
|
|
|
if (errCode !== null) {
|
|
|
|
this.addDecryptionFailure(new DecryptionFailure(e.getId()!, errCode));
|
2018-06-15 12:33:07 +00:00
|
|
|
} else {
|
|
|
|
// Could be an event in the failures, remove it
|
|
|
|
this.removeDecryptionFailuresForEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
public addVisibleEvent(e: MatrixEvent): void {
|
2023-02-16 17:21:44 +00:00
|
|
|
const eventId = e.getId()!;
|
2022-01-19 19:31:43 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
if (this.trackedEvents.has(eventId)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:31:43 +00:00
|
|
|
|
|
|
|
this.visibleEvents.add(eventId);
|
|
|
|
if (this.failures.has(eventId) && !this.visibleFailures.has(eventId)) {
|
2023-02-16 17:21:44 +00:00
|
|
|
this.visibleFailures.set(eventId, this.failures.get(eventId)!);
|
2022-01-19 19:31:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
public addDecryptionFailure(failure: DecryptionFailure): void {
|
2022-01-19 19:31:43 +00:00
|
|
|
const eventId = failure.failedEventId;
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
if (this.trackedEvents.has(eventId)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-19 19:31:43 +00:00
|
|
|
|
|
|
|
this.failures.set(eventId, failure);
|
|
|
|
if (this.visibleEvents.has(eventId) && !this.visibleFailures.has(eventId)) {
|
|
|
|
this.visibleFailures.set(eventId, failure);
|
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
public removeDecryptionFailuresForEvent(e: MatrixEvent): void {
|
2023-02-16 17:21:44 +00:00
|
|
|
const eventId = e.getId()!;
|
2022-01-19 19:31:43 +00:00
|
|
|
this.failures.delete(eventId);
|
|
|
|
this.visibleFailures.delete(eventId);
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start checking for and tracking failures.
|
|
|
|
*/
|
2021-06-22 16:23:13 +00:00
|
|
|
public start(): void {
|
2022-11-30 11:32:56 +00:00
|
|
|
this.checkInterval = window.setInterval(
|
2018-06-15 12:33:07 +00:00
|
|
|
() => this.checkFailures(Date.now()),
|
|
|
|
DecryptionFailureTracker.CHECK_INTERVAL_MS,
|
|
|
|
);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
this.trackInterval = window.setInterval(() => this.trackFailures(), DecryptionFailureTracker.TRACK_INTERVAL_MS);
|
2018-06-15 16:58:43 +00:00
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-06-15 16:58:43 +00:00
|
|
|
/**
|
|
|
|
* Clear state and stop checking for and tracking failures.
|
|
|
|
*/
|
2021-06-22 16:23:13 +00:00
|
|
|
public stop(): void {
|
2023-02-16 17:21:44 +00:00
|
|
|
if (this.checkInterval) clearInterval(this.checkInterval);
|
|
|
|
if (this.trackInterval) clearInterval(this.trackInterval);
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
this.failures = new Map();
|
|
|
|
this.visibleEvents = new Set();
|
|
|
|
this.visibleFailures = new Map();
|
2024-04-17 12:36:01 +00:00
|
|
|
this.failureCounts = new Map();
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-22 16:23:13 +00:00
|
|
|
* Mark failures that occurred before nowTs - GRACE_PERIOD_MS as failures that should be
|
2018-06-15 12:33:07 +00:00
|
|
|
* tracked. Only mark one failure per event ID.
|
|
|
|
* @param {number} nowTs the timestamp that represents the time now.
|
|
|
|
*/
|
2021-06-22 16:23:13 +00:00
|
|
|
public checkFailures(nowTs: number): void {
|
2022-01-19 19:31:43 +00:00
|
|
|
const failuresGivenGrace: Set<DecryptionFailure> = new Set();
|
|
|
|
const failuresNotReady: Map<string, DecryptionFailure> = new Map();
|
|
|
|
for (const [eventId, failure] of this.visibleFailures) {
|
|
|
|
if (nowTs > failure.ts + DecryptionFailureTracker.GRACE_PERIOD_MS) {
|
|
|
|
failuresGivenGrace.add(failure);
|
|
|
|
this.trackedEvents.add(eventId);
|
2018-06-15 13:45:11 +00:00
|
|
|
} else {
|
2022-01-19 19:31:43 +00:00
|
|
|
failuresNotReady.set(eventId, failure);
|
2018-06-15 13:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 19:31:43 +00:00
|
|
|
this.visibleFailures = failuresNotReady;
|
2018-06-15 13:48:20 +00:00
|
|
|
|
2018-06-15 16:08:11 +00:00
|
|
|
// Commented out for now for expediency, we need to consider unbound nature of storing
|
|
|
|
// this in localStorage
|
2022-01-19 19:31:43 +00:00
|
|
|
// this.saveTrackedEvents();
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
this.aggregateFailures(failuresGivenGrace);
|
2018-07-05 12:54:44 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 19:31:43 +00:00
|
|
|
private aggregateFailures(failures: Set<DecryptionFailure>): void {
|
2018-07-05 12:54:44 +00:00
|
|
|
for (const failure of failures) {
|
|
|
|
const errorCode = failure.errorCode;
|
2024-04-17 12:36:01 +00:00
|
|
|
this.failureCounts.set(errorCode, (this.failureCounts.get(errorCode) ?? 0) + 1);
|
2018-07-05 12:54:44 +00:00
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-28 15:06:12 +00:00
|
|
|
* If there are failures that should be tracked, call the given trackDecryptionFailure
|
|
|
|
* function with the number of failures that should be tracked.
|
2018-06-15 12:33:07 +00:00
|
|
|
*/
|
2021-06-22 16:23:13 +00:00
|
|
|
public trackFailures(): void {
|
2024-04-17 12:36:01 +00:00
|
|
|
for (const [errorCode, count] of this.failureCounts.entries()) {
|
|
|
|
if (count > 0) {
|
2021-12-06 10:43:42 +00:00
|
|
|
const trackedErrorCode = this.errorCodeMapFn(errorCode);
|
2018-07-05 12:54:44 +00:00
|
|
|
|
2024-04-17 12:36:01 +00:00
|
|
|
this.fn(count, trackedErrorCode, errorCode);
|
|
|
|
this.failureCounts.set(errorCode, 0);
|
2018-07-05 12:54:44 +00:00
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|