This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Electron App security (polkadot-js#2923)
* WIP on storing accounts without electron remote modules. * Improve security by isolating main and renderer. * Apply suggestions from code review Co-authored-by: Jaco Greeff <[email protected]> Co-authored-by: Jaco Greeff <[email protected]>
- Loading branch information
1 parent
8963c5c
commit f8f7eed
Showing
12 changed files
with
180 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { KeyringJson } from '@polkadot/ui-keyring/types'; | ||
|
||
export interface AccountStoreApi { | ||
all: () => Promise<{ key: string, value: KeyringJson }[]> | ||
get: (key: string) => Promise<KeyringJson> | ||
remove: (key: string) => Promise<void> | ||
set: (key: string, value: KeyringJson) => Promise<void> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { AccountStoreApi } from './account-store-api'; | ||
|
||
export interface ElectronMainApi { | ||
accountStore: AccountStoreApi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { ElectronMainApi } from './electron-main-api'; | ||
|
||
declare global { | ||
interface Window { | ||
ElectronMain: ElectronMainApi | ||
} | ||
} | ||
|
||
export const electronMainApi = window.ElectronMain; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { FileStore } from '@polkadot/ui-keyring/stores'; | ||
import { KeyringJson } from '@polkadot/ui-keyring/types'; | ||
import { app, ipcMain } from 'electron'; | ||
import path from 'path'; | ||
|
||
const ACCOUNTS_SUBFOLDER = 'polkadot-accounts'; | ||
|
||
export const registerAccountStoreHandlers = (): void => { | ||
const defaultStorePath = path.join(app.getPath('userData'), ACCOUNTS_SUBFOLDER); | ||
const fileStore = new FileStore(defaultStorePath); | ||
|
||
ipcMain.handle('account-store-set', async (_, key: string, value: KeyringJson) => new Promise((resolve) => | ||
fileStore.set(key, value, resolve) | ||
)); | ||
|
||
ipcMain.handle('account-store-get', async (_, key: string) => new Promise((resolve) => | ||
fileStore.get(key, resolve) | ||
)); | ||
|
||
ipcMain.handle('account-store-remove', async (_, key: string) => new Promise((resolve) => | ||
fileStore.remove(key, resolve) | ||
)); | ||
|
||
ipcMain.handle('account-store-all', () => { | ||
let result: { key: string, value: KeyringJson }[] = []; | ||
|
||
const collect = (key: string, value: KeyringJson) => { | ||
result = [...result, { key, value }]; | ||
}; | ||
|
||
fileStore.all(collect); | ||
|
||
return result; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { KeyringJson } from '@polkadot/ui-keyring/types'; | ||
import { contextBridge, ipcRenderer } from 'electron'; | ||
|
||
contextBridge.exposeInMainWorld('ElectronMain', { | ||
accountStore: { | ||
all: () => ipcRenderer.invoke('account-store-all'), | ||
get: (key: string) => ipcRenderer.invoke('account-store-get', key), | ||
remove: (key: string) => ipcRenderer.invoke('account-store-remove', key), | ||
set: (key: string, value: KeyringJson) => ipcRenderer.invoke('account-store-set', key, value) | ||
} | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/apps-electron/src/renderer/remote-electron-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2017-2020 @polkadot/apps authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the Apache-2.0 license. See the LICENSE file for details. | ||
|
||
import { KeyringJson, KeyringStore } from '@polkadot/ui-keyring/types'; | ||
import { electronMainApi } from '../api/global-exported-api'; | ||
|
||
export class RemoteElectronStore implements KeyringStore { | ||
all (cb: (key: string, value: KeyringJson) => void): void { | ||
electronMainApi.accountStore.all() | ||
.then((result: { key: string, value: KeyringJson }[]) => result.forEach(({ key, value }) => cb(key, value))) | ||
.catch(() => { | ||
throw new Error('error getting all accounts'); | ||
}); | ||
} | ||
|
||
get (key: string, cb: (value: KeyringJson) => void): void { | ||
electronMainApi.accountStore.get(key) | ||
.then(cb).catch(() => { | ||
throw new Error('error storing account'); | ||
}); | ||
} | ||
|
||
remove (key: string, cb: (() => void) | undefined): void { | ||
electronMainApi.accountStore.remove(key).then(cb).catch(() => { | ||
throw new Error('error removing account'); | ||
}); | ||
} | ||
|
||
set (key: string, value: KeyringJson, cb: (() => void) | undefined): void { | ||
electronMainApi.accountStore.set(key, value).then(cb).catch(() => { | ||
throw new Error('error saving account'); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters