Guide: .NET and NuGet

NuGet audit warnings NU1902, NU1903 and NU1904: what they mean and how to fix them

What the warning says, how to find the package, how to fix it, and what to do when the same fix is needed in every repository.

What the warning says

Your build printed a line like this:

warning NU1902: Package 'NuGet.Protocol' 5.11.2 has a known moderate severity vulnerability, https://github.com/advisories/GHSA-g3q9-xf95-8hp5

It is not a compiler warning. NuGet Audit prints it during dotnet restore. NuGet compares each package version in your project with a list of known vulnerabilities. For nuget.org, that list comes from the GitHub Advisory Database. The warning code tells you the severity. The URL in the message is the advisory, and it shows which versions have the fix. For the background on CVEs, read what is a CVE.

The four severity codes

  • NU1901: low

    A package in your project has a known vulnerability of low severity.

  • NU1902: moderate

    A package in your project has a known vulnerability of moderate severity.

  • NU1903: high

    A package in your project has a known vulnerability of high severity.

  • NU1904: critical

    A package in your project has a known vulnerability of critical severity.

Two neighbours look alike. NU1900 means NuGet could not get the vulnerability data from a package source, so nothing was checked. NU1905 means an audit source does not provide vulnerability data. Neither says that a package is vulnerable.

NuGet Audit for PackageReference projects arrived with NuGet 6.8, the .NET 8 SDK (8.0.100) and Visual Studio 2022 17.8. Older tooling does not run the audit.

Which packages are checked

Audit checks the packages you reference directly. It can also check transitive packages, the ones your packages pull in. Three MSBuild properties decide what you see. Microsoft recommends setting them at repository level, for example in Directory.Build.props.

The three settings

  • NuGetAuditMode

    Either direct or all. It defaults to all when a project targets net10.0 or higher, and to direct otherwise. A project that targets several frameworks uses all if any one of them selects it.

  • NuGetAuditLevel

    The lowest severity that is reported. It defaults to low, so every code above can appear. Set it to moderate to drop NU1901, or to high to keep only NU1903 and NU1904.

  • NuGetAudit

    Set it to false to turn the check off completely.

Moving to .NET 10 can add warnings with no code change. Once a project targets net10.0, transitive packages are checked by default. Microsoft's own note says that if you treat warnings as errors, this can make restore fail. To go back to checking direct packages only, set NuGetAuditMode to direct.

Find the package

List what is vulnerable. Add --include-transitive, because it is not on by default:

dotnet list package --vulnerable --include-transitive

From the .NET 10 SDK the same command is written dotnet package list. From the .NET 9.0.300 SDK, --vulnerable reads vulnerability data from your audit sources.

If the package is not one you chose, find out which package brought it in:

dotnet nuget why <project> <package>

The output is the dependency graph for that package. The command needs the .NET 8.0.4xx SDK or later.

A chain of four boxes: Your project, Package A, Package B and Package C. Package C carries the known flaw. Three numbered markers show the order to try: 1 update A, 2 update B, 3 reference C directly or pin it.
The order to try the fixes when the flaw is in a transitive package.

Fix a direct package

If you reference the package yourself, move to a version that has the fix. The advisory URL in the warning shows which versions are fixed.

  • Edit the version in the project file, or in Directory.Packages.props if the repository uses Central Package Management.
  • With the .NET 10 SDK, dotnet package update --vulnerable moves every vulnerable package in a project or solution to the first version without a known vulnerability.
  • To move one package to its latest version, use dotnet package update <package>. On .NET 9 or earlier, use dotnet add package.

Then run dotnet restore, dotnet build and dotnet test. A security fix is still an upgrade, so a clean restore alone does not prove it.

Fix a transitive package

Say your project references package A, A depends on B, and B depends on C. C has the flaw. Microsoft's advice is to start with the package closest to you and work down.

Five ways to fix C

  1. Update A

    A newer A may already depend on a fixed C.

  2. Update B

    If A is already current, try the package below it.

  3. Reference C directly

    Add a PackageReference for the fixed version of C. Remove it when a newer A or B makes it unnecessary.

  4. Pin C for the whole repository

    With Central Package Management, set CentralPackageTransitivePinningEnabled to true and list the fixed version of C as a PackageVersion. A pin cannot go lower than the version your dependencies ask for, or restore fails with NU1109. If you pack your project as a package, the pinned package becomes a dependency of it.

  5. Ask the owner

    Open an issue with the package that pulls in C.

In a repository that already uses Central Package Management, the pin looks like this in Directory.Packages.props:

<PropertyGroup>
  <CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
  <PackageVersion Include="PackageC" Version="2.0.0" />
</ItemGroup>

If no fixed version exists yet, read the advisory for mitigating factors: the flaw may only matter on one framework, on one operating system, or when one function is called. If the package is deprecated or abandoned, look for a maintained alternative. Otherwise open an issue with the owner.

Suppressing a warning is a decision to record

You can hide one advisory with an item that holds its URL:

<ItemGroup>
  <NuGetAuditSuppress Include="https://github.com/advisories/GHSA-g3q9-xf95-8hp5" />
</ItemGroup>

This works from NuGet 6.11 and the .NET 8.0.400 SDK for PackageReference projects. Microsoft calls it a last resort. It removes the advisory completely, including for other packages that share it.

Write down who suppressed it, why, and when to look again. A comment next to the item is enough. Without one, a suppression that was fair today is still there in two years, and nobody remembers the reason.

The blunt option is <NoWarn>$(NoWarn);NU1901-NU1904</NoWarn>. That hides every vulnerability warning, not one advisory.

What warnings-as-errors does to a build

With TreatWarningsAsErrors on, every NU1901 to NU1904 warning becomes an error. Advisories are published at any time, so a build that was green on Friday can fail on Monday with no change in your code. Microsoft documents these ways to handle it.

Keep the codes as warnings:

<WarningsNotAsErrors>$(WarningsNotAsErrors);NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>

Without TreatWarningsAsErrors, make only the serious ones errors:

<WarningsAsErrors>$(WarningsAsErrors);NU1903;NU1904</WarningsAsErrors>

Fail only in a dedicated audit pipeline, and keep the codes as warnings in every other build. Microsoft shows how, with a property that you pass to dotnet restore.

None of this works for packages.config projects, where NoWarn and TreatWarningsAsErrors are not supported.

Pick one rule on purpose. A rule that fails every build on a moderate flaw can push people to suppress it. A rule that shows no warnings at all means nobody looks.

This is the job for one repository

The steps for one advisory in one repository

  1. Find the package

    Run dotnet list package --vulnerable --include-transitive, then dotnet nuget why if the package is transitive.

  2. Choose the fix

    Update the package, update its parent, reference the fixed version, or pin it.

  3. Change the version

    In the project file, or in Directory.Packages.props.

  4. Prove it

    Run dotnet restore, dotnet build and dotnet test.

  5. Create a branch and commit

    One branch and one commit for this repository.

  6. Open a pull request and get a review

    Someone has to read the change and approve it.

  7. Wait for your pipeline, then merge

    The pipeline runs again before the merge.

In our experience, the code fix alone takes 15 to 30 minutes for one repository. That is steps 1 to 4, for one advisory and one package. The branch, the pull request, the review and the pipeline run come on top, and each one waits on other people.

Now count the repositories that use the package. If you have 40, the code fix alone is 10 to 20 hours, before any review or pipeline run. A new advisory next week starts the whole list again.

It is boring work, and it comes back every week. The change is invisible to the business or the client, so it is hard to ask for time, and the active project wins. The older applications get none. Those are the repositories that stay vulnerable. A warning also shows only when a repository is restored, so a repository nobody builds says nothing at all. Each repository holds its own audit settings and its own suppressions, so those drift too. How to update a NuGet package in many repositories shows the manual steps.

One repository box lists seven steps. Steps 1 to 4, the code fix, take 15 to 30 minutes. Steps 5 to 7, the branch, the pull request and the pipeline, come on top. An arrow marked times every repository leads to a grid of nine repositories.
The same job in every repository: the code fix is 15 to 30 minutes in our experience, before review and pipeline.

Do the first four steps once for the whole group

Smell That Code reads your repositories as one repository group, so the work you repeat in every repository is done once.

How Smell That Code changes it

  1. See every version

    It finds the package version in every repository. This is step 1, done once for the group.

  2. Security fixes first

    Suggested upgrades start with the security fixes.

  3. Approve once

    A person approves a version for the repository group, or a rule you set approves it. This is the choice in step 2, made once.

  4. Prove it on canary repositories

    The version goes to a few repositories first. If build and tests pass there, it counts as proven, and then it spreads. This is step 4, done before it spreads.

  5. Run stc upgrade

    stc edits the build files and runs restore, build and test in each repository (steps 3 and 4). It never commits, pushes or merges.

Not for you with one or two repositories. The commands above are enough. If a dependency bot already works for you, keep it. In the documentation we read, we found no view of one package version across all repositories, no approval that covers a group, and no rollout that proves a version on a few repositories first.

Questions

What does NU1902 mean?

NuGet found a package with a known moderate severity vulnerability. NU1901 is low, NU1903 is high and NU1904 is critical. The message ends with the URL of the advisory.

What is the difference between NU1902, NU1903 and NU1904?

Only the severity: moderate, high and critical. The cause is the same, and so are the ways to fix it.

How do I fix NU1903?

If you reference the package directly, update it to a version that has the fix. If another package brought it in, update that package first. If that does not clear the warning, reference the fixed version directly or pin it with Central Package Management.

How do I find which package causes NU1904?

Run dotnet nuget why <project> <package> to see the dependency graph for that package.

Why did NU1903 appear after I moved to .NET 10?

A project that targets net10.0 or higher checks transitive packages by default. To go back to checking direct packages only, set NuGetAuditMode to direct.

How do I suppress NU1902?

Add a NuGetAuditSuppress item with the advisory URL from the warning. It needs NuGet 6.11 or the .NET 8.0.400 SDK. Microsoft calls it a last resort, so write down why you did it.

Why does NU1904 fail my build?

TreatWarningsAsErrors turns it into an error. Add <WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors> to keep the codes as warnings.

How long does it take to fix a NuGet vulnerability warning?

In our experience, 15 to 30 minutes for the code fix in one repository, not counting the branch, the review and the pipeline. With many repositories, that is repeated in each one.

How do I fix NU1903 in many repositories?

Do the fix in each repository, or use a tool that reads the whole repository group. A repository group in Smell That Code lets you approve one version for all of them.

Does Smell That Code commit or push my changes?

No. stc upgrade edits your build files and runs restore, build and test. You commit, push and merge.

Find the page for your situation

Smell That Code fits several kinds of teams. Pick the one that sounds like yours.

  • A vulnerability report with a deadline

    You own many .NET repositories, managers read a report of open vulnerabilities, and your team has days to fix each one.

    For teams with a vulnerability KPI
  • Shared packages across many services

    You look after the packages, templates and pipelines that many .NET services use.

    For platform teams
  • Many client projects

    You keep the repositories of many clients up to date, including old projects nobody has opened for months.

    For software houses

Join the waitlist

Smell That Code is not open to everyone yet. Join the waitlist and be the first to try it. A free tier lets you try it on your own repositories.