Skip to content

Build your personal knowledge base with Trilium Notes

chore(desktop): address requested changes

Elian Doran9adca0cd1d50
diff --git a/apps/desktop/src/protocol.ts b/apps/desktop/src/protocol.ts
index 00b9698695..11649586b0 100644
--- a/apps/desktop/src/protocol.ts
+++ b/apps/desktop/src/protocol.ts
@@ -345,7 +345,7 @@ function normalizeResponseHeaders(headers: Record<string, number | string | stri
if (value === undefined) continue;
if (STRIPPED_HEADERS.has(name.toLowerCase())) continue;
if (Array.isArray(value)) {
- for (const v of value) out.push([name, v]);
+ for (const v of value) out.push([name, String(v)]);
} else {
out.push([name, String(value)]);
}
diff --git a/apps/desktop/src/services/custom_dictionary.ts b/apps/desktop/src/services/custom_dictionary.ts
index 5f809f1c7a..e84999b269 100644
--- a/apps/desktop/src/services/custom_dictionary.ts
+++ b/apps/desktop/src/services/custom_dictionary.ts
@@ -125,7 +125,14 @@ export function setupCustomDictionary() {
);
});
- electron.ipcMain.on("add-word-to-dictionary", (event, word: string) => {
+ electron.ipcMain.on("add-word-to-dictionary", (event, word: unknown) => {
+ // Defensive: ipcMain accepts any structured-clonable payload, so a
+ // compromised renderer could send a non-string. Reject without doing
+ // anything observable.
+ if (typeof word !== "string" || word.length === 0) {
+ getLog().error("add-word-to-dictionary: invalid word payload received");
+ return;
+ }
event.sender.session.addWordToSpellCheckerDictionary(word);
addWord(word);
});
diff --git a/apps/desktop/src/services/request.ts b/apps/desktop/src/services/request.ts
index 8b56e928d6..2a635cab5e 100644
--- a/apps/desktop/src/services/request.ts
+++ b/apps/desktop/src/services/request.ts
@@ -14,6 +14,14 @@ import electron from "electron";
export default class ElectronRequestProvider extends NodeRequestProvider {
protected async getClient(opts: ClientOpts): Promise<Client> {
if (!opts.proxy) {
+ // `electron.net` can only be used after `app.ready` has fired.
+ // The sync timer is scheduled inside `buildApp()` and could in
+ // principle fire before the renderer's `ready` event, so wait
+ // defensively. After ready, `whenReady()` resolves synchronously
+ // and adds no overhead.
+ if (!electron.app.isReady()) {
+ await electron.app.whenReady();
+ }
return electron.net as unknown as Client;
}
return super.getClient(opts);
diff --git a/apps/desktop/src/services/shell.ts b/apps/desktop/src/services/shell.ts
index df1c694ce7..0e702cdc2d 100644
--- a/apps/desktop/src/services/shell.ts
+++ b/apps/desktop/src/services/shell.ts
@@ -273,50 +273,56 @@ export function setupShellHandlers() {
// Defense in depth: validate the path is one the server itself wrote into
// Trilium's tmp dir via /api/.../save-to-tmp-dir, and that it exists.
// Without this, a compromised renderer (e.g. via XSS) could ask us to
- // launch arbitrary local files.
- const resolved = validateOpenCustomPath(filePath, dataDirs.TMP_DIR);
-
- const platform = process.platform;
-
- if (platform === "linux") {
- const terminals = ["x-terminal-emulator", "gnome-terminal", "konsole", "xterm", "xfce4-terminal", "mate-terminal", "rxvt", "terminator", "terminology"];
-
- // The terminal's `-e` argument is reparsed by the terminal's own shell,
- // so the path must be POSIX single-quoted before interpolation.
- const sqQuote = (s: string) => `'${s.replace(/'/g, `'\\''`)}'`;
- const innerCommand = `mimeopen -d ${sqQuote(resolved)}`;
-
- const tryTerminal = (index: number) => {
- if (index >= terminals.length) {
- getLog().error("open-custom: no terminal emulator found");
- electron.shell.openPath(resolved);
- return;
- }
- const terminal = terminals[index];
- execFile(terminal, ["-e", innerCommand], (err) => {
+ // launch arbitrary local files. The try/catch is essential — the
+ // validator throws on hostile input, and an unhandled throw inside an
+ // ipcMain.on handler would crash the main process.
+ try {
+ const resolved = validateOpenCustomPath(filePath, dataDirs.TMP_DIR);
+
+ const platform = process.platform;
+
+ if (platform === "linux") {
+ const terminals = ["x-terminal-emulator", "gnome-terminal", "konsole", "xterm", "xfce4-terminal", "mate-terminal", "rxvt", "terminator", "terminology"];
+
+ // The terminal's `-e` argument is reparsed by the terminal's own shell,
+ // so the path must be POSIX single-quoted before interpolation.
+ const sqQuote = (s: string) => `'${s.replace(/'/g, `'\\''`)}'`;
+ const innerCommand = `mimeopen -d ${sqQuote(resolved)}`;
+
+ const tryTerminal = (index: number) => {
+ if (index >= terminals.length) {
+ getLog().error("open-custom: no terminal emulator found");
+ electron.shell.openPath(resolved);
+ return;
+ }
+ const terminal = terminals[index];
+ execFile(terminal, ["-e", innerCommand], (err) => {
+ if (err) {
+ getLog().info(`open-custom: ${terminal} failed: ${err.message}`);
+ tryTerminal(index + 1);
+ }
+ });
+ };
+ tryTerminal(0);
+ } else if (platform === "win32") {
+ const winPath = resolved.replace(/\//g, "\\");
+ // OpenAs_RunDLL doesn't strip surrounding quotes from its arg, so we
+ // must NOT let Node quote the path on our behalf. windowsVerbatimArguments
+ // is safe here: CreateProcess passes the command line to rundll32 without
+ // any shell interpretation (so `&` is inert), and the path is validated
+ // above to live inside dataDirs.TMP_DIR with a sanitize-filename'd basename
+ // (so it cannot contain quotes or other rundll32-confusing characters).
+ execFile("rundll32.exe", ["shell32.dll,OpenAs_RunDLL", winPath], { windowsVerbatimArguments: true }, (err) => {
if (err) {
- getLog().info(`open-custom: ${terminal} failed: ${err.message}`);
- tryTerminal(index + 1);
+ getLog().error(`open-custom: rundll32 failed: ${err.message}`);
+ electron.shell.openPath(resolved);
}
});
- };
- tryTerminal(0);
- } else if (platform === "win32") {
- const winPath = resolved.replace(/\//g, "\\");
- // OpenAs_RunDLL doesn't strip surrounding quotes from its arg, so we
- // must NOT let Node quote the path on our behalf. windowsVerbatimArguments
- // is safe here: CreateProcess passes the command line to rundll32 without
- // any shell interpretation (so `&` is inert), and the path is validated
- // above to live inside dataDirs.TMP_DIR with a sanitize-filename'd basename
- // (so it cannot contain quotes or other rundll32-confusing characters).
- execFile("rundll32.exe", ["shell32.dll,OpenAs_RunDLL", winPath], { windowsVerbatimArguments: true }, (err) => {
- if (err) {
- getLog().error(`open-custom: rundll32 failed: ${err.message}`);
- electron.shell.openPath(resolved);
- }
- });
- } else {
- electron.shell.openPath(resolved);
+ } else {
+ electron.shell.openPath(resolved);
+ }
+ } catch (e) {
+ getLog().error(`open-custom failed: ${coreUtils.safeExtractMessageAndStackFromError(e)}`);
}
});
}
diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.ts
index df5c3a39c1..2107f7dd76 100644
--- a/apps/desktop/src/services/window.ts
+++ b/apps/desktop/src/services/window.ts
@@ -169,7 +169,9 @@ async function configureWebContents(webContents: WebContents, spellcheckEnabled:
(await import("electron")).shell.openExternal(details.url);
}
- openExternal();
+ openExternal().catch(err => {
+ getLog().error(`Failed to open external URL ${details.url}: ${err}`);
+ });
return { action: "deny" };
});
@@ -228,7 +230,8 @@ function setupSpellcheckForSession(session: Session) {
const languageCodes = optionService
.getOption("spellCheckLanguageCode")
.split(",")
- .map((code) => code.trim());
+ .map((code) => code.trim())
+ .filter(Boolean);
session.setSpellCheckerLanguages(languageCodes);
}
@@ -370,12 +373,12 @@ export function setupWindowing() {
try {
const image = electron.nativeImage.createFromBuffer(Buffer.from(buffer));
if (image.isEmpty()) {
- console.error("copy-image-to-clipboard: nativeImage is empty, unsupported format?");
+ getLog().error("copy-image-to-clipboard: nativeImage is empty, unsupported format?");
return;
}
electron.clipboard.writeImage(image);
} catch (e) {
- console.error("copy-image-to-clipboard failed:", e);
+ getLog().error(`copy-image-to-clipboard failed: ${coreUtils.safeExtractMessageAndStackFromError(e)}`);
}
});