Skip to content

Build your personal knowledge base with Trilium Notes

fix(sync): require the second factor to fetch the sync seed

Elian Doran2d3c54191bcb
diff --git a/apps/server/src/express.d.ts b/apps/server/src/express.d.ts
index 5f5f645ea0..007c4e801c 100644
--- a/apps/server/src/express.d.ts
+++ b/apps/server/src/express.d.ts
@@ -8,6 +8,7 @@ export declare module "express-serve-static-core" {
authorization?: string;
"trilium-cred"?: string;
+ "trilium-totp"?: string;
"x-csrf-token"?: string;
"trilium-component-id"?: string;
diff --git a/apps/server/src/services/auth.spec.ts b/apps/server/src/services/auth.spec.ts
index aab4b70f34..f8562a9416 100644
--- a/apps/server/src/services/auth.spec.ts
+++ b/apps/server/src/services/auth.spec.ts
@@ -480,6 +480,51 @@ describe("Auth", () => {
expect(next).toHaveBeenCalled();
});
+
+ it("checkCredentials requires a valid second factor when TOTP is enabled", async () => {
+ vi.spyOn(sqlInit, "isDbInitialized").mockReturnValue(true);
+ vi.spyOn(passwordService, "isPasswordSet").mockReturnValue(true);
+ vi.spyOn(totp, "isTotpEnabled").mockReturnValue(true);
+ const verifyPasswordSpy = vi.spyOn(passwordEncryptionService, "verifyPassword").mockResolvedValue(true as never);
+ vi.spyOn(totp, "verifyTOTP").mockImplementation((token) => token === "123456");
+ const recoverySpy = vi.spyOn(recoveryCodeService, "verifyRecoveryCode").mockReturnValue(false);
+ const cred = Buffer.from("user:correctpass").toString("base64");
+
+ async function call(headers: Record<string, unknown>) {
+ const res = makeRes();
+ const next = vi.fn();
+ await auth.checkCredentials(makeReq({ headers: { "trilium-cred": cred, ...headers } }), res as never, next);
+ return { res, next };
+ }
+
+ // Missing token -> 401 before the password is checked, so the answer reveals nothing about it.
+ const missing = await call({});
+ expect(missing.next).not.toHaveBeenCalled();
+ expect(missing.res.statusCode).toBe(401);
+ expect(missing.res.body).toBe("TOTP token is required");
+ expect(verifyPasswordSpy).not.toHaveBeenCalled();
+
+ const wrong = await call({ "trilium-totp": "000000" });
+ expect(wrong.next).not.toHaveBeenCalled();
+ expect(wrong.res.statusCode).toBe(401);
+ expect(wrong.res.body).toBe("Incorrect TOTP token");
+
+ // A wrong password is reported as such and never reaches the recovery codes.
+ verifyPasswordSpy.mockResolvedValueOnce(false as never);
+ recoverySpy.mockClear();
+ const wrongPassword = await call({ "trilium-totp": "000000" });
+ expect(wrongPassword.res.statusCode).toBe(401);
+ expect(wrongPassword.res.body).toBe("Incorrect password");
+ expect(recoverySpy).not.toHaveBeenCalled();
+
+ const valid = await call({ "trilium-totp": "123456" });
+ expect(valid.next).toHaveBeenCalled();
+
+ recoverySpy.mockReturnValue(true);
+ const recovery = await call({ "trilium-totp": "recovery-code" });
+ expect(recovery.next).toHaveBeenCalled();
+ expect(recoverySpy).toHaveBeenLastCalledWith("recovery-code");
+ });
});
describe("verifyLoginCredentials", () => {
diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.ts
index a3e510d6e3..6fae4e4737 100644
--- a/apps/server/src/services/auth.ts
+++ b/apps/server/src/services/auth.ts
@@ -267,9 +267,21 @@ async function checkCredentials(req: Request, res: Response, next: NextFunction)
const password = colonIndex === -1 ? "" : auth.substr(colonIndex + 1);
// username is ignored
- if (!(await passwordEncryptionService.verifyPassword(password))) {
+ // Checked before the password so that the answer says nothing about the password.
+ const totpToken = req.headers["trilium-totp"];
+ if (totp.isTotpEnabled() && (typeof totpToken !== "string" || !totpToken)) {
+ res.setHeader("Content-Type", "text/plain").status(401).send("TOTP token is required");
+ getLog().info(`WARNING: Missing TOTP token from ${req.ip}, rejecting.`);
+ return;
+ }
+
+ const failedFactor = await verifyLoginCredentials(password, totpToken);
+ if (failedFactor === "password") {
res.setHeader("Content-Type", "text/plain").status(401).send("Incorrect password");
getLog().info(`WARNING: Wrong password from ${req.ip}, rejecting.`);
+ } else if (failedFactor === "totp") {
+ res.setHeader("Content-Type", "text/plain").status(401).send("Incorrect TOTP token");
+ getLog().info(`WARNING: Wrong TOTP token from ${req.ip}, rejecting.`);
} else {
next();
}