From 8773731682a10e7084d9b264b6fed1b3f96d59c7 Mon Sep 17 00:00:00 2001 From: compiler-segfault <238626475+compiler-segfault@users.noreply.github.com> Date: Wed, 25 Nov 2026 20:57:35 +0800 Subject: [PATCH] fix(auth): drop the fragment from the OIDC logout redirect The frontend routes are hash routes, so frontendUrl("/login") builds https://host/app/#/login. That value went into post_logout_redirect_uri, which OIDC RP-Initiated Logout does not allow to carry a fragment; Keycloak rejects the request, so logging out with logout_idp_session: true failed, and turning it off left the IdP session open. Strip the fragment before the value is put on the end session URL. The browser lands on the app root instead, which the client already handles. --- internal/app/auth/auth_oidc.go | 17 ++++++++++- internal/app/auth/auth_oidc_test.go | 46 +++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/internal/app/auth/auth_oidc.go b/internal/app/auth/auth_oidc.go index 2dc01378..c250fef3 100644 --- a/internal/app/auth/auth_oidc.go +++ b/internal/app/auth/auth_oidc.go @@ -124,13 +124,28 @@ func (o OidcAuthenticator) GetLogoutUrl(idTokenHint, postLogoutRedirectUri strin params.Set("id_token_hint", idTokenHint) } if postLogoutRedirectUri != "" { - params.Set("post_logout_redirect_uri", postLogoutRedirectUri) + // A post_logout_redirect_uri must not contain a fragment (OIDC RP-Initiated + // Logout 1.0, section 2), and providers such as Keycloak reject the logout + // request when it does. The frontend routes are hash routes, so drop the + // fragment instead of failing the whole logout. + params.Set("post_logout_redirect_uri", withoutFragment(postLogoutRedirectUri)) } logoutUrl.RawQuery = params.Encode() return logoutUrl.String(), true } +// withoutFragment returns uri without its fragment, so it can be used as an OIDC +// redirect URI. An unparsable uri is returned unchanged. +func withoutFragment(uri string) string { + parsed, err := url.Parse(uri) + if err != nil { + return uri + } + parsed.Fragment = "" + return parsed.String() +} + // PKCEAuthCodeOptions returns PKCE options for the authorization request and the verifier for the token exchange. func (o OidcAuthenticator) PKCEAuthCodeOptions() ([]oauth2.AuthCodeOption, string) { if !o.usePKCE { diff --git a/internal/app/auth/auth_oidc_test.go b/internal/app/auth/auth_oidc_test.go index d57de35a..7cac9057 100644 --- a/internal/app/auth/auth_oidc_test.go +++ b/internal/app/auth/auth_oidc_test.go @@ -77,3 +77,49 @@ func TestOidcAuthenticatorPKCEDisabled(t *testing.T) { t.Fatalf("expected no token options, got %d", len(tokenOptions)) } } + +func TestOidcAuthenticatorGetLogoutUrl(t *testing.T) { + provider := OidcAuthenticator{ + name: "test", + endSessionEndpoint: "https://idp.example.com/realms/test/protocol/openid-connect/logout", + logoutIdpSession: true, + } + + logoutUrl, ok := provider.GetLogoutUrl("the-token", "https://wg.example.com/app/#/login") + if !ok { + t.Fatal("expected a logout url") + } + + parsed, err := url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + + query := parsed.Query() + if got := query.Get("id_token_hint"); got != "the-token" { + t.Fatalf("expected id_token_hint to be kept, got %q", got) + } + if got := query.Get("post_logout_redirect_uri"); got != "https://wg.example.com/app/" { + t.Fatalf("expected the fragment to be dropped, got %q", got) + } + + // a redirect that has no fragment is passed on unchanged + logoutUrl, _ = provider.GetLogoutUrl("", "https://wg.example.com/app/login") + parsed, err = url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + if got := parsed.Query().Get("post_logout_redirect_uri"); got != "https://wg.example.com/app/login" { + t.Fatalf("expected the redirect to be kept, got %q", got) + } + + // without a redirect there is nothing to normalise + logoutUrl, _ = provider.GetLogoutUrl("the-token", "") + parsed, err = url.Parse(logoutUrl) + if err != nil { + t.Fatalf("failed to parse logout url: %v", err) + } + if got := parsed.Query().Get("post_logout_redirect_uri"); got != "" { + t.Fatalf("expected no post_logout_redirect_uri, got %q", got) + } +}