Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Validate SAML LogoutResponse before clearing session

rcourtman0ac04c8d39ae
diff --git a/internal/api/saml_handlers.go b/internal/api/saml_handlers.go
index 4007495ccb..f5f7f7220d 100644
--- a/internal/api/saml_handlers.go
+++ b/internal/api/saml_handlers.go
@@ -425,10 +425,70 @@ func (r *Router) handleSAMLLogout(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, logoutURL, http.StatusFound)
}
-// handleSAMLSLO handles SAML Single Logout responses/requests
+// handleSAMLSLO handles the IdP's signed LogoutResponse following an
+// SP-initiated SLO (the redirect target our MakeLogoutRequest pointed at).
+//
+// The previous implementation cleared the user's session unconditionally on
+// any POST or GET to this endpoint — an unauthenticated cross-origin force-
+// logout DoS against any user with a SAML session. Verify the IdP's
+// XML-DSig on the LogoutResponse before clearing anything; on validation
+// failure log the audit event and refuse to mutate session state.
func (r *Router) handleSAMLSLO(w http.ResponseWriter, req *http.Request) {
- // For now, just redirect to home with logout success
- // A full implementation would validate the LogoutResponse
+ providerID := extractSAMLProviderID(req.URL.Path, "slo")
+ if providerID == "" || !validateProviderID(providerID) {
+ http.NotFound(w, req)
+ return
+ }
+
+ service := r.samlManager.GetService(providerID)
+ if service == nil {
+ http.NotFound(w, req)
+ return
+ }
+
+ // Require either a SAMLResponse (response to our SLO request) or a
+ // SAMLRequest (IdP-initiated SLO). Requests with neither are not real
+ // SAML traffic and should not touch session state.
+ if err := req.ParseForm(); err != nil {
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
+ hasResponse := req.URL.Query().Get("SAMLResponse") != "" || req.PostForm.Get("SAMLResponse") != ""
+ hasRequest := req.URL.Query().Get("SAMLRequest") != "" || req.PostForm.Get("SAMLRequest") != ""
+ if !hasResponse && !hasRequest {
+ log.Warn().
+ Str("provider_id", providerID).
+ Str("client_ip", GetClientIP(req)).
+ Msg("SAML SLO endpoint hit with no SAMLResponse/SAMLRequest payload — refusing to clear session")
+ LogAuditEventForTenant(GetOrgID(req.Context()), "saml_slo_callback", "", GetClientIP(req), req.URL.Path, false, "Missing SAMLResponse/SAMLRequest payload")
+ http.Error(w, "missing SAML payload", http.StatusBadRequest)
+ return
+ }
+
+ // IdP-initiated LogoutRequest validation isn't wired up yet (no stored
+ // in-flight request IDs to bind it to). Reject explicitly rather than
+ // silently treating it as a force-logout signal.
+ if hasRequest && !hasResponse {
+ log.Warn().
+ Str("provider_id", providerID).
+ Str("client_ip", GetClientIP(req)).
+ Msg("IdP-initiated SAML LogoutRequest received but not supported on this SP")
+ LogAuditEventForTenant(GetOrgID(req.Context()), "saml_slo_callback", "", GetClientIP(req), req.URL.Path, false, "IdP-initiated LogoutRequest not supported")
+ http.Error(w, "IdP-initiated logout not supported", http.StatusBadRequest)
+ return
+ }
+
+ if err := service.ValidateLogoutResponse(req); err != nil {
+ log.Warn().
+ Err(err).
+ Str("provider_id", providerID).
+ Str("client_ip", GetClientIP(req)).
+ Msg("SAML LogoutResponse failed signature/validation — refusing to clear session")
+ LogAuditEventForTenant(GetOrgID(req.Context()), "saml_slo_callback", "", GetClientIP(req), req.URL.Path, false, "LogoutResponse validation failed")
+ http.Error(w, "invalid LogoutResponse", http.StatusForbidden)
+ return
+ }
+
r.clearSession(w, req)
LogAuditEventForTenant(GetOrgID(req.Context()), "saml_slo_callback", "", GetClientIP(req), req.URL.Path, true, "SAML SLO complete")
http.Redirect(w, req, "/?logout=success", http.StatusFound)
diff --git a/internal/api/saml_handlers_more_test.go b/internal/api/saml_handlers_more_test.go
index 6500e75d02..7c4e891fe4 100644
--- a/internal/api/saml_handlers_more_test.go
+++ b/internal/api/saml_handlers_more_test.go
@@ -125,18 +125,89 @@ func TestClearSession(t *testing.T) {
}
}
-func TestHandleSAMLSLO_Redirects(t *testing.T) {
- router := &Router{}
+// TestHandleSAMLSLO_RejectsEmptyPayload regresses the unauthenticated force-
+// logout DoS that previously existed on /api/saml/{id}/slo. A bare GET or
+// POST with no SAMLResponse/SAMLRequest payload must not redirect to the
+// "logout=success" page and must not touch session state.
+func TestHandleSAMLSLO_RejectsEmptyPayload(t *testing.T) {
+ router := &Router{samlManager: NewSAMLServiceManager("https://pulse.example.com")}
+ metadataXML := `<?xml version="1.0"?>
+<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="idp">
+ <IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
+ <SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://idp.example.com/sso"/>
+ </IDPSSODescriptor>
+</EntityDescriptor>`
+ router.samlManager.services["okta"] = newTestSAMLService(t, "okta", metadataXML)
+
req := httptest.NewRequest(http.MethodGet, "/api/saml/okta/slo", nil)
rec := httptest.NewRecorder()
router.handleSAMLSLO(rec, req)
- if rec.Code != http.StatusFound {
- t.Fatalf("expected status %d, got %d", http.StatusFound, rec.Code)
+ if rec.Code != http.StatusBadRequest {
+ t.Fatalf("expected 400 on empty SLO payload, got %d body=%q", rec.Code, rec.Body.String())
}
- if loc := rec.Header().Get("Location"); loc != "/?logout=success" {
- t.Fatalf("unexpected redirect location %q", loc)
+ if loc := rec.Header().Get("Location"); loc != "" {
+ t.Fatalf("must not redirect on empty SLO payload, got %q", loc)
+ }
+}
+
+// TestHandleSAMLSLO_RejectsInvalidLogoutResponse confirms a SAMLResponse
+// parameter with a value the IdP cert cannot validate (gibberish) is
+// rejected with 403, again without clearing session.
+func TestHandleSAMLSLO_RejectsInvalidLogoutResponse(t *testing.T) {
+ router := &Router{samlManager: NewSAMLServiceManager("https://pulse.example.com")}
+ metadataXML := `<?xml version="1.0"?>
+<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="idp">
+ <IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
+ <SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://idp.example.com/sso"/>
+ </IDPSSODescriptor>
+</EntityDescriptor>`
+ router.samlManager.services["okta"] = newTestSAMLService(t, "okta", metadataXML)
+
+ // A garbage SAMLResponse value: not real base64-encoded XML, no signature.
+ req := httptest.NewRequest(
+ http.MethodGet,
+ "/api/saml/okta/slo?SAMLResponse=not-a-real-response",
+ nil,
+ )
+ rec := httptest.NewRecorder()
+
+ router.handleSAMLSLO(rec, req)
+
+ if rec.Code != http.StatusForbidden {
+ t.Fatalf("expected 403 on invalid LogoutResponse, got %d body=%q", rec.Code, rec.Body.String())
+ }
+ if loc := rec.Header().Get("Location"); loc != "" {
+ t.Fatalf("must not redirect on invalid LogoutResponse, got %q", loc)
+ }
+}
+
+// TestHandleSAMLSLO_RejectsIDPInitiatedLogoutRequest documents the explicit
+// rejection of IdP-initiated SLO until proper request-binding is wired up.
+// Today an unvalidated LogoutRequest must not be treated as a force-logout
+// signal.
+func TestHandleSAMLSLO_RejectsIDPInitiatedLogoutRequest(t *testing.T) {
+ router := &Router{samlManager: NewSAMLServiceManager("https://pulse.example.com")}
+ metadataXML := `<?xml version="1.0"?>
+<EntityDescriptor xmlns="urn:oasis:names:tc:SAML:2.0:metadata" entityID="idp">
+ <IDPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
+ <SingleSignOnService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="https://idp.example.com/sso"/>
+ </IDPSSODescriptor>
+</EntityDescriptor>`
+ router.samlManager.services["okta"] = newTestSAMLService(t, "okta", metadataXML)
+
+ req := httptest.NewRequest(
+ http.MethodGet,
+ "/api/saml/okta/slo?SAMLRequest=anything",
+ nil,
+ )
+ rec := httptest.NewRecorder()
+
+ router.handleSAMLSLO(rec, req)
+
+ if rec.Code != http.StatusBadRequest {
+ t.Fatalf("expected 400 on IdP-initiated SLO, got %d body=%q", rec.Code, rec.Body.String())
}
}
diff --git a/internal/api/saml_service.go b/internal/api/saml_service.go
index f6e1eb53ac..01d6fbd187 100644
--- a/internal/api/saml_service.go
+++ b/internal/api/saml_service.go
@@ -622,6 +622,28 @@ func (s *SAMLService) MakeLogoutRequest(nameID, sessionIdx string) (string, erro
return validateSAMLRedirectTarget(redirectURL.String(), s.idpMetadata.IDPSSODescriptors[0].SingleLogoutServices)
}
+// ValidateLogoutResponse verifies an incoming IdP-signed SAML LogoutResponse,
+// checking the XML-DSig signature against the IdP's published certificate as
+// well as the standard LogoutResponse temporal / target invariants. Returns
+// nil only when the response is genuinely from the configured IdP and bound
+// to the SAML state — anything that fails validation (no payload, bad
+// signature, unknown issuer, expired) returns an error and the caller MUST
+// NOT mutate any session state on the strength of the request.
+//
+// Without this validation the SLO callback endpoint was an unauthenticated
+// force-logout DoS: a cross-origin POST with no payload, or any forged
+// payload, cleared the user's session.
+func (s *SAMLService) ValidateLogoutResponse(req *http.Request) error {
+ s.mu.RLock()
+ defer s.mu.RUnlock()
+
+ if s.sp == nil {
+ return errors.New("service provider not initialized")
+ }
+
+ return s.sp.ValidateLogoutResponseRequest(req)
+}
+
func validateSAMLRedirectTarget(rawURL string, allowedEndpoints []saml.Endpoint) (string, error) {
validatedURL, err := securityutil.NormalizeAbsoluteHTTPURL(rawURL)
if err != nil {