Skip to content

Commit

Permalink
fix: DecodeCertificates cert length check (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
susanshi authored May 14, 2024
1 parent 3c0ccac commit 68c93a6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
3 changes: 3 additions & 0 deletions pkg/certificateprovider/certificate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@ func DecodeCertificates(value []byte) ([]*x509.Certificate, error) {
}
}

if len(certs) == 0 {
return nil, errors.ErrorCodeCertInvalid.WithComponentType(errors.CertProvider).WithDetail("no certificates found in the pem block")
}
return certs, nil
}
7 changes: 6 additions & 1 deletion pkg/certificateprovider/certificate_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ func TestDecodeCertificates(t *testing.T) {
}{
{
desc: "empty string",
expectedErr: false,
expectedErr: true,
},
{
desc: "invalid certificate",
pemString: "-----BEGIN CERTIFICATE-----\nbaddata\n-----END CERTIFICATE-----\n",
expectedErr: true,
},
{
desc: "invalid certificate",
pemString: "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAweAc4xikYT4ZszXVdF5mrgP0zKVYi4Ces0py9dw8XZfh/Hlxb5xWMs4DzTcKwmLatgKNSrvNyOaxkBD90PvcYNaTCwzwQ09kZ5dYtVOV4sdzeyOj8UDtf4MF5eJgJj/wWCQJnWrX/4n6nSdNTXSJEFAZkDv0BKVkZekJHn3fh+pOuv8UtvOrY1NjNK/TLWxB+8xpwugeB9oZ+VgV/gHZBLprxYkmUDsfngYy3+r6RZ+hInalZc5uAbtRUoB8+nVhXXOe3iVcVWFoWPMJ2fuPHz/8cDjv02MNWa/MeAt+ItW3N+VFZNkwbu5en3FepsxzRl04rhZzr1DSX6V6CVX43wIDAQAB-----END PUBLIC KEY-----",
expectedErr: true,
},
{
desc: "single certificate",
pemString: "-----BEGIN CERTIFICATE-----\nMIID2jCCAsKgAwIBAgIQX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pVimd0aYv6mJfOieRILBF4XFomjhrJOLI55oVwLN/AgX6kuC3CJY2NMyJKlTao9oZgpHhs\nLlxB/r0n9JnUoN0Gq93oc1+OLFjPI7gNuPXYOP1N46oKgEmAEmNkP1etFrEjFRgsdIFHksrmlOlD\nIed9RcQ087VLjmuymLgqMTFX34Q3j7XgN2ENwBSnkHotE9CcuGRW+NuiOeJalL8DBmFXXWwHTKLQ\nPp5g6m1yZXylLJaFLKz7tdMmO355\n-----END CERTIFICATE-----\n",
Expand Down
4 changes: 2 additions & 2 deletions pkg/keymanagementprovider/inline/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,15 @@ func (f *inlineKMProviderFactory) Create(_ string, keyManagementProviderConfig c
case certificateContentType:
certs, err := keymanagementprovider.DecodeCertificates([]byte(conf.Value))
if err != nil {
return nil, errors.ErrorCodeCertInvalid.WithComponentType(errors.KeyManagementProvider)
return nil, err
}
certMap = map[keymanagementprovider.KMPMapKey][]*x509.Certificate{
{}: certs,
}
case keyContentType:
key, err := keymanagementprovider.DecodeKey([]byte(conf.Value))
if err != nil {
return nil, errors.ErrorCodeKeyInvalid.WithComponentType(errors.KeyManagementProvider)
return nil, err
}
keyMap = map[keymanagementprovider.KMPMapKey]crypto.PublicKey{
{}: key,
Expand Down
4 changes: 4 additions & 0 deletions pkg/keymanagementprovider/keymanagementprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func DecodeCertificates(value []byte) ([]*x509.Certificate, error) {
}
}

if len(certs) == 0 {
return nil, errors.ErrorCodeCertInvalid.WithComponentType(errors.CertProvider).WithDetail("no certificates found in the pem block")
}

return certs, nil
}

Expand Down
7 changes: 6 additions & 1 deletion pkg/keymanagementprovider/keymanagementprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ func TestDecodeCertificates(t *testing.T) {
}{
{
desc: "empty string",
expectedErr: false,
expectedErr: true,
},
{
desc: "invalid certificate",
pemString: "-----BEGIN CERTIFICATE-----\nbaddata\n-----END CERTIFICATE-----\n",
expectedErr: true,
},
{
desc: "invalid certificate",
pemString: "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAweAc4xikYT4ZszXVdF5mrgP0zKVYi4Ces0py9dw8XZfh/Hlxb5xWMs4DzTcKwmLatgKNSrvNyOaxkBD90PvcYNaTCwzwQ09kZ5dYtVOV4sdzeyOj8UDtf4MF5eJgJj/wWCQJnWrX/4n6nSdNTXSJEFAZkDv0BKVkZekJHn3fh+pOuv8UtvOrY1NjNK/TLWxB+8xpwugeB9oZ+VgV/gHZBLprxYkmUDsfngYy3+r6RZ+hInalZc5uAbtRUoB8+nVhXXOe3iVcVWFoWPMJ2fuPHz/8cDjv02MNWa/MeAt+ItW3N+VFZNkwbu5en3FepsxzRl04rhZzr1DSX6V6CVX43wIDAQAB-----END PUBLIC KEY-----",
expectedErr: true,
},
{
desc: "single certificate",
pemString: "-----BEGIN CERTIFICATE-----\nMIID2jCCAsKgAwIBAgIQXy2VqtlhSkiZKAGhsnkjbDANBgkqhkiG9w0BAQsFADBvMRswGQYDVQQD\nExJyYXRpZnkuZXhhbXBsZS5jb20xDzANBgNVBAsTBk15IE9yZzETMBEGA1UEChMKTXkgQ29tcGFu\neTEQMA4GA1UEBxMHUmVkbW9uZDELMAkGA1UECBMCV0ExCzAJBgNVBAYTAlVTMB4XDTIzMDIwMTIy\nNDUwMFoXDTI0MDIwMTIyNTUwMFowbzEbMBkGA1UEAxMScmF0aWZ5LmV4YW1wbGUuY29tMQ8wDQYD\nVQQLEwZNeSBPcmcxEzARBgNVBAoTCk15IENvbXBhbnkxEDAOBgNVBAcTB1JlZG1vbmQxCzAJBgNV\nBAgTAldBMQswCQYDVQQGEwJVUzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL10bM81\npPAyuraORABsOGS8M76Bi7Guwa3JlM1g2D8CuzSfSTaaT6apy9GsccxUvXd5cmiP1ffna5z+EFmc\nizFQh2aq9kWKWXDvKFXzpQuhyqD1HeVlRlF+V0AfZPvGt3VwUUjNycoUU44ctCWmcUQP/KShZev3\n6SOsJ9q7KLjxxQLsUc4mg55eZUThu8mGB8jugtjsnLUYvIWfHhyjVpGrGVrdkDMoMn+u33scOmrt\nsBljvq9WVo4T/VrTDuiOYlAJFMUae2Ptvo0go8XTN3OjLblKeiK4C+jMn9Dk33oGIT9pmX0vrDJV\nX56w/2SejC1AxCPchHaMuhlwMpftBGkCAwEAAaNyMHAwDgYDVR0PAQH/BAQDAgeAMAkGA1UdEwQC\nMAAwEwYDVR0lBAwwCgYIKwYBBQUHAwMwHwYDVR0jBBgwFoAU0eaKkZj+MS9jCp9Dg1zdv3v/aKww\nHQYDVR0OBBYEFNHmipGY/jEvYwqfQ4Nc3b97/2isMA0GCSqGSIb3DQEBCwUAA4IBAQBNDcmSBizF\nmpJlD8EgNcUCy5tz7W3+AAhEbA3vsHP4D/UyV3UgcESx+L+Nye5uDYtTVm3lQejs3erN2BjW+ds+\nXFnpU/pVimd0aYv6mJfOieRILBF4XFomjhrJOLI55oVwLN/AgX6kuC3CJY2NMyJKlTao9oZgpHhs\nLlxB/r0n9JnUoN0Gq93oc1+OLFjPI7gNuPXYOP1N46oKgEmAEmNkP1etFrEjFRgsdIFHksrmlOlD\nIed9RcQ087VLjmuymLgqMTFX34Q3j7XgN2ENwBSnkHotE9CcuGRW+NuiOeJalL8DBmFXXWwHTKLQ\nPp5g6m1yZXylLJaFLKz7tdMmO355\n-----END CERTIFICATE-----\n",
Expand Down

0 comments on commit 68c93a6

Please sign in to comment.