From 728c6d23a30f4afe2b9aac803172344894a7eaed Mon Sep 17 00:00:00 2001 From: Shareef Jalloq Date: Wed, 11 Nov 2026 22:27:41 +0000 Subject: [PATCH 1/2] test(client): show that an ignored artifactType filter is not applied A registry may ignore the `artifactType` filter of the referrers API. It reports a filter that it applied in the `OCI-Filters-Applied` header. `pull_referrers` does not read the header and does not filter the response, so the caller gets every referrer. The first two tests describe the expected behaviour. They are ignored because they fail until the fix in the next commit. The other two tests pass already: they show that the response of a registry which reports the filter is returned as it is. Refs: #318 Signed-off-by: Shareef Jalloq --- tests/referrers_filters_applied.rs | 146 +++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 tests/referrers_filters_applied.rs diff --git a/tests/referrers_filters_applied.rs b/tests/referrers_filters_applied.rs new file mode 100644 index 00000000..ef71435a --- /dev/null +++ b/tests/referrers_filters_applied.rs @@ -0,0 +1,146 @@ +// The distribution specification lets a registry ignore the `artifactType` filter of the +// referrers API: +// +// > If filtering is requested and applied, the response MUST include a header +// > `OCI-Filters-Applied: artifactType` denoting that an `artifactType` filter was applied. +// +// https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#listing-referrers +// +// When the registry does not report the filter, the client must apply it. Version 1.1.0-rc1 +// of the specification reported the filter in an annotation of the index, which the client +// also accepts. +use std::collections::HashMap; + +use axum::{ + extract::Query, + http::{HeaderMap, StatusCode}, + routing::get, + Router, +}; +use oci_client::{ + client::{ClientConfig, ClientProtocol}, + Client, Reference, +}; +use tokio::net::TcpListener; + +const DIGEST: &str = "sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; +const INDEX_MEDIA_TYPE: &str = "application/vnd.oci.image.index.v1+json"; +const SBOM: &str = "application/vnd.example.sbom.v1"; +const SIGNATURE: &str = "application/vnd.example.signature.v1"; + +/// How the registry reports the filters it applied. +#[derive(Clone, Copy)] +enum Report { + Nothing, + Header(&'static str), + Annotation(&'static str), +} + +/// An image index with one SBOM referrer and one signature referrer: the registry did not +/// filter, whatever it reports. +fn unfiltered_index(report: Report) -> String { + let manifests: Vec<_> = [SBOM, SIGNATURE] + .iter() + .enumerate() + .map(|(i, artifact_type)| { + serde_json::json!({ + "mediaType": "application/vnd.oci.image.manifest.v1+json", + "size": 1, + "digest": format!("sha256:{:064x}", i + 1), + "artifactType": artifact_type, + }) + }) + .collect(); + let mut index = serde_json::json!({ + "schemaVersion": 2, + "mediaType": INDEX_MEDIA_TYPE, + "manifests": manifests, + }); + if let Report::Annotation(filters) = report { + index["annotations"] = + serde_json::json!({ "org.opencontainers.referrers.filtersApplied": filters }); + } + index.to_string() +} + +/// A registry whose referrers endpoint answers with both referrers and reports its filters +/// as `report` says. It refuses a request that does not ask for the SBOM artifact type. +async fn spawn(report: Report) -> String { + let router = Router::new().route( + &format!("/v2/repo/referrers/{DIGEST}"), + get( + move |Query(query): Query>| async move { + if query.get("artifactType").map(String::as_str) != Some(SBOM) { + return Err(StatusCode::BAD_REQUEST); + } + let mut headers = HeaderMap::new(); + headers.insert("Content-Type", INDEX_MEDIA_TYPE.parse().unwrap()); + if let Report::Header(filters) = report { + headers.insert("OCI-Filters-Applied", filters.parse().unwrap()); + } + Ok((headers, unfiltered_index(report))) + }, + ), + ); + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let address = listener.local_addr().unwrap().to_string(); + tokio::spawn(async move { axum::serve(listener, router).await.unwrap() }); + address +} + +/// The artifact types of the SBOM referrers that the client returns. +async fn sbom_referrers(report: Report) -> Vec> { + let registry = spawn(report).await; + let client = Client::new(ClientConfig { + protocol: ClientProtocol::Http, + ..Default::default() + }); + let subject = Reference::try_from(format!("{registry}/repo@{DIGEST}")).unwrap(); + client + .pull_referrers(&subject, Some(SBOM)) + .await + .unwrap() + .manifests + .into_iter() + .map(|entry| entry.artifact_type) + .collect() +} + +fn only_sbom() -> Vec> { + vec![Some(SBOM.to_string())] +} + +fn as_the_registry_sent_it() -> Vec> { + vec![Some(SBOM.to_string()), Some(SIGNATURE.to_string())] +} + +#[tokio::test] +#[ignore = "fails until the fix in the next commit"] +async fn filters_when_the_registry_reports_no_filter() { + assert_eq!(sbom_referrers(Report::Nothing).await, only_sbom()); +} + +#[tokio::test] +#[ignore = "fails until the fix in the next commit"] +async fn filters_when_the_registry_reports_another_filter() { + assert_eq!(sbom_referrers(Report::Header("digest")).await, only_sbom()); +} + +// The registry did not filter in the two tests below, so they show that the client reads +// the report of the registry, and does not filter a second time. + +#[tokio::test] +async fn trusts_the_header_of_a_registry_that_applied_the_filter() { + assert_eq!( + sbom_referrers(Report::Header("digest,artifactType")).await, + as_the_registry_sent_it() + ); +} + +#[tokio::test] +async fn trusts_the_annotation_of_a_registry_that_applied_the_filter() { + assert_eq!( + sbom_referrers(Report::Annotation("artifactType")).await, + as_the_registry_sent_it() + ); +} From a61f30290f33a01834973710cd1ffd418d8f7f30 Mon Sep 17 00:00:00 2001 From: Shareef Jalloq Date: Wed, 11 Nov 2026 22:28:00 +0000 Subject: [PATCH 2/2] fix(client): filter referrers when the registry ignores artifactType A registry may ignore the `artifactType` filter of the referrers API. It reports a filter that it applied in the `OCI-Filters-Applied` header. `pull_referrers` did not read the header and did not filter the response, so the caller got every referrer from such a registry. `pull_referrers` now filters the response itself, unless the registry reports that it applied the filter. This follows `referrersPageByAPI` in oras-go: the report is read from the header, or from the index annotation `org.opencontainers.referrers.filtersApplied` that version 1.1.0-rc1 of the distribution specification used. An entry of the list must be equal to `artifactType`. Fixes: #318 Signed-off-by: Shareef Jalloq --- src/client.rs | 44 ++++++++++++++++++++++++++++-- tests/referrers_filters_applied.rs | 2 -- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index aaee5be1..1281e2f2 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1755,6 +1755,10 @@ impl Client { /// under a tag derived from the subject digest by replacing `:` with `-` /// (e.g. `sha256:abc…` → tag `sha256-abc…`). /// + /// A registry may ignore the `artifact_type` filter. It reports a filter that it + /// applied in the `OCI-Filters-Applied` header of the response. When the registry + /// does not report the filter, it is applied client-side. + /// /// When the fallback is used, `artifact_type` filtering is applied client-side, /// since the tag schema stores a single unfiltered index with no query-parameter /// support. @@ -1781,6 +1785,11 @@ impl Client { .await?; let status = res.status(); let effective_url = response_url(&res, &url); + let filters_header = res + .headers() + .get(OCI_FILTERS_APPLIED_HEADER) + .and_then(|value| value.to_str().ok()) + .map(str::to_owned); let body = transport::collect(res.into_body()).await?; // Per the OCI Distribution Spec, a 404 on the native referrers endpoint means the @@ -1796,10 +1805,27 @@ impl Client { } validate_registry_response(status, &body, &effective_url)?; - let manifest = serde_json::from_slice(&body) + let mut index: OciImageIndex = serde_json::from_slice(&body) .map_err(|e| OciDistributionError::ManifestParsingError(e.to_string()))?; - Ok(manifest) + // The registry may ignore the filter. It reports a filter that it applied in a + // header (spec v1.1.1), or in an annotation of the index (spec v1.1.0-rc1). + if let Some(at) = artifact_type { + let filters_annotation = index + .annotations + .as_ref() + .and_then(|annotations| annotations.get(REFERRERS_FILTERS_APPLIED_ANNOTATION)) + .map(String::as_str); + if !is_referrers_filter_applied(filters_header.as_deref(), ARTIFACT_TYPE_FILTER) + && !is_referrers_filter_applied(filters_annotation, ARTIFACT_TYPE_FILTER) + { + index + .manifests + .retain(|entry| entry.artifact_type.as_deref() == Some(at)); + } + } + + Ok(index) } /// Pulls the referrers index using the OCI referrers tag schema fallback. @@ -2035,6 +2061,20 @@ fn content_length_header(headers: &HeaderMap) -> Option { headers.get(CONTENT_LENGTH)?.to_str().ok()?.parse().ok() } +/// The header in which a registry lists the filters it applied to a referrers response. +const OCI_FILTERS_APPLIED_HEADER: &str = "OCI-Filters-Applied"; +/// The index annotation which had the role of [`OCI_FILTERS_APPLIED_HEADER`] in version +/// 1.1.0-rc1 of the distribution specification. +const REFERRERS_FILTERS_APPLIED_ANNOTATION: &str = "org.opencontainers.referrers.filtersApplied"; +/// The name of the `artifactType` filter in the list of applied filters. +const ARTIFACT_TYPE_FILTER: &str = "artifactType"; + +/// Returns whether `requested` is in `applied`, the comma-separated list of the filters +/// that a registry applied to a referrers response. +fn is_referrers_filter_applied(applied: Option<&str>, requested: &str) -> bool { + applied.is_some_and(|applied| applied.split(',').any(|filter| filter == requested)) +} + /// Returns an empty OCI Image Index, as used when no referrers exist. fn empty_image_index() -> OciImageIndex { OciImageIndex { diff --git a/tests/referrers_filters_applied.rs b/tests/referrers_filters_applied.rs index ef71435a..f3cf6691 100644 --- a/tests/referrers_filters_applied.rs +++ b/tests/referrers_filters_applied.rs @@ -115,13 +115,11 @@ fn as_the_registry_sent_it() -> Vec> { } #[tokio::test] -#[ignore = "fails until the fix in the next commit"] async fn filters_when_the_registry_reports_no_filter() { assert_eq!(sbom_referrers(Report::Nothing).await, only_sbom()); } #[tokio::test] -#[ignore = "fails until the fix in the next commit"] async fn filters_when_the_registry_reports_another_filter() { assert_eq!(sbom_referrers(Report::Header("digest")).await, only_sbom()); }