From 8f5086227a370ac2247d18a36afb7f5ce8682927 Mon Sep 17 00:00:00 2001 From: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com> Date: Mon, 14 Jun 2021 17:09:37 -0700 Subject: [PATCH] Enforce stronger types for ArrayBuffers and storage --- background.html | 4 - js/models/blockedNumbers.js | 101 ----------- js/storage.js | 131 -------------- libtextsecure/protocol_wrapper.js | 7 +- libtextsecure/storage/unprocessed.js | 43 ----- libtextsecure/storage/user.js | 70 -------- libtextsecure/test/index.html | 6 - sticker-creator/index.html | 3 - test/index.html | 6 - ts/RemoteConfig.ts | 2 +- ts/SignalProtocolStore.ts | 7 +- ts/background.ts | 57 +++--- ts/challenge.ts | 6 +- ts/groups.ts | 8 +- ts/jobs/removeStorageKeyJobQueue.ts | 2 +- ts/models/conversations.ts | 25 +-- ts/models/messages.ts | 5 +- ts/routineProfileRefresh.ts | 14 +- ts/services/calling.ts | 9 +- ts/services/ourProfileKey.ts | 12 +- ts/services/senderCertificate.ts | 12 +- ts/services/storage.ts | 30 +++- ts/services/storageRecordOps.ts | 6 +- ts/shims/storage.ts | 11 +- ts/sql/Client.ts | 38 ++-- ts/sql/Interface.ts | 17 +- ts/sql/Server.ts | 29 ++- ts/state/ducks/items.ts | 14 +- ts/test-both/challenge_test.ts | 4 +- ts/test-both/util/getProvisioningUrl_test.ts | 4 +- ts/test-both/util/retryPlaceholders_test.ts | 4 +- ts/test-both/util/synchronousCrypto_test.ts | 6 +- ts/test-electron/SignalProtocolStore_test.ts | 16 +- ts/test-electron/WebsocketResources_test.ts | 12 +- ts/test-electron/background_test.ts | 7 +- ts/test-electron/models/messages_test.ts | 4 +- .../routineProfileRefresh_test.ts | 2 +- ts/textsecure.d.ts | 45 +---- ts/textsecure/AccountManager.ts | 8 +- ts/textsecure/MessageReceiver.ts | 28 +-- ts/textsecure/OutgoingMessage.ts | 3 +- ts/textsecure/SendMessage.ts | 34 ++-- ts/textsecure/Storage.ts | 170 ++++++++++++++---- ts/textsecure/Types.d.ts | 12 ++ ts/textsecure/WebsocketResources.ts | 3 +- ts/textsecure/index.ts | 4 +- ts/textsecure/storage/Blocked.ts | 98 ++++++++++ ts/textsecure/storage/User.ts | 76 ++++++++ ts/types/Colors.ts | 5 + ts/types/Storage.d.ts | 133 ++++++++++++++ .../connectToServerWithStoredCredentials.ts | 8 +- ts/util/retryPlaceholders.ts | 2 +- ts/util/safetyNumber.ts | 5 +- ts/util/sendToGroup.ts | 5 +- ts/views/conversation_view.ts | 10 +- ts/window.d.ts | 40 ++--- 56 files changed, 748 insertions(+), 675 deletions(-) delete mode 100644 js/models/blockedNumbers.js delete mode 100644 js/storage.js delete mode 100644 libtextsecure/storage/unprocessed.js delete mode 100644 libtextsecure/storage/user.js create mode 100644 ts/textsecure/storage/Blocked.ts create mode 100644 ts/textsecure/storage/User.ts create mode 100644 ts/types/Storage.d.ts diff --git a/background.html b/background.html index e5e3aad232..c01692a931 100644 --- a/background.html +++ b/background.html @@ -332,11 +332,8 @@ - - - @@ -348,7 +345,6 @@ - diff --git a/js/models/blockedNumbers.js b/js/models/blockedNumbers.js deleted file mode 100644 index 852564663e..0000000000 --- a/js/models/blockedNumbers.js +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2016-2020 Signal Messenger, LLC -// SPDX-License-Identifier: AGPL-3.0-only - -/* global storage, _ */ - -// eslint-disable-next-line func-names -(function () { - const BLOCKED_NUMBERS_ID = 'blocked'; - const BLOCKED_UUIDS_ID = 'blocked-uuids'; - const BLOCKED_GROUPS_ID = 'blocked-groups'; - - function getArray(key) { - const result = storage.get(key, []); - - if (!Array.isArray(result)) { - window.log.error( - `Expected storage key ${JSON.stringify( - key - )} to contain an array or nothing` - ); - return []; - } - - return result; - } - - storage.getBlockedNumbers = () => getArray(BLOCKED_NUMBERS_ID); - storage.isBlocked = number => { - const numbers = storage.getBlockedNumbers(); - - return _.include(numbers, number); - }; - storage.addBlockedNumber = number => { - const numbers = storage.getBlockedNumbers(); - if (_.include(numbers, number)) { - return; - } - - window.log.info('adding', number, 'to blocked list'); - storage.put(BLOCKED_NUMBERS_ID, numbers.concat(number)); - }; - storage.removeBlockedNumber = number => { - const numbers = storage.getBlockedNumbers(); - if (!_.include(numbers, number)) { - return; - } - - window.log.info('removing', number, 'from blocked list'); - storage.put(BLOCKED_NUMBERS_ID, _.without(numbers, number)); - }; - - storage.getBlockedUuids = () => getArray(BLOCKED_UUIDS_ID); - storage.isUuidBlocked = uuid => { - const uuids = storage.getBlockedUuids(); - - return _.include(uuids, uuid); - }; - storage.addBlockedUuid = uuid => { - const uuids = storage.getBlockedUuids(); - if (_.include(uuids, uuid)) { - return; - } - - window.log.info('adding', uuid, 'to blocked list'); - storage.put(BLOCKED_UUIDS_ID, uuids.concat(uuid)); - }; - storage.removeBlockedUuid = uuid => { - const numbers = storage.getBlockedUuids(); - if (!_.include(numbers, uuid)) { - return; - } - - window.log.info('removing', uuid, 'from blocked list'); - storage.put(BLOCKED_UUIDS_ID, _.without(numbers, uuid)); - }; - - storage.getBlockedGroups = () => getArray(BLOCKED_GROUPS_ID); - storage.isGroupBlocked = groupId => { - const groupIds = storage.getBlockedGroups(); - - return _.include(groupIds, groupId); - }; - storage.addBlockedGroup = groupId => { - const groupIds = storage.getBlockedGroups(); - if (_.include(groupIds, groupId)) { - return; - } - - window.log.info(`adding group(${groupId}) to blocked list`); - storage.put(BLOCKED_GROUPS_ID, groupIds.concat(groupId)); - }; - storage.removeBlockedGroup = groupId => { - const groupIds = storage.getBlockedGroups(); - if (!_.include(groupIds, groupId)) { - return; - } - - window.log.info(`removing group(${groupId} from blocked list`); - storage.put(BLOCKED_GROUPS_ID, _.without(groupIds, groupId)); - }; -})(); diff --git a/js/storage.js b/js/storage.js deleted file mode 100644 index 47eb94b4b2..0000000000 --- a/js/storage.js +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2014-2020 Signal Messenger, LLC -// SPDX-License-Identifier: AGPL-3.0-only - -/* global _ */ -/* eslint-disable more/no-then */ - -// eslint-disable-next-line func-names -(function () { - window.Whisper = window.Whisper || {}; - - let ready = false; - let items; - let callbacks = []; - - reset(); - - async function put(key, value) { - if (value === undefined) { - window.log.warn(`storage/put: undefined provided for key ${key}`); - } - if (!ready) { - window.log.warn('Called storage.put before storage is ready. key:', key); - } - - const data = { id: key, value }; - - items[key] = data; - await window.Signal.Data.createOrUpdateItem(data); - - if (_.has(window, ['reduxActions', 'items', 'putItemExternal'])) { - window.reduxActions.items.putItemExternal(key, value); - } - } - - function get(key, defaultValue) { - if (!ready) { - window.log.warn('Called storage.get before storage is ready. key:', key); - } - - const item = items[key]; - if (!item) { - return defaultValue; - } - - return item.value; - } - - async function remove(key) { - if (!ready) { - window.log.warn( - 'Called storage.remove before storage is ready. key:', - key - ); - } - - delete items[key]; - await window.Signal.Data.removeItemById(key); - - if (_.has(window, ['reduxActions', 'items', 'removeItemExternal'])) { - window.reduxActions.items.removeItemExternal(key); - } - } - - function onready(callback) { - if (ready) { - callback(); - } else { - callbacks.push(callback); - } - } - - function callListeners() { - if (ready) { - callbacks.forEach(callback => callback()); - callbacks = []; - } - } - - async function fetch() { - this.reset(); - const array = await window.Signal.Data.getAllItems(); - - for (let i = 0, max = array.length; i < max; i += 1) { - const item = array[i]; - const { id } = item; - items[id] = item; - } - - ready = true; - callListeners(); - } - - function getItemsState() { - const data = _.clone(items); - const ids = Object.keys(data); - ids.forEach(id => { - data[id] = data[id].value; - }); - - return data; - } - - function reset() { - ready = false; - items = Object.create(null); - } - - const storage = { - fetch, - put, - get, - getItemsState, - remove, - onready, - reset, - }; - - // Keep a reference to this storage system, since there are scenarios where - // we need to replace it with the legacy storage system for a while. - window.newStorage = storage; - - window.textsecure = window.textsecure || {}; - window.textsecure.storage = window.textsecure.storage || {}; - - window.installStorage = newStorage => { - window.storage = newStorage; - window.textsecure.storage.impl = newStorage; - }; - - window.installStorage(storage); -})(); diff --git a/libtextsecure/protocol_wrapper.js b/libtextsecure/protocol_wrapper.js index 5445e5f557..0f43e029e5 100644 --- a/libtextsecure/protocol_wrapper.js +++ b/libtextsecure/protocol_wrapper.js @@ -1,12 +1,9 @@ // Copyright 2016-2020 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only -/* global window, textsecure, SignalProtocolStore */ +/* global window, SignalProtocolStore */ // eslint-disable-next-line func-names (function () { - window.textsecure = window.textsecure || {}; - window.textsecure.storage = window.textsecure.storage || {}; - - textsecure.storage.protocol = new SignalProtocolStore(); + window.textsecure.storage.protocol = new SignalProtocolStore(); })(); diff --git a/libtextsecure/storage/unprocessed.js b/libtextsecure/storage/unprocessed.js deleted file mode 100644 index 0ce444b4b9..0000000000 --- a/libtextsecure/storage/unprocessed.js +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2017-2020 Signal Messenger, LLC -// SPDX-License-Identifier: AGPL-3.0-only - -/* global window, textsecure */ - -// eslint-disable-next-line func-names -(function () { - /** *************************************** - *** Not-yet-processed message storage *** - **************************************** */ - window.textsecure = window.textsecure || {}; - window.textsecure.storage = window.textsecure.storage || {}; - - window.textsecure.storage.unprocessed = { - getCount() { - return textsecure.storage.protocol.getUnprocessedCount(); - }, - getAll() { - return textsecure.storage.protocol.getAllUnprocessed(); - }, - get(id) { - return textsecure.storage.protocol.getUnprocessedById(id); - }, - updateAttempts(id, attempts) { - return textsecure.storage.protocol.updateUnprocessedAttempts( - id, - attempts - ); - }, - addDecryptedData(id, data) { - return textsecure.storage.protocol.updateUnprocessedWithData(id, data); - }, - addDecryptedDataToList(array) { - return textsecure.storage.protocol.updateUnprocessedsWithData(array); - }, - remove(idOrArray) { - return textsecure.storage.protocol.removeUnprocessed(idOrArray); - }, - removeAll() { - return textsecure.storage.protocol.removeAllUnprocessed(); - }, - }; -})(); diff --git a/libtextsecure/storage/user.js b/libtextsecure/storage/user.js deleted file mode 100644 index 6a1ea686da..0000000000 --- a/libtextsecure/storage/user.js +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2015-2020 Signal Messenger, LLC -// SPDX-License-Identifier: AGPL-3.0-only - -/* global textsecure, window */ - -// eslint-disable-next-line func-names -(function () { - /** ******************************************* - *** Utilities to store data about the user *** - ********************************************* */ - window.textsecure = window.textsecure || {}; - window.textsecure.storage = window.textsecure.storage || {}; - - window.textsecure.storage.user = { - setNumberAndDeviceId(number, deviceId, deviceName) { - textsecure.storage.put('number_id', `${number}.${deviceId}`); - if (deviceName) { - textsecure.storage.put('device_name', deviceName); - } - }, - - setUuidAndDeviceId(uuid, deviceId) { - textsecure.storage.put('uuid_id', `${uuid}.${deviceId}`); - }, - - getNumber() { - const numberId = textsecure.storage.get('number_id'); - if (numberId === undefined) return undefined; - return textsecure.utils.unencodeNumber(numberId)[0]; - }, - - getUuid() { - const uuid = textsecure.storage.get('uuid_id'); - if (uuid === undefined) return undefined; - return textsecure.utils.unencodeNumber(uuid.toLowerCase())[0]; - }, - - getDeviceId() { - return this._getDeviceIdFromUuid() || this._getDeviceIdFromNumber(); - }, - - _getDeviceIdFromUuid() { - const uuid = textsecure.storage.get('uuid_id'); - if (uuid === undefined) return undefined; - return textsecure.utils.unencodeNumber(uuid)[1]; - }, - - _getDeviceIdFromNumber() { - const numberId = textsecure.storage.get('number_id'); - if (numberId === undefined) return undefined; - return textsecure.utils.unencodeNumber(numberId)[1]; - }, - - getDeviceName() { - return textsecure.storage.get('device_name'); - }, - - setDeviceNameEncrypted() { - return textsecure.storage.put('deviceNameEncrypted', true); - }, - - getDeviceNameEncrypted() { - return textsecure.storage.get('deviceNameEncrypted'); - }, - - getSignalingKey() { - return textsecure.storage.get('signaling_key'); - }, - }; -})(); diff --git a/libtextsecure/test/index.html b/libtextsecure/test/index.html index 9c59b3f2ba..bda21e922d 100644 --- a/libtextsecure/test/index.html +++ b/libtextsecure/test/index.html @@ -22,18 +22,12 @@ - - - - - - diff --git a/sticker-creator/index.html b/sticker-creator/index.html index 76eb698ab0..ef53cedcfc 100644 --- a/sticker-creator/index.html +++ b/sticker-creator/index.html @@ -10,11 +10,8 @@
- - -