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(setup): rate-limit failed wizard unlocks
Elian Doranff24f13d37b8
diff --git a/apps/client/src/setup_unlock.spec.tsx b/apps/client/src/setup_unlock.spec.tsxindex 85b0b0ad30..edbe90147b 100644--- a/apps/client/src/setup_unlock.spec.tsx+++ b/apps/client/src/setup_unlock.spec.tsx@@ -5,8 +5,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";vi.mock("./services/i18n", () => ({ t: (key: string) => key }));const serverMock = vi.hoisted(() => ({- // `get` is here for the eager module-load fetches the import graph makes (options, keyboard- // actions), not for anything this screen asks for itself.+ // The eager module-load fetches the import graph makes (options, keyboard actions). This screen+ // asks for nothing through `server` itself — it reads the status of its own `fetch`.get: vi.fn(async (url: string): Promise<unknown> => (url === "keyboard-actions" ? [] : {})),post: vi.fn(async (_url: string, _body?: unknown): Promise<unknown> => ({}))}));@@ -39,11 +39,34 @@ function submit() {container.querySelector("form")?.dispatchEvent(new Event("submit", { bubbles: true, cancelable: true }));}+/** The answer that unlocks, which most cases want and none of them vary. */+const UNLOCKED = { status: 200, body: { authenticated: true, token: "a-token" } };++/** What `POST /api/setup/auth` answers with, or a connection that never reached it. */+function answerWith(resp: { status: number; body?: unknown } | "reject") {+ const fn = resp === "reject"+ ? vi.fn().mockRejectedValue(new Error("network down"))+ : vi.fn().mockResolvedValue({+ ok: resp.status >= 200 && resp.status < 300,+ status: resp.status,+ json: async () => resp.body ?? {}+ });+ globalThis.fetch = fn as typeof fetch;++ return fn;+}++/** The body of the single call the screen made. */+function sentBody(fn: ReturnType<typeof answerWith>) {+ return JSON.parse(fn.mock.calls[0]?.[1]?.body as string);+}+beforeEach(() => {vi.useFakeTimers();onUnlocked.mockReset();setSetupAuthToken.mockReset();- serverMock.post.mockReset().mockResolvedValue({ authenticated: true, token: "a-token" });+ serverMock.post.mockReset();+ answerWith(UNLOCKED);window.glob.setupSecondFactorRequired = undefined;});@@ -71,6 +94,8 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {renderScreen();await settle();+ const fetchFn = answerWith(UNLOCKED);+const input = password();if (input) {input.value = "hunter2";@@ -78,13 +103,14 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {submit();await settle();- expect(serverMock.post).toHaveBeenCalledWith("setup/auth", { password: "hunter2", totpToken: "" });+ expect(fetchFn.mock.calls[0]?.[0]).toBe("api/setup/auth");+ expect(sentBody(fetchFn)).toEqual({ password: "hunter2", totpToken: "" });expect(setSetupAuthToken).toHaveBeenCalledWith("a-token");expect(onUnlocked).toHaveBeenCalled();});it("says so and stays put on a wrong password, so the next attempt is a keystroke away", async () => {- serverMock.post.mockResolvedValue({ authenticated: false });+ answerWith({ status: 401, body: { authenticated: false } });renderScreen();await settle();@@ -98,9 +124,8 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {});it("says so on a connection that failed, which a wrong password never looks like", async () => {- // A refused password is answered, not thrown. Anything landing here is the connection, or- // the rate limiter counting the attempts.- serverMock.post.mockRejectedValue(new Error("network down"));+ // A refused password is a status, not a rejection: `fetch` rejects only below HTTP.+ answerWith("reject");renderScreen();await settle();@@ -111,8 +136,23 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {expect(onUnlocked).not.toHaveBeenCalled();});+ it("reports the limiter's refusal as a wait, not as an unreachable server", async () => {+ // 429 is the limiter, and only reachable because a refusal is a 401 that it counts.+ // Reported apart from a wrong answer, and apart from a dead link.+ answerWith({ status: 429 });+ renderScreen();+ await settle();++ submit();+ await settle();++ const error = container.querySelector(".page-error")?.textContent;+ expect(error).toContain("login.too-many-attempts");+ expect(onUnlocked).not.toHaveBeenCalled();+ });+it("refuses the answer where the server said yes but handed nothing over", async () => {- serverMock.post.mockResolvedValue({ authenticated: true });+ answerWith({ status: 200, body: { authenticated: true } });renderScreen();await settle();@@ -126,9 +166,10 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {it("does not ask twice over while an answer is still coming", async () => {// The button is disabled while it runs, but Enter in the field is a second way in.let answer: (value: unknown) => void = () => {};- serverMock.post.mockImplementation(() => new Promise((resolve) => {+ const fetchFn = vi.fn(() => new Promise((resolve) => {answer = resolve;}));+ globalThis.fetch = fetchFn as unknown as typeof fetch;renderScreen();await settle();@@ -137,9 +178,9 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {submit();await settle();- expect(serverMock.post).toHaveBeenCalledOnce();+ expect(fetchFn).toHaveBeenCalledOnce();- answer({ authenticated: true, token: "a-token" });+ answer({ ok: true, status: 200, json: async () => UNLOCKED.body });await settle();expect(onUnlocked).toHaveBeenCalled();});@@ -163,6 +204,8 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {renderScreen();await settle();+ const fetchFn = answerWith(UNLOCKED);+const totp = container.querySelector<HTMLInputElement>("input[name=totpToken]");if (totp) {totp.value = "123456";@@ -170,7 +213,7 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {submit();await settle();- expect(serverMock.post).toHaveBeenCalledWith("setup/auth", { password: "", totpToken: "123456" });+ expect(sentBody(fetchFn)).toEqual({ password: "", totpToken: "123456" });});it("is not asked for where the instance has none", async () => {@@ -188,6 +231,8 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {renderScreen();await settle();+ const fetchFn = answerWith(UNLOCKED);+const input = password();if (input) {input.value = "filled-in-by-the-browser";@@ -195,6 +240,6 @@ describe("unlocking a wizard that is standing over a knowledge base", () => {submit();await settle();- expect(serverMock.post).toHaveBeenCalledWith("setup/auth", { password: "filled-in-by-the-browser", totpToken: "" });+ expect(sentBody(fetchFn)).toEqual({ password: "filled-in-by-the-browser", totpToken: "" });});});diff --git a/apps/client/src/setup_unlock.tsx b/apps/client/src/setup_unlock.tsxindex dc897fa7fd..9f8f9d0aa1 100644--- a/apps/client/src/setup_unlock.tsx+++ b/apps/client/src/setup_unlock.tsx@@ -2,7 +2,6 @@ import "./setup_unlock.css";import logo from "./assets/icon-color.svg?url";import { t } from "./services/i18n";-import server from "./services/server";import { setSetupAuthToken } from "./services/setup_auth";import CredentialsForm, { type Credentials } from "./widgets/react/CredentialsForm";@@ -28,22 +27,39 @@ import CredentialsForm, { type Credentials } from "./widgets/react/CredentialsFoexport default function SetupUnlock({ onUnlocked }: { onUnlocked: () => void }) {async function submit({ password, totpToken }: Credentials) {try {- const { authenticated, token } = await server.post<{ authenticated: boolean; token?: string }>(- "setup/auth", { password, totpToken });+ // `fetch` rather than `server.post`, as on the login screen: the status carries the+ // answer here, and `server.post` drops it — it reports a non-2xx through a toast and+ // rejects with the response text alone.+ const resp = await fetch(`${window.glob.baseApiUrl}setup/auth`, {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify({ password, totpToken })+ });- if (!authenticated || !token) {+ if (resp.status === 429) {+ // The limiter counting the attempts, not a wrong answer.+ return t("login.too-many-attempts");+ }++ if (!resp.ok) {// Which of the two was wrong is not said, because the server does not say: both are// asked for together and a failure in either is one refusal.return t("setup.unlock-refused");}- setSetupAuthToken(token);+ // The body is read as well as the status: standalone's `apiResultHandler` drops the+ // status out of a `[status, body]` tuple, so a refusal reaches this build as a 200.+ const answer = await resp.json() as { authenticated?: boolean; token?: string };+ if (!answer.authenticated || !answer.token) {+ return t("setup.unlock-refused");+ }++ setSetupAuthToken(answer.token);onUnlocked();return null;} catch {- // A wrong answer is answered, not thrown, so anything landing here is the connection or- // the rate limiter the attempts are counted by.+ // `fetch` rejects only on a network-level failure, never on an HTTP status.return t("login.connection-error");}}diff --git a/apps/server/src/routes/login.spec.ts b/apps/server/src/routes/login.spec.tsindex 7f67a26c9c..aff16bfb65 100644--- a/apps/server/src/routes/login.spec.ts+++ b/apps/server/src/routes/login.spec.ts@@ -1,5 +1,5 @@import { dayjs } from "@triliumnext/commons";-import type { Application } from "express";+import express, { type Application } from "express";import { SessionData } from "express-session";import supertest, { type Response } from "supertest";import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";@@ -7,12 +7,40 @@ import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";import { cls, options } from "@triliumnext/core";import { refreshAuth } from "../services/auth.js";import config from "../services/config.js";+import { createLoginRateLimiter } from "./login.js";import { type SQLiteSessionStore } from "./session_parser.js";let app: Application;let sessionStore: SQLiteSessionStore;let CLEAN_UP_INTERVAL: number;+describe("createLoginRateLimiter", () => {+ it("counts failed attempts and stops answering past the limit", async () => {+ const { app, handled } = buildRateLimitedApp((_req, res) => {+ res.status(401).json({ success: false });+ });++ const statuses = await sendRepeatedly(app, 12);++ expect(handled()).toBe(10); // the configured max+ expect(statuses).toEqual([ ...Array(10).fill(401), 429, 429 ]);+ });++ it("refunds every response below 400, which is why a refusal must not be one", async () => {+ // A route answering a refused credential with 200 has its hit handed straight back by+ // `skipSuccessfulRequests`, and the limit is never reached. Guards the contract+ // `createLoginRateLimiter` documents.+ const { app, handled } = buildRateLimitedApp((_req, res) => {+ res.status(200).json({ authenticated: false });+ });++ const statuses = await sendRepeatedly(app, 12);++ expect(handled()).toBe(12);+ expect(statuses).toEqual(Array(12).fill(200));+ });+});+describe("Login Route test", () => {beforeAll(async () => {@@ -288,3 +316,27 @@ function getSessionFromStore(sessionId: string) {});});}++/** An app whose only route counts how many requests made it past the limiter. */+function buildRateLimitedApp(handler: express.RequestHandler) {+ let handled = 0;++ const app = express();+ app.post("/login", createLoginRateLimiter(), (req, res, next) => {+ handled++;+ handler(req, res, next);+ });++ return { app, handled: () => handled };+}++async function sendRepeatedly(app: Application, count: number) {+ const statuses: number[] = [];++ // Sequentially: the limiter's budget is spent in request order, which is what's asserted.+ for (let i = 0; i < count; i++) {+ statuses.push((await supertest(app).post("/login")).status);+ }++ return statuses;+}diff --git a/apps/server/src/routes/login.ts b/apps/server/src/routes/login.tsindex 8cd026085b..62386ae215 100644--- a/apps/server/src/routes/login.ts+++ b/apps/server/src/routes/login.ts@@ -1,5 +1,6 @@import { password as passwordService, ValidationError } from "@triliumnext/core";import type { Request, Response } from 'express';+import rateLimit from "express-rate-limit";import { verifyLoginCredentials } from "../services/auth.js";import openIDEncryption from '../services/encryption/open_id_encryption.js';@@ -143,6 +144,25 @@ function logout(req: Request, res: Response) {});}+/**+ * Caps password guessing per IP across every route that checks a credential: `/login`,+ * `/api/login/*`, `/api/setup/auth` and the ETAPI login.+ *+ * `skipSuccessfulRequests` refunds the hit from a `res.on("finish")` handler, and+ * express-rate-limit decides what to refund with `requestWasSuccessful`, which defaults to+ * `response.statusCode < 400`. So every route behind this MUST answer a failed credential with a+ * status of 400 or above, or its attempts are refunded as fast as they are counted and the limit is+ * never reached. A successful login to `/login` is counted all the same: it redirects, and a 302 is+ * below 400.+ */+export function createLoginRateLimiter() {+ return rateLimit({+ windowMs: 15 * 60 * 1000, // 15 minutes+ max: 10, // limit each IP to 10 requests per windowMs+ skipSuccessfulRequests: true+ });+}+export default {loginPage,setPasswordPage,diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.tsindex ce38e529f9..6933c8e606 100644--- a/apps/server/src/routes/routes.ts+++ b/apps/server/src/routes/routes.ts@@ -1,6 +1,5 @@import { routes } from "@triliumnext/core";import express from "express";-import rateLimit from "express-rate-limit";import etapiAppInfoRoutes from "../etapi/app_info.js";import etapiAttachmentRoutes from "../etapi/attachments.js";@@ -34,7 +33,7 @@ import systemInfoRoute from "./api/system_info.js";import totp from './api/totp.js';import { doubleCsrfProtection as csrfMiddleware } from "./csrf_protection.js";import * as indexRoute from "./index.js";-import loginRoute from "./login.js";+import loginRoute, { createLoginRateLimiter } from "./login.js";import { apiResultHandler, apiRoute, asyncApiRoute, asyncRoute, importMiddlewareWithErrorHandling, route, router, uploadMiddlewareWithErrorHandling } from "./route_api.js";// page routesimport setupRoute from "./setup.js";@@ -48,11 +47,7 @@ function register(app: express.Application) {route(GET, "/login", [auth.checkAppInitialized, auth.checkPasswordSet], loginRoute.loginPage);route(GET, "/set-password", [auth.checkAppInitialized, auth.checkPasswordNotSet], loginRoute.setPasswordPage);- const loginRateLimiter = rateLimit({- windowMs: 15 * 60 * 1000, // 15 minutes- max: 10, // limit each IP to 10 requests per windowMs- skipSuccessfulRequests: true // successful auth to rate-limited ETAPI routes isn't counted. However, successful auth to /login is still counted!- });+ const loginRateLimiter = createLoginRateLimiter();route(GET, "/bootstrap", [ auth.checkAuth ], indexRoute.bootstrap);asyncRoute(PST, "/login", [loginRateLimiter], loginRoute.login, null);diff --git a/packages/trilium-core/src/routes/api/setup.spec.ts b/packages/trilium-core/src/routes/api/setup.spec.tsindex b3a1008767..acaf4b4445 100644--- a/packages/trilium-core/src/routes/api/setup.spec.ts+++ b/packages/trilium-core/src/routes/api/setup.spec.ts@@ -6,7 +6,11 @@ import * as passwordService from "../../services/encryption/password";import passwordEncryptionService from "../../services/encryption/password_encryption";import optionService from "../../services/options";import setupService from "../../services/setup";-import { isSetupAuthorized, resetSetupAuth } from "../../services/setup_auth";+import {+ initSetupSecondFactor,+ isSetupAuthorized,+ resetSetupAuth+} from "../../services/setup_auth";import {enterSetupMode,initSetupPlatform,@@ -298,13 +302,28 @@ describe("unlocking the wizard (core)", () => {expect(isSetupAuthorized(res.body.token)).toBe(true);});- it("says only no for the wrong one, and hands back nothing to go on", async () => {+ it("refuses the wrong one with 401, and hands back nothing to go on", async () => {vi.spyOn(passwordEncryptionService, "verifyPassword").mockResolvedValue(false);const res = await api.post<{ authenticated: boolean; token?: string }>("/api/setup/auth", { body: { password: "wrong" } });- expect(res.status).toBe(200);+ // 401 rather than 200, so `loginRateLimiter` counts the attempt: it runs with+ // `skipSuccessfulRequests`, which refunds every response below 400.+ expect(res.status).toBe(401);+ expect(res.body).toEqual({ authenticated: false });+ });++ it("refuses a wrong second factor exactly as it refuses a wrong password", async () => {+ // The one that unlimited guessing actually breaks: a passcode is six digits, and+ // `SetupSecondFactor.verify` deliberately does not consume a recovery code here.+ vi.spyOn(passwordEncryptionService, "verifyPassword").mockResolvedValue(true);+ initSetupSecondFactor({ isRequired: () => true, verify: () => false });++ const res = await api.post<{ authenticated: boolean; token?: string }>(+ "/api/setup/auth", { body: { password: "hunter2", totpToken: "000000" } });++ expect(res.status).toBe(401);expect(res.body).toEqual({ authenticated: false });});diff --git a/packages/trilium-core/src/routes/api/setup.ts b/packages/trilium-core/src/routes/api/setup.tsindex fa0b267f0c..434680a069 100644--- a/packages/trilium-core/src/routes/api/setup.ts+++ b/packages/trilium-core/src/routes/api/setup.ts@@ -56,16 +56,19 @@ function getStatus() {/*** Unlocks the wizard with the password of the knowledge base sitting behind it.*- * Rate limited from the outside like every other password check. Answers with the token the rest of- * the setup routes want, or with a plain no: which of the two is the only thing it ever says about- * the password.+ * Returns the token the rest of the setup routes require, or 401 and nothing else: a wrong password+ * and a wrong second factor are refused identically, which is all this ever says about either.+ *+ * The 401 is what makes a failed attempt count. `loginRateLimiter` runs with+ * `skipSuccessfulRequests`, and express-rate-limit refunds the hit for every response below 400, so+ * answering a refusal with 200 lets the password and the second factor be guessed without limit.*/async function authenticate(req: Request) {const password = typeof req.body?.password === "string" ? req.body.password : "";const totpToken = typeof req.body?.totpToken === "string" ? req.body.totpToken : "";const token = await authenticateSetup(password, totpToken);- return token ? { authenticated: true, token } : { authenticated: false };+ return token ? { authenticated: true, token } : [ 401, { authenticated: false } ];}/**