From 86e3b9e42312df8160fbe5180e4547143f054e81 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 14:15:15 +0200 Subject: [PATCH 01/12] Add missing types --- src/background/commands.background.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 32d3243f098..31663e1e82a 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -18,19 +18,19 @@ export default class CommandsBackground { async init() { if (this.isVivaldi) { - BrowserApi.messageListener('commands.background', async (msg: any, sender: any, sendResponse: any) => { + BrowserApi.messageListener('commands.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { await this.processCommand(msg.shortcut, sender); } }); } else if (chrome && chrome.commands) { - chrome.commands.onCommand.addListener(async (command: any) => { + chrome.commands.onCommand.addListener(async (command: string) => { await this.processCommand(command); }); } } - private async processCommand(command: string, sender?: any) { + private async processCommand(command: string, sender?: chrome.runtime.MessageSender) { switch (command) { case 'generate_password': await this.generatePasswordToClipboard(); @@ -56,7 +56,7 @@ export default class CommandsBackground { this.passwordGenerationService.addHistory(password); } - private async autoFillLogin(tab?: any) { + private async autoFillLogin(tab?: chrome.tabs.Tab) { if (await this.vaultTimeoutService.isLocked()) { return; } From c793552dfd465722eb50689728ba6d8b4922889e Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 15:55:36 +0200 Subject: [PATCH 02/12] Enable unlock on autofill via keyboard shortcut --- src/background/commands.background.ts | 37 +++++++++++++++++++-------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 31663e1e82a..2e78cdd3308 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -5,6 +5,7 @@ import MainBackground from './main.background'; import { PasswordGenerationService } from 'jslib-common/abstractions/passwordGeneration.service'; import { PlatformUtilsService } from 'jslib-common/abstractions/platformUtils.service'; import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.service'; +import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; export default class CommandsBackground { private isSafari: boolean; @@ -17,13 +18,17 @@ export default class CommandsBackground { } async init() { - if (this.isVivaldi) { - BrowserApi.messageListener('commands.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { - if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { - await this.processCommand(msg.shortcut, sender); - } - }); - } else if (chrome && chrome.commands) { + BrowserApi.messageListener('commands.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { + if (msg.command === 'unlockCompleted' && msg.data.target === 'commands.background') { + await this.processCommand(msg.data.commandToRetry.msg.command, msg.data.commandToRetry.sender); + } + + if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { + await this.processCommand(msg.shortcut, sender); + } + }); + + if (!this.isVivaldi && chrome && chrome.commands) { chrome.commands.onCommand.addListener(async (command: string) => { await this.processCommand(command); }); @@ -57,10 +62,6 @@ export default class CommandsBackground { } private async autoFillLogin(tab?: chrome.tabs.Tab) { - if (await this.vaultTimeoutService.isLocked()) { - return; - } - if (!tab) { tab = await BrowserApi.getTabFromCurrentWindowId(); } @@ -69,6 +70,20 @@ export default class CommandsBackground { return; } + if (await this.vaultTimeoutService.isLocked()) { + const retryMessage: LockedVaultPendingNotificationsItem = { + commandToRetry: { + msg: { command: 'autofill_login' }, + sender: { tab: tab }, + }, + target: 'commands.background', + }; + await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage); + + BrowserApi.tabSendMessageData(tab, 'promptForLogin'); + return; + } + await this.main.collectPageDetailsForContentScript(tab, 'autofill_cmd'); } From fc0c7a04b7b5091605fa507641ce6e8ad01fd4de Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:00:38 +0200 Subject: [PATCH 03/12] Add types for contextMenus.background --- src/background/contextMenus.background.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index e46fe8e3694..c48bc26c1fb 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -27,7 +27,7 @@ export default class ContextMenusBackground { return; } - this.contextMenus.onClicked.addListener(async (info: any, tab: any) => { + this.contextMenus.onClicked.addListener(async (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) => { if (info.menuItemId === 'generate-password') { await this.generatePasswordToClipboard(); } else if (info.menuItemId === 'copy-identifier') { @@ -57,7 +57,7 @@ export default class ContextMenusBackground { BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId }); } - private async cipherAction(info: any) { + private async cipherAction(info: chrome.contextMenus.OnClickData) { const id = info.menuItemId.split('_')[1]; if (id === 'noop') { if (chrome.browserAction && (chrome.browserAction as any).openPopup) { From 421ab36215d49b148fd319a2bb8ab0bbcc8099c4 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:03:39 +0200 Subject: [PATCH 04/12] Pass tab onto cipherAction, no need to look up the current tab --- src/background/contextMenus.background.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index c48bc26c1fb..ddfaf464008 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -36,7 +36,7 @@ export default class ContextMenusBackground { info.parentMenuItemId === 'copy-username' || info.parentMenuItemId === 'copy-password' || info.parentMenuItemId === 'copy-totp') { - await this.cipherAction(info); + await this.cipherAction(tab, info); } }); } @@ -57,7 +57,7 @@ export default class ContextMenusBackground { BrowserApi.tabSendMessage(tab, { command: 'getClickedElement' }, { frameId: frameId }); } - private async cipherAction(info: chrome.contextMenus.OnClickData) { + private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) { const id = info.menuItemId.split('_')[1]; if (id === 'noop') { if (chrome.browserAction && (chrome.browserAction as any).openPopup) { @@ -77,7 +77,7 @@ export default class ContextMenusBackground { } if (info.parentMenuItemId === 'autofill') { - await this.startAutofillPage(cipher); + await this.startAutofillPage(tab, cipher); } else if (info.parentMenuItemId === 'copy-username') { this.platformUtilsService.copyToClipboard(cipher.login.username, { window: window }); } else if (info.parentMenuItemId === 'copy-password') { @@ -89,9 +89,8 @@ export default class ContextMenusBackground { } } - private async startAutofillPage(cipher: CipherView) { + private async startAutofillPage(tab: chrome.tabs.Tab, cipher: CipherView) { this.main.loginToAutoFill = cipher; - const tab = await BrowserApi.getTabFromCurrentWindow(); if (tab == null) { return; } From 0f000731b973d50c1796615de0bd3189217fc72a Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:04:32 +0200 Subject: [PATCH 05/12] Pass tab onto getClickedElement, no need to look up the current tab --- src/background/contextMenus.background.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index ddfaf464008..146601b58ad 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -31,7 +31,7 @@ export default class ContextMenusBackground { if (info.menuItemId === 'generate-password') { await this.generatePasswordToClipboard(); } else if (info.menuItemId === 'copy-identifier') { - await this.getClickedElement(info.frameId); + await this.getClickedElement(tab, info.frameId); } else if (info.parentMenuItemId === 'autofill' || info.parentMenuItemId === 'copy-username' || info.parentMenuItemId === 'copy-password' || @@ -48,8 +48,7 @@ export default class ContextMenusBackground { this.passwordGenerationService.addHistory(password); } - private async getClickedElement(frameId: number) { - const tab = await BrowserApi.getTabFromCurrentWindow(); + private async getClickedElement(tab: chrome.tabs.Tab, frameId: number) { if (tab == null) { return; } From 0e832ee4351312f6cc26bbcc07aa0ca3ca1492dd Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:08:40 +0200 Subject: [PATCH 06/12] Remove noop - openPopup and use promptForLogin instead when vault is locked --- src/background/contextMenus.background.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 146601b58ad..5ec1b46fbe4 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -11,6 +11,7 @@ import { VaultTimeoutService } from 'jslib-common/abstractions/vaultTimeout.serv import { EventType } from 'jslib-common/enums/eventType'; import { CipherView } from 'jslib-common/models/view/cipherView'; +import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; export default class ContextMenusBackground { private contextMenus: any; @@ -58,14 +59,18 @@ export default class ContextMenusBackground { private async cipherAction(tab: chrome.tabs.Tab, info: chrome.contextMenus.OnClickData) { const id = info.menuItemId.split('_')[1]; - if (id === 'noop') { - if (chrome.browserAction && (chrome.browserAction as any).openPopup) { - (chrome.browserAction as any).openPopup(); - } - return; - } if (await this.vaultTimeoutService.isLocked()) { + const retryMessage: LockedVaultPendingNotificationsItem = { + commandToRetry: { + msg: { command: 'noop', data: info }, + sender: { tab: tab }, + }, + target: 'contextmenus.background', + }; + await BrowserApi.tabSendMessageData(tab, 'addToLockedVaultPendingNotifications', retryMessage); + + BrowserApi.tabSendMessageData(tab, 'promptForLogin'); return; } From de1d26bd8d2906b64e5f75ae68ee0f0d0ff0db4e Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:10:43 +0200 Subject: [PATCH 07/12] Add messageListener for unlockCompleted --- src/background/contextMenus.background.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 5ec1b46fbe4..a9173f2dfed 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -40,6 +40,12 @@ export default class ContextMenusBackground { await this.cipherAction(tab, info); } }); + + BrowserApi.messageListener('contextmenus.background', async (msg: any, sender: chrome.runtime.MessageSender, sendResponse: any) => { + if (msg.command === 'unlockCompleted' && msg.data.target === 'contextmenus.background') { + await this.cipherAction(msg.data.commandToRetry.sender.tab, msg.data.commandToRetry.msg.data); + } + }); } private async generatePasswordToClipboard() { From ec13cfd70c447d7e082023a04d5f2ec9a34cd1b5 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Tue, 19 Oct 2021 16:11:48 +0200 Subject: [PATCH 08/12] Pick first cipher matching url as no specific cipher was selected by the user when vault is locked --- src/background/contextMenus.background.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index a9173f2dfed..609081b2b72 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -80,8 +80,16 @@ export default class ContextMenusBackground { return; } - const ciphers = await this.cipherService.getAllDecrypted(); - const cipher = ciphers.find(c => c.id === id); + let cipher: CipherView; + if (id === 'noop') { + const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url); + cipher = ciphers.length > 0 ? ciphers[0] : null; + } + else { + const ciphers = await this.cipherService.getAllDecrypted(); + cipher = ciphers.find(c => c.id === id); + } + if (cipher == null) { return; } From fd9455873d161c2ddca9637d8302124b318a4c12 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Wed, 20 Oct 2021 17:45:19 +0200 Subject: [PATCH 09/12] Add literal for noopCommand suffix --- src/background/contextMenus.background.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index 609081b2b72..f4b310e809e 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -14,6 +14,7 @@ import { CipherView } from 'jslib-common/models/view/cipherView'; import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNotificationsItem'; export default class ContextMenusBackground { + private readonly noopCommandSuffix = 'noop'; private contextMenus: any; constructor(private main: MainBackground, private cipherService: CipherService, @@ -69,7 +70,7 @@ export default class ContextMenusBackground { if (await this.vaultTimeoutService.isLocked()) { const retryMessage: LockedVaultPendingNotificationsItem = { commandToRetry: { - msg: { command: 'noop', data: info }, + msg: { command: this.noopCommandSuffix, data: info }, sender: { tab: tab }, }, target: 'contextmenus.background', @@ -81,7 +82,7 @@ export default class ContextMenusBackground { } let cipher: CipherView; - if (id === 'noop') { + if (id === this.noopCommandSuffix) { const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url); cipher = ciphers.length > 0 ? ciphers[0] : null; } From 39a189e7b3dfb24b049a684dd62346d6609c22d9 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Wed, 20 Oct 2021 17:49:58 +0200 Subject: [PATCH 10/12] Fixed formatting --- src/background/contextMenus.background.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/background/contextMenus.background.ts b/src/background/contextMenus.background.ts index f4b310e809e..9a29294d4f1 100644 --- a/src/background/contextMenus.background.ts +++ b/src/background/contextMenus.background.ts @@ -85,8 +85,7 @@ export default class ContextMenusBackground { if (id === this.noopCommandSuffix) { const ciphers = await this.cipherService.getAllDecryptedForUrl(tab.url); cipher = ciphers.length > 0 ? ciphers[0] : null; - } - else { + } else { const ciphers = await this.cipherService.getAllDecrypted(); cipher = ciphers.find(c => c.id === id); } From c318b58185e97e613d921fe15fdf5cf9c5e6e430 Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Wed, 20 Oct 2021 17:52:10 +0200 Subject: [PATCH 11/12] Remove check for isVivaldi --- src/background/commands.background.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 2e78cdd3308..51d3f47aace 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -9,12 +9,10 @@ import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNoti export default class CommandsBackground { private isSafari: boolean; - private isVivaldi: boolean; constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService, private platformUtilsService: PlatformUtilsService, private vaultTimeoutService: VaultTimeoutService) { this.isSafari = this.platformUtilsService.isSafari(); - this.isVivaldi = this.platformUtilsService.isVivaldi(); } async init() { @@ -23,12 +21,12 @@ export default class CommandsBackground { await this.processCommand(msg.data.commandToRetry.msg.command, msg.data.commandToRetry.sender); } - if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { + if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { await this.processCommand(msg.shortcut, sender); } }); - if (!this.isVivaldi && chrome && chrome.commands) { + if (chrome && chrome.commands) { chrome.commands.onCommand.addListener(async (command: string) => { await this.processCommand(command); }); From f394cbf57f7c8216b18a41da97a1742340cec95f Mon Sep 17 00:00:00 2001 From: Daniel James Smith Date: Fri, 22 Oct 2021 10:01:18 +0200 Subject: [PATCH 12/12] Revert "Remove check for isVivaldi" This reverts commit c318b58185e97e613d921fe15fdf5cf9c5e6e430. --- src/background/commands.background.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/background/commands.background.ts b/src/background/commands.background.ts index 51d3f47aace..2e78cdd3308 100644 --- a/src/background/commands.background.ts +++ b/src/background/commands.background.ts @@ -9,10 +9,12 @@ import LockedVaultPendingNotificationsItem from './models/lockedVaultPendingNoti export default class CommandsBackground { private isSafari: boolean; + private isVivaldi: boolean; constructor(private main: MainBackground, private passwordGenerationService: PasswordGenerationService, private platformUtilsService: PlatformUtilsService, private vaultTimeoutService: VaultTimeoutService) { this.isSafari = this.platformUtilsService.isSafari(); + this.isVivaldi = this.platformUtilsService.isVivaldi(); } async init() { @@ -21,12 +23,12 @@ export default class CommandsBackground { await this.processCommand(msg.data.commandToRetry.msg.command, msg.data.commandToRetry.sender); } - if (msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { + if (this.isVivaldi && msg.command === 'keyboardShortcutTriggered' && msg.shortcut) { await this.processCommand(msg.shortcut, sender); } }); - if (chrome && chrome.commands) { + if (!this.isVivaldi && chrome && chrome.commands) { chrome.commands.onCommand.addListener(async (command: string) => { await this.processCommand(command); });