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.

Harden local SSO redirect targets

rcourtman57361e0c40f5
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 83041e3149..7b7001bfc5 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -324,6 +324,11 @@ manual SAML test payload preserves the optional `idpSloUrl` alongside
identity before first-user or first-host handoff do not silently lose logout
endpoint validation when they choose manual SAML entry instead of metadata
import.
+That same shared `internal/api/` dependency also now assumes post-auth browser
+handoff stays on one canonical local redirect builder. Lifecycle-adjacent setup
+and hosted bootstrap surfaces may depend on shared OIDC/SAML callbacks, but
+they must not reintroduce per-handler `returnTo` shaping that can bypass the
+governed local-path validation before success or error query markers are added.
Agent lifecycle owns the install/register/update continuity surfaces, but it
does not own unified-resource history or control-plane timeline persistence.
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 173b2960a9..2c502fd472 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -343,6 +343,11 @@ That same SSO boundary also owns manual SAML endpoint validation payloads.
through the same validated absolute HTTP(S) helpers instead of letting the
manual logout URL drift out of the request model or bypass the governed URL
normalization path.
+That same SSO API boundary also owns final browser redirect construction after
+local auth handoff. OIDC and SAML success/error handlers must build their
+local `returnTo` targets through one canonical local-path helper that rejects
+absolute or host-bearing targets before query params are appended, so shared
+identity flows cannot drift back to per-handler open-redirect shaping.
Commercial self-service actions in that shell must stay same-origin as well:
the frontend may only call the portal-owned `/api/portal/commercial/*` routes,
and `internal/cloudcp/portal/commercial_proxy.go` plus `internal/cloudcp/routes.go`
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index b8f260b973..be39b1d651 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -143,6 +143,10 @@ querying, and the operator-facing storage health presentation layer.
handoff. `frontend-modern/src/pages/Dashboard.tsx` may stay storage/recovery-
owned for dashboard composition, but when no resources have reported yet it
must route operators to `/settings/infrastructure/install` instead of
+8. Keep shared OIDC/SAML callback redirects on the canonical local-target helper
+ contract when storage- or recovery-adjacent routes inherit shared auth
+ browser handoff through `internal/api/`, so adjacent surfaces do not revive
+ per-handler absolute-target acceptance or raw `returnTo` concatenation.
leaving the dashboard as a passive dead end.
8. Keep dependent first-session reset behavior honest on the shared `internal/api/`
boundary: when `/api/security/dev/reset-first-run` is used to reopen the
@@ -1210,6 +1214,11 @@ or recover those tokens through encrypted-at-rest session payloads, and any
missing-crypto or invalid-ciphertext path must drop the refresh token instead
of preserving plaintext-at-rest session state that storage and recovery
surfaces might inherit through shared auth runtime helpers.
+That same shared `internal/api/` dependency also assumes shared OIDC/SAML
+callbacks finish on canonical local redirect targets. Storage- or
+recovery-adjacent routes that rely on shared auth helpers may not reintroduce
+per-handler `returnTo` concatenation or absolute-target acceptance when they
+inherit those browser handoff paths through the common API router surface.
That same shared `internal/api/` dependency also assumes notification test
handlers stay decode-and-delegate only: `internal/api/notifications.go` may
share the API helper boundary with storage-adjacent routes, but service-template
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index 724010b669..3e4238dfe1 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -1280,6 +1280,20 @@ func TestContract_SSOTestOIDCDiscoveryKeepsIssuerBasePath(t *testing.T) {
}
}
+func TestContract_SSOLocalRedirectTargetsStayCanonical(t *testing.T) {
+ if got := buildLocalRedirectTarget("https://evil.example.com/pwn", map[string]string{
+ "oidc": "error",
+ }); got != "/?oidc=error" {
+ t.Fatalf("absolute redirect target = %q, want %q", got, "/?oidc=error")
+ }
+
+ if got := buildLocalRedirectTarget("/login?foo=bar#section", map[string]string{
+ "saml": "success",
+ }); got != "/login?foo=bar&saml=success#section" {
+ t.Fatalf("local redirect target = %q, want %q", got, "/login?foo=bar&saml=success#section")
+ }
+}
+
func TestContract_FindingJSONSnapshot(t *testing.T) {
now := time.Date(2026, 2, 8, 13, 14, 15, 0, time.UTC)
lastSeen := now.Add(5 * time.Minute)
diff --git a/internal/api/oidc_handlers.go b/internal/api/oidc_handlers.go
index 39b7496765..35d653beb6 100644
--- a/internal/api/oidc_handlers.go
+++ b/internal/api/oidc_handlers.go
@@ -20,23 +20,24 @@ func sanitizeOIDCReturnTo(raw string) string {
if trimmed == "" {
return ""
}
- if !strings.HasPrefix(trimmed, "/") || strings.HasPrefix(trimmed, "//") {
+ if !strings.HasPrefix(trimmed, "/") {
+ return ""
+ }
+ if len(trimmed) > 1 && (trimmed[1] == '/' || trimmed[1] == '\\') {
+ return ""
+ }
+ parsed, err := url.Parse(trimmed)
+ if err != nil || parsed.IsAbs() || parsed.Host != "" {
return ""
}
return trimmed
}
func (r *Router) redirectOIDCError(w http.ResponseWriter, req *http.Request, returnTo string, code string) {
- target := returnTo
- if target == "" {
- target = "/"
- }
- target = addQueryParam(target, "oidc", "error")
- if code != "" {
- target = addQueryParam(target, "oidc_error", code)
- }
-
- http.Redirect(w, req, target, http.StatusFound)
+ http.Redirect(w, req, buildLocalRedirectTarget(returnTo, map[string]string{
+ "oidc": "error",
+ "oidc_error": code,
+ }), http.StatusFound)
}
func addQueryParam(path, key, value string) string {
@@ -53,6 +54,32 @@ func addQueryParam(path, key, value string) string {
return u.String()
}
+func buildLocalRedirectTarget(returnTo string, queryParams map[string]string) string {
+ target := sanitizeOIDCReturnTo(returnTo)
+ if target == "" {
+ target = "/"
+ }
+
+ parsed, err := url.Parse(target)
+ if err != nil || parsed.IsAbs() || parsed.Host != "" {
+ target = "/"
+ } else if parsed.Path == "" {
+ parsed.Path = "/"
+ target = parsed.String()
+ } else {
+ target = parsed.String()
+ }
+
+ for key, value := range queryParams {
+ if key == "" || value == "" {
+ continue
+ }
+ target = addQueryParam(target, key, value)
+ }
+
+ return target
+}
+
func extractStringClaim(claims map[string]any, key string) string {
if key == "" {
return ""
@@ -582,10 +609,7 @@ func (r *Router) handleSSOOIDCCallback(w http.ResponseWriter, req *http.Request)
LogAuditEventForTenant(GetOrgID(req.Context()), "sso_oidc_login", username, GetClientIP(req), req.URL.Path, true, "SSO OIDC login success via provider: "+providerID)
- target := entry.ReturnTo
- if target == "" {
- target = "/"
- }
- target = addQueryParam(target, "oidc", "success")
- http.Redirect(w, req, target, http.StatusFound)
+ http.Redirect(w, req, buildLocalRedirectTarget(entry.ReturnTo, map[string]string{
+ "oidc": "success",
+ }), http.StatusFound)
}
diff --git a/internal/api/oidc_handlers_test.go b/internal/api/oidc_handlers_test.go
index 2e7cd6add7..4bfe62c112 100644
--- a/internal/api/oidc_handlers_test.go
+++ b/internal/api/oidc_handlers_test.go
@@ -1,7 +1,11 @@
package api
import (
+ "net/http"
+ "net/http/httptest"
"testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
)
func TestSanitizeOIDCReturnTo(t *testing.T) {
@@ -79,6 +83,11 @@ func TestSanitizeOIDCReturnTo(t *testing.T) {
raw: "//evil.com/path",
want: "",
},
+ {
+ name: "backslash relative path",
+ raw: "/\\evil.com/path",
+ want: "",
+ },
// Whitespace handling
{
@@ -110,6 +119,64 @@ func TestSanitizeOIDCReturnTo(t *testing.T) {
}
}
+func TestBuildLocalRedirectTarget(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ returnTo string
+ queryParams map[string]string
+ want string
+ }{
+ {
+ name: "rejects absolute URL",
+ returnTo: "https://evil.example.com/pwn",
+ queryParams: map[string]string{"oidc": "error"},
+ want: "/?oidc=error",
+ },
+ {
+ name: "preserves query and fragment",
+ returnTo: "/login?foo=bar#section",
+ queryParams: map[string]string{"oidc": "success"},
+ want: "/login?foo=bar&oidc=success#section",
+ },
+ {
+ name: "drops empty key and value",
+ returnTo: "/",
+ queryParams: map[string]string{"": "ignored", "oidc": "", "state": "ok"},
+ want: "/?state=ok",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ t.Parallel()
+
+ result := buildLocalRedirectTarget(tc.returnTo, tc.queryParams)
+ if result != tc.want {
+ t.Fatalf("buildLocalRedirectTarget(%q, %#v) = %q, want %q", tc.returnTo, tc.queryParams, result, tc.want)
+ }
+ })
+ }
+}
+
+func TestRedirectOIDCErrorRejectsAbsoluteReturnTo(t *testing.T) {
+ t.Parallel()
+
+ router := &Router{config: &config.Config{}}
+ req := httptest.NewRequest(http.MethodGet, "/api/oidc/callback", nil)
+ rec := httptest.NewRecorder()
+
+ router.redirectOIDCError(rec, req, "https://evil.example.com/pwn", "bad")
+
+ if rec.Code != http.StatusFound {
+ t.Fatalf("status = %d, want %d", rec.Code, http.StatusFound)
+ }
+ if loc := rec.Header().Get("Location"); loc != "/?oidc=error&oidc_error=bad" {
+ t.Fatalf("unexpected redirect location %q", loc)
+ }
+}
+
func TestAddQueryParam(t *testing.T) {
t.Parallel()
diff --git a/internal/api/saml_handlers.go b/internal/api/saml_handlers.go
index bf981c59c6..0ce4aece1d 100644
--- a/internal/api/saml_handlers.go
+++ b/internal/api/saml_handlers.go
@@ -258,13 +258,9 @@ func (r *Router) handleSAMLACS(w http.ResponseWriter, req *http.Request) {
LogAuditEventForTenant(GetOrgID(req.Context()), "saml_login", username, GetClientIP(req), req.URL.Path, true, "SAML login success via "+providerID)
- // Redirect to return URL - sanitize relayState to prevent open redirect
- target := sanitizeOIDCReturnTo(relayState)
- if target == "" {
- target = "/"
- }
- target = addQueryParam(target, "saml", "success")
- http.Redirect(w, req, target, http.StatusFound)
+ http.Redirect(w, req, buildLocalRedirectTarget(relayState, map[string]string{
+ "saml": "success",
+ }), http.StatusFound)
}
// handleSAMLMetadata returns the SP metadata XML
@@ -497,16 +493,10 @@ func (r *Router) clearSession(w http.ResponseWriter, req *http.Request) {
}
func (r *Router) redirectSAMLError(w http.ResponseWriter, req *http.Request, returnTo string, code string) {
- // Sanitize returnTo to prevent open redirect attacks
- target := sanitizeOIDCReturnTo(returnTo)
- if target == "" {
- target = "/"
- }
- target = addQueryParam(target, "saml", "error")
- if code != "" {
- target = addQueryParam(target, "saml_error", code)
- }
- http.Redirect(w, req, target, http.StatusFound)
+ http.Redirect(w, req, buildLocalRedirectTarget(returnTo, map[string]string{
+ "saml": "error",
+ "saml_error": code,
+ }), http.StatusFound)
}
// extractSAMLProviderID extracts the provider ID from a SAML endpoint path
diff --git a/internal/api/saml_handlers_additional_test.go b/internal/api/saml_handlers_additional_test.go
index 02699569fb..863aa26c4e 100644
--- a/internal/api/saml_handlers_additional_test.go
+++ b/internal/api/saml_handlers_additional_test.go
@@ -125,6 +125,18 @@ func TestRedirectSAMLError(t *testing.T) {
}
}
+func TestRedirectSAMLErrorRejectsAbsoluteReturnTo(t *testing.T) {
+ router := &Router{}
+ req := httptest.NewRequest(http.MethodGet, "/", nil)
+ rec := httptest.NewRecorder()
+
+ router.redirectSAMLError(rec, req, "https://evil.example.com/pwn", "session_failed")
+
+ if loc := rec.Header().Get("Location"); loc != "/?saml=error&saml_error=session_failed" {
+ t.Fatalf("unexpected redirect location %q", loc)
+ }
+}
+
func TestInitializeSAMLProviders(t *testing.T) {
provider := testSAMLProvider("okta", true)
provider.SAML = &config.SAMLProviderConfig{