-
Notifications
You must be signed in to change notification settings - Fork 6
/
preload.js
executable file
·64 lines (57 loc) · 2.1 KB
/
preload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// This file is loaded whenever a javascript context is created. It runs in a
// private scope that can access a subset of electron renderer APIs. We must be
// careful to not leak any objects into the global scope!
const { ipcRenderer } = require('electron');
const flatten = (obj) => Object.keys(obj)
.reduce((acc, key) => {
const val = obj[key];
return acc.concat(typeof val === 'object' ? flatten(val) : val);
}, []);
/**
* SafeIpcRenderer
*
* This class wraps electron's ipcRenderer an prevents
* invocations to channels passed to the constructor. The instance methods
* are all created in the constructor to ensure that the protect method
* and validEvents array cannot be overridden.
*
*/
class SafeIpcRenderer {
constructor (events) {
const validEvents = flatten(events);
const protect = (fn) => {
return (channel, ...args) => {
let validChannel = channel;
if (channel.indexOf(':') !== -1) {
if (Number.isInteger(+channel.substr(channel.indexOf(':') + 1))) {
validChannel = channel.substr(0, channel.indexOf(':'));
}
}
if (!validEvents.includes(validChannel)) {
throw new Error(`Blocked access to unknown channel
${channel} ${validChannel} from the renderer`);
}
return fn.apply(ipcRenderer, [channel].concat(args));
};
};
this.on = protect(ipcRenderer.on);
this.once = protect(ipcRenderer.once);
this.send = protect(ipcRenderer.send);
this.sendSync = protect(ipcRenderer.sendSync);
this.sendToHost = protect(ipcRenderer.sendToHost);
this.removeListener = protect(ipcRenderer.removeListener);
this.removeAllListeners = protect(ipcRenderer.removeAllListeners);
this.listenerCount = protect(ipcRenderer.listenerCount);
}
}
window.ipc = new SafeIpcRenderer([
'front-choosewallet',
'zmq',
'front-walletready',
'rpc-channel',
'rx-ipc-check-reply:rpc-channel',
'rx-ipc-check-listener',
'notification',
'rx-ipc-check-reply:front-choosewallet'
]);
window.electron = true;