2
0
mirror of https://github.com/frappe/books.git synced 2024-12-22 19:09:01 +00:00

incr: enable context isolation

This commit is contained in:
18alantom 2023-07-10 14:42:20 +05:30
parent e2b9ea1f49
commit 92e8dfb6d7
7 changed files with 78 additions and 59 deletions

View File

@ -50,6 +50,7 @@ export type BespokeFunction = (
db: DatabaseCore, db: DatabaseCore,
...args: unknown[] ...args: unknown[]
) => Promise<unknown>; ) => Promise<unknown>;
export type SingleValue<T> = { export type SingleValue<T> = {
fieldname: string; fieldname: string;
parent: string; parent: string;

View File

@ -1,7 +1,5 @@
import { AuthDemuxBase } from 'utils/auth/types'; import { AuthDemuxBase } from 'utils/auth/types';
import { IPC_ACTIONS } from 'utils/messages';
import { Creds } from 'utils/types'; import { Creds } from 'utils/types';
const { ipcRenderer } = require('electron');
export class AuthDemux extends AuthDemuxBase { export class AuthDemux extends AuthDemuxBase {
#isElectron = false; #isElectron = false;
@ -12,7 +10,7 @@ export class AuthDemux extends AuthDemuxBase {
async getCreds(): Promise<Creds> { async getCreds(): Promise<Creds> {
if (this.#isElectron) { if (this.#isElectron) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds; return await ipc.getCreds();
} else { } else {
return { errorLogUrl: '', tokenString: '', telemetryUrl: '' }; return { errorLogUrl: '', tokenString: '', telemetryUrl: '' };
} }

View File

@ -1,26 +1,12 @@
import type Store from 'electron-store';
import { ConfigMap } from 'fyo/core/types'; import { ConfigMap } from 'fyo/core/types';
import type { IPC } from 'main/preload';
export class Config { export class Config {
config: Map<string, unknown> | Store; config: Map<string, unknown> | IPC['store'];
constructor(isElectron: boolean) { constructor(isElectron: boolean) {
this.config = new Map(); this.config = new Map();
if (isElectron) { if (isElectron) {
const Config = require('electron-store') as typeof Store; this.config = ipc.store;
this.config = new Config();
}
}
get store() {
if (this.config instanceof Map) {
const store: Record<string, unknown> = {};
for (const key of this.config.keys()) {
store[key] = this.config.get(key);
}
return store;
} else {
return this.config;
} }
} }
@ -39,8 +25,4 @@ export class Config {
delete(key: keyof ConfigMap) { delete(key: keyof ConfigMap) {
this.config.delete(key); this.config.delete(key);
} }
clear() {
this.config.clear();
}
} }

View File

@ -1,9 +1,7 @@
const { ipcRenderer } = require('electron');
import { DatabaseError, NotImplemented } from 'fyo/utils/errors'; import { DatabaseError, NotImplemented } from 'fyo/utils/errors';
import { SchemaMap } from 'schemas/types'; import { SchemaMap } from 'schemas/types';
import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types'; import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types';
import { BackendResponse } from 'utils/ipc/types'; import { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS } from 'utils/messages';
export class DatabaseDemux extends DatabaseDemuxBase { export class DatabaseDemux extends DatabaseDemuxBase {
#isElectron = false; #isElectron = false;
@ -32,9 +30,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
} }
return (await this.#handleDBCall(async () => { return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke( return await ipc.db.getSchema();
IPC_ACTIONS.DB_SCHEMA
)) as BackendResponse;
})) as SchemaMap; })) as SchemaMap;
} }
@ -47,11 +43,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
} }
return (await this.#handleDBCall(async () => { return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke( return ipc.db.create(dbPath, countryCode);
IPC_ACTIONS.DB_CREATE,
dbPath,
countryCode
)) as BackendResponse;
})) as string; })) as string;
} }
@ -64,11 +56,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
} }
return (await this.#handleDBCall(async () => { return (await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke( return ipc.db.connect(dbPath, countryCode);
IPC_ACTIONS.DB_CONNECT,
dbPath,
countryCode
)) as BackendResponse;
})) as string; })) as string;
} }
@ -78,11 +66,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
} }
return await this.#handleDBCall(async () => { return await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke( return await ipc.db.call(method, ...args);
IPC_ACTIONS.DB_CALL,
method,
...args
)) as BackendResponse;
}); });
} }
@ -92,11 +76,7 @@ export class DatabaseDemux extends DatabaseDemuxBase {
} }
return await this.#handleDBCall(async () => { return await this.#handleDBCall(async () => {
return (await ipcRenderer.invoke( return await ipc.db.bespoke(method, ...args);
IPC_ACTIONS.DB_BESPOKE,
method,
...args
)) as BackendResponse;
}); });
} }
} }

View File

@ -96,7 +96,7 @@ export class Fyo {
setIsElectron() { setIsElectron() {
try { try {
this.isElectron = Boolean(require('electron')); this.isElectron = !!window?.ipc;
} catch { } catch {
this.isElectron = false; this.isElectron = false;
} }

View File

@ -4,6 +4,7 @@ require('source-map-support').install({
environment: 'node', environment: 'node',
}); });
import { emitMainProcessError } from 'backend/helpers';
import { import {
app, app,
BrowserWindow, BrowserWindow,
@ -12,7 +13,6 @@ import {
ProtocolRequest, ProtocolRequest,
ProtocolResponse, ProtocolResponse,
} from 'electron'; } from 'electron';
import Store from 'electron-store';
import { autoUpdater } from 'electron-updater'; import { autoUpdater } from 'electron-updater';
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
@ -21,7 +21,6 @@ import registerAutoUpdaterListeners from './main/registerAutoUpdaterListeners';
import registerIpcMainActionListeners from './main/registerIpcMainActionListeners'; import registerIpcMainActionListeners from './main/registerIpcMainActionListeners';
import registerIpcMainMessageListeners from './main/registerIpcMainMessageListeners'; import registerIpcMainMessageListeners from './main/registerIpcMainMessageListeners';
import registerProcessListeners from './main/registerProcessListeners'; import registerProcessListeners from './main/registerProcessListeners';
import { emitMainProcessError } from 'backend/helpers';
export class Main { export class Main {
title = 'Frappe Books'; title = 'Frappe Books';
@ -54,8 +53,6 @@ export class Main {
'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0', 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0',
}; };
Store.initRenderer();
this.registerListeners(); this.registerListeners();
if (this.isMac && this.isDevelopment) { if (this.isMac && this.isDevelopment) {
app.dock.setIcon(this.icon); app.dock.setIcon(this.icon);
@ -95,7 +92,7 @@ export class Main {
titleBarStyle: 'hidden', titleBarStyle: 'hidden',
trafficLightPosition: { x: 16, y: 16 }, trafficLightPosition: { x: 16, y: 16 },
webPreferences: { webPreferences: {
contextIsolation: false, // TODO: Switch this off contextIsolation: true,
nodeIntegration: true, nodeIntegration: true,
preload, preload,
}, },

View File

@ -4,11 +4,15 @@ import type {
SaveDialogOptions, SaveDialogOptions,
SaveDialogReturnValue, SaveDialogReturnValue,
} from 'electron'; } from 'electron';
import { ipcRenderer } from 'electron'; import { contextBridge, ipcRenderer } from 'electron';
import { BackendResponse } from 'utils/ipc/types'; import type { ConfigMap } from 'fyo/core/types';
import config from 'utils/config';
import type { DatabaseMethod } from 'utils/db/types';
import type { BackendResponse } from 'utils/ipc/types';
import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages'; import { IPC_ACTIONS, IPC_CHANNELS, IPC_MESSAGES } from 'utils/messages';
import type { import type {
ConfigFilesWithModified, ConfigFilesWithModified,
Creds,
LanguageMap, LanguageMap,
SelectFileOptions, SelectFileOptions,
SelectFileReturn, SelectFileReturn,
@ -23,6 +27,10 @@ const ipc = {
return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW); return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW);
}, },
async getCreds() {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds;
},
async getLanguageMap(code: string) { async getLanguageMap(code: string) {
return (await ipcRenderer.invoke(IPC_ACTIONS.GET_LANGUAGE_MAP, code)) as { return (await ipcRenderer.invoke(IPC_ACTIONS.GET_LANGUAGE_MAP, code)) as {
languageMap: LanguageMap; languageMap: LanguageMap;
@ -136,8 +144,61 @@ const ipc = {
registerConsoleLogListener(listener: IPCRendererListener) { registerConsoleLogListener(listener: IPCRendererListener) {
ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener); ipcRenderer.on(IPC_CHANNELS.CONSOLE_LOG, listener);
}, },
db: {
async getSchema() {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_SCHEMA
)) as BackendResponse;
},
async create(dbPath: string, countryCode?: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CREATE,
dbPath,
countryCode
)) as BackendResponse;
},
async connect(dbPath: string, countryCode?: string) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CONNECT,
dbPath,
countryCode
)) as BackendResponse;
},
async call(method: DatabaseMethod, ...args: unknown[]) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_CALL,
method,
...args
)) as BackendResponse;
},
async bespoke(method: string, ...args: unknown[]) {
return (await ipcRenderer.invoke(
IPC_ACTIONS.DB_BESPOKE,
method,
...args
)) as BackendResponse;
},
},
store: {
get<K extends keyof ConfigMap>(key: K) {
return config.get(key);
},
set<K extends keyof ConfigMap>(key: K, value: ConfigMap[K]) {
return config.set(key, value);
},
delete(key: keyof ConfigMap) {
return config.delete(key);
},
},
} as const; } as const;
// contextBridge.exposeInMainWorld('api', ipc); contextBridge.exposeInMainWorld('ipc', ipc);
window.ipc = ipc;
export type IPC = typeof ipc; export type IPC = typeof ipc;