Skip to content

Commit 2f619be

Browse files
benoittgtmattfarina
authored andcommitted
Avoid "panic: interface conversion: interface {} is nil"
Closes: #31202 Signed-off-by: Benoit Tigeot <[email protected]> (cherry picked from commit 2fe49f9)
1 parent 8112d47 commit 2f619be

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

pkg/chartutil/jsonschema.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,20 @@ func ValidateAgainstSchema(chrt *chart.Chart, values map[string]interface{}) err
8282
}
8383

8484
for _, subchart := range chrt.Dependencies() {
85-
subchartValues := values[subchart.Name()].(map[string]interface{})
85+
raw, exists := values[subchart.Name()]
86+
if !exists || raw == nil {
87+
// No values provided for this subchart; nothing to validate
88+
continue
89+
}
90+
91+
subchartValues, ok := raw.(map[string]any)
92+
if !ok {
93+
sb.WriteString(fmt.Sprintf(
94+
"%s:\ninvalid type for values: expected object (map), got %T\n",
95+
subchart.Name(), raw,
96+
))
97+
continue
98+
}
8699
if err := ValidateAgainstSchema(subchart, subchartValues); err != nil {
87100
sb.WriteString(err.Error())
88101
}

pkg/chartutil/jsonschema_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,93 @@ func TestHTTPURLLoader_Load(t *testing.T) {
286286
}
287287
})
288288
}
289+
290+
// Non-regression tests for https://github.com/helm/helm/issues/31202
291+
// Ensure ValidateAgainstSchema does not panic when:
292+
// - subchart key is missing
293+
// - subchart value is nil
294+
// - subchart value has an invalid type
295+
296+
func TestValidateAgainstSchema_MissingSubchartValues_NoPanic(t *testing.T) {
297+
subchartJSON := []byte(subchartSchema)
298+
subchart := &chart.Chart{
299+
Metadata: &chart.Metadata{Name: "subchart"},
300+
Schema: subchartJSON,
301+
}
302+
chrt := &chart.Chart{
303+
Metadata: &chart.Metadata{Name: "chrt"},
304+
}
305+
chrt.AddDependency(subchart)
306+
307+
// No "subchart" key present in values
308+
vals := map[string]any{
309+
"name": "John",
310+
}
311+
312+
defer func() {
313+
if r := recover(); r != nil {
314+
t.Fatalf("ValidateAgainstSchema panicked (missing subchart values): %v", r)
315+
}
316+
}()
317+
318+
if err := ValidateAgainstSchema(chrt, vals); err != nil {
319+
t.Fatalf("expected no error when subchart values are missing, got: %v", err)
320+
}
321+
}
322+
323+
func TestValidateAgainstSchema_SubchartNil_NoPanic(t *testing.T) {
324+
subchartJSON := []byte(subchartSchema)
325+
subchart := &chart.Chart{
326+
Metadata: &chart.Metadata{Name: "subchart"},
327+
Schema: subchartJSON,
328+
}
329+
chrt := &chart.Chart{
330+
Metadata: &chart.Metadata{Name: "chrt"},
331+
}
332+
chrt.AddDependency(subchart)
333+
334+
// "subchart" key present but nil
335+
vals := map[string]any{
336+
"name": "John",
337+
"subchart": nil,
338+
}
339+
340+
defer func() {
341+
if r := recover(); r != nil {
342+
t.Fatalf("ValidateAgainstSchema panicked (nil subchart values): %v", r)
343+
}
344+
}()
345+
346+
if err := ValidateAgainstSchema(chrt, vals); err != nil {
347+
t.Fatalf("expected no error when subchart values are nil, got: %v", err)
348+
}
349+
}
350+
351+
func TestValidateAgainstSchema_InvalidSubchartValuesType_NoPanic(t *testing.T) {
352+
subchartJSON := []byte(subchartSchema)
353+
subchart := &chart.Chart{
354+
Metadata: &chart.Metadata{Name: "subchart"},
355+
Schema: subchartJSON,
356+
}
357+
chrt := &chart.Chart{
358+
Metadata: &chart.Metadata{Name: "chrt"},
359+
}
360+
chrt.AddDependency(subchart)
361+
362+
// "subchart" is the wrong type (string instead of map)
363+
vals := map[string]any{
364+
"name": "John",
365+
"subchart": "oops",
366+
}
367+
368+
defer func() {
369+
if r := recover(); r != nil {
370+
t.Fatalf("ValidateAgainstSchema panicked (invalid subchart values type): %v", r)
371+
}
372+
}()
373+
374+
// We expect a non-nil error (invalid type), but crucially no panic.
375+
if err := ValidateAgainstSchema(chrt, vals); err == nil {
376+
t.Fatalf("expected an error when subchart values have invalid type, got nil")
377+
}
378+
}

0 commit comments

Comments
 (0)