Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): let browsers log in to a desktop instance (#10589)

Elian Doran93ff08acd5b7
diff --git a/apps/server/src/routes/index.spec.ts b/apps/server/src/routes/index.spec.ts
new file mode 100644
index 0000000000..2d41cc54dc
--- /dev/null
+++ b/apps/server/src/routes/index.spec.ts
@@ -0,0 +1,86 @@
+import type { Request, Response } from "express";
+import { describe, expect, it, vi } from "vitest";
+
+import { markAsInternalElectronRequest } from "../services/electron_request.js";
+
+// Force the process-wide desktop-build flag ON so these tests reproduce the
+// #10589 setup: a browser hitting the desktop app's HTTP listener. The gating
+// must key off the per-request trusted-renderer marker, NOT this flag — that's
+// exactly what the assertions below verify.
+vi.mock("../services/utils.js", async (orig) => {
+ const actual = await orig<typeof import("../services/utils.js")>();
+ return { ...actual, isElectron: true, isDev: true, isMac: false, isWindows11: false };
+});
+
+vi.mock("../services/config.js", () => ({ default: { General: {}, Network: {} } }));
+vi.mock("../services/port.js", () => ({ default: 8080 }));
+vi.mock("../services/app_path.js", () => ({ default: "app" }));
+vi.mock("../services/asset_path.js", () => ({ default: "assets" }));
+vi.mock("./csrf_protection.js", () => ({ generateCsrfToken: () => "csrf-test-token" }));
+vi.mock("../services/open_id.js", () => ({
+ default: { isOpenIDEnabled: () => false, getSSOIssuerName: () => "", getSSOIssuerIcon: () => "" }
+}));
+vi.mock("../services/totp.js", () => ({ default: { isTotpEnabled: () => false } }));
+
+vi.mock("@triliumnext/core", () => ({
+ attributes: {},
+ BNote: class {},
+ getSharedBootstrapItems: () => ({}),
+ icon_packs: { getIconPacks: () => [], generateCss: () => "", MIME_TO_EXTENSION_MAPPINGS: {} },
+ options: { getOptionMap: () => ({}) },
+ password: { isPasswordSet: () => true },
+ sql_init: { isDbInitialized: () => true },
+ task_states: { generateTaskStateCss: () => "" },
+ getLog: () => ({ info: () => {}, error: () => {} })
+}));
+
+const { bootstrap } = await import("./index.js");
+
+function makeReq(overrides: { loggedIn?: boolean; internal?: boolean } = {}): Request {
+ const req = {
+ session: { loggedIn: overrides.loggedIn ?? false },
+ query: {},
+ headers: {},
+ cookies: {}
+ } as unknown as Request;
+ if (overrides.internal) {
+ markAsInternalElectronRequest(req);
+ }
+ return req;
+}
+
+function makeRes(): { res: Response; body: () => any } {
+ let captured: any;
+ const res = { send: (payload: any) => { captured = payload; } } as unknown as Response;
+ return { res, body: () => captured };
+}
+
+describe("bootstrap auth gating (desktop build, #10589)", () => {
+ it("serves the login screen to an untrusted (browser) request even on a desktop build", () => {
+ const { res, body } = makeRes();
+ bootstrap(makeReq({ loggedIn: false, internal: false }), res);
+
+ // A browser hitting the desktop's HTTP listener must get the login screen,
+ // not a spurious loggedIn:true (which stranded it with 401s on every API call).
+ expect(body().loggedIn).toBe(false);
+ expect(body().login).toBeTruthy();
+ expect(body().csrfToken).toBeUndefined();
+ });
+
+ it("treats a browser as logged in once it has a real session", () => {
+ const { res, body } = makeRes();
+ bootstrap(makeReq({ loggedIn: true, internal: false }), res);
+
+ expect(body().loggedIn).toBe(true);
+ expect(body().csrfToken).toBe("csrf-test-token");
+ });
+
+ it("bypasses the login screen for the trusted desktop renderer with no web session", () => {
+ const { res, body } = makeRes();
+ bootstrap(makeReq({ loggedIn: false, internal: true }), res);
+
+ // The trilium-app:// renderer is trusted and never gates on a web session.
+ expect(body().loggedIn).toBe(true);
+ expect(body().csrfToken).toBe("csrf-test-token");
+ });
+});
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts
index bfc45b382f..5398b0c945 100644
--- a/apps/server/src/routes/index.ts
+++ b/apps/server/src/routes/index.ts
@@ -7,6 +7,7 @@ import appPath from "../services/app_path.js";
import assetPath from "../services/asset_path.js";
import config from "../services/config.js";
import { getLog } from "@triliumnext/core";
+import { isInternalElectronRequest } from "../services/electron_request.js";
import port from "../services/port.js";
import openID from "../services/open_id.js";
import { isDev, isElectron, isMac, isWindows11 } from "../services/utils.js";
@@ -30,6 +31,15 @@ export function bootstrap(req: Request, res: Response) {
// When auth is disabled the user is implicitly authenticated, so the set-password
// and login pre-auth screens never apply — fall through to the full payload.
const noAuthentication = config.General?.noAuthentication === true;
+ // Whether *this request* is the trusted desktop renderer (arriving via the
+ // trilium-app:// custom protocol, which tags it — see electron_request.ts).
+ // The pre-auth screens and the Electron-only window chrome are gated on this
+ // per-request marker, not the process-wide `isElectron` flag: on a desktop
+ // build, a browser hitting the same HTTP listener is NOT the renderer and must
+ // still go through the login screen and get a real session (#10589). Using the
+ // global flag told every such browser it was already logged in, so it skipped
+ // login and then had every API call rejected with "Logged in session not found".
+ const isElectronRenderer = isInternalElectronRequest(req);
const commonItems = {
...getSharedBootstrapItems(assetPath, isDbInitialized),
baseApiUrl: "api/",
@@ -55,19 +65,20 @@ export function bootstrap(req: Request, res: Response) {
res.send({
...commonItems,
hasNativeTitleBar: false,
- hasBackgroundEffects: isElectron && (isWindows11 || isMac),
+ hasBackgroundEffects: isElectronRenderer && (isWindows11 || isMac),
isMainWindow: true,
appCssNoteIds: []
} satisfies BootstrapDefinition);
return;
}
- if (!isElectron && !noAuthentication && !passwordService.isPasswordSet()) {
+ if (!isElectronRenderer && !noAuthentication && !passwordService.isPasswordSet()) {
// Pre-auth window: the DB is initialized but no password has been set yet.
- // This screen is web/server-only — the desktop app manages its protected-notes
- // password through the options UI and never gates the app on it — so we exclude
- // Electron here, which also means the Electron-only title-bar / background-effect
- // flags are unconditionally false. We serve a minimal payload (no CSRF token /
+ // The desktop renderer manages its protected-notes password through the options
+ // UI and never gates the app on it — so we exclude only the trusted renderer here
+ // (a browser hitting the desktop's HTTP listener still gets this screen), which
+ // also means the Electron-only title-bar / background-effect flags are
+ // unconditionally false. We serve a minimal payload (no CSRF token /
// session data) carrying `passwordSet: false`; theme and icon-pack CSS still come
// from commonItems so the screen matches the rest of the app.
res.send({
@@ -80,9 +91,10 @@ export function bootstrap(req: Request, res: Response) {
return;
}
- if (!isElectron && !noAuthentication && !req.session.loggedIn) {
- // Pre-auth window: a password is set but the user hasn't logged in. Web/server
- // only — the desktop app doesn't gate on a web session. Serve a minimal payload
+ if (!isElectronRenderer && !noAuthentication && !req.session.loggedIn) {
+ // Pre-auth window: a password is set but the user hasn't logged in. Skipped only
+ // for the trusted desktop renderer, which doesn't gate on a web session; a browser
+ // reaching the desktop's HTTP listener still logs in here. Serve a minimal payload
// (no CSRF token / session data) carrying `loggedIn: false` plus the login-screen
// config, which the client uses to render the login screen. The one-shot SSO error
// left by a failed OIDC round-trip is read and cleared here (previously done by the
@@ -133,9 +145,9 @@ export function bootstrap(req: Request, res: Response) {
loggedIn: true,
csrfToken,
oauthJustEnrolled,
- hasNativeTitleBar: isElectron && nativeTitleBarVisible,
+ hasNativeTitleBar: isElectronRenderer && nativeTitleBarVisible,
hasBackgroundEffects: options.backgroundEffects === "true"
- && isElectron
+ && isElectronRenderer
&& (isWindows11 || isMac)
&& !nativeTitleBarVisible,
isMainWindow: view === "mobile" ? true : !req.query.extraWindow,