Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): migrate csrf to v4

Elian Doran6f6041ee7b95
diff --git a/apps/server/src/routes/csrf_protection.ts b/apps/server/src/routes/csrf_protection.ts
index 391be0aaa7..bd382e3691 100644
--- a/apps/server/src/routes/csrf_protection.ts
+++ b/apps/server/src/routes/csrf_protection.ts
@@ -10,7 +10,8 @@ const doubleCsrfUtilities = doubleCsrf({
sameSite: "strict",
httpOnly: !isElectron // set to false for Electron, see https://github.com/TriliumNext/Notes/pull/966
},
- cookieName: "_csrf"
+ cookieName: "_csrf",
+ getSessionIdentifier: (req) => req.session.id
});
-export const { generateToken, doubleCsrfProtection } = doubleCsrfUtilities;
+export const { generateCsrfToken, doubleCsrfProtection } = doubleCsrfUtilities;
diff --git a/apps/server/src/routes/index.ts b/apps/server/src/routes/index.ts
index 79a40f1864..5a907cee48 100644
--- a/apps/server/src/routes/index.ts
+++ b/apps/server/src/routes/index.ts
@@ -10,7 +10,7 @@ import protectedSessionService from "../services/protected_session.js";
import packageJson from "../../package.json" with { type: "json" };
import assetPath from "../services/asset_path.js";
import appPath from "../services/app_path.js";
-import { generateToken as generateCsrfToken } from "./csrf_protection.js";
+import { generateCsrfToken } from "./csrf_protection.js";
import type { Request, Response } from "express";
import type BNote from "../becca/entities/bnote.js";
@@ -19,9 +19,10 @@ function index(req: Request, res: Response) {
const options = optionService.getOptionMap();
const view = getView(req);
- //'overwrite' set to false (default) => the existing token will be re-used and validated
- //'validateOnReuse' set to false => if validation fails, generate a new token instead of throwing an error
- const csrfToken = generateCsrfToken(req, res, false, false);
+ const csrfToken = generateCsrfToken(req, res, {
+ overwrite: true,
+ validateOnReuse: false // if validation fails, generate a new token instead of throwing an error
+ });
log.info(`CSRF token generation: ${csrfToken ? "Successful" : "Failed"}`);
// We force the page to not be cached since on mobile the CSRF token can be