rotateSignedPreKey: Protect against too-frequent rotation

This commit is contained in:
Scott Nonnenberg 2020-06-11 13:29:14 -07:00
parent b842b69660
commit f04812eb5d

View file

@ -299,6 +299,21 @@ export default class AccountManager extends EventTarget {
const store = window.textsecure.storage.protocol;
const { server, cleanSignedPreKeys } = this;
const existingKeys = await store.loadSignedPreKeys();
existingKeys.sort((a, b) => (b.created_at || 0) - (a.created_at || 0));
const confirmedKeys = existingKeys.filter(key => key.confirmed);
const ONE_DAY_AGO = Date.now() - 24 * 60 * 60 * 1000;
if (
confirmedKeys.length >= 3 &&
confirmedKeys[0].created_at > ONE_DAY_AGO
) {
window.log.warn(
'rotateSignedPreKey: 3+ confirmed keys, most recent is less than a day old. Cancelling rotation.'
);
return;
}
return store
.getIdentityKeyPair()
.then(
@ -377,8 +392,7 @@ export default class AccountManager extends EventTarget {
const MINIMUM_KEYS = 3;
const store = window.textsecure.storage.protocol;
return store.loadSignedPreKeys().then(async allKeys => {
allKeys.sort((a, b) => (a.created_at || 0) - (b.created_at || 0));
allKeys.reverse(); // we want the most recent first
allKeys.sort((a, b) => (b.created_at || 0) - (a.created_at || 0));
const confirmed = allKeys.filter(key => key.confirmed);
const unconfirmed = allKeys.filter(key => !key.confirmed);