Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:electron: security.js and preload.js ➡️ .ts #3066

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions packages/desktop-electron/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
protocol,
utilityProcess,
UtilityProcess,
OpenDialogSyncOptions,
SaveDialogOptions,
} from 'electron';
import isDev from 'electron-is-dev';
// @ts-strict-ignore
Expand Down Expand Up @@ -269,28 +271,52 @@ 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', () => {
app.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) => {
Expand Down Expand Up @@ -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)})`,
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
);
}
});
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
const { ipcRenderer, contextBridge } = require('electron');
import { ipcRenderer, contextBridge } 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: (...args: unknown[]) => {
console.log(...args);
},
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved

ipcConnect: func => {

Check failure on line 19 in packages/desktop-electron/preload.ts

View workflow job for this annotation

GitHub Actions / typecheck

Parameter 'func' implicitly has an 'any' type.
func({
on(name, handler) {
on(name: string, handler: (payload: unknown) => void) {
return ipcRenderer.on(name, (_event, value) => handler(value));
},
emit(name, data) {
emit(name: string, data: unknown) {
return ipcRenderer.send('message', { name, args: data });
},
});
Expand All @@ -25,35 +31,39 @@
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,
fileContents: contents,
});
},

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);
},

getServerSocket: () => {
return null;
},

setTheme: theme => {
setTheme: (theme: string) => {
ipcRenderer.send('set-theme', theme);
},
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
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;
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved

webPreferences.nodeIntegration = false;
webPreferences.webSecurity = true;
webPreferences.allowRunningInsecureContent = false;
webPreferences.experimentalFeatures = false;
webPreferences.enableBlinkFeatures = false;

MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
// For now, we never use <webview>. Just disable it entirely.
event.preventDefault();
Expand All @@ -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();
MikesGlitch marked this conversation as resolved.
Show resolved Hide resolved
});
});

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://')) {
Expand Down
Loading