From e993945612c44a2091439ba989b8853ce697fe54 Mon Sep 17 00:00:00 2001 From: Zhe Li Date: Sun, 4 Mar 2018 12:22:28 +0800 Subject: [PATCH 1/2] hotfix for 5.0.4 (#59) --- src/models/storage.ts | 180 ++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 86 deletions(-) diff --git a/src/models/storage.ts b/src/models/storage.ts index 3cb7d9c4..f638ed67 100644 --- a/src/models/storage.ts +++ b/src/models/storage.ts @@ -42,16 +42,12 @@ class EntryStorage { return newData; } - private static isOTPStorage(entry: object) { - const properties = [ - 'account', 'hash', 'index', 'issuer', 'type', 'counter', 'secret', - 'encrypted' - ]; - for (let i = 0; i < properties.length; i++) { - if (!entry.hasOwnProperty(properties[i])) { - return false; - } + /* tslint:disable-next-line:no-any */ + private static isOTPStorage(entry: any) { + if (!entry.hasOwnProperty('secret')) { + return false; } + return true; } @@ -252,91 +248,96 @@ class EntryStorage { (resolve: (value: OTPEntry[]) => void, reject: (reason: Error) => void) => { try { - chrome.storage.sync.get((_data: {[hash: string]: OTPStorage}) => { - const data: OTPEntry[] = []; - for (let hash of Object.keys(_data)) { - if (!this.isValidEntry(_data, hash)) { - continue; - } - const entryData = _data[hash]; - let needMigrate = false; + chrome.storage.sync.get( + async (_data: {[hash: string]: OTPStorage}) => { + const data: OTPEntry[] = []; + for (let hash of Object.keys(_data)) { + if (!this.isValidEntry(_data, hash)) { + continue; + } + const entryData = _data[hash]; + let needMigrate = false; - if (!entryData.type) { - entryData.type = OTPType[OTPType.totp]; - needMigrate = true; - } + if (!entryData.hash) { + entryData.hash = hash; + needMigrate = true; + } - let type: OTPType; - switch (entryData.type) { - case 'totp': - case 'hotp': - case 'battle': - case 'steam': - type = OTPType[entryData.type]; - break; - default: - // we need correct the type here - // and save it - type = OTPType.totp; - entryData.type = OTPType[OTPType.totp]; - needMigrate = true; - } - entryData.secret = entryData.encrypted ? - encryption.getDecryptedSecret(entryData.secret) : - entryData.secret; - - const entry = new OTPEntry( - type, entryData.issuer, entryData.secret, entryData.account, - entryData.index, entryData.counter); - data.push(entry); + if (!entryData.type) { + entryData.type = OTPType[OTPType.totp]; + needMigrate = true; + } - // we need migrate secret in old format here - if (/^(blz\-|bliz\-)/.test(entryData.secret)) { - const secretMatches = - entryData.secret.match(/^(blz\-|bliz\-)(.*)/); - if (secretMatches && secretMatches.length >= 3) { + let type: OTPType; + switch (entryData.type) { + case 'totp': + case 'hotp': + case 'battle': + case 'steam': + type = OTPType[entryData.type]; + break; + default: + // we need correct the type here + // and save it + type = OTPType.totp; + entryData.type = OTPType[OTPType.totp]; + needMigrate = true; + } entryData.secret = entryData.encrypted ? - secretMatches[2] : - encryption.getEncryptedSecret(entry.secret); - entryData.type = OTPType[OTPType.battle]; - needMigrate = true; - } - } + encryption.getDecryptedSecret(entryData.secret) : + entryData.secret; - if (/^stm\-/.test(entryData.secret)) { - const secretMatches = entryData.secret.match(/^stm\-(.*)/); - if (secretMatches && secretMatches.length >= 2) { - entryData.secret = entryData.encrypted ? - secretMatches[2] : - encryption.getEncryptedSecret(entry.secret); - entryData.type = OTPType[OTPType.steam]; - needMigrate = true; - } - } + // we need migrate secret in old format here + if (/^(blz\-|bliz\-)/.test(entryData.secret)) { + const secretMatches = + entryData.secret.match(/^(blz\-|bliz\-)(.*)/); + if (secretMatches && secretMatches.length >= 3) { + entryData.secret = secretMatches[2]; + entryData.type = OTPType[OTPType.battle]; + needMigrate = true; + } + } - // we need correct the hash - if (entry.secret !== 'Encrypted') { - const _hash = CryptoJS.MD5(entry.secret).toString(); - if (hash !== _hash) { - chrome.storage.sync.remove(hash); - hash = _hash; - entryData.hash = hash; - needMigrate = true; - } - } + if (/^stm\-/.test(entryData.secret)) { + const secretMatches = + entryData.secret.match(/^stm\-(.*)/); + if (secretMatches && secretMatches.length >= 2) { + entryData.secret = secretMatches[1]; + entryData.type = OTPType[OTPType.steam]; + needMigrate = true; + } + } - if (needMigrate) { - const _entry: {[hash: string]: OTPStorage} = {}; - _entry[hash] = entryData; - this.import(encryption, _entry); - } - } + const entry = new OTPEntry( + type, entryData.issuer, entryData.secret, + entryData.account, entryData.index, entryData.counter); - data.sort((a, b) => { - return a.index - b.index; - }); - return resolve(data); - }); + data.push(entry); + + // we need correct the hash + if (entry.secret !== 'Encrypted') { + const _hash = CryptoJS.MD5(entryData.secret).toString(); + if (hash !== _hash) { + await this.remove(hash); + hash = _hash; + entryData.hash = hash; + needMigrate = true; + } + } + + if (needMigrate) { + const _entry: {[hash: string]: OTPStorage} = {}; + _entry[hash] = entryData; + _entry[hash].encrypted = false; + this.import(encryption, _entry); + } + } + + data.sort((a, b) => { + return a.index - b.index; + }); + return resolve(data); + }); return; } catch (error) { return reject(error); @@ -344,6 +345,13 @@ class EntryStorage { }); } + static async remove(hash: string) { + return new Promise( + (resolve: () => void, reject: (reason: Error) => void) => { + return chrome.storage.sync.remove(hash, resolve); + }); + } + static async delete(entry: OTPEntry) { return new Promise( (resolve: () => void, reject: (reason: Error) => void) => { From 3b086a9545389d7ac26b12163befcc34179029d6 Mon Sep 17 00:00:00 2001 From: Li Zhe Date: Sun, 4 Mar 2018 12:23:39 +0800 Subject: [PATCH 2/2] 5.0.5 --- manifest-chrome.json | 2 +- manifest-firefox.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manifest-chrome.json b/manifest-chrome.json index 045afe68..4dd29510 100644 --- a/manifest-chrome.json +++ b/manifest-chrome.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "__MSG_extName__", "short_name": "__MSG_extShortName__", - "version": "5.0.4", + "version": "5.0.5", "default_locale": "en", "description": "__MSG_extDesc__", "icons": { diff --git a/manifest-firefox.json b/manifest-firefox.json index 0946636a..b5a2c50b 100644 --- a/manifest-firefox.json +++ b/manifest-firefox.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "__MSG_extName__", "short_name": "__MSG_extShortName__", - "version": "5.0.4", + "version": "5.0.5", "default_locale": "en", "description": "__MSG_extDesc__", "applications": {