Fix nil pointer dereference in license validation (#34116)

Add nil check after pem.Decode() to prevent crash when public key
PEM data is corrupted or invalid. This fixes a panic at license.go:86
that occurred when block was nil.

Also add test case to verify the fix handles corrupted public keys
gracefully without panicking.
This commit is contained in:
Jesse Hallam 2025-10-15 11:03:20 -03:00 committed by GitHub
parent abbf01b9db
commit 366d057a48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View file

@ -82,6 +82,9 @@ func (l *LicenseValidatorImpl) ValidateLicense(signed []byte) (string, error) {
publicKey = testPublicKey
}
block, _ := pem.Decode(publicKey)
if block == nil {
return "", fmt.Errorf("failed to decode public key PEM block for environment %q", model.GetServiceEnvironment())
}
public, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {

View file

@ -92,6 +92,23 @@ func TestValidateLicense(t *testing.T) {
require.Error(t, err)
require.Empty(t, str)
})
t.Run("should handle corrupted public key without panicking", func(t *testing.T) {
os.Setenv("MM_SERVICEENVIRONMENT", model.ServiceEnvironmentTest)
defer os.Unsetenv("MM_SERVICEENVIRONMENT")
mockValidator := &LicenseValidatorImpl{}
originalTestKey := testPublicKey
defer func() { testPublicKey = originalTestKey }()
testPublicKey = []byte("not a valid PEM block")
str, err := mockValidator.ValidateLicense(validTestLicense)
require.Error(t, err)
require.Empty(t, str)
require.Contains(t, err.Error(), "failed to decode public key PEM block")
})
}
func TestGetLicenseFileLocation(t *testing.T) {