Skip to content
Snippets Groups Projects
Unverified Commit f75d0f80 authored by Michael Clark's avatar Michael Clark Committed by GitHub
Browse files

:electron: security.js and preload.js :arrow_right: .ts (#3066)

parent 07bbe000
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,8 @@ import {
protocol,
utilityProcess,
UtilityProcess,
OpenDialogSyncOptions,
SaveDialogOptions,
} from 'electron';
import isDev from 'electron-is-dev';
// @ts-strict-ignore
......@@ -269,11 +271,18 @@ app.on('activate', () => {
}
});
export type GetBootstrapDataPayload = {
version: string;
isDev: boolean;
};
ipcMain.on('get-bootstrap-data', event => {
event.returnValue = {
const payload: GetBootstrapDataPayload = {
version: app.getVersion(),
isDev,
};
event.returnValue = payload;
});
ipcMain.handle('relaunch', () => {
......@@ -281,16 +290,33 @@ ipcMain.handle('relaunch', () => {
app.exit();
});
ipcMain.handle('open-file-dialog', (event, { filters, properties }) => {
return dialog.showOpenDialogSync({
properties: properties || ['openFile'],
filters,
});
});
export type OpenFileDialogPayload = {
properties: OpenDialogSyncOptions['properties'];
filters?: OpenDialogSyncOptions['filters'];
};
ipcMain.handle(
'open-file-dialog',
(_event, { filters, properties }: OpenFileDialogPayload) => {
return dialog.showOpenDialogSync({
properties: properties || ['openFile'],
filters,
});
},
);
export type SaveFileDialogPayload = {
title: SaveDialogOptions['title'];
defaultPath?: SaveDialogOptions['defaultPath'];
fileContents: string | NodeJS.ArrayBufferView;
};
ipcMain.handle(
'save-file-dialog',
async (event, { title, defaultPath, fileContents }) => {
async (
_event,
{ title, defaultPath, fileContents }: SaveFileDialogPayload,
) => {
const fileLocation = await dialog.showSaveDialog({ title, defaultPath });
return new Promise<void>((resolve, reject) => {
......@@ -327,15 +353,15 @@ ipcMain.on('screenshot', () => {
}
});
ipcMain.on('update-menu', (event, budgetId?: string) => {
ipcMain.on('update-menu', (_event, budgetId?: string) => {
updateMenu(budgetId);
});
ipcMain.on('set-theme', theme => {
ipcMain.on('set-theme', (_event, theme: string) => {
const obj = { theme };
if (clientWin) {
clientWin.webContents.executeJavaScript(
`window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs(${obj})`,
`window.__actionsForMenu && window.__actionsForMenu.saveGlobalPrefs(${JSON.stringify(obj)})`,
);
}
});
const { ipcRenderer, contextBridge } = require('electron');
import { ipcRenderer, contextBridge, IpcRenderer } from 'electron';
const { version: VERSION, isDev: IS_DEV } =
import {
GetBootstrapDataPayload,
OpenFileDialogPayload,
SaveFileDialogPayload,
} from './index';
const { version: VERSION, isDev: IS_DEV }: GetBootstrapDataPayload =
ipcRenderer.sendSync('get-bootstrap-data');
contextBridge.exposeInMainWorld('Actual', {
IS_DEV,
ACTUAL_VERSION: VERSION,
logToTerminal: (...args) => {
require('console').log(...args);
},
logToTerminal: console.log,
ipcConnect: func => {
ipcConnect: (
func: (payload: {
on: IpcRenderer['on'];
emit: (name: string, data: unknown) => void;
}) => void,
) => {
func({
on(name, handler) {
return ipcRenderer.on(name, (_event, value) => handler(value));
......@@ -25,11 +34,15 @@ contextBridge.exposeInMainWorld('Actual', {
ipcRenderer.invoke('relaunch');
},
openFileDialog: opts => {
openFileDialog: (opts: OpenFileDialogPayload) => {
return ipcRenderer.invoke('open-file-dialog', opts);
},
saveFile: async (contents, filename, dialogTitle) => {
saveFile: async (
contents: SaveFileDialogPayload['fileContents'],
filename: SaveFileDialogPayload['defaultPath'],
dialogTitle: SaveFileDialogPayload['title'],
) => {
await ipcRenderer.invoke('save-file-dialog', {
title: dialogTitle,
defaultPath: filename,
......@@ -37,15 +50,15 @@ contextBridge.exposeInMainWorld('Actual', {
});
},
openURLInBrowser: url => {
openURLInBrowser: (url: string) => {
ipcRenderer.invoke('open-external-url', url);
},
onEventFromMain: (type, handler) => {
onEventFromMain: (type: string, handler: (...args: unknown[]) => void) => {
ipcRenderer.on(type, handler);
},
updateAppMenu: budgetId => {
updateAppMenu: (budgetId?: string) => {
ipcRenderer.send('update-menu', budgetId);
},
......@@ -53,7 +66,7 @@ contextBridge.exposeInMainWorld('Actual', {
return null;
},
setTheme: theme => {
setTheme: (theme: string) => {
ipcRenderer.send('set-theme', theme);
},
});
const electron = require('electron');
import { app, session } from 'electron';
electron.app.on('web-contents-created', function (event, contents) {
app.on('web-contents-created', function (event, contents) {
contents.on('will-attach-webview', function (event, webPreferences) {
delete webPreferences.preloadURL;
delete webPreferences.preload;
webPreferences.nodeIntegration = false;
webPreferences.webSecurity = true;
webPreferences.allowRunningInsecureContent = false;
webPreferences.experimentalFeatures = false;
webPreferences.enableBlinkFeatures = false;
// For now, we never use <webview>. Just disable it entirely.
event.preventDefault();
......@@ -18,14 +16,10 @@ electron.app.on('web-contents-created', function (event, contents) {
contents.on('will-navigate', event => {
event.preventDefault();
});
contents.on('new-window', event => {
event.preventDefault();
});
});
electron.app.on('ready', function () {
electron.session.defaultSession.setPermissionRequestHandler(
app.on('ready', function () {
session.defaultSession.setPermissionRequestHandler(
function (webContents, permission, callback) {
const url = webContents.getURL();
if (url.startsWith('file://')) {
......
---
category: Maintenance
authors: [MikesGlitch]
---
Updated security.js and preload.js to Typescript and fixed Theme not setting correctly when set via dev console
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment