Something went wrong. Try again.
Build and run CI pipelines anywhere, consistently. fluentci.io
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121use super::error::{Failure, Result};use axum::http::StatusCode;use object_store::path::Path;
pub(super) struct Key { pub(super) path: Path, pub(super) account: Option<String>, pub(super) cas: Option<String>, pub(super) content_type: &'static str, pub(super) nix_info: bool,}
impl Key { pub(super) fn is_immutable_nix_payload(&self) -> bool { let parts: Vec<_> = self.path.as_ref().split('/').collect(); let offset = if self.account.is_some() { 2 } else { 0 }; if !self.is_nix() || parts.len() != offset + 3 || parts[offset + 1] != "nar" { return false; } let file = parts[offset + 2]; [".nar.zst", ".nar.xz", ".nar.bz2", ".nar.gz", ".nar"] .iter() .find_map(|suffix| file.strip_suffix(suffix)) .is_some_and(|hash| { hash.len() == 52 && hash .bytes() .all(|byte| b"0123456789abcdfghijklmnpqrsvwxyz".contains(&byte)) }) }
pub(super) fn is_nix(&self) -> bool { self.path .as_ref() .split('/') .nth(if self.account.is_some() { 2 } else { 0 }) == Some("nix") }}
pub(super) fn key(raw: &str) -> Result<Key> { if raw.len() > 2048 || raw.contains(['\\', '\0']) || raw.chars().any(char::is_control) || raw .split('/') .any(|s| s.is_empty() || s == "." || s == "..") { return Err(Failure(StatusCode::BAD_REQUEST, "invalid cache key\n")); } let (account, raw) = if let Some(scoped) = raw.strip_prefix("accounts/") { let (id, rest) = scoped .split_once('/') .ok_or(Failure(StatusCode::BAD_REQUEST, "account key required\n"))?; if !super::aliases::valid_did(id) { return Err(Failure( StatusCode::BAD_REQUEST, "invalid account namespace\n", )); } (Some(id.to_owned()), rest) } else { (None, raw) }; if raw.contains('%') { return Err(Failure(StatusCode::BAD_REQUEST, "invalid cache key\n")); } let (namespace, rest) = raw.split_once('/').unwrap_or((raw, "")); let (namespace, rest) = match namespace { "nix" => ("nix", rest), "bazel" => ("bazel", rest), "ac" | "cas" => ("bazel", raw), "gradle" | "sccache" | "maven" | "generic" | "gocache" => (namespace, rest), _ if raw.ends_with(".narinfo") || namespace == "nar" || raw == "nix-cache-info" => { ("nix", raw) } _ => return Err(Failure(StatusCode::NOT_FOUND, "unknown cache namespace\n")), }; if rest.is_empty() { return Err(Failure(StatusCode::BAD_REQUEST, "cache key required\n")); } let mut cas = None; if namespace == "bazel" { let (kind, hash) = rest.split_once('/').ok_or(Failure( StatusCode::BAD_REQUEST, "expected ac/hash or cas/hash\n", ))?; if !matches!(kind, "ac" | "cas") || hash.len() != 64 || !hash .bytes() .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) { return Err(Failure( StatusCode::BAD_REQUEST, "expected lowercase SHA-256 digest\n", )); } if kind == "cas" { cas = Some(hash.to_owned()); } } let content_type = if namespace == "nix" && rest.ends_with(".narinfo") { "text/x-nix-narinfo" } else { "application/octet-stream" }; let prefix = account .as_ref() .map(|id| format!("accounts/{id}/")) .unwrap_or_default(); Ok(Key { account, path: Path::parse(format!("{prefix}{namespace}/{rest}")) .map_err(|_| Failure(StatusCode::BAD_REQUEST, "invalid key\n"))?, cas, content_type, nix_info: namespace == "nix" && rest == "nix-cache-info", })}