use base64::Engine as _;
use chrono::Utc;
use ed25519_dalek::{Signature, VerifyingKey};
use greentic_trust::DidWeb;
use sha2::{Digest, Sha256};
use std::io::Read as _;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::time::Duration;
use crate::mcp_local::cache_dir;
pub(crate) const TRUSTED_SIGNERS_ENV: &str = "GREENTIC_MCP_TRUSTED_SIGNERS";
pub(crate) const STORE_URL_ENV: &str = "GREENTIC_STORE_URL";
pub(crate) const STORE_TOKEN_ENV: &str = "GREENTIC_STORE_TOKEN";
pub(crate) const TRUST_DID_ENV: &str = "GREENTIC_RUNNER_TRUST_DID";
const DESCRIBE_ENTRY: &str = "describe.json";
const WASM_ENTRY: &str = "extension.wasm";
const MAX_ARTIFACT_BYTES: u64 = 256 * 1024 * 1024;
const DOWNLOAD_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const DOWNLOAD_READ_TIMEOUT: Duration = Duration::from_secs(30);
const DOWNLOAD_TOTAL_TIMEOUT: Duration = Duration::from_secs(300);
const MAX_ZIP_ENTRY_BYTES: u64 = 512 * 1024 * 1024;
#[derive(Debug, thiserror::Error)]
pub enum StorePullError {
#[error("store-pull config error: {0}")]
Config(String),
#[error("store-pull network error: {0}")]
Network(String),
#[error("store-pull integrity error: {0}")]
Integrity(String),
#[error("store-pull signature error: {0}")]
Signature(String),
#[error("store-pull archive error: {0}")]
Archive(String),
#[error("store-pull io error: {0}")]
Io(String),
}
pub async fn ensure_cached(
component_ref: &str,
component_version: &str,
component_digest: &str,
) -> Result<(), StorePullError> {
validate_component_ref(component_ref)?;
validate_component_version(component_version)?;
let dest = cache_dir().join(format!("{component_ref}.wasm"));
if cache_is_current(&dest, component_digest) {
return Ok(());
}
let lock = pull_lock(component_ref);
let _held = lock.lock().await;
if cache_is_current(&dest, component_digest) {
return Ok(());
}
let base = std::env::var(STORE_URL_ENV)
.map_err(|_| StorePullError::Config(format!("{STORE_URL_ENV} unset")))?;
let url = format!(
"{}/api/v1/extensions/{component_ref}/{component_version}/artifact",
base.trim_end_matches('/')
);
let archive_bytes = download_artifact(&url).await?;
let computed = hex_sha256(&archive_bytes);
if !computed.eq_ignore_ascii_case(component_digest) {
return Err(StorePullError::Integrity(format!(
"digest mismatch for {component_ref}@{component_version}: computed {computed}, expected {component_digest}"
)));
}
let (describe, wasm) = unzip_describe_and_wasm(&archive_bytes)?;
verify_authenticity(&describe).await?;
let wasm_digest = hex_sha256(&wasm);
write_atomic(&sidecar_path(&dest), wasm_digest.as_bytes())
.map_err(|e| StorePullError::Io(format!("write wasm sidecar: {e}")))?;
write_atomic(&dest, &wasm).map_err(|e| StorePullError::Io(e.to_string()))?;
write_atomic(>xpack_marker_path(&dest), component_digest.as_bytes())
.map_err(|e| StorePullError::Io(format!("write gtxpack marker: {e}")))?;
Ok(())
}
fn cache_is_current(dest: &Path, digest: &str) -> bool {
dest.exists()
&& sidecar_path(dest).exists()
&& gtxpack_marker_matches(>xpack_marker_path(dest), digest)
}
fn pull_lock(component_ref: &str) -> std::sync::Arc<tokio::sync::Mutex<()>> {
static LOCKS: OnceLock<
std::sync::Mutex<std::collections::HashMap<String, std::sync::Arc<tokio::sync::Mutex<()>>>>,
> = OnceLock::new();
let map = LOCKS.get_or_init(Default::default);
let mut guard = map
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
guard.entry(component_ref.to_string()).or_default().clone()
}
pub(crate) fn validate_component_ref(component_ref: &str) -> Result<(), StorePullError> {
let ok = !component_ref.is_empty()
&& component_ref.len() <= 128
&& !component_ref.starts_with('.')
&& !component_ref.contains("..")
&& component_ref
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'));
if ok {
Ok(())
} else {
Err(StorePullError::Config(format!(
"invalid component_ref {component_ref:?}: only [A-Za-z0-9._-] is allowed, no path traversal"
)))
}
}
fn validate_component_version(version: &str) -> Result<(), StorePullError> {
let ok = !version.is_empty()
&& version.len() <= 128
&& !version.contains("..")
&& version
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-' | b'+'));
if ok {
Ok(())
} else {
Err(StorePullError::Config(format!(
"invalid component_version {version:?}"
)))
}
}
pub(crate) fn sidecar_path(wasm_dest: &std::path::Path) -> std::path::PathBuf {
let mut path = wasm_dest.to_path_buf();
let new_extension = match path.extension() {
Some(ext) => format!("{}.sha256", ext.to_string_lossy()),
None => "sha256".to_string(),
};
path.set_extension(new_extension);
path
}
fn gtxpack_marker_path(wasm_dest: &std::path::Path) -> std::path::PathBuf {
let mut path = wasm_dest.to_path_buf();
let new_extension = match path.extension() {
Some(ext) => format!("{}.gtxpack", ext.to_string_lossy()),
None => "gtxpack".to_string(),
};
path.set_extension(new_extension);
path
}
fn gtxpack_marker_matches(marker: &std::path::Path, expected_digest: &str) -> bool {
match std::fs::read_to_string(marker) {
Ok(contents) => contents.trim().eq_ignore_ascii_case(expected_digest.trim()),
Err(_) => false,
}
}
pub fn verify_describe_signature(
describe: &serde_json::Value,
trusted: &[VerifyingKey],
) -> Result<(), StorePullError> {
let signature_object = describe
.get("signature")
.ok_or_else(|| StorePullError::Signature("describe.json has no signature".into()))?;
let signature_b64 = signature_object
.get("value")
.and_then(|value| value.as_str())
.ok_or_else(|| {
StorePullError::Signature("signature.value missing or not a string".into())
})?;
let mut unsigned = describe.clone();
unsigned
.as_object_mut()
.ok_or_else(|| StorePullError::Signature("describe.json is not a JSON object".into()))?
.remove("signature");
let signed_message = serde_jcs::to_vec(&unsigned)
.map_err(|e| StorePullError::Signature(format!("re-serialize describe (JCS): {e}")))?;
let signature_bytes = base64::engine::general_purpose::STANDARD
.decode(signature_b64.trim())
.map_err(|e| StorePullError::Signature(format!("signature is not valid base64: {e}")))?;
let signature = Signature::from_slice(&signature_bytes)
.map_err(|e| StorePullError::Signature(format!("malformed Ed25519 signature: {e}")))?;
if trusted.is_empty() {
return Err(StorePullError::Signature(format!(
"no trusted signers configured ({TRUSTED_SIGNERS_ENV} empty)"
)));
}
if trusted
.iter()
.any(|key| key.verify_strict(&signed_message, &signature).is_ok())
{
Ok(())
} else {
Err(StorePullError::Signature(
"describe signature does not match any trusted signer".into(),
))
}
}
pub fn trusted_signers() -> Vec<VerifyingKey> {
let raw = match std::env::var(TRUSTED_SIGNERS_ENV) {
Ok(value) => value,
Err(_) => return Vec::new(),
};
raw.split(',')
.filter_map(|entry| parse_trusted_signer(entry.trim()))
.collect()
}
pub(crate) fn trust_did() -> Option<String> {
match std::env::var(TRUST_DID_ENV) {
Ok(value) if !value.trim().is_empty() => Some(value.trim().to_string()),
_ => None,
}
}
pub(crate) fn map_trust_error(err: greentic_trust::TrustError) -> StorePullError {
StorePullError::Signature(format!("did:web trust verification failed: {err}"))
}
const RESOLVER_TTL: Duration = Duration::from_secs(300);
const RESOLVER_CAPACITY: u64 = 16;
fn shared_resolver() -> Result<&'static greentic_trust::HttpResolver, StorePullError> {
static RESOLVER: OnceLock<greentic_trust::HttpResolver> = OnceLock::new();
if let Some(resolver) = RESOLVER.get() {
return Ok(resolver);
}
let built = greentic_trust::HttpResolver::new(RESOLVER_TTL, RESOLVER_CAPACITY)
.map_err(|e| StorePullError::Config(format!("failed to build trust resolver: {e}")))?;
let _ = RESOLVER.set(built);
RESOLVER
.get()
.ok_or_else(|| StorePullError::Config("trust resolver unavailable after init".into()))
}
pub(crate) async fn verify_certified(
describe: &serde_json::Value,
did: &str,
resolver: &greentic_trust::HttpResolver,
) -> Result<(), StorePullError> {
let did = DidWeb::parse(did)
.map_err(|e| StorePullError::Config(format!("invalid {TRUST_DID_ENV}: {e}")))?;
greentic_trust::verify_describe(describe, &did, resolver, Utc::now())
.await
.map(|_key| ())
.map_err(map_trust_error)
}
async fn verify_authenticity(describe: &serde_json::Value) -> Result<(), StorePullError> {
match trust_did() {
Some(did) => verify_certified(describe, &did, shared_resolver()?).await,
None => verify_describe_signature(describe, &trusted_signers()),
}
}
#[cfg(test)]
pub(crate) async fn verify_authenticity_for_test(
describe: &serde_json::Value,
) -> Result<(), StorePullError> {
verify_authenticity(describe).await
}
fn parse_trusted_signer(entry: &str) -> Option<VerifyingKey> {
if entry.is_empty() {
return None;
}
let key_b64 = match entry.split_once(':') {
Some((algorithm, key)) => {
if !algorithm.eq_ignore_ascii_case("ed25519") {
return None;
}
key.trim()
}
None => entry,
};
let raw = base64::engine::general_purpose::STANDARD
.decode(key_b64)
.ok()?;
let key_bytes: [u8; 32] = raw.as_slice().try_into().ok()?;
VerifyingKey::from_bytes(&key_bytes).ok()
}
async fn download_artifact(url: &str) -> Result<Vec<u8>, StorePullError> {
let client = reqwest::Client::builder()
.connect_timeout(DOWNLOAD_CONNECT_TIMEOUT)
.read_timeout(DOWNLOAD_READ_TIMEOUT)
.timeout(DOWNLOAD_TOTAL_TIMEOUT)
.build()
.map_err(|e| StorePullError::Network(format!("build http client: {e}")))?;
let shown = crate::url_redact::redact_http_url(url);
let mut request = client.get(url);
if let Ok(token) = std::env::var(STORE_TOKEN_ENV)
&& !token.is_empty()
{
request = request.bearer_auth(token);
}
let response = request
.send()
.await
.map_err(|e| StorePullError::Network(format!("GET {shown}: {}", e.without_url())))?;
let status = response.status();
if !status.is_success() {
return Err(StorePullError::Network(format!(
"GET {shown} returned HTTP {status}"
)));
}
if let Some(content_length) = response.content_length()
&& content_length > MAX_ARTIFACT_BYTES
{
return Err(StorePullError::Integrity(format!(
"artifact at {shown} claims Content-Length {content_length} which exceeds the {MAX_ARTIFACT_BYTES}-byte cap"
)));
}
use futures::StreamExt as _;
let stream = response
.bytes_stream()
.map(|chunk| chunk.map_err(|e| e.without_url().to_string()));
collect_capped(Box::pin(stream), MAX_ARTIFACT_BYTES, url).await
}
async fn collect_capped<S, B, E>(
mut stream: S,
cap: u64,
url: &str,
) -> Result<Vec<u8>, StorePullError>
where
S: futures::Stream<Item = Result<B, E>> + Unpin,
B: AsRef<[u8]>,
E: std::fmt::Display,
{
use futures::StreamExt as _;
let url = crate::url_redact::redact_http_url(url);
let mut body: Vec<u8> = Vec::new();
while let Some(chunk) = stream.next().await {
let chunk =
chunk.map_err(|e| StorePullError::Network(format!("read body from {url}: {e}")))?;
let chunk = chunk.as_ref();
if (body.len() as u64).saturating_add(chunk.len() as u64) > cap {
return Err(StorePullError::Integrity(format!(
"artifact at {url} exceeds the {cap}-byte cap while streaming"
)));
}
body.extend_from_slice(chunk);
}
Ok(body)
}
pub(crate) fn hex_sha256(bytes: &[u8]) -> String {
use std::fmt::Write as _;
let digest = Sha256::digest(bytes);
digest.iter().fold(
String::with_capacity(digest.len() * 2),
|mut accumulator, byte| {
let _ = write!(accumulator, "{byte:02x}");
accumulator
},
)
}
fn unzip_describe_and_wasm(
archive_bytes: &[u8],
) -> Result<(serde_json::Value, Vec<u8>), StorePullError> {
let cursor = std::io::Cursor::new(archive_bytes);
let mut archive = zip::ZipArchive::new(cursor)
.map_err(|e| StorePullError::Archive(format!("open gtxpack: {e}")))?;
let describe_raw = read_zip_entry(&mut archive, DESCRIBE_ENTRY)?;
let describe: serde_json::Value = serde_json::from_slice(&describe_raw)
.map_err(|e| StorePullError::Archive(format!("parse {DESCRIBE_ENTRY}: {e}")))?;
let wasm = read_zip_entry(&mut archive, WASM_ENTRY)?;
Ok((describe, wasm))
}
fn read_zip_entry<R: std::io::Read + std::io::Seek>(
archive: &mut zip::ZipArchive<R>,
name: &str,
) -> Result<Vec<u8>, StorePullError> {
let entry = archive
.by_name(name)
.map_err(|e| StorePullError::Archive(format!("{name} not in gtxpack: {e}")))?;
let prealloc = usize::try_from(entry.size().min(MAX_ZIP_ENTRY_BYTES)).unwrap_or(0);
let mut buffer = Vec::with_capacity(prealloc);
let read = std::io::Read::take(entry, MAX_ZIP_ENTRY_BYTES + 1)
.read_to_end(&mut buffer)
.map_err(|e| StorePullError::Archive(format!("read {name}: {e}")))?;
if read as u64 > MAX_ZIP_ENTRY_BYTES {
return Err(StorePullError::Archive(format!(
"{name} decompresses past the {MAX_ZIP_ENTRY_BYTES}-byte limit (possible zip bomb)"
)));
}
Ok(buffer)
}
fn write_atomic(dest: &Path, bytes: &[u8]) -> std::io::Result<()> {
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)?;
}
let temp = unique_temp_path(dest);
{
let mut file = std::fs::File::create(&temp)?;
std::io::Write::write_all(&mut file, bytes)?;
file.sync_all()?;
}
if let Err(error) = std::fs::rename(&temp, dest) {
let _ = std::fs::remove_file(&temp);
return Err(error);
}
Ok(())
}
fn unique_temp_path(dest: &Path) -> PathBuf {
use std::sync::atomic::{AtomicU64, Ordering};
static COUNTER: AtomicU64 = AtomicU64::new(0);
let nonce = COUNTER.fetch_add(1, Ordering::Relaxed);
let pid = std::process::id();
let stem = dest
.file_name()
.and_then(|name| name.to_str())
.unwrap_or("artifact");
let parent = dest.parent().unwrap_or_else(|| Path::new("."));
parent.join(format!(".{stem}.{pid}.{nonce}.tmp"))
}
#[cfg(test)]
pub(crate) mod fixtures;
#[cfg(test)]
mod tests;