mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-27 20:36:27 -08:00
fix(notifications): keep encrypted push previews decryptable for the full retry window (#2094)
<!-- Please read https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before submitting your pull request --> ### Description <!-- Please include a summary of the change. Please also include relevant motivation and context. List any dependencies that are required for this change. --> Fixes # #### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update ### Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings ### AI disclosure: - [ ] Created without the usage of AI - [ ] Partially AI assisted (clarify which code was AI assisted and briefly explain what it does). - [ ] Fully AI generated (explain what all the generated code does in moderate detail). <!-- Write any explanation required here, but do not generate the explanation using AI!! You MUST prove you understand what the code in this PR does. You MUST explain what modifications you have made in order to ensure that the result is compatible with THE LICENSE By deleting this section or comment, in code or in body, the PR should be rejected and closed. -->
This commit is contained in:
2 files changed
+91
-7
No files matched your search
@@ -53,6 +53,9 @@ const matrixClient = vi.hoisted(() => ({
|
||||
fetchRoomEvent: vi.fn<() => Promise<unknown>>(),
|
||||
}));
|
||||
|
||||
const requestPushDrain = vi.hoisted(() => vi.fn<() => void>());
|
||||
const getSlidingSyncManager = vi.hoisted(() => vi.fn<() => unknown>());
|
||||
|
||||
const invoke = vi.hoisted(() =>
|
||||
vi.fn<(cmd: string, args?: Record<string, unknown>) => Promise<unknown>>()
|
||||
);
|
||||
@@ -78,6 +81,8 @@ const addPluginListener = vi.hoisted(() =>
|
||||
|
||||
vi.mock('./UnifiedPushTransport', () => unifiedPushTransport);
|
||||
|
||||
vi.mock('$client/initMatrix', () => ({ getSlidingSyncManager }));
|
||||
|
||||
vi.mock('./TauriNotificationsApiClient', () => ({
|
||||
getTauriNotificationsApi,
|
||||
isMobileTauri: () => false,
|
||||
@@ -151,6 +156,7 @@ describe('UnifiedPushNotifications', () => {
|
||||
}
|
||||
});
|
||||
matrixClient.getRoom.mockReturnValue(undefined);
|
||||
getSlidingSyncManager.mockReturnValue({ requestPushDrain });
|
||||
invoke.mockResolvedValue(undefined);
|
||||
addPluginListener.mockImplementation(
|
||||
async (_plugin: string, _event: string, handler: (data: unknown) => void) => {
|
||||
@@ -568,7 +574,8 @@ describe('UnifiedPushNotifications', () => {
|
||||
await listenAndPush(encryptedPush('$expired:example.com'));
|
||||
await vi.waitFor(() => expect(notificationsApi.sendNotification).toHaveBeenCalledOnce());
|
||||
|
||||
await vi.advanceTimersByTimeAsync(5 * 60_000);
|
||||
// The window, plus the grace for the attempt made as it lapses.
|
||||
await vi.advanceTimersByTimeAsync(5 * 60_000 + 10_000);
|
||||
resolveDecryption({
|
||||
clearEvent: {
|
||||
type: 'm.room.message',
|
||||
@@ -583,6 +590,61 @@ describe('UnifiedPushNotifications', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('enriches from the decryption attempt made as the retry window lapses', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
matrixClient.getRoom.mockReturnValue(makeRoom());
|
||||
const keyArrivesAt = Date.now() + 5 * 60_000;
|
||||
matrixClient.getCrypto.mockReturnValue({
|
||||
decryptEvent: vi
|
||||
.fn<() => Promise<Record<string, unknown>>>()
|
||||
.mockImplementation(async () => {
|
||||
if (Date.now() < keyArrivesAt) throw new Error('MissingRoomKey');
|
||||
return {
|
||||
clearEvent: { type: 'm.room.message', content: { body: 'key arrived' } },
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
await listenAndPush(encryptedPush('$suspended:example.com'));
|
||||
await vi.waitFor(() => expect(notificationsApi.sendNotification).toHaveBeenCalledOnce());
|
||||
|
||||
await vi.advanceTimersByTimeAsync(5 * 60_000);
|
||||
|
||||
await vi.waitFor(() => expect(notificationsApi.sendNotification).toHaveBeenCalledTimes(2));
|
||||
expect(notificationsApi.sendNotification.mock.calls[1]?.[0]).toMatchObject({
|
||||
body: 'You: key arrived',
|
||||
silent: true,
|
||||
});
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('keeps the to-device drain alive while an encrypted preview is still pending', async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
matrixClient.getRoom.mockReturnValue(makeRoom());
|
||||
matrixClient.getCrypto.mockReturnValue({
|
||||
decryptEvent: vi
|
||||
.fn<() => Promise<Record<string, unknown>>>()
|
||||
.mockRejectedValue(new Error('MissingRoomKey')),
|
||||
});
|
||||
|
||||
await listenAndPush(encryptedPush('$pending-drain:example.com'));
|
||||
await vi.waitFor(() => expect(notificationsApi.sendNotification).toHaveBeenCalledOnce());
|
||||
// Only the drain the incoming push itself asked for.
|
||||
expect(requestPushDrain).toHaveBeenCalledOnce();
|
||||
|
||||
// Past the two-minute drain window.
|
||||
await vi.advanceTimersByTimeAsync(3 * 60_000);
|
||||
|
||||
expect(requestPushDrain.mock.calls.length).toBeGreaterThan(1);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
|
||||
it('retries a duplicate rich event after its initial native post fails', async () => {
|
||||
matrixClient.getRoom.mockReturnValue(makeRoom());
|
||||
let attempts = 0;
|
||||
|
||||
@@ -475,6 +475,8 @@ function holdsPlaintext(event: MatrixEvent): boolean {
|
||||
|
||||
const DECRYPT_RETRY_DELAYS_MS = [1000, 2000, 5000, 10_000, 30_000, 60_000] as const;
|
||||
|
||||
const FINAL_DECRYPT_ATTEMPT_TIMEOUT_MS = 10_000;
|
||||
|
||||
const supportsEventDecryption = (crypto: CryptoApi | undefined): crypto is CryptoBackend =>
|
||||
!!crypto && 'decryptEvent' in crypto && typeof crypto.decryptEvent === 'function';
|
||||
|
||||
@@ -501,21 +503,41 @@ function whenDecrypted(event: MatrixEvent, apply: () => Promise<void>, mx: Matri
|
||||
};
|
||||
|
||||
event.on(MatrixEventEvent.Decrypted, onDecrypted);
|
||||
const giveUp = () => {
|
||||
if (finished) return;
|
||||
finish();
|
||||
unifiedPushLog.warn('notification', 'Encrypted preview never decrypted within retry window', {
|
||||
roomId: event.getRoomId(),
|
||||
sessionId: event.getWireContent().session_id,
|
||||
attempts: retryIndex,
|
||||
});
|
||||
};
|
||||
const retry = () => {
|
||||
if (finished) return;
|
||||
const crypto = mx.getCrypto();
|
||||
const attempt = supportsEventDecryption(crypto)
|
||||
? event.attemptDecryption(crypto, { isRetry: true }).catch(() => undefined)
|
||||
: undefined;
|
||||
|
||||
const remaining = retryDeadline - Date.now();
|
||||
if (remaining > 0) {
|
||||
const crypto = mx.getCrypto();
|
||||
if (supportsEventDecryption(crypto)) {
|
||||
void event.attemptDecryption(crypto, { isRetry: true }).catch(() => undefined);
|
||||
}
|
||||
// Sync is what carries the room key, and a backgrounded client only keeps
|
||||
// it running while a push drain is outstanding.
|
||||
getSlidingSyncManager(mx)?.requestPushDrain();
|
||||
const delay = DECRYPT_RETRY_DELAYS_MS[retryIndex] ?? 60_000;
|
||||
retryTimer = setTimeout(retry, Math.min(delay, remaining));
|
||||
retryIndex += 1;
|
||||
return;
|
||||
}
|
||||
finish();
|
||||
unifiedPushLog.warn('notification', 'Encrypted preview never decrypted within retry window');
|
||||
|
||||
// The window can lapse on a tick that runs long after the key landed, so
|
||||
// settle on this last attempt rather than on the clock.
|
||||
if (!attempt) {
|
||||
giveUp();
|
||||
return;
|
||||
}
|
||||
retryTimer = setTimeout(giveUp, FINAL_DECRYPT_ATTEMPT_TIMEOUT_MS);
|
||||
void attempt.then(giveUp);
|
||||
};
|
||||
retryTimer = setTimeout(retry, DECRYPT_RETRY_DELAYS_MS[0]);
|
||||
retryIndex += 1;
|
||||
|
||||
Reference in new issue
Block a user