-
Notifications
You must be signed in to change notification settings - Fork 22
Description
Describe the bug
When a Vault secret contains keys with null values, the configuration provider throws a NullReferenceException. This happens because the code attempts to cast a null value directly to JsonElement without checking for null, causing the failure.
To Reproduce
A link to some code to reproduce the bug can speed up a fix. Alternatively, show steps to reproduce the behaviour:
- Store a secret in Vault with a key that has a null value. For example:
{
"Key1": null
}
- Configure VaultSharp.Extensions.Configuration in a .NET app to load this secret.
- Attempt to read the configuration via IConfiguration, or bind it to a strongly-typed class.
- The application throws a NullReferenceException at the provider layer, specifically in the SetData method when casting null to JsonElement, with message: "Object reference not set to an instance of an object"
Problematic code snippet:
var nestedValue = (JsonElement)(object)pair.Value!;
Expected behaviour
-
The configuration provider should gracefully handle keys with null values from Vault secrets without throwing exceptions.
-
Keys with null values should appear in the IConfiguration with their corresponding keys present and values represented as null (e.g., a JsonElement with JsonValueKind.Null).
-
Consuming code should be able to retrieve these keys and detect that their value is null without causing any runtime errors.
-
This behavior should enable applications to safely bind configuration classes or access individual keys even when Vault returns null values.