In this lesson, you will learn how to shift security left by integrating automated vulnerability scanning into your .NET CI/CD pipeline before images reach your container registry.
The Problem of Stale Dependencies
Even with optimized multi-stage Dockerfiles and careful layer caching, your container images are only as secure as the NuGet packages and OS libraries they contain. A vulnerability discovered in a base library (like libssl or a malicious NuGet package) can compromise your production environment. Relying on manual checks is insufficient; we must automate the discovery of Common Vulnerabilities and Exposures (CVEs) during the build process.
Integrating Scanning into the Build Pipeline
Instead of waiting for an image to sit in a registry to be scanned by cloud-native tools, you can incorporate vulnerability scanning directly into your build orchestration. For .NET projects, this often involves two layers:
- Application Dependencies: Using
dotnet list package --vulnerableto identify known security issues in your NuGet packages during the CI build stage. - Container Layer Scanning: Utilizing tools like
TrivyorSnykwithin your CI pipeline (e.g., GitHub Actions, Azure DevOps) to inspect the filesystem of the container image immediately after thedocker buildcommand.
Example CI Workflow Step
In your CI YAML file, you can trigger a scan immediately after building the image to fail the build if high-severity vulnerabilities are found:
- name: Build Docker Image
run: docker build -t my-app:${{ github.sha }} .
- name: Run Vulnerability Scan
run: |
# Scan the image for OS and language-specific vulnerabilities
trivy image --severity HIGH,CRITICAL --exit-code 1 my-app:${{ github.sha }}
By setting the --exit-code 1 flag, you ensure that any security breach in your dependencies prevents the image from being pushed to your registry. This creates a "secure-by-default" gate, ensuring that the container registry remains a repository of trusted, vetted artifacts rather than a collection of forgotten, vulnerable images.
Key Takeaway: Automating vulnerability scanning as a build-breaking gate in your CI pipeline is the most effective way to prevent vulnerable .NET dependencies and base OS packages from ever reaching your production registry.