Skip to content

Build your personal knowledge base with Trilium Notes

fix(oauth): address automated review findings

Elian Doran4fca22cc932f
diff --git a/apps/client/src/widgets/type_widgets/options/password.tsx b/apps/client/src/widgets/type_widgets/options/password.tsx
index ae383a044f..4de8b99230 100644
--- a/apps/client/src/widgets/type_widgets/options/password.tsx
+++ b/apps/client/src/widgets/type_widgets/options/password.tsx
@@ -387,6 +387,10 @@ function OAuthProviderRows({ status }: { status?: OAuthStatus }) {
function OAuthProviderIcon({ src }: { src?: string }) {
const [ failed, setFailed ] = useState(false);
+ // Reset the fallback flag when the source changes (e.g. the user switches providers), otherwise a
+ // previously-failed load would keep showing the key glyph even when the new URL is perfectly valid.
+ useEffect(() => setFailed(false), [src]);
+
if (!src || failed) {
return <span className="bx bx-key oauth-provider-icon" />;
}
diff --git a/apps/server/src/routes/login.ts b/apps/server/src/routes/login.ts
index 704b51cc68..e3c23a74f6 100644
--- a/apps/server/src/routes/login.ts
+++ b/apps/server/src/routes/login.ts
@@ -166,9 +166,11 @@ function logout(req: Request, res: Response) {
req.session.regenerate(() => {
req.session.loggedIn = false;
- if (openID.isOpenIDEnabled() && openIDEncryption.isSubjectIdentifierSaved()) {
+ if (openID.isOpenIDEnabled() && openIDEncryption.isSubjectIdentifierSaved() && res.oidc) {
// oidc.logout() already issues the redirect (to the provider's end-session
// endpoint, or locally), so we must not send our own response afterwards.
+ // res.oidc is only present once the OIDC middleware has initialised; if it
+ // hasn't (e.g. a failed lazy init), fall through to the local redirect below.
void res.oidc.logout({ returnTo: '/' });
return;
}
diff --git a/apps/server/src/services/open_id.spec.ts b/apps/server/src/services/open_id.spec.ts
index 2d66fa5dc6..7a16f9e012 100644
--- a/apps/server/src/services/open_id.spec.ts
+++ b/apps/server/src/services/open_id.spec.ts
@@ -569,4 +569,21 @@ describe("createReactiveOidcMiddleware", () => {
expect(t.buildAuth).toHaveBeenCalledOnce();
expect(t.oidcHandler).toHaveBeenCalledTimes(2);
});
+
+ // A failed first init must not be cached as a permanently-rejected promise: a transient failure
+ // (discovery probe error, malformed config) would otherwise break every subsequent OAuth request
+ // until a server restart. The next request must be allowed to retry and recover.
+ it("retries the build on a subsequent request after a failed init", async () => {
+ const t = setup();
+ t.setConfigured(true);
+ t.isRpInitiatedLogoutSupported.mockRejectedValueOnce(new Error("transient discovery failure"));
+
+ await expect(run(t.middleware)).rejects.toThrow("transient discovery failure");
+ expect(t.oidcHandler).not.toHaveBeenCalled();
+
+ // Second request: the probe succeeds and the handler is finally built and delegated to.
+ const { req, res } = await run(t.middleware);
+ expect(t.buildAuth).toHaveBeenCalledOnce();
+ expect(t.oidcHandler).toHaveBeenCalledWith(req, res, expect.any(Function));
+ });
});
diff --git a/apps/server/src/services/open_id.ts b/apps/server/src/services/open_id.ts
index 61d24bd76c..7f676aeed6 100644
--- a/apps/server/src/services/open_id.ts
+++ b/apps/server/src/services/open_id.ts
@@ -300,7 +300,15 @@ export function createReactiveOidcMiddleware(deps: Partial<ReactiveOidcDeps> = {
const endSessionSupported = await probeRpLogout();
oidcMiddleware = authFactory(buildOAuthConfig(endSessionSupported));
})();
- await oidcInit;
+ try {
+ await oidcInit;
+ } catch (error) {
+ // Reset so the next request can retry — otherwise a single failed init (transient
+ // discovery-probe failure, malformed config, etc.) would leave the rejected promise
+ // cached and break every subsequent OAuth request until a server restart.
+ oidcInit = null;
+ throw error;
+ }
}
// Hand off entirely to the express-openid-connect handler: it drives the request by side effect