From e9412df60eb0cfff812f9e19132613bd460e4146 Mon Sep 17 00:00:00 2001 From: audric Date: Fri, 6 Aug 2021 14:23:08 +1000 Subject: [PATCH] fix lint and steal autofocus on registration to seed --- .eslintignore | 3 + libloki/test/components.js | 3 +- libloki/test/test.js | 32 +-- .../registration/RegistrationUserDetails.tsx | 8 +- ts/opengroup/opengroupV2/ApiUtil.ts | 9 +- .../opengroupV2/OpenGroupMessageV2.ts | 2 +- ts/session/snode_api/SNodeAPI.ts | 265 +++++++++--------- 7 files changed, 170 insertions(+), 152 deletions(-) diff --git a/.eslintignore b/.eslintignore index f0d5a4a8c..2c86b4ce6 100644 --- a/.eslintignore +++ b/.eslintignore @@ -11,6 +11,9 @@ js/util_worker.js libtextsecure/components.js libtextsecure/test/test.js test/test.js +libloki/test/components.js +libloki/test/test.js + # Third-party files js/Mp3LameEncoder.min.js diff --git a/libloki/test/components.js b/libloki/test/components.js index f0ebb0385..da22796da 100644 --- a/libloki/test/components.js +++ b/libloki/test/components.js @@ -492,7 +492,8 @@ return ( e(t) || (t = o(t)), (this.unsigned === t.unsigned || this.high >>> 31 != 1 || t.high >>> 31 != 1) && - this.high === t.high && this.low === t.low + this.high === t.high && + this.low === t.low ); }), (B.eq = B.equals), diff --git a/libloki/test/test.js b/libloki/test/test.js index 19c18dfe2..8b240e3ed 100644 --- a/libloki/test/test.js +++ b/libloki/test/test.js @@ -8116,14 +8116,9 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /*istanbul ignore start*/ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; - } // Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode + } // Based on https://en.wikipedia.org/wiki/Latin_script_in_Unicode // // Ranges and exceptions: // Latin-1 Supplement, 0080–00FF // - U+00D7 × Multiplication sign // - U+00F7 ÷ Division sign - /*istanbul ignore end*/ // - // Ranges and exceptions: - // Latin-1 Supplement, 0080–00FF - // - U+00D7 × Multiplication sign - // - U+00F7 ÷ Division sign - // Latin Extended-A, 0100–017F + /*istanbul ignore end*/ // Latin Extended-A, 0100–017F // Latin Extended-B, 0180–024F // IPA Extensions, 0250–02AF // Spacing Modifier Letters, 02B0–02FF @@ -8176,10 +8171,12 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. }; function diffWords(oldStr, newStr, options) { - options = /*istanbul ignore start*/ (0, - _params.generateOptions)(/*istanbul ignore end*/ options, { - ignoreWhitespace: true, - }); + options = /*istanbul ignore start*/ (0, _params.generateOptions)( + /*istanbul ignore end*/ options, + { + ignoreWhitespace: true, + } + ); return wordDiff.diff(oldStr, newStr, options); } @@ -8267,10 +8264,12 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. return lineDiff.diff(oldStr, newStr, callback); } function diffTrimmedLines(oldStr, newStr, callback) { - var options = /*istanbul ignore start*/ (0, - _params.generateOptions)(/*istanbul ignore end*/ callback, { - ignoreWhitespace: true, - }); + var options = /*istanbul ignore start*/ (0, _params.generateOptions)( + /*istanbul ignore end*/ callback, + { + ignoreWhitespace: true, + } + ); return lineDiff.diff(oldStr, newStr, options); } @@ -22562,7 +22561,8 @@ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. if ( !doLength && - objType === 'date' && (startType !== 'date' || finishType !== 'date') + objType === 'date' && + (startType !== 'date' || finishType !== 'date') ) { errorMessage = msgPrefix + 'the arguments to within must be dates'; } else if ( diff --git a/ts/components/session/registration/RegistrationUserDetails.tsx b/ts/components/session/registration/RegistrationUserDetails.tsx index 4a7935e14..73baa4c3d 100644 --- a/ts/components/session/registration/RegistrationUserDetails.tsx +++ b/ts/components/session/registration/RegistrationUserDetails.tsx @@ -30,13 +30,15 @@ const RecoveryPhraseInput = (props: { recoveryPhrase: string; onSeedChanged: (val: string) => any; handlePressEnter: () => any; + stealAutoFocus?: boolean; }) => { return ( + // tslint:disable-next-line: use-simple-attributes { recoveryPhrase={props.recoveryPhrase as string} handlePressEnter={props.handlePressEnter} onSeedChanged={props.onSeedChanged as any} + stealAutoFocus={props.stealAutoFocus} /> )}
{props.showDisplayNameField && ( + // tslint:disable-next-line: use-simple-attributes { }; export const textToArrayBuffer = async (text: string) => { - return await window.callWorker('bytesFromString', text); + return window.callWorker('bytesFromString', text); }; export const verifyED25519Signature = async ( @@ -58,12 +58,7 @@ export const verifyED25519Signature = async ( base64EncodedData: string, base64EncondedSignature: string ): Promise => { - return await window.callWorker( - 'verifySignature', - pubkey, - base64EncodedData, - base64EncondedSignature - ); + return window.callWorker('verifySignature', pubkey, base64EncodedData, base64EncondedSignature); }; export const parseMessages = async ( diff --git a/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts b/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts index dfa77d44d..22c2ec241 100644 --- a/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts +++ b/ts/opengroup/opengroupV2/OpenGroupMessageV2.ts @@ -67,7 +67,7 @@ export class OpenGroupMessageV2 { if (!signature || signature.length === 0) { throw new Error("Couldn't sign message"); } - let base64Sig = await window.callWorker('arrayBufferToStringBase64', signature); + const base64Sig = await window.callWorker('arrayBufferToStringBase64', signature); return new OpenGroupMessageV2({ base64EncodedData: this.base64EncodedData, sentTimestamp: this.sentTimestamp, diff --git a/ts/session/snode_api/SNodeAPI.ts b/ts/session/snode_api/SNodeAPI.ts index e6f538fb6..7934f9652 100644 --- a/ts/session/snode_api/SNodeAPI.ts +++ b/ts/session/snode_api/SNodeAPI.ts @@ -32,7 +32,7 @@ import { import { Snode } from '../../data/data'; import { updateIsOnline } from '../../state/ducks/onion'; import { ed25519Str } from '../onions/onionPath'; -import { UserUtils, StringUtils } from '../utils'; +import { StringUtils, UserUtils } from '../utils'; // ONS name can have [a-zA-Z0-9_-] except that - is not allowed as start or end // do not define a regex but rather create it on the fly to avoid https://stackoverflow.com/questions/3891641/regex-test-only-works-every-other-time @@ -607,7 +607,8 @@ export async function retrieveNextMessages( * @param snode Snode to send request to * @returns timestamp of the response from snode */ -const getNetworkTime = async (snode: Snode): Promise => { +// tslint:disable-next-line: variable-name +export const TEST_getNetworkTime = async (snode: Snode): Promise => { const response: any = await snodeRpc('info', {}, snode); const body = JSON.parse(response.body); const timestamp = body?.timestamp; @@ -625,139 +626,153 @@ export const forceNetworkDeletion = async (): Promise | null> => { const userED25519KeyPair = await UserUtils.getUserED25519KeyPair(); if (!userED25519KeyPair) { - window.log.warn('Cannot forceNetworkDeletion, did not find user ed25519 key.'); + window?.log?.warn('Cannot forceNetworkDeletion, did not find user ed25519 key.'); return null; } const edKeyPriv = userED25519KeyPair.privKey; try { - const maliciousSnodes = await pRetry(async () => { - const userSwarm = await getSwarmFor(userX25519PublicKey); - const snodeToMakeRequestTo: Snode | undefined = _.sample(userSwarm); - const edKeyPrivBytes = fromHexToArray(edKeyPriv); + const maliciousSnodes = await pRetry( + async () => { + const userSwarm = await getSwarmFor(userX25519PublicKey); + const snodeToMakeRequestTo: Snode | undefined = _.sample(userSwarm); + const edKeyPrivBytes = fromHexToArray(edKeyPriv); - if (!snodeToMakeRequestTo) { - window.log.warn('Cannot forceNetworkDeletion, without a valid swarm node.'); - return null; - } - - return pRetry( - async () => { - const timestamp = await getNetworkTime(snodeToMakeRequestTo); - - const verificationData = StringUtils.encode(`delete_all${timestamp}`, 'utf8'); - const message = new Uint8Array(verificationData); - const signature = sodium.crypto_sign_detached(message, edKeyPrivBytes); - const signatureBase64 = fromUInt8ArrayToBase64(signature); - - const deleteMessageParams = { - pubkey: userX25519PublicKey, - pubkey_ed25519: userED25519KeyPair.pubKey.toUpperCase(), - timestamp, - signature: signatureBase64, - }; - const ret = await snodeRpc( - 'delete_all', - deleteMessageParams, - snodeToMakeRequestTo, - userX25519PublicKey - ); - - if (!ret) { - throw new Error( - `Empty response got for delete_all on snode ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )}` - ); - } - - try { - const parsedResponse = JSON.parse(ret.body); - const { swarm } = parsedResponse; - - if (!swarm) { - throw new Error( - `Invalid JSON swarm response got for delete_all on snode ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )}, ${ret?.body}` - ); - } - const swarmAsArray = Object.entries(swarm) as Array>; - if (!swarmAsArray.length) { - throw new Error( - `Invalid JSON swarmAsArray response got for delete_all on snode ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )}, ${ret?.body}` - ); - } - // results will only contains the snode pubkeys which returned invalid/empty results - const results: Array = _.compact( - swarmAsArray.map(snode => { - const snodePubkey = snode[0]; - const snodeJson = snode[1]; - - const isFailed = snodeJson.failed || false; - - if (isFailed) { - const reason = snodeJson.reason; - const statusCode = snodeJson.code; - if (reason && statusCode) { - window.log.warn( - `Could not delete data from ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )} due to error: ${reason}: ${statusCode}` - ); - } else { - window.log.warn( - `Could not delete data from ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )}` - ); - } - return snodePubkey; - } - - const hashes = snodeJson.deleted as Array; - const signatureSnode = snodeJson.signature as string; - // The signature format is ( PUBKEY_HEX || TIMESTAMP || DELETEDHASH[0] || ... || DELETEDHASH[N] ) - const dataToVerify = `${userX25519PublicKey}${timestamp}${hashes.join('')}`; - const dataToVerifyUtf8 = StringUtils.encode(dataToVerify, 'utf8'); - const isValid = sodium.crypto_sign_verify_detached( - fromBase64ToArray(signatureSnode), - new Uint8Array(dataToVerifyUtf8), - fromHexToArray(snodePubkey) - ); - if (!isValid) { - return snodePubkey; - } - return null; - }) - ); - - return results; - } catch (e) { - throw new Error( - `Invalid JSON response got for delete_all on snode ${ed25519Str( - snodeToMakeRequestTo.pubkey_ed25519 - )}, ${ret?.body}` - ); - } - }, - { - retries: 3, - minTimeout: 500, - onFailedAttempt: e => { - window?.log?.warn( - `delete_all request attempt #${e.attemptNumber} failed. ${e.retriesLeft} retries left...` - ); - }, + if (!snodeToMakeRequestTo) { + window?.log?.warn('Cannot forceNetworkDeletion, without a valid swarm node.'); + return null; } - ); - }, {}); + + return pRetry( + async () => { + const timestamp = await exports.TEST_getNetworkTime(snodeToMakeRequestTo); + + const verificationData = StringUtils.encode(`delete_all${timestamp}`, 'utf8'); + const message = new Uint8Array(verificationData); + const signature = sodium.crypto_sign_detached(message, edKeyPrivBytes); + const signatureBase64 = fromUInt8ArrayToBase64(signature); + + const deleteMessageParams = { + pubkey: userX25519PublicKey, + pubkey_ed25519: userED25519KeyPair.pubKey.toUpperCase(), + timestamp, + signature: signatureBase64, + }; + const ret = await snodeRpc( + 'delete_all', + deleteMessageParams, + snodeToMakeRequestTo, + userX25519PublicKey + ); + + if (!ret) { + throw new Error( + `Empty response got for delete_all on snode ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )}` + ); + } + + try { + const parsedResponse = JSON.parse(ret.body); + const { swarm } = parsedResponse; + + if (!swarm) { + throw new Error( + `Invalid JSON swarm response got for delete_all on snode ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )}, ${ret?.body}` + ); + } + const swarmAsArray = Object.entries(swarm) as Array>; + if (!swarmAsArray.length) { + throw new Error( + `Invalid JSON swarmAsArray response got for delete_all on snode ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )}, ${ret?.body}` + ); + } + // results will only contains the snode pubkeys which returned invalid/empty results + const results: Array = _.compact( + swarmAsArray.map(snode => { + const snodePubkey = snode[0]; + const snodeJson = snode[1]; + + const isFailed = snodeJson.failed || false; + + if (isFailed) { + const reason = snodeJson.reason; + const statusCode = snodeJson.code; + if (reason && statusCode) { + window?.log?.warn( + `Could not delete data from ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )} due to error: ${reason}: ${statusCode}` + ); + } else { + window?.log?.warn( + `Could not delete data from ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )}` + ); + } + return snodePubkey; + } + + const hashes = snodeJson.deleted as Array; + const signatureSnode = snodeJson.signature as string; + // The signature format is ( PUBKEY_HEX || TIMESTAMP || DELETEDHASH[0] || ... || DELETEDHASH[N] ) + const dataToVerify = `${userX25519PublicKey}${timestamp}${hashes.join('')}`; + const dataToVerifyUtf8 = StringUtils.encode(dataToVerify, 'utf8'); + const isValid = sodium.crypto_sign_verify_detached( + fromBase64ToArray(signatureSnode), + new Uint8Array(dataToVerifyUtf8), + fromHexToArray(snodePubkey) + ); + if (!isValid) { + return snodePubkey; + } + return null; + }) + ); + + return results; + } catch (e) { + throw new Error( + `Invalid JSON response got for delete_all on snode ${ed25519Str( + snodeToMakeRequestTo.pubkey_ed25519 + )}, ${ret?.body}` + ); + } + }, + { + retries: 3, + minTimeout: exports.TEST_getMinTimeout(), + onFailedAttempt: e => { + window?.log?.warn( + `delete_all INNER request attempt #${e.attemptNumber} failed. ${e.retriesLeft} retries left...` + ); + }, + } + ); + }, + { + retries: 3, + minTimeout: exports.TEST_getMinTimeout(), + onFailedAttempt: e => { + window?.log?.warn( + `delete_all OUTER request attempt #${e.attemptNumber} failed. ${e.retriesLeft} retries left...` + ); + }, + } + ); return maliciousSnodes; } catch (e) { - window.log.warn('failed to delete everything on network:', e); + window?.log?.warn('failed to delete everything on network:', e); return null; } }; + +// tslint:disable-next-line: variable-name +export const TEST_getMinTimeout = () => 500;