Repository navigation
fix(oci): verify the manifests fetched against their digest, and take only complete cache entries - #14490
Conversation
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>
docker-agent
left a comment
There was a problem hiding this comment.
Assessment: 🟡 NEEDS ATTENTION
| 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) |
There was a problem hiding this comment.
[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:
| 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 |
| 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) |
There was a problem hiding this comment.
[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:
| 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 |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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/andoci/directories of the cache).What the PR brings
oci.Getare verified against the digest they are requested or resolved by, as the blobs of the layers already were.ocidirectory 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.jsonfile. 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.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