Safeguard
Supply Chain•

Can a NuGet package run code during your .NET build?

Yes. NuGet packages can ship MSBuild props and targets, analyzers and tools that take part in your build. Learn what flows where and how to limit it.

Safeguard Security Team
4 min read

Yes. A NuGet package is more than a library. It can include MSBuild props and targets files, analyzers and other assets that take part in your build, which means a package you add can influence what the build does on your machine or CI agent. You control this with asset metadata, review and clean build agents.

What a package can contribute

Microsoft's PackageReference documentation lists the asset types a package may carry and that your project can include or exclude.

AssetWhat it is
compileAssemblies in the lib folder you compile against
runtimeAssemblies copied to the build output
contentFilesFiles in the contentfiles folder
buildProps and targets files in the build folder
buildMultitargetingProps and targets for cross framework targeting
buildTransitiveProps and targets that flow transitively to consuming projects
analyzers.NET analyzers
nativeNative binaries

The build, buildMultitargeting and buildTransitive items are MSBuild files. They become part of how your project builds, not only what it references.

Why this matters for security

An ordinary dependency review looks at the code your application calls. Build assets act earlier, while compiling and packaging. A malicious or compromised package could abuse that stage. The build stage is part of the same trust decision as the library code. Treat any package that ships build assets as having a wider reach than a plain library.

Also note the default flow. For PrivateAssets, the documented default is contentfiles, analyzers and build. Because build is in that default list, props and targets from a package are kept private to the referencing project unless you change the metadata. If a package is itself packed into another package, what you expose depends on this setting.

How to limit exposure

  1. Prefer packages that need no build assets for simple libraries.
  2. Mark development only tools with PrivateAssets set to all, so they do not flow to your consumers.
  3. Use ExcludeAssets for asset types you do not want, for example build.
  4. Read the package contents before first use, since a nupkg is a zip archive.
  5. Restrict who can add dependencies by reviewing changes to project files and Directory.Packages.props.
  6. Run builds on clean, short lived agents, and keep secrets out of the build environment where possible.
<PackageReference Include="Contoso.BuildTool" Version="1.4.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; analyzers</IncludeAssets>
</PackageReference>

Where analyzers fit

Analyzers run inside the compiler. That is useful for catching issues, but it means they are code you trust at build time. The built in .NET analyzers ship with the SDK. Third party analyzers arrive as packages and deserve the same review as any dependency.

A review habit

When a pull request adds or updates a package, check three things: who publishes it, whether it carries build, analyzers or tools folders, and whether the version changed more than expected. Ask for a short note on why it is needed.

Try it yourself

Click through it at your own pace, or open it full screen. Use it to see how many packages sit behind your direct references, each with its own assets.

Safeguard's dependency scanning and SBOM output give you the inventory to review, and a free scan is a quick way to get one.

Quick check

Do I need to worry about build assets from well known packages?

Apply the same review to every package, but focus more where a package ships build or analyzer assets, or where its maintainers recently changed.

What does PrivateAssets all do?

It keeps the package's assets from flowing to projects that consume your project or package.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.