You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: How to audit package dependencies for security vulnerabilities and acting on security audit reports.
4
4
author: JonDouglas
5
5
ms.author: jodou
6
-
ms.date: 05/04/2023
6
+
ms.date: 10/11/2023
7
7
ms.topic: conceptual
8
8
---
9
9
10
10
# Auditing package dependencies for security vulnerabilities
11
11
12
12
## About security audits
13
13
14
-
A security audit for package managers like NuGet is a process that involves analyzing the security of the packages that are included in a software project. This involves identifying vulnerabilities, evaluating risks, and making recommendations for improving security. The audit can include a review of the packages themselves, as well as any dependencies and their associated risks. The goal of the audit is to identify and mitigate any security vulnerabilities that could be exploited by attackers, such as code injection or cross-site scripting attacks.
14
+
A security audit for package managers like NuGet is a process that involves analyzing the security of the packages that are included in a software project.
15
+
This involves identifying vulnerabilities, evaluating risks, and making recommendations for improving security.
16
+
The audit can include a review of the packages themselves, as well as any dependencies and their associated risks.
17
+
The goal of the audit is to identify and mitigate any security vulnerabilities that could be exploited by attackers, such as code injection or cross-site scripting attacks.
15
18
16
-
> [!IMPORTANT]
17
-
> Security auditing at restore time is available in .NET 8 Preview 4+ and Visual Studio 17.8 Preview 1+.
19
+
NuGet Audit is available starting from NuGet 6.8, the .NET 8 SDK (8.0.100), and Visual Studio 2022 17.8.
18
20
19
21
## Running a security audit with `restore`
20
22
21
-
The `restore` command automatically runs when you do a common package operation such as loading a project for the first time, adding a new package, updating a package version, or removing a package from your project in your favorite IDE. A description of your dependencies is checked against a report of known vulnerabilities on the [GitHub Advisory Database](https://github.com/advisories?query=type%3Areviewed+ecosystem%3Anuget).
23
+
The `restore` command automatically runs when you do a common package operation such as loading a project for the first time, adding a new package, updating a package version, or removing a package from your project in your favorite IDE.
24
+
A description of your dependencies is checked against a report of known vulnerabilities on the [GitHub Advisory Database](https://github.com/advisories?query=type%3Areviewed+ecosystem%3Anuget).
22
25
23
26
> [!IMPORTANT]
24
27
> For Audit to check packages, a package source that provides a vulnerability database must be used.
25
28
> NuGet.org's V3 URL is one such example (https://api.nuget.org/v3/index.json), but note that NuGet.org's V2 endpoint does not.
26
29
27
-
> [!NOTE]
28
-
> .NET 8 preview 6+ enables Audit by default, but Visual Studio does not ship .NET 8 yet.
29
-
> To opt-in to Audit explicitly, set `<NuGetAudit>true</NuGetAudit>` in your project file, or a *Directory.Build.props* file.
30
-
31
30
1. On the command line, navigate to your project or solution directory.
32
-
2. Ensure your project or solution contains a `.csproj` file.
33
-
3. Type `dotnet restore` or `restore` using your preferred tooling (i.e. MSBuild, NuGet.exe, etc).
34
-
4. Review the audit report and address the known security vulnerabilities.
31
+
1. Ensure your project or solution contains a `.csproj` file.
32
+
1. Type `dotnet restore` or `restore` using your preferred tooling (i.e. MSBuild, NuGet.exe, etc).
33
+
1. Review the audit report and address the known security vulnerabilities.
34
+
35
+
> [!NOTE]
36
+
> At this time, NuGet does not audit `packages.config` projects.
35
37
36
38
## Reviewing and acting on the security audit report
37
39
@@ -56,7 +58,8 @@ In the case that a known vulnerability exists in a package without a security fi
56
58
57
59
#### Check for mitigating factors
58
60
59
-
Review the security advisor for any mitigating factors that may allow you to continue using the package with the vulnerability. The vulnerability may only exist when the code is used on a specific framework, operating system, or a special function is called.
61
+
Review the security advisor for any mitigating factors that may allow you to continue using the package with the vulnerability.
62
+
The vulnerability may only exist when the code is used on a specific framework, operating system, or a special function is called.
60
63
61
64
#### Use a suggested package
62
65
@@ -68,33 +71,43 @@ If a fix does not exist for the security advisory, you may want to suggest chang
68
71
69
72
#### Open an issue
70
73
71
-
If you do not want to fix the vulnerability or are unable to update or replace the package, open an issue in the package's issue tracker or preferred contact method. On NuGet.org, you can navigate to the package details page and click `Report package` which will guide you to get in contact with the author.
74
+
If you do not want to fix the vulnerability or are unable to update or replace the package, open an issue in the package's issue tracker or preferred contact method.
75
+
On NuGet.org, you can navigate to the package details page and click `Report package` which will guide you to get in contact with the author.
72
76
73
77
### No security vulnerabilities found
74
78
75
-
If no security vulnerabilities are found, this means that packages with known vulnerabilities were not found in your package graph at the present moment of time you checked. Since the advisory database can be updated at any time, we recommend regularly checking your `dotnet restore` output and ensuring the same in your continuous integration process.
79
+
If no security vulnerabilities are found, this means that packages with known vulnerabilities were not found in your package graph at the present moment of time you checked.
80
+
Since the advisory database can be updated at any time, we recommend regularly checking your `dotnet restore` output and ensuring the same in your continuous integration process.
76
81
77
82
### Setting a security audit mode
78
83
79
84
By default, a security audit is done for top-level dependencies.
80
85
In the case that you'd like to audit both top-level and transitive dependencies, you can set the `<NuGetAuditMode>` MSBuild property to the desired mode in which auditing will run.
81
-
Possible values are `direct` and `all`. For example if you wanted to audit all dependencies for security advisories, you can set the following:
86
+
Possible values are `direct` and `all`.
87
+
For example if you wanted to audit all dependencies for security advisories, you can set the following:
82
88
83
89
```xml
84
90
<NuGetAuditMode>all</NuGetAuditMode>
85
91
```
86
92
93
+
> [!NOTE]
94
+
> Visual Studio 2022 17.8 does not support changing audit mode for SDK style packages.
95
+
> It works from 17.9 Preview 2.
96
+
87
97
### Setting a security audit level
88
98
89
-
In cases where you only care about a certain threshold of a security advisory severity, you can set the `<NuGetAuditLevel>` MSBuild property to the desired level in which auditing will fail. Possible values are `low`, `moderate`, `high`, and `critical`. For example if you only want to see `moderate`, `high`, and `critical` advisories, you can set the following:
99
+
In cases where you only care about a certain threshold of a security advisory severity, you can set the `<NuGetAuditLevel>` MSBuild property to the desired level in which auditing will fail.
100
+
Possible values are `low`, `moderate`, `high`, and `critical`.
101
+
For example if you only want to see `moderate`, `high`, and `critical` advisories, you can set the following:
90
102
91
103
```xml
92
104
<NuGetAuditLevel>moderate</NuGetAuditLevel>
93
105
```
94
106
95
107
### Excluding advisories
96
108
97
-
There is no support for excluding individual advisories at this time. You can use `<NoWarn>` to suppress `NU1901`-`NU1904` warnings or use the `<NuGetAuditLevel>` functionality to ensure your audit reports are useful to your workflow.
109
+
There is no support for excluding individual advisories at this time.
110
+
You can use `<NoWarn>` to suppress `NU1901`-`NU1904` warnings or use the `<NuGetAuditLevel>` functionality to ensure your audit reports are useful to your workflow.
98
111
99
112
### Warning codes
100
113
@@ -105,11 +118,10 @@ There is no support for excluding individual advisories at this time. You can us
105
118
| NU1902 | Package with moderate severity detected |
106
119
| NU1903 | Package with high severity detected |
107
120
| NU1904 | Package with critical severity detected |
108
-
| NU1905 | NuGetAudit is explicitly enabled, but no package sources are providing vulnerability data |
109
121
110
122
You can customize your build to treat these warnings as errors to [treat warnings as errors, or treat warnings not as errors](/dotnet/csharp/language-reference/compiler-options/errors-warnings#warningsaserrors-and-warningsnotaserrors).
111
123
For example, if you're already using `<TreatWarningsAsErrors>` to treat all (C#, NuGet, MSBuild, etc) warnings as errors, you can use `<WarningsNotAsErrors>NU1901;NU1902;NU1903;NU1904</WarningsNotAsErrors>` to prevent vulnerabilities discovered in the future from breaking your build.
112
-
Alternatively, if you want to keep low and moderate vulnerabilities as warnings, but treat missing vulnerability data and high and critical vulnerabilities as errors, and you're not using `TreatWarningsAsErrors`, you can use `<WarningsAsErrors>NU1903;NU1904;NU1905</WarningsAsErrors>`.
124
+
Alternatively, if you want to keep low and moderate vulnerabilities as warnings, but treat high and critical vulnerabilities as errors, and you're not using `TreatWarningsAsErrors`, you can use `<WarningsAsErrors>NU1903;NU1904</WarningsAsErrors>`.
113
125
114
126
### Disabling security auditing
115
127
@@ -121,4 +133,5 @@ At any time you wish to not receive security audit reports, you can opt-out of t
121
133
122
134
## Summary
123
135
124
-
Security auditing features are crucial for maintaining the security and integrity of software projects. These features provide you with an additional layer of protection against security vulnerabilities and ensures that you can use open source packages with confidence.
136
+
Security auditing features are crucial for maintaining the security and integrity of software projects.
137
+
These features provide you with an additional layer of protection against security vulnerabilities and ensures that you can use open source packages with confidence.
0 commit comments