~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
466 lines · 14.2 KB Raw History
//! Docker/OCI release source tests.
//!
//! `src/release/sources/docker.rs` is compiled into `sources` by the
//! integration wave; until that wiring lands this test includes the module
//! directly and provides the `crate::` paths it uses. Every request goes
//! through the scripted [`FakeHttp`] seam; no test touches the network.

#![allow(dead_code)]

use std::collections::{BTreeMap, HashMap, VecDeque};
use std::sync::{Arc, Mutex};

use anyhow::Result;
use async_trait::async_trait;
use serde_json::{Value, json};

mod release {
    pub mod artifact {
        pub use vuln_notif::release::artifact::normalize_version;
    }
    pub mod http {
        pub use vuln_notif::release::http::{HTTP_TIMEOUT, USER_AGENT};
    }
    pub mod source {
        pub use vuln_notif::release::source::*;
    }
}

#[path = "../src/release/sources/docker.rs"]
mod docker;

use docker::{DockerHttp, DockerResponse, DockerSource, unsupported_reason};
use release::source::{
    ProjectRef, ReleaseSource, SourceCapabilities, UnsupportedReason, VersionInfo,
};

const TOKEN_FIXTURE: &str = include_str!("fixtures/sources/docker/token.json");
const TAGS_PAGE1: &str = include_str!("fixtures/sources/docker/tags-page1.json");
const TAGS_PAGE2: &str = include_str!("fixtures/sources/docker/tags-page2.json");
const MANIFEST_INDEX: &str = include_str!("fixtures/sources/docker/manifest-index.json");

const AUTH_URL: &str =
    "https://auth.docker.io/token?service=registry.docker.io&scope=repository:library/nginx:pull";
const TAGS_URL: &str = "https://registry-1.docker.io/v2/library/nginx/tags/list?n=100";
const TAGS_PAGE2_PATH: &str = "/v2/library/nginx/tags/list?n=100&last=1.24.0";
const TAGS_PAGE2_URL: &str =
    "https://registry-1.docker.io/v2/library/nginx/tags/list?n=100&last=1.24.0";
const MANIFEST_URL: &str = "https://registry-1.docker.io/v2/library/nginx/manifests/1.25.3";

const DIGEST_A: &str = "sha256:9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
const HEX_A: &str = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
const DIGEST_B: &str = "sha256:abababababababababababababababababababababababababababababababab";

fn fixture(raw: &str) -> Value {
    serde_json::from_str(raw).expect("fixture JSON")
}

// ---------------------------------------------------------------------------
// Scripted DockerHttp fake
// ---------------------------------------------------------------------------

#[derive(Clone, Default)]
struct FakeResponse {
    status: u16,
    headers: BTreeMap<String, String>,
    body: Value,
}

#[derive(Clone, Debug)]
struct FakeCall {
    url: String,
    headers: Vec<(String, String)>,
}

/// Queued replies per URL; every request is recorded so tests can assert the
/// token flow, pagination, and media-type headers.
#[derive(Default)]
struct FakeHttp {
    replies: Mutex<HashMap<String, VecDeque<FakeResponse>>>,
    calls: Mutex<Vec<FakeCall>>,
}

impl FakeHttp {
    fn new() -> Arc<Self> {
        Arc::new(Self::default())
    }

    fn enqueue(&self, url: &str, body: Value) {
        self.enqueue_response(url, 200, &[], body);
    }

    fn enqueue_response(&self, url: &str, status: u16, headers: &[(&str, &str)], body: Value) {
        self.replies
            .lock()
            .unwrap()
            .entry(url.to_owned())
            .or_default()
            .push_back(FakeResponse {
                status,
                headers: headers
                    .iter()
                    .map(|(name, value)| ((*name).to_owned(), (*value).to_owned()))
                    .collect(),
                body,
            });
    }

    fn token(&self) {
        self.enqueue(AUTH_URL, fixture(TOKEN_FIXTURE));
    }

    fn calls(&self) -> Vec<FakeCall> {
        self.calls.lock().unwrap().clone()
    }

    fn urls(&self) -> Vec<String> {
        self.calls().into_iter().map(|call| call.url).collect()
    }
}

#[async_trait]
impl DockerHttp for FakeHttp {
    async fn get(&self, url: &str, headers: &[(String, String)]) -> Result<DockerResponse> {
        self.calls.lock().unwrap().push(FakeCall {
            url: url.to_owned(),
            headers: headers.to_vec(),
        });
        let reply = self
            .replies
            .lock()
            .unwrap()
            .get_mut(url)
            .and_then(VecDeque::pop_front)
            .unwrap_or_else(|| panic!("unexpected HTTP request: {url}"));
        Ok(DockerResponse {
            status: reply.status,
            headers: reply.headers,
            body: reply.body,
        })
    }
}

// ---------------------------------------------------------------------------
// Builders
// ---------------------------------------------------------------------------

fn nginx() -> ProjectRef {
    ProjectRef {
        purl_type: "docker".to_owned(),
        name: "library/nginx".to_owned(),
        purl: "pkg:docker/library/nginx".to_owned(),
    }
}

fn version(raw: &str, normalized: &str) -> VersionInfo {
    VersionInfo {
        raw: raw.to_owned(),
        version: normalized.to_owned(),
        deprecated: false,
        reason: String::new(),
    }
}

fn header_value<'a>(call: &'a FakeCall, name: &str) -> Option<&'a str> {
    call.headers
        .iter()
        .find(|(key, _)| key.eq_ignore_ascii_case(name))
        .map(|(_, value)| value.as_str())
}

fn enqueue_tag_pages(http: &FakeHttp) {
    let link = format!("<{TAGS_PAGE2_PATH}>; rel=\"next\"");
    http.enqueue_response(
        TAGS_URL,
        200,
        &[("Link", link.as_str())],
        fixture(TAGS_PAGE1),
    );
    http.enqueue(TAGS_PAGE2_URL, fixture(TAGS_PAGE2));
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn versions_paginate_and_channel_tags_stay_out_of_versions() {
    let http = FakeHttp::new();
    http.token();
    enqueue_tag_pages(&http);
    let source = DockerSource::new(http.clone());

    let versions = source.versions(&nginx()).await.expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["1.24.0", "1.25.1", "1.25.2", "1.25.3"]);
    assert!(versions.iter().all(|info| info.raw == info.version));
    assert_eq!(http.urls(), [AUTH_URL, TAGS_URL, TAGS_PAGE2_URL]);

    // The same registry input always yields the same order.
    http.token();
    enqueue_tag_pages(&http);
    let again = source.versions(&nginx()).await.expect("versions again");
    assert_eq!(again, versions);

    // Channel tags are reported by project_meta, not versions.
    http.token();
    enqueue_tag_pages(&http);
    let meta = source.project_meta(&nginx()).await.expect("project meta");
    assert_eq!(meta["registry"], json!("registry-1.docker.io"));
    assert_eq!(meta["repository"], json!("library/nginx"));
    assert_eq!(meta["authRequired"], json!(true));
    assert_eq!(
        meta["tags"],
        json!([
            "1.24.0", "1.25.1", "1.25.2", "1.25.3", "edge", "latest", "nightly", "stable"
        ])
    );
    assert_eq!(
        meta["channelTags"],
        json!(["edge", "latest", "nightly", "stable"])
    );
    for channel in ["latest", "stable", "edge", "nightly"] {
        assert!(!names.contains(&channel));
    }
}

#[tokio::test]
async fn artifacts_use_content_digest_for_immutable_url_and_size() {
    let http = FakeHttp::new();
    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[
            ("Docker-Content-Digest", DIGEST_A),
            ("Content-Length", "3210"),
        ],
        fixture(MANIFEST_INDEX),
    );
    let source = DockerSource::new(http.clone());

    let artifacts = source
        .artifacts(&nginx(), &version("1.25.3", "1.25.3"))
        .await
        .expect("artifacts");
    assert_eq!(artifacts.len(), 1);
    let artifact = &artifacts[0];
    assert_eq!(artifact.file_name, "nginx-1.25.3.oci.json");
    assert_eq!(
        artifact.url,
        format!("https://registry-1.docker.io/v2/library/nginx/manifests/{DIGEST_A}")
    );
    assert_eq!(artifact.sha256.as_deref(), Some(HEX_A));
    assert_eq!(artifact.size, Some(3210));
    assert_eq!(
        artifact.mime_type.as_deref(),
        Some("application/vnd.oci.image.index.v1+json")
    );

    let calls = http.calls();
    let accept = header_value(&calls[1], "Accept").expect("accept header");
    assert_eq!(accept, docker::OCI_INDEX_ACCEPT);
    assert_eq!(
        header_value(&calls[1], "Authorization"),
        Some("Bearer anonymous-token")
    );

    // Without Content-Length the index entry sizes are summed.
    let http = FakeHttp::new();
    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[("Docker-Content-Digest", DIGEST_A)],
        fixture(MANIFEST_INDEX),
    );
    let source = DockerSource::new(http);
    let artifacts = source
        .artifacts(&nginx(), &version("1.25.3", "1.25.3"))
        .await
        .expect("artifacts without content length");
    assert_eq!(artifacts[0].size, Some(3000));
}

#[tokio::test]
async fn mutable_tag_warning_appears_only_after_the_digest_changes() {
    let http = FakeHttp::new();
    let source = DockerSource::new(http.clone());
    let tag = version("1.25.3", "1.25.3");

    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[("Docker-Content-Digest", DIGEST_A)],
        fixture(MANIFEST_INDEX),
    );
    assert!(
        source
            .statuses(&nginx(), &tag)
            .await
            .expect("first call")
            .is_empty()
    );

    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[("Docker-Content-Digest", DIGEST_A)],
        fixture(MANIFEST_INDEX),
    );
    assert!(
        source
            .statuses(&nginx(), &tag)
            .await
            .expect("same digest")
            .is_empty()
    );

    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[("Docker-Content-Digest", DIGEST_B)],
        fixture(MANIFEST_INDEX),
    );
    let statuses = source.statuses(&nginx(), &tag).await.expect("moved tag");
    assert_eq!(statuses.len(), 1);
    assert_eq!(statuses[0].status, "deprecated");
    assert_eq!(statuses[0].kind.as_deref(), Some("mutable_tag"));
    assert!(statuses[0].reason.contains(DIGEST_A));
    assert!(statuses[0].reason.contains(DIGEST_B));

    http.token();
    http.enqueue_response(
        MANIFEST_URL,
        200,
        &[("Docker-Content-Digest", DIGEST_B)],
        fixture(MANIFEST_INDEX),
    );
    assert!(
        source
            .statuses(&nginx(), &tag)
            .await
            .expect("stable again")
            .is_empty()
    );
}

#[tokio::test]
async fn anonymous_token_flow_and_401_are_typed() {
    let http = FakeHttp::new();
    http.token();
    http.enqueue(TAGS_URL, fixture(TAGS_PAGE1));
    let source = DockerSource::new(http.clone());

    let versions = source.versions(&nginx()).await.expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["1.24.0", "1.25.2", "1.25.3"]);

    let calls = http.calls();
    assert_eq!(calls[0].url, AUTH_URL);
    assert_eq!(header_value(&calls[0], "Accept"), Some("application/json"));
    assert_eq!(header_value(&calls[0], "Authorization"), None);
    assert_eq!(
        header_value(&calls[1], "Authorization"),
        Some("Bearer anonymous-token")
    );

    // Registry 401 becomes a typed unsupported reason.
    http.token();
    http.enqueue_response(
        TAGS_URL,
        401,
        &[],
        json!({"errors": [{"code": "UNAUTHORIZED"}]}),
    );
    let error = source.versions(&nginx()).await.expect_err("registry 401");
    assert!(
        matches!(
            unsupported_reason(&error),
            Some(UnsupportedReason::Unavailable(_))
        ),
        "{error:?}"
    );

    // Token endpoint 401 is typed too.
    http.enqueue_response(AUTH_URL, 401, &[], json!({"details": "unauthorized"}));
    let error = source.versions(&nginx()).await.expect_err("token 401");
    assert!(
        matches!(
            unsupported_reason(&error),
            Some(UnsupportedReason::Unavailable(_))
        ),
        "{error:?}"
    );
}

#[tokio::test]
async fn oci_purls_resolve_docker_hub_and_reject_foreign_registries() {
    let http = FakeHttp::new();
    http.token();
    http.enqueue(TAGS_URL, fixture(TAGS_PAGE1));
    let source = DockerSource::new(http.clone());
    let oci = ProjectRef {
        purl_type: "oci".to_owned(),
        name: "nginx".to_owned(),
        purl: "pkg:oci/nginx@sha256%3Adeadbeef?repository_url=docker.io%2Flibrary%2Fnginx"
            .to_owned(),
    };

    let versions = source.versions(&oci).await.expect("oci versions");
    assert_eq!(versions.len(), 3);
    assert_eq!(http.urls(), [AUTH_URL, TAGS_URL]);

    let foreign_http = FakeHttp::new();
    let foreign = DockerSource::new(foreign_http.clone());
    let foreign_project = ProjectRef {
        purl_type: "oci".to_owned(),
        name: "nginx".to_owned(),
        purl: "pkg:oci/nginx?repository_url=quay.io%2Flibrary%2Fnginx".to_owned(),
    };
    let error = foreign
        .versions(&foreign_project)
        .await
        .expect_err("foreign registry");
    assert!(
        matches!(
            unsupported_reason(&error),
            Some(UnsupportedReason::Unavailable(_))
        ),
        "{error:?}"
    );
    assert!(foreign_http.calls().is_empty());

    let unknown = ProjectRef {
        purl_type: "npm".to_owned(),
        name: "left-pad".to_owned(),
        purl: "pkg:npm/left-pad".to_owned(),
    };
    let error = foreign.versions(&unknown).await.expect_err("unknown type");
    assert_eq!(
        unsupported_reason(&error),
        Some(UnsupportedReason::UnknownPurlType("npm".to_owned()))
    );
    assert!(foreign_http.calls().is_empty());
}

#[test]
fn capabilities_and_production_transport() {
    let source = DockerSource::new(FakeHttp::new());
    assert_eq!(source.name(), "docker");
    assert_eq!(
        source.capabilities(),
        SourceCapabilities {
            versions: true,
            artifacts: true,
            statuses: true,
            project_meta: true,
        }
    );

    let transport = docker::ReqwestDockerHttp::new().expect("build transport");
    let _: Arc<dyn DockerHttp> = Arc::new(transport);
}