Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(options/security): improve message boxes
Elian Doran5430df4ca0a5
diff --git a/apps/client/src/widgets/type_widgets/options/security.tsx b/apps/client/src/widgets/type_widgets/options/security.tsxindex 0292834cf6..2eb82b8631 100644--- a/apps/client/src/widgets/type_widgets/options/security.tsx+++ b/apps/client/src/widgets/type_widgets/options/security.tsx@@ -1,7 +1,6 @@import { useState } from "preact/hooks";import { t } from "../../../services/i18n";-import toast from "../../../services/toast";import { isElectron, restartDesktopApp } from "../../../services/utils";import Button from "../../react/Button";import Collapsible from "../../react/Collapsible";@@ -10,13 +9,31 @@ import OptionsRow, { OptionsRowWithToggle } from "./components/OptionsRow";import OptionsSection from "./components/OptionsSection";export default function SecuritySettings() {- const [pendingRestart, setPendingRestart] = useState(false);+ // Local state tracks what's been written to security.json (pending restart).+ // null = no change made yet, use the live config value.+ const [pendingBackendScripting, setPendingBackendScripting] = useState<boolean | null>(null);+ const [pendingSqlConsole, setPendingSqlConsole] = useState<boolean | null>(null);++ const [liveBackendScripting] = useTriliumOptionBool("backendScriptingEnabled");+ const [liveSqlConsole] = useTriliumOptionBool("sqlConsoleEnabled");++ const hasPendingChanges =+ (pendingBackendScripting !== null && pendingBackendScripting !== liveBackendScripting) ||+ (pendingSqlConsole !== null && pendingSqlConsole !== liveSqlConsole);return (<>- <BackendScriptingSettings pendingRestart={pendingRestart} setPendingRestart={setPendingRestart} />- <SqlConsoleSettings pendingRestart={pendingRestart} setPendingRestart={setPendingRestart} />- {pendingRestart && isElectron() && (+ <BackendScriptingSettings+ liveValue={liveBackendScripting}+ pendingValue={pendingBackendScripting}+ setPendingValue={setPendingBackendScripting}+ />+ <SqlConsoleSettings+ liveValue={liveSqlConsole}+ pendingValue={pendingSqlConsole}+ setPendingValue={setPendingSqlConsole}+ />+ {hasPendingChanges && isElectron() && (<OptionsSection noCard><OptionsRow name="restart" centered><Button@@ -49,19 +66,21 @@ function ServerConfigHint({ configKey, envVar }: { configKey: string; envVar: st}interface ToggleSectionProps {- pendingRestart: boolean;- setPendingRestart: (value: boolean) => void;+ liveValue: boolean;+ pendingValue: boolean | null;+ setPendingValue: (value: boolean | null) => void;}-function BackendScriptingSettings({ pendingRestart, setPendingRestart }: ToggleSectionProps) {- const [backendScriptingEnabled] = useTriliumOptionBool("backendScriptingEnabled");+function BackendScriptingSettings({ liveValue, pendingValue, setPendingValue }: ToggleSectionProps) {const isDesktop = isElectron();+ const displayValue = pendingValue ?? liveValue;+ const hasPendingChange = pendingValue !== null && pendingValue !== liveValue;async function handleToggle(enabled: boolean) {const confirmed = await window.electronApi!.security.setBackendScriptingEnabled(enabled);if (confirmed) {- setPendingRestart(true);- toast.showMessage(t("security.restart_required"));+ // If toggling back to the live value, clear pending state+ setPendingValue(enabled === liveValue ? null : enabled);}}@@ -74,12 +93,12 @@ function BackendScriptingSettings({ pendingRestart, setPendingRestart }: ToggleS<OptionsRowWithTogglename="backend-scripting-enabled"label={t("security.backend_scripting_label")}- description={pendingRestart+ description={hasPendingChange? t("security.restart_required"): t("security.backend_scripting_description")}- currentValue={pendingRestart ? !backendScriptingEnabled : backendScriptingEnabled}+ currentValue={displayValue}onChange={handleToggle}- disabled={!isDesktop || pendingRestart}+ disabled={!isDesktop}/><ServerConfigHintconfigKey="backendScriptingEnabled"@@ -89,15 +108,15 @@ function BackendScriptingSettings({ pendingRestart, setPendingRestart }: ToggleS);}-function SqlConsoleSettings({ pendingRestart, setPendingRestart }: ToggleSectionProps) {- const [sqlConsoleEnabled] = useTriliumOptionBool("sqlConsoleEnabled");+function SqlConsoleSettings({ liveValue, pendingValue, setPendingValue }: ToggleSectionProps) {const isDesktop = isElectron();+ const displayValue = pendingValue ?? liveValue;+ const hasPendingChange = pendingValue !== null && pendingValue !== liveValue;async function handleToggle(enabled: boolean) {const confirmed = await window.electronApi!.security.setSqlConsoleEnabled(enabled);if (confirmed) {- setPendingRestart(true);- toast.showMessage(t("security.restart_required"));+ setPendingValue(enabled === liveValue ? null : enabled);}}@@ -110,12 +129,12 @@ function SqlConsoleSettings({ pendingRestart, setPendingRestart }: ToggleSection<OptionsRowWithTogglename="sql-console-enabled"label={t("security.sql_console_label")}- description={pendingRestart+ description={hasPendingChange? t("security.restart_required"): t("security.sql_console_description")}- currentValue={pendingRestart ? !sqlConsoleEnabled : sqlConsoleEnabled}+ currentValue={displayValue}onChange={handleToggle}- disabled={!isDesktop || pendingRestart}+ disabled={!isDesktop}/><ServerConfigHintconfigKey="sqlConsoleEnabled"diff --git a/apps/desktop/src/services/security_settings.ts b/apps/desktop/src/services/security_settings.tsindex b7820d74eb..1ee9c5e917 100644--- a/apps/desktop/src/services/security_settings.ts+++ b/apps/desktop/src/services/security_settings.ts@@ -34,18 +34,29 @@ 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.`;-+async function confirmEnable(settingLabel: string, warning: string): Promise<boolean> {const result = await electron.dialog.showMessageBox({- type: enabled ? "warning" : "question",- buttons: ["Cancel", enabled ? "Enable" : "Disable"],+ type: "warning",+ buttons: ["Cancel", "Enable"],defaultId: 0,cancelId: 0,- title: `${enabled ? "Enable" : "Disable"} ${settingLabel}`,- message+ title: `Enable ${settingLabel}`,+ message: `Are you sure you want to enable ${settingLabel}?`,+ detail: `${warning}\n\nOnly enable this if you explicitly intend to use this feature. Do not enable it if prompted by a script or an unfamiliar note.\n\nThis change requires a restart to take effect.`+ });++ return result.response === 1;+}++async function confirmDisable(settingLabel: string): Promise<boolean> {+ const result = await electron.dialog.showMessageBox({+ type: "info",+ buttons: ["Cancel", "Disable"],+ defaultId: 1,+ cancelId: 0,+ title: `Disable ${settingLabel}`,+ message: `${settingLabel} will be disabled.`,+ detail: "This change requires a restart to take effect."});return result.response === 1;@@ -53,7 +64,10 @@ async function confirmToggle(settingLabel: string, enabled: boolean): Promise<boexport function registerSecurityIpcHandlers(): void {electron.ipcMain.handle("security-set-backend-scripting", async (_event, enabled: boolean) => {- const confirmed = await confirmToggle("backend script execution", enabled);+ const confirmed = enabled+ ? await confirmEnable("Backend script execution",+ "Backend scripts have full access to the server, including the file system and network.")+ : await confirmDisable("Backend script execution");if (!confirmed) {return false;}@@ -65,7 +79,10 @@ export function registerSecurityIpcHandlers(): void {});electron.ipcMain.handle("security-set-sql-console", async (_event, enabled: boolean) => {- const confirmed = await confirmToggle("SQL console", enabled);+ const confirmed = enabled+ ? await confirmEnable("SQL console",+ "The SQL console allows executing arbitrary SQL queries against the database.")+ : await confirmDisable("SQL console");if (!confirmed) {return false;}