Strengthening Security in .NET Development with packages.lock.json

Anand Upadhayay
Anand Upadhayay

In the ever-evolving landscape of .NET development, security is a paramount concern. Maintaining a secure development/runtime environment can be challenging as projects become more complex and rely on numerous third-party libraries and frameworks. This makes their maintenance more challenging due to dealing with multiple versions while ensuring the versions are the expected ones. The support of the lock file in Nuget 4.9 and above has revolutionized how developers maintain the consistency of third-party libraries in their .NET projects. In this blog post, we will explore how `packages.lock.json can help maintain a secure .NET development and why it should be an integral part of your development workflow. A quick look into all the OSS projects on GitHub shows that out of total 2.5M c# projects, only 4.9k are using the lock file to manage the nuget package dependencies.

What is packages.lock.json?

In a .NET project, the packages.lock.json is a file that can be automatically generated, that is used by the NuGet package manager to ensure consistent package installations across different environments. The packages.lock.json file precisely records crucial information about the package’s requested version, resolved version, dependencies, contentHash, and their types that are direct/Transitive. This lock file locks the dependency graph of a .NET project. It ensures consistent restoration and usage of identical package versions across diverse environments, thus averting unintentional upgrades or changes that might introduce security vulnerabilities.

How to enable the lock file?

To enable the lock file in the dotnet restore/build you can use an MSBuild property in the *.csproj file.


This will create a `packages.lock.json` file alongside the *.csproj file during a restore/build that can be safely checked into the repository source.

Alternatively, The lock file can also be generated during the restore process by using the flag `--use-lock-file`. By default, the lock file will be generated in $PROJECT_ROOT directory which can be changed using `--lock-file-path`  flag.

Benefits of packages.lock.json in Maintaining Secure .NET Development

1. Facilitating Secure Version Pinning:

Including the `packages.lock.json` file in your source control establishes a reference point for secure package versions. This lock file precisely documents the versions of each package no matter direct or transitive, thereby preventing unexpected updates or downgrades during the package restoration or build process. Secure version pinning eliminates the potential introduction of security vulnerabilities arising from untested or incompatible package versions.

2. Controlled Package Updates:

Regular package updates are crucial for addressing security vulnerabilities and incorporating bug fixes. However, haphazardly updating packages without proper testing or consideration can introduce compatibility issues or security risks. The packages.lock.json file acts as a safeguard, allowing you to selectively update packages while maintaining a controlled and secure development environment. By carefully reviewing package updates, assessing their impact, and conducting thorough testing, you can ensure a secure and stable project.

3. Enhancing Reproducibility:

Reproducible builds are imperative for security audits, debugging, and ensuring consistent behavior across various environments. The packages.lock.json file plays a pivotal role in achieving reproducibility. Capturing precise package versions, enables the recreation of the same development environment, ensuring consistent results and facilitating the identification and resolution of security-related issues. The lock file is modified each time a direct/indirect dependency is modified and that can be controlled using `--locked-mode`  to fail if the dependency graph is changed silently which makes sense for a lot of CI/CD environments.

How does Endor Labs use the lock file?

Endor Labs scan fully supports dependency scanning and vulnerability listing using the `packages.lock.json` file. When the `packages.lock.json` file is present in the repository, it accurately reads all the dependency information and creates a comprehensive dependency graph. It then lists any vulnerabilities associated with these dependencies in the UI. In cases where the lock file is not found in the repository, but the `*.csproj` file contains the `RestorePackagesWithLockFile` attribute, the restore process will be triggered. This process generates the lock file, which is subsequently used for dependency scanning and vulnerability listing.


Maintaining a secure .NET development environment is a top priority for both development and security teams, considering that modern projects have a vast array of dependencies. The packages.lock.json file, provided by NuGet, offers a robust solution to enhance security and maintain consistent dependencies. The packages.lock.json file is an indispensable tool in the arsenal of any .NET developer concerned with security and the build reproducibility. you can incorporate this file into your development workflow and take significant strides toward achieving a secure and stable .NET development workflow. You can read more about locking dependencies in the Microsoft documentation and in an official blog post.