Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): answer rejected async routes instead of hanging (closes #11919)

Claude6292263ca374
diff --git a/apps/server/src/routes/login.spec.ts b/apps/server/src/routes/login.spec.ts
index aff16bfb65..391d4489c0 100644
--- a/apps/server/src/routes/login.spec.ts
+++ b/apps/server/src/routes/login.spec.ts
@@ -71,6 +71,15 @@ describe("Login Route test", () => {
});
+ 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);
+ }
+ });
+
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.
diff --git a/apps/server/src/routes/route_api.spec.ts b/apps/server/src/routes/route_api.spec.ts
index 0d510bddf3..595b53e246 100644
--- a/apps/server/src/routes/route_api.spec.ts
+++ b/apps/server/src/routes/route_api.spec.ts
@@ -230,3 +230,22 @@ describe("internalRoute CLS wiring", () => {
expect(closeProbe.wrote).toBe("written");
});
});
+
+describe("internalRoute without a result handler", () => {
+ let app: express.Application;
+
+ beforeAll(() => {
+ asyncRoute("get", "/no-handler/reject", [], async () => {
+ throw new Error("boom");
+ }, null);
+
+ app = express();
+ app.use(router);
+ });
+
+ it("answers a rejected async handler with a 500 instead of leaving the request open", async () => {
+ const res = await request(app).get("/no-handler/reject").timeout(2000).expect(500);
+
+ expect(res.body).toEqual({ message: "boom" });
+ });
+});
diff --git a/apps/server/src/routes/route_api.ts b/apps/server/src/routes/route_api.ts
index 7b1f45e2c5..dbcaaeb3b4 100644
--- a/apps/server/src/routes/route_api.ts
+++ b/apps/server/src/routes/route_api.ts
@@ -101,6 +101,10 @@ function internalRoute<P extends ParamsDictionary>(method: HttpMethod, path: str
});
if (!resultHandler) {
+ // The handler writes its own response, but a rejection still needs one.
+ if (result instanceof Promise) {
+ result.catch((e: unknown) => handleException(e, method, path, res));
+ }
return;
}
diff --git a/apps/server/src/services/auth.ts b/apps/server/src/services/auth.ts
index 0c0fa54706..a3e510d6e3 100644
--- a/apps/server/src/services/auth.ts
+++ b/apps/server/src/services/auth.ts
@@ -287,12 +287,12 @@ export type LoginFactor = "password" | "totp";
* wrong password — silently wasting one of the user's limited codes. Password-first guarantees a
* recovery code is only ever consumed once the rest of the login is known to succeed.
*/
-export async function verifyLoginCredentials(password: string, totpToken: string): Promise<LoginFactor | null> {
- if (!(await passwordEncryptionService.verifyPassword(password))) {
+export async function verifyLoginCredentials(password: unknown, totpToken: unknown): Promise<LoginFactor | null> {
+ if (typeof password !== "string" || !(await passwordEncryptionService.verifyPassword(password))) {
return "password";
}
- if (totp.isTotpEnabled() && !verifyTOTP(totpToken)) {
+ if (totp.isTotpEnabled() && (typeof totpToken !== "string" || !verifyTOTP(totpToken))) {
return "totp";
}