use std::path::{Path, PathBuf};
use crate::statements::ApprovalStatement;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use super::receipt::{ArtifactEntry, SessionReceipt, RECEIPT_TYPE};
use crate::statements::{
approval_revocation_record_digest, approval_use_record_digest,
journal_checkpoint_record_digest, ApprovalRevocation, ApprovalUse, JournalCheckpoint,
ReplayCheck, ReplayCheckLevel,
};
#[derive(Debug)]
pub enum PackageError {
Io(std::io::Error),
Json(serde_json::Error),
InvalidPackage(String),
}
impl std::fmt::Display for PackageError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Io(e) => write!(f, "package io: {e}"),
Self::Json(e) => write!(f, "package json: {e}"),
Self::InvalidPackage(msg) => write!(f, "invalid package: {msg}"),
}
}
}
impl std::error::Error for PackageError {}
impl From<std::io::Error> for PackageError {
fn from(e: std::io::Error) -> Self {
Self::Io(e)
}
}
impl From<serde_json::Error> for PackageError {
fn from(e: serde_json::Error) -> Self {
Self::Json(e)
}
}
const RECEIPT_FILE: &str = "receipt.json";
const MERKLE_FILE: &str = "merkle.json";
const RENDER_FILE: &str = "render.json";
const ARTIFACTS_DIR: &str = "artifacts";
const PROOFS_DIR: &str = "proofs";
const PREVIEW_FILE: &str = "preview.html";
const APPROVALS_DIR: &str = "approvals";
const APPROVALS_GRANTS: &str = "approvals/grants";
const APPROVALS_USES: &str = "approvals/uses";
const APPROVALS_CHECKPOINTS: &str = "approvals/checkpoints";
const APPROVALS_INDEX_FILE: &str = "approvals/index.json";
#[derive(Debug, Clone, Default)]
pub struct ApprovalsBundle {
pub grants: Vec<(String, Vec<u8>)>,
pub uses: Vec<ApprovalUse>,
pub checkpoints: Vec<JournalCheckpoint>,
pub revocations: Vec<ApprovalRevocation>,
pub action_envelopes: Vec<(String, Vec<u8>)>,
pub sealed_envelopes: Vec<(String, Vec<u8>)>,
pub signer_keys: Vec<(String, String)>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PackageKeys {
pub schema: String,
pub keys: std::collections::BTreeMap<String, String>,
}
pub const KEYS_FILE: &str = "keys.json";
pub const RECORD_FILE: &str = "record.json";
pub const PACKAGE_KEYS_SCHEMA: &str = "treeship/package-keys/v1";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApprovalsIndex {
#[serde(rename = "type")]
pub type_: String,
pub schema_version: u32,
pub grants: Vec<String>,
pub uses: Vec<String>,
pub checkpoints: Vec<String>,
pub revocations: Vec<String>,
}
impl ApprovalsIndex {
pub fn type_string() -> &'static str {
"treeship/approvals-index/v1"
}
}
pub struct PackageOutput {
pub path: PathBuf,
pub receipt_digest: String,
pub merkle_root: Option<String>,
pub file_count: usize,
}
pub fn build_package(
receipt: &SessionReceipt,
output_dir: &Path,
) -> Result<PackageOutput, PackageError> {
build_package_with_approvals(receipt, output_dir, None)
}
pub fn build_package_with_approvals(
receipt: &SessionReceipt,
output_dir: &Path,
bundle: Option<&ApprovalsBundle>,
) -> Result<PackageOutput, PackageError> {
let session_id = &receipt.session.id;
let pkg_dir = output_dir.join(format!("{session_id}.treeship"));
std::fs::create_dir_all(&pkg_dir)?;
std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
std::fs::create_dir_all(pkg_dir.join(PROOFS_DIR))?;
let mut file_count = 0usize;
let receipt_bytes = serde_json::to_vec_pretty(receipt)?;
std::fs::write(pkg_dir.join(RECEIPT_FILE), &receipt_bytes)?;
file_count += 1;
let receipt_hash = Sha256::digest(&receipt_bytes);
let receipt_digest = format!("sha256:{}", hex::encode(receipt_hash));
let merkle_bytes = serde_json::to_vec_pretty(&receipt.merkle)?;
std::fs::write(pkg_dir.join(MERKLE_FILE), &merkle_bytes)?;
file_count += 1;
let render_bytes = serde_json::to_vec_pretty(&receipt.render)?;
std::fs::write(pkg_dir.join(RENDER_FILE), &render_bytes)?;
file_count += 1;
for proof_entry in &receipt.merkle.inclusion_proofs {
let proof_bytes = serde_json::to_vec_pretty(proof_entry)?;
let filename = format!("{}.proof.json", proof_entry.artifact_id);
std::fs::write(pkg_dir.join(PROOFS_DIR).join(filename), &proof_bytes)?;
file_count += 1;
}
if receipt.render.generate_preview {
let preview = render_preview_html_with_approvals(receipt, bundle);
std::fs::write(pkg_dir.join(PREVIEW_FILE), preview.as_bytes())?;
file_count += 1;
}
if let Some(b) = bundle {
if !b.sealed_envelopes.is_empty() {
std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
for (artifact_id, envelope_bytes) in &b.sealed_envelopes {
let safe = sanitize_filename(artifact_id);
let path = pkg_dir.join(ARTIFACTS_DIR).join(format!("{safe}.json"));
if !path.exists() {
std::fs::write(path, envelope_bytes)?;
file_count += 1;
}
}
}
if !b.signer_keys.is_empty() {
let keys = PackageKeys {
schema: PACKAGE_KEYS_SCHEMA.into(),
keys: b.signer_keys.iter().cloned().collect(),
};
std::fs::write(pkg_dir.join(KEYS_FILE), serde_json::to_vec_pretty(&keys)?)?;
file_count += 1;
}
if !b.grants.is_empty()
|| !b.uses.is_empty()
|| !b.checkpoints.is_empty()
|| !b.revocations.is_empty()
|| !b.action_envelopes.is_empty()
{
std::fs::create_dir_all(pkg_dir.join(APPROVALS_GRANTS))?;
std::fs::create_dir_all(pkg_dir.join(APPROVALS_USES))?;
std::fs::create_dir_all(pkg_dir.join(APPROVALS_CHECKPOINTS))?;
std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
for (artifact_id, envelope_bytes) in &b.action_envelopes {
let safe = sanitize_filename(artifact_id);
std::fs::write(
pkg_dir.join(ARTIFACTS_DIR).join(format!("{safe}.json")),
envelope_bytes,
)?;
file_count += 1;
}
let mut grant_ids = Vec::with_capacity(b.grants.len());
for (grant_id, envelope_bytes) in &b.grants {
let safe = sanitize_filename(grant_id);
std::fs::write(
pkg_dir.join(APPROVALS_GRANTS).join(format!("{safe}.json")),
envelope_bytes,
)?;
grant_ids.push(grant_id.clone());
file_count += 1;
}
let mut use_ids = Vec::with_capacity(b.uses.len());
for u in &b.uses {
let safe = sanitize_filename(&u.use_id);
let bytes = serde_json::to_vec_pretty(u)?;
std::fs::write(
pkg_dir.join(APPROVALS_USES).join(format!("{safe}.json")),
&bytes,
)?;
use_ids.push(u.use_id.clone());
file_count += 1;
}
let mut checkpoint_ids = Vec::with_capacity(b.checkpoints.len());
for cp in &b.checkpoints {
let safe = sanitize_filename(&cp.checkpoint_id);
let bytes = serde_json::to_vec_pretty(cp)?;
std::fs::write(
pkg_dir
.join(APPROVALS_CHECKPOINTS)
.join(format!("{safe}.json")),
&bytes,
)?;
checkpoint_ids.push(cp.checkpoint_id.clone());
file_count += 1;
}
let mut revocation_ids = Vec::with_capacity(b.revocations.len());
for rev in &b.revocations {
let safe = sanitize_filename(&rev.revocation_id);
let bytes = serde_json::to_vec_pretty(rev)?;
std::fs::write(
pkg_dir
.join(APPROVALS_DIR)
.join(format!("revocations-{safe}.json")),
&bytes,
)?;
revocation_ids.push(rev.revocation_id.clone());
file_count += 1;
}
let index = ApprovalsIndex {
type_: ApprovalsIndex::type_string().into(),
schema_version: 1,
grants: grant_ids,
uses: use_ids,
checkpoints: checkpoint_ids,
revocations: revocation_ids,
};
let index_bytes = serde_json::to_vec_pretty(&index)?;
std::fs::write(pkg_dir.join(APPROVALS_INDEX_FILE), &index_bytes)?;
file_count += 1;
}
}
Ok(PackageOutput {
path: pkg_dir,
receipt_digest,
merkle_root: receipt.merkle.root.clone(),
file_count,
})
}
fn sanitize_filename(s: &str) -> String {
s.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' {
c
} else {
'_'
}
})
.collect()
}
pub fn read_approvals_bundle(pkg_dir: &Path) -> Result<ApprovalsBundle, PackageError> {
let approvals_dir = pkg_dir.join(APPROVALS_DIR);
if !approvals_dir.is_dir() {
return Ok(ApprovalsBundle::default());
}
let mut bundle = ApprovalsBundle::default();
let grants_dir = pkg_dir.join(APPROVALS_GRANTS);
if grants_dir.is_dir() {
for entry in std::fs::read_dir(&grants_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("json") {
continue;
}
let id = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_string();
let bytes = std::fs::read(&path)?;
bundle.grants.push((id, bytes));
}
}
let uses_dir = pkg_dir.join(APPROVALS_USES);
if uses_dir.is_dir() {
for entry in std::fs::read_dir(&uses_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("json") {
continue;
}
let bytes = std::fs::read(&path)?;
let u: ApprovalUse = serde_json::from_slice(&bytes)?;
bundle.uses.push(u);
}
}
let cps_dir = pkg_dir.join(APPROVALS_CHECKPOINTS);
if cps_dir.is_dir() {
for entry in std::fs::read_dir(&cps_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("json") {
continue;
}
let bytes = std::fs::read(&path)?;
let cp: JournalCheckpoint = serde_json::from_slice(&bytes)?;
bundle.checkpoints.push(cp);
}
}
let arts_dir = pkg_dir.join(ARTIFACTS_DIR);
if arts_dir.is_dir() {
for entry in std::fs::read_dir(&arts_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) != Some("json") {
continue;
}
let id = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("")
.to_string();
let bytes = std::fs::read(&path)?;
bundle.action_envelopes.push((id, bytes));
}
}
Ok(bundle)
}
pub fn read_package(pkg_dir: &Path) -> Result<SessionReceipt, PackageError> {
let receipt_path = pkg_dir.join(RECEIPT_FILE);
if !receipt_path.exists() {
return Err(PackageError::InvalidPackage(format!(
"missing {RECEIPT_FILE} in {}",
pkg_dir.display()
)));
}
let bytes = std::fs::read(&receipt_path)?;
let receipt: SessionReceipt = serde_json::from_slice(&bytes)?;
if receipt.type_ != RECEIPT_TYPE {
return Err(PackageError::InvalidPackage(format!(
"unexpected type: {} (expected {RECEIPT_TYPE})",
receipt.type_
)));
}
Ok(receipt)
}
pub fn verify_package(pkg_dir: &Path) -> Result<Vec<VerifyCheck>, PackageError> {
let trust = match crate::trust::TrustRootStore::open_default_or_empty() {
Ok(t) => t,
Err(e) => {
return Ok(vec![VerifyCheck::fail(
"trust-root",
&format!("trust store unreadable: {e}"),
)]);
}
};
verify_package_with_trust(pkg_dir, &trust)
}
pub fn verify_package_with_trust(
pkg_dir: &Path,
trust: &crate::trust::TrustRootStore,
) -> Result<Vec<VerifyCheck>, PackageError> {
verify_package_with_options(pkg_dir, trust, false)
}
pub fn verify_package_structural(pkg_dir: &Path) -> Result<Vec<VerifyCheck>, PackageError> {
let trust = crate::trust::TrustRootStore::open_default_or_empty()
.unwrap_or_else(|_| crate::trust::TrustRootStore::empty());
verify_package_with_options(pkg_dir, &trust, true)
}
pub fn verify_package_with_options(
pkg_dir: &Path,
trust: &crate::trust::TrustRootStore,
structural_only: bool,
) -> Result<Vec<VerifyCheck>, PackageError> {
let mut checks = Vec::new();
let receipt = match read_package(pkg_dir) {
Ok(r) => {
checks.push(VerifyCheck::pass(
"receipt.json",
"Parses as valid Session Receipt",
));
r
}
Err(e) => {
checks.push(VerifyCheck::fail(
"receipt.json",
&format!("Failed to parse: {e}"),
));
return Ok(checks);
}
};
if receipt.type_ == RECEIPT_TYPE {
checks.push(VerifyCheck::pass("type", "Correct receipt type"));
} else {
checks.push(VerifyCheck::fail(
"type",
&format!("Expected {RECEIPT_TYPE}, got {}", receipt.type_),
));
}
let receipt_path = pkg_dir.join(RECEIPT_FILE);
let on_disk = std::fs::read(&receipt_path)?;
let re_serialized = serde_json::to_vec_pretty(&receipt)?;
if on_disk == re_serialized {
checks.push(VerifyCheck::pass(
"determinism",
"receipt.json round-trips identically (structural, NOT a signature)",
));
} else {
checks.push(VerifyCheck::warn(
"determinism",
"receipt.json does not byte-match after re-serialization",
));
}
checks.push(VerifyCheck::warn(
"receipt_body_binding",
"timeline/side-effects/narrative are NOT signed in this package — only the artifacts and Merkle root are cryptographically bound. For an authenticated record of the session, verify the actor-signed session.v1 record (or the published report).",
));
if !receipt.artifacts.is_empty() {
let version = receipt.merkle.merkle_version;
let mut tree = match crate::merkle::MerkleTree::with_version(version) {
Ok(t) => t,
Err(e) => {
checks.push(VerifyCheck::fail(
"merkle_root",
&format!("receipt declared unknown merkle_version: {e}"),
));
return Ok(finish_package_checks(checks, &receipt));
}
};
for art in &receipt.artifacts {
tree.append(&art.artifact_id);
}
let root_bytes = tree.root();
let recomputed_root = root_bytes.map(|r| format!("mroot_{}", hex::encode(r)));
let root_hex = root_bytes.map(hex::encode).unwrap_or_default();
if recomputed_root == receipt.merkle.root {
checks.push(VerifyCheck::pass(
"merkle_root",
"Merkle root matches recomputed value",
));
} else {
checks.push(VerifyCheck::fail(
"merkle_root",
&format!(
"Mismatch: on-disk {:?} vs recomputed {:?}",
receipt.merkle.root, recomputed_root
),
));
}
for proof_entry in &receipt.merkle.inclusion_proofs {
if proof_entry.proof.merkle_version != version {
checks.push(VerifyCheck::fail(
&format!("inclusion:{}", proof_entry.artifact_id),
&format!(
"proof merkle_version {} != receipt section v{}",
proof_entry.proof.merkle_version, version,
),
));
continue;
}
let verified = crate::merkle::MerkleTree::verify_proof(
version,
&root_hex,
&proof_entry.artifact_id,
&proof_entry.proof,
);
if verified {
checks.push(VerifyCheck::pass(
&format!("inclusion:{}", proof_entry.artifact_id),
"Inclusion proof valid",
));
} else {
checks.push(VerifyCheck::fail(
&format!("inclusion:{}", proof_entry.artifact_id),
"Inclusion proof failed verification",
));
}
}
} else {
checks.push(VerifyCheck::warn("merkle_root", "No artifacts to verify"));
}
verify_sealed_envelopes(pkg_dir, &receipt, trust, structural_only, &mut checks);
verify_receipt_binding(pkg_dir, &receipt, structural_only, &mut checks);
verify_session_window(pkg_dir, &receipt, &mut checks);
if receipt.merkle.leaf_count == receipt.artifacts.len() {
checks.push(VerifyCheck::pass(
"leaf_count",
"Leaf count matches artifact count",
));
} else {
checks.push(VerifyCheck::fail(
"leaf_count",
&format!(
"leaf_count {} != artifact count {}",
receipt.merkle.leaf_count,
receipt.artifacts.len()
),
));
}
let ordered = receipt.timeline.windows(2).all(|w| {
(&w[0].timestamp, w[0].sequence_no, &w[0].event_id)
<= (&w[1].timestamp, w[1].sequence_no, &w[1].event_id)
});
if ordered {
checks.push(VerifyCheck::pass(
"timeline_order",
"Timeline is correctly ordered",
));
} else {
checks.push(VerifyCheck::fail(
"timeline_order",
"Timeline entries are not in deterministic order",
));
}
if receipt.proofs.event_log_skipped > 0 {
checks.push(VerifyCheck::warn(
"event_log_completeness",
&format!(
"{} event(s) skipped during close (malformed lines in events.jsonl). \
Receipt is cryptographically valid but does not represent the full event stream. \
Inspect close-time stderr or the events.jsonl directly to investigate.",
receipt.proofs.event_log_skipped,
),
));
}
if receipt.proofs.reconcile_untracked_truncated > 0 {
checks.push(VerifyCheck::warn(
"reconcile_completeness",
&format!(
"untracked git reconcile exceeded cap {} (saw at least {}). \
Per-file synthetic events were skipped and the receipt is bounded, not complete for untracked files.",
receipt.proofs.reconcile_untracked_cap,
receipt.proofs.reconcile_untracked_truncated,
),
));
}
if receipt.proofs.reconcile_degraded {
checks.push(VerifyCheck::warn(
"reconcile_degraded",
"the git reconcile backstop was UNAVAILABLE at session close although git worked at start \
(.git removed, corrupt index, or git not on PATH). Files changed outside a captured \
AgentWroteFile event may be MISSING from this receipt's file ledger — treat the \
\"Files changed\" list as incomplete.",
));
}
let bundle = read_approvals_bundle(pkg_dir).unwrap_or_default();
add_approval_evidence_checks(&mut checks, &bundle, trust);
Ok(checks)
}
const SIGNER_KINDS: &[crate::trust::TrustRootKind] = &[
crate::trust::TrustRootKind::CertIssuer,
crate::trust::TrustRootKind::AgentCert,
crate::trust::TrustRootKind::SessionHost,
];
fn read_package_keys(pkg_dir: &Path) -> Option<PackageKeys> {
let raw = std::fs::read(pkg_dir.join(KEYS_FILE)).ok()?;
serde_json::from_slice(&raw).ok()
}
fn package_verifying_keys(
pkg_dir: &Path,
) -> std::collections::BTreeMap<String, ed25519_dalek::VerifyingKey> {
let mut keys = std::collections::BTreeMap::new();
if let Some(pk) = read_package_keys(pkg_dir) {
for (id, encoded) in pk.keys {
if let Ok(vk) = crate::trust::decode_ed25519_pubkey(&encoded) {
keys.insert(id, vk);
}
}
}
keys
}
fn verify_receipt_binding(
pkg_dir: &Path,
receipt: &SessionReceipt,
structural_only: bool,
checks: &mut Vec<VerifyCheck>,
) {
use sha2::{Digest, Sha256};
let path = pkg_dir.join(RECORD_FILE);
let raw = match std::fs::read(&path) {
Ok(b) => b,
Err(_) => {
checks.push(VerifyCheck::warn(
"receipt_binding",
"the package carries no close record (built before 0.31.4), so the sealed set is not under a signature: an artifact could be added to the list and the tree recomputed without any per-artifact row failing",
));
return;
}
};
if structural_only {
checks.push(VerifyCheck::warn(
"receipt_binding",
"close record present but not checked under --structural",
));
return;
}
let envelope = match crate::attestation::Envelope::from_json(&raw) {
Ok(e) => e,
Err(e) => {
checks.push(VerifyCheck::fail(
"receipt_binding",
&format!("record.json does not parse as a DSSE envelope: {e}"),
));
return;
}
};
let Some(sig) = envelope.signatures.first() else {
checks.push(VerifyCheck::fail(
"receipt_binding",
"record.json carries no signature",
));
return;
};
let keys = package_verifying_keys(pkg_dir);
let Some(vk) = keys.get(&sig.keyid) else {
checks.push(VerifyCheck::fail(
"receipt_binding",
&format!(
"record.json is signed by {}, a key the package does not carry",
sig.keyid
),
));
return;
};
if let Err(e) = crate::attestation::verify_with_key(&envelope, &sig.keyid, *vk) {
checks.push(VerifyCheck::fail(
"receipt_binding",
&format!("record.json signature invalid for key {}: {e}", sig.keyid),
));
return;
}
let payload: serde_json::Value = match envelope
.payload_bytes()
.ok()
.and_then(|b| serde_json::from_slice(&b).ok())
{
Some(v) => v,
None => {
checks.push(VerifyCheck::fail(
"receipt_binding",
"record.json payload is not JSON",
));
return;
}
};
let signed_digest = payload
.get("payload")
.and_then(|p| p.get("receipt_digest"))
.and_then(|d| d.as_str())
.unwrap_or("");
let signed_session = payload
.get("payload")
.and_then(|p| p.get("session_id"))
.and_then(|d| d.as_str())
.unwrap_or("");
let receipt_bytes = std::fs::read(pkg_dir.join(RECEIPT_FILE)).unwrap_or_default();
let actual = format!("sha256:{}", hex::encode(Sha256::digest(&receipt_bytes)));
if signed_session != receipt.session.id {
checks.push(VerifyCheck::fail(
"receipt_binding",
&format!(
"the close record names session {} but this receipt is {}",
signed_session, receipt.session.id
),
));
} else if signed_digest != actual {
checks.push(VerifyCheck::fail(
"receipt_binding",
&format!(
"the producer signed receipt digest {} at close but receipt.json now digests to {}: the sealed set was rewritten after it was signed",
signed_digest, actual
),
));
} else {
checks.push(VerifyCheck::pass(
"receipt_binding",
&format!(
"close record signed by {} binds receipt.json ({}) and names this session",
sig.keyid, actual
),
));
}
}
fn verify_session_window(pkg_dir: &Path, receipt: &SessionReceipt, checks: &mut Vec<VerifyCheck>) {
use crate::statements::invitation::parse_rfc3339_to_unix;
const SKEW: u64 = 120;
let Some(started) = parse_rfc3339_to_unix(&receipt.session.started_at) else {
return;
};
let ended = receipt
.session
.ended_at
.as_deref()
.and_then(parse_rfc3339_to_unix);
let art_dir = pkg_dir.join(ARTIFACTS_DIR);
let mut outside: Vec<String> = Vec::new();
let mut seen = 0usize;
for entry in &receipt.artifacts {
let path = art_dir.join(format!("{}.json", sanitize_filename(&entry.artifact_id)));
let Ok(raw) = std::fs::read(&path) else {
continue;
};
let Ok(env) = crate::attestation::Envelope::from_json(&raw) else {
continue;
};
let Some(ts) = env
.payload_bytes()
.ok()
.and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
.and_then(|v| {
v.get("timestamp")
.and_then(|t| t.as_str())
.map(str::to_string)
})
else {
continue;
};
let Some(t) = parse_rfc3339_to_unix(&ts) else {
continue;
};
seen += 1;
let before = t + SKEW < started;
let after = ended.map(|e| t > e + SKEW).unwrap_or(false);
if before || after {
outside.push(format!("{} ({})", entry.artifact_id, ts));
}
}
if seen == 0 {
return;
}
if outside.is_empty() {
checks.push(VerifyCheck::pass(
"session_window",
&format!("{seen} sealed artifact(s) were signed inside the session's window"),
));
} else {
checks.push(VerifyCheck::warn(
"session_window",
&format!(
"{} sealed artifact(s) were signed outside the session's window ({} to {}): {}",
outside.len(),
receipt.session.started_at,
receipt
.session
.ended_at
.clone()
.unwrap_or_else(|| "open".into()),
outside.join(", ")
),
));
}
}
fn verify_sealed_envelopes(
pkg_dir: &Path,
receipt: &SessionReceipt,
trust: &crate::trust::TrustRootStore,
structural_only: bool,
checks: &mut Vec<VerifyCheck>,
) {
use std::collections::{BTreeMap, BTreeSet};
if receipt.artifacts.is_empty() {
return;
}
let art_dir = pkg_dir.join(ARTIFACTS_DIR);
let any_envelope = receipt.artifacts.iter().any(|a| {
art_dir
.join(format!("{}.json", sanitize_filename(&a.artifact_id)))
.exists()
});
if !any_envelope {
let detail = "the package carries no artifact envelopes (built before 0.31.2), so nothing here is signature-checked: the sealed set is structurally consistent and nothing more. Verify the artifacts from the producer's store, a bundle, or the hub with `treeship verify <id>`, or read structure only with --structural (verdict: structural-pass)";
checks.push(if structural_only {
VerifyCheck::warn("envelopes", detail)
} else {
VerifyCheck::fail("envelopes", detail)
});
return;
}
let mut keys: BTreeMap<String, ed25519_dalek::VerifyingKey> = BTreeMap::new();
match read_package_keys(pkg_dir) {
Some(pk) => {
for (id, encoded) in pk.keys {
match crate::trust::decode_ed25519_pubkey(&encoded) {
Ok(vk) => {
keys.insert(id, vk);
}
Err(e) => checks.push(VerifyCheck::fail(
"keys.json",
&format!("key {id} is not a valid ed25519 public key: {e}"),
)),
}
}
}
None => checks.push(VerifyCheck::fail(
"keys.json",
"package has artifact envelopes but no keys.json naming the signing keys",
)),
}
let mut parents: Vec<(String, Option<String>)> = Vec::new();
let mut signers: BTreeSet<String> = BTreeSet::new();
let mut ok_count = 0usize;
for entry in &receipt.artifacts {
let id = &entry.artifact_id;
let name = format!("signature:{id}");
let path = art_dir.join(format!("{}.json", sanitize_filename(id)));
let raw = match std::fs::read(&path) {
Ok(b) => b,
Err(_) => {
checks.push(VerifyCheck::fail(
&name,
"sealed in the Merkle tree but its signed envelope is not in the package",
));
parents.push((id.clone(), None));
continue;
}
};
let envelope = match crate::attestation::Envelope::from_json(&raw) {
Ok(e) => e,
Err(e) => {
checks.push(VerifyCheck::fail(
&name,
&format!("envelope does not parse: {e}"),
));
parents.push((id.clone(), None));
continue;
}
};
let Some(sig) = envelope.signatures.first() else {
checks.push(VerifyCheck::fail(&name, "envelope carries no signature"));
parents.push((id.clone(), None));
continue;
};
let Some(vk) = keys.get(&sig.keyid) else {
checks.push(VerifyCheck::fail(
&name,
&format!("signed by {}, a key the package does not carry", sig.keyid),
));
parents.push((id.clone(), None));
continue;
};
match crate::attestation::verify_with_key(&envelope, &sig.keyid, *vk) {
Ok(res) => {
if res.artifact_id != *id {
checks.push(VerifyCheck::fail(
&name,
&format!(
"the signed bytes re-derive to {}, not the sealed id",
res.artifact_id
),
));
} else if entry
.digest
.as_deref()
.map(|d| d != res.digest)
.unwrap_or(false)
{
checks.push(VerifyCheck::fail(
&name,
&format!(
"receipt lists digest {} but the signed bytes digest to {}",
entry.digest.clone().unwrap_or_default(),
res.digest
),
));
} else {
ok_count += 1;
signers.insert(sig.keyid.clone());
checks.push(VerifyCheck::pass(&name, &format!("Ed25519 signature by {} verifies; id and digest re-derived from the signed bytes", sig.keyid)));
}
}
Err(e) => checks.push(VerifyCheck::fail(
&name,
&format!("invalid signature for key {}: {e}", sig.keyid),
)),
}
let parent = envelope
.payload_bytes()
.ok()
.and_then(|b| serde_json::from_slice::<serde_json::Value>(&b).ok())
.and_then(|v| {
v.get("parentId")
.and_then(|p| p.as_str())
.map(str::to_string)
});
parents.push((id.clone(), parent));
}
let chained: Vec<(usize, &ArtifactEntry)> = receipt
.artifacts
.iter()
.enumerate()
.filter(|(_, a)| !a.unchained)
.collect();
let mut broken: Vec<String> = Vec::new();
for w in chained.windows(2) {
let (i_prev, prev) = w[0];
let (i_cur, cur) = w[1];
let _ = (i_prev, i_cur);
let signed_parent = parents
.iter()
.find(|(id, _)| *id == cur.artifact_id)
.and_then(|(_, p)| p.clone());
match signed_parent {
Some(p) if p == prev.artifact_id => {}
Some(p) => broken.push(format!(
"{} names parent {} but follows {}",
cur.artifact_id, p, prev.artifact_id
)),
None => broken.push(format!("{} has no readable parentId", cur.artifact_id)),
}
}
if chained.len() >= 2 {
if broken.is_empty() {
checks.push(VerifyCheck::pass("chain_linkage", &format!("{} chained artifacts each name the previous one as parent, inside the signature", chained.len())));
} else {
checks.push(VerifyCheck::fail("chain_linkage", &broken.join("; ")));
}
}
let unchained: Vec<&str> = receipt
.artifacts
.iter()
.filter(|a| a.unchained)
.map(|a| a.artifact_id.as_str())
.collect();
if !unchained.is_empty() {
checks.push(VerifyCheck::warn("chain_completeness", &format!("{} sealed artifact(s) were signed during the session but never chained onto it ({}); signed and sealed, but their order relative to the chain is the signer's claim only", unchained.len(), unchained.join(", "))));
}
if ok_count > 0 {
let unpinned: Vec<String> = signers
.iter()
.filter(|k| {
let vk = keys.get(*k).expect("signer seen in keys");
!SIGNER_KINDS.iter().any(|kind| trust.contains(vk, *kind))
})
.cloned()
.collect();
if unpinned.is_empty() {
checks.push(VerifyCheck::pass(
"signer_trust",
&format!(
"all {} signing key(s) are pinned trust roots",
signers.len()
),
));
} else {
let pins: Vec<String> = unpinned
.iter()
.map(|k| {
let vk = keys.get(k).expect("key");
format!(
"treeship trust add {k} {} --kind cert_issuer",
crate::trust::encode_ed25519_pubkey(vk)
)
})
.collect();
checks.push(VerifyCheck::warn("signer_trust", &format!("signature(s) verify for the key(s) the package names, but {} of them are not pinned trust roots here: {}. Pin what you have decided to trust: {}", unpinned.len(), unpinned.join(", "), pins.join("; "))));
}
}
}
fn finish_package_checks(
mut checks: Vec<VerifyCheck>,
receipt: &SessionReceipt,
) -> Vec<VerifyCheck> {
if receipt.merkle.leaf_count == receipt.artifacts.len() {
checks.push(VerifyCheck::pass(
"leaf_count",
"Leaf count matches artifact count",
));
} else {
checks.push(VerifyCheck::fail(
"leaf_count",
&format!(
"leaf_count {} != artifact count {}",
receipt.merkle.leaf_count,
receipt.artifacts.len(),
),
));
}
let ordered = receipt.timeline.windows(2).all(|w| {
(&w[0].timestamp, w[0].sequence_no, &w[0].event_id)
<= (&w[1].timestamp, w[1].sequence_no, &w[1].event_id)
});
if ordered {
checks.push(VerifyCheck::pass(
"timeline_order",
"Timeline is correctly ordered",
));
} else {
checks.push(VerifyCheck::fail(
"timeline_order",
"Timeline entries are not in deterministic order",
));
}
checks
}
pub(crate) fn add_approval_evidence_checks(
checks: &mut Vec<VerifyCheck>,
bundle: &ApprovalsBundle,
trust: &crate::trust::TrustRootStore,
) {
if bundle.uses.is_empty() && bundle.checkpoints.is_empty() {
return;
}
use std::collections::HashMap;
let mut by_nonce: HashMap<(String, String), Vec<&ApprovalUse>> = HashMap::new();
let mut by_use_id: HashMap<&str, Vec<&ApprovalUse>> = HashMap::new();
for u in &bundle.uses {
by_nonce
.entry((u.grant_id.clone(), u.nonce_digest.clone()))
.or_default()
.push(u);
by_use_id.entry(&u.use_id).or_default().push(u);
}
let over_max: Vec<((String, String), Vec<&ApprovalUse>, u32)> = by_nonce
.iter()
.filter_map(|(key, uses)| {
let max = uses.iter().filter_map(|u| u.max_uses).next()?;
if (uses.len() as u32) > max {
Some((key.clone(), uses.to_vec(), max))
} else {
None
}
})
.collect();
let dup_use_ids: Vec<(&&str, &Vec<&ApprovalUse>)> =
by_use_id.iter().filter(|(_, v)| v.len() > 1).collect();
if over_max.is_empty() && dup_use_ids.is_empty() {
checks.push(VerifyCheck::pass(
"replay-package-local",
&format!(
"no duplicate approval use inside package ({} uses scanned)",
bundle.uses.len()
),
));
} else {
let mut detail = String::from("package-local replay violation:");
for ((grant_id, _nd), uses, max) in &over_max {
detail.push_str(&format!(
" grant {grant_id} consumed {} times in this package (max_uses={max});",
uses.len(),
));
}
for (uid, uses) in &dup_use_ids {
detail.push_str(&format!(" use_id {uid} appears {} times;", uses.len()));
}
checks.push(VerifyCheck::fail("replay-package-local", &detail));
}
if !bundle.checkpoints.is_empty() {
let mut tampered = Vec::new();
for cp in &bundle.checkpoints {
let recomputed = journal_checkpoint_record_digest(cp);
if recomputed != cp.record_digest {
tampered.push((
cp.checkpoint_id.clone(),
cp.record_digest.clone(),
recomputed,
));
}
}
if tampered.is_empty() {
checks.push(VerifyCheck::pass(
"replay-included-checkpoint",
&format!(
"{} included journal checkpoint(s) verify offline",
bundle.checkpoints.len()
),
));
} else {
let detail = tampered
.iter()
.map(|(id, expected, actual)| {
format!("checkpoint {id} tampered (stored {expected}, recomputed {actual})")
})
.collect::<Vec<_>>()
.join("; ");
checks.push(VerifyCheck::fail("replay-included-checkpoint", &detail));
}
}
let mut tampered_uses = Vec::new();
for u in &bundle.uses {
let recomputed = approval_use_record_digest(u);
if recomputed != u.record_digest {
tampered_uses.push((u.use_id.clone(), u.record_digest.clone(), recomputed));
}
}
if !bundle.uses.is_empty() {
if tampered_uses.is_empty() {
checks.push(VerifyCheck::pass(
"approval-use-record-digest",
&format!("{} use record(s) recompute identically", bundle.uses.len()),
));
} else {
let detail = tampered_uses
.iter()
.map(|(id, expected, actual)| {
format!("use {id} tampered (stored {expected}, recomputed {actual})")
})
.collect::<Vec<_>>()
.join("; ");
checks.push(VerifyCheck::fail("approval-use-record-digest", &detail));
}
}
if !bundle.uses.is_empty() {
use crate::attestation::envelope::Envelope;
use crate::attestation::{artifact_id_from_pae, pae};
use crate::statements::{nonce_digest, ApprovalStatement};
let mut grant_nonce_digest: std::collections::HashMap<String, String> =
std::collections::HashMap::new();
let mut tampered_grants: Vec<String> = Vec::new();
for (grant_id, env_bytes) in &bundle.grants {
let env = match Envelope::from_json(env_bytes) {
Ok(e) => e,
Err(_) => {
tampered_grants.push(format!("grant {grant_id} envelope unparseable"));
continue;
}
};
let derived = match env.payload_bytes() {
Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
Err(_) => {
tampered_grants.push(format!("grant {grant_id} envelope payload undecodable"));
continue;
}
};
if &derived != grant_id {
tampered_grants.push(format!(
"grant {grant_id} envelope content derives to {derived} -- envelope substituted or tampered",
));
continue;
}
let approval: ApprovalStatement = match env.unmarshal_statement() {
Ok(a) => a,
Err(_) => {
tampered_grants
.push(format!("grant {grant_id} payload not an ApprovalStatement"));
continue;
}
};
grant_nonce_digest.insert(grant_id.clone(), nonce_digest(&approval.nonce));
}
let mut mismatches: Vec<String> = Vec::new();
let mut missing_grants: Vec<String> = Vec::new();
for u in &bundle.uses {
match grant_nonce_digest.get(&u.grant_id) {
Some(expected) => {
if expected != &u.nonce_digest {
mismatches.push(format!(
"use {} claims nonce_digest {} but grant {} signed nonce hashes to {}",
u.use_id, u.nonce_digest, u.grant_id, expected,
));
}
}
None => {
missing_grants.push(format!(
"use {} references grant {} but no usable grant envelope is in the package",
u.use_id, u.grant_id,
));
}
}
}
if mismatches.is_empty() && missing_grants.is_empty() && tampered_grants.is_empty() {
checks.push(VerifyCheck::pass(
"approval-use-nonce-binding",
&format!(
"{} use record(s) bind to content-addressed grant signed nonces",
bundle.uses.len(),
),
));
} else {
let mut parts: Vec<String> = Vec::new();
if !tampered_grants.is_empty() {
parts.push(tampered_grants.join("; "));
}
if !mismatches.is_empty() {
parts.push(mismatches.join("; "));
}
if !missing_grants.is_empty() {
parts.push(missing_grants.join("; "));
}
checks.push(VerifyCheck::fail(
"approval-use-nonce-binding",
&parts.join("; "),
));
}
}
if !bundle.uses.is_empty() {
use crate::attestation::envelope::Envelope;
use crate::attestation::{artifact_id_from_pae, pae};
use crate::statements::{nonce_digest, ActionStatement};
if bundle.action_envelopes.is_empty() {
checks.push(VerifyCheck::warn(
"approval-use-action-binding",
"no action envelopes embedded -- action↔use binding not asserted by package (pre-v0.9.10)",
));
} else {
let use_ids: std::collections::HashSet<&str> =
bundle.uses.iter().map(|u| u.use_id.as_str()).collect();
let mut violations: Vec<String> = Vec::new();
let mut bound_count = 0usize;
for (artifact_id, env_bytes) in &bundle.action_envelopes {
let env = match Envelope::from_json(env_bytes) {
Ok(e) => e,
Err(_) => {
violations.push(format!("action {artifact_id} envelope unparseable"));
continue;
}
};
let derived = match env.payload_bytes() {
Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
Err(_) => {
violations
.push(format!("action {artifact_id} envelope payload undecodable"));
continue;
}
};
if &derived != artifact_id {
violations.push(format!(
"action {artifact_id} envelope content derives to {derived} -- envelope substituted or tampered",
));
continue;
}
let action: ActionStatement = match env.unmarshal_statement() {
Ok(a) => a,
Err(_) => {
violations.push(format!("action {artifact_id} not an ActionStatement"));
continue;
}
};
let raw_nonce = match action.approval_nonce.as_deref() {
Some(n) => n,
None => continue,
};
let claimed_use_id = action
.meta
.as_ref()
.and_then(|m| m.get("approval_use_id"))
.and_then(|v| v.as_str());
let Some(claimed_use_id) = claimed_use_id else {
violations.push(format!(
"action {artifact_id} consumed an approval but its meta has no approval_use_id"
));
continue;
};
if !use_ids.contains(claimed_use_id) {
violations.push(format!(
"action {artifact_id} claims approval_use_id={} but no such use is embedded",
claimed_use_id,
));
continue;
}
let expected = nonce_digest(raw_nonce);
let matched_use = bundle.uses.iter().find(|u| u.use_id == claimed_use_id);
if let Some(u) = matched_use {
if u.nonce_digest != expected {
violations.push(format!(
"action {artifact_id} approval_nonce hashes to {} but use {} stores nonce_digest {}",
expected, claimed_use_id, u.nonce_digest,
));
continue;
}
}
bound_count += 1;
}
if violations.is_empty() {
checks.push(VerifyCheck::pass(
"approval-use-action-binding",
&format!(
"{bound_count} consuming action(s) bind cleanly to content-addressed envelope(s)",
),
));
} else {
checks.push(VerifyCheck::fail(
"approval-use-action-binding",
&violations.join("; "),
));
}
}
}
if !bundle.uses.is_empty() || !bundle.checkpoints.is_empty() {
use std::collections::{HashMap, HashSet};
struct Node<'a> {
label: String,
digest: &'a str,
prev: &'a str,
}
let mut nodes: Vec<Node> = Vec::new();
for u in &bundle.uses {
nodes.push(Node {
label: format!("use {}", u.use_id),
digest: u.record_digest.as_str(),
prev: u.previous_record_digest.as_str(),
});
}
for cp in &bundle.checkpoints {
nodes.push(Node {
label: format!("checkpoint {}", cp.checkpoint_id),
digest: cp.record_digest.as_str(),
prev: cp.previous_record_digest.as_str(),
});
}
let owned: HashSet<&str> = std::iter::once("")
.chain(nodes.iter().map(|n| n.digest))
.collect();
let mut violations: Vec<String> = Vec::new();
for n in &nodes {
if !owned.contains(n.prev) {
violations.push(format!(
"{} previous_record_digest {} not anchored in package",
n.label, n.prev,
));
}
}
let genesis: Vec<&Node> = nodes.iter().filter(|n| n.prev.is_empty()).collect();
if genesis.len() > 1 {
violations.push(format!(
"{} records claim previous_record_digest='' (genesis): {}",
genesis.len(),
genesis
.iter()
.map(|n| n.label.clone())
.collect::<Vec<_>>()
.join(", "),
));
}
let mut by_prev: HashMap<&str, Vec<&Node>> = HashMap::new();
for n in &nodes {
by_prev.entry(n.prev).or_default().push(n);
}
for (prev, group) in &by_prev {
if group.len() > 1 && !prev.is_empty() {
violations.push(format!(
"fork: {} records share previous_record_digest {}: {}",
group.len(),
prev,
group
.iter()
.map(|n| n.label.clone())
.collect::<Vec<_>>()
.join(", "),
));
}
}
if violations.is_empty() {
let by_digest: HashMap<&str, &Node> = nodes.iter().map(|n| (n.digest, n)).collect();
let next_of: HashMap<&str, &Node> = nodes
.iter()
.filter(|n| !n.prev.is_empty())
.map(|n| (n.prev, n))
.collect();
let start = genesis.first().copied();
let mut visited: HashSet<&str> = HashSet::new();
let mut current = start;
while let Some(node) = current {
if !visited.insert(node.digest) {
violations.push(format!(
"cycle detected at {} (record_digest {})",
node.label, node.digest,
));
break;
}
current = next_of.get(node.digest).copied();
}
if violations.is_empty() && visited.len() != nodes.len() {
let unreached: Vec<String> = nodes
.iter()
.filter(|n| !visited.contains(n.digest))
.map(|n| n.label.clone())
.collect();
if !unreached.is_empty() {
violations.push(format!(
"disconnected subchain: {} record(s) not reachable from genesis: {}",
unreached.len(),
unreached.join(", "),
));
}
}
let _ = by_digest; }
if violations.is_empty() {
checks.push(VerifyCheck::pass(
"approval-use-chain-continuity",
&format!(
"{} record(s) form a single connected linked list from one genesis with no cycles or forks",
nodes.len(),
),
));
} else {
checks.push(VerifyCheck::fail(
"approval-use-chain-continuity",
&violations.join("; "),
));
}
}
let hub_checkpoints: Vec<&JournalCheckpoint> = bundle
.checkpoints
.iter()
.filter(|cp| cp.checkpoint_kind == crate::statements::CheckpointKind::HubOrg)
.collect();
if !hub_checkpoints.is_empty() {
let mut all_ok = true;
let mut details: Vec<String> = Vec::new();
let mut have_valid_signature = false;
let mut security_fatal = false;
for cp in &hub_checkpoints {
match crate::statements::verify_hub_checkpoint_signature(cp, trust) {
crate::statements::HubCheckpointVerification::Valid => {
have_valid_signature = true;
let covered: std::collections::HashSet<&String> =
cp.covered_use_ids.iter().collect();
let missing: Vec<String> = bundle
.uses
.iter()
.filter(|u| !covered.contains(&u.use_id))
.map(|u| u.use_id.clone())
.collect();
if missing.is_empty() {
details.push(format!(
"{} signed by {} verifies; covers {} use(s)",
cp.checkpoint_id,
cp.hub_id,
cp.covered_use_ids.len(),
));
} else {
all_ok = false;
details.push(format!(
"{} verifies but does not cover {} use(s): {}",
cp.checkpoint_id,
missing.len(),
missing.join(", "),
));
}
}
crate::statements::HubCheckpointVerification::MissingFields(field) => {
all_ok = false;
details.push(format!(
"{} declares kind=hub-org but field `{}` is missing",
cp.checkpoint_id, field,
));
}
crate::statements::HubCheckpointVerification::Tampered => {
all_ok = false;
security_fatal = true;
details.push(format!(
"{} hub signature failed verification (tampered or wrong key)",
cp.checkpoint_id,
));
}
crate::statements::HubCheckpointVerification::NotHubKind => {
all_ok = false;
security_fatal = true;
details.push(format!(
"{} kind toggled out of hub-org during verify",
cp.checkpoint_id,
));
}
crate::statements::HubCheckpointVerification::UntrustedIssuer => {
all_ok = false;
security_fatal = true;
details.push(format!(
"{} hub_public_key is not a trusted root (configure via `treeship trust add`)",
cp.checkpoint_id,
));
}
}
}
if all_ok && have_valid_signature {
checks.push(VerifyCheck::pass("replay-hub-org", &details.join("; ")));
} else if security_fatal {
checks.push(VerifyCheck::fail("replay-hub-org", &details.join("; ")));
} else {
checks.push(VerifyCheck::warn("replay-hub-org", &details.join("; ")));
}
}
let _ = ReplayCheckLevel::HubOrg;
let _ = approval_revocation_record_digest as fn(&ApprovalRevocation) -> String;
let _ = ReplayCheck::not_performed;
}
#[derive(Debug, Clone)]
pub struct VerifyCheck {
pub name: String,
pub status: VerifyStatus,
pub detail: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerifyStatus {
Pass,
Fail,
Warn,
}
impl VerifyCheck {
pub fn pass(name: &str, detail: &str) -> Self {
Self {
name: name.into(),
status: VerifyStatus::Pass,
detail: detail.into(),
}
}
pub fn fail(name: &str, detail: &str) -> Self {
Self {
name: name.into(),
status: VerifyStatus::Fail,
detail: detail.into(),
}
}
pub fn warn(name: &str, detail: &str) -> Self {
Self {
name: name.into(),
status: VerifyStatus::Warn,
detail: detail.into(),
}
}
}
impl VerifyCheck {
pub fn passed(&self) -> bool {
self.status == VerifyStatus::Pass
}
}
const PREVIEW_TEMPLATE: &str = include_str!("preview_template.html");
const FRAUNCES_WOFF2: &[u8] = include_bytes!("../../assets/fonts/fraunces-latin-var.woff2");
fn fraunces_data_uri() -> String {
use base64::engine::general_purpose::STANDARD;
use base64::Engine;
format!("data:font/woff2;base64,{}", STANDARD.encode(FRAUNCES_WOFF2))
}
pub fn render_preview_html(receipt: &SessionReceipt) -> String {
render_preview_html_with_approvals(receipt, None)
}
pub fn preview_approvals_json(bundle: Option<&ApprovalsBundle>) -> serde_json::Value {
let Some(b) = bundle else {
return serde_json::Value::Null;
};
if b.grants.is_empty() && b.uses.is_empty() {
return serde_json::Value::Null;
}
let grants: Vec<serde_json::Value> = b
.grants
.iter()
.map(|(grant_id, bytes)| {
let parsed = crate::attestation::Envelope::from_json(bytes)
.ok()
.and_then(|env| env.unmarshal_statement::<ApprovalStatement>().ok());
match parsed {
Some(st) => serde_json::json!({
"grant_id": grant_id,
"parsed": true,
"approver": st.approver,
"description": st.description,
"timestamp": st.timestamp,
"expires_at": st.expires_at,
"scope": st.scope.as_ref().map(|sc| serde_json::json!({
"allowed_actors": sc.allowed_actors,
"allowed_actions": sc.allowed_actions,
"allowed_subjects": sc.allowed_subjects,
"max_uses": sc.max_actions,
"valid_until": sc.valid_until,
})),
}),
None => serde_json::json!({ "grant_id": grant_id, "parsed": false }),
}
})
.collect();
let uses: Vec<serde_json::Value> = b
.uses
.iter()
.map(|u| serde_json::to_value(u).unwrap_or(serde_json::Value::Null))
.collect();
serde_json::json!({ "grants": grants, "uses": uses })
}
pub fn render_preview_html_with_approvals(
receipt: &SessionReceipt,
bundle: Option<&ApprovalsBundle>,
) -> String {
let approvals_json = preview_approvals_json(bundle).to_string();
let safe_approvals = approvals_json.replace('<', r"\u003c");
let receipt_json = serde_json::to_string_pretty(receipt).unwrap_or_else(|_| "{}".to_string());
let safe_json = receipt_json.replace('<', r"\u003c");
PREVIEW_TEMPLATE
.replacen("__RECEIPT_JSON__", &safe_json, 1)
.replacen("__APPROVALS_JSON__", &safe_approvals, 1)
.replace("__FONT_FRAUNCES__", &fraunces_data_uri())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::session::event::*;
use crate::session::manifest::SessionManifest;
use crate::session::receipt::{ArtifactEntry, ReceiptComposer};
fn make_receipt() -> SessionReceipt {
let manifest = SessionManifest::new(
"ssn_pkg_test".into(),
"agent://test".into(),
"2026-04-05T08:00:00Z".into(),
1743843600000,
);
let mk = |seq: u64, inst: &str, et: EventType| -> SessionEvent {
SessionEvent {
session_id: "ssn_pkg_test".into(),
event_id: format!("evt_{:016x}", seq),
timestamp: format!("2026-04-05T08:{:02}:00Z", seq),
sequence_no: seq,
trace_id: "trace_1".into(),
span_id: format!("span_{seq}"),
parent_span_id: None,
agent_id: format!("agent://{inst}"),
agent_instance_id: inst.into(),
agent_name: inst.into(),
agent_role: None,
host_id: "host_1".into(),
tool_runtime_id: None,
event_type: et,
artifact_ref: None,
meta: None,
}
};
let events = vec![
mk(0, "root", EventType::SessionStarted),
mk(
1,
"root",
EventType::AgentStarted {
parent_agent_instance_id: None,
},
),
mk(
2,
"root",
EventType::AgentCalledTool {
tool_name: "read_file".into(),
tool_input_digest: None,
tool_output_digest: None,
duration_ms: Some(10),
},
),
mk(
3,
"root",
EventType::AgentCompleted {
termination_reason: None,
},
),
mk(
4,
"root",
EventType::SessionClosed {
summary: Some("Done".into()),
duration_ms: Some(60000),
},
),
];
let artifacts = vec![ArtifactEntry {
artifact_id: "art_001".into(),
payload_type: "action".into(),
digest: None,
signed_at: None,
unchained: false,
}];
ReceiptComposer::compose(&manifest, &events, artifacts)
}
#[test]
fn build_and_read_package() {
let receipt = make_receipt();
let tmp = std::env::temp_dir().join(format!("treeship-pkg-test-{}", rand::random::<u32>()));
let output = build_package(&receipt, &tmp).unwrap();
assert!(output.path.exists());
assert!(output.path.join("receipt.json").exists());
assert!(output.path.join("merkle.json").exists());
assert!(output.path.join("render.json").exists());
assert!(output.path.join("preview.html").exists());
assert!(output.receipt_digest.starts_with("sha256:"));
assert!(output.file_count >= 4);
let read_back = read_package(&output.path).unwrap();
assert_eq!(read_back.session.id, "ssn_pkg_test");
assert_eq!(read_back.type_, RECEIPT_TYPE);
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn verify_valid_package() {
let receipt = make_receipt();
let tmp =
std::env::temp_dir().join(format!("treeship-pkg-verify-{}", rand::random::<u32>()));
let output = build_package(&receipt, &tmp).unwrap();
let checks = verify_package_structural(&output.path).unwrap();
let fails: Vec<_> = checks
.iter()
.filter(|c| c.status == VerifyStatus::Fail)
.collect();
assert!(fails.is_empty(), "unexpected failures: {fails:?}");
let passes: Vec<_> = checks
.iter()
.filter(|c| c.status == VerifyStatus::Pass)
.collect();
assert!(
passes.len() >= 5,
"expected at least 5 pass checks, got {}",
passes.len()
);
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn verify_warns_when_reconcile_degraded() {
let mut receipt = make_receipt();
receipt.proofs.reconcile_degraded = true;
let tmp =
std::env::temp_dir().join(format!("treeship-pkg-degraded-{}", rand::random::<u32>()));
let output = build_package(&receipt, &tmp).unwrap();
let checks = verify_package_structural(&output.path).unwrap();
let warned = checks
.iter()
.any(|c| c.name == "reconcile_degraded" && c.status == VerifyStatus::Warn);
assert!(warned, "expected a reconcile_degraded WARN, got {checks:?}");
let fails: Vec<_> = checks
.iter()
.filter(|c| c.status == VerifyStatus::Fail)
.collect();
assert!(fails.is_empty(), "must not hard-fail: {fails:?}");
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn verify_no_degraded_warn_when_clean() {
let receipt = make_receipt();
let tmp =
std::env::temp_dir().join(format!("treeship-pkg-clean-{}", rand::random::<u32>()));
let output = build_package(&receipt, &tmp).unwrap();
let checks = verify_package_structural(&output.path).unwrap();
assert!(
!checks.iter().any(|c| c.name == "reconcile_degraded"),
"clean receipt must not emit a reconcile_degraded check"
);
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn verify_detects_missing_receipt() {
let tmp =
std::env::temp_dir().join(format!("treeship-pkg-empty-{}", rand::random::<u32>()));
std::fs::create_dir_all(&tmp).unwrap();
let err = read_package(&tmp);
assert!(err.is_err());
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn preview_html_renders_approval_evidence_from_the_bundle() {
use crate::attestation::sign::sign;
use crate::attestation::Ed25519Signer;
use crate::statements::ApprovalScope;
use crate::statements::TYPE_APPROVAL_USE;
let receipt = make_receipt();
assert_eq!(preview_approvals_json(None), serde_json::Value::Null);
assert_eq!(
preview_approvals_json(Some(&ApprovalsBundle::default())),
serde_json::Value::Null,
"an empty bundle is the same as none"
);
let signer = Ed25519Signer::generate("key_test_preview").unwrap();
let mut grant = ApprovalStatement::new("human://operator", "nonce-preview-0001");
grant.description = Some("apply change chg-0001: 3% clearance".into());
grant.scope = Some(ApprovalScope {
max_actions: Some(1),
valid_until: None,
allowed_actors: vec!["agent://merchant".into()],
allowed_actions: vec!["commerce.tool.apply_change.intent".into()],
allowed_subjects: vec!["change://chg-0001".into()],
extra: None,
});
let signed = sign("application/vnd.treeship.approval.v1+json", &grant, &signer).unwrap();
let grant_id = signed.artifact_id.to_string();
let grant_bytes = serde_json::to_vec(&signed.envelope).unwrap();
let use_record = ApprovalUse {
type_: TYPE_APPROVAL_USE.into(),
use_id: "use_preview_0001".into(),
grant_id: grant_id.clone(),
grant_digest: signed.digest.clone(),
nonce_digest: "sha256:00".into(),
actor: "agent://merchant".into(),
action: "commerce.tool.apply_change.intent".into(),
subject: "change://chg-0001".into(),
session_id: Some("ssn_pkg_test".into()),
action_artifact_id: Some("art_apply_intent".into()),
receipt_digest: None,
use_number: 1,
max_uses: Some(1),
idempotency_key: None,
created_at: "2026-09-07T10:45:49Z".into(),
expires_at: None,
previous_record_digest: String::new(),
record_digest: String::new(),
signature: None,
signature_alg: None,
signing_key_id: None,
};
let bundle = ApprovalsBundle {
grants: vec![
(grant_id.clone(), grant_bytes),
("art_garbage".into(), b"not json".to_vec()),
],
uses: vec![use_record],
..Default::default()
};
let summary = preview_approvals_json(Some(&bundle));
let grants = summary["grants"].as_array().unwrap();
assert_eq!(grants.len(), 2);
assert_eq!(grants[0]["parsed"], true);
assert_eq!(grants[0]["approver"], "human://operator");
assert_eq!(grants[0]["scope"]["max_uses"], 1);
assert_eq!(
grants[0]["scope"]["allowed_subjects"][0],
"change://chg-0001"
);
assert_eq!(grants[1]["parsed"], false);
assert_eq!(grants[1]["grant_id"], "art_garbage");
let uses = summary["uses"].as_array().unwrap();
assert_eq!(uses[0]["use_number"], 1);
assert_eq!(uses[0]["action_artifact_id"], "art_apply_intent");
let html = render_preview_html_with_approvals(&receipt, Some(&bundle));
assert!(html.contains("id=\"approvals-data\""));
assert!(html.contains("\"approver\":\"human://operator\""));
assert!(html.contains("\"subject\":\"change://chg-0001\""));
assert!(
!html.contains("__APPROVALS_JSON__"),
"placeholder must be substituted"
);
let plain = render_preview_html(&receipt);
assert!(plain.contains("type=\"application/json\">null</script>"));
}
#[test]
fn preview_html_contains_session_info() {
let receipt = make_receipt();
let html = render_preview_html(&receipt);
assert!(html.contains("ssn_pkg_test"));
assert!(html.contains("treeship.dev"));
assert!(html.contains("Timeline"));
assert!(
html.contains("'__RECEIPT'+'_JSON__'"),
"JS placeholder check was clobbered by the receipt substitution",
);
assert!(
!html.contains("application/json\">__RECEIPT_JSON__</script>"),
"data slot was not substituted with the receipt JSON",
);
assert_eq!(
html.matches("__RECEIPT_JSON__").count(),
0,
"no raw placeholder token should remain after substitution",
);
}
}