Extend cleanSignedPreKeys to support PNI

This commit is contained in:
Fedor Indutny 2022-09-27 09:33:56 -07:00 committed by GitHub
parent cfe66a2c0b
commit 373f121887
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 17 deletions

View File

@ -2,12 +2,11 @@
// SPDX-License-Identifier: AGPL-3.0-only // SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai'; import { assert } from 'chai';
import { v4 as getGuid } from 'uuid';
import { getRandomBytes } from '../../Crypto'; import { getRandomBytes } from '../../Crypto';
import AccountManager from '../../textsecure/AccountManager'; import AccountManager from '../../textsecure/AccountManager';
import type { OuterSignedPrekeyType } from '../../textsecure/Types.d'; import type { OuterSignedPrekeyType } from '../../textsecure/Types.d';
import { UUID } from '../../types/UUID'; import { UUID, UUIDKind } from '../../types/UUID';
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
@ -32,7 +31,7 @@ describe('AccountManager', () => {
const identityKey = window.Signal.Curve.generateKeyPair(); const identityKey = window.Signal.Curve.generateKeyPair();
beforeEach(async () => { beforeEach(async () => {
const ourUuid = new UUID(getGuid()); const ourUuid = UUID.generate();
originalGetUuid = window.textsecure.storage.user.getUuid; originalGetUuid = window.textsecure.storage.user.getUuid;
originalGetIdentityKeyPair = originalGetIdentityKeyPair =
@ -107,7 +106,7 @@ describe('AccountManager', () => {
]; ];
// should be no calls to store.removeSignedPreKey, would cause crash // should be no calls to store.removeSignedPreKey, would cause crash
return accountManager.cleanSignedPreKeys(); return accountManager.cleanSignedPreKeys(UUIDKind.ACI);
}); });
it('eliminates oldest keys, even if recent key is unconfirmed', async () => { it('eliminates oldest keys, even if recent key is unconfirmed', async () => {
@ -170,7 +169,7 @@ describe('AccountManager', () => {
count += 1; count += 1;
}; };
await accountManager.cleanSignedPreKeys(); await accountManager.cleanSignedPreKeys(UUIDKind.ACI);
assert.strictEqual(count, 1); assert.strictEqual(count, 1);
}); });
@ -211,7 +210,7 @@ describe('AccountManager', () => {
throw new Error('None should be removed!'); throw new Error('None should be removed!');
}; };
await accountManager.cleanSignedPreKeys(); await accountManager.cleanSignedPreKeys(UUIDKind.ACI);
}); });
}); });
}); });

View File

@ -441,7 +441,10 @@ export default class AccountManager extends EventTarget {
]); ]);
try { try {
await this.cleanSignedPreKeys(); await Promise.all([
this.cleanSignedPreKeys(UUIDKind.ACI),
this.cleanSignedPreKeys(UUIDKind.PNI),
]);
} catch (_error) { } catch (_error) {
// Ignoring the error // Ignoring the error
} }
@ -455,9 +458,10 @@ export default class AccountManager extends EventTarget {
return this.pendingQueue.add(taskWithTimeout); return this.pendingQueue.add(taskWithTimeout);
} }
async cleanSignedPreKeys(): Promise<void> { async cleanSignedPreKeys(uuidKind: UUIDKind): Promise<void> {
const ourUuid = window.textsecure.storage.user.getCheckedUuid(); const ourUuid = window.textsecure.storage.user.getCheckedUuid(uuidKind);
const store = window.textsecure.storage.protocol; const store = window.textsecure.storage.protocol;
const logId = `AccountManager.cleanSignedPreKeys(${uuidKind})`;
const allKeys = await store.loadSignedPreKeys(ourUuid); const allKeys = await store.loadSignedPreKeys(ourUuid);
allKeys.sort((a, b) => (b.created_at || 0) - (a.created_at || 0)); allKeys.sort((a, b) => (b.created_at || 0) - (a.created_at || 0));
@ -467,15 +471,13 @@ export default class AccountManager extends EventTarget {
const recent = allKeys[0] ? allKeys[0].keyId : 'none'; const recent = allKeys[0] ? allKeys[0].keyId : 'none';
const recentConfirmed = confirmed[0] ? confirmed[0].keyId : 'none'; const recentConfirmed = confirmed[0] ? confirmed[0].keyId : 'none';
const recentUnconfirmed = unconfirmed[0] ? unconfirmed[0].keyId : 'none'; const recentUnconfirmed = unconfirmed[0] ? unconfirmed[0].keyId : 'none';
log.info(`cleanSignedPreKeys: Most recent signed key: ${recent}`); log.info(`${logId}: Most recent signed key: ${recent}`);
log.info(`${logId}: Most recent confirmed signed key: ${recentConfirmed}`);
log.info( log.info(
`cleanSignedPreKeys: Most recent confirmed signed key: ${recentConfirmed}` `${logId}: Most recent unconfirmed signed key: ${recentUnconfirmed}`
); );
log.info( log.info(
`cleanSignedPreKeys: Most recent unconfirmed signed key: ${recentUnconfirmed}` `${logId}: Total signed key count:`,
);
log.info(
'cleanSignedPreKeys: Total signed key count:',
allKeys.length, allKeys.length,
'-', '-',
confirmed.length, confirmed.length,
@ -494,7 +496,8 @@ export default class AccountManager extends EventTarget {
const timestamp = new Date(createdAt).toJSON(); const timestamp = new Date(createdAt).toJSON();
const confirmedText = key.confirmed ? ' (confirmed)' : ''; const confirmedText = key.confirmed ? ' (confirmed)' : '';
log.info( log.info(
`Removing signed prekey: ${key.keyId} with timestamp ${timestamp}${confirmedText}` `${logId}: Removing signed prekey: ${key.keyId} with ` +
`timestamp ${timestamp}${confirmedText}`
); );
await store.removeSignedPreKey(ourUuid, key.keyId); await store.removeSignedPreKey(ourUuid, key.keyId);
} }
@ -830,7 +833,8 @@ export default class AccountManager extends EventTarget {
await Promise.all(promises); await Promise.all(promises);
// This is primarily for the signed prekey summary it logs out // This is primarily for the signed prekey summary it logs out
this.cleanSignedPreKeys(); this.cleanSignedPreKeys(UUIDKind.ACI);
this.cleanSignedPreKeys(UUIDKind.PNI);
return { return {
...result, ...result,