Skip to content

Commit af059d8

Browse files
committed
Add null safety checks for Azure SDK responses
- Add null checks for certificate.Value.Cer in GetCertificateAsync - Add null checks for certificate.Value.Value in GetCertificateWithPrivateKeyAsync - Prevents NullReferenceException when mocked clients return null responses - Improves defensive programming for edge cases
1 parent 361c96d commit af059d8

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/LettuceEncrypt.Azure/Internal/AzureKeyVaultCertificateRepository.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public async Task<IEnumerable<X509Certificate2>> GetCertificatesAsync(Cancellati
6363

6464
var certificate = await certificateClient.GetCertificateAsync(normalizedName, token);
6565

66+
if (certificate?.Value?.Cer == null)
67+
{
68+
_logger.LogWarning("Certificate response for {domainName} was null or empty", domainName);
69+
return null;
70+
}
71+
6672
return new X509Certificate2(certificate.Value.Cer);
6773
}
6874
catch (RequestFailedException ex) when (ex.Status == 404)
@@ -95,6 +101,12 @@ public async Task<IEnumerable<X509Certificate2>> GetCertificatesAsync(Cancellati
95101

96102
var certificate = await secretClient.GetSecretAsync(normalizedName, null, token);
97103

104+
if (certificate?.Value?.Value == null)
105+
{
106+
_logger.LogWarning("Certificate secret for {domainName} was null or empty", domainName);
107+
return null;
108+
}
109+
98110
var certBytes = Convert.FromBase64String(certificate.Value.Value);
99111
var cert = new X509Certificate2(certBytes, (string?)null, X509KeyStorageFlags.Exportable);
100112

0 commit comments

Comments
 (0)