Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -2035,6 +2061,20 @@ fn content_length_header(headers: &HeaderMap) -> Option<u64> {
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 {
Expand Down
144 changes: 144 additions & 0 deletions tests/referrers_filters_applied.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// 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<HashMap<String, String>>| 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<Option<String>> {
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<Option<String>> {
vec![Some(SBOM.to_string())]
}

fn as_the_registry_sent_it() -> Vec<Option<String>> {
vec![Some(SBOM.to_string()), Some(SIGNATURE.to_string())]
}

#[tokio::test]
async fn filters_when_the_registry_reports_no_filter() {
assert_eq!(sbom_referrers(Report::Nothing).await, only_sbom());
}

#[tokio::test]
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()
);
}