From d3184cc42c4258d0a80a21ff5c2318476d9a3d2a Mon Sep 17 00:00:00 2001 From: varjolintu Date: Thu, 24 Aug 2023 16:42:00 +0300 Subject: [PATCH] Commit 2023-08-24 --- keepassxc-browser/background/protocol.js | 71 ++++++++----------- .../background/protocolClient.js | 2 +- keepassxc-browser/content/fill.js | 22 +++--- 3 files changed, 40 insertions(+), 55 deletions(-) diff --git a/keepassxc-browser/background/protocol.js b/keepassxc-browser/background/protocol.js index 5f1f953..458dd0b 100644 --- a/keepassxc-browser/background/protocol.js +++ b/keepassxc-browser/background/protocol.js @@ -19,8 +19,8 @@ protocol.associate = async function(tab, args = []) { const messageData = { action: kpActions.ASSOCIATE, - publicKey: publicKey, - idKey: idKey + idKey: idKey, + publicKey: publicKey }; const response = await protocolClient.sendMessage(tab, messageData, false, true); @@ -54,9 +54,9 @@ protocol.changePublicKeys = async function(tab, enableTimeout = false, connectio const request = { action: kpAction, - publicKey: key, - nonce: nonce, clientID: keepass.clientID, + nonce: nonce, + publicKey: key, requestID: protocolClient.getRequestId() }; @@ -105,21 +105,18 @@ protocol.createNewGroup = async function(tab, args = []) { keepass.clearErrorMessage(tab); const [ groupName ] = args; - //const [ dbid ] = keepass.getCryptoKey(); const messageData = { action: kpActions.CREATE_NEW_GROUP, - //id: dbid, groupName: groupName, - keys: protocol.getKeys(), // Added - hash: keepass.databaseHash // Added + keys: protocol.getCurrentKey() }; try { // TODO: Handle errors const response = await protocolClient.sendMessage(tab, messageData); if (response) { - keepass.updateLastUsed(keepass.databaseHash); // ? + keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? return response; } else { logError('getDatabaseGroups rejected'); @@ -142,8 +139,6 @@ protocol.generatePassword = async function(tab, args = []) { return undefined; } - let password; - const messageData = { action: kpActions.GENERATE_PASSWORD, }; @@ -156,13 +151,14 @@ protocol.generatePassword = async function(tab, args = []) { return undefined; } - password = response.entries ?? response.password; - keepass.updateLastUsed(keepass.databaseHash); // ? + const password = response.entries ?? response.password; + keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? + return password; } else { logError('generatePassword rejected'); } - return password; + return undefined; } catch (err) { logError(`generatePassword failed: ${err}`); return undefined; @@ -181,8 +177,8 @@ protocol.getCredentials = async function(tab, args = []) { const messageData = { action: kpActions.GET_CREDENTIALS, - url: url, - keys: protocol.getKeys() + keys: protocol.getKeys(), + url: url }; if (submiturl) { @@ -202,7 +198,7 @@ protocol.getCredentials = async function(tab, args = []) { } entries = keepass.removeDuplicateEntries(response.entries); - keepass.updateLastUsed(keepass.databaseHash); // What about this? + keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? if (entries.length === 0) { // Questionmark-icon is not triggered, so we have to trigger for the normal symbol @@ -228,14 +224,11 @@ protocol.getDatabaseGroups = async function(tab, args = []) { keepass.clearErrorMessage(tab); - //const [ dbid ] = keepass.getCryptoKey(); let groups = []; const messageData = { action: kpActions.GET_DATABASE_GROUPS, - //id: dbid, - keys: protocol.getKeys(), // Added - hash: keepass.databaseHash // Added + keys: protocol.getCurrentKey() }; try { @@ -249,7 +242,7 @@ protocol.getDatabaseGroups = async function(tab, args = []) { groups = response.groups; groups.defaultGroup = page.settings.defaultGroup; groups.defaultGroupAlwaysAsk = page.settings.defaultGroupAlwaysAsk; - keepass.updateLastUsed(keepass.databaseHash); // ? + keepass.updateLastUsed(keepass.databaseHash); // TODO: Remove? return groups; } @@ -293,32 +286,21 @@ protocol.getDatabaseStatuses = async function(tab, args = []) { return response; } - //keepass.isDatabaseClosed = true; - //keepass.isKeePassXCAvailable = false; - //keepass.databaseHash = ''; keepass.handleError(tab, kpErrors.ACTION_TIMEOUT); } catch (err) { logError(`getDatabaseStatuses failed: ${err}`); } }; -// TODO: Finish this protocol.getTotp = async function(tab, args = []) { if (!keepass.isConnected) { return []; } - /*const [ uuids, oldTotp ] = args; - // KeePassXC 2.6.1 and older does not support retrieving - if (!keepass.compareVersion('2.6.1', keepass.currentKeePassXC, true)) { - return oldTotp; - }*/ - const messageData = { action: kpActions.GET_TOTP, - uuids: args, - keys: protocol.getKeys(), // Added - hash: keepass.databaseHash // Added + keys: protocol.getKeys(), + uuids: args }; try { @@ -398,7 +380,6 @@ protocol.testAssociationFromDatabaseStatuses = async function(tab, args = []) { isAnyAssociated: false }; - // TODO: Handle this already in getDatabaseStatuses? if (!databaseStatuses || databaseStatuses.statuses.length === 0) { keepass.handleError(tab, kpErrors.DATABASE_NOT_OPENED); return result; @@ -446,17 +427,14 @@ protocol.updateCredentials = async function(tab, args = []) { } const [ entryId, username, password, url, group, groupUuid ] = args; - //const [ dbid ] = keepass.getCryptoKey(); const messageData = { action: kpActions.CREATE_CREDENTIALS, - hash: keepass.databaseHash, // Added - keys: protocol.getKeys(), // Added - //id: dbid, // Needed? + keys: protocol.getCurrentKey(), login: username, password: password, - url: url, submitUrl: url, + url: url, }; if (entryId) { @@ -505,3 +483,14 @@ protocol.getKeys = function() { return keys; }; + +// Gets the key only from the current active database +protocol.getCurrentKey = function() { + const [ id, key ] = keepass.getCryptoKey(); + return [ + { + id: id, + key: key + } + ]; +}; diff --git a/keepassxc-browser/background/protocolClient.js b/keepassxc-browser/background/protocolClient.js index 89e9bcc..13134ea 100644 --- a/keepassxc-browser/background/protocolClient.js +++ b/keepassxc-browser/background/protocolClient.js @@ -36,6 +36,7 @@ protocolClient.messageTimeout = 500; // Milliseconds protocolClient.nativeHostName = 'org.keepassxc.keepassxc_browser'; protocolClient.nativePort = null; +// TODO: Merge with fix/refactor_messaging protocolClient.sendNativeMessage = function(requestAction, request, enableTimeout = false, timeoutValue = protocolClient.messageTimeout) { return new Promise((resolve, reject) => { let timeout; @@ -43,7 +44,6 @@ protocolClient.sendNativeMessage = function(requestAction, request, enableTimeou const listener = ((port) => { const handler = (msg) => { - console.log('msg recevied: ', msg); if (msg && (msg?.requestID === request.requestID || msg?.action === kpActions.CHANGE_PUBLIC_KEYS)) { // Only resolve a matching response if (protocolBuffer.matchAndRemove(msg)) { diff --git a/keepassxc-browser/content/fill.js b/keepassxc-browser/content/fill.js index 62b84b5..fd489a1 100644 --- a/keepassxc-browser/content/fill.js +++ b/keepassxc-browser/content/fill.js @@ -143,24 +143,20 @@ kpxcFill.fillTOTPFromUuid = async function(el, uuid) { let totpFound = false; if (user.totp?.length > 0) { const protocolV2 = await sendMessage('is_protocol_v2'); - if (protocolV2) { - const totpList = await sendMessage('get_totp', [ user.uuid ]); - if (!totpList) { - kpxcUI.createNotification('warning', tr('credentialsNoTOTPFound')); - return; - } - const result = totpList.find(t => t.uuid === uuid); + const totp = await sendMessage('get_totp', (protocolV2 ? [ user.uuid ] : [ user.uuid, user.totp ])); + if (!totp) { + kpxcUI.createNotification('warning', tr('credentialsNoTOTPFound')); + return; + } + + if (protocolV2) { + const result = totp.find(t => t.uuid === uuid); kpxcFill.setTOTPValue(el, result?.totp); } else { - const totp = await sendMessage('get_totp', [ user.uuid, user.totp ]); - if (!totp) { - kpxcUI.createNotification('warning', tr('credentialsNoTOTPFound')); - return; - } - kpxcFill.setTOTPValue(el, totp); } + return; } else if (user.stringFields?.length > 0) { const stringFields = user.stringFields;