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.
|
|
|
|
*/
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
import { MatrixError } from "matrix-js-sdk/src/http-api";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
export class DecryptionFailure {
|
2021-06-22 16:23:13 +00:00
|
|
|
public readonly ts: number;
|
|
|
|
|
|
|
|
constructor(public readonly failedEventId: string, public readonly errorCode: string) {
|
2018-06-15 12:33:07 +00:00
|
|
|
this.ts = Date.now();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:43:42 +00:00
|
|
|
type ErrorCode = "OlmKeysNotSentError" | "OlmIndexError" | "UnknownError" | "OlmUnspecifiedError";
|
|
|
|
|
|
|
|
type TrackingFn = (count: number, trackedErrCode: ErrorCode) => void;
|
|
|
|
|
|
|
|
export type ErrCodeMapFn = (errcode: string) => ErrorCode;
|
2021-06-22 16:23:13 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
export class DecryptionFailureTracker {
|
2018-06-15 12:33:07 +00:00
|
|
|
// Array of items of type DecryptionFailure. Every `CHECK_INTERVAL_MS`, this list
|
|
|
|
// is checked for failures that happened > `GRACE_PERIOD_MS` ago. Those that did
|
2018-07-05 12:54:44 +00:00
|
|
|
// are accumulated in `failureCounts`.
|
2021-06-22 16:23:13 +00:00
|
|
|
public failures: DecryptionFailure[] = [];
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-07-05 12:54:44 +00:00
|
|
|
// A histogram of the number of failures that will be tracked at the next tracking
|
|
|
|
// interval, split by failure error code.
|
2021-06-22 16:23:13 +00:00
|
|
|
public failureCounts: Record<string, number> = {
|
2018-07-05 12:54:44 +00:00
|
|
|
// [errorCode]: 42
|
|
|
|
};
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-06-15 13:48:20 +00:00
|
|
|
// Event IDs of failures that were tracked previously
|
2021-06-22 16:23:13 +00:00
|
|
|
public trackedEventHashMap: Record<string, boolean> = {
|
2018-06-15 15:59:42 +00:00
|
|
|
// [eventId]: true
|
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
|
2021-07-12 08:02:46 +00:00
|
|
|
public checkInterval: number = null;
|
|
|
|
public trackInterval: number = 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`.
|
|
|
|
static TRACK_INTERVAL_MS = 60000;
|
2018-06-15 12:33:07 +00:00
|
|
|
|
|
|
|
// Call `checkFailures` every `CHECK_INTERVAL_MS`.
|
|
|
|
static CHECK_INTERVAL_MS = 5000;
|
|
|
|
|
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`.
|
2018-06-28 14:07:58 +00:00
|
|
|
static GRACE_PERIOD_MS = 60000;
|
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) => {...}`,
|
|
|
|
* where `count` is the number of failures and `errorCode` matches the `.code` of
|
|
|
|
* provided DecryptionError errors (by default, unless `errorCodeMapFn` is specified.
|
|
|
|
* @param {function?} errorCodeMapFn The function used to map error codes to the
|
|
|
|
* trackedErrorCode. If not provided, the `.code` of errors will be used.
|
|
|
|
*/
|
2021-12-06 10:43:42 +00:00
|
|
|
constructor(private readonly fn: TrackingFn, private readonly errorCodeMapFn: ErrCodeMapFn) {
|
2018-06-15 12:33:07 +00:00
|
|
|
if (!fn || typeof fn !== 'function') {
|
|
|
|
throw new Error('DecryptionFailureTracker requires tracking function');
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:43:42 +00:00
|
|
|
if (typeof errorCodeMapFn !== 'function') {
|
2018-07-05 12:54:44 +00:00
|
|
|
throw new Error('DecryptionFailureTracker second constructor argument should be a function');
|
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 16:08:11 +00:00
|
|
|
// loadTrackedEventHashMap() {
|
|
|
|
// this.trackedEventHashMap = JSON.parse(localStorage.getItem('mx-decryption-failure-event-id-hashes')) || {};
|
|
|
|
// }
|
2018-06-15 14:26:53 +00:00
|
|
|
|
2018-06-15 16:08:11 +00:00
|
|
|
// saveTrackedEventHashMap() {
|
|
|
|
// localStorage.setItem('mx-decryption-failure-event-id-hashes', JSON.stringify(this.trackedEventHashMap));
|
|
|
|
// }
|
2018-06-15 14:26:53 +00:00
|
|
|
|
2021-12-14 15:34:54 +00:00
|
|
|
public eventDecrypted(e: MatrixEvent, err: MatrixError): void {
|
2018-07-05 12:54:44 +00:00
|
|
|
if (err) {
|
2021-12-14 15:34:54 +00:00
|
|
|
this.addDecryptionFailure(new DecryptionFailure(e.getId(), err.errcode));
|
2018-06-15 12:33:07 +00:00
|
|
|
} else {
|
|
|
|
// Could be an event in the failures, remove it
|
|
|
|
this.removeDecryptionFailuresForEvent(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
public addDecryptionFailure(failure: DecryptionFailure): void {
|
2018-07-05 12:54:44 +00:00
|
|
|
this.failures.push(failure);
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
public removeDecryptionFailuresForEvent(e: MatrixEvent): void {
|
2018-06-15 12:33:07 +00:00
|
|
|
this.failures = this.failures.filter((f) => f.failedEventId !== e.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start checking for and tracking failures.
|
|
|
|
*/
|
2021-06-22 16:23:13 +00:00
|
|
|
public start(): void {
|
2018-06-15 16:58:43 +00:00
|
|
|
this.checkInterval = setInterval(
|
2018-06-15 12:33:07 +00:00
|
|
|
() => this.checkFailures(Date.now()),
|
|
|
|
DecryptionFailureTracker.CHECK_INTERVAL_MS,
|
|
|
|
);
|
|
|
|
|
2018-06-15 16:58:43 +00:00
|
|
|
this.trackInterval = setInterval(
|
2018-07-05 12:54:44 +00:00
|
|
|
() => this.trackFailures(),
|
2018-06-15 12:33:07 +00:00
|
|
|
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 {
|
2018-06-15 16:58:43 +00:00
|
|
|
clearInterval(this.checkInterval);
|
|
|
|
clearInterval(this.trackInterval);
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2018-06-15 16:58:43 +00:00
|
|
|
this.failures = [];
|
2018-07-05 12:54:44 +00:00
|
|
|
this.failureCounts = {};
|
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 {
|
2018-06-15 13:45:11 +00:00
|
|
|
const failuresGivenGrace = [];
|
|
|
|
const failuresNotReady = [];
|
|
|
|
while (this.failures.length > 0) {
|
|
|
|
const f = this.failures.shift();
|
|
|
|
if (nowTs > f.ts + DecryptionFailureTracker.GRACE_PERIOD_MS) {
|
|
|
|
failuresGivenGrace.push(f);
|
|
|
|
} else {
|
|
|
|
failuresNotReady.push(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.failures = failuresNotReady;
|
2018-06-15 12:33:07 +00:00
|
|
|
|
|
|
|
// Only track one failure per event
|
|
|
|
const dedupedFailuresMap = failuresGivenGrace.reduce(
|
2018-06-15 14:49:33 +00:00
|
|
|
(map, failure) => {
|
2018-06-15 15:59:42 +00:00
|
|
|
if (!this.trackedEventHashMap[failure.failedEventId]) {
|
2018-06-15 14:49:33 +00:00
|
|
|
return map.set(failure.failedEventId, failure);
|
2018-06-15 13:48:20 +00:00
|
|
|
} else {
|
2018-06-15 14:49:33 +00:00
|
|
|
return map;
|
2018-06-15 13:48:20 +00:00
|
|
|
}
|
|
|
|
},
|
2018-06-15 14:49:33 +00:00
|
|
|
// Use a map to preseve key ordering
|
|
|
|
new Map(),
|
2018-06-15 12:33:07 +00:00
|
|
|
);
|
2018-06-15 13:48:20 +00:00
|
|
|
|
2018-06-15 14:49:33 +00:00
|
|
|
const trackedEventIds = [...dedupedFailuresMap.keys()];
|
2018-06-15 13:48:20 +00:00
|
|
|
|
2018-06-15 14:26:53 +00:00
|
|
|
this.trackedEventHashMap = trackedEventIds.reduce(
|
2021-06-29 12:11:58 +00:00
|
|
|
(result, eventId) => ({ ...result, [eventId]: true }),
|
2018-06-15 14:26:53 +00:00
|
|
|
this.trackedEventHashMap,
|
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
|
|
|
|
// this.saveTrackedEventHashMap();
|
2018-06-15 14:26:53 +00:00
|
|
|
|
2018-06-15 14:49:33 +00:00
|
|
|
const dedupedFailures = dedupedFailuresMap.values();
|
2018-06-15 12:33:07 +00:00
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
this.aggregateFailures(dedupedFailures);
|
2018-07-05 12:54:44 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
private aggregateFailures(failures: DecryptionFailure[]): void {
|
2018-07-05 12:54:44 +00:00
|
|
|
for (const failure of failures) {
|
|
|
|
const errorCode = failure.errorCode;
|
|
|
|
this.failureCounts[errorCode] = (this.failureCounts[errorCode] || 0) + 1;
|
|
|
|
}
|
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 {
|
2018-07-05 12:54:44 +00:00
|
|
|
for (const errorCode of Object.keys(this.failureCounts)) {
|
|
|
|
if (this.failureCounts[errorCode] > 0) {
|
2021-12-06 10:43:42 +00:00
|
|
|
const trackedErrorCode = this.errorCodeMapFn(errorCode);
|
2018-07-05 12:54:44 +00:00
|
|
|
|
2021-06-22 16:23:13 +00:00
|
|
|
this.fn(this.failureCounts[errorCode], trackedErrorCode);
|
2018-07-05 12:54:44 +00:00
|
|
|
this.failureCounts[errorCode] = 0;
|
|
|
|
}
|
2018-06-15 12:33:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|