VaultSharp.Extensions.Configuration icon indicating copy to clipboard operation
VaultSharp.Extensions.Configuration copied to clipboard

NullReferenceException when Vault secret contains null values in keys

Open MarcelaCarvalho opened this issue 4 months ago • 0 comments

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:

  1. Store a secret in Vault with a key that has a null value. For example:
{
  "Key1": null
}
  1. Configure VaultSharp.Extensions.Configuration in a .NET app to load this secret.
  2. Attempt to read the configuration via IConfiguration, or bind it to a strongly-typed class.
  3. 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.

MarcelaCarvalho avatar Sep 15 '25 19:09 MarcelaCarvalho