Skip to content

Commit d2b21cb

Browse files
committed
[Desktop] Disable nodeIntegration as a security precaution
electron/electron#5113 electron/electron#4362 PaulLeCam/react-native-electron#6
1 parent 856c101 commit d2b21cb

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

@types/electron/index.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
/// <reference path="../../node_modules/electron/electron.d.ts" />
2+
13
interface Window {
4+
ipc: Electron.IpcRenderer
25
process?: {
36
type?: string
47
}

packages/components/src/libs/linking/index-electron.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,37 @@
22

33
import _ from 'lodash'
44

5-
const { remote, shell } = window.require('electron')
6-
75
import { LinkingCrossPlatform } from './index'
6+
import { Linking as LinkingOriginal } from './index-native'
87

98
const eventHandlers = new Map()
109

1110
export const Linking: LinkingCrossPlatform = {
1211
addEventListener: (type: string, handler: any) => {
1312
if (!(type === 'url' && typeof handler === 'function')) return
1413

15-
const wrapHandler = (_event: object, url: string) => {
14+
const wrapHandler = (_e: any, url: string) => {
1615
handler({ type, url })
1716
}
1817

1918
eventHandlers.set(handler, wrapHandler)
20-
remote.app.on('open-url', wrapHandler)
19+
window.ipc.addListener('open-url', wrapHandler)
2120
},
22-
async canOpenURL() {
23-
return true
21+
async canOpenURL(url: string) {
22+
return LinkingOriginal.canOpenURL(url)
2423
},
2524
async getInitialURL() {
26-
return remote.process.argv[1] || null
25+
return ''
2726
},
28-
openURL: (url: string, options?: object): Promise<void> => {
29-
return shell.openExternal(url, options)
30-
? Promise.resolve()
31-
: Promise.reject(new Error('Could not open URL'))
27+
openURL: (url: string): Promise<void> => {
28+
return LinkingOriginal.openURL(url)
3229
},
3330
removeEventListener: (type: string, handler: any) => {
3431
if (!(type === 'url' && typeof handler === 'function')) return
3532

3633
const wrapHandler = eventHandlers.get(handler)
3734
if (wrapHandler) {
38-
remote.app.removeListener('open-url', wrapHandler)
35+
window.ipc.removeListener('open-url', wrapHandler)
3936
}
4037

4138
eventHandlers.delete(handler)

packages/desktop/src/index.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function getBrowserWindowOptions() {
6565
affinity: 'main-window',
6666
backgroundThrottling: false,
6767
nativeWindowOpen: true,
68-
nodeIntegration: true,
68+
nodeIntegration: false,
69+
preload: path.join(__dirname, 'preload.js'),
6970
},
7071
...(config.get('isMenuBarMode')
7172
? {
@@ -261,6 +262,10 @@ function init() {
261262
},
262263
)
263264
})
265+
266+
app.on('open-url', (_event, url) => {
267+
if (mainWindow) mainWindow.webContents.send('open-url', url)
268+
})
264269
}
265270

266271
function getCenterPosition(obj: Electron.BrowserWindow | Electron.Tray) {

packages/desktop/src/preload.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import electron from 'electron'
2+
3+
window.ipc = electron.ipcRenderer

tslint.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"no-console": [true, "debug", "dir", "log", "trace", "warn"],
1515
"no-empty-interface": false,
1616
"no-namespace": false,
17+
"no-reference": false,
1718
"object-literal-sort-keys": false,
1819
"object-shorthand-properties-first": false,
1920
"prefer-array-literal": false,

0 commit comments

Comments
 (0)