Skip to content

Build your personal knowledge base with Trilium Notes

fix(oauth): surface OAuth/OIDC error detail in the error log

Elian Doran8c16f5dc2515
diff --git a/apps/server/src/routes/error_handlers.spec.ts b/apps/server/src/routes/error_handlers.spec.ts
new file mode 100644
index 0000000000..f527aea39a
--- /dev/null
+++ b/apps/server/src/routes/error_handlers.spec.ts
@@ -0,0 +1,23 @@
+import { describe, expect, it } from "vitest";
+
+import { extractOAuthErrorDetail } from "./error_handlers.js";
+
+describe("extractOAuthErrorDetail", () => {
+ it("combines OAuth error code and description", () => {
+ expect(extractOAuthErrorDetail({ error: "invalid_client", error_description: "Unauthorized" }))
+ .toBe("invalid_client: Unauthorized");
+ });
+
+ it("returns whichever field is present on its own", () => {
+ expect(extractOAuthErrorDetail({ error: "redirect_uri_mismatch" })).toBe("redirect_uri_mismatch");
+ expect(extractOAuthErrorDetail({ error_description: "Bad Request" })).toBe("Bad Request");
+ });
+
+ it("returns null for errors without OAuth detail", () => {
+ expect(extractOAuthErrorDetail(new Error("server responded with an error in the response body"))).toBeNull();
+ expect(extractOAuthErrorDetail({ error: 42 })).toBeNull();
+ expect(extractOAuthErrorDetail("just a string")).toBeNull();
+ expect(extractOAuthErrorDetail(null)).toBeNull();
+ expect(extractOAuthErrorDetail(undefined)).toBeNull();
+ });
+});
diff --git a/apps/server/src/routes/error_handlers.ts b/apps/server/src/routes/error_handlers.ts
index e8b04f7a56..2ab4759cce 100644
--- a/apps/server/src/routes/error_handlers.ts
+++ b/apps/server/src/routes/error_handlers.ts
@@ -39,6 +39,16 @@ function register(app: Application) {
getLog().info(errMessage);
+ // Some upstream errors carry their real cause in OAuth/OIDC-style `error` /
+ // `error_description` properties rather than the generic `.message`. The express-openid-connect
+ // callback is the prime example: a failed token exchange surfaces only "server responded with an
+ // error in the response body" as the message, while the actual reason (e.g. redirect_uri_mismatch,
+ // invalid_client) lives in these fields. Log them so such failures are diagnosable.
+ const oauthDetail = extractOAuthErrorDetail(err);
+ if (oauthDetail) {
+ getLog().error(`OAuth/OpenID error on ${req.method} ${req.url}: ${oauthDetail}`);
+ }
+
res.status(statusCode).send({
message: err instanceof Error ? err.message : "Unknown Error"
});
@@ -46,6 +56,26 @@ function register(app: Application) {
});
}
+/**
+ * Pulls the OAuth/OIDC error code and description off an error, if present. Returns `null` when the
+ * error carries neither, so callers can skip logging for ordinary errors.
+ */
+export function extractOAuthErrorDetail(err: unknown) {
+ if (typeof err !== "object" || err === null) {
+ return null;
+ }
+
+ const candidate = err as { error?: unknown; error_description?: unknown };
+ const code = typeof candidate.error === "string" ? candidate.error : undefined;
+ const description = typeof candidate.error_description === "string" ? candidate.error_description : undefined;
+
+ if (!code && !description) {
+ return null;
+ }
+
+ return [ code, description ].filter(Boolean).join(": ");
+}
+
export default {
register
};