2022-01-14 21:34:52 +00:00
|
|
|
// Copyright 2020-2022 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-04-13 00:50:17 +00:00
|
|
|
import { debounce, isNumber, chunk } from 'lodash';
|
2020-09-09 00:56:23 +00:00
|
|
|
import pMap from 'p-map';
|
2022-03-23 20:49:27 +00:00
|
|
|
import Long from 'long';
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
import dataInterface from '../sql/Client';
|
2021-07-13 18:54:53 +00:00
|
|
|
import * as Bytes from '../Bytes';
|
2020-09-09 00:56:23 +00:00
|
|
|
import {
|
2021-09-24 00:49:05 +00:00
|
|
|
getRandomBytes,
|
2020-09-09 00:56:23 +00:00
|
|
|
deriveStorageItemKey,
|
|
|
|
deriveStorageManifestKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
encryptProfile,
|
|
|
|
decryptProfile,
|
2022-02-22 22:30:59 +00:00
|
|
|
deriveMasterKeyFromGroupV1,
|
2020-09-09 00:56:23 +00:00
|
|
|
} from '../Crypto';
|
|
|
|
import {
|
|
|
|
mergeAccountRecord,
|
|
|
|
mergeContactRecord,
|
|
|
|
mergeGroupV1Record,
|
2020-09-09 02:25:05 +00:00
|
|
|
mergeGroupV2Record,
|
2022-07-01 00:52:03 +00:00
|
|
|
mergeStoryDistributionListRecord,
|
2022-08-03 17:10:49 +00:00
|
|
|
mergeStickerPackRecord,
|
2020-09-09 00:56:23 +00:00
|
|
|
toAccountRecord,
|
|
|
|
toContactRecord,
|
|
|
|
toGroupV1Record,
|
2020-09-09 02:25:05 +00:00
|
|
|
toGroupV2Record,
|
2022-07-01 00:52:03 +00:00
|
|
|
toStoryDistributionListRecord,
|
2022-08-03 17:10:49 +00:00
|
|
|
toStickerPackRecord,
|
2020-09-09 00:56:23 +00:00
|
|
|
} from './storageRecordOps';
|
2022-02-08 18:00:18 +00:00
|
|
|
import type { MergeResultType } from './storageRecordOps';
|
2022-04-13 00:50:17 +00:00
|
|
|
import { MAX_READ_KEYS } from './storageConstants';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2021-07-13 18:54:53 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2022-03-23 20:49:27 +00:00
|
|
|
import { dropNull } from '../util/dropNull';
|
2021-08-26 14:10:58 +00:00
|
|
|
import * as durations from '../util/durations';
|
2021-06-09 22:28:54 +00:00
|
|
|
import { BackOff } from '../util/BackOff';
|
2020-09-29 23:29:11 +00:00
|
|
|
import { storageJobQueue } from '../util/JobQueue';
|
2020-12-18 17:09:31 +00:00
|
|
|
import { sleep } from '../util/sleep';
|
2021-04-09 20:12:05 +00:00
|
|
|
import { isMoreRecentThan } from '../util/timestamp';
|
2022-10-08 00:19:02 +00:00
|
|
|
import { map, filter } from '../util/iterables';
|
2021-05-05 16:39:16 +00:00
|
|
|
import { ourProfileKeyService } from './ourProfileKey';
|
2021-06-07 16:39:13 +00:00
|
|
|
import {
|
|
|
|
ConversationTypes,
|
|
|
|
typeofConversation,
|
|
|
|
} from '../util/whatTypeOfConversation';
|
2021-07-13 18:54:53 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-01-14 21:34:52 +00:00
|
|
|
import { singleProtoJobQueue } from '../jobs/singleProtoJobQueue';
|
|
|
|
import * as Errors from '../types/errors';
|
2022-02-14 19:36:32 +00:00
|
|
|
import type {
|
|
|
|
ExtendedStorageID,
|
|
|
|
RemoteRecord,
|
|
|
|
UnknownRecord,
|
|
|
|
} from '../types/StorageService.d';
|
2022-06-13 21:39:35 +00:00
|
|
|
import MessageSender from '../textsecure/SendMessage';
|
2022-08-03 17:10:49 +00:00
|
|
|
import type {
|
|
|
|
StoryDistributionWithMembersType,
|
|
|
|
StorageServiceFieldsType,
|
|
|
|
StickerPackType,
|
|
|
|
UninstalledStickerPackType,
|
|
|
|
} from '../sql/Interface';
|
2022-07-21 00:07:09 +00:00
|
|
|
import { MY_STORIES_ID } from '../types/Stories';
|
2022-10-08 00:19:02 +00:00
|
|
|
import { isNotNil } from '../util/isNotNil';
|
2021-07-13 18:54:53 +00:00
|
|
|
|
|
|
|
type IManifestRecordIdentifier = Proto.ManifestRecord.IIdentifier;
|
|
|
|
|
2022-03-09 18:22:34 +00:00
|
|
|
const {
|
|
|
|
eraseStorageServiceStateFromConversations,
|
|
|
|
updateConversation,
|
|
|
|
updateConversations,
|
|
|
|
} = dataInterface;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
const uploadBucket: Array<number> = [];
|
2020-09-29 23:29:11 +00:00
|
|
|
|
|
|
|
const validRecordTypes = new Set([
|
|
|
|
0, // UNKNOWN
|
|
|
|
1, // CONTACT
|
|
|
|
2, // GROUPV1
|
|
|
|
3, // GROUPV2
|
|
|
|
4, // ACCOUNT
|
2022-07-01 00:52:03 +00:00
|
|
|
5, // STORY_DISTRIBUTION_LIST
|
2022-08-16 07:05:09 +00:00
|
|
|
6, // STICKER_PACK
|
2020-09-29 23:29:11 +00:00
|
|
|
]);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-06-09 22:28:54 +00:00
|
|
|
const backOff = new BackOff([
|
2021-08-26 14:10:58 +00:00
|
|
|
durations.SECOND,
|
|
|
|
5 * durations.SECOND,
|
|
|
|
30 * durations.SECOND,
|
|
|
|
2 * durations.MINUTE,
|
|
|
|
5 * durations.MINUTE,
|
2021-06-09 22:28:54 +00:00
|
|
|
]);
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
const conflictBackOff = new BackOff([
|
|
|
|
durations.SECOND,
|
|
|
|
5 * durations.SECOND,
|
|
|
|
30 * durations.SECOND,
|
|
|
|
]);
|
2020-09-09 23:07:58 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
function redactStorageID(
|
|
|
|
storageID: string,
|
|
|
|
version?: number,
|
|
|
|
conversation?: ConversationModel
|
|
|
|
): string {
|
|
|
|
const convoId = conversation ? ` ${conversation?.idForLogging()}` : '';
|
|
|
|
return `${version ?? '?'}:${storageID.substring(0, 3)}${convoId}`;
|
2021-04-06 22:54:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
function redactExtendedStorageID({
|
2022-02-08 18:00:18 +00:00
|
|
|
storageID,
|
|
|
|
storageVersion,
|
2022-02-14 19:36:32 +00:00
|
|
|
}: ExtendedStorageID): string {
|
2022-02-08 18:00:18 +00:00
|
|
|
return redactStorageID(storageID, storageVersion);
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
function encryptRecord(
|
2020-09-09 00:56:23 +00:00
|
|
|
storageID: string | undefined,
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord: Proto.IStorageRecord
|
2022-07-01 00:52:03 +00:00
|
|
|
): Proto.StorageItem {
|
2021-07-13 18:54:53 +00:00
|
|
|
const storageItem = new Proto.StorageItem();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
const storageKeyBuffer = storageID
|
2022-10-08 00:19:02 +00:00
|
|
|
? Bytes.fromBase64(storageID)
|
2020-09-09 00:56:23 +00:00
|
|
|
: generateStorageID();
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
|
|
|
const storageItemKey = deriveStorageItemKey(
|
2020-09-09 00:56:23 +00:00
|
|
|
storageKey,
|
2021-09-24 00:49:05 +00:00
|
|
|
Bytes.toBase64(storageKeyBuffer)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const encryptedRecord = encryptProfile(
|
|
|
|
Proto.StorageRecord.encode(storageRecord).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
storageItemKey
|
|
|
|
);
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
storageItem.key = storageKeyBuffer;
|
|
|
|
storageItem.value = encryptedRecord;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
return storageItem;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
function generateStorageID(): Uint8Array {
|
|
|
|
return getRandomBytes(16);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GeneratedManifestType = {
|
2022-03-04 21:14:52 +00:00
|
|
|
postUploadUpdateFunctions: Array<() => unknown>;
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID: Map<string, MergeableItemType | RemoteRecord>;
|
|
|
|
insertKeys: Set<string>;
|
|
|
|
deleteKeys: Set<string>;
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function generateManifest(
|
|
|
|
version: number,
|
2021-07-13 18:54:53 +00:00
|
|
|
previousManifest?: Proto.IManifestRecord,
|
2022-02-14 19:36:32 +00:00
|
|
|
isNewManifest = false
|
2020-09-09 00:56:23 +00:00
|
|
|
): Promise<GeneratedManifestType> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): generating manifest ` +
|
2022-02-14 19:36:32 +00:00
|
|
|
`new=${isNewManifest}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-03-12 01:56:01 +00:00
|
|
|
await window.ConversationController.checkForConflicts();
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-03-04 21:14:52 +00:00
|
|
|
const postUploadUpdateFunctions: Array<() => unknown> = [];
|
2022-10-08 00:19:02 +00:00
|
|
|
const insertKeys = new Set<string>();
|
|
|
|
const deleteKeys = new Set<string>();
|
|
|
|
const recordsByID = new Map<string, MergeableItemType | RemoteRecord>();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
function processStorageRecord({
|
|
|
|
conversation,
|
|
|
|
currentStorageID,
|
|
|
|
currentStorageVersion,
|
|
|
|
identifierType,
|
|
|
|
storageNeedsSync,
|
|
|
|
storageRecord,
|
|
|
|
}: {
|
|
|
|
conversation?: ConversationModel;
|
|
|
|
currentStorageID?: string;
|
|
|
|
currentStorageVersion?: number;
|
|
|
|
identifierType: Proto.ManifestRecord.Identifier.Type;
|
|
|
|
storageNeedsSync: boolean;
|
|
|
|
storageRecord: Proto.IStorageRecord;
|
|
|
|
}) {
|
|
|
|
const currentRedactedID = currentStorageID
|
|
|
|
? redactStorageID(currentStorageID, currentStorageVersion)
|
|
|
|
: undefined;
|
|
|
|
|
|
|
|
const isNewItem = isNewManifest || storageNeedsSync || !currentStorageID;
|
|
|
|
|
|
|
|
const storageID = isNewItem
|
|
|
|
? Bytes.toBase64(generateStorageID())
|
|
|
|
: currentStorageID;
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.set(storageID, {
|
|
|
|
itemType: identifierType,
|
|
|
|
storageID,
|
|
|
|
storageRecord,
|
|
|
|
});
|
2022-07-01 00:52:03 +00:00
|
|
|
|
|
|
|
// When a client needs to update a given record it should create it
|
|
|
|
// under a new key and delete the existing key.
|
|
|
|
if (isNewItem) {
|
2022-10-08 00:19:02 +00:00
|
|
|
insertKeys.add(storageID);
|
2022-07-01 00:52:03 +00:00
|
|
|
const newRedactedID = redactStorageID(storageID, version, conversation);
|
|
|
|
if (currentStorageID) {
|
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`updating from=${currentRedactedID} ` +
|
|
|
|
`to=${newRedactedID}`
|
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
deleteKeys.add(currentStorageID);
|
2022-07-01 00:52:03 +00:00
|
|
|
} else {
|
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): adding key=${newRedactedID}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
isNewItem,
|
|
|
|
storageID,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const conversations = window.getConversations();
|
|
|
|
for (let i = 0; i < conversations.length; i += 1) {
|
|
|
|
const conversation = conversations.models[i];
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
let identifierType;
|
2020-09-09 00:56:23 +00:00
|
|
|
let storageRecord;
|
2021-06-07 16:39:13 +00:00
|
|
|
|
|
|
|
const conversationType = typeofConversation(conversation.attributes);
|
|
|
|
if (conversationType === ConversationTypes.Me) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageRecord.account = await toAccountRecord(conversation);
|
2022-07-01 00:52:03 +00:00
|
|
|
identifierType = ITEM_TYPE.ACCOUNT;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.Direct) {
|
2021-09-10 02:38:11 +00:00
|
|
|
// Contacts must have UUID
|
|
|
|
if (!conversation.get('uuid')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-09-19 18:47:49 +00:00
|
|
|
let shouldDrop = false;
|
|
|
|
let dropReason: string | undefined;
|
|
|
|
|
2021-11-11 21:38:02 +00:00
|
|
|
const validationError = conversation.validate();
|
|
|
|
if (validationError) {
|
2022-09-19 18:47:49 +00:00
|
|
|
shouldDrop = true;
|
|
|
|
dropReason = `local validation error=${validationError}`;
|
|
|
|
} else if (conversation.isUnregisteredAndStale()) {
|
|
|
|
shouldDrop = true;
|
|
|
|
dropReason = 'unregistered and stale';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldDrop) {
|
2022-02-11 21:05:24 +00:00
|
|
|
const droppedID = conversation.get('storageID');
|
|
|
|
const droppedVersion = conversation.get('storageVersion');
|
|
|
|
if (!droppedID) {
|
|
|
|
continue;
|
2021-11-11 21:38:02 +00:00
|
|
|
}
|
2022-02-11 21:05:24 +00:00
|
|
|
|
|
|
|
const recordID = redactStorageID(
|
|
|
|
droppedID,
|
|
|
|
droppedVersion,
|
|
|
|
conversation
|
|
|
|
);
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
`storageService.generateManifest(${version}): ` +
|
2022-09-19 18:47:49 +00:00
|
|
|
`dropping contact=${recordID} ` +
|
|
|
|
`due to ${dropReason}`
|
2022-02-11 21:05:24 +00:00
|
|
|
);
|
|
|
|
conversation.unset('storageID');
|
2022-10-08 00:19:02 +00:00
|
|
|
deleteKeys.add(droppedID);
|
2021-11-11 21:38:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2020-09-09 00:56:23 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
|
|
|
storageRecord.contact = await toContactRecord(conversation);
|
2022-07-01 00:52:03 +00:00
|
|
|
identifierType = ITEM_TYPE.CONTACT;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.GroupV2) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2022-07-01 00:52:03 +00:00
|
|
|
storageRecord.groupV2 = toGroupV2Record(conversation);
|
|
|
|
identifierType = ITEM_TYPE.GROUPV2;
|
2021-06-07 16:39:13 +00:00
|
|
|
} else if (conversationType === ConversationTypes.GroupV1) {
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord = new Proto.StorageRecord();
|
2022-07-01 00:52:03 +00:00
|
|
|
storageRecord.groupV1 = toGroupV1Record(conversation);
|
|
|
|
identifierType = ITEM_TYPE.GROUPV1;
|
2020-09-10 22:37:20 +00:00
|
|
|
} else {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`unknown conversation=${conversation.idForLogging()}`
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
if (!storageRecord || !identifierType) {
|
2022-02-08 18:00:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
const { isNewItem, storageID } = processStorageRecord({
|
|
|
|
conversation,
|
|
|
|
currentStorageID: conversation.get('storageID'),
|
|
|
|
currentStorageVersion: conversation.get('storageVersion'),
|
|
|
|
identifierType,
|
|
|
|
storageNeedsSync: Boolean(conversation.get('needsStorageServiceSync')),
|
|
|
|
storageRecord,
|
|
|
|
});
|
2022-02-08 18:00:18 +00:00
|
|
|
|
|
|
|
if (isNewItem) {
|
2022-03-04 21:14:52 +00:00
|
|
|
postUploadUpdateFunctions.push(() => {
|
|
|
|
conversation.set({
|
|
|
|
needsStorageServiceSync: false,
|
|
|
|
storageVersion: version,
|
|
|
|
storageID,
|
|
|
|
});
|
|
|
|
updateConversation(conversation.attributes);
|
2022-02-08 18:00:18 +00:00
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
const {
|
|
|
|
storyDistributionLists,
|
|
|
|
installedStickerPacks,
|
|
|
|
uninstalledStickerPacks,
|
|
|
|
} = await getNonConversationRecords();
|
2022-07-01 00:52:03 +00:00
|
|
|
|
|
|
|
log.info(
|
2022-08-03 17:10:49 +00:00
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`adding storyDistributionLists=${storyDistributionLists.length}`
|
2022-07-01 00:52:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
storyDistributionLists.forEach(storyDistributionList => {
|
2022-07-21 00:07:09 +00:00
|
|
|
const storageRecord = new Proto.StorageRecord();
|
|
|
|
storageRecord.storyDistributionList = toStoryDistributionListRecord(
|
|
|
|
storyDistributionList
|
|
|
|
);
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
const { isNewItem, storageID } = processStorageRecord({
|
|
|
|
currentStorageID: storyDistributionList.storageID,
|
|
|
|
currentStorageVersion: storyDistributionList.storageVersion,
|
|
|
|
identifierType: ITEM_TYPE.STORY_DISTRIBUTION_LIST,
|
|
|
|
storageNeedsSync: storyDistributionList.storageNeedsSync,
|
2022-07-21 00:07:09 +00:00
|
|
|
storageRecord,
|
2022-07-01 00:52:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (isNewItem) {
|
|
|
|
postUploadUpdateFunctions.push(() => {
|
|
|
|
dataInterface.modifyStoryDistribution({
|
|
|
|
...storyDistributionList,
|
|
|
|
storageID,
|
|
|
|
storageVersion: version,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`adding uninstalled stickerPacks=${uninstalledStickerPacks.length}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const uninstalledStickerPackIds = new Set<string>();
|
|
|
|
|
|
|
|
uninstalledStickerPacks.forEach(stickerPack => {
|
|
|
|
const storageRecord = new Proto.StorageRecord();
|
|
|
|
storageRecord.stickerPack = toStickerPackRecord(stickerPack);
|
|
|
|
|
|
|
|
uninstalledStickerPackIds.add(stickerPack.id);
|
|
|
|
|
|
|
|
const { isNewItem, storageID } = processStorageRecord({
|
|
|
|
currentStorageID: stickerPack.storageID,
|
|
|
|
currentStorageVersion: stickerPack.storageVersion,
|
|
|
|
identifierType: ITEM_TYPE.STICKER_PACK,
|
|
|
|
storageNeedsSync: stickerPack.storageNeedsSync,
|
|
|
|
storageRecord,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isNewItem) {
|
|
|
|
postUploadUpdateFunctions.push(() => {
|
|
|
|
dataInterface.addUninstalledStickerPack({
|
|
|
|
...stickerPack,
|
|
|
|
storageID,
|
|
|
|
storageVersion: version,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`adding installed stickerPacks=${installedStickerPacks.length}`
|
|
|
|
);
|
|
|
|
|
|
|
|
installedStickerPacks.forEach(stickerPack => {
|
|
|
|
if (uninstalledStickerPackIds.has(stickerPack.id)) {
|
|
|
|
log.error(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`sticker pack ${stickerPack.id} is both installed and uninstalled`
|
|
|
|
);
|
|
|
|
window.reduxActions.stickers.uninstallStickerPack(
|
|
|
|
stickerPack.id,
|
|
|
|
stickerPack.key,
|
|
|
|
{ fromSync: true }
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const storageRecord = new Proto.StorageRecord();
|
|
|
|
storageRecord.stickerPack = toStickerPackRecord(stickerPack);
|
|
|
|
|
|
|
|
const { isNewItem, storageID } = processStorageRecord({
|
|
|
|
currentStorageID: stickerPack.storageID,
|
|
|
|
currentStorageVersion: stickerPack.storageVersion,
|
|
|
|
identifierType: ITEM_TYPE.STICKER_PACK,
|
|
|
|
storageNeedsSync: stickerPack.storageNeedsSync,
|
|
|
|
storageRecord,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isNewItem) {
|
|
|
|
postUploadUpdateFunctions.push(() => {
|
|
|
|
dataInterface.createOrUpdateStickerPack({
|
|
|
|
...stickerPack,
|
|
|
|
storageID,
|
|
|
|
storageVersion: version,
|
|
|
|
storageNeedsSync: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const unknownRecordsArray: ReadonlyArray<UnknownRecord> = (
|
2020-10-06 22:25:00 +00:00
|
|
|
window.storage.get('storage-service-unknown-records') || []
|
|
|
|
).filter((record: UnknownRecord) => !validRecordTypes.has(record.itemType));
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
const redactedUnknowns = unknownRecordsArray.map(redactExtendedStorageID);
|
2022-02-08 18:00:18 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): adding unknown ` +
|
|
|
|
`records=${JSON.stringify(redactedUnknowns)} ` +
|
|
|
|
`count=${redactedUnknowns.length}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// When updating the manifest, ensure all "unknown" keys are added to the
|
|
|
|
// new manifest, so we don't inadvertently delete something we don't understand
|
|
|
|
unknownRecordsArray.forEach((record: UnknownRecord) => {
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.set(record.storageID, record);
|
2020-09-29 23:29:11 +00:00
|
|
|
});
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const recordsWithErrors: ReadonlyArray<UnknownRecord> = window.storage.get(
|
|
|
|
'storage-service-error-records',
|
|
|
|
new Array<UnknownRecord>()
|
|
|
|
);
|
2022-02-14 19:36:32 +00:00
|
|
|
const redactedErrors = recordsWithErrors.map(redactExtendedStorageID);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): adding error ` +
|
|
|
|
`records=${JSON.stringify(redactedErrors)} count=${redactedErrors.length}`
|
2020-09-29 23:29:11 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// These records failed to merge in the previous fetchManifest, but we still
|
|
|
|
// need to include them so that the manifest is complete
|
|
|
|
recordsWithErrors.forEach((record: UnknownRecord) => {
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.set(record.storageID, record);
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
// Delete keys that we wanted to drop during the processing of the manifest.
|
|
|
|
const storedPendingDeletes = window.storage.get(
|
|
|
|
'storage-service-pending-deletes',
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
const redactedPendingDeletes = storedPendingDeletes.map(
|
|
|
|
redactExtendedStorageID
|
|
|
|
);
|
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
`deleting extra keys=${JSON.stringify(redactedPendingDeletes)} ` +
|
|
|
|
`count=${redactedPendingDeletes.length}`
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const { storageID } of storedPendingDeletes) {
|
2022-10-08 00:19:02 +00:00
|
|
|
deleteKeys.add(storageID);
|
2022-02-14 19:36:32 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 22:37:20 +00:00
|
|
|
// Validate before writing
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
const duplicates = new Set<string>();
|
|
|
|
const typeDuplicates = new Set();
|
2020-09-10 22:37:20 +00:00
|
|
|
let hasAccountType = false;
|
2022-10-08 00:19:02 +00:00
|
|
|
for (const [storageID, { itemType }] of recordsByID) {
|
2020-09-10 22:37:20 +00:00
|
|
|
// Ensure there are no duplicate StorageIdentifiers in your manifest
|
|
|
|
// This can be broken down into two parts:
|
|
|
|
// There are no duplicate type+raw pairs
|
|
|
|
// There are no duplicate raw bytes
|
2022-10-08 00:19:02 +00:00
|
|
|
const typeAndID = `${itemType}+${storageID}`;
|
|
|
|
if (duplicates.has(storageID) || typeDuplicates.has(typeAndID)) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.upload(${version}): removing from duplicate item ` +
|
|
|
|
'from the manifest',
|
|
|
|
redactStorageID(storageID),
|
2022-10-08 00:19:02 +00:00
|
|
|
itemType
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.delete(storageID);
|
2020-09-10 22:37:20 +00:00
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
duplicates.add(storageID);
|
|
|
|
typeDuplicates.add(typeAndID);
|
2020-09-10 22:37:20 +00:00
|
|
|
|
|
|
|
// Ensure all deletes are not present in the manifest
|
2022-10-08 00:19:02 +00:00
|
|
|
const hasDeleteKey = deleteKeys.has(storageID);
|
2020-09-10 22:37:20 +00:00
|
|
|
if (hasDeleteKey) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.upload(${version}): removing key which has been deleted`,
|
|
|
|
redactStorageID(storageID),
|
2022-10-08 00:19:02 +00:00
|
|
|
itemType
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.delete(storageID);
|
2020-09-10 22:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that there is *exactly* one Account type in the manifest
|
2022-10-08 00:19:02 +00:00
|
|
|
if (itemType === ITEM_TYPE.ACCOUNT) {
|
2020-09-10 22:37:20 +00:00
|
|
|
if (hasAccountType) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.upload(${version}): removing duplicate account`,
|
|
|
|
redactStorageID(storageID)
|
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
recordsByID.delete(storageID);
|
2020-09-10 22:37:20 +00:00
|
|
|
}
|
|
|
|
hasAccountType = true;
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
}
|
2020-09-10 22:37:20 +00:00
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
duplicates.clear();
|
|
|
|
typeDuplicates.clear();
|
2020-09-10 22:37:20 +00:00
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const storageKeyDuplicates = new Set<string>();
|
2020-09-10 22:37:20 +00:00
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
for (const storageID of insertKeys) {
|
2020-09-10 22:37:20 +00:00
|
|
|
// Ensure there are no duplicate StorageIdentifiers in your list of inserts
|
|
|
|
if (storageKeyDuplicates.has(storageID)) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.upload(${version}): ` +
|
|
|
|
'removing duplicate identifier from inserts',
|
2021-04-06 22:54:47 +00:00
|
|
|
redactStorageID(storageID)
|
2020-09-10 22:37:20 +00:00
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
insertKeys.delete(storageID);
|
2020-09-10 22:37:20 +00:00
|
|
|
}
|
|
|
|
storageKeyDuplicates.add(storageID);
|
2022-10-08 00:19:02 +00:00
|
|
|
}
|
2020-09-10 22:37:20 +00:00
|
|
|
|
|
|
|
storageKeyDuplicates.clear();
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
// If we have a copy of what the current remote manifest is then we run these
|
|
|
|
// additional validations comparing our pending manifest to the remote
|
|
|
|
// manifest:
|
|
|
|
if (previousManifest) {
|
|
|
|
const pendingInserts: Set<string> = new Set();
|
|
|
|
const pendingDeletes: Set<string> = new Set();
|
|
|
|
|
|
|
|
const remoteKeys: Set<string> = new Set();
|
2021-07-13 18:54:53 +00:00
|
|
|
(previousManifest.keys ?? []).forEach(
|
|
|
|
(identifier: IManifestRecordIdentifier) => {
|
|
|
|
strictAssert(identifier.raw, 'Identifier without raw field');
|
|
|
|
const storageID = Bytes.toBase64(identifier.raw);
|
2021-04-09 20:12:05 +00:00
|
|
|
remoteKeys.add(storageID);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const localKeys: Set<string> = new Set();
|
2022-10-08 00:19:02 +00:00
|
|
|
for (const storageID of recordsByID.keys()) {
|
2021-04-09 20:12:05 +00:00
|
|
|
localKeys.add(storageID);
|
|
|
|
|
|
|
|
if (!remoteKeys.has(storageID)) {
|
|
|
|
pendingInserts.add(storageID);
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
}
|
2021-04-09 20:12:05 +00:00
|
|
|
|
|
|
|
remoteKeys.forEach(storageID => {
|
|
|
|
if (!localKeys.has(storageID)) {
|
|
|
|
pendingDeletes.add(storageID);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
if (deleteKeys.size !== pendingDeletes.size) {
|
|
|
|
const localDeletes = Array.from(deleteKeys).map(key =>
|
|
|
|
redactStorageID(key)
|
2021-09-16 22:25:49 +00:00
|
|
|
);
|
|
|
|
const remoteDeletes: Array<string> = [];
|
|
|
|
pendingDeletes.forEach(id => remoteDeletes.push(redactStorageID(id)));
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): delete key sizes do not match`,
|
2021-09-16 22:25:49 +00:00
|
|
|
'local',
|
|
|
|
localDeletes.join(','),
|
|
|
|
'remote',
|
|
|
|
remoteDeletes.join(',')
|
|
|
|
);
|
2021-04-09 20:12:05 +00:00
|
|
|
throw new Error('invalid write delete keys length do not match');
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
if (insertKeys.size !== pendingInserts.size) {
|
2021-04-09 20:12:05 +00:00
|
|
|
throw new Error('invalid write insert items length do not match');
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
for (const storageID of deleteKeys) {
|
2021-04-09 20:12:05 +00:00
|
|
|
if (!pendingDeletes.has(storageID)) {
|
|
|
|
throw new Error(
|
|
|
|
'invalid write delete key missing from pending deletes'
|
|
|
|
);
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
}
|
|
|
|
for (const storageID of insertKeys) {
|
2021-04-09 20:12:05 +00:00
|
|
|
if (!pendingInserts.has(storageID)) {
|
|
|
|
throw new Error(
|
|
|
|
'invalid write insert key missing from pending inserts'
|
|
|
|
);
|
|
|
|
}
|
2022-10-08 00:19:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
postUploadUpdateFunctions,
|
|
|
|
recordsByID,
|
|
|
|
insertKeys,
|
|
|
|
deleteKeys,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type EncryptManifestOptionsType = {
|
|
|
|
recordsByID: Map<string, MergeableItemType | RemoteRecord>;
|
|
|
|
insertKeys: Set<string>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type EncryptedManifestType = {
|
|
|
|
newItems: Set<Proto.IStorageItem>;
|
|
|
|
storageManifest: Proto.IStorageManifest;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function encryptManifest(
|
|
|
|
version: number,
|
|
|
|
{ recordsByID, insertKeys }: EncryptManifestOptionsType
|
|
|
|
): Promise<EncryptedManifestType> {
|
|
|
|
const manifestRecordKeys: Set<IManifestRecordIdentifier> = new Set();
|
|
|
|
const newItems: Set<Proto.IStorageItem> = new Set();
|
|
|
|
|
|
|
|
for (const [storageID, { itemType, storageRecord }] of recordsByID) {
|
|
|
|
const identifier = new Proto.ManifestRecord.Identifier({
|
|
|
|
type: itemType,
|
|
|
|
raw: Bytes.fromBase64(storageID),
|
2021-04-09 20:12:05 +00:00
|
|
|
});
|
2022-10-08 00:19:02 +00:00
|
|
|
|
|
|
|
manifestRecordKeys.add(identifier);
|
|
|
|
|
|
|
|
if (insertKeys.has(storageID)) {
|
|
|
|
strictAssert(
|
|
|
|
storageRecord !== undefined,
|
|
|
|
'Inserted items must have an associated record'
|
|
|
|
);
|
|
|
|
|
|
|
|
let storageItem;
|
|
|
|
try {
|
|
|
|
storageItem = encryptRecord(storageID, storageRecord);
|
|
|
|
} catch (err) {
|
|
|
|
log.error(
|
|
|
|
`storageService.upload(${version}): encrypt record failed:`,
|
|
|
|
Errors.toLogFormat(err)
|
|
|
|
);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
newItems.add(storageItem);
|
|
|
|
}
|
2021-04-09 20:12:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const manifestRecord = new Proto.ManifestRecord();
|
2022-03-23 20:49:27 +00:00
|
|
|
manifestRecord.version = Long.fromNumber(version);
|
2022-05-25 19:20:45 +00:00
|
|
|
manifestRecord.sourceDevice = window.storage.user.getDeviceId() ?? 0;
|
2020-09-09 00:56:23 +00:00
|
|
|
manifestRecord.keys = Array.from(manifestRecordKeys);
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
2022-03-23 20:49:27 +00:00
|
|
|
const storageManifestKey = deriveStorageManifestKey(
|
|
|
|
storageKey,
|
|
|
|
Long.fromNumber(version)
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
const encryptedManifest = encryptProfile(
|
|
|
|
Proto.ManifestRecord.encode(manifestRecord).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
storageManifestKey
|
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const storageManifest = new Proto.StorageManifest();
|
2022-03-23 20:49:27 +00:00
|
|
|
storageManifest.version = manifestRecord.version;
|
2021-09-24 00:49:05 +00:00
|
|
|
storageManifest.value = encryptedManifest;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
newItems,
|
|
|
|
storageManifest,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uploadManifest(
|
|
|
|
version: number,
|
2022-10-08 00:19:02 +00:00
|
|
|
{ postUploadUpdateFunctions, deleteKeys }: GeneratedManifestType,
|
|
|
|
{ newItems, storageManifest }: EncryptedManifestType
|
2020-09-09 00:56:23 +00:00
|
|
|
): Promise<void> {
|
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.uploadManifest: We are offline!');
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
if (newItems.size === 0 && deleteKeys.size === 0) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(`storageService.upload(${version}): nothing to upload`);
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
const credentials = window.storage.get('storageCredentials');
|
|
|
|
try {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): inserting=${newItems.size} ` +
|
2022-10-08 00:19:02 +00:00
|
|
|
`deleting=${deleteKeys.size}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const writeOperation = new Proto.WriteOperation();
|
2020-09-10 22:37:20 +00:00
|
|
|
writeOperation.manifest = storageManifest;
|
|
|
|
writeOperation.insertItem = Array.from(newItems);
|
2022-10-08 00:19:02 +00:00
|
|
|
writeOperation.deleteKey = Array.from(deleteKeys).map(storageID =>
|
|
|
|
Bytes.fromBase64(storageID)
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
await window.textsecure.messaging.modifyStorageRecords(
|
2021-09-24 00:49:05 +00:00
|
|
|
Proto.WriteOperation.encode(writeOperation).finish(),
|
2020-09-09 00:56:23 +00:00
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): upload complete, updating ` +
|
2022-03-04 21:14:52 +00:00
|
|
|
`items=${postUploadUpdateFunctions.length}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// update conversations with the new storageID
|
2022-03-04 21:14:52 +00:00
|
|
|
postUploadUpdateFunctions.forEach(fn => fn());
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): failed!`,
|
|
|
|
Errors.toLogFormat(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (err.code === 409) {
|
2021-06-09 22:28:54 +00:00
|
|
|
if (conflictBackOff.isFull()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): exceeded maximum consecutive ` +
|
|
|
|
'conflicts'
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): conflict found with ` +
|
|
|
|
`version=${version}, running sync job ` +
|
|
|
|
`times=${conflictBackOff.getIndex()}`
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(`storageService.upload(${version}): setting new manifestVersion`);
|
2020-09-09 00:56:23 +00:00
|
|
|
window.storage.put('manifestVersion', version);
|
2021-06-09 22:28:54 +00:00
|
|
|
conflictBackOff.reset();
|
|
|
|
backOff.reset();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
try {
|
2022-06-13 21:39:35 +00:00
|
|
|
await singleProtoJobQueue.add(MessageSender.getFetchManifestSyncMessage());
|
2022-01-14 21:34:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.upload(${version}): Failed to queue sync message`,
|
2022-01-14 21:34:52 +00:00
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
async function stopStorageServiceSync(reason: Error) {
|
|
|
|
log.warn('storageService.stopStorageServiceSync', Errors.toLogFormat(reason));
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
await window.storage.remove('storageKey');
|
|
|
|
|
2021-06-09 22:28:54 +00:00
|
|
|
if (backOff.isFull()) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
2021-06-09 22:28:54 +00:00
|
|
|
'storageService.stopStorageServiceSync: too many consecutive stops'
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
2021-06-09 22:28:54 +00:00
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2021-06-09 22:28:54 +00:00
|
|
|
|
|
|
|
await sleep(backOff.getAndIncrement());
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.stopStorageServiceSync: requesting new keys');
|
2022-01-14 21:34:52 +00:00
|
|
|
setTimeout(async () => {
|
2021-07-15 23:48:09 +00:00
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.warn(
|
2021-07-15 23:48:09 +00:00
|
|
|
'stopStorageServiceSync: We are primary device; not sending key sync request'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-14 21:34:52 +00:00
|
|
|
try {
|
2022-06-13 21:39:35 +00:00
|
|
|
await singleProtoJobQueue.add(MessageSender.getRequestKeySyncMessage());
|
2022-01-14 21:34:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'storageService.stopStorageServiceSync: Failed to queue sync message',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2021-06-09 22:28:54 +00:00
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function createNewManifest() {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.createNewManifest: creating new manifest');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const version = window.storage.get('manifestVersion', 0);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
const generatedManifest = await generateManifest(version, undefined, true);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
const encryptedManifest = await encryptManifest(version, generatedManifest);
|
|
|
|
|
|
|
|
await uploadManifest(
|
|
|
|
version,
|
|
|
|
{
|
|
|
|
...generatedManifest,
|
|
|
|
// we have created a new manifest, there should be no keys to delete
|
|
|
|
deleteKeys: new Set(),
|
|
|
|
},
|
|
|
|
encryptedManifest
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function decryptManifest(
|
2021-07-13 18:54:53 +00:00
|
|
|
encryptedManifest: Proto.IStorageManifest
|
|
|
|
): Promise<Proto.ManifestRecord> {
|
2020-09-09 00:56:23 +00:00
|
|
|
const { version, value } = encryptedManifest;
|
|
|
|
|
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
|
|
|
const storageManifestKey = deriveStorageManifestKey(
|
2020-09-09 00:56:23 +00:00
|
|
|
storageKey,
|
2022-03-23 20:49:27 +00:00
|
|
|
dropNull(version)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
strictAssert(value, 'StorageManifest has no value field');
|
2021-09-24 00:49:05 +00:00
|
|
|
const decryptedManifest = decryptProfile(value, storageManifestKey);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
return Proto.ManifestRecord.decode(decryptedManifest);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchManifest(
|
2021-06-15 00:09:37 +00:00
|
|
|
manifestVersion: number
|
2021-07-13 18:54:53 +00:00
|
|
|
): Promise<Proto.ManifestRecord | undefined> {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info('storageService.sync: fetch start');
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
if (!window.textsecure.messaging) {
|
2022-02-08 18:00:18 +00:00
|
|
|
throw new Error('storageService.sync: we are offline!');
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-11-11 22:43:05 +00:00
|
|
|
const credentials =
|
|
|
|
await window.textsecure.messaging.getStorageCredentials();
|
2020-09-09 00:56:23 +00:00
|
|
|
window.storage.put('storageCredentials', credentials);
|
|
|
|
|
|
|
|
const manifestBinary = await window.textsecure.messaging.getStorageManifest(
|
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
greaterThanVersion: manifestVersion,
|
|
|
|
}
|
|
|
|
);
|
2021-09-24 00:49:05 +00:00
|
|
|
const encryptedManifest = Proto.StorageManifest.decode(manifestBinary);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
return decryptManifest(encryptedManifest);
|
|
|
|
} catch (err) {
|
2022-02-08 18:00:18 +00:00
|
|
|
await stopStorageServiceSync(err);
|
2020-09-09 00:56:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-01-31 19:58:20 +00:00
|
|
|
if (err.code === 204) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info('storageService.sync: no newer manifest, ok');
|
2022-01-31 19:58:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
log.error('storageService.sync: failed!', Errors.toLogFormat(err));
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
if (err.code === 404) {
|
|
|
|
await createNewManifest();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MergeableItemType = {
|
|
|
|
itemType: number;
|
|
|
|
storageID: string;
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecord: Proto.IStorageRecord;
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type MergedRecordType = UnknownRecord & {
|
|
|
|
hasConflict: boolean;
|
2022-02-11 21:05:24 +00:00
|
|
|
shouldDrop: boolean;
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError: boolean;
|
2020-09-09 00:56:23 +00:00
|
|
|
isUnsupported: boolean;
|
2022-03-09 18:22:34 +00:00
|
|
|
updatedConversations: ReadonlyArray<ConversationModel>;
|
|
|
|
needProfileFetch: ReadonlyArray<ConversationModel>;
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function mergeRecord(
|
2022-02-08 18:00:18 +00:00
|
|
|
storageVersion: number,
|
2020-09-09 00:56:23 +00:00
|
|
|
itemToMerge: MergeableItemType
|
|
|
|
): Promise<MergedRecordType> {
|
|
|
|
const { itemType, storageID, storageRecord } = itemToMerge;
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
let mergeResult: MergeResultType = { hasConflict: false, details: [] };
|
2020-09-09 00:56:23 +00:00
|
|
|
let isUnsupported = false;
|
2020-09-29 23:29:11 +00:00
|
|
|
let hasError = false;
|
2022-03-09 18:22:34 +00:00
|
|
|
let updatedConversations = new Array<ConversationModel>();
|
|
|
|
const needProfileFetch = new Array<ConversationModel>();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (itemType === ITEM_TYPE.UNKNOWN) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn('storageService.mergeRecord: Unknown item type', storageID);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.CONTACT && storageRecord.contact) {
|
2022-02-08 18:00:18 +00:00
|
|
|
mergeResult = await mergeContactRecord(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.contact
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.GROUPV1 && storageRecord.groupV1) {
|
2022-02-08 18:00:18 +00:00
|
|
|
mergeResult = await mergeGroupV1Record(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.groupV1
|
|
|
|
);
|
2020-10-07 18:27:08 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.GROUPV2 && storageRecord.groupV2) {
|
2022-02-08 18:00:18 +00:00
|
|
|
mergeResult = await mergeGroupV2Record(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.groupV2
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else if (itemType === ITEM_TYPE.ACCOUNT && storageRecord.account) {
|
2022-02-08 18:00:18 +00:00
|
|
|
mergeResult = await mergeAccountRecord(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.account
|
|
|
|
);
|
2022-07-01 00:52:03 +00:00
|
|
|
} else if (
|
|
|
|
itemType === ITEM_TYPE.STORY_DISTRIBUTION_LIST &&
|
|
|
|
storageRecord.storyDistributionList
|
|
|
|
) {
|
|
|
|
mergeResult = await mergeStoryDistributionListRecord(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.storyDistributionList
|
|
|
|
);
|
2022-08-03 17:10:49 +00:00
|
|
|
} else if (
|
|
|
|
itemType === ITEM_TYPE.STICKER_PACK &&
|
|
|
|
storageRecord.stickerPack
|
|
|
|
) {
|
|
|
|
mergeResult = await mergeStickerPackRecord(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
storageRecord.stickerPack
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
} else {
|
|
|
|
isUnsupported = true;
|
2022-02-08 18:00:18 +00:00
|
|
|
log.warn(
|
|
|
|
`storageService.merge(${redactStorageID(
|
|
|
|
storageID,
|
|
|
|
storageVersion
|
|
|
|
)}): unknown item type=${itemType}`
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2022-02-08 18:00:18 +00:00
|
|
|
|
|
|
|
const redactedID = redactStorageID(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
mergeResult.conversation
|
|
|
|
);
|
|
|
|
const oldID = mergeResult.oldStorageID
|
|
|
|
? redactStorageID(mergeResult.oldStorageID, mergeResult.oldStorageVersion)
|
|
|
|
: '?';
|
2022-03-09 18:22:34 +00:00
|
|
|
updatedConversations = [
|
|
|
|
...updatedConversations,
|
|
|
|
...(mergeResult.updatedConversations ?? []),
|
|
|
|
];
|
|
|
|
if (mergeResult.needsProfileFetch) {
|
|
|
|
strictAssert(mergeResult.conversation, 'needsProfileFetch, but no convo');
|
|
|
|
needProfileFetch.push(mergeResult.conversation);
|
|
|
|
}
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.merge(${redactedID}): merged item type=${itemType} ` +
|
|
|
|
`oldID=${oldID} ` +
|
|
|
|
`conflict=${mergeResult.hasConflict} ` +
|
2022-02-11 21:05:24 +00:00
|
|
|
`shouldDrop=${Boolean(mergeResult.shouldDrop)} ` +
|
2022-02-08 18:00:18 +00:00
|
|
|
`details=${JSON.stringify(mergeResult.details)}`
|
2021-04-06 22:54:47 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError = true;
|
2022-02-08 18:00:18 +00:00
|
|
|
const redactedID = redactStorageID(storageID, storageVersion);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.merge(${redactedID}): error with ` +
|
|
|
|
`item type=${itemType} ` +
|
|
|
|
`details=${Errors.toLogFormat(err)}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-02-08 18:00:18 +00:00
|
|
|
hasConflict: mergeResult.hasConflict,
|
2022-02-11 21:05:24 +00:00
|
|
|
shouldDrop: Boolean(mergeResult.shouldDrop),
|
2020-09-29 23:29:11 +00:00
|
|
|
hasError,
|
2020-09-09 00:56:23 +00:00
|
|
|
isUnsupported,
|
|
|
|
itemType,
|
|
|
|
storageID,
|
2022-03-09 18:22:34 +00:00
|
|
|
updatedConversations,
|
|
|
|
needProfileFetch,
|
2020-09-09 00:56:23 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
type NonConversationRecordsResultType = Readonly<{
|
|
|
|
installedStickerPacks: ReadonlyArray<StickerPackType>;
|
|
|
|
uninstalledStickerPacks: ReadonlyArray<UninstalledStickerPackType>;
|
|
|
|
storyDistributionLists: ReadonlyArray<StoryDistributionWithMembersType>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
// TODO: DESKTOP-3929
|
|
|
|
async function getNonConversationRecords(): Promise<NonConversationRecordsResultType> {
|
|
|
|
const [
|
|
|
|
storyDistributionLists,
|
|
|
|
uninstalledStickerPacks,
|
|
|
|
installedStickerPacks,
|
|
|
|
] = await Promise.all([
|
|
|
|
dataInterface.getAllStoryDistributionsWithMembers(),
|
|
|
|
dataInterface.getUninstalledStickerPacks(),
|
|
|
|
dataInterface.getInstalledStickerPacks(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
storyDistributionLists,
|
|
|
|
uninstalledStickerPacks,
|
|
|
|
installedStickerPacks,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
async function processManifest(
|
2022-02-08 18:00:18 +00:00
|
|
|
manifest: Proto.IManifestRecord,
|
|
|
|
version: number
|
2022-02-14 19:36:32 +00:00
|
|
|
): Promise<number> {
|
2020-09-09 00:56:23 +00:00
|
|
|
if (!window.textsecure.messaging) {
|
|
|
|
throw new Error('storageService.processManifest: We are offline!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteKeysTypeMap = new Map();
|
2021-07-13 18:54:53 +00:00
|
|
|
(manifest.keys || []).forEach(({ raw, type }: IManifestRecordIdentifier) => {
|
|
|
|
strictAssert(raw, 'Identifier without raw field');
|
|
|
|
remoteKeysTypeMap.set(Bytes.toBase64(raw), type);
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
const remoteKeys = new Set(remoteKeysTypeMap.keys());
|
2022-02-08 18:00:18 +00:00
|
|
|
const localVersions = new Map<string, number | undefined>();
|
2022-08-03 17:10:49 +00:00
|
|
|
let localRecordCount = 0;
|
2021-04-08 19:27:20 +00:00
|
|
|
|
|
|
|
const conversations = window.getConversations();
|
|
|
|
conversations.forEach((conversation: ConversationModel) => {
|
|
|
|
const storageID = conversation.get('storageID');
|
|
|
|
if (storageID) {
|
2022-02-08 18:00:18 +00:00
|
|
|
localVersions.set(storageID, conversation.get('storageVersion'));
|
2021-04-08 19:27:20 +00:00
|
|
|
}
|
|
|
|
});
|
2022-08-03 17:10:49 +00:00
|
|
|
localRecordCount += conversations.length;
|
|
|
|
|
|
|
|
{
|
|
|
|
const {
|
|
|
|
storyDistributionLists,
|
|
|
|
installedStickerPacks,
|
|
|
|
uninstalledStickerPacks,
|
|
|
|
} = await getNonConversationRecords();
|
|
|
|
|
|
|
|
const collectLocalKeysFromFields = ({
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
}: StorageServiceFieldsType): void => {
|
|
|
|
if (storageID) {
|
|
|
|
localVersions.set(storageID, storageVersion);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
storyDistributionLists.forEach(collectLocalKeysFromFields);
|
|
|
|
localRecordCount += storyDistributionLists.length;
|
|
|
|
|
|
|
|
uninstalledStickerPacks.forEach(collectLocalKeysFromFields);
|
|
|
|
localRecordCount += uninstalledStickerPacks.length;
|
|
|
|
|
|
|
|
installedStickerPacks.forEach(collectLocalKeysFromFields);
|
|
|
|
localRecordCount += installedStickerPacks.length;
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const unknownRecordsArray: ReadonlyArray<UnknownRecord> =
|
2020-09-09 00:56:23 +00:00
|
|
|
window.storage.get('storage-service-unknown-records') || [];
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
const stillUnknown = unknownRecordsArray.filter((record: UnknownRecord) => {
|
2020-09-29 23:29:11 +00:00
|
|
|
// Do not include any unknown records that we already support
|
|
|
|
if (!validRecordTypes.has(record.itemType)) {
|
2022-02-08 18:00:18 +00:00
|
|
|
localVersions.set(record.storageID, record.storageVersion);
|
2021-04-08 19:27:20 +00:00
|
|
|
return false;
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
2021-04-08 19:27:20 +00:00
|
|
|
return true;
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
const remoteOnlySet = new Set<string>();
|
|
|
|
for (const key of remoteKeys) {
|
|
|
|
if (!localVersions.has(key)) {
|
|
|
|
remoteOnlySet.add(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const localOnlySet = new Set<string>();
|
|
|
|
for (const key of localVersions.keys()) {
|
|
|
|
if (!remoteKeys.has(key)) {
|
|
|
|
localOnlySet.add(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const redactedRemoteOnly = Array.from(remoteOnlySet).map(id =>
|
|
|
|
redactStorageID(id, version)
|
|
|
|
);
|
|
|
|
const redactedLocalOnly = Array.from(localOnlySet).map(id =>
|
|
|
|
redactStorageID(id, localVersions.get(id))
|
|
|
|
);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-08-03 17:10:49 +00:00
|
|
|
`storageService.process(${version}): localRecords=${localRecordCount} ` +
|
2022-02-08 18:00:18 +00:00
|
|
|
`localKeys=${localVersions.size} unknownKeys=${stillUnknown.length} ` +
|
|
|
|
`remoteKeys=${remoteKeys.size}`
|
2021-04-08 19:27:20 +00:00
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${version}): ` +
|
|
|
|
`remoteOnlyCount=${remoteOnlySet.size} ` +
|
|
|
|
`remoteOnlyKeys=${JSON.stringify(redactedRemoteOnly)}`
|
2021-04-08 19:27:20 +00:00
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${version}): ` +
|
|
|
|
`localOnlyCount=${localOnlySet.size} ` +
|
|
|
|
`localOnlyKeys=${JSON.stringify(redactedLocalOnly)}`
|
2021-04-08 19:27:20 +00:00
|
|
|
);
|
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const remoteOnlyRecords = new Map<string, RemoteRecord>();
|
|
|
|
remoteOnlySet.forEach(storageID => {
|
|
|
|
remoteOnlyRecords.set(storageID, {
|
|
|
|
storageID,
|
|
|
|
itemType: remoteKeysTypeMap.get(storageID),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
let conflictCount = 0;
|
|
|
|
if (remoteOnlyRecords.size) {
|
2022-10-08 00:19:02 +00:00
|
|
|
const fetchResult = await fetchRemoteRecords(version, remoteOnlyRecords);
|
|
|
|
conflictCount = await processRemoteRecords(version, fetchResult);
|
2021-10-02 00:01:30 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 19:27:20 +00:00
|
|
|
// Post-merge, if our local records contain any storage IDs that were not
|
|
|
|
// present in the remote manifest then we'll need to clear it, generate a
|
|
|
|
// new storageID for that record, and upload.
|
|
|
|
// This might happen if a device pushes a manifest which doesn't contain
|
|
|
|
// the keys that we have in our local database.
|
|
|
|
window.getConversations().forEach((conversation: ConversationModel) => {
|
|
|
|
const storageID = conversation.get('storageID');
|
|
|
|
if (storageID && !remoteKeys.has(storageID)) {
|
2022-02-08 18:00:18 +00:00
|
|
|
const storageVersion = conversation.get('storageVersion');
|
|
|
|
const missingKey = redactStorageID(
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
conversation
|
|
|
|
);
|
2022-09-19 18:47:49 +00:00
|
|
|
|
|
|
|
// Remote might have dropped this conversation already, but our value of
|
|
|
|
// `firstUnregisteredAt` is too high for us to drop it. Don't reupload it!
|
|
|
|
if (conversation.isUnregistered()) {
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${version}): localKey=${missingKey} is ` +
|
|
|
|
'unregistered and not in remote manifest'
|
|
|
|
);
|
|
|
|
conversation.setUnregistered({
|
|
|
|
timestamp: Date.now() - durations.MONTH,
|
|
|
|
fromStorageService: true,
|
|
|
|
|
|
|
|
// Saving below
|
|
|
|
shouldSave: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${version}): localKey=${missingKey} ` +
|
|
|
|
'was not in remote manifest'
|
|
|
|
);
|
|
|
|
}
|
2021-04-09 20:12:05 +00:00
|
|
|
conversation.unset('storageID');
|
2022-02-08 18:00:18 +00:00
|
|
|
conversation.unset('storageVersion');
|
2021-04-08 19:27:20 +00:00
|
|
|
updateConversation(conversation.attributes);
|
|
|
|
}
|
|
|
|
});
|
2021-03-17 22:10:31 +00:00
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
// Refetch various records post-merge
|
|
|
|
{
|
|
|
|
const {
|
|
|
|
storyDistributionLists,
|
|
|
|
installedStickerPacks,
|
|
|
|
uninstalledStickerPacks,
|
|
|
|
} = await getNonConversationRecords();
|
|
|
|
|
|
|
|
uninstalledStickerPacks.forEach(stickerPack => {
|
|
|
|
const { storageID, storageVersion } = stickerPack;
|
|
|
|
if (!storageID || remoteKeys.has(storageID)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
const missingKey = redactStorageID(storageID, storageVersion);
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${version}): localKey=${missingKey} was not ` +
|
|
|
|
'in remote manifest'
|
|
|
|
);
|
|
|
|
dataInterface.addUninstalledStickerPack({
|
|
|
|
...stickerPack,
|
|
|
|
storageID: undefined,
|
|
|
|
storageVersion: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
installedStickerPacks.forEach(stickerPack => {
|
|
|
|
const { storageID, storageVersion } = stickerPack;
|
|
|
|
if (!storageID || remoteKeys.has(storageID)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const missingKey = redactStorageID(storageID, storageVersion);
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${version}): localKey=${missingKey} was not ` +
|
|
|
|
'in remote manifest'
|
|
|
|
);
|
|
|
|
dataInterface.createOrUpdateStickerPack({
|
|
|
|
...stickerPack,
|
|
|
|
storageID: undefined,
|
|
|
|
storageVersion: undefined,
|
|
|
|
});
|
|
|
|
});
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
storyDistributionLists.forEach(storyDistributionList => {
|
|
|
|
const { storageID, storageVersion } = storyDistributionList;
|
|
|
|
if (!storageID || remoteKeys.has(storageID)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
const missingKey = redactStorageID(storageID, storageVersion);
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${version}): localKey=${missingKey} was not ` +
|
|
|
|
'in remote manifest'
|
|
|
|
);
|
|
|
|
dataInterface.modifyStoryDistribution({
|
|
|
|
...storyDistributionList,
|
|
|
|
storageID: undefined,
|
|
|
|
storageVersion: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check to make sure we have a "My Stories" distribution list set up
|
|
|
|
const myStories = storyDistributionLists.find(
|
|
|
|
({ id }) => id === MY_STORIES_ID
|
2022-07-21 00:07:09 +00:00
|
|
|
);
|
|
|
|
|
2022-08-03 17:10:49 +00:00
|
|
|
if (!myStories) {
|
2022-09-19 22:08:55 +00:00
|
|
|
log.info(`storageService.process(${version}): creating my stories`);
|
2022-08-03 17:10:49 +00:00
|
|
|
const storyDistribution: StoryDistributionWithMembersType = {
|
|
|
|
allowsReplies: true,
|
|
|
|
id: MY_STORIES_ID,
|
|
|
|
isBlockList: true,
|
|
|
|
members: [],
|
|
|
|
name: MY_STORIES_ID,
|
|
|
|
senderKeyInfo: undefined,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
await dataInterface.createNewStoryDistribution(storyDistribution);
|
|
|
|
|
|
|
|
const shouldSave = false;
|
|
|
|
window.reduxActions.storyDistributionLists.createDistributionList(
|
|
|
|
storyDistribution.name,
|
|
|
|
storyDistribution.members,
|
|
|
|
storyDistribution,
|
|
|
|
shouldSave
|
|
|
|
);
|
|
|
|
|
|
|
|
conflictCount += 1;
|
|
|
|
}
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(
|
2022-02-14 19:36:32 +00:00
|
|
|
`storageService.process(${version}): conflictCount=${conflictCount}`
|
2022-02-08 18:00:18 +00:00
|
|
|
);
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
return conflictCount;
|
2021-03-17 22:10:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
export type FetchRemoteRecordsResultType = Readonly<{
|
|
|
|
missingKeys: Set<string>;
|
|
|
|
decryptedItems: ReadonlyArray<MergeableItemType>;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
async function fetchRemoteRecords(
|
2022-02-08 18:00:18 +00:00
|
|
|
storageVersion: number,
|
2021-04-09 20:12:05 +00:00
|
|
|
remoteOnlyRecords: Map<string, RemoteRecord>
|
2022-10-08 00:19:02 +00:00
|
|
|
): Promise<FetchRemoteRecordsResultType> {
|
2021-03-17 22:10:31 +00:00
|
|
|
const storageKeyBase64 = window.storage.get('storageKey');
|
2021-06-15 00:09:37 +00:00
|
|
|
if (!storageKeyBase64) {
|
|
|
|
throw new Error('No storage key');
|
|
|
|
}
|
2022-06-13 21:39:35 +00:00
|
|
|
const { messaging } = window.textsecure;
|
|
|
|
if (!messaging) {
|
|
|
|
throw new Error('messaging is not available');
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageKey = Bytes.fromBase64(storageKeyBase64);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-10-08 00:19:02 +00:00
|
|
|
`storageService.fetchRemoteRecords(${storageVersion}): ` +
|
|
|
|
`fetching remote keys count=${remoteOnlyRecords.size}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const credentials = window.storage.get('storageCredentials');
|
2022-04-13 00:50:17 +00:00
|
|
|
const batches = chunk(Array.from(remoteOnlyRecords.keys()), MAX_READ_KEYS);
|
|
|
|
|
|
|
|
const storageItems = (
|
|
|
|
await pMap(
|
|
|
|
batches,
|
|
|
|
async (
|
|
|
|
batch: ReadonlyArray<string>
|
|
|
|
): Promise<Array<Proto.IStorageItem>> => {
|
|
|
|
const readOperation = new Proto.ReadOperation();
|
|
|
|
readOperation.readKey = batch.map(Bytes.fromBase64);
|
|
|
|
|
2022-06-13 21:39:35 +00:00
|
|
|
const storageItemsBuffer = await messaging.getStorageRecords(
|
|
|
|
Proto.ReadOperation.encode(readOperation).finish(),
|
|
|
|
{
|
|
|
|
credentials,
|
|
|
|
}
|
|
|
|
);
|
2022-04-13 00:50:17 +00:00
|
|
|
|
|
|
|
return Proto.StorageItems.decode(storageItemsBuffer).items ?? [];
|
|
|
|
},
|
|
|
|
{ concurrency: 5 }
|
|
|
|
)
|
|
|
|
).flat();
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-02-11 21:05:24 +00:00
|
|
|
const missingKeys = new Set<string>(remoteOnlyRecords.keys());
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
const decryptedItems = await pMap(
|
2022-04-13 00:50:17 +00:00
|
|
|
storageItems,
|
2020-09-29 22:07:03 +00:00
|
|
|
async (
|
2021-07-13 18:54:53 +00:00
|
|
|
storageRecordWrapper: Proto.IStorageItem
|
2020-09-29 22:07:03 +00:00
|
|
|
): Promise<MergeableItemType> => {
|
2020-09-09 00:56:23 +00:00
|
|
|
const { key, value: storageItemCiphertext } = storageRecordWrapper;
|
|
|
|
|
|
|
|
if (!key || !storageItemCiphertext) {
|
2022-02-08 18:00:18 +00:00
|
|
|
const error = new Error(
|
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
'missing key and/or Ciphertext'
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
2022-02-08 18:00:18 +00:00
|
|
|
await stopStorageServiceSync(error);
|
|
|
|
throw error;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
const base64ItemID = Bytes.toBase64(key);
|
2022-02-11 21:05:24 +00:00
|
|
|
missingKeys.delete(base64ItemID);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageItemKey = deriveStorageItemKey(storageKey, base64ItemID);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
let storageItemPlaintext;
|
|
|
|
try {
|
2021-09-24 00:49:05 +00:00
|
|
|
storageItemPlaintext = decryptProfile(
|
|
|
|
storageItemCiphertext,
|
2020-09-09 00:56:23 +00:00
|
|
|
storageItemKey
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
'Error decrypting storage item',
|
|
|
|
Errors.toLogFormat(err)
|
2020-09-09 23:07:58 +00:00
|
|
|
);
|
2022-02-08 18:00:18 +00:00
|
|
|
await stopStorageServiceSync(err);
|
2020-09-09 00:56:23 +00:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const storageRecord = Proto.StorageRecord.decode(storageItemPlaintext);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
const remoteRecord = remoteOnlyRecords.get(base64ItemID);
|
|
|
|
if (!remoteRecord) {
|
|
|
|
throw new Error(
|
|
|
|
"Got a remote record that wasn't requested with " +
|
|
|
|
`storageID: ${base64ItemID}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
return {
|
2021-03-17 22:10:31 +00:00
|
|
|
itemType: remoteRecord.itemType,
|
2020-09-09 00:56:23 +00:00
|
|
|
storageID: base64ItemID,
|
|
|
|
storageRecord,
|
|
|
|
};
|
|
|
|
},
|
2020-09-29 23:29:11 +00:00
|
|
|
{ concurrency: 5 }
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2022-02-11 21:05:24 +00:00
|
|
|
const redactedMissingKeys = Array.from(missingKeys).map(id =>
|
|
|
|
redactStorageID(id, storageVersion)
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info(
|
2022-10-08 00:19:02 +00:00
|
|
|
`storageService.fetchRemoteRecords(${storageVersion}): missing remote ` +
|
2022-02-11 21:05:24 +00:00
|
|
|
`keys=${JSON.stringify(redactedMissingKeys)} ` +
|
|
|
|
`count=${missingKeys.size}`
|
|
|
|
);
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
return { decryptedItems, missingKeys };
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processRemoteRecords(
|
|
|
|
storageVersion: number,
|
|
|
|
{ decryptedItems, missingKeys }: FetchRemoteRecordsResultType
|
|
|
|
): Promise<number> {
|
2022-02-22 22:30:59 +00:00
|
|
|
const ITEM_TYPE = Proto.ManifestRecord.Identifier.Type;
|
2022-02-11 21:05:24 +00:00
|
|
|
const droppedKeys = new Set<string>();
|
|
|
|
|
2022-02-22 22:30:59 +00:00
|
|
|
// Drop all GV1 records for which we have GV2 record in the same manifest
|
|
|
|
const masterKeys = new Map<string, string>();
|
2022-10-08 00:19:02 +00:00
|
|
|
for (const { itemType, storageID, storageRecord } of decryptedItems) {
|
2022-02-22 22:30:59 +00:00
|
|
|
if (itemType === ITEM_TYPE.GROUPV2 && storageRecord.groupV2?.masterKey) {
|
|
|
|
masterKeys.set(
|
|
|
|
Bytes.toBase64(storageRecord.groupV2.masterKey),
|
|
|
|
storageID
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let accountItem: MergeableItemType | undefined;
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
const prunedStorageItems = decryptedItems.filter(item => {
|
2022-02-22 22:30:59 +00:00
|
|
|
const { itemType, storageID, storageRecord } = item;
|
|
|
|
if (itemType === ITEM_TYPE.ACCOUNT) {
|
|
|
|
if (accountItem !== undefined) {
|
|
|
|
log.warn(
|
|
|
|
`storageService.process(${storageVersion}): duplicate account ` +
|
|
|
|
`record=${redactStorageID(storageID, storageVersion)} ` +
|
|
|
|
`previous=${redactStorageID(accountItem.storageID, storageVersion)}`
|
|
|
|
);
|
|
|
|
droppedKeys.add(accountItem.storageID);
|
|
|
|
}
|
|
|
|
|
|
|
|
accountItem = item;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemType !== ITEM_TYPE.GROUPV1 || !storageRecord.groupV1?.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const masterKey = deriveMasterKeyFromGroupV1(storageRecord.groupV1.id);
|
|
|
|
const gv2StorageID = masterKeys.get(Bytes.toBase64(masterKey));
|
|
|
|
if (!gv2StorageID) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
log.warn(
|
|
|
|
`storageService.process(${storageVersion}): dropping ` +
|
|
|
|
`GV1 record=${redactStorageID(storageID, storageVersion)} ` +
|
|
|
|
`GV2 record=${redactStorageID(gv2StorageID, storageVersion)} ` +
|
|
|
|
'is in the same manifest'
|
|
|
|
);
|
|
|
|
droppedKeys.add(storageID);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
2020-09-29 22:07:03 +00:00
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
try {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
2022-02-22 22:30:59 +00:00
|
|
|
`attempting to merge records=${prunedStorageItems.length}`
|
2020-09-16 18:04:28 +00:00
|
|
|
);
|
2022-03-01 02:43:45 +00:00
|
|
|
if (accountItem !== undefined) {
|
2022-02-22 22:30:59 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.process(${storageVersion}): account ` +
|
|
|
|
`record=${redactStorageID(accountItem.storageID, storageVersion)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const mergedRecords = [
|
|
|
|
...(await pMap(
|
|
|
|
prunedStorageItems,
|
|
|
|
(item: MergeableItemType) => mergeRecord(storageVersion, item),
|
2022-03-09 18:22:34 +00:00
|
|
|
{ concurrency: 32 }
|
2022-02-22 22:30:59 +00:00
|
|
|
)),
|
|
|
|
|
|
|
|
// Merge Account records last since it contains the pinned conversations
|
|
|
|
// and we need all other records merged first before we can find the pinned
|
|
|
|
// records in our db
|
|
|
|
...(accountItem ? [await mergeRecord(storageVersion, accountItem)] : []),
|
|
|
|
];
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`processed records=${mergedRecords.length}`
|
2020-09-16 18:04:28 +00:00
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-03-09 18:22:34 +00:00
|
|
|
const updatedConversations = mergedRecords
|
|
|
|
.map(record => record.updatedConversations)
|
|
|
|
.flat()
|
|
|
|
.map(convo => convo.attributes);
|
|
|
|
await updateConversations(updatedConversations);
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`updated conversations=${updatedConversations.length}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const needProfileFetch = mergedRecords
|
|
|
|
.map(record => record.needProfileFetch)
|
|
|
|
.flat();
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`kicking off profile fetches=${needProfileFetch.length}`
|
|
|
|
);
|
|
|
|
|
|
|
|
// Intentionally not awaiting
|
2022-07-14 00:46:46 +00:00
|
|
|
needProfileFetch.map(convo => convo.getProfiles());
|
2022-03-09 18:22:34 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
// Collect full map of previously and currently unknown records
|
2020-09-09 00:56:23 +00:00
|
|
|
const unknownRecords: Map<string, UnknownRecord> = new Map();
|
2021-03-17 22:10:31 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
const previousUnknownRecords: ReadonlyArray<UnknownRecord> =
|
2021-11-11 22:43:05 +00:00
|
|
|
window.storage.get(
|
|
|
|
'storage-service-unknown-records',
|
|
|
|
new Array<UnknownRecord>()
|
|
|
|
);
|
2022-02-08 18:00:18 +00:00
|
|
|
previousUnknownRecords.forEach((record: UnknownRecord) => {
|
2020-09-09 00:56:23 +00:00
|
|
|
unknownRecords.set(record.storageID, record);
|
|
|
|
});
|
|
|
|
|
2020-10-06 22:25:00 +00:00
|
|
|
const newRecordsWithErrors: Array<UnknownRecord> = [];
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-03-17 22:10:31 +00:00
|
|
|
let conflictCount = 0;
|
2020-09-29 23:29:11 +00:00
|
|
|
|
|
|
|
mergedRecords.forEach((mergedRecord: MergedRecordType) => {
|
2020-09-09 00:56:23 +00:00
|
|
|
if (mergedRecord.isUnsupported) {
|
|
|
|
unknownRecords.set(mergedRecord.storageID, {
|
|
|
|
itemType: mergedRecord.itemType,
|
|
|
|
storageID: mergedRecord.storageID,
|
2022-02-08 18:00:18 +00:00
|
|
|
storageVersion,
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
2020-09-29 23:29:11 +00:00
|
|
|
} else if (mergedRecord.hasError) {
|
2020-10-06 22:25:00 +00:00
|
|
|
newRecordsWithErrors.push({
|
2020-09-29 23:29:11 +00:00
|
|
|
itemType: mergedRecord.itemType,
|
|
|
|
storageID: mergedRecord.storageID,
|
2022-02-08 18:00:18 +00:00
|
|
|
storageVersion,
|
2020-09-29 23:29:11 +00:00
|
|
|
});
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
if (mergedRecord.hasConflict) {
|
2021-03-17 22:10:31 +00:00
|
|
|
conflictCount += 1;
|
|
|
|
}
|
2022-02-11 21:05:24 +00:00
|
|
|
|
|
|
|
if (mergedRecord.shouldDrop) {
|
|
|
|
droppedKeys.add(mergedRecord.storageID);
|
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
});
|
|
|
|
|
2022-02-11 21:05:24 +00:00
|
|
|
const redactedDroppedKeys = Array.from(droppedKeys.values()).map(key =>
|
|
|
|
redactStorageID(key, storageVersion)
|
|
|
|
);
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${storageVersion}): ` +
|
2022-02-14 19:36:32 +00:00
|
|
|
`dropped keys=${JSON.stringify(redactedDroppedKeys)} ` +
|
2022-02-11 21:05:24 +00:00
|
|
|
`count=${redactedDroppedKeys.length}`
|
|
|
|
);
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
// Filter out all the unknown records we're already supporting
|
|
|
|
const newUnknownRecords = Array.from(unknownRecords.values()).filter(
|
|
|
|
(record: UnknownRecord) => !validRecordTypes.has(record.itemType)
|
|
|
|
);
|
2022-02-14 19:36:32 +00:00
|
|
|
const redactedNewUnknowns = newUnknownRecords.map(redactExtendedStorageID);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`unknown records=${JSON.stringify(redactedNewUnknowns)} ` +
|
|
|
|
`count=${redactedNewUnknowns.length}`
|
|
|
|
);
|
|
|
|
await window.storage.put(
|
|
|
|
'storage-service-unknown-records',
|
|
|
|
newUnknownRecords
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
const redactedErrorRecords = newRecordsWithErrors.map(
|
2022-02-14 19:36:32 +00:00
|
|
|
redactExtendedStorageID
|
2022-02-08 18:00:18 +00:00
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`error records=${JSON.stringify(redactedErrorRecords)} ` +
|
|
|
|
`count=${redactedErrorRecords.length}`
|
2020-09-29 23:29:11 +00:00
|
|
|
);
|
2020-10-06 22:25:00 +00:00
|
|
|
// Refresh the list of records that had errors with every push, that way
|
|
|
|
// this list doesn't grow unbounded and we keep the list of storage keys
|
|
|
|
// fresh.
|
2022-02-08 18:00:18 +00:00
|
|
|
await window.storage.put(
|
|
|
|
'storage-service-error-records',
|
|
|
|
newRecordsWithErrors
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
// Store/overwrite keys pending deletion, but use them only when we have to
|
|
|
|
// upload a new manifest to avoid oscillation.
|
|
|
|
const pendingDeletes = [...missingKeys, ...droppedKeys].map(storageID => ({
|
|
|
|
storageID,
|
|
|
|
storageVersion,
|
|
|
|
}));
|
|
|
|
const redactedPendingDeletes = pendingDeletes.map(redactExtendedStorageID);
|
|
|
|
log.info(
|
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
`pending deletes=${JSON.stringify(redactedPendingDeletes)} ` +
|
|
|
|
`count=${redactedPendingDeletes.length}`
|
|
|
|
);
|
|
|
|
await window.storage.put('storage-service-pending-deletes', pendingDeletes);
|
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
if (conflictCount === 0) {
|
|
|
|
conflictBackOff.reset();
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-09-09 02:25:05 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
return conflictCount;
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2022-02-08 18:00:18 +00:00
|
|
|
`storageService.process(${storageVersion}): ` +
|
|
|
|
'failed to process remote records',
|
|
|
|
Errors.toLogFormat(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
// conflictCount
|
|
|
|
return 0;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 22:25:49 +00:00
|
|
|
async function sync(
|
|
|
|
ignoreConflicts = false
|
|
|
|
): Promise<Proto.ManifestRecord | undefined> {
|
2020-09-09 00:56:23 +00:00
|
|
|
if (!window.storage.get('storageKey')) {
|
2020-09-29 23:29:11 +00:00
|
|
|
throw new Error('storageService.sync: Cannot start; no storage key!');
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 22:30:59 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.sync: starting... ignoreConflicts=${ignoreConflicts}`
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
let manifest: Proto.ManifestRecord | undefined;
|
2020-09-09 00:56:23 +00:00
|
|
|
try {
|
2020-10-29 00:40:50 +00:00
|
|
|
// If we've previously interacted with strage service, update 'fetchComplete' record
|
|
|
|
const previousFetchComplete = window.storage.get('storageFetchComplete');
|
|
|
|
const manifestFromStorage = window.storage.get('manifestVersion');
|
|
|
|
if (!previousFetchComplete && isNumber(manifestFromStorage)) {
|
|
|
|
window.storage.put('storageFetchComplete', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const localManifestVersion = manifestFromStorage || 0;
|
2022-01-04 15:22:48 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(
|
|
|
|
'storageService.sync: fetching latest ' +
|
|
|
|
`after version=${localManifestVersion}`
|
|
|
|
);
|
2021-04-09 20:12:05 +00:00
|
|
|
manifest = await fetchManifest(localManifestVersion);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
// Guarding against no manifests being returned, everything should be ok
|
|
|
|
if (!manifest) {
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.sync: no updates, version=${localManifestVersion}`
|
|
|
|
);
|
2021-04-09 20:12:05 +00:00
|
|
|
return undefined;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 21:40:44 +00:00
|
|
|
strictAssert(manifest.version != null, 'Manifest without version');
|
2022-03-23 20:49:27 +00:00
|
|
|
const version = manifest.version?.toNumber() ?? 0;
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2022-05-25 19:20:45 +00:00
|
|
|
`storageService.sync: updating to remoteVersion=${version} ` +
|
|
|
|
`sourceDevice=${manifest.sourceDevice ?? '?'} from ` +
|
2022-02-08 18:00:18 +00:00
|
|
|
`version=${localManifestVersion}`
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
const conflictCount = await processManifest(manifest, version);
|
2022-01-04 15:22:48 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.sync: updated to version=${version} ` +
|
2022-02-14 19:36:32 +00:00
|
|
|
`conflicts=${conflictCount}`
|
2022-02-08 18:00:18 +00:00
|
|
|
);
|
2022-01-04 15:22:48 +00:00
|
|
|
|
2022-02-08 18:00:18 +00:00
|
|
|
await window.storage.put('manifestVersion', version);
|
2020-09-29 23:29:11 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
const hasConflicts = conflictCount !== 0;
|
2021-09-16 22:25:49 +00:00
|
|
|
if (hasConflicts && !ignoreConflicts) {
|
2022-02-14 19:36:32 +00:00
|
|
|
await upload(true);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2020-10-29 00:40:50 +00:00
|
|
|
|
|
|
|
// We now know that we've successfully completed a storage service fetch
|
2022-02-08 18:00:18 +00:00
|
|
|
await window.storage.put('storageFetchComplete', true);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2020-10-07 23:44:55 +00:00
|
|
|
'storageService.sync: error processing manifest',
|
2022-02-08 18:00:18 +00:00
|
|
|
Errors.toLogFormat(err)
|
2020-09-09 00:56:23 +00:00
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
2020-09-09 00:56:23 +00:00
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.sync: complete');
|
2021-04-09 20:12:05 +00:00
|
|
|
return manifest;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
async function upload(fromSync = false): Promise<void> {
|
2020-09-09 23:07:58 +00:00
|
|
|
if (!window.textsecure.messaging) {
|
2020-09-29 23:29:11 +00:00
|
|
|
throw new Error('storageService.upload: We are offline!');
|
2020-09-09 23:07:58 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
// Rate limit uploads coming from syncing
|
|
|
|
if (fromSync) {
|
|
|
|
uploadBucket.push(Date.now());
|
|
|
|
if (uploadBucket.length >= 3) {
|
|
|
|
const [firstMostRecentWrite] = uploadBucket;
|
|
|
|
|
2021-08-26 14:10:58 +00:00
|
|
|
if (isMoreRecentThan(5 * durations.MINUTE, firstMostRecentWrite)) {
|
2021-04-09 20:12:05 +00:00
|
|
|
throw new Error(
|
|
|
|
'storageService.uploadManifest: too many writes too soon.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadBucket.shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-09 00:56:23 +00:00
|
|
|
if (!window.storage.get('storageKey')) {
|
2020-09-09 23:07:58 +00:00
|
|
|
// requesting new keys runs the sync job which will detect the conflict
|
|
|
|
// and re-run the upload job once we're merged and up-to-date.
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.upload: no storageKey, requesting new keys');
|
2021-06-09 22:28:54 +00:00
|
|
|
backOff.reset();
|
2021-07-15 23:48:09 +00:00
|
|
|
|
|
|
|
if (window.ConversationController.areWePrimaryDevice()) {
|
2022-01-14 21:34:52 +00:00
|
|
|
log.warn(
|
|
|
|
'storageService.upload: We are primary device; not sending key sync request'
|
|
|
|
);
|
2021-07-15 23:48:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-14 21:34:52 +00:00
|
|
|
try {
|
2022-06-13 21:39:35 +00:00
|
|
|
await singleProtoJobQueue.add(MessageSender.getRequestKeySyncMessage());
|
2022-01-14 21:34:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'storageService.upload: Failed to queue sync message',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-09 23:07:58 +00:00
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 18:54:53 +00:00
|
|
|
let previousManifest: Proto.ManifestRecord | undefined;
|
2021-03-12 17:38:43 +00:00
|
|
|
if (!fromSync) {
|
2021-03-31 00:16:28 +00:00
|
|
|
// Syncing before we upload so that we repair any unknown records and
|
|
|
|
// records with errors as well as ensure that we have the latest up to date
|
|
|
|
// manifest.
|
2021-09-16 22:25:49 +00:00
|
|
|
// We are going to upload after this sync so we can ignore any conflicts
|
|
|
|
// that arise during the sync.
|
|
|
|
const ignoreConflicts = true;
|
|
|
|
previousManifest = await sync(ignoreConflicts);
|
2021-03-12 17:38:43 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 00:09:37 +00:00
|
|
|
const localManifestVersion = window.storage.get('manifestVersion', 0);
|
2020-09-09 00:56:23 +00:00
|
|
|
const version = Number(localManifestVersion) + 1;
|
|
|
|
|
2022-02-11 21:05:24 +00:00
|
|
|
log.info(
|
|
|
|
`storageService.upload(${version}): will update to manifest version`
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
|
|
|
|
try {
|
2022-02-11 21:05:24 +00:00
|
|
|
const generatedManifest = await generateManifest(
|
|
|
|
version,
|
|
|
|
previousManifest,
|
2022-02-14 19:36:32 +00:00
|
|
|
false
|
2022-02-11 21:05:24 +00:00
|
|
|
);
|
2022-10-08 00:19:02 +00:00
|
|
|
const encryptedManifest = await encryptManifest(version, generatedManifest);
|
|
|
|
await uploadManifest(version, generatedManifest, encryptedManifest);
|
2022-02-22 22:30:59 +00:00
|
|
|
|
|
|
|
// Clear pending delete keys after successful upload
|
|
|
|
await window.storage.put('storage-service-pending-deletes', []);
|
2020-09-09 00:56:23 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 409) {
|
2021-06-09 22:28:54 +00:00
|
|
|
await sleep(conflictBackOff.getAndIncrement());
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.upload: pushing sync on the queue');
|
2020-09-29 23:29:11 +00:00
|
|
|
// The sync job will check for conflicts and as part of that conflict
|
|
|
|
// check if an item needs sync and doesn't match with the remote record
|
|
|
|
// it'll kick off another upload.
|
|
|
|
setTimeout(runStorageServiceSyncJob);
|
|
|
|
return;
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
2022-02-11 21:05:24 +00:00
|
|
|
log.error(
|
|
|
|
`storageService.upload(${version}): error`,
|
|
|
|
Errors.toLogFormat(err)
|
|
|
|
);
|
2020-09-09 00:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:29:11 +00:00
|
|
|
let storageServiceEnabled = false;
|
|
|
|
|
|
|
|
export function enableStorageService(): void {
|
|
|
|
storageServiceEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: this function is meant to be called before ConversationController is hydrated.
|
|
|
|
// It goes directly to the database, so in-memory conversations will be out of date.
|
2022-02-02 21:15:39 +00:00
|
|
|
export async function eraseAllStorageServiceState({
|
|
|
|
keepUnknownFields = false,
|
|
|
|
}: { keepUnknownFields?: boolean } = {}): Promise<void> {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.eraseAllStorageServiceState: starting...');
|
2020-09-29 23:29:11 +00:00
|
|
|
await Promise.all([
|
|
|
|
window.storage.remove('manifestVersion'),
|
2022-02-02 21:15:39 +00:00
|
|
|
keepUnknownFields
|
|
|
|
? Promise.resolve()
|
|
|
|
: window.storage.remove('storage-service-unknown-records'),
|
2020-09-29 23:29:11 +00:00
|
|
|
window.storage.remove('storageCredentials'),
|
|
|
|
]);
|
|
|
|
await eraseStorageServiceStateFromConversations();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.eraseAllStorageServiceState: complete');
|
2020-09-29 23:29:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-08 00:19:02 +00:00
|
|
|
export async function reprocessUnknownFields(): Promise<void> {
|
|
|
|
ourProfileKeyService.blockGetWithPromise(
|
|
|
|
storageJobQueue(async () => {
|
|
|
|
const version = window.storage.get('manifestVersion') ?? 0;
|
|
|
|
|
|
|
|
log.info(`storageService.reprocessUnknownFields(${version}): starting`);
|
|
|
|
|
|
|
|
const { recordsByID, insertKeys } = await generateManifest(
|
|
|
|
version,
|
|
|
|
undefined,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const newRecords = Array.from(
|
|
|
|
filter(
|
|
|
|
map(recordsByID, ([key, item]): MergeableItemType | undefined => {
|
|
|
|
if (!insertKeys.has(key)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
strictAssert(
|
|
|
|
item.storageRecord !== undefined,
|
|
|
|
'Inserted records must have storageRecord'
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!item.storageRecord.__unknownFields?.length) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
|
|
|
|
storageRecord: Proto.StorageRecord.decode(
|
|
|
|
Proto.StorageRecord.encode(item.storageRecord).finish()
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
isNotNil
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
const conflictCount = await processRemoteRecords(version, {
|
|
|
|
decryptedItems: newRecords,
|
|
|
|
missingKeys: new Set(),
|
|
|
|
});
|
|
|
|
|
|
|
|
log.info(
|
|
|
|
`storageService.reprocessUnknownFields(${version}): done, ` +
|
|
|
|
`conflictCount=${conflictCount}`
|
|
|
|
);
|
|
|
|
|
|
|
|
const hasConflicts = conflictCount !== 0;
|
|
|
|
if (hasConflicts) {
|
|
|
|
log.info(
|
|
|
|
`storageService.reprocessUnknownFields(${version}): uploading`
|
|
|
|
);
|
|
|
|
await upload();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-29 00:40:50 +00:00
|
|
|
export const storageServiceUploadJob = debounce(() => {
|
2020-09-29 23:29:11 +00:00
|
|
|
if (!storageServiceEnabled) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.storageServiceUploadJob: called before enabled');
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-09 20:12:05 +00:00
|
|
|
storageJobQueue(async () => {
|
|
|
|
await upload();
|
|
|
|
}, `upload v${window.storage.get('manifestVersion')}`);
|
2020-09-29 23:29:11 +00:00
|
|
|
}, 500);
|
|
|
|
|
2020-10-29 00:40:50 +00:00
|
|
|
export const runStorageServiceSyncJob = debounce(() => {
|
2020-09-29 23:29:11 +00:00
|
|
|
if (!storageServiceEnabled) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('storageService.runStorageServiceSyncJob: called before enabled');
|
2020-09-29 23:29:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 16:39:16 +00:00
|
|
|
ourProfileKeyService.blockGetWithPromise(
|
|
|
|
storageJobQueue(async () => {
|
|
|
|
await sync();
|
2022-03-02 22:53:47 +00:00
|
|
|
|
|
|
|
// Notify listeners about sync completion
|
|
|
|
window.Whisper.events.trigger('storageService:syncComplete');
|
2021-05-05 16:39:16 +00:00
|
|
|
}, `sync v${window.storage.get('manifestVersion')}`)
|
|
|
|
);
|
2020-09-29 23:29:11 +00:00
|
|
|
}, 500);
|