From c79071d31d7d199d6965078856c56a55790db400 Mon Sep 17 00:00:00 2001 From: Jochen Ehret Date: Wed, 2 Dec 2026 14:54:07 +0100 Subject: [PATCH] Fix panic in manifest diff when env value is not a map CAPI 1.253.0 (cloud_controller_ng PR #5712) redacts sensitive manifest values server-side, sending the env map in the manifest diff as the string "[PRIVATE DATA HIDDEN]". redactDiff assumed any diff whose path ends in "env" carried a map and asserted diff.Value.(map[string]interface{}) unconditionally, so `cf push --redact-env` crashed with "interface conversion: interface {} is string, not map[string]interface {}". Use comma-ok assertions in redactDiff and redactEnvVarsInRawManifest and redact the value directly when it is not a map. Adds regression tests for both paths. https://github.com/cloudfoundry/cloud_controller_ng/pull/5712 --- command/v7/shared/manifest_diff_displayer.go | 29 ++++++++-- .../v7/shared/manifest_diff_displayer_test.go | 54 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/command/v7/shared/manifest_diff_displayer.go b/command/v7/shared/manifest_diff_displayer.go index eaac038ad6a..48e315b8162 100644 --- a/command/v7/shared/manifest_diff_displayer.go +++ b/command/v7/shared/manifest_diff_displayer.go @@ -31,7 +31,16 @@ func redactEnvVarsInRawManifest(rawManifest []byte) ([]byte, error) { for appListIndex, application := range manifestDataStructure.Applications { for appDataIndex, appDataKV := range application { if appDataKV.Key == "env" { - envMapSlice := appDataKV.Value.(yaml.MapSlice) + envMapSlice, ok := appDataKV.Value.(yaml.MapSlice) + if !ok { + // env is present but not a map (e.g. a scalar). Redact the + // value as-is rather than asserting it to be a MapSlice, + // which would panic. + appDataKV.Value = redacted + application[appDataIndex] = appDataKV + manifestDataStructure.Applications[appListIndex] = application + continue + } for envKeyIndex := range envMapSlice { envMapSlice[envKeyIndex].Value = redacted } @@ -168,11 +177,21 @@ func redactDiff(diff resources.Diff) resources.Diff { diff.Was = redacted } if isEnvMap { - cleanMap := diff.Value.(map[string]interface{}) - for index := range cleanMap { - cleanMap[index] = redacted + if cleanMap, ok := diff.Value.(map[string]interface{}); ok { + for index := range cleanMap { + cleanMap[index] = redacted + } + diff.Value = cleanMap + } else { + // The diff path ends in "env" but the value is not a map. + // CAPI 1.253.0 (cloud_controller_ng PR #5712) redacts sensitive + // manifest values server-side and sends the whole env map as the + // string "[PRIVATE DATA HIDDEN]". Redact the value directly rather + // than asserting it to be a map, which would panic. + // https://github.com/cloudfoundry/cloud_controller_ng/pull/5712 + diff.Value = redacted + diff.Was = redacted } - diff.Value = cleanMap } else if envMap, containsEnvInMap := fullMap["env"].(map[string]interface{}); containsMap && containsEnvInMap { for index := range envMap { envMap[index] = redacted diff --git a/command/v7/shared/manifest_diff_displayer_test.go b/command/v7/shared/manifest_diff_displayer_test.go index f9d990911bf..0e9b3aaf390 100644 --- a/command/v7/shared/manifest_diff_displayer_test.go +++ b/command/v7/shared/manifest_diff_displayer_test.go @@ -604,6 +604,60 @@ applications: }) }) + When("the raw manifest has env set to a non-map value", func() { + // Regression test for a panic in redactEnvVarsInRawManifest: + // it asserted the raw manifest's env value to be a yaml.MapSlice + // unconditionally, panicking when env was a scalar. + BeforeEach(func() { + rawManifest = []byte(`--- +applications: +- name: dora + env: somestring`) + diff = resources.ManifestDiff{ + Diffs: []resources.Diff{ + {Op: resources.AddOperation, Path: "/applications/0/name", Value: "dora"}, + }, + } + }) + + It("does not panic and redacts the env value", func() { + Expect(err).NotTo(HaveOccurred()) + Expect(testUI.Out).To(Say(`env: `)) + }) + }) + + When("the diff server redacts env to a scalar string", func() { + // Regression test for a panic in redactDiff: isEnvMap is computed + // via strings.HasSuffix(diff.Path, "env"), so any diff whose path + // ends in "env" takes the branch that unconditionally asserts + // diff.Value.(map[string]interface{}). CAPI 1.253.0 + // (cloud_controller_ng PR #5712, which redacts sensitive manifest + // values server-side) sends the whole env map as the string + // "[PRIVATE DATA HIDDEN]", so that assertion panics with + // "interface conversion: interface {} is string, not map[string]interface {}". + // See https://github.com/cloudfoundry/cloud_controller_ng/pull/5712 + BeforeEach(func() { + rawManifest = []byte(`--- +applications: +- name: dora + env: + a: b`) + diff = resources.ManifestDiff{ + Diffs: []resources.Diff{ + {Op: resources.AddOperation, Path: "/applications/0/env", Value: "[PRIVATE DATA HIDDEN]"}, + }, + } + }) + + It("does not panic and redacts the value", func() { + Expect(err).NotTo(HaveOccurred()) + Expect(testUI.Out).To(Say(` --- + applications: + - name: dora +\+ env: `)) + }) + }) + When("remove", func() { BeforeEach(func() { rawManifest = []byte(`---