Skip to content

NullReferenceException when Vault secret contains null values in keys #70

@MarcelaCarvalho

Description

@MarcelaCarvalho

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions