Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(server): sync login API broken
Elian Doran50dc12cf7eeb
diff --git a/apps/server/src/routes/api/login.spec.ts b/apps/server/src/routes/api/login.spec.tsindex 2a176490d2..584e184621 100644--- a/apps/server/src/routes/api/login.spec.ts+++ b/apps/server/src/routes/api/login.spec.ts@@ -1,47 +1,93 @@import { app_info as appInfo, cls, date_utils as dateUtils, options, options as optionService } from "@triliumnext/core";-import type { Request } from "express";-import { describe, expect, it } from "vitest";+import type { Application, Request } from "express";+import supertest from "supertest";+import { beforeAll, describe, expect, it } from "vitest";import totpService from "../../services/totp.js";import utils from "../../services/utils.js";import loginApiRoute from "./login.js";function syncReq(body: Record<string, unknown>) {- return { body, session: {} } as unknown as Request;+ const session: Record<string, unknown> = {+ // mimic express-session: regenerate clears the session and invokes the callback+ regenerate(cb: (err?: unknown) => void) {+ regenerateCalled = true;+ for (const key of Object.keys(session)) {+ if (key !== "regenerate") {+ delete session[key];+ }+ }+ cb();+ }+ };+ return { body, session } as unknown as Request;}+let regenerateCalled = false;+describe("Login (sync) API", () => {- it("rejects a timestamp that is out of sync", () => {+ it("rejects a timestamp that is out of sync", async () => {const stale = dateUtils.utcDateTimeStr(new Date(Date.now() - 10 * 60 * 1000));- const result = loginApiRoute.loginSync(syncReq({ timestamp: stale, syncVersion: appInfo.syncVersion }));+ const result = await loginApiRoute.loginSync(syncReq({ timestamp: stale, syncVersion: appInfo.syncVersion }));expect(result[0]).toBe(401);});- it("rejects a non-matching sync version", () => {+ it("rejects a non-matching sync version", async () => {const now = dateUtils.utcNowDateTime();- const result = loginApiRoute.loginSync(syncReq({ timestamp: now, syncVersion: appInfo.syncVersion + 1 }));+ const result = await loginApiRoute.loginSync(syncReq({ timestamp: now, syncVersion: appInfo.syncVersion + 1 }));expect(result[0]).toBe(400);expect((result[1] as { message: string }).message).toContain("Non-matching sync versions");});- it("rejects an incorrect hash", () => {+ it("rejects an incorrect hash", async () => {const now = dateUtils.utcNowDateTime();- const result = loginApiRoute.loginSync(syncReq({ timestamp: now, syncVersion: appInfo.syncVersion, hash: "wrong" }));+ const result = await loginApiRoute.loginSync(syncReq({ timestamp: now, syncVersion: appInfo.syncVersion, hash: "wrong" }));expect(result[0]).toBe(400);expect((result[1] as { message: string }).message).toContain("Sync login credentials are incorrect");});- it("logs in with a correct HMAC of the document secret", () => {+ it("logs in with a correct HMAC of the document secret and regenerates the session", async () => {const now = dateUtils.utcNowDateTime();const documentSecret = options.getOption("documentSecret");const hash = utils.hmac(documentSecret, now);const req = syncReq({ timestamp: now, syncVersion: appInfo.syncVersion, hash });- const result = loginApiRoute.loginSync(req) as { instanceId: string; maxEntityChangeId: number };+ regenerateCalled = false;+ const result = await loginApiRoute.loginSync(req) as { instanceId: string; maxEntityChangeId: number };expect(result.instanceId).toBeTruthy();+ // session must be regenerated (fixation protection) before being marked as logged in+ expect(regenerateCalled).toBe(true);expect(req.session.loggedIn).toBe(true);});});+// Integration test that drives the request through the real route registration+// (app.js -> routes.ts). loginSync is async, so it MUST be registered with+// `asyncRoute`; the synchronous `route()` wraps the handler in better-sqlite3's+// `db.transaction()`, which throws "Transaction function cannot return a promise"+// for an async handler. A direct unit call (above) cannot catch that regression.+describe("Login (sync) API (integration)", () => {+ let agent: ReturnType<typeof supertest.agent>;++ beforeAll(async () => {+ const buildApp = (await import("../../app.js")).default;+ const app: Application = await buildApp();+ agent = supertest.agent(app);+ });++ it("logs in over HTTP through the transactional route wrapper", async () => {+ const now = dateUtils.utcNowDateTime();+ const documentSecret = options.getOption("documentSecret");+ const hash = utils.hmac(documentSecret, now);++ const res = await agent+ .post("/api/login/sync")+ .send({ timestamp: now, syncVersion: appInfo.syncVersion, hash })+ .expect(200);++ expect(res.body.instanceId).toBeTruthy();+ });+});+describe("Login (token) API", () => {it("rejects an incorrect password", async () => {const req = { body: { password: "wrongpassword" } } as unknown as Request;diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.tsindex 5feda1384c..ecad96bf53 100644--- a/apps/server/src/routes/routes.ts+++ b/apps/server/src/routes/routes.ts@@ -128,7 +128,7 @@ function register(app: express.Application) {// docker health checkroute(GET, "/api/health-check", [], () => ({ status: "ok" }), apiResultHandler);- route(PST, "/api/login/sync", [loginRateLimiter], loginApiRoute.loginSync, apiResultHandler);+ asyncRoute(PST, "/api/login/sync", [loginRateLimiter], loginApiRoute.loginSync, apiResultHandler);asyncRoute(PST, "/api/login/token", [loginRateLimiter], loginApiRoute.token, apiResultHandler);apiRoute(GET, "/api/etapi-tokens", etapiTokensApiRoutes.getTokens);