Skip to content

Build your personal knowledge base with Trilium Notes

feat(script api): add isBackendScriptingEnabled/isSqlConsoleEnabled detection

Elian Doranc1bff273ff19
diff --git a/apps/client/src/services/frontend_script_api.spec.ts b/apps/client/src/services/frontend_script_api.spec.ts
index 58d31a7829..7fbe95404c 100644
--- a/apps/client/src/services/frontend_script_api.spec.ts
+++ b/apps/client/src/services/frontend_script_api.spec.ts
@@ -10,6 +10,7 @@ import { preactAPI } from "./frontend_script_api_preact.js";
import froca from "./froca.js";
import linkService from "./link.js";
import noteTooltipService from "./note_tooltip.js";
+import options from "./options.js";
import protectedSessionService from "./protected_session.js";
import searchService from "./search.js";
import server from "./server.js";
@@ -285,6 +286,30 @@ describe("runAsyncOnBackendWithManualTransactionHandling", () => {
});
});
+describe("scripting availability detection", () => {
+ it("isBackendScriptingEnabled reflects the backendScriptingEnabled option", () => {
+ const is = vi.spyOn(options, "is").mockImplementation((key) => key === "backendScriptingEnabled");
+ const api = makeApi();
+
+ expect(api.isBackendScriptingEnabled()).toBe(true);
+ expect(is).toHaveBeenCalledWith("backendScriptingEnabled");
+
+ is.mockReturnValue(false);
+ expect(api.isBackendScriptingEnabled()).toBe(false);
+ });
+
+ it("isSqlConsoleEnabled reflects the sqlConsoleEnabled option", () => {
+ const is = vi.spyOn(options, "is").mockImplementation((key) => key === "sqlConsoleEnabled");
+ const api = makeApi();
+
+ expect(api.isSqlConsoleEnabled()).toBe(true);
+ expect(is).toHaveBeenCalledWith("sqlConsoleEnabled");
+
+ is.mockReturnValue(false);
+ expect(api.isSqlConsoleEnabled()).toBe(false);
+ });
+});
+
describe("search", () => {
it("searchForNotes delegates to the search service", async () => {
const note = buildNote({ title: "Hit" });
diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.ts
index 4ac424215f..eff08fb06b 100644
--- a/apps/client/src/services/frontend_script_api.ts
+++ b/apps/client/src/services/frontend_script_api.ts
@@ -16,6 +16,7 @@ import { preactAPI } from "./frontend_script_api_preact.js";
import { t } from "./i18n.js";
import linkService from "./link.js";
import noteTooltipService from "./note_tooltip.js";
+import options from "./options.js";
import protectedSessionService from "./protected_session.js";
import searchService from "./search.js";
import server from "./server.js";
@@ -182,6 +183,23 @@ export interface Api {
*/
runAsyncOnBackendWithManualTransactionHandling(func: Func, params: unknown[]): unknown;
+ /**
+ * Whether backend script execution is enabled on the server (the
+ * `[Security] backendScriptingEnabled` config toggle). When it's disabled,
+ * `api.runOnBackend()` / `api.runAsyncOnBackendWithManualTransactionHandling()`
+ * reject with a "Backend script execution is disabled" error, so check this
+ * first to let a script degrade gracefully instead of throwing.
+ */
+ isBackendScriptingEnabled(): boolean;
+
+ /**
+ * Whether the SQL console is enabled on the server (the
+ * `[Security] sqlConsoleEnabled` config toggle). When it's disabled, backend
+ * scripts that run raw SQL (`api.sql.*`) fail, so check this before invoking
+ * SQL-backed logic via `api.runOnBackend()`.
+ */
+ isSqlConsoleEnabled(): boolean;
+
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See full documentation for all options at: https://triliumnext.github.io/Docs/Wiki/search.html
@@ -619,6 +637,9 @@ function FrontendScriptApi(this: Api, startNote: FNote, currentNote: FNote, orig
return await this.__runOnBackendInner(func, params, false);
};
+ this.isBackendScriptingEnabled = () => options.is("backendScriptingEnabled");
+ this.isSqlConsoleEnabled = () => options.is("sqlConsoleEnabled");
+
this.searchForNotes = async (searchString) => {
return await searchService.searchForNotes(searchString);
};
diff --git a/packages/commons/src/lib/script_api.ts b/packages/commons/src/lib/script_api.ts
index 9d3ca81aaa..02b8d6d181 100644
--- a/packages/commons/src/lib/script_api.ts
+++ b/packages/commons/src/lib/script_api.ts
@@ -397,6 +397,22 @@ export interface FrontendApi {
*/
runAsyncOnBackendWithManualTransactionHandling(func: Func, params?: unknown[]): Promise<unknown>;
+ /**
+ * Whether backend script execution is enabled on the server (the
+ * `[Security] backendScriptingEnabled` config toggle). When it's disabled,
+ * `api.runOnBackend()` / `api.runAsyncOnBackendWithManualTransactionHandling()`
+ * reject with a "Backend script execution is disabled" error, so check this
+ * first to let a script degrade gracefully instead of throwing.
+ */
+ isBackendScriptingEnabled(): boolean;
+ /**
+ * Whether the SQL console is enabled on the server (the
+ * `[Security] sqlConsoleEnabled` config toggle). When it's disabled, backend
+ * scripts that run raw SQL (`api.sql.*`) fail, so check this before invoking
+ * SQL-backed logic via `api.runOnBackend()`.
+ */
+ isSqlConsoleEnabled(): boolean;
+
/**
* This is a powerful search method - you can search by attributes and their values, e.g.:
* "#dateModified =* MONTH AND #log". See full documentation for all options at: https://triliumnext.github.io/Docs/Wiki/search.html