Skip to content

Build your personal knowledge base with Trilium Notes

feat(desktop): toggleable security flags

Elian Doran8a771378f94f
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 0e4ea08bc7..c2a163d95d 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1580,7 +1580,8 @@
"sql_console_description": "Allow execution of queries in the SQL console.",
"how_to_enable": "How to enable",
"server_config_hint": "Add the following to your config.ini file:",
- "server_env_hint": "Or set the following environment variable:"
+ "server_env_hint": "Or set the following environment variable:",
+ "restart_required": "Restart required for this change to take effect."
},
"password": {
"heading": "Password",
diff --git a/apps/client/src/widgets/type_widgets/options/security.tsx b/apps/client/src/widgets/type_widgets/options/security.tsx
index 85e7ce5d44..d3b0fc09de 100644
--- a/apps/client/src/widgets/type_widgets/options/security.tsx
+++ b/apps/client/src/widgets/type_widgets/options/security.tsx
@@ -1,4 +1,7 @@
+import { useState } from "preact/hooks";
+
import { t } from "../../../services/i18n";
+import toast from "../../../services/toast";
import { isElectron } from "../../../services/utils";
import Collapsible from "../../react/Collapsible";
import { useTriliumOptionBool } from "../../react/hooks";
@@ -30,9 +33,17 @@ function ServerConfigHint({ configKey, envVar }: { configKey: string; envVar: st
}
function BackendScriptingSettings() {
- // Read-only for now — the actual toggle mechanism (IPC + native
- // dialog on desktop, config.ini on server) will be implemented separately.
const [backendScriptingEnabled] = useTriliumOptionBool("backendScriptingEnabled");
+ const [pendingRestart, setPendingRestart] = useState(false);
+ const isDesktop = isElectron();
+
+ async function handleToggle(enabled: boolean) {
+ const confirmed = await window.electronApi!.security.setBackendScriptingEnabled(enabled);
+ if (confirmed) {
+ setPendingRestart(true);
+ toast.showMessage(t("security.restart_required"));
+ }
+ }
return (
<OptionsSection
@@ -43,10 +54,12 @@ function BackendScriptingSettings() {
<OptionsRowWithToggle
name="backend-scripting-enabled"
label={t("security.backend_scripting_label")}
- description={t("security.backend_scripting_description")}
- currentValue={backendScriptingEnabled}
- onChange={() => {}}
- disabled={true}
+ description={pendingRestart
+ ? t("security.restart_required")
+ : t("security.backend_scripting_description")}
+ currentValue={pendingRestart ? !backendScriptingEnabled : backendScriptingEnabled}
+ onChange={handleToggle}
+ disabled={!isDesktop || pendingRestart}
/>
<ServerConfigHint
configKey="backendScriptingEnabled"
@@ -58,6 +71,16 @@ function BackendScriptingSettings() {
function SqlConsoleSettings() {
const [sqlConsoleEnabled] = useTriliumOptionBool("sqlConsoleEnabled");
+ const [pendingRestart, setPendingRestart] = useState(false);
+ const isDesktop = isElectron();
+
+ async function handleToggle(enabled: boolean) {
+ const confirmed = await window.electronApi!.security.setSqlConsoleEnabled(enabled);
+ if (confirmed) {
+ setPendingRestart(true);
+ toast.showMessage(t("security.restart_required"));
+ }
+ }
return (
<OptionsSection
@@ -68,10 +91,12 @@ function SqlConsoleSettings() {
<OptionsRowWithToggle
name="sql-console-enabled"
label={t("security.sql_console_label")}
- description={t("security.sql_console_description")}
- currentValue={sqlConsoleEnabled}
- onChange={() => {}}
- disabled={true}
+ description={pendingRestart
+ ? t("security.restart_required")
+ : t("security.sql_console_description")}
+ currentValue={pendingRestart ? !sqlConsoleEnabled : sqlConsoleEnabled}
+ onChange={handleToggle}
+ disabled={!isDesktop || pendingRestart}
/>
<ServerConfigHint
configKey="sqlConsoleEnabled"
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 6ed338404d..4d4cbe65be 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -26,6 +26,7 @@ import DesktopPlatformProvider from "./platform_provider";
import { registerTriliumAppScheme, setupTriliumAppProtocol } from "./protocol";
import { setupCustomDictionary } from "./services/custom_dictionary";
import { setupPrintingHandlers } from "./services/printing";
+import { getSecuritySettings, registerSecurityIpcHandlers } from "./services/security_settings";
import { setupShellHandlers } from "./services/shell";
import { setupSystemTray } from "./services/tray";
@@ -105,6 +106,7 @@ async function main() {
setupCustomDictionary();
setupShellHandlers();
setupPrintingHandlers();
+ registerSecurityIpcHandlers();
app.on("will-quit", () => {
globalShortcut.unregisterAll();
@@ -137,6 +139,15 @@ async function main() {
const { DOCUMENT_PATH } = (await import("@triliumnext/server/src/services/data_dir.js")).default;
const config = (await import("@triliumnext/server/src/services/config.js")).default;
+ // Override scripting config from security.json (lives outside the DB for tamper resistance)
+ const securitySettings = getSecuritySettings();
+ if (securitySettings.backendScriptingEnabled !== undefined) {
+ config.Scripting.backendScriptingEnabled = securitySettings.backendScriptingEnabled;
+ }
+ if (securitySettings.sqlConsoleEnabled !== undefined) {
+ config.Scripting.sqlConsoleEnabled = securitySettings.sqlConsoleEnabled;
+ }
+
const dbProvider = new BetterSqlite3Provider();
dbProvider.loadFromFile(DOCUMENT_PATH, config.General.readOnly);
diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts
index 612cf24328..4b75196b10 100644
--- a/apps/desktop/src/preload.ts
+++ b/apps/desktop/src/preload.ts
@@ -228,5 +228,14 @@ contextBridge.exposeInMainWorld("electronApi", {
ipcRenderer.removeAllListeners("did-navigate");
ipcRenderer.removeAllListeners("did-navigate-in-page");
}
+ },
+
+ security: {
+ setBackendScriptingEnabled(enabled: boolean): Promise<boolean> {
+ return ipcRenderer.invoke("security-set-backend-scripting", enabled);
+ },
+ setSqlConsoleEnabled(enabled: boolean): Promise<boolean> {
+ return ipcRenderer.invoke("security-set-sql-console", enabled);
+ }
}
} satisfies ElectronApi);
diff --git a/apps/desktop/src/services/security_settings.ts b/apps/desktop/src/services/security_settings.ts
new file mode 100644
index 0000000000..b7820d74eb
--- /dev/null
+++ b/apps/desktop/src/services/security_settings.ts
@@ -0,0 +1,78 @@
+import electron from "electron";
+import fs from "fs";
+import path from "path";
+
+import dataDirs from "@triliumnext/server/src/services/data_dir.js";
+
+const SECURITY_JSON_PATH = path.join(dataDirs.TRILIUM_DATA_DIR, "security.json");
+
+interface SecuritySettings {
+ backendScriptingEnabled?: boolean;
+ sqlConsoleEnabled?: boolean;
+}
+
+function readSettings(): SecuritySettings {
+ try {
+ if (fs.existsSync(SECURITY_JSON_PATH)) {
+ return JSON.parse(fs.readFileSync(SECURITY_JSON_PATH, "utf-8"));
+ }
+ } catch {
+ // Corrupted or unreadable — treat as defaults
+ }
+ return {};
+}
+
+function writeSettings(settings: SecuritySettings): void {
+ fs.writeFileSync(SECURITY_JSON_PATH, JSON.stringify(settings, null, 2), "utf-8");
+}
+
+/**
+ * Reads security settings from `data_dir/security.json`.
+ * Called at startup to inject into the server config.
+ */
+export function getSecuritySettings(): SecuritySettings {
+ return readSettings();
+}
+
+async function confirmToggle(settingLabel: string, enabled: boolean): Promise<boolean> {
+ const message = enabled
+ ? `Are you sure you want to enable ${settingLabel}?\n\nThis change requires a restart to take effect.`
+ : `${settingLabel} will be disabled.\n\nThis change requires a restart to take effect.`;
+
+ const result = await electron.dialog.showMessageBox({
+ type: enabled ? "warning" : "question",
+ buttons: ["Cancel", enabled ? "Enable" : "Disable"],
+ defaultId: 0,
+ cancelId: 0,
+ title: `${enabled ? "Enable" : "Disable"} ${settingLabel}`,
+ message
+ });
+
+ return result.response === 1;
+}
+
+export function registerSecurityIpcHandlers(): void {
+ electron.ipcMain.handle("security-set-backend-scripting", async (_event, enabled: boolean) => {
+ const confirmed = await confirmToggle("backend script execution", enabled);
+ if (!confirmed) {
+ return false;
+ }
+
+ const settings = readSettings();
+ settings.backendScriptingEnabled = enabled;
+ writeSettings(settings);
+ return true;
+ });
+
+ electron.ipcMain.handle("security-set-sql-console", async (_event, enabled: boolean) => {
+ const confirmed = await confirmToggle("SQL console", enabled);
+ if (!confirmed) {
+ return false;
+ }
+
+ const settings = readSettings();
+ settings.sqlConsoleEnabled = enabled;
+ writeSettings(settings);
+ return true;
+ });
+}
diff --git a/packages/commons/src/lib/electron_api_interface.ts b/packages/commons/src/lib/electron_api_interface.ts
index b9a886f01e..76a87cc7d1 100644
--- a/packages/commons/src/lib/electron_api_interface.ts
+++ b/packages/commons/src/lib/electron_api_interface.ts
@@ -412,6 +412,23 @@ export interface ElectronNavigationApi {
removeDidNavigateListeners(): void;
}
+/**
+ * Security settings that live outside the database (in `data_dir/security.json`)
+ * to prevent malicious scripts from modifying them. Changes require a native
+ * OS confirmation dialog and an app restart to take effect.
+ */
+export interface ElectronSecurityApi {
+ /**
+ * Requests a change to a security setting. Shows a native OS confirmation
+ * dialog before writing. Returns `true` if the user confirmed and the
+ * change was written (restart required to take effect), `false` if cancelled.
+ */
+ setBackendScriptingEnabled(enabled: boolean): Promise<boolean>;
+
+ /** Requests a change to the SQL console setting. Same flow as above. */
+ setSqlConsoleEnabled(enabled: boolean): Promise<boolean>;
+}
+
/**
* The complete surface exposed to the renderer as `window.electronApi` via
* `contextBridge`. The renderer must access Electron-only functionality through
@@ -442,4 +459,6 @@ export interface ElectronApi {
navigation: ElectronNavigationApi;
/** In-process bridge that replaces the renderer↔server WebSocket. */
ws: ElectronWsApi;
+ /** Security settings (backend scripting, SQL console) stored outside the DB. */
+ security: ElectronSecurityApi;
}