Skip to content

AD2027: EnableSourceLink

Summary

Property Value
ID AD2027
Name EnableSourceLink
Category Maintainability
Severity Note
Applies to PE (Windows) with PDB files

Description

Binaries should include SourceLink information in their PDB files. SourceLink enables debuggers to automatically download matching source code from source control, improving the debugging and incident response experience for both developers and security researchers.

How It Works

The rule examines PDB files for the presence of SourceLink JSON data. This data maps local file paths to source control URLs, allowing debuggers to fetch the exact source version.

Why This Matters

SourceLink bridges the gap between compiled binaries and their source code, enabling precise debugging and security analysis. When a security incident occurs, SourceLink can mean the difference between hours of investigation and immediate root cause analysis.

The Debugging Problem

Without SourceLink, debugging production crashes is painful:

1. Get crash dump with stack trace
2. Which version of the code is this?
3. Hunt through source control history
4. Manually match commit to binary version
5. Checkout correct version
6. Finally, examine the code

Time: Hours to days

With SourceLink:

1. Get crash dump with stack trace
2. Open in debugger
3. Source automatically downloaded from correct commit
4. Immediate investigation

Time: Minutes

Security Investigation Value

SourceLink accelerates security response:

Scenario Without SourceLink With SourceLink
Crash triage Manual version matching Automatic
Vulnerability analysis Which code has the bug? Click to see exact line
Patch verification Compare binary to source Direct correlation
Forensics Which source produced this? Cryptographic proof

Supply Chain Security

SourceLink contributes to supply chain verification:

  1. Provenance: Links binary to specific source control commit
  2. Reproducibility: Supports reproducible build verification
  3. Audit trail: Documents exactly what code was compiled
  4. Integrity: Source URLs in PDB provide verification path
// SourceLink JSON in PDB
{
  "documents": {
    "C:\\src\\*": "https://raw.githubusercontent.com/org/repo/commit/*"
  }
}

The debugger: 1. Reads local path from PDB 2. Maps through SourceLink rules 3. Fetches source from URL 4. Displays in debugger

NuGet Package Best Practice

For published packages, SourceLink is essential:

Package Type SourceLink Value
Open source Users can debug into your code
Commercial Support can investigate issues
Internal Any team can debug

Security Considerations

SourceLink URLs should point to authenticated, versioned sources:

Good Bad
GitHub commit URL Latest branch URL
Internal git with auth Public unauthenticated
Immutable reference Mutable reference

Privacy Considerations

SourceLink URLs are embedded in PDBs:

  • Public packages: Source URLs are intentionally public
  • Private software: Ensure URLs don't leak internal infrastructure
  • Sensitive code: Consider if source URL exposure is acceptable

For private code, strip SourceLink data from publicly distributed PDBs.

Resolution

Install the SourceLink NuGet package for your source control:

GitHub:

<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />

Azure DevOps:

<PackageReference Include="Microsoft.SourceLink.AzureRepos.Git" Version="1.1.1" PrivateAssets="All" />

GitLab:

<PackageReference Include="Microsoft.SourceLink.GitLab" Version="1.1.1" PrivateAssets="All" />

MSBuild Properties

Enable SourceLink in your project:

<PropertyGroup>
  <PublishRepositoryUrl>true</PublishRepositoryUrl>
  <EmbedUntrackedSources>true</EmbedUntrackedSources>
  <IncludeSymbols>true</IncludeSymbols>
  <SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

CMake

For CMake projects, use a post-build step or custom tooling to inject SourceLink data.

When to Suppress

This rule may be suppressed for:

  • Private/internal code: When source shouldn't be externally accessible
  • Security concerns: When exposing source URLs is problematic
  • No source control: Projects not using Git
  • Build system limitations: When SourceLink can't be integrated

Caveats

  • Source must be accessible from where debugging occurs
  • Private repos need authentication configured
  • Only works with supported source control providers
  • Requires proper CI/CD configuration
Provider Package
GitHub Microsoft.SourceLink.GitHub
Azure DevOps Microsoft.SourceLink.AzureRepos.Git
GitLab Microsoft.SourceLink.GitLab
Bitbucket Microsoft.SourceLink.Bitbucket.Git
gitea Microsoft.SourceLink.Gitea

Check if SourceLink is embedded:

# Using sourcelink tool
dotnet tool install -g sourcelink
sourcelink print-urls MyAssembly.pdb

References