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

Sandboxing the renderer process #969

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions src/lib/convertId.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs-extra';
import path from 'path';
import * as apmJson from './apmJson';
import { download, existsTempFile } from './ipcWrapper';
import { download, existsFile, getTempFilePath } from './ipcWrapper';
import * as modList from './modList';

/**
Expand All @@ -20,12 +20,12 @@ export async function getIdDict(
});
return convertJson ? await fs.readJson(convertJson) : {};
} else {
const convertJson = await existsTempFile(
const convertJsonPath = await getTempFilePath(
path.join('package', path.basename(dictUrl)),
dictUrl,
);
if (convertJson.exists) {
return await fs.readJson(convertJson.path);
if (existsFile(convertJsonPath)) {
return await fs.readJson(convertJsonPath);
} else {
return {};
}
Expand Down
20 changes: 15 additions & 5 deletions src/lib/ipcWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { ipcRenderer } from 'electron';

export const app = {
/**

Check warning on line 4 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Gets the app's name.
*/
getName: async function () {
return (await ipcRenderer.invoke('get-app-name')) as string;
},

/**

Check warning on line 11 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Gets the app's version.
*/
getVersion: async function () {
return (await ipcRenderer.invoke('get-app-version')) as string;
},

/**

Check warning on line 18 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Gets a path to a special directory or file associated with `name`.
* @param {string} name - A name associated with a special directory or file you get.
*/
Expand Down Expand Up @@ -49,7 +49,7 @@
},
};

/**

Check warning on line 52 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Whether the app is exe version.
*/
export async function isExeVersion() {
Expand All @@ -63,7 +63,7 @@
await ipcRenderer.invoke('check-update');
}

/**

Check warning on line 66 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Opens a file explorer and returns whether the directory exists.
* @param {string} relativePath - A relative path from the data directory.
*/
Expand All @@ -72,22 +72,32 @@
}

/**
* Returns whether the temporary file exists and the path.
* Returns the temporary file path.
* @param {string} relativePath - A relative path from the data directory.
* @param {string} [keyText] - String used to generate the hash.
* @returns {Promise<string>} The temporary file path.
*/
export async function existsTempFile(
export async function getTempFilePath(
relativePath: string,
keyText: string = undefined,
) {
return (await ipcRenderer.invoke(
'exists-temp-file',
return ipcRenderer.invoke(
'get-temp-file-path',
relativePath,
keyText,
)) as { exists: boolean; path: string };
) as Promise<string>;
}

/**
* Returns whether the file exists.
* @param {string} filePath - A file path
* @returns {Promise<boolean>} Whether the file exists
*/
export async function existsFile(filePath: string) {
return ipcRenderer.invoke('exists-file', filePath) as Promise<boolean>;
}

/**

Check warning on line 100 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Opens a directory dialog and returns the path selected by a user.
* @param {string} title - A title of the dialog.
* @param {string} defaultPath - A path of the initial directory.
Expand All @@ -114,7 +124,7 @@
await ipcRenderer.invoke('open-dialog', title, message, type);
}

/**

Check warning on line 127 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Opens a yes-no dialog and returns a response.
* @param {string} title - A title of the dialog.
* @param {string} message - A message showed in the dialog.
Expand All @@ -127,7 +137,7 @@
)) as boolean;
}

/**

Check warning on line 140 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Gets nicommons' data.
* @param {string} id - A nicommons ID.
*/
Expand All @@ -142,14 +152,14 @@
await ipcRenderer.invoke('open-about-window');
}

/**

Check warning on line 155 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Opens the confirm dialog for migration v1 to v2.
*/
export async function migration1to2ConfirmDialog() {
return (await ipcRenderer.invoke('migration1to2-confirm-dialog')) as number;
}

/**

Check warning on line 162 in src/lib/ipcWrapper.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing JSDoc @returns declaration
* Opens the input dialog of a data url for migration v1 to v2.
*/
export async function migration1to2DataurlInputDialog() {
Expand Down
12 changes: 6 additions & 6 deletions src/lib/modList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra';
import * as os from 'os';
import path from 'path';
import { isParent } from './apmPath';
import { download, existsTempFile } from './ipcWrapper';
import { download, existsFile, getTempFilePath } from './ipcWrapper';
import * as parseJson from './parseJson';
const store = new Store();

Expand Down Expand Up @@ -68,14 +68,14 @@ export async function updateInfo() {
* @returns {Promise<object>} - An object parsed from list.json.
*/
export async function getInfo() {
const modFile = await existsTempFile('list.json');
if (modFile.exists) {
return await parseJson.getMod(modFile.path).catch((): null => null);
const modFilePath = await getTempFilePath('list.json');
if (existsFile(modFilePath)) {
return await parseJson.getMod(modFilePath).catch((): null => null);
} else {
await updateInfo();
const downloadedModFile = await existsTempFile('list.json');
const downloadedModFilePath = await getTempFilePath('list.json');
return await parseJson
.getMod(downloadedModFile.path)
.getMod(downloadedModFilePath)
.catch((): null => null);
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import log from 'electron-log';
import prompt from 'electron-prompt';
import Store from 'electron-store';
import windowStateKeeper from 'electron-window-state';
import fs, { mkdir, readJsonSync } from 'fs-extra';
import fs, { exists, mkdir, readJsonSync } from 'fs-extra';
import path from 'path';
import 'source-map-support/register';
import { updateElectronApp } from 'update-electron-app';
Expand Down Expand Up @@ -205,17 +205,19 @@ ipcMain.handle('open-path', (event, relativePath) => {
return folderExists;
});

ipcMain.handle('exists-temp-file', (event, relativePath, keyText) => {
let filePath = path.join(app.getPath('userData'), 'Data/', relativePath);
ipcMain.handle('get-temp-file-path', (event, relativePath, keyText) => {
const filePath = path.join(app.getPath('userData'), 'Data/', relativePath);
if (keyText) {
filePath = path.join(
return path.join(
path.dirname(filePath),
getHash(keyText) + '_' + path.basename(filePath),
);
}
return { exists: fs.existsSync(filePath), path: filePath };
return filePath;
});

ipcMain.handle('exists-file', async (event, filePath) => exists(filePath));

ipcMain.handle('open-dir-dialog', async (event, title, defaultPath) => {
const win = BrowserWindow.getFocusedWindow();
const dir = await dialog.showOpenDialog(win, {
Expand Down Expand Up @@ -414,7 +416,6 @@ async function launch() {
icon: icon,
webPreferences: {
preload: ABOUT_WINDOW_PRELOAD_WEBPACK_ENTRY,
sandbox: false,
},
});
aboutWindow.once('close', () => {
Expand Down
28 changes: 11 additions & 17 deletions src/renderer/about/about_preload.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import log from 'electron-log';
import 'source-map-support/register';
import { app, openDialog } from '../../lib/ipcWrapper';
import replaceText from '../../lib/replaceText';
// import log from 'electron-log';
// import 'source-map-support/register';
import { contextBridge } from 'electron';
import { app } from '../../lib/ipcWrapper';

log.catchErrors({
/* log.catchErrors({
onError: async () => {
await openDialog(
'エラー',
Expand All @@ -13,16 +13,10 @@ log.catchErrors({
'error',
);
},
});
}); */

window.addEventListener('click', () => {
window.close();
});

window.addEventListener('DOMContentLoaded', async () => {
const appVersion = await app.getVersion();
replaceText('app-version', appVersion);
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency]);
}
});
contextBridge.exposeInMainWorld(
'appVersion',
async () => await app.getVersion(),
);
contextBridge.exposeInMainWorld('versions', process.versions);
15 changes: 15 additions & 0 deletions src/renderer/about/about_renderer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import '../../../node_modules/bootstrap/dist/css/bootstrap.min.css';
import '../main.css';
import './about.css';
import replaceText from '../../lib/replaceText';

declare const appVersion: () => Promise<string>;
declare const versions: { [key: string]: string };

window.addEventListener('click', () => {
window.close();
});

window.addEventListener('DOMContentLoaded', async () => {
replaceText('app-version', await appVersion());
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, versions[dependency]);
}
});
Loading