From 77fc0ce55b1ede3071838ec645d2e626c225fe86 Mon Sep 17 00:00:00 2001 From: Hinton Date: Wed, 21 Oct 2020 16:48:40 +0200 Subject: [PATCH] Cleanup code --- proxy/app.ts | 4 +++ proxy/ipc.ts | 4 +++ src/main/nativeMessaging.main.ts | 33 ++++++++++--------------- src/services/nativeMessaging.service.ts | 7 +++--- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/proxy/app.ts b/proxy/app.ts index 5a88089d88..c67b464387 100644 --- a/proxy/app.ts +++ b/proxy/app.ts @@ -3,6 +3,10 @@ import IPC from './ipc'; const args = process.argv.slice(2); +// Proxy is a lightweight application which provides bi-directional communication +// between the browser extension and a running desktop application. +// +// Browser extension <-[native messaging]-> proxy <-[ipc]-> desktop class Proxy { private ipc: IPC; private nativeMessage: NativeMessage; diff --git a/proxy/ipc.ts b/proxy/ipc.ts index c0df5b15a3..3009f19846 100644 --- a/proxy/ipc.ts +++ b/proxy/ipc.ts @@ -17,12 +17,16 @@ export default class IPC { '## connected to bitwarden desktop ##', ipc.config.delay ); + + // Notify browser extension, connection is established to desktop application. this.onMessage({command: 'connected'}) }); ipc.of.bitwarden.on('disconnect', () => { this.connected = false; console.error('disconnected from world'); + + // Notify browser extension, no connection to desktop application. this.onMessage({command: 'disconnected'}) }); diff --git a/src/main/nativeMessaging.main.ts b/src/main/nativeMessaging.main.ts index 087e59e85b..938b132ca4 100644 --- a/src/main/nativeMessaging.main.ts +++ b/src/main/nativeMessaging.main.ts @@ -5,8 +5,7 @@ import * as util from 'util'; import { homedir } from 'os'; import { LogService } from 'jslib/abstractions/log.service'; -import { MessagingService } from 'jslib/abstractions/messaging.service'; -import { ipcMain, ipcRenderer } from 'electron'; +import { ipcMain } from 'electron'; import { WindowMain } from 'jslib/electron/window.main'; export class NativeMessagingMain { @@ -66,14 +65,9 @@ export class NativeMessagingMain { const firefoxJson = {...baseJson, ...{ 'allowed_extensions': ['{446900e4-71c2-419f-a6a7-df9c091e268b}']}} const chromeJson = {...baseJson, ...{ 'allowed_origins': ['chrome-extension://ijeheppnniijonkinoakkofcdhdfojda/']}} - if (!existsSync(path.join(this.userPath, 'browsers'))) { - fs.mkdir(path.join(this.userPath, 'browsers')) - .catch(this.logService.error) - } - switch (process.platform) { case 'win32': - const destination = path.join(this.userPath, 'browsers') + const destination = path.join(this.userPath, 'browsers'); this.writeManifest(path.join(destination, 'firefox.json'), firefoxJson); this.writeManifest(path.join(destination, 'chrome.json'), chromeJson); @@ -82,24 +76,20 @@ export class NativeMessagingMain { break; case 'darwin': if (existsSync(`${homedir()}/Library/Application\ Support/Mozilla/`)) { - fs.mkdir(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/`); - this.writeManifest(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json`, firefoxJson); + this.writeManifest(`${homedir()}/Library/Application\ Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json`, firefoxJson) } if (existsSync(`${homedir()}/Library/Application\ Support/Google/Chrome`)) { - fs.mkdir(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/`); - this.writeManifest(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson); + this.writeManifest(`${homedir()}/Library/Application\ Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson) } break; case 'linux': if (existsSync(`${homedir()}/.mozilla/`)) { - fs.mkdir(`${homedir()}/.mozilla/native-messaging-hosts`); - this.writeManifest(`${homedir()}/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json`, firefoxJson); + this.writeManifest(`${homedir()}/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json`, firefoxJson) } if (existsSync(`${homedir()}/.config/google-chrome/`)) { - fs.mkdir(`${homedir()}/.config/google-chrome/NativeMessagingHosts/`); - this.writeManifest(`${homedir()}/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson); + this.writeManifest(`${homedir()}/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json`, chromeJson) } break; default: @@ -110,25 +100,27 @@ export class NativeMessagingMain { removeManifests() { switch (process.platform) { case 'win32': + fs.unlink(path.join(this.userPath, 'browsers', 'firefox.json')); + fs.unlink(path.join(this.userPath, 'browsers', 'chrome.json')); this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Mozilla\\NativeMessagingHosts\\com.8bit.bitwarden'); this.deleteWindowsRegistry('HKCU\\SOFTWARE\\Google\\Chrome\\NativeMessagingHosts\\com.8bit.bitwarden'); break; case 'darwin': if (existsSync('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json')) { - fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json') + fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json'); } if (existsSync('~/Library/Application Support/Google/Chrome/NativeMessagingHosts/com.8bit.bitwarden.json')) { - fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json') + fs.unlink('~/Library/Application Support/Mozilla/NativeMessagingHosts/com.8bit.bitwarden.json'); } break; case 'linux': if (existsSync('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json')) { - fs.unlink('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json') + fs.unlink('~/.mozilla/native-messaging-hosts/com.8bit.bitwarden.json'); } if (existsSync('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json')) { - fs.unlink('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json') + fs.unlink('~/.config/google-chrome/NativeMessagingHosts/com.8bit.bitwarden.json'); } break; default: @@ -137,6 +129,7 @@ export class NativeMessagingMain { } private writeManifest(destination: string, manifest: object) { + fs.mkdir(path.dirname(destination)); fs.writeFile(destination, JSON.stringify(manifest, null, 2)).catch(this.logService.error); } diff --git a/src/services/nativeMessaging.service.ts b/src/services/nativeMessaging.service.ts index 2f9ba4155c..d5a1ffc23f 100644 --- a/src/services/nativeMessaging.service.ts +++ b/src/services/nativeMessaging.service.ts @@ -24,10 +24,13 @@ export class NativeMessagingService { } private async messageHandler(rawMessage: any) { + + // Request to setup secure encryption if (rawMessage.command == 'setupEncryption') { const remotePublicKey = Utils.fromB64ToArray(rawMessage.publicKey).buffer; const fingerprint = (await this.cryptoService.getFingerprint(await this.userService.getUserId(), remotePublicKey)).join(' '); + // Await confirmation that fingerprint is correct const submitted = await Swal.fire({ title: this.i18nService.t('verifyBrowserTitle'), html: `${this.i18nService.t('verifyBrowserDescription')}

${fingerprint}`, @@ -46,7 +49,6 @@ export class NativeMessagingService { return; } - // TODO: Add error handler, if it fails we should invalidate the key and send a re-authenticate message to browser const message = JSON.parse(await this.cryptoService.decryptToUtf8(rawMessage, this.sharedSecret)); if (Math.abs(message.timestamp - Date.now()) > MessageValidTimeout) { @@ -57,8 +59,7 @@ export class NativeMessagingService { switch (message.command) { case 'biometricUnlock': if (! this.platformUtilService.supportsBiometric()) { - ipcRenderer.send('nativeMessagingSync', ) - return this.send({command: 'biometricUnlock', response: 'not supported'}) + return this.send({command: 'biometricUnlock', response: 'not supported'}); } const response = await this.platformUtilService.authenticateBiometric();