Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
DecryptionFailureTracker: remove use of DecryptionError
Browse files Browse the repository at this point in the history
The second argument to `MatrixEventEvent.Decrypted` callbacks is deprecatedf,
and we can get the info we need direct from the event. This means that we no
longer need to reference the internal `DecryptionError` class in the js-sdk.
  • Loading branch information
richvdh committed Apr 3, 2024
1 parent 74251eb commit de59ee8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 86 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ module.exports = {
"!matrix-js-sdk/src/extensible_events_v1/PollEndEvent",
"!matrix-js-sdk/src/extensible_events_v1/InvalidEventError",
"!matrix-js-sdk/src/crypto",
"!matrix-js-sdk/src/crypto/algorithms",
"!matrix-js-sdk/src/crypto/aes",
"!matrix-js-sdk/src/crypto/olmlib",
"!matrix-js-sdk/src/crypto/crypto",
Expand Down
19 changes: 10 additions & 9 deletions src/DecryptionFailureTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { DecryptionError } from "matrix-js-sdk/src/crypto/algorithms";
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import { Error as ErrorEvent } from "@matrix-org/analytics-events/types/typescript/Error";
import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";
Expand Down Expand Up @@ -107,10 +106,10 @@ export class DecryptionFailureTracker {
*
* @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.
* 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`.
*/
private constructor(
private readonly fn: TrackingFn,
Expand All @@ -137,13 +136,15 @@ export class DecryptionFailureTracker {
// localStorage.setItem('mx-decryption-failure-event-ids', JSON.stringify([...this.trackedEvents]));
// }

public eventDecrypted(e: MatrixEvent, err: DecryptionError): void {
// for now we only track megolm decrytion failures
public eventDecrypted(e: MatrixEvent): void {
// for now we only track megolm decryption failures
if (e.getWireContent().algorithm != "m.megolm.v1.aes-sha2") {
return;
}
if (err) {
this.addDecryptionFailure(new DecryptionFailure(e.getId()!, err.code));

const errCode = e.decryptionFailureReason;
if (errCode !== null) {
this.addDecryptionFailure(new DecryptionFailure(e.getId()!, errCode));
} else {
// Could be an event in the failures, remove it
this.removeDecryptionFailuresForEvent(e);
Expand Down
3 changes: 1 addition & 2 deletions src/components/structures/MatrixChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { defer, IDeferred, QueryDict } from "matrix-js-sdk/src/utils";
import { logger } from "matrix-js-sdk/src/logger";
import { throttle } from "lodash";
import { CryptoEvent } from "matrix-js-sdk/src/crypto";
import { DecryptionError } from "matrix-js-sdk/src/crypto/algorithms";
import { IKeyBackupInfo } from "matrix-js-sdk/src/crypto/keybackup";
import { TooltipProvider } from "@vector-im/compound-web";

Expand Down Expand Up @@ -1595,7 +1594,7 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {

// When logging out, stop tracking failures and destroy state
cli.on(HttpApiEvent.SessionLoggedOut, () => dft.stop());
cli.on(MatrixEventEvent.Decrypted, (e, err) => dft.eventDecrypted(e, err as DecryptionError));
cli.on(MatrixEventEvent.Decrypted, (e) => dft.eventDecrypted(e));

cli.on(ClientEvent.Room, (room) => {
if (cli.isCryptoEnabled()) {
Expand Down
123 changes: 49 additions & 74 deletions test/DecryptionFailureTracker-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,11 @@ import { DecryptionFailureCode } from "matrix-js-sdk/src/crypto-api";

import { DecryptionFailureTracker } from "../src/DecryptionFailureTracker";

class MockDecryptionError extends Error {
public readonly code: string;

constructor(code?: string) {
super();

this.code = code || "MOCK_DECRYPTION_ERROR";
}
}

async function createFailedDecryptionEvent() {
async function createFailedDecryptionEvent(code?: DecryptionFailureCode) {
return await mkDecryptionFailureMatrixEvent({
roomId: "!room:id",
sender: "@alice:example.com",
code: DecryptionFailureCode.UNKNOWN_ERROR,
code: code ?? DecryptionFailureCode.UNKNOWN_ERROR,
msg: ":(",
});
}
Expand All @@ -50,9 +40,7 @@ describe("DecryptionFailureTracker", function () {
);

tracker.addVisibleEvent(failedDecryptionEvent);

const err = new MockDecryptionError();
tracker.eventDecrypted(failedDecryptionEvent, err);
tracker.eventDecrypted(failedDecryptionEvent);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand All @@ -65,7 +53,9 @@ describe("DecryptionFailureTracker", function () {
});

it("tracks a failed decryption with expected raw error for a visible event", async function () {
const failedDecryptionEvent = await createFailedDecryptionEvent();
const failedDecryptionEvent = await createFailedDecryptionEvent(
DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX,
);

let count = 0;
let reportedRawCode = "";
Expand All @@ -79,9 +69,7 @@ describe("DecryptionFailureTracker", function () {
);

tracker.addVisibleEvent(failedDecryptionEvent);

const err = new MockDecryptionError("INBOUND_SESSION_MISMATCH_ROOM_ID");
tracker.eventDecrypted(failedDecryptionEvent, err);
tracker.eventDecrypted(failedDecryptionEvent);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand All @@ -93,7 +81,7 @@ describe("DecryptionFailureTracker", function () {
expect(count).not.toBe(0);

// Should add the rawCode to the event context
expect(reportedRawCode).toBe("INBOUND_SESSION_MISMATCH_ROOM_ID");
expect(reportedRawCode).toBe("OLM_UNKNOWN_MESSAGE_INDEX");
});

it("tracks a failed decryption for an event that becomes visible later", async function () {
Expand All @@ -106,9 +94,7 @@ describe("DecryptionFailureTracker", function () {
() => "UnknownError",
);

const err = new MockDecryptionError();
tracker.eventDecrypted(failedDecryptionEvent, err);

tracker.eventDecrypted(failedDecryptionEvent);
tracker.addVisibleEvent(failedDecryptionEvent);

// Pretend "now" is Infinity
Expand All @@ -131,8 +117,7 @@ describe("DecryptionFailureTracker", function () {
() => "UnknownError",
);

const err = new MockDecryptionError();
tracker.eventDecrypted(failedDecryptionEvent, err);
tracker.eventDecrypted(failedDecryptionEvent);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand All @@ -156,9 +141,7 @@ describe("DecryptionFailureTracker", function () {
);

tracker.addVisibleEvent(decryptedEvent);

const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent);

// Indicate successful decryption.
await decryptExistingEvent(decryptedEvent, {
Expand Down Expand Up @@ -188,15 +171,14 @@ describe("DecryptionFailureTracker", function () {
() => "UnknownError",
);

const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent);

// Indicate successful decryption.
await decryptExistingEvent(decryptedEvent, {
plainType: "m.room.message",
plainContent: { body: "success" },
});
tracker.eventDecrypted(decryptedEvent, null);
tracker.eventDecrypted(decryptedEvent);

tracker.addVisibleEvent(decryptedEvent);

Expand All @@ -222,16 +204,15 @@ describe("DecryptionFailureTracker", function () {
tracker.addVisibleEvent(decryptedEvent);

// Arbitrary number of failed decryptions for both events
const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent2, err);
tracker.eventDecrypted(decryptedEvent2, err);
tracker.eventDecrypted(decryptedEvent);
tracker.eventDecrypted(decryptedEvent);
tracker.eventDecrypted(decryptedEvent);
tracker.eventDecrypted(decryptedEvent);
tracker.eventDecrypted(decryptedEvent);
tracker.eventDecrypted(decryptedEvent2);
tracker.eventDecrypted(decryptedEvent2);
tracker.addVisibleEvent(decryptedEvent2);
tracker.eventDecrypted(decryptedEvent2, err);
tracker.eventDecrypted(decryptedEvent2);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand Down Expand Up @@ -259,16 +240,15 @@ describe("DecryptionFailureTracker", function () {
tracker.addVisibleEvent(decryptedEvent);

// Indicate decryption
const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);

tracker.trackFailures();

// Indicate a second decryption, after having tracked the failure
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent);

tracker.trackFailures();

Expand All @@ -292,8 +272,7 @@ describe("DecryptionFailureTracker", function () {
tracker.addVisibleEvent(decryptedEvent);

// Indicate decryption
const err = new MockDecryptionError();
tracker.eventDecrypted(decryptedEvent, err);
tracker.eventDecrypted(decryptedEvent);

// Pretend "now" is Infinity
// NB: This saves to localStorage specific to DFT
Expand All @@ -312,7 +291,7 @@ describe("DecryptionFailureTracker", function () {

//secondTracker.loadTrackedEvents();

secondTracker.eventDecrypted(decryptedEvent, err);
secondTracker.eventDecrypted(decryptedEvent);
secondTracker.checkFailures(Infinity);
secondTracker.trackFailures();

Expand All @@ -326,25 +305,27 @@ describe("DecryptionFailureTracker", function () {
// @ts-ignore access to private constructor
const tracker = new DecryptionFailureTracker(
(total: number, errorCode: string) => (counts[errorCode] = (counts[errorCode] || 0) + total),
(error: string) => (error === "UnknownError" ? "UnknownError" : "OlmKeysNotSentError"),
(error: DecryptionFailureCode) =>
error === DecryptionFailureCode.UNKNOWN_ERROR ? "UnknownError" : "OlmKeysNotSentError",
);

const decryptedEvent1 = await createFailedDecryptionEvent();
const decryptedEvent2 = await createFailedDecryptionEvent();
const decryptedEvent3 = await createFailedDecryptionEvent();

const error1 = new MockDecryptionError("UnknownError");
const error2 = new MockDecryptionError("OlmKeysNotSentError");
const decryptedEvent1 = await createFailedDecryptionEvent(DecryptionFailureCode.UNKNOWN_ERROR);
const decryptedEvent2 = await createFailedDecryptionEvent(
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
);
const decryptedEvent3 = await createFailedDecryptionEvent(
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
);

tracker.addVisibleEvent(decryptedEvent1);
tracker.addVisibleEvent(decryptedEvent2);
tracker.addVisibleEvent(decryptedEvent3);

// One failure of ERROR_CODE_1, and effectively two for ERROR_CODE_2
tracker.eventDecrypted(decryptedEvent1, error1);
tracker.eventDecrypted(decryptedEvent2, error2);
tracker.eventDecrypted(decryptedEvent2, error2);
tracker.eventDecrypted(decryptedEvent3, error2);
tracker.eventDecrypted(decryptedEvent1);
tracker.eventDecrypted(decryptedEvent2);
tracker.eventDecrypted(decryptedEvent2);
tracker.eventDecrypted(decryptedEvent3);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand All @@ -364,21 +345,19 @@ describe("DecryptionFailureTracker", function () {
(_errorCode: string) => "OlmUnspecifiedError",
);

const decryptedEvent1 = await createFailedDecryptionEvent();
const decryptedEvent2 = await createFailedDecryptionEvent();
const decryptedEvent3 = await createFailedDecryptionEvent();

const error1 = new MockDecryptionError("ERROR_CODE_1");
const error2 = new MockDecryptionError("ERROR_CODE_2");
const error3 = new MockDecryptionError("ERROR_CODE_3");
const decryptedEvent1 = await createFailedDecryptionEvent(
DecryptionFailureCode.MEGOLM_UNKNOWN_INBOUND_SESSION_ID,
);
const decryptedEvent2 = await createFailedDecryptionEvent(DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX);
const decryptedEvent3 = await createFailedDecryptionEvent(DecryptionFailureCode.UNKNOWN_ERROR);

tracker.addVisibleEvent(decryptedEvent1);
tracker.addVisibleEvent(decryptedEvent2);
tracker.addVisibleEvent(decryptedEvent3);

tracker.eventDecrypted(decryptedEvent1, error1);
tracker.eventDecrypted(decryptedEvent2, error2);
tracker.eventDecrypted(decryptedEvent3, error3);
tracker.eventDecrypted(decryptedEvent1);
tracker.eventDecrypted(decryptedEvent2);
tracker.eventDecrypted(decryptedEvent3);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);
Expand All @@ -397,20 +376,16 @@ describe("DecryptionFailureTracker", function () {
(errorCode: string) => Array.from(errorCode).reverse().join(""),
);

const decryptedEvent = await createFailedDecryptionEvent();

const error = new MockDecryptionError("ERROR_CODE_1");

const decryptedEvent = await createFailedDecryptionEvent(DecryptionFailureCode.OLM_UNKNOWN_MESSAGE_INDEX);
tracker.addVisibleEvent(decryptedEvent);

tracker.eventDecrypted(decryptedEvent, error);
tracker.eventDecrypted(decryptedEvent);

// Pretend "now" is Infinity
tracker.checkFailures(Infinity);

tracker.trackFailures();

// should track remapped error code
expect(counts["1_EDOC_RORRE"]).toBe(1);
expect(counts["XEDNI_EGASSEM_NWONKNU_MLO"]).toBe(1);
});
});

0 comments on commit de59ee8

Please sign in to comment.