Skip to content

Comments

Normalize package source names when resolving environment variable credentials#6808

Closed
Foitn wants to merge 1 commit intoNuGet:devfrom
Foitn:dev
Closed

Normalize package source names when resolving environment variable credentials#6808
Foitn wants to merge 1 commit intoNuGet:devfrom
Foitn:dev

Conversation

@Foitn
Copy link

@Foitn Foitn commented Sep 29, 2025

Bug

Fixes: NuGet/Home#14562

Description

This PR fixes a bug where environment variable credential lookup fails if the package source key in nuget.config contains characters that are invalid in environment variable names (such as -, space, @, etc.).

Currently, only . is normalized to _ when constructing the environment variable name, but other invalid characters are not handled. For example:

  • Source key: foo-bar
  • Expected env vars: NuGetPackageSourceCredentials_foo_bar_Username, NuGetPackageSourceCredentials_foo_bar_ClearTextPassword
  • Actual lookup: NuGetPackageSourceCredentials_foo-bar_Username (invalid, cannot be set as an environment variable)

This PR adds normalization logic so that all non-alphanumeric characters are replaced with _ before environment variable lookup.

Example behavior

Source key Old lookup name New lookup name
foo.bar NuGetPackageSourceCredentials_foo_bar_* NuGetPackageSourceCredentials_foo_bar_* (unchanged)
foo-bar NuGetPackageSourceCredentials_foo-bar_* (invalid) NuGetPackageSourceCredentials_foo_bar_*
my source! NuGetPackageSourceCredentials_my source!* (invalid) NuGetPackageSourceCredentials_my_source_*

This change aligns behavior with the documentation and makes environment variable credentials usable with any source name.

PR Checklist

  • [ x] Meaningful title, helpful description and a linked NuGet/Home issue
  • [x ] Added tests
  • [x ] Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@Foitn Foitn requested a review from a team as a code owner September 29, 2025 16:45
@dotnet-policy-service dotnet-policy-service bot added the Community PRs created by someone not in the NuGet team label Sep 29, 2025
@Foitn
Copy link
Author

Foitn commented Sep 29, 2025

@dotnet-policy-service agree

Copy link
Contributor

@martinrrm martinrrm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is affecting two existing tests:

public void NuGetEnviromentPath_OnWindows_ReturnsCorrectPath()

and

https://github.com/NuGet/NuGet.Client/pull/6808/files#diff-2fca7fef8d0694494faad14f1088a0ee2a58cadd350f58a28ded8dfdb4369931R31

I believe this is a breaking change that we need to talk more about

private static string NormalizeSourceName(string sourceName)
{
// Replace invalid env var chars with underscore
return Regex.Replace(sourceName, @"[^A-Za-z0-9_]", "_");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This excludes a whole lot of allowed environment variable characters, basically all valid characters that are not English language letters. I tried both accented latin characters, as well as one non-latin character, and at least on Windows there's no problems using them as environment variables.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we could also use a filter the other way around, so lets say that all characters that match "#@%!-*" etc, are replaced as '_'
That would at least solve the issue with allowed environment variable characters that are currently not in this set and it would solve the linked issue

@dotnet-policy-service dotnet-policy-service bot added the Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed label Oct 7, 2025
@dotnet-policy-service
Copy link
Contributor

This PR has been automatically marked as stale because it has no activity for 7 days. It will be closed if no further activity occurs within another 30 days of this comment. If it is closed, you may reopen it anytime when you're ready again, as long as you don't delete the branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community PRs created by someone not in the NuGet team Status:No recent activity PRs that have not had any recent activity and will be closed if the label is not removed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Normalize package source names for environment variable credential lookup

3 participants