Skip to content

Build your personal knowledge base with Trilium Notes

core/start again: add authentication for setup routes

Adorian Doran4b9132f9a729
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index e40830afcf..868f5065db 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -189,9 +189,15 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTrans
// this is how the app asks the next start to be the wizard, and how it takes the request back.
// Authenticated like the rest of the running app, which is what keeps a passer-by from sending
// somebody else's instance to a screen that can erase it.
- asyncApiRoute(PST, "/api/setup/boot", setupApiRoute.bootToSetup);
- asyncApiRoute(GET, "/api/setup/boot", setupApiRoute.isBootToSetupRequested);
- asyncApiRoute(DEL, "/api/setup/boot", setupApiRoute.cancelBootToSetup);
+ //
+ // `checkSetupAuth` as well, spelled out rather than taken from `asyncApiRoute`, because the
+ // session check above stands down on an instance that reports itself uninitialized — which is
+ // exactly what an instance sitting in the wizard does. Without it these three are the one part
+ // of the wizard a passer-by could still reach, to re-arm a start-over or to call off one the
+ // owner is waiting to act on.
+ asyncRoute(PST, "/api/setup/boot", [checkApiAuth, checkSetupAuth, csrfMiddleware], setupApiRoute.bootToSetup, apiResultHandler);
+ asyncRoute(GET, "/api/setup/boot", [checkApiAuth, checkSetupAuth, csrfMiddleware], setupApiRoute.isBootToSetupRequested, apiResultHandler);
+ asyncRoute(DEL, "/api/setup/boot", [checkApiAuth, checkSetupAuth, csrfMiddleware], setupApiRoute.cancelBootToSetup, apiResultHandler);
// What becomes of the database the wizard was booted away from. Guarded like the rest of setup,
// and refused again inside on an instance that has no such database. Not transactional: the
diff --git a/packages/trilium-core/src/routes/setup_routes.spec.ts b/packages/trilium-core/src/routes/setup_routes.spec.ts
index b031343a1a..d4e0102fd4 100644
--- a/packages/trilium-core/src/routes/setup_routes.spec.ts
+++ b/packages/trilium-core/src/routes/setup_routes.spec.ts
@@ -5,43 +5,115 @@ import { buildSharedApiRoutes } from "./index.js";
/**
* How the setup routes are registered, rather than what they do.
*
- * One property is worth pinning down here, because nothing else catches it and the failure it
- * causes is remote from its cause: a handler that erases the knowledge base must not be wrapped in
- * a transaction. `asyncRoute` opens one in the browser and not on the server, so a route that
- * erases goes on working on the desktop and dies on the standalone build with "cannot rollback - no
- * transaction is active" — the transaction belonged to a connection the erasure closed, and the one
- * opened in its place has nothing to roll back.
+ * Two properties are worth pinning down here, because nothing else catches either and both fail far
+ * from their cause.
+ *
+ * A handler that erases the knowledge base must not be wrapped in a transaction. `asyncRoute` opens
+ * one in the browser and not on the server, so a route that erases goes on working on the desktop
+ * and dies on the standalone build with "cannot rollback - no transaction is active" — the
+ * transaction belonged to a connection the erasure closed, and the one opened in its place has
+ * nothing to roll back.
+ *
+ * And a handler that can lose a knowledge base must carry the wizard's own password gate. The
+ * session check the rest of the application relies on stands down on an instance that reports itself
+ * uninitialized, which is exactly what an instance sitting in the wizard does, so `checkSetupAuth`
+ * is the only thing between these routes and whoever can reach the port.
*/
describe("how the setup routes are registered", () => {
/** Every route that erases the knowledge base before it creates one in its place. */
const ERASING_ROUTES = [
- "/api/setup/new-document",
- "/api/setup/sync-from-server",
- "/api/setup/existing/delete",
+ "POST /api/setup/new-document",
+ "POST /api/setup/sync-from-server",
+ "POST /api/setup/existing/delete",
// Not an erasure, but it reopens the database it was asked to leave alone, which closes the
// connection just the same.
- "/api/setup/existing/keep"
+ "POST /api/setup/existing/keep"
+ ];
+
+ /** Every route a knowledge base can be lost, replaced or read through while the wizard holds it. */
+ const GATED_ROUTES = [
+ "POST /api/setup/new-document",
+ "POST /api/setup/sync-from-server",
+ "POST /api/setup/existing/backup",
+ "GET /api/setup/existing/backup-defaults",
+ "GET /api/setup/existing/status",
+ "POST /api/setup/existing/delete",
+ "POST /api/setup/existing/keep",
+ // Not a way to lose one, but a way to send somebody else's instance to the screen it can be
+ // lost from, or to call off a start-over its owner is waiting to act on.
+ "POST /api/setup/boot",
+ "GET /api/setup/boot",
+ "DELETE /api/setup/boot"
];
it("keeps every route that closes the database out of a transaction", () => {
- const registered = captureRoutes();
+ const { builders } = captureRegistrations();
- for (const path of ERASING_ROUTES) {
- expect(registered.get(path), `${path} is not registered at all`).toBe("asyncRouteWithoutTransaction");
+ for (const route of ERASING_ROUTES) {
+ expect(builders.get(route), `${route} is not registered at all`)
+ .toBe("asyncRouteWithoutTransaction");
}
});
+
+ it("puts the wizard's password gate in front of everything a knowledge base can be lost through", () => {
+ const { middleware } = captureRegistrations();
+
+ for (const route of GATED_ROUTES) {
+ expect(middleware.get(route), `${route} is not registered at all`)
+ .toContain("checkSetupAuth");
+ }
+ });
+
+ it("leaves open only the routes that have to be", () => {
+ // Read as a whole rather than one at a time: what matters is that the list of setup routes
+ // anybody can reach stays this short, and that each one on it is deliberate. A route added
+ // without the gate lands here and has to be argued for.
+ const { middleware } = captureRegistrations();
+ const open = [ ...middleware ]
+ .filter(([ route ]) => route.includes(" /api/setup/"))
+ .filter(([ , guards ]) => !guards.includes("checkSetupAuth"))
+ .map(([ route ]) => route)
+ .sort();
+
+ expect(open).toEqual([
+ // Says whether there is a database and whether the wizard is locked, and nothing about
+ // the knowledge base behind it — see `getStatus`.
+ "GET /api/setup/status",
+ // Answered with the caller's own password rather than with a token of ours.
+ "GET /api/setup/sync-seed",
+ // The password check itself, which is what every gate above is satisfied by. Rate limited.
+ "POST /api/setup/auth",
+ // Pushed by another device, which can carry nothing this instance issued. Refused from
+ // inside while there is a knowledge base to lose — see `saveSyncSeed`.
+ "POST /api/setup/sync-seed"
+ ]);
+ });
});
/**
- * Runs the route builder against a context that records which builder each path went through, and
- * does nothing else. Enough for the question above, which is about registration rather than about
+ * Runs the route builder against a context that records what each route was registered with, and
+ * does nothing else. Enough for the questions above, which are about registration rather than about
* anything a handler does.
+ *
+ * The middleware are recorded by name: each is handed in as a distinguishable stub, so a route's
+ * guards can be read back as the names of the ones it was given.
*/
-function captureRoutes(): Map<string, string> {
- const registered = new Map<string, string>();
- const noop = () => {};
- const record = (builder: string) => (_method: string, path: string) => {
- registered.set(path, builder);
+function captureRegistrations(): { builders: Map<string, string>; middleware: Map<string, string[]> } {
+ const builders = new Map<string, string>();
+ const middleware = new Map<string, string[]>();
+ const names = new Map<unknown, string>();
+ const stub = (name: string) => {
+ const fn = () => {};
+ names.set(fn, name);
+ return fn;
+ };
+ const record = (builder: string) => (method: string, path: string, ...rest: unknown[]) => {
+ const route = `${method.toUpperCase()} ${path}`;
+ builders.set(route, builder);
+ // The middleware array is the third argument where there is one; the shorthand builders
+ // (`apiRoute`, `asyncApiRoute`) take the handler there instead and supply their own.
+ const given = Array.isArray(rest[0]) ? rest[0] : [];
+ middleware.set(route, given.map((fn) => names.get(fn) ?? "unknown"));
};
buildSharedApiRoutes({
@@ -50,17 +122,17 @@ function captureRoutes(): Map<string, string> {
asyncRouteWithoutTransaction: record("asyncRouteWithoutTransaction"),
apiRoute: record("apiRoute"),
asyncApiRoute: record("asyncApiRoute"),
- apiResultHandler: noop,
- checkApiAuth: noop,
- checkApiAuthOrElectron: noop,
- checkAppNotInitialized: noop,
- checkSetupAuth: noop,
- checkCredentials: noop,
- loginRateLimiter: noop,
- uploadMiddlewareWithErrorHandling: noop,
- importMiddlewareWithErrorHandling: noop,
- csrfMiddleware: noop
+ apiResultHandler: stub("apiResultHandler"),
+ checkApiAuth: stub("checkApiAuth"),
+ checkApiAuthOrElectron: stub("checkApiAuthOrElectron"),
+ checkAppNotInitialized: stub("checkAppNotInitialized"),
+ checkSetupAuth: stub("checkSetupAuth"),
+ checkCredentials: stub("checkCredentials"),
+ loginRateLimiter: stub("loginRateLimiter"),
+ uploadMiddlewareWithErrorHandling: stub("uploadMiddlewareWithErrorHandling"),
+ importMiddlewareWithErrorHandling: stub("importMiddlewareWithErrorHandling"),
+ csrfMiddleware: stub("csrfMiddleware")
});
- return registered;
+ return { builders, middleware };
}