Skip to content

mutate: verify layer digests in Extract and Time - #2303

Merged
Subserial merged 3 commits into
google:mainfrom
momenashrafff:fix/mutate-verify-layer-digest
May 20, 2026
Merged

Subserial merged 3 commits into
google:mainfrom
momenashrafff:fix/mutate-verify-layer-digest

Conversation

@momenashrafff

@momenashrafff momenashrafff commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

What

extractLayer and layerTime in pkg/v1/mutate/mutate.go read a layer through an archive/tar reader. archive/tar returns io.EOF from Next() as soon as it reads the two-block end-of-archive marker, so the loop stops before the underlying layer stream reaches its own io.EOF.

internal/verify's reader checks the layer's SHA-256 against the expected digest only when the wrapped stream returns io.EOF (verifyReader.Read). Because the tar loop stops early that point is never reached, and Close() does not verify either. As a result mutate.Extract and mutate.Time, and crane export / crane edit which build on mutate.Extract, accept a layer whose contents do not match the layer digest listed in the manifest.

crane pull and pkg/v1/validate are not affected: they read each layer to io.EOF, so the digest check fires.

Fix

pkg/v1/validate/layer.go already drains the stream after its tar.Reader loop for this reason:

// Discard any trailing padding that the tar.Reader doesn't consume.
if _, err := io.Copy(io.Discard, hashUncompressed); err != nil {

This change adds the same drain after the tar.Reader loop in extractLayer and layerTime, so the underlying verifying reader reaches io.EOF and the layer digest is checked.

Testing

  • A layer whose bytes do not match the manifest's layer digest: crane export built from this branch fails with error verifying sha256 checksum instead of succeeding. Built from current main it succeeds.
  • Honest images extract and pull unchanged.
  • go test ./pkg/v1/mutate/... passes.

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented May 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

extractLayer and layerTime read a layer through an archive/tar reader, which stops at the tar end-of-archive marker before the layer stream reaches io.EOF. The verifying reader in internal/verify checks the layer digest only at io.EOF, so on these paths the digest is never verified: mutate.Extract and mutate.Time accept a layer whose contents do not match the layer digest in the manifest.

pkg/v1/validate/layer.go already drains the stream after its tar loop for this reason. Add the same drain to extractLayer and layerTime so the verifying reader reaches io.EOF and the digest is checked.
@momenashrafff
momenashrafff force-pushed the fix/mutate-verify-layer-digest branch 2 times, most recently from 1e891d2 to b96b250 Compare May 18, 2026 20:39
@codecov-commenter

codecov-commenter commented May 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.90%. Comparing base (e5fd9d1) to head (6ae051b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2303      +/-   ##
==========================================
+ Coverage   56.85%   56.90%   +0.05%     
==========================================
  Files         165      165              
  Lines       11355    11359       +4     
==========================================
+ Hits         6456     6464       +8     
+ Misses       4136     4134       -2     
+ Partials      763      761       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Subserial

Copy link
Copy Markdown

Please provide tests that model the scenario you are describing.

Add regression tests for the digest check added in the previous commit.
Each test builds a layer whose Uncompressed stream is wrapped by
internal/verify against a digest that does not match the bytes served --
modelling a layer whose contents disagree with the manifest's layer
digest -- and asserts that Extract and Time reject it.

The tests also cover the matching-digest case so the success path is
exercised. Without the drain added to extractLayer and layerTime the
mismatch cases pass through undetected; both fail if the fix is reverted.
@momenashrafff

Copy link
Copy Markdown
Contributor Author

Hello @Subserial, thank you for your comment

I just added tests in pkg/v1/mutate/mutate_test.go (5f9072a).

TestExtractVerifiesLayerDigest and TestTimeVerifiesLayerDigest each build a layer whose Uncompressed() stream is wrapped by internal/verify against a digest that doesn't match the bytes it serves, which is the case described in the PR. Both assert the operation fails with a checksum error, and both have a matching-digest case to cover the normal path.

I reverted the fix locally to check the tests actually catch it: the mismatch cases pass through silently without the drain and fail with it. go test ./pkg/v1/mutate/... is green.

@Subserial
Subserial merged commit d807d51 into google:main May 20, 2026
17 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
crane 0.21.7

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* tarball: return error instead of panicking on missing rootfs.diff_ids by @iahsanGill in google/go-containerregistry#2304
* gcrane: honor --platform flag in copy by @iahsanGill in google/go-containerregistry#2307
* mutate: verify layer digests in Extract and Time by @momenashrafff in google/go-containerregistry#2303
* tarball: close layer readers during Write by @nandbhat in google/go-containerregistry#2308
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2311
* build(deps): bump github.com/docker/cli from 29.4.3+incompatible to 29.5.2+incompatible in the go-deps group across 1 directory by @dependabot[bot] in google/go-containerregistry#2312
* BUGFIX: Fail with error when read exceeds maximum by @inteon in google/go-containerregistry#2328
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2327
* fix(name): anchor loopback registry detection by @rohan-patnaik in google/go-containerregistry#2314
* Reject symlinks in OCI layout blobs by @mosskappa in google/go-containerregistry#2306
* fix(crane): avoid creating export tar on pull failure by @Haihan-Jiang in google/go-containerregistry#2318
* feat(kubernetes): allow ignoring pull secrets by @rohan-patnaik in google/go-containerregistry#2315
* fix(name): preserve localhost registry references by @rohan-patnaik in google/go-containerregistry#2316
* pkg/registry: export ErrNotFound by @malt3 in google/go-containerregistry#2176
* pkg/registry: export RedirectError by @malt3 in google/go-containerregistry#2177
* build(deps): bump the go-deps group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2343
* build(deps): bump the actions group across 1 directory with 2 updates by @dependabot[bot] in google/go-containerregistry#2344
* fix: prevent SSRF in google.List() pagination by @tufstraka in google/go-containerregistry#2332
* internal/gzip: fix goroutine leak in ReadCloserLevel by @amarkdotdev in google/go-containerregistry#2347
* fix(transport): apply refreshed bearer token after cross-host redirect by @64johnlee in google/go-containerregistry#2337
* build(deps): bump the go-deps group across 3 directories with 4 updates by @dependabot[bot] in google/go-containerregistry#2348
* fix(tarball): normalize paths when matching files by @bstoll in google/go-containerregistry#2334
* transport: do not re-attach bearer token after cross-host redirect by @evilgensec in google/go-containerregistry#2349
* Bump CI go version to 1.26.4 by @Subserial in google/go-containerregistry#2350

## New Contributors
* @momenashrafff made their first contribution in google/go-containerregistry#2303
* @nandbhat made their first contribution in google/go-containerregistry#2308
* @inteon made their first contribution in google/go-containerregistry#2328
* @rohan-patnaik made their first contribution in google/go-containerregistry#2314
* @mosskappa made their first contribution in google/go-containerregistry#2306
* @Haihan-Jiang made their first contribution in google/go-containerregistry#2318
* @tufstraka made their first contribution in google/go-containerregistry#2332
* @amarkdotdev made their first contribution in google/go-containerregistry#2347
* @64johnlee made their first contribution in google/go-containerregistry#2337
* @bstoll made their first contribution in google/go-containerregistry#2334

**Full Changelog**: https://github.com/google/go-containerregistry/compare/v0.21.6...v0.21.7</pre>
  <p>View the full release notes at <a href="https://github.com/google/go-containerregistry/releases/tag/v0.21.7">https://github.com/google/go-containerregistry/releases/tag/v0.21.7</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!11861
lazedo added a commit to lazedo/kustomize that referenced this pull request Oct 23, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 2, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 7, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 8, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 9, 2026
From v0.21.7 on, go-containerregistry reads each layer to its end once
extracted, so that mutate.Extract verifies it against its digest and
size (google/go-containerregistry#2303). Bump the dependency from
v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did
this here. A tampered layer still fails the pull, now on
go-containerregistry's own checks: the digest of the uncompressed
layer, the size of the gzip one.

The bump also removes estargz, docker/distribution, go-homedir,
pkg/errors and tar-split from the dependencies of the api module.

Note that extraction stands in for a file system view of the image
until google/go-containerregistry#921 is implemented.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 18, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 18, 2026
From v0.21.7 on, go-containerregistry reads each layer to its end once
extracted, so that mutate.Extract verifies it against its digest and
size (google/go-containerregistry#2303). Bump the dependency from
v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did
this here. A tampered layer still fails the pull, now on
go-containerregistry's own checks: the digest of the uncompressed
layer, the size of the gzip one.

The bump also removes estargz, docker/distribution, go-homedir,
pkg/errors and tar-split from the dependencies of the api module.

Note that extraction stands in for a file system view of the image
until google/go-containerregistry#921 is implemented.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 25, 2026
The puller now takes an HTTP client, nil for the default one, and
sends its requests through its transport, so that tests can reach
in-memory registries and override TLS settings. The loader passes the
client it uses for remote files. As in go-containerregistry,
registries are reached over HTTPS, and plain HTTP is only tried for
local and private hosts; this is now documented.

mutate.Extract stops reading a layer at the end of its tar archive,
while go-containerregistry only verifies a blob against its digest
once it is read to its end: a tampered layer was extracted without an
error, even for a reference pinned by digest. Each layer is now read to
its end once extracted, and a mismatch fails the pull. This was fixed
in go-containerregistry v0.21.7 (google/go-containerregistry#2303);
the wrapper can go once the dependency is bumped from v0.20.6.

Files are copied to the file system instead of being read into memory
first.

ocitest builds and pushes images with crane, and runs registries with
httptest.NewTestServer: on its in-memory network, serving HTTPS only,
for the puller tests, and on the loopback interface for the build
tests, which use the default client. The registries ignore the local
docker credentials, as credential helpers may block. New tests cover
untrusted certificates, HTTP-only registries, and malformed and
tampered layers.
lazedo added a commit to lazedo/kustomize that referenced this pull request Nov 25, 2026
From v0.21.7 on, go-containerregistry reads each layer to its end once
extracted, so that mutate.Extract verifies it against its digest and
size (google/go-containerregistry#2303). Bump the dependency from
v0.20.6 to v0.22.1, the latest release, and drop the wrapper that did
this here. A tampered layer still fails the pull, now on
go-containerregistry's own checks: the digest of the uncompressed
layer, the size of the gzip one.

The bump also removes estargz, docker/distribution, go-homedir,
pkg/errors and tar-split from the dependencies of the api module.

Note that extraction stands in for a file system view of the image
until google/go-containerregistry#921 is implemented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants