Merge pull request #785 from compiler-segfault/fix/logout-redirect-fragment

fix(auth): drop the fragment from the OIDC logout redirect
This commit is contained in:
PROLICHT GmbH
2026-12-03 13:34:30 +01:00
committed by GitHub
2 changed files with 62 additions and 1 deletions
+16 -1
View File
@@ -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 {
+46
View File
@@ -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)
}
}