Skip to content

Build your personal knowledge base with Trilium Notes

test(core): broken tests after backend killswitch

Elian Doran0965e16f2b5a
diff --git a/apps/server/src/routes/custom.spec.ts b/apps/server/src/routes/custom.spec.ts
index 7456dc21a0..3af8d304fe 100644
--- a/apps/server/src/routes/custom.spec.ts
+++ b/apps/server/src/routes/custom.spec.ts
@@ -1,12 +1,24 @@
-import { cls, note_service as noteService } from "@triliumnext/core";
+import { cls, getConfig, note_service as noteService } from "@triliumnext/core";
import type { Application } from "express";
import supertest from "supertest";
-import { beforeAll, describe, expect, it } from "vitest";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+
+import config from "../services/config.js";
let app: Application;
describe("Custom request/resource handlers", () => {
+ // Custom request handlers run backend scripts, gated by the backendScriptingEnabled toggle at
+ // two layers: the /custom route checks the server config, while execution (executeBundle) checks
+ // core's config. The test setup never injects the server config into core, so they are distinct
+ // objects and both must be enabled.
+ const coreConfig = getConfig();
+ const originalServerScripting = config.Security.backendScriptingEnabled;
+ const originalCoreScripting = coreConfig.Security.backendScriptingEnabled;
+
beforeAll(async () => {
+ config.Security.backendScriptingEnabled = true;
+ coreConfig.Security.backendScriptingEnabled = true;
app = await (await import("../app.js")).default();
cls.init(() => {
@@ -19,6 +31,8 @@ describe("Custom request/resource handlers", () => {
content: `api.res.status(200).send("handled:" + api.pathParams[0]);`
}).note;
handler.setLabel("customRequestHandler", "greet/([a-z]+)");
+ // Public so the unauthenticated supertest request isn't rejected with 401.
+ handler.setLabel("customRequestHandlerPublic");
// A script note that throws, to exercise the error branch.
const thrower = noteService.createNewNote({
@@ -29,6 +43,7 @@ describe("Custom request/resource handlers", () => {
content: `throw new Error("boom in handler");`
}).note;
thrower.setLabel("customRequestHandler", "explode");
+ thrower.setLabel("customRequestHandlerPublic");
// A resource note served directly.
const resource = noteService.createNewNote({
@@ -48,6 +63,11 @@ describe("Custom request/resource handlers", () => {
});
});
+ afterAll(() => {
+ config.Security.backendScriptingEnabled = originalServerScripting;
+ coreConfig.Security.backendScriptingEnabled = originalCoreScripting;
+ });
+
it("runs a custom request handler with captured path params", async () => {
const res = await supertest(app).get("/custom/greet/world").expect(200);
expect(res.text).toBe("handled:world");
diff --git a/packages/trilium-core/src/routes/api/script.spec.ts b/packages/trilium-core/src/routes/api/script.spec.ts
index 8bdb500110..dccdf28075 100644
--- a/packages/trilium-core/src/routes/api/script.spec.ts
+++ b/packages/trilium-core/src/routes/api/script.spec.ts
@@ -1,6 +1,7 @@
-import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import attributeService from "../../services/attributes";
+import config from "../../services/config";
import scriptService from "../../services/script";
import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
@@ -22,10 +23,18 @@ async function createCodeNote(content: string): Promise<string> {
}
describe("Script API (core)", () => {
+ // Script execution enforces the backendScriptingEnabled security toggle (default false).
+ const originalScriptingEnabled = config.Security.backendScriptingEnabled;
+
beforeAll(() => {
+ config.Security.backendScriptingEnabled = true;
api = CoreApiTester.build();
});
+ afterAll(() => {
+ config.Security.backendScriptingEnabled = originalScriptingEnabled;
+ });
+
afterEach(() => {
vi.restoreAllMocks();
});
diff --git a/packages/trilium-core/src/routes/api/sql.spec.ts b/packages/trilium-core/src/routes/api/sql.spec.ts
index 5d5553b9e8..df07251bcc 100644
--- a/packages/trilium-core/src/routes/api/sql.spec.ts
+++ b/packages/trilium-core/src/routes/api/sql.spec.ts
@@ -1,6 +1,7 @@
-import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
+import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import becca from "../../becca/becca";
+import config from "../../services/config";
import { getSql } from "../../services/sql/index";
import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
@@ -23,10 +24,18 @@ interface ExecuteResult {
}
describe("SQL API (core)", () => {
+ // The SQL routes are gated behind the sqlConsoleEnabled security toggle (default false).
+ const originalSqlConsoleEnabled = config.Security.sqlConsoleEnabled;
+
beforeAll(() => {
+ config.Security.sqlConsoleEnabled = true;
api = CoreApiTester.build();
});
+ afterAll(() => {
+ config.Security.sqlConsoleEnabled = originalSqlConsoleEnabled;
+ });
+
describe("schema", () => {
it("returns the database table schema", async () => {
const res = await api.get<SchemaTable[]>("/api/sql/schema");
diff --git a/packages/trilium-core/src/services/bulk_actions.spec.ts b/packages/trilium-core/src/services/bulk_actions.spec.ts
index b40494a5ac..f6d0824541 100644
--- a/packages/trilium-core/src/services/bulk_actions.spec.ts
+++ b/packages/trilium-core/src/services/bulk_actions.spec.ts
@@ -1,11 +1,12 @@
import type { BulkAction } from "@triliumnext/commons";
-import { describe, expect, it } from "vitest";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
import becca from "../becca/becca.js";
import type BBranch from "../becca/entities/bbranch.js";
import type BNote from "../becca/entities/bnote.js";
import bulkActionService from "./bulk_actions.js";
import cloningService from "./cloning.js";
+import config from "./config.js";
import { getContext } from "./context.js";
import noteService from "./notes.js";
@@ -37,6 +38,17 @@ function createNote(parentNoteId: string): { note: BNote; branch: BBranch } {
}
describe("bulk_actions service (real DB)", () => {
+ // The executeScript action runs a backend script, gated by the backendScriptingEnabled toggle.
+ const originalScriptingEnabled = config.Security.backendScriptingEnabled;
+
+ beforeAll(() => {
+ config.Security.backendScriptingEnabled = true;
+ });
+
+ afterAll(() => {
+ config.Security.backendScriptingEnabled = originalScriptingEnabled;
+ });
+
describe("executeActions", () => {
it("skips note IDs that don't resolve to a note", () => {
// No note exists for this id, so nothing should throw and no handler runs.
diff --git a/packages/trilium-core/src/services/script.spec.ts b/packages/trilium-core/src/services/script.spec.ts
index a14fc10dc9..2779f0514e 100644
--- a/packages/trilium-core/src/services/script.spec.ts
+++ b/packages/trilium-core/src/services/script.spec.ts
@@ -1,13 +1,25 @@
import { trimIndentation } from "@triliumnext/commons";
-import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import becca from "../becca/becca.js";
import { buildNote } from "../test/becca_easy_mocking.js";
+import config from "./config.js";
import { getContext } from "./context.js";
import scriptService, { buildJsx, executeBundle, getScriptBundle } from "./script.js";
import ws from "./ws.js";
describe("Script", () => {
+ // executeBundle enforces the backendScriptingEnabled security toggle (default false).
+ const originalScriptingEnabled = config.Security.backendScriptingEnabled;
+
+ beforeAll(() => {
+ config.Security.backendScriptingEnabled = true;
+ });
+
+ afterAll(() => {
+ config.Security.backendScriptingEnabled = originalScriptingEnabled;
+ });
+
beforeEach(() => {
becca.reset();
diff --git a/packages/trilium-core/src/services/script_context.spec.ts b/packages/trilium-core/src/services/script_context.spec.ts
index f79479a9a0..512a73ec10 100644
--- a/packages/trilium-core/src/services/script_context.spec.ts
+++ b/packages/trilium-core/src/services/script_context.spec.ts
@@ -1,11 +1,23 @@
-import { beforeEach, describe, expect, it } from "vitest";
+import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
import becca from "../becca/becca.js";
import { buildNote } from "../test/becca_easy_mocking.js";
import BackendScriptApi from "./backend_script_api.js";
+import config from "./config.js";
import ScriptContext from "./script_context.js";
describe("ScriptContext", () => {
+ // One test executes a bundle, which enforces the backendScriptingEnabled toggle (default false).
+ const originalScriptingEnabled = config.Security.backendScriptingEnabled;
+
+ beforeAll(() => {
+ config.Security.backendScriptingEnabled = true;
+ });
+
+ afterAll(() => {
+ config.Security.backendScriptingEnabled = originalScriptingEnabled;
+ });
+
beforeEach(() => {
becca.reset();
});
@@ -66,9 +78,9 @@ describe("ScriptContext", () => {
const moduleNote = buildNote({ title: "myModule" });
const ctx = new ScriptContext([moduleNote], { startNote: moduleNote });
- // "path" is not a Trilium module note, so it must resolve via Node's require.
- const path = ctx.require([moduleNote.noteId])("path");
- expect(typeof (path as { join?: unknown }).join).toBe("function");
+ // "lodash" is not a Trilium module note but is whitelisted, so it resolves via Node's require.
+ const lodash = ctx.require([moduleNote.noteId])("lodash");
+ expect(typeof (lodash as { join?: unknown }).join).toBe("function");
});
it("require() throws when falling back to a non-existent native module", () => {