mirror of
https://github.com/Novaloop-AG/healthchat-web.git
synced 2026-12-03 12:49:30 +00:00
e4da468d07
* Avoid destroying a still-valid session when the OS keychain is temporarily unreadable SafeStorageWriter.get() previously caught a decrypt failure and returned undefined, indistinguishable from "no secret stored". When the OS keychain is briefly locked or its ACL is invalidated by an app re-sign/update, an existing valid pickle key read as absent, so the renderer fell back to the default key and createPickleKey then overwrote the still-valid ciphertext - turning a transient hiccup into permanent session and encryption-key loss. Distinguish absent from present-but-undecryptable with a typed SafeStorageDecryptionError, and never overwrite an existing undecryptable secret, so the session recovers on a later launch once the keychain is readable again. * Fail closed when a stored secret cannot be read, and defer the basic_text migration instead of poisoning skipped secrets - isSecretUndecryptable now treats any error reading an existing secret as undecryptable, so an unexpected storage-layer error can never re-enable the destructive pickle-key overwrite. - migrateBasicTextToPlaintext decrypts all secrets before writing anything: if any secret cannot be decrypted the migration is aborted and the app sticks with the basic_text backend (safeStorageBackendOverride) instead of recording the store as plaintext while a secret is still ciphertext - which would make the next launch re-encrypt that ciphertext as though it were the secret, silently corrupting it and defeating the overwrite guard. * Fix the safeStorage tests so they run under CI store.ts reads getConfig().brand when it raises a degraded-mode dialog, and the real config module only populates itself in loadConfig(), which these tests never call. Stubbing it lets the tests reach the dialog they are actually about instead of dying on an undefined brand. The ElectronStore fake also carried a constructor whose only job was to swallow the options object. oxlint's no-useless-constructor is an error here, and it was the one genuine failure on this branch, so the fake now leans on the implicit default constructor - which ignores its arguments just as happily.
241 lines
9.3 KiB
TypeScript
241 lines
9.3 KiB
TypeScript
/*
|
|
Copyright 2022-2025 New Vector Ltd.
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
|
|
Please see LICENSE files in the repository root for full details.
|
|
*/
|
|
|
|
import { app, autoUpdater, desktopCapturer, ipcMain, powerSaveBlocker, TouchBar, nativeImage } from "electron";
|
|
|
|
import IpcMainEvent = Electron.IpcMainEvent;
|
|
import { randomArray } from "./utils.js";
|
|
import { getDisplayMediaCallback, setDisplayMediaCallback } from "./displayMediaCallback.js";
|
|
import Store, { clearDataAndRelaunch } from "./store.js";
|
|
import { getConfig } from "./config.js";
|
|
|
|
let focusHandlerAttached = false;
|
|
ipcMain.on("loudNotification", function (): void {
|
|
if (process.platform === "win32" || process.platform === "linux") {
|
|
if (global.mainWindow && !global.mainWindow.isFocused() && !focusHandlerAttached) {
|
|
global.mainWindow.flashFrame(true);
|
|
global.mainWindow.once("focus", () => {
|
|
global.mainWindow?.flashFrame(false);
|
|
focusHandlerAttached = false;
|
|
});
|
|
focusHandlerAttached = true;
|
|
}
|
|
}
|
|
});
|
|
|
|
let powerSaveBlockerId: number | null = null;
|
|
ipcMain.on("app_onAction", function (_ev: IpcMainEvent, payload) {
|
|
switch (payload.action) {
|
|
case "call_state": {
|
|
if (powerSaveBlockerId !== null && powerSaveBlocker.isStarted(powerSaveBlockerId)) {
|
|
if (payload.state === "ended") {
|
|
powerSaveBlocker.stop(powerSaveBlockerId);
|
|
powerSaveBlockerId = null;
|
|
}
|
|
} else {
|
|
if (powerSaveBlockerId === null && payload.state === "connected") {
|
|
powerSaveBlockerId = powerSaveBlocker.start("prevent-display-sleep");
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
|
|
ipcMain.on("ipcCall", async function (_ev: IpcMainEvent, payload) {
|
|
const store = Store.instance;
|
|
if (!global.mainWindow || !store) return;
|
|
|
|
const args = payload.args || [];
|
|
let ret: any;
|
|
|
|
switch (payload.name) {
|
|
case "getUpdateFeedUrl":
|
|
ret = autoUpdater.getFeedURL();
|
|
break;
|
|
case "setLanguage":
|
|
global.appLocalization.setAppLocale(args[0]);
|
|
break;
|
|
case "getAppVersion":
|
|
ret = app.getVersion();
|
|
break;
|
|
case "focusWindow":
|
|
if (global.mainWindow.isMinimized()) {
|
|
global.mainWindow.restore();
|
|
} else {
|
|
global.mainWindow.show();
|
|
global.mainWindow.focus();
|
|
}
|
|
break;
|
|
|
|
case "navigateBack":
|
|
if (global.mainWindow.webContents.canGoBack()) {
|
|
global.mainWindow.webContents.goBack();
|
|
}
|
|
break;
|
|
case "navigateForward":
|
|
if (global.mainWindow.webContents.canGoForward()) {
|
|
global.mainWindow.webContents.goForward();
|
|
}
|
|
break;
|
|
case "setSpellCheckEnabled":
|
|
if (typeof args[0] !== "boolean") return;
|
|
|
|
global.mainWindow.webContents.session.setSpellCheckerEnabled(args[0]);
|
|
store.set("spellCheckerEnabled", args[0]);
|
|
break;
|
|
|
|
case "getSpellCheckEnabled":
|
|
ret = store.get("spellCheckerEnabled");
|
|
break;
|
|
|
|
case "setSpellCheckLanguages":
|
|
try {
|
|
global.mainWindow.webContents.session.setSpellCheckerLanguages(args[0]);
|
|
} catch (er) {
|
|
console.log("There were problems setting the spellcheck languages", er);
|
|
}
|
|
break;
|
|
|
|
case "getSpellCheckLanguages":
|
|
ret = global.mainWindow.webContents.session.getSpellCheckerLanguages();
|
|
break;
|
|
case "getAvailableSpellCheckLanguages":
|
|
ret = global.mainWindow.webContents.session.availableSpellCheckerLanguages;
|
|
break;
|
|
|
|
case "getPickleKey":
|
|
try {
|
|
ret = await store.getSecret(`${args[0]}|${args[1]}`);
|
|
} catch {
|
|
// An error is thrown if we can't initialise safeStorage, or if a stored secret exists
|
|
// but can't be decrypted this launch (SafeStorageDecryptionError, e.g. the OS keychain
|
|
// is temporarily unavailable). In both cases return null so the default pickle key is
|
|
// used; we must NOT destroy the existing secret (see createPickleKey below) so the
|
|
// session can recover on a later launch. See element-web#32521 / #32715.
|
|
ret = null;
|
|
}
|
|
break;
|
|
|
|
case "createPickleKey":
|
|
try {
|
|
// Never overwrite a pickle key that already exists but is currently undecryptable.
|
|
// Overwriting it with a freshly-generated key would turn a transient keychain failure
|
|
// into permanent session and encryption-key loss. Preserve it so the existing session
|
|
// can be restored on a later launch once the keychain is readable again.
|
|
if (await store.isSecretUndecryptable(`${args[0]}|${args[1]}`)) {
|
|
console.warn("Refusing to overwrite an existing undecryptable pickle key; preserving it");
|
|
ret = null;
|
|
} else {
|
|
const pickleKey = await randomArray(32);
|
|
await store.setSecret(`${args[0]}|${args[1]}`, pickleKey);
|
|
ret = pickleKey;
|
|
}
|
|
} catch (e) {
|
|
console.error("Failed to create pickle key", e);
|
|
ret = null;
|
|
}
|
|
break;
|
|
|
|
case "destroyPickleKey":
|
|
try {
|
|
await store.deleteSecret(`${args[0]}|${args[1]}`);
|
|
} catch (e) {
|
|
console.error("Failed to destroy pickle key", e);
|
|
}
|
|
break;
|
|
case "getDesktopCapturerSources":
|
|
ret = (await desktopCapturer.getSources(args[0])).map((source) => ({
|
|
id: source.id,
|
|
name: source.name,
|
|
thumbnailURL: source.thumbnail.toDataURL(),
|
|
}));
|
|
break;
|
|
case "callDisplayMediaCallback":
|
|
getDisplayMediaCallback()?.({ video: args[0] });
|
|
setDisplayMediaCallback(null);
|
|
ret = null;
|
|
break;
|
|
|
|
case "clearStorage":
|
|
await clearDataAndRelaunch(global.mainWindow.webContents.session);
|
|
return; // the app is about to stop, we don't need to reply to the IPC
|
|
|
|
case "breadcrumbs": {
|
|
if (process.platform === "darwin") {
|
|
const { TouchBarPopover, TouchBarButton } = TouchBar;
|
|
|
|
const recentsBar = new TouchBar({
|
|
items: args[0].map((r: { roomId: string; avatarUrl: string | null; initial: string }) => {
|
|
const defaultColors = ["#0DBD8B", "#368bd6", "#ac3ba8"];
|
|
let total = 0;
|
|
for (let i = 0; i < r.roomId.length; ++i) {
|
|
total += r.roomId.charCodeAt(i);
|
|
}
|
|
|
|
const button = new TouchBarButton({
|
|
label: r.initial,
|
|
backgroundColor: defaultColors[total % defaultColors.length],
|
|
click: (): void => {
|
|
void global.mainWindow?.loadURL(`vector://vector/webapp/#/room/${r.roomId}`);
|
|
},
|
|
});
|
|
if (r.avatarUrl) {
|
|
void fetch(r.avatarUrl)
|
|
.then((resp) => {
|
|
if (!resp.ok) return;
|
|
return resp.arrayBuffer();
|
|
})
|
|
.then((arrayBuffer) => {
|
|
if (!arrayBuffer) return;
|
|
const buffer = Buffer.from(arrayBuffer);
|
|
button.icon = nativeImage.createFromBuffer(buffer);
|
|
button.label = "";
|
|
button.backgroundColor = "";
|
|
});
|
|
}
|
|
return button;
|
|
}),
|
|
});
|
|
|
|
const touchBar = new TouchBar({
|
|
items: [
|
|
new TouchBarPopover({
|
|
label: "Recents",
|
|
showCloseButton: true,
|
|
items: recentsBar,
|
|
}),
|
|
],
|
|
});
|
|
global.mainWindow.setTouchBar(touchBar);
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
global.mainWindow.webContents.send("ipcReply", {
|
|
id: payload.id,
|
|
error: "Unknown IPC Call: " + payload.name,
|
|
});
|
|
return;
|
|
}
|
|
|
|
global.mainWindow?.webContents.send("ipcReply", {
|
|
id: payload.id,
|
|
reply: ret,
|
|
});
|
|
});
|
|
|
|
ipcMain.handle("getConfig", getConfig);
|
|
|
|
const initialisePromiseWithResolvers = Promise.withResolvers<void>();
|
|
export const initialisePromise = initialisePromiseWithResolvers.promise;
|
|
|
|
ipcMain.once("initialise", () => {
|
|
initialisePromiseWithResolvers.resolve();
|
|
});
|