Skip to content

Commit cd11a44

Browse files
authored
Document how to enforce audit during restore (#3394)
1 parent 9235893 commit cd11a44

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/concepts/Auditing-Packages.md

+29
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ description: How to audit package dependencies for security vulnerabilities and
44
author: JonDouglas
55
ms.author: jodou
66
ms.topic: conceptual
7+
ms.date: 02/11/2025
78
---
89

910
# Auditing package dependencies for security vulnerabilities
@@ -105,6 +106,34 @@ Alternatively, if you want to keep low and moderate vulnerabilities as warnings,
105106
> [!NOTE]
106107
> MSBuild properties for message severity such as `NoWarn` and `TreatWarningsAsErrors` are not supported for packages.config projects.
107108
109+
## Ensure restore audited projects
110+
111+
NuGet in MSBuild 17.13 and .NET 9.0.200 added output properties `RestoreProjectCount`, `RestoreSkippedCount` and `RestoreProjectsAuditedCount` on the restore task.
112+
This can be used to enforce that audit ran during a restore.
113+
Note that these output properties are not available with [static graph restore](../reference/msbuild-targets.md#restoring-with-msbuild-static-graph-evaluation).
114+
115+
Since MSBuild is a scripting language, this can be achieved a number of different ways, but also has the same restrictions as MSBuild has.
116+
One example is to create a file *Directory.Solution.targets* in the same directory as your solution file, whose contents has a target similar to the following.
117+
Note that *Directory.Build.props* is commonly used, but is imported by projects.
118+
However, NuGet's restore target and task runs at the solution level, so needs to be in MSBuild's solution extensibility file, not the project/build file.
119+
120+
```xml
121+
<Project>
122+
<Target Name="AssertRestoreTaskOutputProperties"
123+
AfterTargets="Restore"
124+
Condition="'$(CI)' == 'true'">
125+
<Error
126+
Condition="'$(RestoreProjectsAuditedCount)' != '$(RestoreProjectCount)'"
127+
Text=""Restore did not audit every project in the solution. Expected: $(RestoreProjectCount) Found: $(RestoreProjectsAuditedCount)"" />
128+
</Target>
129+
</Project>
130+
```
131+
132+
Depending on your use-case, you may wish to use condition `'$(RestoreProjectCount)' != '$([MSBuild::Add($(RestoreProjectsAuditedCount), $(RestoreSkippedCount))'` on the error message, to account for projects that restore skipped because they were already up to date.
133+
Similarly, think about if you want this error to happen everywhere, or only in CI pipelines, and what environment variables are defined in your CI environment, and factor this into the target's condition.
134+
Again, since MSBuild is a scripting language, you can use any of its capabilities to customize your repo however you want.
135+
Viewing [MSBuild's metaproj](/visualstudio/msbuild/how-to-build-specific-targets-in-solutions-by-using-msbuild-exe#troubleshooting) and [binlogs](/visualstudio/msbuild/msbuild-command-line-reference#switches-for-loggers) are useful to develop and troubleshoot solution level targets.
136+
108137
## `dotnet list package --vulnerable`
109138

110139
Once a project is successfully restored, [`dotnet list package`](/dotnet/core/tools/dotnet-list-package) has a `--vulnerable` argument to filter the packages based on which packages have known vulnerabilities.

0 commit comments

Comments
 (0)