use std::path::Path;
use mise_sigstore::sources::github::GitHubSource;
use mise_sigstore::{ArtifactRef, AttestationClient, AttestationSource, FetchParams, RetryConfig};
pub use mise_sigstore::{
Attestation, AttestationError, CosignIdentity, SlsaArtifact, SlsaSignerIdentity,
};
type AttestationResult<T> = std::result::Result<T, AttestationError>;
type VerificationCell = std::sync::Arc<tokio::sync::OnceCell<bool>>;
static INVOCATION_VERIFICATIONS: std::sync::LazyLock<
std::sync::Mutex<std::collections::HashMap<String, VerificationCell>>,
> = std::sync::LazyLock::new(Default::default);
async fn shared_verification(
key: String,
verification: impl std::future::Future<Output = AttestationResult<bool>>,
) -> AttestationResult<bool> {
let settings = mise_settings::Settings::get();
if !settings.generate_lockfiles() || settings.force_provenance_verify() {
return verification.await;
}
let cell = INVOCATION_VERIFICATIONS
.lock()
.unwrap()
.entry(key)
.or_default()
.clone();
cell.get_or_try_init(|| verification).await.copied()
}
#[derive(Debug)]
enum CachedAttestationVerification {
Verified(Vec<String>),
Retry(Option<AttestationError>),
}
fn classify_cached_attestation_verification(
result: AttestationResult<Vec<String>>,
) -> CachedAttestationVerification {
match result {
Ok(sources) if !sources.is_empty() => CachedAttestationVerification::Verified(sources),
Ok(_) => CachedAttestationVerification::Retry(None),
Err(err) => CachedAttestationVerification::Retry(Some(err)),
}
}
fn resolve_token_for_wrapper(api_url: Option<&str>) -> Option<String> {
let url = api_url.unwrap_or(crate::github::API_URL);
crate::github::resolve_token_for_api_url(url)
}
fn routed_api_url(
api_url: &str,
token: Option<String>,
) -> AttestationResult<(String, Option<String>)> {
let Ok(mut url) = url::Url::parse(api_url) else {
debug!("invalid GitHub attestation API URL, skipping url_replacements: {api_url}");
return Ok((api_url.to_string(), token));
};
let original = url.clone();
crate::http::apply_url_replacements(&mut url);
let token = token.filter(|_| url.host() == original.host());
crate::http::clear_cross_host_credentials(
&mut reqwest::header::HeaderMap::new(),
&original,
&mut url,
);
crate::http::ensure_secure_url_replacement(&original, &url, token.is_some())
.map_err(|err| AttestationError::Verification(err.to_string()))?;
if url == original {
Ok((api_url.to_string(), token))
} else {
Ok((url.to_string(), token))
}
}
fn routed_tuf_url() -> Option<String> {
let Ok(mut url) = url::Url::parse(mise_sigstore::DEFAULT_TUF_URL) else {
debug!(
"invalid Sigstore TUF URL, skipping url_replacements: {}",
mise_sigstore::DEFAULT_TUF_URL
);
return None;
};
let original = url.clone();
crate::http::apply_url_replacements(&mut url);
if let Err(err) = crate::http::ensure_secure_url_replacement(&original, &url, false) {
warn!("{err}; ignoring insecure Sigstore TUF URL replacement");
return None;
}
(url != original).then(|| url.to_string())
}
fn mise_retry_config() -> RetryConfig {
let settings = mise_settings::Settings::get();
RetryConfig {
timeout: crate::network::http_timeout(&settings),
retries: crate::network::http_retries(&settings).max(0) as usize,
..RetryConfig::default()
}
}
fn attestation_client(api_url: &str) -> AttestationResult<AttestationClient> {
let (base_url, token) = routed_api_url(api_url, resolve_token_for_wrapper(Some(api_url)))?;
let mut builder = AttestationClient::builder()
.base_url(&base_url)
.retry_config(mise_retry_config());
if let Some(token) = token.as_deref() {
builder = builder.github_token(token);
}
builder.build()
}
pub async fn verify_attestation(
artifact_path: &Path,
owner: &str,
repo: &str,
expected_workflow: Option<&str>,
api_url: Option<&str>,
use_versions_host: bool,
) -> AttestationResult<bool> {
if !mise_settings::Settings::get().generate_lockfiles() {
return verify_attestation_uncached(
artifact_path,
owner,
repo,
expected_workflow,
api_url,
use_versions_host,
)
.await;
}
let digest = mise_sigstore::calculate_file_digest(artifact_path).await?;
let key = format!(
"github:{owner}/{repo}:{digest}:{expected_workflow:?}:{api_url:?}:{use_versions_host}"
);
shared_verification(
key,
verify_attestation_uncached(
artifact_path,
owner,
repo,
expected_workflow,
api_url,
use_versions_host,
),
)
.await
}
async fn verify_attestation_uncached(
artifact_path: &Path,
owner: &str,
repo: &str,
expected_workflow: Option<&str>,
api_url: Option<&str>,
use_versions_host: bool,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
let mut digest = None;
let mut mirror_err = None;
if use_versions_host_for_attestations(api_url, use_versions_host) {
let artifact_digest = mise_sigstore::calculate_file_digest(artifact_path).await?;
match verify_with_mirror(
artifact_path,
owner,
repo,
expected_workflow,
None,
&artifact_digest,
)
.await
{
MirrorVerdict::Done(result) => return result,
MirrorVerdict::AskGithub => {}
MirrorVerdict::RetryGithub(err) => mirror_err = Some(err),
}
digest = Some(artifact_digest);
}
let (base_url, token) = routed_api_url(
api_url.unwrap_or(crate::github::API_URL),
resolve_token_for_wrapper(api_url),
)?;
let result = if let Some(digest) = digest {
mise_sigstore::verify_github_attestation_with_base_url_and_digest(
mise_sigstore::GithubAttestationRequest {
artifact_path,
owner,
repo,
token: token.as_deref(),
signer_workflow: expected_workflow,
base_url: Some(&base_url),
digest: Some(&digest),
retry_config: mise_retry_config(),
},
)
.await
} else {
mise_sigstore::verify_github_attestation_with_base_url(
artifact_path,
owner,
repo,
token.as_deref(),
expected_workflow,
&base_url,
mise_retry_config(),
)
.await
};
accept_renamed_repository(owner, repo, api_url, result)
.await
.map_err(|err| after_mirror_failure(err, owner, repo, mirror_err.as_ref()))
}
async fn accept_renamed_repository(
owner: &str,
repo: &str,
api_url: Option<&str>,
result: AttestationResult<bool>,
) -> AttestationResult<bool> {
let on_github = api_url.is_none_or(|url| url.trim_end_matches('/') == crate::github::API_URL);
match result {
Err(AttestationError::RepositoryMismatch { found, .. })
if on_github && crate::github::attested_by_repository(owner, repo, &found).await =>
{
Ok(true)
}
result => result,
}
}
pub async fn verify_attestation_with_predicate_type(
artifact_path: &Path,
owner: &str,
repo: &str,
expected_workflow: Option<&str>,
predicate_type: Option<&str>,
api_url: Option<&str>,
use_versions_host: bool,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
let Some(predicate_type) = predicate_type else {
return verify_attestation(
artifact_path,
owner,
repo,
expected_workflow,
api_url,
use_versions_host,
)
.await;
};
let artifact_digest = mise_sigstore::calculate_file_digest(artifact_path).await?;
let mut mirror_err = None;
if use_versions_host_for_attestations(api_url, use_versions_host) {
match verify_with_mirror(
artifact_path,
owner,
repo,
expected_workflow,
Some(predicate_type),
&artifact_digest,
)
.await
{
MirrorVerdict::Done(result) => return result,
MirrorVerdict::AskGithub => {}
MirrorVerdict::RetryGithub(err) => mirror_err = Some(err),
}
}
let client = attestation_client(api_url.unwrap_or(crate::github::API_URL))?;
let attestations = client
.fetch_attestations(FetchParams {
owner: owner.to_string(),
repo: Some(format!("{owner}/{repo}")),
digest: format!("sha256:{artifact_digest}"),
limit: 30,
predicate_type: Some(predicate_type.to_string()),
})
.await
.map_err(|err| after_mirror_failure(err, owner, repo, mirror_err.as_ref()))?;
let result = mise_sigstore::verify_github_attestation_with_attestations(
artifact_path,
&attestations,
owner,
repo,
expected_workflow,
)
.await;
accept_renamed_repository(owner, repo, api_url, result).await
}
#[derive(Debug)]
pub enum DetectError {
SourceCreation(AttestationError),
Fetch(AttestationError),
}
impl std::fmt::Display for DetectError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DetectError::SourceCreation(e) => write!(f, "{e}"),
DetectError::Fetch(e) => write!(f, "{e}"),
}
}
}
impl std::error::Error for DetectError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
DetectError::SourceCreation(e) => Some(e),
DetectError::Fetch(e) => Some(e),
}
}
}
pub async fn detect_attestations(
owner: &str,
repo: &str,
api_url: &str,
digest: &str,
use_versions_host: bool,
) -> Result<bool, DetectError> {
let mut mirror_err = None;
if use_versions_host_for_attestations(Some(api_url), use_versions_host) {
match detect_with_mirror(owner, repo, digest, None).await {
MirrorVerdict::Done(found) => return found,
MirrorVerdict::AskGithub => {}
MirrorVerdict::RetryGithub(err) => mirror_err = Some(err),
}
}
let (base_url, token) = routed_api_url(api_url, resolve_token_for_wrapper(Some(api_url)))
.map_err(DetectError::SourceCreation)?;
let source = GitHubSource::with_base_url(owner, repo, token.as_deref(), &base_url)
.map_err(DetectError::SourceCreation)?;
let artifact_ref = ArtifactRef::from_digest(digest);
let attestations = source
.fetch_attestations(&artifact_ref)
.await
.map_err(|err| {
DetectError::Fetch(after_mirror_failure(err, owner, repo, mirror_err.as_ref()))
})?;
Ok(!attestations.is_empty())
}
pub async fn detect_attestations_with_predicate_type(
owner: &str,
repo: &str,
api_url: &str,
digest: &str,
predicate_type: Option<&str>,
use_versions_host: bool,
) -> Result<bool, DetectError> {
let Some(predicate_type) = predicate_type else {
return detect_attestations(owner, repo, api_url, digest, use_versions_host).await;
};
let mut mirror_err = None;
if use_versions_host_for_attestations(Some(api_url), use_versions_host) {
match detect_with_mirror(owner, repo, digest, Some(predicate_type)).await {
MirrorVerdict::Done(found) => return found,
MirrorVerdict::AskGithub => {}
MirrorVerdict::RetryGithub(err) => mirror_err = Some(err),
}
}
let client = attestation_client(api_url).map_err(DetectError::SourceCreation)?;
let digest = if digest.contains(':') {
digest.to_string()
} else {
format!("sha256:{digest}")
};
let attestations = client
.fetch_attestations(FetchParams {
owner: owner.to_string(),
repo: Some(format!("{owner}/{repo}")),
digest,
limit: 30,
predicate_type: Some(predicate_type.to_string()),
})
.await
.map_err(|err| {
DetectError::Fetch(after_mirror_failure(err, owner, repo, mirror_err.as_ref()))
})?;
Ok(!attestations.is_empty())
}
const MIRRORED_ATTESTATIONS_TTL: std::time::Duration =
std::time::Duration::from_secs(7 * 24 * 60 * 60);
type MirroredAttestations = Option<Vec<mise_sigstore::Attestation>>;
type MirroredAttestationsCell = std::sync::Arc<tokio::sync::OnceCell<MirroredAttestations>>;
static MIRRORED_ATTESTATIONS: std::sync::LazyLock<
std::sync::Mutex<std::collections::HashMap<String, MirroredAttestationsCell>>,
> = std::sync::LazyLock::new(Default::default);
async fn mirrored_attestations(repo: &str, digest: &str) -> eyre::Result<MirroredAttestations> {
if !crate::versions_host::enabled_for_github_repo(repo) {
return Ok(None);
}
if mise_settings::Settings::get().paranoid.provenance() {
return crate::versions_host::github_attestations(repo, digest).await;
}
let key = mirrored_attestations_key(repo, digest);
let cell = MIRRORED_ATTESTATIONS
.lock()
.unwrap()
.entry(key.clone())
.or_default()
.clone();
cell.get_or_try_init(async || {
mirrored_attestations_cache(&key)
.get_or_try_init_async_if(
|| crate::versions_host::github_attestations(repo, digest),
keep_mirrored_attestations,
)
.await
})
.await
.cloned()
}
fn mirrored_attestations_key(repo: &str, digest: &str) -> String {
format!(
"attestations-{}",
crate::hash::hash_to_str(&(repo.to_ascii_lowercase(), digest))
)
}
fn mirrored_attestations_cache(key: &str) -> crate::cache::CacheManager<MirroredAttestations> {
crate::cache::CacheManagerBuilder::new(
crate::dirs::CACHE
.join("github")
.join(format!("{key}.msgpack.z")),
)
.with_fresh_duration(Some(MIRRORED_ATTESTATIONS_TTL))
.build()
}
fn keep_mirrored_attestations(attestations: &MirroredAttestations) -> bool {
attestations
.as_ref()
.is_some_and(|a| !a.is_empty() && a.iter().all(|a| a.has_inline_bundle()))
}
async fn refreshed_mirrored_attestations(
repo: &str,
digest: &str,
) -> eyre::Result<MirroredAttestations> {
let fresh = crate::versions_host::github_attestations(repo, digest).await?;
if keep_mirrored_attestations(&fresh) && !mise_settings::Settings::get().paranoid.provenance() {
let key = mirrored_attestations_key(repo, digest);
if let Err(err) = mirrored_attestations_cache(&key).write(&fresh) {
debug!("failed to keep refreshed attestations for {repo}: {err:#}");
}
MIRRORED_ATTESTATIONS.lock().unwrap().insert(
key,
std::sync::Arc::new(tokio::sync::OnceCell::new_with(Some(fresh.clone()))),
);
}
Ok(fresh)
}
enum MirrorVerdict<T = AttestationResult<bool>> {
Done(T),
AskGithub,
RetryGithub(eyre::Report),
}
fn mirror_error_verdict(full: &str, err: eyre::Report) -> MirrorVerdict {
if crate::versions_host::retries_on_github(&err) {
return MirrorVerdict::RetryGithub(err);
}
MirrorVerdict::Done(Err(mirror_failure(full, &err)))
}
fn after_mirror_failure(
err: AttestationError,
owner: &str,
repo: &str,
mirror_err: Option<&eyre::Report>,
) -> AttestationError {
match mirror_err {
Some(mirror_err) if is_api_failure(&err) => {
let failed = crate::versions_host::GithubRetryFailed::new(
&format!("GitHub attestations of {owner}/{repo}"),
mirror_err,
eyre::Report::new(err),
);
AttestationError::Api(format!("{:#}", eyre::Report::new(failed)))
}
_ => err,
}
}
async fn verify_with_mirror(
artifact_path: &Path,
owner: &str,
repo: &str,
expected_workflow: Option<&str>,
predicate_type: Option<&str>,
artifact_digest: &str,
) -> MirrorVerdict {
let full = format!("{owner}/{repo}");
if !crate::versions_host::enabled_for_github_repo(&full) {
return MirrorVerdict::AskGithub;
}
let digest = format!("sha256:{artifact_digest}");
for fresh in [false, true] {
let found = if fresh {
refreshed_mirrored_attestations(&full, &digest).await
} else {
mirrored_attestations(&full, &digest).await
};
let attestations = match found {
Ok(Some(attestations)) => attestations,
Ok(None) => {
return verdict_after_attestation_404(
&full,
repo_listing(&full).await,
!mirror_may_answer_none(),
|| resolve_token_for_wrapper(None).is_some(),
);
}
Err(_) if !mirror_may_answer_none() => return MirrorVerdict::AskGithub,
Err(err) => return mirror_error_verdict(&full, err),
};
trace!(
"got {} GitHub attestations for {full}@{digest} from mise-versions",
attestations.len()
);
if attestations.is_empty() {
return no_attestations_verdict(&full);
}
if attestations.iter().any(|a| !a.has_inline_bundle()) {
if !fresh {
continue;
}
if !mirror_may_answer_none() {
return MirrorVerdict::AskGithub;
}
return MirrorVerdict::Done(Err(AttestationError::Api(format!(
"mise-versions returned attestations for {full} without their bundles"
))));
}
let selected: Vec<_> = attestations
.into_iter()
.filter(|a| predicate_type.is_none_or(|p| a.matches_predicate_type(p)))
.collect();
if selected.is_empty() {
if fresh {
return no_attestations_verdict(&full);
}
continue;
}
match classify_cached_attestation_verification(
mise_sigstore::verify_github_attestation_sources(
artifact_path,
&selected,
expected_workflow,
)
.await,
) {
CachedAttestationVerification::Verified(sources)
if crate::github::attested_by_repository(owner, repo, &sources).await =>
{
return MirrorVerdict::Done(Ok(true));
}
CachedAttestationVerification::Verified(sources) => {
let made_by = sources.join(", ");
if !mirror_may_answer_none() {
warn!(
"mise-versions returned attestations for {full} made by {made_by}; verifying with GitHub instead"
);
return MirrorVerdict::AskGithub;
}
return MirrorVerdict::Done(Err(AttestationError::Verification(format!(
"the attestations mise-versions has for {full} were made by {made_by}"
))));
}
CachedAttestationVerification::Retry(err) if fresh => {
if !mirror_may_answer_none() {
debug!(
"mise-versions GitHub attestations did not verify for {full}; paranoid mode asks GitHub"
);
return MirrorVerdict::AskGithub;
}
return MirrorVerdict::Done(Err(err.unwrap_or_else(|| {
AttestationError::Verification(format!(
"no attestation mise-versions has for {full} verified this artifact"
))
})));
}
CachedAttestationVerification::Retry(_) => debug!(
"GitHub attestations kept for {full} did not verify; asking mise-versions again"
),
}
}
unreachable!("the second pass always returns")
}
async fn detect_with_mirror(
owner: &str,
repo: &str,
digest: &str,
predicate_type: Option<&str>,
) -> MirrorVerdict<Result<bool, DetectError>> {
let full = format!("{owner}/{repo}");
if !crate::versions_host::enabled_for_github_repo(&full) {
return MirrorVerdict::AskGithub;
}
let digest = if digest.contains(':') {
digest.to_string()
} else {
format!("sha256:{digest}")
};
for fresh in [false, true] {
let found = if fresh {
refreshed_mirrored_attestations(&full, &digest).await
} else {
mirrored_attestations(&full, &digest).await
};
let attestations = match found {
Ok(Some(attestations)) => attestations,
Ok(None) => {
return detect_verdict(verdict_after_attestation_404(
&full,
repo_listing(&full).await,
!mirror_may_answer_none(),
|| resolve_token_for_wrapper(None).is_some(),
));
}
Err(_) if !mirror_may_answer_none() => return MirrorVerdict::AskGithub,
Err(err) => return detect_verdict(mirror_error_verdict(&full, err)),
};
trace!(
"got {} GitHub attestation probes for {full}@{digest} from mise-versions",
attestations.len()
);
if predicate_type.is_some() && attestations.iter().any(|a| !a.has_inline_bundle()) {
if !fresh {
continue;
}
if !mirror_may_answer_none() {
return MirrorVerdict::AskGithub;
}
return MirrorVerdict::Done(Err(DetectError::Fetch(AttestationError::Api(format!(
"mise-versions returned attestations for {full} without their bundles"
)))));
}
let found = attestations
.iter()
.any(|a| predicate_type.is_none_or(|p| a.matches_predicate_type(p)));
if found || attestations.is_empty() || fresh {
if !found && !mirror_may_answer_none() {
debug!(
"mise-versions has no attestations for {full}@{digest}; paranoid mode asks GitHub"
);
return MirrorVerdict::AskGithub;
}
return MirrorVerdict::Done(Ok(found));
}
}
unreachable!("the second pass always returns")
}
fn no_attestations_verdict(full: &str) -> MirrorVerdict {
if mirror_may_answer_none() {
MirrorVerdict::Done(Err(AttestationError::NoAttestations))
} else {
debug!("mise-versions has no GitHub attestations for {full}; paranoid mode asks GitHub");
MirrorVerdict::AskGithub
}
}
enum RepoListing {
Listed,
Unknown,
Failed(eyre::Report),
}
async fn repo_listing(full: &str) -> RepoListing {
match crate::versions_host::github_releases(full, 1).await {
Ok(Some(_)) => RepoListing::Listed,
Ok(None) => RepoListing::Unknown,
Err(err) => RepoListing::Failed(err),
}
}
fn verdict_after_attestation_404(
full: &str,
listing: RepoListing,
paranoid: bool,
has_token: impl FnOnce() -> bool,
) -> MirrorVerdict {
match listing {
RepoListing::Listed if paranoid => MirrorVerdict::AskGithub,
RepoListing::Listed => MirrorVerdict::Done(Err(AttestationError::NoAttestations)),
RepoListing::Unknown if paranoid => MirrorVerdict::AskGithub,
RepoListing::Unknown => unknown_repo_verdict(full, has_token()),
RepoListing::Failed(_) if paranoid => MirrorVerdict::AskGithub,
RepoListing::Failed(err) => mirror_error_verdict(full, err),
}
}
fn detect_verdict(verdict: MirrorVerdict) -> MirrorVerdict<Result<bool, DetectError>> {
match verdict {
MirrorVerdict::AskGithub => MirrorVerdict::AskGithub,
MirrorVerdict::RetryGithub(err) => MirrorVerdict::RetryGithub(err),
MirrorVerdict::Done(Err(AttestationError::NoAttestations)) => {
MirrorVerdict::Done(Ok(false))
}
MirrorVerdict::Done(Err(err)) => MirrorVerdict::Done(Err(DetectError::Fetch(err))),
MirrorVerdict::Done(Ok(found)) => MirrorVerdict::Done(Ok(found)),
}
}
fn unknown_repo_verdict(full: &str, has_token: bool) -> MirrorVerdict {
if has_token {
debug!("mise-versions does not know {full}; asking GitHub, which may hold it privately");
return MirrorVerdict::AskGithub;
}
MirrorVerdict::Done(Err(AttestationError::Api(format!(
"GitHub repository {full} not found (404 from mise-versions.jdx.dev, which mise asks \
before api.github.com). If the repository is private, set GITHUB_TOKEN; to ask \
GitHub directly, set use_versions_host = false."
))))
}
fn mirror_failure(full: &str, err: &eyre::Report) -> AttestationError {
AttestationError::Api(format!(
"{} {err:#}",
crate::github::versions_host_failure(&format!("GitHub attestations of {full}"))
))
}
fn mirror_may_answer_none() -> bool {
!mise_settings::Settings::get().paranoid.provenance()
}
fn use_versions_host_for_attestations(api_url: Option<&str>, use_versions_host: bool) -> bool {
if !use_versions_host || !crate::versions_host::enabled_for_github_metadata() {
return false;
}
api_url
.unwrap_or(crate::github::API_URL)
.trim_end_matches('/')
== crate::github::API_URL
}
pub async fn verify_slsa_provenance(
artifact_path: &Path,
provenance_path: &Path,
min_level: u8,
signer: SlsaSignerIdentity<'_>,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
let verify = async {
let result = mise_sigstore::verify_slsa_provenance(
artifact_path,
provenance_path,
min_level,
signer,
)
.await;
match renamed_slsa_source(&result, provenance_path, signer, None).await {
Some(renamed) => {
mise_sigstore::verify_slsa_provenance(
artifact_path,
provenance_path,
min_level,
SlsaSignerIdentity {
source_repository: Some(&renamed),
..signer
},
)
.await
}
None => result,
}
};
if !mise_settings::Settings::get().generate_lockfiles() {
return verify.await;
}
let artifact_digest = mise_sigstore::calculate_file_digest(artifact_path).await?;
let provenance_digest = mise_sigstore::calculate_file_digest(provenance_path).await?;
let key = format!(
"slsa:{artifact_digest}:{provenance_digest}:{min_level}:{}:{}:{:?}:{:?}",
signer.identity, signer.issuer, signer.source_repository, signer.source_tag
);
shared_verification(key, verify).await
}
pub async fn verify_slsa_provenance_artifacts(
provenance_path: &Path,
artifacts: &[SlsaArtifact],
min_level: u8,
signer: SlsaSignerIdentity<'_>,
) -> AttestationResult<bool> {
verify_slsa_provenance_artifacts_with(provenance_path, artifacts, min_level, signer, None).await
}
type ConfirmRename<'a> = Option<&'a (dyn Fn(&str, &str, &str) -> bool + Sync)>;
async fn verify_slsa_provenance_artifacts_with(
provenance_path: &Path,
artifacts: &[SlsaArtifact],
min_level: u8,
signer: SlsaSignerIdentity<'_>,
confirm: ConfirmRename<'_>,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
let result = mise_sigstore::verify_slsa_provenance_artifacts(
provenance_path,
artifacts,
min_level,
signer,
)
.await;
match renamed_slsa_source(&result, provenance_path, signer, confirm).await {
Some(renamed) => {
mise_sigstore::verify_slsa_provenance_artifacts(
provenance_path,
artifacts,
min_level,
SlsaSignerIdentity {
source_repository: Some(&renamed),
..signer
},
)
.await
}
None => result,
}
}
async fn renamed_slsa_source(
result: &AttestationResult<bool>,
provenance_path: &Path,
signer: SlsaSignerIdentity<'_>,
confirm: ConfirmRename<'_>,
) -> Option<String> {
if result.is_ok() {
return None;
}
let (owner, repo) = signer
.source_repository
.and_then(mise_sigstore::normalize_github_repository)?
.split_once('/')?;
let unaccepted =
mise_sigstore::slsa_provenance_unaccepted_source_repositories(provenance_path, signer)
.await
.ok()?;
for other in unaccepted.into_iter().take(3) {
let confirmed = match confirm {
Some(confirm) => confirm(owner, repo, &other),
None => {
crate::github::attested_by_repository(owner, repo, std::slice::from_ref(&other))
.await
}
};
if confirmed {
debug!("SLSA provenance for {owner}/{repo} was built in {other}, its former name");
return Some(other);
}
}
None
}
pub fn is_slsa_subject_mismatch(error: &AttestationError) -> bool {
mise_sigstore::is_slsa_subject_mismatch(error)
}
pub fn is_api_failure(error: &AttestationError) -> bool {
matches!(error, AttestationError::Api(_) | AttestationError::Http(_))
}
pub async fn verify_cosign_signature(
artifact_path: &Path,
sig_or_bundle_path: &Path,
identity: &CosignIdentity,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
if !mise_settings::Settings::get().generate_lockfiles() {
return mise_sigstore::verify_cosign_signature(artifact_path, sig_or_bundle_path, identity)
.await;
}
let artifact_digest = mise_sigstore::calculate_file_digest(artifact_path).await?;
let signature_digest = mise_sigstore::calculate_file_digest(sig_or_bundle_path).await?;
let key = format!("cosign:{artifact_digest}:{signature_digest}:{identity:?}");
shared_verification(
key,
mise_sigstore::verify_cosign_signature(artifact_path, sig_or_bundle_path, identity),
)
.await
}
pub async fn verify_cosign_signature_with_key(
artifact_path: &Path,
sig_or_bundle_path: &Path,
public_key_path: &Path,
) -> AttestationResult<bool> {
mise_sigstore::set_tuf_url(routed_tuf_url());
mise_sigstore::verify_cosign_signature_with_key(
artifact_path,
sig_or_bundle_path,
public_key_path,
)
.await
}
pub async fn verify_github_attestation_sources_for_sha512(
sha512: &[u8],
attestations: &[Attestation],
signer_workflow: Option<&str>,
) -> AttestationResult<Vec<String>> {
mise_sigstore::set_tuf_url(routed_tuf_url());
mise_sigstore::verify_github_attestation_sources_for_sha512(
sha512,
attestations,
signer_workflow,
)
.await
}
pub async fn verify_forge_attestation_sources_for_sha512(
sha512: &[u8],
attestations: &[Attestation],
) -> AttestationResult<Vec<String>> {
mise_sigstore::set_tuf_url(routed_tuf_url());
mise_sigstore::verify_forge_attestation_sources_for_sha512(sha512, attestations).await
}
#[cfg(test)]
mod tests {
use super::*;
fn listed() -> RepoListing {
RepoListing::Listed
}
fn unknown() -> RepoListing {
RepoListing::Unknown
}
fn failed_with(status: Option<u16>) -> RepoListing {
RepoListing::Failed(
crate::versions_host::VersionsHostUnavailable {
status,
message: format!("HTTP {status:?}"),
}
.into(),
)
}
fn failed() -> RepoListing {
failed_with(Some(503))
}
#[test]
fn test_attestation_404_verdict_by_listing() {
let verdict = |listing, paranoid, has_token| {
verdict_after_attestation_404("o/r", listing, paranoid, || has_token)
};
assert!(matches!(
verdict(listed(), false, false),
MirrorVerdict::Done(Err(AttestationError::NoAttestations))
));
assert!(matches!(
verdict(unknown(), false, false),
MirrorVerdict::Done(Err(AttestationError::Api(_)))
));
assert!(matches!(
verdict(unknown(), false, true),
MirrorVerdict::AskGithub
));
for listing in [failed(), failed_with(None), failed_with(Some(429))] {
assert!(matches!(
verdict(listing, false, false),
MirrorVerdict::RetryGithub(_)
));
}
assert!(matches!(
verdict(failed(), false, true),
MirrorVerdict::RetryGithub(_)
));
assert!(matches!(
verdict(failed_with(Some(403)), false, false),
MirrorVerdict::Done(Err(AttestationError::Api(_)))
));
assert!(matches!(
verdict(listed(), true, false),
MirrorVerdict::AskGithub
));
assert!(matches!(
verdict(unknown(), true, false),
MirrorVerdict::AskGithub
));
assert!(matches!(
verdict(failed(), true, false),
MirrorVerdict::AskGithub
));
}
#[test]
fn test_attestation_404_resolves_a_token_only_for_an_unknown_repo() {
for listing in [listed(), failed()] {
let _ = verdict_after_attestation_404("o/r", listing, false, || {
panic!("token resolved for a repository mise-versions can answer for")
});
}
}
#[test]
fn test_attestation_404_probe_by_listing() {
let probe = |listing, paranoid, has_token| {
detect_verdict(verdict_after_attestation_404(
"o/r",
listing,
paranoid,
|| has_token,
))
};
assert!(matches!(
probe(listed(), false, false),
MirrorVerdict::Done(Ok(false))
));
assert!(matches!(
probe(unknown(), false, false),
MirrorVerdict::Done(Err(DetectError::Fetch(AttestationError::Api(_))))
));
assert!(matches!(
probe(unknown(), false, true),
MirrorVerdict::AskGithub
));
assert!(matches!(
probe(failed(), false, false),
MirrorVerdict::RetryGithub(_)
));
assert!(matches!(
probe(failed_with(Some(403)), false, false),
MirrorVerdict::Done(Err(DetectError::Fetch(AttestationError::Api(_))))
));
assert!(matches!(
probe(listed(), true, false),
MirrorVerdict::AskGithub
));
assert!(matches!(
probe(failed(), true, false),
MirrorVerdict::AskGithub
));
}
#[test]
fn test_github_failure_after_mirror_failure_names_both() {
let mirror_err: eyre::Report = crate::versions_host::VersionsHostUnavailable {
status: Some(503),
message: "HTTP status server error (503 Service Unavailable)".to_string(),
}
.into();
let err = after_mirror_failure(
AttestationError::Api("HTTP 403 rate limit exceeded".to_string()),
"o",
"r",
Some(&mirror_err),
);
assert!(is_api_failure(&err));
let message = err.to_string();
assert!(message.contains("mise-versions.jdx.dev"), "{message}");
assert!(message.contains("503 Service Unavailable"), "{message}");
assert!(message.contains("api.github.com"), "{message}");
assert!(message.contains("rate limit exceeded"), "{message}");
assert!(matches!(
after_mirror_failure(
AttestationError::NoAttestations,
"o",
"r",
Some(&mirror_err)
),
AttestationError::NoAttestations
));
assert!(matches!(
after_mirror_failure(AttestationError::Api("x".into()), "o", "r", None),
AttestationError::Api(m) if m == "x"
));
}
use crate::env as mise_env;
use confique::Layer;
const TOKEN_ENV_VARS: &[&str] = &[
"MISE_GITHUB_TOKEN",
"GITHUB_API_TOKEN",
"GITHUB_TOKEN",
"MISE_GITHUB_ENTERPRISE_TOKEN",
];
struct SettingsGuard {
_lock: std::sync::MutexGuard<'static, ()>,
}
impl SettingsGuard {
fn new(replacements: Option<indexmap::IndexMap<String, String>>) -> Self {
Self::with_versions_host(replacements, None)
}
fn with_versions_host(
replacements: Option<indexmap::IndexMap<String, String>>,
use_versions_host: Option<bool>,
) -> Self {
let lock = crate::testing::lock_ignoring_poison(&crate::testing::SETTINGS_LOCK);
let mut settings = mise_settings::SettingsPartial::empty();
settings.url_replacements = replacements;
settings.use_versions_host = use_versions_host;
crate::testing::reset_settings(Some(settings));
Self { _lock: lock }
}
}
impl Drop for SettingsGuard {
fn drop(&mut self) {
crate::testing::reset_settings(None);
}
}
struct TokenEnvGuard {
saved: Vec<(&'static str, Option<String>)>,
}
impl TokenEnvGuard {
fn new() -> Self {
let saved: Vec<_> = TOKEN_ENV_VARS
.iter()
.map(|name| (*name, std::env::var(name).ok()))
.collect();
for name in TOKEN_ENV_VARS {
mise_env::remove_var(name);
}
Self { saved }
}
}
impl Drop for TokenEnvGuard {
fn drop(&mut self) {
for (name, value) in std::mem::take(&mut self.saved) {
match value {
Some(v) => mise_env::set_var(name, v),
None => mise_env::remove_var(name),
}
}
}
}
#[test]
fn test_resolve_token_wrapper_uses_env_var_with_default_url() {
let _lock = crate::testing::lock_ignoring_poison(&crate::github::TEST_ENV_LOCK);
let _env = TokenEnvGuard::new();
mise_env::set_var("GITHUB_TOKEN", "ghp_wrapper_default");
let resolved = resolve_token_for_wrapper(None);
assert_eq!(
resolved.as_deref(),
Some("ghp_wrapper_default"),
"env var should flow through the wrapper with the default API URL"
);
}
#[test]
fn test_resolve_token_wrapper_uses_env_var_with_explicit_api_url() {
let _lock = crate::testing::lock_ignoring_poison(&crate::github::TEST_ENV_LOCK);
let _env = TokenEnvGuard::new();
mise_env::set_var("MISE_GITHUB_TOKEN", "ghp_explicit_api");
let resolved = resolve_token_for_wrapper(Some(crate::github::API_URL));
assert_eq!(
resolved.as_deref(),
Some("ghp_explicit_api"),
"explicit api.github.com URL should resolve identically to the default"
);
}
#[test]
fn test_resolve_token_wrapper_respects_enterprise_api_url() {
let _lock = crate::testing::lock_ignoring_poison(&crate::github::TEST_ENV_LOCK);
let _env = TokenEnvGuard::new();
mise_env::set_var("GITHUB_TOKEN", "ghp_public_only");
mise_env::set_var("MISE_GITHUB_ENTERPRISE_TOKEN", "ghp_enterprise_only");
let resolved =
resolve_token_for_wrapper(Some("https://github.enterprise.example.com/api/v3"));
assert_eq!(
resolved.as_deref(),
Some("ghp_enterprise_only"),
"enterprise api_url should resolve the enterprise token, not the public one"
);
let resolved_default = resolve_token_for_wrapper(None);
assert_eq!(
resolved_default.as_deref(),
Some("ghp_public_only"),
"default api_url should still resolve the public token"
);
}
struct TokensFileOverrideGuard;
impl TokensFileOverrideGuard {
fn set(host: &str, token: &str) -> Self {
let mut map = std::collections::HashMap::new();
map.insert(host.to_string(), token.to_string());
*crate::github::test_support::TOKENS_FILE_OVERRIDE
.write()
.unwrap() = Some(map);
Self
}
}
impl Drop for TokensFileOverrideGuard {
fn drop(&mut self) {
*crate::github::test_support::TOKENS_FILE_OVERRIDE
.write()
.unwrap() = None;
}
}
#[test]
fn test_resolve_token_wrapper_uses_github_tokens_toml_source() {
let _lock = crate::testing::lock_ignoring_poison(&crate::github::TEST_ENV_LOCK);
let _env = TokenEnvGuard::new();
let _tokens_file = TokensFileOverrideGuard::set("github.com", "ghp_from_tokens_file");
let resolved = resolve_token_for_wrapper(None);
assert_eq!(
resolved.as_deref(),
Some("ghp_from_tokens_file"),
"wrapper should resolve tokens from github_tokens.toml when env vars are empty"
);
}
#[test]
fn test_is_api_failure_excludes_malformed_payloads() {
assert!(is_api_failure(&AttestationError::Api(
"rate limited".into()
)));
assert!(!is_api_failure(&AttestationError::Json(
serde_json::from_str::<serde_json::Value>("{").unwrap_err()
)));
}
#[tokio::test]
async fn test_mirrored_attestations_survive_the_disk_cache() {
let tmp = tempfile::tempdir().unwrap();
let path = tmp.path().join("attestations.msgpack.z");
let bundle = serde_json::json!({
"mediaType": "application/vnd.dev.sigstore.bundle.v0.3+json",
"verificationMaterial": {"tlogEntries": [{"logIndex": "1", "integratedTime": 2}]},
"dsseEnvelope": {"payload": "e30=", "signatures": [{"sig": "AA=="}]},
});
let attestation: mise_sigstore::Attestation =
serde_json::from_value(serde_json::json!({"bundle": bundle, "bundle_url": null}))
.unwrap();
let write = crate::cache::CacheManagerBuilder::new(&path)
.with_fresh_duration(Some(MIRRORED_ATTESTATIONS_TTL))
.build::<MirroredAttestations>();
write.write(&Some(vec![attestation])).unwrap();
let read = crate::cache::CacheManagerBuilder::new(&path)
.with_fresh_duration(Some(MIRRORED_ATTESTATIONS_TTL))
.build::<MirroredAttestations>();
let cached = read
.get_or_try_init_async_if(
async || -> eyre::Result<MirroredAttestations> {
panic!("a fresh cached set must not be fetched again")
},
|a| a.as_ref().is_some_and(|a| !a.is_empty()),
)
.await
.unwrap()
.unwrap();
assert_eq!(cached.len(), 1);
assert!(cached[0].has_inline_bundle());
assert_eq!(
serde_json::to_value(&cached[0]).unwrap()["bundle"],
bundle,
"the bundle changed on its way through the cache"
);
}
#[test]
fn test_cached_attestation_verification_accepts_only_success() {
assert!(matches!(
classify_cached_attestation_verification(Ok(vec!["jdx/mise".to_string()])),
CachedAttestationVerification::Verified(sources) if sources == ["jdx/mise"]
));
assert!(matches!(
classify_cached_attestation_verification(Ok(vec![])),
CachedAttestationVerification::Retry(None)
));
let outcome = classify_cached_attestation_verification(Err(
AttestationError::WorkflowMismatch("stale cached signer".to_string()),
));
assert!(matches!(
outcome,
CachedAttestationVerification::Retry(Some(AttestationError::WorkflowMismatch(_)))
));
}
fn sops_provenance() -> &'static Path {
Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../mise-sigstore/tests/fixtures/sops_v3_9_0.intoto.jsonl"
))
}
fn sops_artifact() -> SlsaArtifact {
SlsaArtifact {
name: "sops-v3.9.0.linux.amd64".to_string(),
sha256: "0d65660fbe785647ff4f1764d7f69edf598f79d6d79ebbef4a501909b6ff6b82".to_string(),
}
}
const SOPS_SIGNER_BEFORE_TRANSFER: SlsaSignerIdentity<'static> = SlsaSignerIdentity {
identity: "https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@refs/tags/v2.0.0",
issuer: "https://token.actions.githubusercontent.com",
source_repository: Some("mozilla/sops"),
source_tag: None,
};
fn verify_sops_rename(
confirm: &(dyn Fn(&str, &str, &str) -> bool + Sync),
) -> AttestationResult<bool> {
let _settings = SettingsGuard::new(None);
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(verify_slsa_provenance_artifacts_with(
sops_provenance(),
&[sops_artifact()],
1,
SOPS_SIGNER_BEFORE_TRANSFER,
Some(confirm),
))
}
#[test]
fn test_slsa_refuses_a_rename_github_does_not_confirm() {
let err = verify_sops_rename(&|_, _, _| false).unwrap_err();
assert!(err.to_string().contains("built in getsops/sops"), "{err}");
}
#[test]
fn test_slsa_accepts_a_rename_github_confirms() {
let confirmed = verify_sops_rename(&|owner, repo, other| {
(owner, repo, other) == ("mozilla", "sops", "getsops/sops")
});
assert!(confirmed.unwrap());
}
fn verify_sops_v3_9_0_as(tag: &str) -> AttestationResult<bool> {
let _settings = SettingsGuard::new(None);
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(verify_slsa_provenance_artifacts(
sops_provenance(),
&[sops_artifact()],
1,
SlsaSignerIdentity {
source_repository: Some("getsops/sops"),
source_tag: Some(tag),
..SOPS_SIGNER_BEFORE_TRANSFER
},
))
}
#[test]
fn test_slsa_rejects_provenance_of_another_release() {
let err = verify_sops_v3_9_0_as("v3.10.0").unwrap_err();
assert!(
err.to_string()
.contains("built from refs/tags/v3.9.0, expected refs/tags/v3.10.0"),
"{err}"
);
}
#[test]
fn test_slsa_accepts_provenance_of_its_release() {
assert!(verify_sops_v3_9_0_as("v3.9.0").unwrap());
}
#[test]
fn test_slsa_rename_cannot_be_confirmed_by_a_mirror() {
let mut mirror = mockito::Server::new();
let lookups = ["mozilla/sops", "getsops/sops"].map(|repo| {
mirror
.mock("GET", format!("/repos/{repo}").as_str())
.with_header("content-type", "application/json")
.with_body(r#"{"id": 1, "full_name": "getsops/sops"}"#)
.expect(0)
.create()
});
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => mirror.url(),
}));
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let err = runtime
.block_on(verify_slsa_provenance_artifacts(
sops_provenance(),
&[sops_artifact()],
1,
SOPS_SIGNER_BEFORE_TRANSFER,
))
.unwrap_err();
assert!(err.to_string().contains("built in getsops/sops"), "{err}");
for lookup in lookups {
lookup.assert();
}
}
#[test]
fn test_rerouted_api_cannot_confirm_a_rename() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "https://github-proxy.invalid".to_string(),
}));
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let err = runtime
.block_on(crate::github::canonical_repo("erlang/rebar3"))
.unwrap_err();
assert!(
err.to_string()
.contains("only GitHub can confirm a repository rename"),
"{err}"
);
let result = runtime.block_on(accept_renamed_repository(
"erlang",
"rebar3",
None,
Err(AttestationError::RepositoryMismatch {
expected: "erlang/rebar3".to_string(),
found: vec!["jdx/mise".to_string()],
}),
));
assert!(
matches!(result, Err(AttestationError::RepositoryMismatch { .. })),
"{result:?}"
);
}
fn original_host_token() -> Option<String> {
Some("ghp_original_host".to_string())
}
#[test]
fn test_routed_api_url_applies_simple_url_replacement() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "https://github-proxy.example.com".to_string(),
}));
let (routed, token) =
routed_api_url(crate::github::API_URL, original_host_token()).unwrap();
assert_eq!(routed, "https://github-proxy.example.com/");
assert_eq!(token, None);
}
#[test]
fn test_routed_api_url_applies_regex_url_replacement() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"regex:^https://api\\.github\\.com".to_string() => "https://github-proxy.example.com/api".to_string(),
}));
let (routed, token) =
routed_api_url(crate::github::API_URL, original_host_token()).unwrap();
assert_eq!(routed, "https://github-proxy.example.com/api/");
assert_eq!(token, None);
}
#[test]
fn test_routed_api_url_keeps_original_url_without_replacement() {
let _settings = SettingsGuard::new(None);
let (routed, token) =
routed_api_url(crate::github::API_URL, original_host_token()).unwrap();
assert_eq!(routed, crate::github::API_URL);
assert_eq!(token, original_host_token());
}
#[test]
fn test_routed_api_url_keeps_token_on_same_host_replacement() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "https://api.github.com/proxy".to_string(),
}));
let (routed, token) =
routed_api_url(crate::github::API_URL, original_host_token()).unwrap();
assert_eq!(routed, "https://api.github.com/proxy/");
assert_eq!(token, original_host_token());
}
#[test]
fn test_routed_api_url_drops_inherited_userinfo_on_cross_host_replacement() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"regex:^https://(.*)ghe\\.example\\.com".to_string()
=> "https://${1}github-proxy.example.com".to_string(),
}));
let (routed, token) = routed_api_url(
"https://user:password@ghe.example.com/api/v3",
original_host_token(),
)
.unwrap();
assert_eq!(routed, "https://github-proxy.example.com/api/v3");
assert_eq!(token, None);
}
#[test]
fn test_routed_api_url_rejects_credential_downgrade() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "http://api.github.com".to_string(),
}));
let err = routed_api_url(crate::github::API_URL, original_host_token()).unwrap_err();
assert!(err.to_string().contains("refusing to send credentials"));
assert!(!is_api_failure(&err));
}
#[test]
fn test_routed_api_url_drops_token_on_cross_host_downgrade() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "http://github-proxy.example.com".to_string(),
}));
let (routed, token) =
routed_api_url(crate::github::API_URL, original_host_token()).unwrap();
assert_eq!(routed, "http://github-proxy.example.com/");
assert_eq!(token, None);
}
#[test]
fn test_routed_api_url_rejects_userinfo_downgrade_without_token() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string()
=> "http://user:password@github-proxy.example.com".to_string(),
}));
let err = routed_api_url(crate::github::API_URL, None).unwrap_err();
assert!(err.to_string().contains("refusing to send credentials"));
assert!(!err.to_string().contains("password"));
assert!(!is_api_failure(&err));
}
#[test]
fn test_routed_tuf_url_applies_url_replacement() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://tuf-repo-cdn.sigstore.dev".to_string()
=> "https://tuf-mirror.example.com".to_string(),
}));
let routed = routed_tuf_url();
assert_eq!(routed.as_deref(), Some("https://tuf-mirror.example.com/"));
}
#[test]
fn test_routed_tuf_url_none_without_replacement() {
let _settings = SettingsGuard::new(None);
assert_eq!(routed_tuf_url(), None);
}
#[test]
fn test_routed_tuf_url_ignores_userinfo_downgrade() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://tuf-repo-cdn.sigstore.dev".to_string()
=> "http://user:password@tuf-mirror.example.com".to_string(),
}));
assert_eq!(routed_tuf_url(), None);
}
#[test]
fn test_use_versions_host_for_attestations_respects_setting() {
let _settings = SettingsGuard::with_versions_host(None, Some(false));
assert!(!use_versions_host_for_attestations(
Some(crate::github::API_URL),
true
));
}
#[test]
fn test_use_versions_host_for_attestations_defers_to_url_replacements() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com".to_string() => "https://github-proxy.example.com".to_string(),
}));
assert!(!use_versions_host_for_attestations(
Some(crate::github::API_URL),
true
));
}
#[tokio::test]
async fn test_mirrored_attestations_defer_to_a_repository_url_replacement() {
let repo = "acme/attested-tool";
let digest = "sha256:0";
let key = format!(
"attestations-{}",
crate::hash::hash_to_str(&(repo.to_ascii_lowercase(), digest))
);
let attestation: mise_sigstore::Attestation =
serde_json::from_value(serde_json::json!({"bundle": {}, "bundle_url": null})).unwrap();
let cell = MirroredAttestationsCell::default();
cell.set(Some(vec![attestation])).unwrap();
MIRRORED_ATTESTATIONS.lock().unwrap().insert(key, cell);
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com/repos/acme/attested-tool/".to_string()
=> "https://github-proxy.example.com/repos/acme/attested-tool/".to_string(),
}));
assert!(mirrored_attestations(repo, digest).await.unwrap().is_none());
}
#[test]
fn test_path_specific_url_replacements_are_detected_per_repository() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com/repos/acme/tool/".to_string()
=> "https://github-proxy.example.com/repos/acme/tool/".to_string(),
}));
assert!(!crate::versions_host::github_is_url_replaced(None));
assert!(crate::versions_host::github_is_url_replaced(Some(
"acme/tool"
)));
assert!(!crate::versions_host::github_is_url_replaced(Some(
"acme/other"
)));
}
#[test]
fn test_exact_release_url_replacements_are_honored() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
"https://api.github.com/repos/acme/tool/releases/tags/v2.0.0".to_string()
=> "https://github-proxy.example.com/acme/tool/v2.0.0".to_string(),
}));
assert!(crate::versions_host::github_release_is_url_replaced(
"acme/tool",
"v2.0.0"
));
assert!(!crate::versions_host::github_release_is_url_replaced(
"acme/tool",
"v1.0.0"
));
assert!(
crate::github::release_cache_key_for_test("acme/tool", "v2.0.0").ends_with("direct")
);
assert!(
crate::github::release_cache_key_for_test("acme/tool", "v1.0.0").ends_with("hosted-2")
);
}
#[test]
fn test_download_only_url_replacements_keep_mise_versions() {
let _settings = SettingsGuard::new(Some(indexmap::indexmap! {
r"regex:^https://github\.com/([^/]+)/([^/]+)/releases/download/(.+)".to_string()
=> "https://hub.example.com/artifactory/github/$1/$2/$3".to_string(),
}));
assert!(!crate::versions_host::github_is_url_replaced(Some(
"acme/tool"
)));
}
#[test]
fn test_use_versions_host_for_attestations_respects_caller_gate() {
let _settings = SettingsGuard::with_versions_host(None, Some(true));
assert!(!use_versions_host_for_attestations(
Some(crate::github::API_URL),
false
));
assert!(use_versions_host_for_attestations(
Some(crate::github::API_URL),
true
));
}
}