Skip to content

Build your personal knowledge base with Trilium Notes

16.8 KB
Raw Blame
import { dayjs } from "@triliumnext/commons";
import express, { type Application } from "express";
import { SessionData } from "express-session";
import supertest, { type Response } from "supertest";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";

import { cls, events, initMessaging, options } from "@triliumnext/core";
import { refreshAuth } from "../services/auth.js";
import config from "../services/config.js";
import totp from "../services/totp.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 () => {
        vi.useFakeTimers();
        const buildApp = (await import("../app.js")).default;
        app = await buildApp();
        ({ sessionStore, CLEAN_UP_INTERVAL } = (await import("./session_parser.js")));
    });

    afterAll(() => {
        vi.useRealTimers();
    });

    it("redirects GET /login to the SPA root with the login marker, since login is served client-side", async () => {
        const res = await supertest(app)
            .get("/login")
            .expect(302);

        expect(res.headers.location).toBe("./?login");
    });

    it("returns a 401 status, when login fails with wrong password", async () => {

        await supertest(app)
            .post("/login")
            .send({ password: "fakePassword" })
            .expect(401);

    });

    it("returns a 401 status, when the password field is missing or not a string", async () => {
        for (const body of [{}, { password: ["a"] }]) {
            await supertest(app)
                .post("/login")
                .send(body)
                .expect(401);
        }
    });

    it("rejects a TOTP token that is not a string without verifying it", async () => {
        vi.spyOn(totp, "isTotpEnabled").mockReturnValue(true);
        const verifyTOTP = vi.spyOn(totp, "verifyTOTP");

        try {
            const res = await supertest(app)
                .post("/login")
                .send({ password: "demo1234", totpToken: ["123456"] })
                .expect(401);

            expect(res.body).toEqual({ success: false, factor: "totp" });
            expect(verifyTOTP).not.toHaveBeenCalled();
        } finally {
            vi.restoreAllMocks();
        }
    });

    describe("login stays reachable with redirectBareDomain enabled (#10552)", () => {
        // The fixture DB contains a #shareRoot note (y0AFOwgOgkWO), so enabling
        // redirectBareDomain arms the bare-domain → share redirect in checkAuth.
        // The login screen is served by the SPA at the root, so an explicit login
        // request must still be able to reach it — otherwise the share redirect
        // locks the owner out of their own instance.
        let originalNoAuthentication: boolean;

        beforeAll(() => {
            // The spec data dir's config.ini enables noAuthentication, which makes
            // checkAuth wave every request through and would mask the redirect
            // behavior under test.
            originalNoAuthentication = config.General.noAuthentication;
            config.General.noAuthentication = false;
            refreshAuth();
            cls.init(() => options.setOption("redirectBareDomain", "true"));
        });

        afterAll(() => {
            config.General.noAuthentication = originalNoAuthentication;
            refreshAuth();
            cls.init(() => options.setOption("redirectBareDomain", "false"));
        });

        it("still redirects the bare domain itself to the share page", async () => {
            const res = await supertest(app).get("/").expect(302);
            expect(res.headers.location).toBe("share");
        });

        it("GET /login ends on the login screen, not the share page", async () => {
            const { res, path } = await followRedirects(app, "/login");

            expect(path).not.toMatch(/^\/share/);
            expect(res.status).toBe(200);
        });

        it("GET /login/ (trailing slash) ends on the login screen, not a redirect loop", async () => {
            const { res, path } = await followRedirects(app, "/login/");

            expect(path).not.toMatch(/^\/share/);
            expect(res.status).toBe(200);
        });

        it("serves the bootstrap payload to a logged-out client instead of redirecting it to share", async () => {
            // The SPA's login screen is driven by the bootstrap `loggedIn: false`
            // payload; if /bootstrap gets bounced to the share page, the login
            // screen can never render even when the SPA itself is served.
            const res = await supertest(app).get("/bootstrap").expect(200);
            expect(res.body.loggedIn).toBe(false);
        });
    });

    describe("Login when 'Remember Me' is ticked", async () => {
        // TriliumNextTODO: make setting cookieMaxAge via env variable work
        // => process.env.TRILIUM_SESSION_COOKIEMAXAGE
        // the custom cookieMaxAge is currently hardocded in the test data dir's config.ini

        let res: Response;
        let setCookieHeader: string;
        let expectedExpiresDate: string;

        beforeAll(async () => {
            const CUSTOM_MAX_AGE_SECONDS = 86400;

            expectedExpiresDate = dayjs().utc().add(CUSTOM_MAX_AGE_SECONDS, "seconds").toDate().toUTCString();
            res = await supertest(app)
                .post("/login")
                .send({ password: "demo1234", rememberMe: 1 })
                .expect(302);
            setCookieHeader = res.headers["set-cookie"][0];
        });

        it("sets correct Expires for the cookie", async () => {
            // match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
            const expiresCookieRegExp = /Expires=(?<date>[\w\s,:]+)/;
            const expiresCookieMatch = setCookieHeader.match(expiresCookieRegExp);
            const actualExpiresDate = new Date(expiresCookieMatch?.groups?.date || "").toUTCString();

            expect(actualExpiresDate).to.not.eql("Invalid Date");

            // ignore the seconds in the comparison, just to avoid flakiness in tests,
            // if for some reason execution is slow between calculation of expected and actual
            expect(actualExpiresDate.slice(0,23)).toBe(expectedExpiresDate.slice(0,23));
        });

        it("sets the correct sesssion data", async () => {
            // Check the session is stored in the database.
            const { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session!).toBeTruthy();
            expect(session!.cookie.expires).toBeTruthy();
            expect(new Date(session!.cookie.expires!).toUTCString().substring(0, 23))
                .toBe(expectedExpiresDate.substring(0, 23));
            expect(session!.loggedIn).toBe(true);
            expect(expiry).toStrictEqual(new Date(session!.cookie.expires!));
        });

        it("doesn't renew the session on subsequent requests", async () => {
            const { expiry: originalExpiry } = await getSessionFromCookie(setCookieHeader);

            // Simulate user waiting half the period before the session expires.
            vi.setSystemTime(originalExpiry!.getTime() - (originalExpiry!.getTime() - Date.now()) / 2);

            // Make a request to renew the session.
            await supertest(app)
                .get("/")
                .set("Cookie", setCookieHeader)
                .expect(200);

            // Check the session is still valid and has not been renewed.
            const { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session).toBeTruthy();
            expect(expiry!.getTime()).toStrictEqual(originalExpiry!.getTime());
        });

        it("cleans up expired sessions", async () => {
            let { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session).toBeTruthy();
            expect(expiry).toBeTruthy();

            vi.setSystemTime(expiry!);
            cls.init(() => vi.advanceTimersByTime(CLEAN_UP_INTERVAL));
            ({ session } = await getSessionFromCookie(setCookieHeader));
            expect(session).toBeFalsy();
        });
    });

    describe("Login when 'Remember Me' is not ticked", async () => {
        let res: Response;
        let setCookieHeader: string;

        beforeAll(async () => {
            res = await supertest(app)
                .post("/login")
                .send({ password: "demo1234" })
                .expect(302);

            setCookieHeader = res.headers["set-cookie"][0];
        });

        it("does not set Expires", async () => {
            // match for e.g. "Expires=Wed, 07 May 2025 07:02:59 GMT;"
            expect(setCookieHeader).not.toMatch(/Expires=(?<date>[\w\s,:]+)/);
        });

        it("stores the session in the database", async () => {
            const { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session!).toBeTruthy();
            expect(session!.cookie.expires).toBeUndefined();
            expect(session!.loggedIn).toBe(true);

            const expectedExpirationDate = dayjs().utc().add(1, "day").toDate();
            expect(expiry?.getTime()).toBeGreaterThan(new Date().getTime());
            expect(expiry?.getTime()).toBeLessThanOrEqual(expectedExpirationDate.getTime());
        });

        it("renews the session on subsequent requests", async () => {
            const { expiry: originalExpiry } = await getSessionFromCookie(setCookieHeader);

            // Simulate user waiting half the period before the session expires.
            vi.setSystemTime(originalExpiry!.getTime() - (originalExpiry!.getTime() - Date.now()) / 2);

            // Make a request to renew the session.
            await supertest(app)
                .get("/")
                .set("Cookie", setCookieHeader)
                .expect(200);

            // Check the session is still valid and has been renewed.
            const { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session).toBeTruthy();
            expect(expiry!.getTime()).toBeGreaterThan(originalExpiry!.getTime());
        });

        it("keeps session up to 24 hours", async () => {
            // Simulate user waiting 23 hours.
            vi.setSystemTime(dayjs().add(23, "hours").toDate());
            vi.advanceTimersByTime(CLEAN_UP_INTERVAL);

            // Check the session is still valid.
            const { session } = await getSessionFromCookie(setCookieHeader);
            expect(session).toBeTruthy();
        });

        it("cleans up expired sessions", async () => {
            let { session, expiry } = await getSessionFromCookie(setCookieHeader);
            expect(session).toBeTruthy();
            expect(expiry).toBeTruthy();

            vi.setSystemTime(expiry!);
            vi.advanceTimersByTime(CLEAN_UP_INTERVAL);
            ({ session } = await getSessionFromCookie(setCookieHeader));
            expect(session).toBeFalsy();
        });
    });

    describe("Password change revokes sessions", () => {
        let originalHash: string;
        const disconnectAllClients = vi.fn();

        beforeAll(() => {
            originalHash = cls.init(() => options.getOption("passwordVerificationHash"));
            initMessaging({
                attachToHttpServer() {},
                disconnectAllClients,
                sendMessageToAllClients() {},
                sendMessageToClient: () => false,
                setClientMessageHandler() {}
            } as Parameters<typeof initMessaging>[0]);
        });

        afterEach(() => {
            cls.init(() => options.setOption("passwordVerificationHash", originalHash));
            disconnectAllClients.mockClear();
        });

        async function logIn() {
            const res = await supertest(app)
                .post("/login")
                .send({ password: "demo1234" })
                .expect(302);
            const cookie = res.headers["set-cookie"][0];
            expect((await getSessionFromCookie(cookie)).session?.loggedIn).toBe(true);
            return cookie;
        }

        it("keeps sessions when an unrelated option changes", async () => {
            const cookie = await logIn();
            cls.init(() => options.setOption("redirectBareDomain", "false"));
            expect((await getSessionFromCookie(cookie)).session?.loggedIn).toBe(true);
            expect(disconnectAllClients).not.toHaveBeenCalled();
        });

        it("removes every session when the password changes on this instance", async () => {
            const first = await logIn();
            const second = await logIn();

            cls.init(() => options.setOption("passwordVerificationHash", "changed-locally"));

            expect((await getSessionFromCookie(first)).session).toBeFalsy();
            expect((await getSessionFromCookie(second)).session).toBeFalsy();
            expect(disconnectAllClients).toHaveBeenCalled();
        });

        it("removes every session when a password change arrives by sync", async () => {
            const cookie = await logIn();

            cls.init(() => events.emit(events.ENTITY_CHANGE_SYNCED, {
                entityName: "options",
                entityRow: { name: "passwordVerificationHash", value: "changed-remotely", isSynced: 1 }
            }));

            expect((await getSessionFromCookie(cookie)).session).toBeFalsy();
            expect(disconnectAllClients).toHaveBeenCalled();
        });
    });
}, 100_000);

const MAX_REDIRECT_HOPS = 5;

/**
 * Follows 3xx redirects the way a browser would, resolving relative `Location`
 * headers (".", "share", …) against the current URL. Stops after
 * {@link MAX_REDIRECT_HOPS} hops so redirect loops surface as a final 3xx
 * response instead of hanging the test.
 */
async function followRedirects(app: Application, startPath: string) {
    let url = new URL(startPath, "http://localhost");
    let res = await supertest(app).get(url.pathname + url.search);

    for (let hop = 0; hop < MAX_REDIRECT_HOPS && res.status >= 300 && res.status < 400; hop++) {
        const location = res.headers.location;
        if (!location) {
            break;
        }

        url = new URL(location, url);
        res = await supertest(app).get(url.pathname + url.search);
    }

    return { res, path: url.pathname };
}

async function getSessionFromCookie(setCookieHeader: string) {
    // Extract the session ID from the cookie.
    const sessionIdMatch = setCookieHeader.match(/trilium.sid=(?<sessionId>[^;]+)/)?.[1];
    expect(sessionIdMatch).toBeTruthy();

    // Check the session is stored in the database.
    const sessionId = decodeURIComponent(sessionIdMatch!).slice(2).split(".")[0];
    return {
        session: await getSessionFromStore(sessionId),
        expiry: sessionStore.getSessionExpiry(sessionId)
    };
}

function getSessionFromStore(sessionId: string) {
    return new Promise<SessionData | null | undefined>((resolve, reject) => {
        sessionStore.get(sessionId, (err, session) => {
            if (err) {
                reject(err);
            } else {
                resolve(session);
            }
        });
    });
}

/** 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;
}