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

johnandersen777.bsky.social/vuln-notif

0
0
main
630 lines · 19.8 KB Raw History
//! GitHub, GitLab, and Bitbucket release source tests.
//!
//! `src/release/sources/{github,gitlab,bitbucket}.rs` are compiled into
//! `release::sources` by the integration wave; until that wiring lands this
//! test includes the modules directly and provides the `crate::` paths they
//! use.

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/bitbucket.rs"]
mod bitbucket;
#[path = "../src/release/sources/github.rs"]
mod github;
#[path = "../src/release/sources/gitlab.rs"]
mod gitlab;

use bitbucket::BitbucketSource;
use github::{GitHttp, GitResponse, GithubSource};
use gitlab::GitlabSource;
use release::source::{ProjectRef, ReleaseSource, SourceCapabilities, VersionInfo};

const GITHUB_RELEASES: &str = include_str!("fixtures/sources/git/github-releases.json");
const GITHUB_RELEASES_PAGE2: &str = include_str!("fixtures/sources/git/github-releases-page2.json");
const GITHUB_TAGS: &str = include_str!("fixtures/sources/git/github-tags.json");
const GITHUB_RELEASE_V2: &str = include_str!("fixtures/sources/git/github-release-v2.json");
const GITHUB_REPO: &str = include_str!("fixtures/sources/git/github-repo.json");
const GITHUB_REPO_ARCHIVED: &str = include_str!("fixtures/sources/git/github-repo-archived.json");
const GITLAB_RELEASES: &str = include_str!("fixtures/sources/git/gitlab-releases.json");
const GITLAB_TAGS: &str = include_str!("fixtures/sources/git/gitlab-tags.json");
const GITLAB_PROJECT_ARCHIVED: &str =
    include_str!("fixtures/sources/git/gitlab-project-archived.json");
const BITBUCKET_TAGS: &str = include_str!("fixtures/sources/git/bitbucket-tags.json");
const BITBUCKET_DOWNLOADS_1: &str =
    include_str!("fixtures/sources/git/bitbucket-downloads-page1.json");
const BITBUCKET_DOWNLOADS_2: &str =
    include_str!("fixtures/sources/git/bitbucket-downloads-page2.json");
const BITBUCKET_REPO: &str = include_str!("fixtures/sources/git/bitbucket-repo.json");

const GITHUB_REPO_URL: &str = "https://api.github.com/repos/acme/widgets";
const BITBUCKET_REPO_URL: &str = "https://api.bitbucket.org/2.0/repositories/acme/widgets";

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

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

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

/// Scripted git-host transport: replies are queued per URL, and every request
/// is recorded so tests can assert pagination and auth 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::<BTreeMap<_, _>>(),
                body,
            });
    }

    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 GitHttp for FakeHttp {
    async fn get_json(&self, url: &str, headers: &[(String, String)]) -> Result<GitResponse> {
        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(GitResponse {
            status: reply.status,
            headers: reply.headers,
            body: reply.body,
        })
    }
}

fn github_project() -> ProjectRef {
    ProjectRef {
        purl_type: "github".to_owned(),
        name: "acme/widgets".to_owned(),
        purl: "pkg:github/acme/widgets".to_owned(),
    }
}

fn gitlab_project() -> ProjectRef {
    ProjectRef {
        purl_type: "gitlab".to_owned(),
        name: "acme/widgets".to_owned(),
        purl: "pkg:gitlab/acme/widgets".to_owned(),
    }
}

fn bitbucket_project() -> ProjectRef {
    ProjectRef {
        purl_type: "bitbucket".to_owned(),
        name: "acme/widgets".to_owned(),
        purl: "pkg:bitbucket/acme/widgets".to_owned(),
    }
}

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

fn flagged(raw: &str, normalized: &str, reason: &str) -> VersionInfo {
    VersionInfo {
        raw: raw.to_owned(),
        version: normalized.to_owned(),
        deprecated: true,
        reason: reason.to_owned(),
    }
}

fn github_url(suffix: &str) -> String {
    format!("{GITHUB_REPO_URL}{suffix}")
}

fn gitlab_url(encoded_path: &str, suffix: &str) -> String {
    format!("https://gitlab.com/api/v4/projects/{encoded_path}{suffix}")
}

fn bitbucket_url(suffix: &str) -> String {
    format!("{BITBUCKET_REPO_URL}{suffix}")
}

#[tokio::test]
async fn github_versions_merge_releases_and_tags_newest_first() {
    let http = FakeHttp::new();
    http.enqueue(
        &github_url("/releases?per_page=100"),
        fixture(GITHUB_RELEASES),
    );
    http.enqueue(&github_url("/tags?per_page=100"), fixture(GITHUB_TAGS));
    let source = GithubSource::new(http.clone(), None);

    let versions = source.versions(&github_project()).await.expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["3.0.0", "2.1.0-rc.1", "2.0.0", "1.8.0"]);
    assert!(versions[0].deprecated);
    assert_eq!(versions[0].reason, "draft release");
    assert!(versions[1].deprecated);
    assert_eq!(versions[1].reason, "prerelease release");
    assert!(!versions[2].deprecated);
    assert_eq!(versions[2].raw, "v2.0.0");
}

#[tokio::test]
async fn github_versions_follow_link_header_pagination() {
    let page2 = github_url("/releases?per_page=100&page=2");
    let link = format!("<{page2}>; rel=\"next\", <{page2}>; rel=\"last\"");
    let http = FakeHttp::new();
    http.enqueue_response(
        &github_url("/releases?per_page=100"),
        200,
        &[("Link", link.as_str())],
        fixture(GITHUB_RELEASES),
    );
    http.enqueue(&page2, fixture(GITHUB_RELEASES_PAGE2));
    http.enqueue(&github_url("/tags?per_page=100"), fixture(GITHUB_TAGS));
    let source = GithubSource::new(http.clone(), None);

    let versions = source.versions(&github_project()).await.expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["3.0.0", "2.1.0-rc.1", "2.0.0", "1.9.0", "1.8.0"]);
    assert_eq!(
        http.urls(),
        [
            github_url("/releases?per_page=100"),
            page2,
            github_url("/tags?per_page=100"),
        ]
    );
}

#[tokio::test]
async fn github_artifacts_use_declared_digest_then_url_and_size() {
    let http = FakeHttp::new();
    http.enqueue(
        &github_url("/releases/tags/v2.0.0"),
        fixture(GITHUB_RELEASE_V2),
    );
    let source = GithubSource::new(http.clone(), None);

    let artifacts = source
        .artifacts(&github_project(), &version("v2.0.0", "2.0.0"))
        .await
        .expect("artifacts");
    assert_eq!(artifacts.len(), 4);

    assert_eq!(
        artifacts[0].sha256.as_deref(),
        Some("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08")
    );
    assert_eq!(artifacts[0].size, Some(1234));
    assert_eq!(artifacts[1].sha256, None);
    assert_eq!(artifacts[1].size, Some(2345));

    assert_eq!(artifacts[2].file_name, "widgets-v2.0.0.tar.gz");
    assert_eq!(artifacts[2].sha256, None);
    assert_eq!(artifacts[2].size, None);
    assert_eq!(artifacts[2].mime_type.as_deref(), Some("application/gzip"));
    assert!(artifacts[3].url.ends_with("/zipball/v2.0.0"));
}

#[tokio::test]
async fn github_artifacts_fall_back_to_source_archives() {
    let http = FakeHttp::new();
    http.enqueue_response(
        &github_url("/releases/tags/v1.9.0"),
        404,
        &[],
        json!({"message": "Not Found"}),
    );
    let source = GithubSource::new(http.clone(), None);

    let artifacts = source
        .artifacts(&github_project(), &version("v1.9.0", "1.9.0"))
        .await
        .expect("archives");
    assert_eq!(artifacts.len(), 2);
    assert!(artifacts[0].url.ends_with("/tarball/v1.9.0"));
    assert!(artifacts[1].url.ends_with("/zipball/v1.9.0"));
    assert!(
        artifacts
            .iter()
            .all(|artifact| artifact.sha256.is_none() && artifact.size.is_none())
    );
}

#[tokio::test]
async fn github_rate_limit_403_propagates_without_exposing_token() {
    let token = "ghp_super_secret_token";
    let http = FakeHttp::new();
    http.enqueue_response(
        &github_url("/releases?per_page=100"),
        403,
        &[
            ("x-ratelimit-remaining", "0"),
            ("x-ratelimit-reset", "1700000000"),
        ],
        json!({"message": "API rate limit exceeded"}),
    );
    let source = GithubSource::new(http.clone(), Some(token.to_owned()));

    let error = source
        .versions(&github_project())
        .await
        .expect_err("rate limit");
    let message = format!("{error:?}");
    assert!(message.contains("rate limit"), "{message}");
    assert!(message.contains("reset=1700000000"), "{message}");
    assert!(!message.contains(token), "{message}");

    let calls = http.calls();
    assert_eq!(calls.len(), 1);
    let authorization = calls[0]
        .headers
        .iter()
        .find(|(name, _)| name == "Authorization")
        .map(|(_, value)| value.as_str());
    assert_eq!(authorization, Some("Bearer ghp_super_secret_token"));
}

#[tokio::test]
async fn github_statuses_mark_draft_prerelease_and_archived() {
    let http = FakeHttp::new();
    http.enqueue(GITHUB_REPO_URL, fixture(GITHUB_REPO_ARCHIVED));
    let source = GithubSource::new(http.clone(), None);

    let statuses = source
        .statuses(
            &github_project(),
            &flagged("v2.1.0-rc.1", "2.1.0-rc.1", "prerelease release"),
        )
        .await
        .expect("statuses");
    assert_eq!(statuses.len(), 2);
    assert_eq!(statuses[0].status, "deprecated");
    assert_eq!(statuses[0].kind.as_deref(), Some("prerelease"));
    assert_eq!(statuses[1].status, "archived");
    assert_eq!(statuses[1].kind.as_deref(), Some("archived"));

    let statuses = source
        .statuses(
            &github_project(),
            &flagged("v3.0.0", "3.0.0", "draft release"),
        )
        .await
        .expect("statuses");
    assert_eq!(statuses[0].kind.as_deref(), Some("draft"));
    assert_eq!(http.urls(), [GITHUB_REPO_URL.to_owned()]);
}

#[tokio::test]
async fn github_project_meta_reports_repository_homepage_license_description() {
    let http = FakeHttp::new();
    http.enqueue(GITHUB_REPO_URL, fixture(GITHUB_REPO));
    let source = GithubSource::new(http.clone(), None);

    let meta = source
        .project_meta(&github_project())
        .await
        .expect("project meta");
    assert_eq!(
        meta,
        json!({
            "repository": "https://github.com/acme/widgets",
            "homepage": "https://widgets.example",
            "license": "MIT",
            "description": "Widget build tooling",
        })
    );
}

#[tokio::test]
async fn gitlab_versions_merge_releases_and_tags() {
    let http = FakeHttp::new();
    http.enqueue(
        &gitlab_url("acme%2Fwidgets", "/releases?per_page=100"),
        fixture(GITLAB_RELEASES),
    );
    http.enqueue(
        &gitlab_url("acme%2Fwidgets", "/repository/tags?per_page=100"),
        fixture(GITLAB_TAGS),
    );
    let source = GitlabSource::new(http.clone(), Some("glpat_secret".to_owned()));

    let versions = source.versions(&gitlab_project()).await.expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["3.0.0", "2.0.0", "1.0.0"]);
    assert!(versions[0].deprecated);
    assert_eq!(versions[0].reason, "upcoming release");
    assert!(!versions[1].deprecated);

    let calls = http.calls();
    assert!(
        calls[0]
            .headers
            .iter()
            .any(|(name, value)| name == "PRIVATE-TOKEN" && value == "glpat_secret")
    );
}

#[tokio::test]
async fn gitlab_subgroup_purl_is_url_encoded_and_assets_have_no_digest() {
    let subgroup = ProjectRef {
        purl_type: "gitlab".to_owned(),
        name: "acme/platform/widgets".to_owned(),
        purl: "pkg:gitlab/acme/platform/widgets".to_owned(),
    };
    let encoded = "acme%2Fplatform%2Fwidgets";
    let http = FakeHttp::new();
    http.enqueue(
        &gitlab_url(encoded, "/releases?per_page=100"),
        fixture(GITLAB_RELEASES),
    );
    http.enqueue(
        &gitlab_url(encoded, "/repository/tags?per_page=100"),
        fixture(GITLAB_TAGS),
    );
    let source = GitlabSource::new(http.clone(), None);

    let versions = source.versions(&subgroup).await.expect("versions");
    assert_eq!(
        http.urls()[0],
        gitlab_url(encoded, "/releases?per_page=100")
    );

    let releases = fixture(GITLAB_RELEASES);
    http.enqueue(
        &gitlab_url(encoded, "/releases/v2.0.0"),
        releases[1].clone(),
    );
    let artifacts = source
        .artifacts(&subgroup, &versions[1])
        .await
        .expect("artifacts");
    assert_eq!(artifacts.len(), 3);
    assert_eq!(artifacts[0].file_name, "widgets-v2.0.0.zip");
    assert_eq!(artifacts[0].mime_type.as_deref(), Some("application/zip"));
    assert!(
        artifacts
            .iter()
            .all(|artifact| artifact.sha256.is_none() && artifact.size.is_none())
    );
    assert_eq!(artifacts[2].file_name, "widgets-2.0.0.jar");
}

#[tokio::test]
async fn gitlab_project_meta_and_archived_status_share_one_request() {
    let http = FakeHttp::new();
    http.enqueue(
        &gitlab_url("acme%2Fwidgets", ""),
        fixture(GITLAB_PROJECT_ARCHIVED),
    );
    let source = GitlabSource::new(http.clone(), None);

    let meta = source
        .project_meta(&gitlab_project())
        .await
        .expect("project meta");
    assert_eq!(
        meta,
        json!({
            "repository": "https://gitlab.com/acme/widgets",
            "license": "MIT License",
            "description": "Widget build tooling",
        })
    );

    let statuses = source
        .statuses(
            &gitlab_project(),
            &flagged("v3.0.0", "3.0.0", "upcoming release"),
        )
        .await
        .expect("statuses");
    assert_eq!(statuses[0].status, "deprecated");
    assert_eq!(statuses[0].kind.as_deref(), Some("draft"));
    assert_eq!(statuses[1].status, "archived");
    assert_eq!(statuses[1].kind.as_deref(), Some("archived"));
    assert_eq!(http.urls().len(), 1);
}

#[tokio::test]
async fn bitbucket_versions_come_from_tags_newest_first() {
    let http = FakeHttp::new();
    http.enqueue(
        &bitbucket_url("/refs/tags?pagelen=100"),
        fixture(BITBUCKET_TAGS),
    );
    let source = BitbucketSource::new(http.clone());

    let versions = source
        .versions(&bitbucket_project())
        .await
        .expect("versions");
    let names: Vec<&str> = versions.iter().map(|info| info.version.as_str()).collect();
    assert_eq!(names, ["2.0.0", "1.0.0"]);
    assert_eq!(versions[0].raw, "v2.0.0");
}

#[tokio::test]
async fn bitbucket_downloads_paginate_and_carry_url_size_without_digest() {
    let page2 =
        "https://api.bitbucket.org/2.0/repositories/acme/widgets/downloads?pagelen=100&page=2";
    let http = FakeHttp::new();
    http.enqueue(
        &bitbucket_url("/downloads?pagelen=100"),
        fixture(BITBUCKET_DOWNLOADS_1),
    );
    http.enqueue(page2, fixture(BITBUCKET_DOWNLOADS_2));
    let source = BitbucketSource::new(http.clone());

    let artifacts = source
        .artifacts(&bitbucket_project(), &version("v2.0.0", "2.0.0"))
        .await
        .expect("artifacts");
    assert_eq!(artifacts.len(), 2);
    assert_eq!(artifacts[0].file_name, "widgets-2.0.0.tar.gz");
    assert_eq!(artifacts[0].size, Some(1234));
    assert_eq!(artifacts[0].sha256, None);
    assert_eq!(artifacts[1].file_name, "widgets-2.0.0.zip");
    assert_eq!(artifacts[1].size, Some(2345));
    assert_eq!(
        http.urls(),
        [bitbucket_url("/downloads?pagelen=100"), page2.to_owned(),]
    );
}

#[tokio::test]
async fn bitbucket_capabilities_are_exact_and_statuses_are_typed_unsupported() {
    let http = FakeHttp::new();
    let source = BitbucketSource::new(http);

    assert_eq!(
        source.capabilities(),
        SourceCapabilities {
            versions: true,
            artifacts: true,
            statuses: false,
            project_meta: true,
        }
    );
    let error = source
        .statuses(&bitbucket_project(), &version("v1.0.0", "1.0.0"))
        .await
        .expect_err("no statuses");
    assert!(format!("{error:?}").contains("NoStatuses"), "{error:?}");
}

#[tokio::test]
async fn bitbucket_project_meta_reports_repository_homepage_description() {
    let http = FakeHttp::new();
    http.enqueue(BITBUCKET_REPO_URL, fixture(BITBUCKET_REPO));
    let source = BitbucketSource::new(http.clone());

    let meta = source
        .project_meta(&bitbucket_project())
        .await
        .expect("project meta");
    assert_eq!(
        meta,
        json!({
            "repository": "https://bitbucket.org/acme/widgets",
            "homepage": "https://widgets.example",
            "description": "Widget build tooling",
        })
    );
}

#[tokio::test]
async fn missing_purl_components_are_reported_as_unavailable() {
    let http = FakeHttp::new();
    let bad = |purl_type: &str, purl: &str| ProjectRef {
        purl_type: purl_type.to_owned(),
        name: String::new(),
        purl: purl.to_owned(),
    };

    let github = GithubSource::new(http.clone(), None);
    let error = github
        .versions(&bad("github", "pkg:github"))
        .await
        .expect_err("missing path");
    assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");

    let gitlab = GitlabSource::new(http.clone(), None);
    let error = gitlab
        .versions(&bad("gitlab", "pkg:gitlab"))
        .await
        .expect_err("missing path");
    assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");

    let bitbucket = BitbucketSource::new(http.clone());
    let error = bitbucket
        .versions(&bad("bitbucket", "pkg:bitbucket/onlyworkspace"))
        .await
        .expect_err("missing repository");
    assert!(format!("{error:?}").contains("Unavailable"), "{error:?}");

    let mismatch = ProjectRef {
        purl_type: "github".to_owned(),
        name: "acme/widgets".to_owned(),
        purl: "pkg:npm/acme/widgets".to_owned(),
    };
    let error = github
        .versions(&mismatch)
        .await
        .expect_err("wrong purl type");
    assert!(
        format!("{error:?}").contains("UnknownPurlType"),
        "{error:?}"
    );
    assert!(http.calls().is_empty());
}

#[test]
fn production_transport_constructs() {
    let transport = github::ReqwestGitHttp::new().expect("build transport");
    let _: Arc<dyn GitHttp> = Arc::new(transport);
}