Potential nil pointer dereference in ClientConfig
Hi, I analyzed the source code using Svace and found an interesting bug.
Problem
In ClientConfig(), the cert variable is initially nil and is assigned a value only when CertFile or KeyFile is provided. https://github.com/helm/helm/blob/144ca65f8501953fa8b41cd1d37c7223051c85b7/internal/tlsutil/cfg.go#L42
If no client certificate is configured, cert remains nil. However, the code later unconditionally dereferences it:
cfg = &tls.Config{ InsecureSkipVerify: opts.InsecureSkipVerify, Certificates: []tls.Certificate{*cert}, RootCAs: pool, }
This dereferences a nil pointer and may result in a panic. https://github.com/helm/helm/blob/144ca65f8501953fa8b41cd1d37c7223051c85b7/internal/tlsutil/cfg.go#L56
At the same time, operating without a client certificate is a valid TLS client configuration. A client certificate is only required when mutual TLS (mTLS) authentication is used.
Fix
The cert pointer should only be dereferenced when it is non-nil. The certificate can be added to cfg.Certificates conditionally:
cfg = &tls.Config{ InsecureSkipVerify: opts.InsecureSkipVerify, RootCAs: pool, }
if cert != nil { cfg.Certificates = []tls.Certificate{*cert} }
return cfg, nil
This allows the TLS client to operate correctly without a client certificate while still using the certificate when one is configured.
Thanks for reporting this! I've submitted a fix targeting the dev-v3 branch with a regression test: #32679.