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.

Keep SSO provider labels on complete UTF-8 boundaries

pulse-triage[bot]d809f91de8e5
diff --git a/docs/release-control/v6/internal/subsystems/registry.json b/docs/release-control/v6/internal/subsystems/registry.json
index bd63a4b7f1..d8ffa14f12 100644
--- a/docs/release-control/v6/internal/subsystems/registry.json
+++ b/docs/release-control/v6/internal/subsystems/registry.json
@@ -2838,6 +2838,24 @@
"exact_files": [],
"require_explicit_path_policy_coverage": true,
"path_policies": [
+ {
+ "id": "sso-provider-management",
+ "label": "SSO provider label, CRUD and runtime-preservation proof",
+ "match_prefixes": [],
+ "match_files": [
+ "internal/api/identity_sso_handlers.go"
+ ],
+ "allow_same_subsystem_tests": false,
+ "test_prefixes": [],
+ "exact_files": [
+ "internal/api/sso_provider_name_test.go",
+ "internal/api/sso_handlers_crud_test.go",
+ "internal/api/sso_presentation_update_test.go",
+ "internal/api/sso_config_preservation_test.go",
+ "internal/api/sso_admin_license_gating_test.go",
+ "internal/api/sso_handlers_test.go"
+ ]
+ },
{
"id": "reporting-activity",
"label": "reporting aggregate outcome proof",
diff --git a/internal/api/identity_sso_handlers.go b/internal/api/identity_sso_handlers.go
index 3ba2abcdab..cca2514d66 100644
--- a/internal/api/identity_sso_handlers.go
+++ b/internal/api/identity_sso_handlers.go
@@ -14,6 +14,7 @@ import (
"regexp"
"strings"
"time"
+ "unicode/utf8"
"github.com/crewjam/saml"
"github.com/google/uuid"
@@ -45,7 +46,13 @@ func validateProviderID(id string) bool {
func sanitizeProviderName(name string) string {
name = strings.TrimSpace(name)
if len(name) > maxProviderNameLength {
- name = name[:maxProviderNameLength]
+ // Keep the byte limit, without cutting a valid code point and
+ // turning its incomplete suffix into replacement characters below.
+ end := maxProviderNameLength
+ for end > 0 && !utf8.RuneStart(name[end]) {
+ end--
+ }
+ name = name[:end]
}
// Remove control characters
name = strings.Map(func(r rune) rune {
diff --git a/internal/api/sso_provider_name_test.go b/internal/api/sso_provider_name_test.go
new file mode 100644
index 0000000000..5362b8930c
--- /dev/null
+++ b/internal/api/sso_provider_name_test.go
@@ -0,0 +1,176 @@
+package api
+
+import (
+ "bytes"
+ "encoding/json"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/config"
+ "github.com/stretchr/testify/require"
+)
+
+func TestSSOProviderNameUTF8Boundary(t *testing.T) {
+ for _, tt := range []struct {
+ name, input, want string
+ }{
+ {"short", "Équipe 東京 🔐", "Équipe 東京 🔐"},
+ {"ascii-exact", strings.Repeat("a", 128), strings.Repeat("a", 128)},
+ {"ascii-over", strings.Repeat("a", 129), strings.Repeat("a", 128)},
+ {"two-byte-exact", strings.Repeat("é", 64), strings.Repeat("é", 64)},
+ {"two-byte-cut", strings.Repeat("a", 127) + "é", strings.Repeat("a", 127)},
+ {"three-byte-cut-one", strings.Repeat("a", 127) + "界", strings.Repeat("a", 127)},
+ {"three-byte-cut-two", strings.Repeat("a", 126) + "界", strings.Repeat("a", 126)},
+ {"four-byte-cut-one", strings.Repeat("a", 127) + "🔐", strings.Repeat("a", 127)},
+ {"four-byte-cut-two", strings.Repeat("a", 126) + "🔐", strings.Repeat("a", 126)},
+ {"four-byte-cut-three", strings.Repeat("a", 125) + "🔐", strings.Repeat("a", 125)},
+ {"mixed-boundary", strings.Repeat("界", 42) + "é-more", strings.Repeat("界", 42) + "é"},
+ {"literal-replacement-kept", strings.Repeat("a", 125) + "\ufffd-more", strings.Repeat("a", 125) + "\ufffd"},
+ {"literal-replacement-cut", strings.Repeat("a", 127) + "\ufffd", strings.Repeat("a", 127)},
+ {"combining-codepoint-cut", strings.Repeat("a", 127) + "\u0301", strings.Repeat("a", 127)},
+ {"trim-before-bound", " \t" + strings.Repeat("界", 43) + "\n", strings.Repeat("界", 42)},
+ {"control-removal", " A\x00B\nC\x7f ", "ABC"},
+ {"no-backfill-after-controls", "a\x00" + strings.Repeat("b", 126) + "é", "a" + strings.Repeat("b", 126)},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ got := sanitizeProviderName(tt.input)
+ require.Equal(t, tt.want, got)
+ require.True(t, utf8.ValidString(got))
+ require.LessOrEqual(t, len(got), maxProviderNameLength, "the limit is bytes, not runes")
+ require.Equal(t, got, sanitizeProviderName(got), "sanitization must be stable on a saved label")
+ })
+ }
+}
+
+// Exercise the registered Community routes and persisted/list/detail/public
+// projections. Providers stay disabled during writes: no real IdP is contacted.
+func TestSSOProviderNameHTTPRoundTrip(t *testing.T) {
+ for _, kind := range []config.SSOProviderType{config.SSOProviderTypeOIDC, config.SSOProviderTypeSAML} {
+ t.Run(string(kind), func(t *testing.T) {
+ resetPersistentAuthStoresForTests()
+ t.Cleanup(resetPersistentAuthStoresForTests)
+ const rawToken = "sso-name-contract-token.12345678"
+ cfg := newTestConfigWithTokens(t, newTokenRecord(t, rawToken, []string{config.ScopeSettingsRead, config.ScopeSettingsWrite}, nil))
+ r := NewRouter(cfg, nil, nil, nil, nil, "test")
+ cleanupTestRouter(t, r)
+ request := func(method, path string, body any, authenticated bool) *httptest.ResponseRecorder {
+ t.Helper()
+ data, err := json.Marshal(body)
+ require.NoError(t, err)
+ req := httptest.NewRequest(method, path, bytes.NewReader(data))
+ req.RemoteAddr = ssoTestIP()
+ req.Header.Set("Content-Type", "application/json")
+ if authenticated {
+ req.Header.Set("X-API-Token", rawToken)
+ }
+ rec := httptest.NewRecorder()
+ r.Handler().ServeHTTP(rec, req)
+ return rec
+ }
+ const collection = "/api/security/sso/providers"
+ for _, tt := range []struct {
+ name, suffix string
+ prefix int
+ }{
+ {"two-byte", "é", 127}, {"three-byte", "界", 126}, {"four-byte", "🔐", 125},
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ p := config.SSOProvider{ID: "name-" + tt.name, Name: strings.Repeat("a", tt.prefix) + tt.suffix, DisplayName: strings.Repeat("界", 42) + "🔒suffix", Type: kind}
+ if kind == config.SSOProviderTypeOIDC {
+ p.OIDC = &config.OIDCProviderConfig{IssuerURL: "https://identity.example.test", ClientID: "synthetic-client", ClientSecret: "synthetic-name-secret", Scopes: []string{"openid", "email"}}
+ } else {
+ p.SAML = &config.SAMLProviderConfig{IDPSSOURL: "https://identity.example.test/sso", SPEntityID: "pulse", SPPrivateKey: "synthetic-name-private-key"}
+ }
+ rec := request(http.MethodPost, collection, p, true)
+ require.Equal(t, http.StatusCreated, rec.Code, rec.Body.String())
+ assertLabels := func(rec *httptest.ResponseRecorder, wantName, wantDisplay string) {
+ t.Helper()
+ require.NotContains(t, rec.Body.String(), "synthetic-name-secret")
+ require.NotContains(t, rec.Body.String(), "synthetic-name-private-key")
+ var got SSOProviderResponse
+ require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
+ require.Equal(t, p.ID, got.ID)
+ require.Equal(t, wantName, got.Name)
+ require.Equal(t, wantDisplay, got.DisplayName)
+ }
+ assertLabels(rec, strings.Repeat("a", tt.prefix), strings.Repeat("界", 42))
+ p.ID = "payload-id-must-not-replace-route-id"
+ p.Name = strings.Repeat("é", 63) + "🔑suffix"
+ p.DisplayName = "企業 " + strings.Repeat("é", 60) + "🔐suffix"
+ p.OIDC, p.SAML = nil, nil // as in a flat list-based save: keep credentials/config
+ pathID := "name-" + tt.name
+ rec = request(http.MethodPut, collection+"/"+pathID, p, true)
+ p.ID = pathID
+ require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
+ wantName, wantDisplay := strings.Repeat("é", 63), "企業 "+strings.Repeat("é", 60)
+ assertLabels(rec, wantName, wantDisplay)
+ rec = request(http.MethodGet, collection+"/"+pathID, nil, true)
+ require.Equal(t, http.StatusOK, rec.Code)
+ assertLabels(rec, wantName, wantDisplay)
+ loaded, err := r.persistence.LoadSSOConfig()
+ require.NoError(t, err)
+ stored := loaded.GetProvider(pathID)
+ require.NotNil(t, stored)
+ require.Equal(t, wantName, stored.Name)
+ require.Equal(t, wantDisplay, stored.DisplayName)
+ if p.Type == config.SSOProviderTypeOIDC {
+ require.Equal(t, "synthetic-name-secret", stored.OIDC.ClientSecret)
+ } else {
+ require.Equal(t, "synthetic-name-private-key", stored.SAML.SPPrivateKey)
+ }
+ // Publish an enabled snapshot only for public discovery. This
+ // does not initialise or claim a working identity provider.
+ applyErr, saveErr := r.commitSSOConfig(func(c *config.SSOConfig) error { c.GetProvider(pathID).Enabled = true; return nil })
+ require.NoError(t, applyErr)
+ require.NoError(t, saveErr)
+ rec = request(http.MethodGet, "/api/security/status", nil, false)
+ require.Equal(t, http.StatusOK, rec.Code)
+ require.NotContains(t, rec.Body.String(), "synthetic-name-secret")
+ require.NotContains(t, rec.Body.String(), "synthetic-name-private-key")
+ var status struct {
+ Providers []SSOProviderResponse `json:"ssoProviders"`
+ }
+ require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &status))
+ require.Len(t, status.Providers, 1)
+ require.Equal(t, wantName, status.Providers[0].Name)
+ require.Equal(t, wantDisplay, status.Providers[0].DisplayName)
+ rec = request(http.MethodGet, collection, nil, true)
+ require.Equal(t, http.StatusOK, rec.Code)
+ var list struct {
+ Providers []SSOProviderResponse `json:"providers"`
+ }
+ require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &list))
+ require.Len(t, list.Providers, 1)
+ require.Equal(t, wantName, list.Providers[0].Name)
+ require.Equal(t, wantDisplay, list.Providers[0].DisplayName)
+ rec = request(http.MethodDelete, collection+"/"+pathID, nil, true)
+ require.Equal(t, http.StatusNoContent, rec.Code)
+ })
+ }
+ })
+ }
+}
+
+func TestSSOProviderNamePresentationKeepsRuntime(t *testing.T) {
+ for _, kind := range []config.SSOProviderType{config.SSOProviderTypeOIDC, config.SSOProviderTypeSAML} {
+ t.Run(string(kind), func(t *testing.T) {
+ f := newPresentationFixture(t, kind)
+ r := f.router
+ oidc, saml := r.oidcManager.GetService(f.provider.ID), r.samlManager.GetService(f.provider.ID)
+ before := f.requests.Load()
+ p := f.provider
+ p.Name, p.DisplayName = strings.Repeat("界", 42)+"🔐suffix", strings.Repeat("é", 63)+"🔑suffix"
+ rec := presentationPut(t, r, p)
+ require.Equal(t, http.StatusOK, rec.Code)
+ got := r.ensureSSOConfig().GetProvider(p.ID)
+ require.Equal(t, strings.Repeat("界", 42), got.Name)
+ require.Equal(t, strings.Repeat("é", 63), got.DisplayName)
+ require.Equal(t, before, f.requests.Load(), "a label-only save must not refetch IdP metadata")
+ require.Same(t, oidc, r.oidcManager.GetService(p.ID))
+ require.Same(t, saml, r.samlManager.GetService(p.ID))
+ })
+ }
+}
diff --git a/scripts/release_control/subsystem_lookup_test.py b/scripts/release_control/subsystem_lookup_test.py
index 26054ba843..a54f44a46f 100644
--- a/scripts/release_control/subsystem_lookup_test.py
+++ b/scripts/release_control/subsystem_lookup_test.py
@@ -97,6 +97,18 @@ def _contract_reference(contract_path: str, needle: str, runtime_path: str) -> d
class SubsystemLookupTest(unittest.TestCase):
+ def test_sso_provider_management_uses_label_and_runtime_proof(self) -> None:
+ result = lookup_paths(["internal/api/identity_sso_handlers.go"])
+ file_result = result["files"][0]
+ match = next(item for item in file_result["matches"] if item["subsystem"] == "api-contracts")
+ proof = match["verification_requirement"]
+ self.assertEqual(proof["id"], "sso-provider-management")
+ self.assertFalse(proof["allow_same_subsystem_tests"])
+ self.assertEqual(proof["test_prefixes"], [])
+ self.assertIn("internal/api/sso_provider_name_test.go", proof["exact_files"])
+ self.assertIn("internal/api/sso_presentation_update_test.go", proof["exact_files"])
+ self.assertIn("internal/api/sso_config_preservation_test.go", proof["exact_files"])
+
def test_proxmox_update_json_uses_authoritative_wire_proof(self) -> None:
result = lookup_paths(["internal/unifiedresources/proxmox_update_evidence_json.go"])
self.assertEqual(result["unowned_runtime_files"], [])