diff options
author | Kristóf Marussy <kristof@marussy.com> | 2022-01-02 11:32:59 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2022-01-02 11:46:08 +0100 |
commit | 3127fc53390bce67f816e4cc9112135785b067d5 (patch) | |
tree | cc9508224119a4c231acede12c72a502e1082537 /packages/main/src | |
parent | fix: Ensure dev user directory exists (diff) | |
download | sophie-3127fc53390bce67f816e4cc9112135785b067d5.tar.gz sophie-3127fc53390bce67f816e4cc9112135785b067d5.tar.zst sophie-3127fc53390bce67f816e4cc9112135785b067d5.zip |
fix: Allow devtools extensions to be installed
On the first startup in dev mode, Electron must be able to download the
devtools extensions and wait for them to be installed.
Loosens the UI process request filter a bit, but the behavior should
match production mode in all cases except chrome webstore URLs.
Nevertheless, only production mode should be considered secure.
Fixes #6
Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to 'packages/main/src')
-rw-r--r-- | packages/main/src/devTools.ts | 14 | ||||
-rw-r--r-- | packages/main/src/index.ts | 23 |
2 files changed, 27 insertions, 10 deletions
diff --git a/packages/main/src/devTools.ts b/packages/main/src/devTools.ts index 7c7c86a..398904c 100644 --- a/packages/main/src/devTools.ts +++ b/packages/main/src/devTools.ts | |||
@@ -21,6 +21,20 @@ | |||
21 | import type { BrowserWindow } from 'electron'; | 21 | import type { BrowserWindow } from 'electron'; |
22 | 22 | ||
23 | /** | 23 | /** |
24 | * URL prefixes Sophie is allowed load in dev mode. | ||
25 | * | ||
26 | * In dev mode, in addition to the application itself, | ||
27 | * Sophie must be able do download and load the devtools and related extensions, | ||
28 | * so we have to make exceptions in the UI process request filter. | ||
29 | */ | ||
30 | export const DEVMODE_ALLOWED_URL_PREFIXES = [ | ||
31 | 'chrome-extension:', | ||
32 | 'devtools:', | ||
33 | 'https://clients2.google.com/service/update2/crx', | ||
34 | 'https://clients2.googleusercontent.com/crx', | ||
35 | ]; | ||
36 | |||
37 | /** | ||
24 | * Installs the react and redux developer tools extensions. | 38 | * Installs the react and redux developer tools extensions. |
25 | * | 39 | * |
26 | * We use the redux devtools and connect the mobx store to it with `mst-middlewares`, | 40 | * We use the redux devtools and connect the mobx store to it with `mst-middlewares`, |
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index ff14332..7c6bfd5 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -42,6 +42,7 @@ import { URL } from 'url'; | |||
42 | 42 | ||
43 | import { init } from './compositionRoot.js'; | 43 | import { init } from './compositionRoot.js'; |
44 | import { | 44 | import { |
45 | DEVMODE_ALLOWED_URL_PREFIXES, | ||
45 | installDevToolsExtensions, | 46 | installDevToolsExtensions, |
46 | openDevToolsWhenReady, | 47 | openDevToolsWhenReady, |
47 | } from './devTools.js'; | 48 | } from './devTools.js'; |
@@ -73,14 +74,6 @@ app.commandLine.appendSwitch( | |||
73 | 'HardwareMediaKeyHandling,MediaSessionService', | 74 | 'HardwareMediaKeyHandling,MediaSessionService', |
74 | ); | 75 | ); |
75 | 76 | ||
76 | // It doesn't seem to cause a race condition to start installing the extensions this early. | ||
77 | if (isDevelopment) { | ||
78 | app.whenReady().then(installDevToolsExtensions).catch((err) => { | ||
79 | console.error('Failed to install devtools extensions', err); | ||
80 | process.exit(1); | ||
81 | }); | ||
82 | } | ||
83 | |||
84 | // Remove sophie and electron from the user-agent string to avoid detection. | 77 | // Remove sophie and electron from the user-agent string to avoid detection. |
85 | const originalUserAgent = app.userAgentFallback; | 78 | const originalUserAgent = app.userAgentFallback; |
86 | const userAgent = originalUserAgent.replaceAll(/\s(sophie|Electron)\/\S+/g, ''); | 79 | const userAgent = originalUserAgent.replaceAll(/\s(sophie|Electron)\/\S+/g, ''); |
@@ -127,7 +120,7 @@ function shouldCancelMainWindowRequest(url: string, method: string): boolean { | |||
127 | return true; | 120 | return true; |
128 | } | 121 | } |
129 | if (isDevelopment) { | 122 | if (isDevelopment) { |
130 | if (normalizedUrl.startsWith('devtools:') || normalizedUrl.startsWith('chrome-extension:')) { | 123 | if (DEVMODE_ALLOWED_URL_PREFIXES.some((prefix) => normalizedUrl.startsWith(prefix))) { |
131 | return false; | 124 | return false; |
132 | } | 125 | } |
133 | if (import.meta.env.VITE_DEV_SERVER_URL !== undefined) { | 126 | if (import.meta.env.VITE_DEV_SERVER_URL !== undefined) { |
@@ -315,7 +308,17 @@ app.on('window-all-closed', () => { | |||
315 | } | 308 | } |
316 | }); | 309 | }); |
317 | 310 | ||
318 | app.whenReady().then(createWindow).catch((err) => { | 311 | app.whenReady().then(async () => { |
312 | if (isDevelopment) { | ||
313 | try { | ||
314 | await installDevToolsExtensions(); | ||
315 | } catch (err) { | ||
316 | console.error('Failed to install devtools extensions', err); | ||
317 | } | ||
318 | } | ||
319 | |||
320 | return createWindow(); | ||
321 | }).catch((err) => { | ||
319 | console.error('Failed to create window', err); | 322 | console.error('Failed to create window', err); |
320 | process.exit(1); | 323 | process.exit(1); |
321 | }); | 324 | }); |