use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum RecoveryOutcome {
Verified,
Missing,
Expired,
Malformed,
DigestMismatch,
Refused(String),
Unsupported,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(default)]
pub(crate) struct RecoveryCounters {
pub(crate) handles_emitted: u64,
pub(crate) handles_verified: u64,
pub(crate) failures: u64,
pub(crate) critical_failures: u64,
}
impl RecoveryCounters {
pub(crate) fn observe(&mut self, outcome: &RecoveryOutcome, critical: bool) {
match outcome {
RecoveryOutcome::Verified => {
self.handles_verified = self.handles_verified.saturating_add(1);
}
RecoveryOutcome::Refused(_) => {}
_ => {
self.failures = self.failures.saturating_add(1);
if critical {
self.critical_failures = self.critical_failures.saturating_add(1);
}
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RecoveryVerification {
pub(crate) outcome: RecoveryOutcome,
pub(crate) byte_len: Option<usize>,
pub(crate) digest: Option<String>,
}
impl RecoveryVerification {
pub(crate) fn is_recoverable_for_model(&self) -> bool {
matches!(self.outcome, RecoveryOutcome::Verified)
}
fn unavailable(outcome: RecoveryOutcome) -> Self {
Self {
outcome,
byte_len: None,
digest: None,
}
}
fn resolved(bytes: &[u8], outcome: RecoveryOutcome) -> Self {
Self {
outcome,
byte_len: Some(bytes.len()),
digest: Some(blake3::hash(bytes).to_hex().to_string()),
}
}
}
const MAX_HANDLE_BYTES: usize = 4096;
pub(crate) fn verify_handle(handle: &str, expected_digest: Option<&str>) -> RecoveryVerification {
if handle.is_empty()
|| handle.len() > MAX_HANDLE_BYTES
|| expected_digest.is_some_and(|digest| !valid_digest(digest))
{
return RecoveryVerification::unavailable(RecoveryOutcome::Malformed);
}
if crate::tools::ctx_expand::is_handle_ref(handle) {
return if crate::tools::ctx_expand::resolve_handle_ref(handle).is_some() {
RecoveryVerification::unavailable(RecoveryOutcome::Refused(
"no source authority to read the resolved path".to_string(),
))
} else {
RecoveryVerification::unavailable(RecoveryOutcome::Missing)
};
}
match crate::proxy::ccr::resolve_tee_checked(handle) {
Ok(path) => return verify_tee_file(&path, expected_digest),
Err(crate::proxy::ccr::TeeResolveError::Refused(reason)) => {
return RecoveryVerification::unavailable(RecoveryOutcome::Refused(reason.to_string()));
}
Err(crate::proxy::ccr::TeeResolveError::Malformed)
if crate::proxy::ccr::looks_like_tee_handle(handle) =>
{
return RecoveryVerification::unavailable(RecoveryOutcome::Malformed);
}
Err(crate::proxy::ccr::TeeResolveError::Missing)
if crate::proxy::ccr::is_tee_handle(handle)
&& !crate::proxy::ccr::is_bare_tee_handle(handle) =>
{
return RecoveryVerification::unavailable(RecoveryOutcome::Missing);
}
_ => {}
}
if handle.starts_with("ref_") {
return match crate::server::reference_store::resolve_checked(handle) {
Ok(content) => verify_content(&content, expected_digest, true),
Err(crate::server::reference_store::ReferenceResolveError::Malformed) => {
RecoveryVerification::unavailable(RecoveryOutcome::Malformed)
}
Err(crate::server::reference_store::ReferenceResolveError::Missing) => {
RecoveryVerification::unavailable(RecoveryOutcome::Missing)
}
Err(crate::server::reference_store::ReferenceResolveError::Expired) => {
RecoveryVerification::unavailable(RecoveryOutcome::Expired)
}
Err(crate::server::reference_store::ReferenceResolveError::Refused(reason)) => {
RecoveryVerification::unavailable(RecoveryOutcome::Refused(reason.to_owned()))
}
Err(crate::server::reference_store::ReferenceResolveError::Gateway(_)) => {
RecoveryVerification::unavailable(RecoveryOutcome::Refused(
"stored output is withheld by the context gateway".to_owned(),
))
}
};
}
if handle.starts_with("shell_") {
let Some(archive_id) = crate::core::archive::resolve_alias(handle) else {
return RecoveryVerification::unavailable(RecoveryOutcome::Missing);
};
return verify_archive(&archive_id, expected_digest);
}
if has_uri_scheme(handle) {
return RecoveryVerification::unavailable(RecoveryOutcome::Unsupported);
}
verify_archive(handle, expected_digest)
}
fn verify_tee_file(path: &std::path::Path, expected_digest: Option<&str>) -> RecoveryVerification {
use crate::core::atomic_fs::StoreReadError;
let Ok(state_dir) = crate::core::paths::state_dir() else {
return RecoveryVerification::unavailable(RecoveryOutcome::Missing);
};
let content = match crate::core::atomic_fs::read_store_file(&state_dir.join("tee"), path) {
Ok(content) => content,
Err(StoreReadError::Escapes) => {
return RecoveryVerification::unavailable(RecoveryOutcome::Refused(
"tee handle resolves outside the tee store".to_string(),
));
}
Err(StoreReadError::Io(error)) => {
return RecoveryVerification::unavailable(match error.kind() {
std::io::ErrorKind::PermissionDenied => {
RecoveryOutcome::Refused("tee read refused by filesystem policy".to_string())
}
std::io::ErrorKind::InvalidData => RecoveryOutcome::Malformed,
_ => RecoveryOutcome::Missing,
});
}
};
verify_content(&content, expected_digest, true)
}
fn verify_archive(id: &str, expected_digest: Option<&str>) -> RecoveryVerification {
match crate::core::archive::retrieve_checked(id) {
Ok(content) => {
let address_matches = crate::core::archive::content_matches_id(id, &content);
verify_content(&content, expected_digest, address_matches)
}
Err(crate::core::archive::ArchiveResolveError::Malformed) => {
RecoveryVerification::unavailable(RecoveryOutcome::Malformed)
}
Err(crate::core::archive::ArchiveResolveError::Missing) => {
RecoveryVerification::unavailable(RecoveryOutcome::Missing)
}
Err(crate::core::archive::ArchiveResolveError::Refused(reason)) => {
RecoveryVerification::unavailable(RecoveryOutcome::Refused(reason.to_string()))
}
}
}
fn verify_content(
content: &str,
expected_digest: Option<&str>,
address_matches: bool,
) -> RecoveryVerification {
let actual = blake3::hash(content.as_bytes()).to_hex().to_string();
let expected_matches = expected_digest.is_none_or(|expected| digest_matches(&actual, expected));
let outcome = if expected_matches && address_matches {
RecoveryOutcome::Verified
} else {
RecoveryOutcome::DigestMismatch
};
RecoveryVerification {
outcome,
byte_len: Some(content.len()),
digest: Some(actual),
}
}
fn valid_digest(digest: &str) -> bool {
matches!(digest.len(), 16 | 64) && digest.bytes().all(|b| b.is_ascii_hexdigit())
}
fn digest_matches(actual_full: &str, expected: &str) -> bool {
match expected.len() {
16 => actual_full
.get(..16)
.is_some_and(|prefix| prefix.eq_ignore_ascii_case(expected)),
64 => actual_full.eq_ignore_ascii_case(expected),
_ => false,
}
}
fn has_uri_scheme(handle: &str) -> bool {
let Some((scheme, target)) = handle.split_once("://") else {
return false;
};
!scheme.is_empty()
&& !target.is_empty()
&& scheme
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'-' || b == b'.')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counters_exclude_policy_refusals_from_mechanism_failures() {
let mut counters = RecoveryCounters {
handles_emitted: 3,
..RecoveryCounters::default()
};
counters.observe(&RecoveryOutcome::Verified, false);
counters.observe(
&RecoveryOutcome::Refused("no source authority".to_string()),
true,
);
counters.observe(&RecoveryOutcome::Missing, true);
assert_eq!(counters.handles_emitted, 3);
assert_eq!(counters.handles_verified, 1);
assert_eq!(counters.failures, 1);
assert_eq!(counters.critical_failures, 1);
}
#[test]
fn source_path_references_are_refused_without_source_authority() {
let _lock = crate::core::data_dir::test_env_lock();
let dir = tempfile::tempdir().expect("ledger test directory");
crate::test_env::set_var("LEAN_CTX_DATA_DIR", dir.path());
let mut ledger = crate::core::context_ledger::ContextLedger::new();
ledger.record("/private/source-without-authority.txt", "full", 80, 20);
ledger.save();
let verification = verify_handle("@F1", None);
crate::test_env::remove_var("LEAN_CTX_DATA_DIR");
assert!(matches!(
verification.outcome,
RecoveryOutcome::Refused(ref reason) if reason.contains("no source authority")
));
assert!(!verification.is_recoverable_for_model());
}
}