Skip to content

Build your personal knowledge base with Trilium Notes

chore(electron): bypass auth

Elian Doran415dd115e396
diff --git a/apps/server/src/routes/csrf_protection.ts b/apps/server/src/routes/csrf_protection.ts
index 972c3b2f60..450b8089e8 100644
--- a/apps/server/src/routes/csrf_protection.ts
+++ b/apps/server/src/routes/csrf_protection.ts
@@ -1,6 +1,8 @@
import { doubleCsrf } from "csrf-csrf";
+import type { NextFunction, Request, Response } from "express";
import sessionSecret from "../services/session_secret.js";
+import { isElectron } from "../services/utils.js";
export const CSRF_COOKIE_NAME = "trilium-csrf";
@@ -16,4 +18,16 @@ const doubleCsrfUtilities = doubleCsrf({
getSessionIdentifier: (req) => req.session.id
});
-export const { generateCsrfToken, doubleCsrfProtection } = doubleCsrfUtilities;
+export const { generateCsrfToken } = doubleCsrfUtilities;
+
+// Skip CSRF validation under Electron. The desktop renderer is our own UI,
+// requests arrive through the `trilium-app://` custom protocol, and Express
+// sessions don't round-trip through that path — so the HMAC double-submit
+// check has nothing meaningful to validate against. Auth is similarly
+// bypassed for Electron in `services/auth.ts`.
+export function doubleCsrfProtection(req: Request, res: Response, next: NextFunction) {
+ if (isElectron) {
+ return next();
+ }
+ return doubleCsrfUtilities.doubleCsrfProtection(req, res, next);
+}
diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.ts
index a20354f7ff..63b6a5a66b 100644
--- a/apps/server/src/services/auth.ts
+++ b/apps/server/src/services/auth.ts
@@ -92,7 +92,14 @@ function checkApiAuth(req: Request, res: Response, next: NextFunction) {
return next();
}
- if (!req.session.loggedIn && !noAuthentication) {
+ // The desktop renderer is trusted (it's our own UI). API requests come in
+ // via the `trilium-app://` custom protocol where Express sessions don't
+ // round-trip, so we bypass auth here the same way `checkAuth` does.
+ if (isElectron || noAuthentication) {
+ return next();
+ }
+
+ if (!req.session.loggedIn) {
console.warn(`Missing session with ID '${req.sessionID}'.`);
reject(req, res, "Logged in session not found");
} else {