From 50dd9c6772f93e7dfdf5f1bee4df67ccf51dee31 Mon Sep 17 00:00:00 2001 From: lilia Date: Tue, 13 Jun 2017 12:38:44 -0700 Subject: [PATCH] Add identityKey model validation This will enforce that all identity record attributes are valid and present before allowing the record to be saved. This is necessary since we will be exposing a lower-level method to save an identity with explicit values for firstUse, nonblockingApproval, and verified status. --- js/signal_protocol_store.js | 56 ++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index f3509b216..86b71fc27 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -15,6 +15,15 @@ UNVERIFIED: 2, }; + function validateVerifiedStatus(status) { + if ( status === VerifiedStatus.DEFAULT + || status === VerifiedStatus.VERIFIED + || status === VerifiedStatus.UNVERIFIED) { + return true; + } + return false; + } + var StaticByteBufferProto = new dcodeIO.ByteBuffer().__proto__; var StaticArrayBufferProto = new ArrayBuffer().__proto__; var StaticUint8ArrayProto = new Uint8Array().__proto__; @@ -95,7 +104,52 @@ return this.fetch({range: [number + '.1', number + '.' + ':']}); } }); - var IdentityKey = Model.extend({ storeName: 'identityKeys' }); + var IdentityKey = Model.extend({ + storeName: 'identityKeys', + validAttributes: [ + 'id', + 'publicKey', + 'firstUse', + 'timestamp', + 'verified', + 'nonblockingApproval' + ], + validate: function(attrs, options) { + var attributeNames = _.keys(attrs); + var validAttributes = this.validAttributes; + var allValid = _.all(attributeNames, function(attributeName) { + return _.contains(validAttributes, attributeName); + }); + if (!allValid) { + return new Error("Invalid identity key attribute names"); + } + var allPresent = _.all(validAttributes, function(attributeName) { + return _.contains(attributeNames, attributeName); + }); + if (!allPresent) { + return new Error("Missing identity key attributes"); + } + + if (typeof attrs.id !== 'string') { + return new Error("Invalid identity key id"); + } + if (!(attrs.publicKey instanceof ArrayBuffer)) { + return new Error("Invalid identity key publicKey"); + } + if (typeof attrs.firstUse !== 'boolean') { + return new Error("Invalid identity key firstUse"); + } + if (typeof attrs.timestamp !== 'number' || !(attrs.timestamp >= 0)) { + return new Error("Invalid identity key timestamp"); + } + if (!validateVerifiedStatus(attrs.verified)) { + return new Error("Invalid identity key verified"); + } + if (typeof attrs.nonblockingApproval !== 'boolean') { + return new Error("Invalid identity key nonblockingApproval"); + } + } + }); var Group = Model.extend({ storeName: 'groups' }); var Item = Model.extend({ storeName: 'items' });