fix: handle null values in Vault secrets to prevent NullReference#71
fix: handle null values in Vault secrets to prevent NullReference#71MarcelaCarvalho wants to merge 1 commit intoMrZoidberg:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a NullReferenceException that occurred when Vault secrets contained keys with null values. The issue was that the code attempted to cast a null value directly to JsonElement without proper null checking, causing the application to crash during configuration loading.
Changes:
- Added null value handling in the SetData method to properly create JsonElement instances representing JSON null values
- Used JsonDocument.Parse("null") with Clone() to create a standalone JsonElement with JsonValueKind.Null that can be safely processed by SetItemData
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| JsonElement nestedValue; | ||
|
|
||
| if (pair.Value == null) | ||
| { | ||
| using var doc = JsonDocument.Parse("null"); | ||
| nestedValue = doc.RootElement.Clone(); | ||
| } | ||
| else | ||
| { | ||
| nestedValue = (JsonElement)(object)pair.Value!; | ||
| } |
There was a problem hiding this comment.
This fix correctly handles null values from Vault secrets and properly creates a JsonElement representing a JSON null value. The use of JsonDocument.Parse with a using statement combined with Clone() is the correct pattern to create a JsonElement that outlives the JsonDocument's disposal. However, there is no test coverage for this specific scenario. Consider adding a test case that verifies null values in Vault secrets are properly handled and can be retrieved from IConfiguration.
|
done in #74 with tests. thanks for contributing! |
This PR fixes an issue where the configuration provider throws a NullReferenceException when Vault secrets contain keys with null values. The root cause was an invalid cast of a null value to JsonElement.
The fix adds a null check and creates a JsonElement representing a JSON null (JsonValueKind.Null) when the value is null. This ensures that:
Keys with null values are properly included in the configuration.
No exceptions are thrown during configuration loading.
Consumers of IConfiguration can safely detect and handle keys with null values.
Issue: NullReferenceException when Vault secret contains null values in keys #70