Skip to content

fix(oci): verify the manifests fetched against their digest, and take only complete cache entries - #14490

Merged
ndeloof merged 3 commits into
docker:mainfrom
ndeloof:oci-verify-manifest-digest
Nov 11, 2026
Merged

ndeloof merged 3 commits into
docker:mainfrom
ndeloof:oci-verify-manifest-digest

Conversation

@ndeloof

@ndeloof ndeloof commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

What this PR does, in one sentence

Compose now checks that the OCI manifests it fetches have the digest they are requested or resolved by, and only takes a directory of its cache for an entry when it holds the manifest of the digest it is named after.

Context

oci:// references are pulled into a cache of extracted projects, one directory for each digest. Manifests were used as the registry served them, without checking them against the digest they were requested by, as the layers already were, and a directory of the cache was taken for an entry as soon as it existed.

This follows #14462 (atomic extraction of an entry) and #14475 (the git/ and oci/ directories of the cache).

What the PR brings

  • Manifests fetched through oci.Get are verified against the digest they are requested or resolved by, as the blobs of the layers already were.
  • Each entry of the oci directory of the cache holds the manifest it was extracted from, as the registry served it (the index when the reference is an image index), in a .manifest/manifest.json file. An artifact writes the files of an entry by names without a path separator, so it cannot write inside a directory of it, and Compose refuses an artifact that has a file named .manifest. The entry is extracted aside, the manifest is written last, and the directory is moved in place in one step, so an entry is never in place without its manifest, and a concurrent run never sees one that is not complete. The check needs no registry, so it also holds offline.
  • A directory is an entry for a digest only if it is named after it and holds the manifest that has it. What earlier releases cached sits at the root of the cache, where nothing looks for it any more, so a remote resource is fetched again the first time it is used after the upgrade. The old directories are not removed.
  • A directory of the new location that is not an entry, because an extraction was interrupted, is replaced rather than used.
  • A manifest is read as the kind of document it says it is, and nothing is kept of an artifact that has no compose file.

The three commits are specific to this change: the verification of the manifests, the cache entry, and the reading of a manifest as the document it says it is.

🤖 Generated with Claude Code

Get returned the manifest the registry served without checking it against
the digest of the descriptor: the one of the reference when it is digested,
the one the registry announces in Docker-Content-Digest when it is a tag.
Neither is checked by containerd on this path, which doesn't go through its
content store, and GetBlob already checks what it fetches for that reason.

The extracted project cache is keyed by that digest alone, whatever the
registry, so a registry could have its content cached as the one of another
manifest, which a later load of the pinned digest, from the registry that
holds it, then returned.

Get now fails when the manifest doesn't hash to its digest, and shares the
check with GetBlob.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
…ds the manifest of its digest

An entry of the cache of extracted projects was taken as complete as soon as
its directory, or a marker file in it, was there. An artifact chooses, through
the annotations of its layers, the files it writes in that directory, so it can
write a marker itself, and so did what releases that did not verify the
manifests they fetched had cached there: neither proves that the directory is
the extraction of the artifact it is named after, or that the extraction ended.

The entries of OCI artifacts, in the oci directory of the cache, now hold the
manifest they were extracted from, as the registry served it (the index when
the reference is an image index), in a .manifest directory of their own. An
artifact names the files of an entry without a path separator, so it can't
write in a directory of it: what is in .manifest was put there by Compose,
which refuses an artifact that has a file of that name. A directory is an entry
for a digest only when it is named after it and holds the manifest that has it,
a check which needs no registry and holds offline. A directory without one, or
with the manifest of another digest, is not an entry.

To make the manifest mean that the entry is complete, the entry is extracted
aside, the manifest is written last, and the directory is moved in place in one
step: nothing is in place which is not complete, so a run never finds an entry
without its manifest. What is in place and is not an entry is set aside and
replaced, never completed, and a run that finds the entry in place after another
run uses it. What earlier releases cached is at the root of the cache, where
nothing looks for it any more.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
…hing of one with no compose file

The media type a registry reports with a manifest is not authenticated, only
the content is: a registry could serve the authentic bytes of an image index
under the media type of a manifest, which was then read as one, extracted as
nothing and cached as a complete entry that holds no compose file.

The document is now read as what it says it is, and a registry that reports
another type for it is refused. An entry is only put in place once it holds the
compose file that is loaded from it.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloof requested review from a team as code owners November 11, 2026 07:40

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assessment: 🟡 NEEDS ATTENTION

Comment thread pkg/remote/oci.go
if err := json.Unmarshal(content, &document); err != nil {
return false, fmt.Errorf("invalid manifest %s: %w", descriptor.Digest, err)
}
isIndex := images.IsIndexType(document.MediaType) || (document.MediaType == "" && document.Manifests != nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[medium] json.RawMessage null decoded as non-nil, misclassifying {"manifests": null} as an image index

json.RawMessage is a []byte, not a pointer. When the JSON payload contains "manifests": null, the Go JSON decoder stores the four bytes []byte("null") — a non-nil slice — rather than setting the field to nil. This means the guard document.Manifests != nil is true even when the manifests list is explicitly null, causing isIndexDocument to return true and resolveComposeManifest to be called, which then returns a confusing "OCI index doesn't refer to compose artifacts" error rather than processing it as a plain manifest.

The fix is to also exclude the JSON null literal:

Suggested change
isIndex := images.IsIndexType(document.MediaType) || (document.MediaType == "" && document.Manifests != nil)
isIndex := images.IsIndexType(document.MediaType) || (document.MediaType == "" && document.Manifests != nil && string(document.Manifests) != "null")

Alternatively, changing the field type to []json.RawMessage (a slice of objects) would decode JSON null to a Go nil slice naturally and also check the array length in one shot.

Confidence Score
🟢 strong 100/100

Comment thread pkg/remote/oci.go
func writeCacheEntryManifest(dir string, manifest []byte) error {
holder := filepath.Join(dir, cacheEntryManifestDir)
if err := os.Mkdir(holder, 0o700); err != nil {
return fmt.Errorf("invalid OCI artifact: %w", err)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[low] writeCacheEntryManifest attributes all os.Mkdir failures to the artifact

The error message "invalid OCI artifact" is returned for any failure of os.Mkdir(holder, 0o700), including host-level causes such as permission errors on the cache directory or out-of-disk-space conditions — not only the case the comment describes (the artifact wrote a file named .manifest). The underlying OS error is wrapped inside the returned error, so the full chain is still visible, but the outer message misleads callers into thinking the artifact itself is malformed when the host environment is actually the issue.

Consider distinguishing the artifact-collision case explicitly:

Suggested change
return fmt.Errorf("invalid OCI artifact: %w", err)
if err := os.Mkdir(holder, 0o700); err != nil {
if os.IsExist(err) {
return fmt.Errorf("invalid OCI artifact: %w", err)
}
return err
}
Confidence Score
🟢 strong 100/100

@ndeloof
ndeloof merged commit fb1fd12 into docker:main Nov 11, 2026
61 of 66 checks passed
@ndeloof
ndeloof deleted the oci-verify-manifest-digest branch November 11, 2026 07:53
@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.91667% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
pkg/remote/oci.go 97.36% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

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