Skip to content

Fix Containers digest test failures and align digest validation closer to OCI spec#53933

Merged
lbussell merged 7 commits intodotnet:mainfrom
lbussell:containers-test-failures
Apr 17, 2026
Merged

Fix Containers digest test failures and align digest validation closer to OCI spec#53933
lbussell merged 7 commits intodotnet:mainfrom
lbussell:containers-test-failures

Conversation

@lbussell
Copy link
Copy Markdown
Member

@lbussell lbussell commented Apr 16, 2026

This PR fixes #53918.

The root cause is that #53724 used the wrong regex for digest validation.

It used ReferenceParser.DigestRegexp.IsMatch(digestString) to validate the digest, which was not anchored. This caused multiple tests to fail.

This PR contains several changes:

  • Added a new regex (AnchoredDigestRegexp) to ReferenceParser.cs which captures the encoded value as well as the algorithm.
  • Updated the digest algorithms in ReferenceParser.cs to adhere strictly to the OCI spec instead of the golang reference implementation. This is a slight departure from the existing code in ReferenceParser.cs.
  • Moved digest format validation from ContentStore.cs to DigestUtils.cs.
    • I tried to make it hashing-algorithm-agnostic as described in the spec, but it's not practical without further refactoring. SHA-256 and Blake3 algorithms could have hash collision in ContentStore.cs. That type of change is out of scope for this PR which is just intending to get tests passing again. The limitation is called out in the code.
  • I renamed methods in DigestUtils.cs to be more technically accurate.
  • Tests target DigestUtils instead of ContentStore as appropriate.

Permalinks to all referenced specs are contained in the doc comments in the code.

It is worth noting that these are all band-aid fixes. The long term fix is Use Oras .NET library for interacting with OCI registries in Microsoft.NET.Build.Containers (dotnet/sdk#53691). The ORAS .NET library does better at validating digests, validating content, and adhering to the OCI spec.

@lbussell lbussell requested a review from a team as a code owner April 16, 2026 23:49
@github-actions github-actions Bot added the Area-Containers Related to dotnet SDK containers functionality label Apr 16, 2026
@lbussell lbussell changed the title Fix Containers digest test failures Fix Containers digest test failures and align digest validation closer to OCI spec Apr 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes container digest-related unit test failures by tightening digest parsing/validation to use an anchored OCI-spec-based regex and by centralizing digest validation in DigestUtils.

Changes:

  • Introduces ReferenceParser.AnchoredDigestRegexp (anchored + capturing groups) and updates digest grammar to align with the OCI image-spec.
  • Moves digest validation to DigestUtils.GetEncoded(...) and updates call sites to use the new API/method names.
  • Updates/adds unit tests to validate DigestUtils behavior and uses valid SHA-256 digests in registry tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
test/Microsoft.NET.Build.Containers.UnitTests/RegistryTests.cs Replaces placeholder digests with valid SHA-256 digests so blob download tests exercise realistic inputs.
test/Microsoft.NET.Build.Containers.UnitTests/DigestUtilsTests.cs Adds focused tests for digest parsing/validation and SHA-256 digest/hash helpers.
test/Microsoft.NET.Build.Containers.UnitTests/ContentStoreTests.cs Narrows ContentStore tests now that digest format validation is owned by DigestUtils.
src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateImageIndex.cs Uses DigestUtils.GetEncoded to extract the encoded SHA for local-registry blob paths.
src/Containers/Microsoft.NET.Build.Containers/ReferenceParser.cs Adds anchored digest regex with capture groups and updates digest grammar components.
src/Containers/Microsoft.NET.Build.Containers/ManifestV2.cs Switches to renamed digest helper (ComputeSha256Digest).
src/Containers/Microsoft.NET.Build.Containers/LocalDaemons/DockerCli.cs Uses renamed digest helpers and GetEncoded when computing blob paths.
src/Containers/Microsoft.NET.Build.Containers/ImageBuilder.cs Uses renamed SHA-256 helpers (ComputeSha256, FormatSha256Digest).
src/Containers/Microsoft.NET.Build.Containers/DigestUtils.cs Implements centralized digest validation via anchored parsing + registered-algorithm enforcement.
src/Containers/Microsoft.NET.Build.Containers/ContentStore.cs Replaces direct regex validation/substr logic with DigestUtils.GetEncoded.

Comment thread src/Containers/Microsoft.NET.Build.Containers/DigestUtils.cs Outdated
Comment thread src/Containers/Microsoft.NET.Build.Containers/ReferenceParser.cs Outdated
Copy link
Copy Markdown
Member

@baronfel baronfel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a delight to read. Thanks for the refactoring and complete doc comments!

Comment thread src/Containers/Microsoft.NET.Build.Containers/DigestUtils.cs
@lbussell lbussell requested a review from mthalman April 17, 2026 15:40
@lbussell
Copy link
Copy Markdown
Member Author

/ba-g All failures appear to be instances of #53869

@lbussell lbussell merged commit 6007ac8 into dotnet:main Apr 17, 2026
19 of 24 checks passed
@lbussell lbussell deleted the containers-test-failures branch April 17, 2026 20:04
marcpopMSFT added a commit that referenced this pull request Apr 17, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
marcpopMSFT added a commit that referenced this pull request Apr 17, 2026
… from #53933 and additional test fixes

# Conflicts:
#	src/Containers/Microsoft.NET.Build.Containers/ContentStore.cs
#	src/Containers/Microsoft.NET.Build.Containers/DigestUtils.cs
#	src/Containers/Microsoft.NET.Build.Containers/LocalDaemons/DockerCli.cs
#	src/Containers/Microsoft.NET.Build.Containers/ManifestV2.cs
#	src/Containers/Microsoft.NET.Build.Containers/Registry/Registry.cs
#	test/dotnet.Tests/CommandTests/Run/GivenDotnetRunBuildsCsProj.cs
#	test/dotnet.Tests/CommandTests/Run/GivenDotnetRunBuildsVbProj.cs
#	test/dotnet.Tests/CommandTests/Run/RunCommandTests.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Containers Related to dotnet SDK containers functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Known Build Error] Invalid digest failures in Containers.UnitTests

4 participants