Skip to content

Commit 5691589

Browse files
authored
fix(qa): bound Matrix E2EE sends (#110968)
1 parent 9918dd7 commit 5691589

3 files changed

Lines changed: 91 additions & 21 deletions

File tree

‎extensions/qa-lab/src/live-transports/matrix/substrate/e2ee-client-internals.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@ export const MATRIX_QA_E2EE_SYNC_FILTER = {
1010
},
1111
};
1212

13+
export async function runMatrixQaE2eeClientOperation<T>(params: {
14+
label: string;
15+
run: () => Promise<T>;
16+
stop: () => void;
17+
timeoutMs: number;
18+
}): Promise<T> {
19+
let timer: NodeJS.Timeout | undefined;
20+
const timeout = new Promise<never>((_resolve, reject) => {
21+
timer = setTimeout(() => {
22+
// Matrix SDK encryption can wait indefinitely after room-key sharing. Stop this
23+
// disposable QA client so the scenario can fail and release its worker resources.
24+
try {
25+
params.stop();
26+
} catch {
27+
// Preserve the operation timeout as the actionable failure.
28+
}
29+
reject(new Error(`${params.label} timed out after ${params.timeoutMs}ms`));
30+
}, params.timeoutMs);
31+
timer.unref();
32+
});
33+
34+
try {
35+
return await Promise.race([params.run(), timeout]);
36+
} finally {
37+
if (timer) {
38+
clearTimeout(timer);
39+
}
40+
}
41+
}
42+
1343
export function shouldRecordMatrixQaObservedEventUpdate(params: {
1444
next: MatrixQaObservedEvent;
1545
previous: MatrixQaObservedEvent | undefined;

‎extensions/qa-lab/src/live-transports/matrix/substrate/e2ee-client.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import { mkdtemp, rm, stat } from "node:fs/promises";
33
import os from "node:os";
44
import path from "node:path";
5-
import { describe, expect, it } from "vitest";
5+
import { describe, expect, it, vi } from "vitest";
66
import {
77
MATRIX_QA_E2EE_SYNC_FILTER,
88
prepareMatrixQaE2eeStorage,
9+
runMatrixQaE2eeClientOperation,
910
shouldRecordMatrixQaObservedEventUpdate,
1011
} from "./e2ee-client-internals.js";
1112
import { findMatrixQaObservedEventMatch } from "./events.js";
@@ -14,10 +15,38 @@ const testing = {
1415
MATRIX_QA_E2EE_SYNC_FILTER,
1516
findMatrixQaObservedEventMatch,
1617
prepareMatrixQaE2eeStorage,
18+
runMatrixQaE2eeClientOperation,
1719
shouldRecordMatrixQaObservedEventUpdate,
1820
};
1921

2022
describe("matrix qa e2ee client storage", () => {
23+
it("stops a disposable client when an E2EE operation exceeds its scenario timeout", async () => {
24+
vi.useFakeTimers();
25+
try {
26+
const stop = vi.fn();
27+
const operation = testing.runMatrixQaE2eeClientOperation({
28+
label: "Matrix E2EE text send",
29+
run: () =>
30+
new Promise<string>(() => {
31+
// Intentionally pending so the timeout owns settlement.
32+
}),
33+
stop,
34+
timeoutMs: 150_000,
35+
});
36+
const rejection = expect(operation).rejects.toThrow(
37+
"Matrix E2EE text send timed out after 150000ms",
38+
);
39+
40+
await vi.advanceTimersByTimeAsync(150_000);
41+
42+
await rejection;
43+
expect(stop).toHaveBeenCalledOnce();
44+
expect(vi.getTimerCount()).toBe(0);
45+
} finally {
46+
vi.useRealTimers();
47+
}
48+
});
49+
2150
it("filters receipt noise without suppressing room state or timeline events", () => {
2251
expect(testing.MATRIX_QA_E2EE_SYNC_FILTER).toEqual({
2352
room: {

‎extensions/qa-lab/src/live-transports/matrix/substrate/e2ee-client.ts‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { buildMatrixQaMessageContent } from "./client-message-content.js";
2626
import {
2727
MATRIX_QA_E2EE_SYNC_FILTER,
2828
prepareMatrixQaE2eeStorage,
29+
runMatrixQaE2eeClientOperation,
2930
shouldRecordMatrixQaObservedEventUpdate,
3031
type MatrixQaE2eeActorId,
3132
} from "./e2ee-client-internals.js";
@@ -391,6 +392,13 @@ export async function createMatrixQaE2eeScenarioClient(
391392
}
392393
return client.crypto;
393394
};
395+
const runClientOperation = <T>(label: string, run: () => Promise<T>) =>
396+
runMatrixQaE2eeClientOperation({
397+
label,
398+
run,
399+
stop: () => client.stopWithoutPersist(),
400+
timeoutMs: params.timeoutMs,
401+
});
394402

395403
return {
396404
async acceptVerification(id) {
@@ -452,16 +460,17 @@ export async function createMatrixQaE2eeScenarioClient(
452460
return await requireCrypto().scanVerificationQr(id, qrDataBase64);
453461
},
454462
async sendTextMessage(opts) {
455-
return await client.sendMessage(
456-
opts.roomId,
457-
buildMatrixQaMessageContent(opts) as MessageEventContent,
463+
return await runClientOperation("Matrix E2EE text send", () =>
464+
client.sendMessage(opts.roomId, buildMatrixQaMessageContent(opts) as MessageEventContent),
458465
);
459466
},
460467
async sendNoticeMessage(opts) {
461-
return await client.sendMessage(opts.roomId, {
462-
...buildMatrixQaMessageContent(opts),
463-
msgtype: "m.notice",
464-
} as MessageEventContent);
468+
return await runClientOperation("Matrix E2EE notice send", () =>
469+
client.sendMessage(opts.roomId, {
470+
...buildMatrixQaMessageContent(opts),
471+
msgtype: "m.notice",
472+
} as MessageEventContent),
473+
);
465474
},
466475
async sendImageMessage(opts) {
467476
const encrypted = await requireCrypto().encryptMedia(opts.buffer);
@@ -471,19 +480,21 @@ export async function createMatrixQaE2eeScenarioClient(
471480
opts.fileName,
472481
);
473482
const file: EncryptedFile = { url: contentUri, ...encrypted.file };
474-
return await client.sendMessage(opts.roomId, {
475-
...buildMatrixQaMessageContent({
476-
body: opts.body,
477-
mentionUserIds: opts.mentionUserIds,
478-
}),
479-
file,
480-
filename: opts.fileName,
481-
info: {
482-
mimetype: opts.contentType,
483-
size: opts.buffer.byteLength,
484-
},
485-
msgtype: "m.image",
486-
} as MessageEventContent);
483+
return await runClientOperation("Matrix E2EE image send", () =>
484+
client.sendMessage(opts.roomId, {
485+
...buildMatrixQaMessageContent({
486+
body: opts.body,
487+
mentionUserIds: opts.mentionUserIds,
488+
}),
489+
file,
490+
filename: opts.fileName,
491+
info: {
492+
mimetype: opts.contentType,
493+
size: opts.buffer.byteLength,
494+
},
495+
msgtype: "m.image",
496+
} as MessageEventContent),
497+
);
487498
},
488499
async startVerification(id, method) {
489500
return await requireCrypto().startVerification(id, method);

0 commit comments

Comments
 (0)