Skip to content

[API Proposal]: Replacement of forbidden characters in variable names in EnvironmentVariablesConfigurationProvider #125137

Closed

Description

@svick

Background and motivation

On Unix systems, the names of environment variables are are often limited to just letters, numbers and the underscore. (This is not a limitation of the OS, but of many tools, including bash or Azure App Service.) This makes it impossible to use the EnvironmentVariablesConfigurationProvider to configure components that expect such names in their configuration. In particular, Microsoft.Extensions.Logging expects hierarchical keys that can look like Logging:LogLevel:Microsoft.Hosting, where the last part is a namespace, so it will very often include dots.

EnvironmentVariablesConfigurationProvider already replaces __ (double underscore) with :, so that hierarchical keys could be represented at all. The proposal here is to effectively expand this capability and make it configurable.

API Proposal

namespace Microsoft.Extensions.Configuration.EnvironmentVariables;

public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
    // existing
    // public string? Prefix { get; set; }

    public Func<string, string>? VariableNameTransformation { get; set; }
}

namespace Microsoft.Extensions.Configuration;

public partial static class EnvironmentVariablesExtensions
{
    // existing
    // public static IConfigurationBuilder AddEnvironmentVariables(
    //     this IConfigurationBuilder configurationBuilder);
    // public static IConfigurationBuilder AddEnvironmentVariables(
    //     this IConfigurationBuilder configurationBuilder, string? prefix);
    // public static IConfigurationBuilder AddEnvironmentVariables(
    //     this IConfigurationBuilder builder, Action<EnvironmentVariablesConfigurationSource>? configureSource);

    public static IConfigurationBuilder AddEnvironmentVariables(
        this IConfigurationBuilder configurationBuilder, string? prefix, Func<string, string>? variableNameTransformation);
 }

The built-in __ → : replacement happens after the user-specified transformation.

API Usage

The code to replace three underscores with a dot would be:

configurationBuilder.AddEnvironmentVariables(null, name => name.Replace("___", "."));

With this, the environment variable assignment of Logging__LogLevel__Microsoft___Hosting=Debug would produce configuration equivalent to:

{
  "Logging": {
    "LogLevel": {
      "Microsoft.Hosting": "Debug"
    }
  }
}

Open Questions

  • Should the user have some way to override the built-in __ → : replacement?

Alternative Designs

More focused

If we only consider the Microsoft.Extensions.Logging case, then we only need to handle the dot character:

public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
    public string? DotReplacement { get; set; }
}

In my opinion, this is too narrow, since any of the forbidden characters can be expected by a library that is being configured. Specifically, we had a report of a library that expected a configuration key with a hyphen (-) (it was since changed to also accept the same key without a hyphen, though).

Another option is to have a dictionary of replacements:

public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
    public IDictionary<string, char>? VariableNameCharacterReplacements { get; set; }
}

This lacks the flexibility of the proposed Func approach (e.g. with a Func, you could only perform the replacement for Logging keys, or any other number of customizations).

More general

Also allow arbitrary changes to values:

public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
{
    public Func<(string Name, string Value), (string Name, string Value)>? VariableTransformation { get; set; }
}

In my opinion, the generality of this alternative is not justified, since it lacks a compelling use case.

Risks

There shouldn't be any.

Activity

  1. added
    api-suggestion
    on Mar 3, 2026
  2. self-assigned this
    on Mar 3, 2026
  3. added
    untriaged
    on Mar 3, 2026
  4. dotnet-policy-service commented on Mar 3, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-configuration
    See info in area-owners.md if you want to be subscribed.

  5. mrek-msft commented on Mar 4, 2026

    @mrek-msft
    Member

    Some validations will be needed. For example, following should be invalid, I think

    configurationBuilder.AddEnvironmentVariables(null, new Dictionary<string, char> { { "__", '.' } });

    Similarly it is questionable how should something like this behave or how to efficiently reject it

    configurationBuilder.AddEnvironmentVariables(null, new Dictionary<string, char> { 
        { "?", '!' },
        { "!", '?' },
     });
  6. svick commented on Mar 4, 2026

    @svick
    MemberAuthor

    @mrek-msft Forbidding __ makes sense to me.

    The other variant should switch ! and ? around, so I don't see a need to reject it, even though it indeed doesn't make sense.

  7. mrek-msft commented on Mar 4, 2026

    @mrek-msft
    Member

    The ! and ? example focus importance of order. Assuming most simple replacement strategy, something like foreach (dict) { str = str.Replace(dict.Key, dict.Value) }. It will behave differently if you first replace ! to ? and then all ? to ! (in that case both symbols in original string becomes !. If you do that other way around, all symbols becomes ?. In both case it is unlikely what user want, I think.

    And I think there is more issues with ordering. For example, it is important to thing about order of replacement of __ to : and new proposal. Depending on it

    configurationBuilder.AddEnvironmentVariables(null, new Dictionary<string, char> { { ":_", '.' } });

    will convert ___ (3x underscore) to . or not. This is what workaround #87130 (comment) utilizes.

  8. svick commented on Mar 4, 2026

    @svick
    MemberAuthor

    In both case it is unlikely what user want, I think.

    Yes. But instead of somehow validating that issues like these can't happen, I think it's better if we use a smarter replacement strategy that doesn't have ordering issues. (The logic would be: at each character position, find the longest replacement string that starts there, if there is one, and replace it with its character.)

    it is important to thing about order of replacement of __ to : and new proposal

    Yes. I think ___ needs to work and :_ shouldn't work.

  9. jez9999 commented on Mar 4, 2026

    @jez9999

    I still prefer the simpler idea I had and posted in #35989 - allow key/value pairs in variable values, in which you can put basically any character, which side-steps the whole issue:

    DOTNETVAR1='Logging:LogLevel:Microsoft.Hosting.Lifetime="Debug"'
    DOTNETVAR2='Some:Config:Section="It'\''s even possible to mix quotes"'
    

    The default EnvironmentVariablesConfigurationProvider should scan for any env vars names DOTNET[i] and interpret them as key/value pairs.

  10. added
    api-ready-for-review
    and removed
    untriaged
    api-suggestion
    on Mar 5, 2026
  11. added this to the 11.0.0 milestone on Mar 24, 2026
  12. bartonjs commented on Apr 14, 2026

    @bartonjs
    Member

    Video

    • We added statics for the default transformation and the most common expected case (colon and dot).
    • We believe that assigning VariableNameTransformation always replaces the default behavior, it's not a pretransformation.
    namespace Microsoft.Extensions.Configuration.EnvironmentVariables
    {
        public partial class EnvironmentVariablesConfigurationSource : IConfigurationSource
        {
            public static Func<string, string> DefaultTransformation { get; }
            public static Func<string, string> ColonAndDotTransformation { get; }        
    
            public Func<string, string>? VariableNameTransformation { get; set; }
        }
    }
    namespace Microsoft.Extensions.Configuration
    {
        public partial static class EnvironmentVariablesExtensions
        {
            public static IConfigurationBuilder AddEnvironmentVariables(
                this IConfigurationBuilder configurationBuilder, string? prefix, Func<string, string>? variableNameTransformation);
        }
    }
  13. added
    api-approved
    and removed
    api-ready-for-review
    on Apr 14, 2026
  14. svick commented on Apr 28, 2026

    @svick
    MemberAuthor

    During implementation, I have found out that an API was missing from the original proposal:

    namespace Microsoft.Extensions.Configuration.EnvironmentVariables;
    
    public partial class EnvironmentVariablesConfigurationProvider : ConfigurationProvider
    {
        // existing
        // public EnvironmentVariablesConfigurationProvider();
        // public EnvironmentVariablesConfigurationProvider(string? prefix);
    
        public EnvironmentVariablesConfigurationProvider(string? prefix, Func<string, string>? variableNameTransformation);
    }

    Under normal circumstances, EnvironmentVariablesConfigurationProvider is created behind the scenes in EnvironmentVariablesConfigurationSource.Build(), but there are also more advanced use cases where it makes sense to create it directly, like composition of configuration providers or manual usage without DI.

  15. jez9999 commented on May 21, 2026

    @jez9999

    Hmm. #127503 isn't the way I would've done it (I still prefer my DOTNETVAR1='foo=bar' idea) but it should work adequately, I guess.

  16. locked and limited conversation to collaborators on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-Extensions-Configuration

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions