Skip to content

Commit 3d99dbe

Browse files
JanKrivanekghogen
andauthored
Add MSBuild security best practices (#13636)
* Add security best practices * fixing typos * Add related configurations section in msbuild.md * Move article * Fix links * Apply suggestions from code review Co-authored-by: Gordon Hogenson <[email protected]> * Improve msbuild-security-best-practices.md * Improve msbuild-security-best-practices.md * Apply suggestions from code review Co-authored-by: Gordon Hogenson <[email protected]> * Reflect Acrolinx scoring suggestions * Update docs/msbuild/msbuild-security-best-practices.md Co-authored-by: Gordon Hogenson <[email protected]> --------- Co-authored-by: Gordon Hogenson <[email protected]>
1 parent 8112297 commit 3d99dbe

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
---
2+
title: Secure MSBuild usage best practices
3+
description: Learn about best practices for configuring and running your builds with MSBuild securely.
4+
ms.date: 02/24/2025
5+
ms.topic: how-to
6+
helpviewer_keywords:
7+
- best practices, MSBuild
8+
- MSBuild, best practices
9+
author: jankrivanek
10+
ms.author: jankrivanek
11+
manager: tkapin
12+
ms.subservice: msbuild
13+
---
14+
# Secure MSBuild usage best practices
15+
16+
MSBuild is highly customizable and extendable (see [Customize your build](./customize-your-build.md) for details), so special care should be paid to the proper configuration of the environment and build.
17+
18+
## Restrict Write access to the install location
19+
20+
Install MSBuild (whether with Visual Studio, the .NET SDK, or standalone) into a location where only trusted users have write access.
21+
22+
You can change the build logic by altering or adding various binary and XML files placed next to the MSBuild executable, and to the subfolders of the MSBuild executable. Therefore, only trusted users should be allowed to write to the folder.
23+
24+
### Related configurations
25+
* [MSBuildExtensionsPath and MSBuildUserExtensionsPath](./customize-your-local-build.md#msbuildextensionspath-and-msbuilduserextensionspath) - specifically `ImportUserLocationsByWildcardBefore{ImportingFileNameWithNoDots}` property that can be used to opt out of the extension logic
26+
27+
## Run build only on sources you know and reviewed
28+
29+
Run MSBuild (for building and restoring projects, but also for opening projects in Visual Studio) only on sources that you fully trust.
30+
31+
MSBuild logic can be extended within the build script files, including your project files, so unknown build logic should be assumed to be capable of executing arbitrary code in the build environment.
32+
33+
### Related configurations
34+
* [Visual Studio Trust Settings](./../ide/reference/trust-settings.md)
35+
* [Disabling response files](./msbuild-response-files.md#disabling-response-files)
36+
37+
## Run your build in a verified, dedicated location
38+
39+
MSBuild can automatically include logic from a folder of your project or solution and any parent folder up to the root of the drive. This includes `.user` files, `[before|after].{solution}.targets` files, `Directory.Build.[props|targets|rsp]` files and others.
40+
41+
Make sure only the authorized users or accounts have write access to the location with your build-related files and any folder in a hierarchic structure up to the root of the drive.
42+
43+
To prevent the unintended inclusion of `Directory.Build.[props|targets|rsp]`, you can include such files in the root of your sources. The file can be an empty MSBuild `Project` element.
44+
45+
### Related configurations
46+
* [Customize the build by folder](./customize-by-directory.md)
47+
48+
## Know and review referenced packages and originating feeds
49+
50+
Build logic can be automatically extended by NuGet packages. Such logic is executed upon restore, which includes the logic into your build or compilation execution. Make sure you're familiar with the [NuGet package asset types](/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets) and their role during the build, compilation and runtime. Specifically the `build`, `buildTransitive`, `buildMultitargeting` and `analyzers` assets are being automatically plugged into (and so automatically executed during) the build, unless they're being explicitly opted out.
51+
52+
Use `ExcludeAssets` whenever you don't need the build or compiler extension logic from a referenced package (or even better, only explicitly `IncludeAssets` the build logic if you want it).
53+
54+
Make sure you're familiar with the current documentation and guidance from the NuGet team. Refer to the [`PackageReference` in project files](/nuget/consume-packages/package-references-in-project-files) document as an authoritative source for this problem.
55+
56+
### Related configurations
57+
* [Controlling dependency assets](/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets)
58+
59+
## Know and review build starting script/process
60+
61+
Build logic can be affected by command-line arguments or environment variables, especially those that can result in the injection of plugins (for example, custom loggers) or build logic (for example, build scripts in `MSBuildUserExtensionsPath`). Ensure that you know what command-line arguments and environment variables are applied to the MSBuild process. That way you will better understand how the build logic is affected.
62+
63+
## Use dedicated user account and session to run your build
64+
65+
Don't run under an account that could be used on a same system to previously run unknown processes or scripts, including a different build. Especially if the unrelated build under same user account might have run on sources that aren't fully trusted and known.
66+
67+
MSBuild can source logic from various locations from the user profile (specifically MSBuild SDK automatically includes build logic located in [MSBuildUserExtensionsPath](./customize-your-local-build.md#msbuildextensionspath-and-msbuilduserextensionspath) location), or from locations injectable by environment variables (you can customize `MSBuildUserExtensionsPath` with a MSBuild property with the same name. Such property doesn't have a default value, so it can be sourced from the environment variable with the same name).
68+
69+
### Related configurations
70+
* [MSBuildExtensionsPath and MSBuildUserExtensionsPath](./customize-your-local-build.md#msbuildextensionspath-and-msbuilduserextensionspath): you can set the `ImportUserLocationsByWildcardBefore{ImportingFileNameWithNoDots}` property to `false` to opt out of the auto-inclusion of the specific extension build logic.

docs/msbuild/msbuild.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ MSBuild is an open-source project that accepts user contributions at the [MSBuil
217217
| [Multitargeting](../msbuild/msbuild-multitargeting-overview.md) | Shows how to target multiple .NET versions and/or multiple platforms. |
218218
| [Obtaining build logs](obtaining-build-logs-with-msbuild.md) | Describes how to log build events, messages, and errors. |
219219
| [How MSBuild builds projects](build-process-overview.md) | Describes the internal build process used within MSBuild |
220+
| [Secure MSBuild usage best practices](msbuild-security-best-practices.md) | Describes the best practices to configure and run your builds with MSBuild |
220221
| [Create a custom task for code generation](tutorial-custom-task-code-generation.md) | Shows how to create a custom task, with a code example. |
221222
| [Use MSBuild to generate a REST API client](tutorial-rest-api-client-msbuild.md) | Shows how to extend the build to handle REST API client generation, with a code example. |
222223
| [Additional resources](https://social.msdn.microsoft.com/forums/vstudio/home?forum=msbuild) | Lists community and support resources for more information about MSBuild. |

docs/msbuild/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ items:
188188
href: customize-cpp-builds.md
189189
- name: Handle code generation
190190
href: customize-builds-for-generated-files.md
191+
- name: MSBuild security best practices
192+
href: msbuild-security-best-practices.md
191193
- name: MSBuild best practices
192194
href: msbuild-best-practices.md
193195
- name: Logging

0 commit comments

Comments
 (0)