fix(install): add nil check for chart in RunWithContext

Prevent nil pointer dereference when a nil chart is passed to
Install.Run or Install.RunWithContext. Instead of crashing with
a SIGSEGV, return an error with a descriptive message.

Add the same nil guard in renderResources for defense in depth.

Fixes #31552

Signed-off-by: efadeev <efadeev@users.noreply.github.com>
Signed-off-by: Evgenii Fadeev <fes94@ya.ru>
pull/32473/head
Evgenii Fadeev 2 months ago
parent 8eb65528be
commit 619c610a25

@ -280,6 +280,10 @@ func (cfg *Configuration) renderResources(ctx context.Context, ch *chart.Chart,
var hs []*release.Hook var hs []*release.Hook
b := bytes.NewBuffer(nil) b := bytes.NewBuffer(nil)
if ch == nil {
return hs, b, "", errors.New("chart is nil")
}
caps, err := cfg.getCapabilities() caps, err := cfg.getCapabilities()
if err != nil { if err != nil {
return hs, b, "", err return hs, b, "", err

@ -291,6 +291,9 @@ func (i *Install) RunWithContext(ctx context.Context, ch ci.Charter, vals map[st
default: default:
return nil, errors.New("invalid chart apiVersion") return nil, errors.New("invalid chart apiVersion")
} }
if chrt == nil {
return nil, errors.New("chart is nil")
}
if interactWithServer(i.DryRunStrategy) { if interactWithServer(i.DryRunStrategy) {
if err := i.cfg.KubeClient.IsReachable(); err != nil { if err := i.cfg.KubeClient.IsReachable(); err != nil {

@ -1245,3 +1245,16 @@ func TestInstallRelease_WaitOptionsPassedDownstream(t *testing.T) {
// Verify that WaitOptions were passed to GetWaiter // Verify that WaitOptions were passed to GetWaiter
is.NotEmpty(failer.RecordedWaitOptions, "WaitOptions should be passed to GetWaiter") is.NotEmpty(failer.RecordedWaitOptions, "WaitOptions should be passed to GetWaiter")
} }
func TestInstallRun_NilChart(t *testing.T) {
is := assert.New(t)
instAction := installAction(t)
instAction.DryRunStrategy = DryRunClient
vals := map[string]any{}
// Pass a typed nil *chart.Chart as the ci.Charter interface
_, err := instAction.Run((*chart.Chart)(nil), vals)
is.Error(err)
is.Contains(err.Error(), "chart is nil")
}

Loading…
Cancel
Save