-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Environment Variable configuration provider doesn't work if the prefix has 2 underscores #40911
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @maryamariyan |
Moving to 6.0 as this isn't a regression in 5.0, and isn't critical to fix for 5.0. |
Unless there is a reason to confine it to strictly one or two underscores, it would probably make sense if the solution be bit more versatile like handling more than two underscores and mixed cases like |
The fix implementation works like so: - The environment variable prefix is stripped from a variable name. - The variable name is then normalized. - The prefix is then prepended to the variable name. - Additionally, the filtering now occurs at the same time as parsing/transformation.
…g environment variables from being parsed (#42932) * Prevent envvar prefix normalization. Fixes #40911 The fix implementation works like so: - The environment variable prefix is stripped from a variable name. - The variable name is then normalized. - The prefix is then prepended to the variable name. - Additionally, the filtering now occurs at the same time as parsing/transformation. * Account for the fact that envPrefix is never null. * Remove LINQ per #44923 * Fix bad tests resulting from misunderstanding how loader is supposed to work. * Fixed merge conflicts * Actually merge #44923 and fix conflicts. Co-authored-by: Stephen Toub <[email protected]>
Description
Environment variable loader doesn't load variables if the prefix is set to a string with one or more double underscores.
The problem lies in the incorrect order of operations here:
runtime/src/libraries/Microsoft.Extensions.Configuration.EnvironmentVariables/src/EnvironmentVariablesConfigurationProvider.cs
Lines 53 to 56 in 98eecc7
The keys are first normalized (that is
__
is replaced with separator; default:
), then they are filtered. The operations should happen the other way around.To reproduce the issue:
TEST__TEST1__TEST2
with a value of42
.Expected result
Console prints
42
.Actual result
Console prints
<null>
.Configuration
This affected my application running on ASP.NET Core 3.1.7, running on Windows 10 Pro 2004 x64, as well as Alpine Linux 3.12.0 x64 (running in Docker).
Browsing source of the master branch reveals the buggy code remains, which suggests the bug is present in .NET 5 as well.
Regression?
Not as far as I can tell.
Other information
Tried on Core 3.1, the problem appears to be present in .NET 5 as well.
Currently, the problem can be mitigated by replacing all instances of
__
in the prefix with:
or whatever other separator was configured, if one was configured. Modifying line 1 from repro to.AddEnvironmentVariables("TEST:")
will cause the program to print42
instead of<null>
.This caveat is not documented, and therefore I consider it a bug.
The issue might not be trivial to fix, as I've noticed that several other prefixes are tested as well.
In my code, I decided to use
__
for consistency (so thatTest1:Test2
becomesTEST__TEST1__TEST2
rather thanTEST_TEST1__TEST2
).The text was updated successfully, but these errors were encountered: