nuget.config, Feeds & Source Mapping
Where does NuGet look for packages? Not in the project file: package sources (feeds) come from nuget.config files. Getting them right is what makes a build work the same on every laptop and CI agent, and, with package source mapping, what protects you from dependency-confusion attacks.
Which config files apply
NuGet merges several files, closest first:
nuget.config(any capitalisation) in the solution or project folder, then in every parent folder up to the root;- the user-wide file:
~/.nuget/NuGet/NuGet.Configon Linux and macOS,%AppData%\NuGet\NuGet.Configon Windows; - machine-wide files, if any.
mkdir -p repo/src/App && cd repo
dotnet nuget config paths
echo "--- the user-wide file on this machine"
cat ~/.nuget/NuGet/NuGet.Config
echo "--- add a repo-level file"
dotnet new nugetconfig > /dev/null
cat nuget.config
cd src/App && dotnet nuget config paths/home/learner/.nuget/NuGet/NuGet.Config
--- the user-wide file on this machine
<?xml version="1.0" encoding="utf-8"?>
<!-- Learner's user-wide NuGet config: nuget.org is cleared, the only source is the local folder feed. -->
<configuration>
<packageSources>
<clear />
<add key="local" value="/opt/nuget-feed" />
</packageSources>
</configuration>
--- add a repo-level file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="nuget" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
/home/learner/repo/nuget.config
/home/learner/.nuget/NuGet/NuGet.Config
dotnet nuget config pathslists the files that apply to the current folder, in priority order. Run fromsrc/App, the repository's file is found two levels up and comes before the user-wide one.dotnet new nugetconfigwrites a file with nuget.org as its only source (this machine has no internet, so the later sessions replace it with the local feed).- The template's
<clear />matters: it removes every source inherited from files further down the list (including nuget.org from the user-wide file). Without it, a repository's sources are added to whatever each developer happens to have configured, and builds depend on the machine.
Tip: Commit a
nuget.configwith<clear />at the repository root that lists every source the build needs. The build then means the same thing everywhere.
Managing sources
The dotnet nuget commands edit config files for you; --configfile picks which one:
mkdir repo && cd repo
cat > nuget.config <<'XML'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="local" value="/opt/nuget-feed" />
</packageSources>
</configuration>
XML
mkdir corp-feed
dotnet nuget add source "$PWD/corp-feed" --name corp --configfile nuget.config
dotnet nuget disable source corp --configfile nuget.config
dotnet nuget list source --configfile nuget.config
dotnet nuget enable source corp --configfile nuget.config > /dev/null
sed -n '/<packageSources>/,/<\/packageSources>/p' nuget.configPackage source with Name: corp added successfully.
Package source with Name: corp disabled successfully.
Registered Sources:
1. local [Enabled]
/opt/nuget-feed
2. corp [Disabled]
/home/learner/repo/corp-feed
<packageSources>
<clear />
<add key="local" value="/opt/nuget-feed" />
<add key="corp" value="/home/learner/repo/corp-feed" />
</packageSources>
A source can be a folder, a UNC share or an https://…/index.json URL (the NuGet v3 protocol, which every hosted feed speaks). For authenticated feeds, never commit passwords: either reference environment variables in a <packageSourceCredentials> section (<add key="Password" value="%FEED_TOKEN%" />) or use a credential provider (Azure Artifacts and GitHub Packages both document one), and pass --store-password-in-clear-text only in throwaway CI containers. Those steps need a real feed, so they're not run here.
Package source mapping
With two or more sources, NuGet by default asks all of them for every package and takes the first answer that satisfies the version. That's the setup behind dependency confusion: an attacker publishes a package on nuget.org with the same name as one of your internal packages and a higher version, and your build happily picks it up.
Package source mapping declares which source may serve which package ids. Patterns are package-id prefixes ending in *, or exact ids; the most specific pattern wins:
mkdir repo && cd repo
mkdir corp-feed
cat > nuget.config <<'XML'
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="local" value="/opt/nuget-feed" />
<add key="corp" value="corp-feed" />
</packageSources>
<packageSourceMapping>
<packageSource key="local">
<package pattern="*" />
</packageSource>
<packageSource key="corp">
<package pattern="Ada.*" />
</packageSource>
</packageSourceMapping>
</configuration>
XML
mkdir lib && cat > lib/lib.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<PackageId>Ada.Text</PackageId>
<Version>1.0.0</Version>
</PropertyGroup>
</Project>
XML
echo 'namespace Ada; public static class Text { public static string Shout(string s) => s.ToUpperInvariant(); }' > lib/Text.cs
dotnet pack lib -o out -nologo -v:q
dotnet nuget push out/Ada.Text.1.0.0.nupkg -s corp
echo "--- an app using both feeds"
mkdir app && cat > app/app.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ada.Text" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
</ItemGroup>
</Project>
XML
echo 'System.Console.WriteLine(Ada.Text.Shout("mapped"));' > app/Program.cs
dotnet restore app -v:n | grep -o 'Installed [^ ]* [^ ]* from [^ ]*' | sed "s|$PWD/||"
dotnet run --project app
echo "--- map Ada.* to the wrong feed"
sed -i 's|<package pattern="Ada.\*" />|<package pattern="Ada.Other.*" />|' nuget.config
rm -rf ~/.nuget/packages/ada.text app/obj
dotnet restore app 2>&1 | grep -E 'NU1' | awk '!seen[$0]++' | sed "s|$PWD/||"Pushing Ada.Text.1.0.0.nupkg to '/home/learner/repo/corp-feed'...
Your package was pushed.
--- an app using both feeds
Installed Ada.Text 1.0.0 from corp-feed
Installed Newtonsoft.Json 13.0.4 from /opt/nuget-feed
MAPPED
--- map Ada.* to the wrong feed
app/app.csproj : error NU1101: Unable to find package Ada.Text. No packages exist with this id in source(s): local. PackageSourceMapping is enabled, the following source(s) were not considered: corp.
- With the mapping in place,
Ada.Textcame only fromcorpandNewtonsoft.Jsononly fromlocal. NuGet didn't even ask the other source. - After the pattern changed,
Ada.Textmatched only*, which maps it tolocal. The package exists oncorp, but that source wasn't consulted, and the error says so. That's the protection: a public package named like an internal one can never be pulled from the wrong source.
Mapping is all-or-nothing: once a packageSourceMapping section exists, every package id must match some pattern, or restore fails.
Other settings worth knowing
Setting (<config> section) |
Effect |
|---|---|
globalPackagesFolder |
moves ~/.nuget/packages (e.g. to a cached CI volume); the NUGET_PACKAGES env var does the same |
signatureValidationMode = require |
only accept signed packages (with <trustedSigners>) |
http_proxy |
proxy for remote feeds |
Test yourself
What does <clear /> inside <packageSources> in a repository's nuget.config do?
- Deletes the global packages folder
- Removes sources inherited from config files further down the hierarchy, such as the user-wide file
- Clears the HTTP cache
- Disables restore
Patterns * → nuget, Contoso.* → internal, Contoso.Public.* → nuget. Where does Contoso.Public.Http come from?
- internal
- nuget, because the most specific pattern wins
- Both feeds are queried
- Restore fails
Which attack does package source mapping prevent?
- Typosquatting on one feed
- Dependency confusion: a public package with an internal package's name being picked from the wrong feed
- Man-in-the-middle on HTTPS
- Vulnerable transitive dependencies