From 2ed283d2461848b8b547203ce488f9c30062e8f3 Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:32:12 +0530 Subject: [PATCH 1/3] Fix custom profile path and label type validation --- changes/54690-validate-custom-settings-types | 1 + cmd/fleetctl/fleetctl/apply_test.go | 50 ++++++++++++++++++ server/fleet/app.go | 53 ++++++++++++++------ server/fleet/app_test.go | 44 +++++++++++++++- 4 files changed, 130 insertions(+), 18 deletions(-) create mode 100644 changes/54690-validate-custom-settings-types diff --git a/changes/54690-validate-custom-settings-types b/changes/54690-validate-custom-settings-types new file mode 100644 index 00000000000..71502f4bb6c --- /dev/null +++ b/changes/54690-validate-custom-settings-types @@ -0,0 +1 @@ +Fixed `fleetctl apply` silently dropping incorrectly typed paths and labels in fleet custom profile settings. Invalid values now return a validation error. diff --git a/cmd/fleetctl/fleetctl/apply_test.go b/cmd/fleetctl/fleetctl/apply_test.go index b9452790a2c..a52993de436 100644 --- a/cmd/fleetctl/fleetctl/apply_test.go +++ b/cmd/fleetctl/fleetctl/apply_test.go @@ -4204,6 +4204,56 @@ spec: `, wantErr: `400 Bad Request: invalid value type at 'macos_settings.enable_disk_encryption': expected bool but got float64`, }, + { + desc: "team custom_settings invalid path", + spec: ` +apiVersion: v1 +kind: fleet +spec: + team: + name: team1 + mdm: + apple_settings: + custom_settings: + - path: 123 +`, + wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.", + wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.path': expected string but got float64`, + }, + { + desc: "team custom_settings invalid labels", + spec: ` +apiVersion: v1 +kind: fleet +spec: + team: + name: team1 + mdm: + apple_settings: + custom_settings: + - path: a + labels: Engineering +`, + wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.", + wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.labels': expected array of strings but got string`, + }, + { + desc: "team custom_settings invalid label element", + spec: ` +apiVersion: v1 +kind: fleet +spec: + team: + name: team1 + mdm: + apple_settings: + custom_settings: + - path: a + labels_include_any: [1, Engineering] +`, + wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.", + wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.labels_include_any': expected string but got float64`, + }, { desc: "team config macos_settings.enable_disk_encryption true", spec: ` diff --git a/server/fleet/app.go b/server/fleet/app.go index eda92593eee..d4406dbd555 100644 --- a/server/fleet/app.go +++ b/server/fleet/app.go @@ -897,20 +897,34 @@ var _ WithMDMProfileSpecs = MacOSSettings{} func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, error) { set := make(map[string]bool) - extractLabelField := func(parentMap map[string]interface{}, fieldName string) []string { + extractLabelField := func(parentMap map[string]interface{}, fieldName string) ([]string, error) { + v, ok := parentMap[fieldName] + if !ok || v == nil { + return nil, nil + } + labels, ok := v.([]interface{}) + if !ok { + return nil, &json.UnmarshalTypeError{ + Value: fmt.Sprintf("%T", v), + Type: reflect.TypeFor[[]string](), + Field: "macos_settings.custom_settings." + fieldName, + } + } var ret []string - if labels, ok := parentMap[fieldName].([]interface{}); ok { - for _, label := range labels { - if strLabel, ok := label.(string); ok { - ret = append(ret, strLabel) + for _, label := range labels { + strLabel, ok := label.(string) + if !ok { + return nil, &json.UnmarshalTypeError{ + Value: fmt.Sprintf("%T", label), + Type: reflect.TypeFor[string](), + Field: "macos_settings.custom_settings." + fieldName, } } + ret = append(ret, strLabel) } - return ret + return ret, nil } - // unlike a bad label, a wrong type here is an error: dropping it would - // clear the stored name or description extractStringField := func(parentMap map[string]any, fieldName string) (string, error) { v, ok := parentMap[fieldName] if !ok || v == nil { @@ -936,11 +950,10 @@ func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, erro for _, v := range vals { if m, ok := v.(map[string]interface{}); ok { var spec MDMProfileSpec - // extract the Path field - if path, ok := m["path"].(string); ok { - spec.Path = path - } var err error + if spec.Path, err = extractStringField(m, "path"); err != nil { + return nil, err + } if spec.Name, err = extractStringField(m, "name"); err != nil { return nil, err } @@ -948,10 +961,18 @@ func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, erro return nil, err } - spec.Labels = extractLabelField(m, "labels") - spec.LabelsIncludeAll = extractLabelField(m, "labels_include_all") - spec.LabelsExcludeAny = extractLabelField(m, "labels_exclude_any") - spec.LabelsIncludeAny = extractLabelField(m, "labels_include_any") + if spec.Labels, err = extractLabelField(m, "labels"); err != nil { + return nil, err + } + if spec.LabelsIncludeAll, err = extractLabelField(m, "labels_include_all"); err != nil { + return nil, err + } + if spec.LabelsExcludeAny, err = extractLabelField(m, "labels_exclude_any"); err != nil { + return nil, err + } + if spec.LabelsIncludeAny, err = extractLabelField(m, "labels_include_any"); err != nil { + return nil, err + } csSpecs = append(csSpecs, spec) } else if m, ok := v.(string); ok { // for backwards compatibility with the old way to define profiles diff --git a/server/fleet/app_test.go b/server/fleet/app_test.go index 64e609b36de..e3a1e0edaa6 100644 --- a/server/fleet/app_test.go +++ b/server/fleet/app_test.go @@ -1392,17 +1392,22 @@ func TestMacOSSettingsFromMapCustomSettings(t *testing.T) { // null is the same as omitted, but any other non-string is rejected require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [ - {"path": "a", "name": null, "description": null} + {"path": "a", "name": null, "description": null}, + {"path": null}, + {} ]}`), &raw)) _, err = s.FromMap(raw) require.NoError(t, err) - require.Equal(t, []MDMProfileSpec{{Path: "a"}}, s.CustomSettings) + require.Equal(t, []MDMProfileSpec{{Path: "a"}, {}, {}}, s.CustomSettings) for _, tc := range []struct{ field, value string }{ {"description", `123`}, {"description", `{"a": "b"}`}, {"name", `true`}, {"name", `["x"]`}, + {"path", `123`}, + {"path", `true`}, + {"path", `["x"]`}, } { require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [{"path": "a", "`+ tc.field+`": `+tc.value+`}]}`), &raw)) @@ -1410,3 +1415,38 @@ func TestMacOSSettingsFromMapCustomSettings(t *testing.T) { require.ErrorContains(t, err, "macos_settings.custom_settings."+tc.field+" of type string", tc.value) } } + +func TestMacOSSettingsFromMapCustomSettingsLabels(t *testing.T) { + for _, field := range []string{"labels", "labels_include_all", "labels_include_any", "labels_exclude_any"} { + t.Run(field, func(t *testing.T) { + for _, value := range []string{`"Engineering"`, `123`, `{}`, `[1, "Engineering"]`, `["Engineering", false]`, `[null]`} { + t.Run(value, func(t *testing.T) { + var raw map[string]any + require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [{"path": "a", "`+field+`": `+value+`}]}`), &raw)) + s := MacOSSettings{CustomSettings: []MDMProfileSpec{{Path: "existing"}}} + _, err := s.FromMap(raw) + var typeErr *json.UnmarshalTypeError + require.ErrorAs(t, err, &typeErr) + require.Equal(t, "macos_settings.custom_settings."+field, typeErr.Field) + require.Equal(t, []MDMProfileSpec{{Path: "existing"}}, s.CustomSettings) + }) + } + for _, value := range []string{`null`, `[]`, `["Engineering", "Sales"]`} { + t.Run(value, func(t *testing.T) { + input := `{"custom_settings": [{"path": "a", "` + field + `": ` + value + `}]}` + var raw map[string]any + require.NoError(t, json.Unmarshal([]byte(input), &raw)) + var want, got MacOSSettings + require.NoError(t, json.Unmarshal([]byte(input), &want)) + if value == `[]` { + want.CustomSettings = []MDMProfileSpec{{Path: "a"}} + } + _, err := got.FromMap(raw) + require.NoError(t, err) + require.Len(t, got.CustomSettings, 1) + require.Equal(t, want.CustomSettings, got.CustomSettings) + }) + } + }) + } +} From 589433054e88979fb4f520880a8748ecc7c69e69 Mon Sep 17 00:00:00 2001 From: Prajwal2246 <213174436+Prajwal2246@users.noreply.github.com> Date: Thu, 8 Oct 2026 16:25:19 +0530 Subject: [PATCH 2/3] Use any in custom profile label validation --- server/fleet/app.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/fleet/app.go b/server/fleet/app.go index d4406dbd555..55cf41de0a4 100644 --- a/server/fleet/app.go +++ b/server/fleet/app.go @@ -897,12 +897,12 @@ var _ WithMDMProfileSpecs = MacOSSettings{} func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, error) { set := make(map[string]bool) - extractLabelField := func(parentMap map[string]interface{}, fieldName string) ([]string, error) { + extractLabelField := func(parentMap map[string]any, fieldName string) ([]string, error) { v, ok := parentMap[fieldName] if !ok || v == nil { return nil, nil } - labels, ok := v.([]interface{}) + labels, ok := v.([]any) if !ok { return nil, &json.UnmarshalTypeError{ Value: fmt.Sprintf("%T", v), From 399b49a884b688c3ff8110ff47006777bfeac748 Mon Sep 17 00:00:00 2001 From: Rajendra Kadam Date: Thu, 8 Oct 2026 18:57:48 +0530 Subject: [PATCH 3/3] Update changes/54690-validate-custom-settings-types --- changes/54690-validate-custom-settings-types | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changes/54690-validate-custom-settings-types b/changes/54690-validate-custom-settings-types index 71502f4bb6c..176f9fe2e3c 100644 --- a/changes/54690-validate-custom-settings-types +++ b/changes/54690-validate-custom-settings-types @@ -1 +1 @@ -Fixed `fleetctl apply` silently dropping incorrectly typed paths and labels in fleet custom profile settings. Invalid values now return a validation error. +- Fixed `fleetctl apply` silently dropping incorrectly typed paths and labels in fleet custom profile settings. Invalid values now return a validation error.