1use std::path::{Path, PathBuf};
13
14use serde::{Deserialize, Serialize};
15use sha2::{Digest, Sha256};
16
17use super::receipt::{SessionReceipt, RECEIPT_TYPE};
18use crate::statements::{
19 ApprovalRevocation, ApprovalUse, JournalCheckpoint,
20 ReplayCheck, ReplayCheckLevel,
21 approval_revocation_record_digest, approval_use_record_digest,
22 journal_checkpoint_record_digest,
23};
24
25#[derive(Debug)]
27pub enum PackageError {
28 Io(std::io::Error),
29 Json(serde_json::Error),
30 InvalidPackage(String),
31}
32
33impl std::fmt::Display for PackageError {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 match self {
36 Self::Io(e) => write!(f, "package io: {e}"),
37 Self::Json(e) => write!(f, "package json: {e}"),
38 Self::InvalidPackage(msg) => write!(f, "invalid package: {msg}"),
39 }
40 }
41}
42
43impl std::error::Error for PackageError {}
44impl From<std::io::Error> for PackageError {
45 fn from(e: std::io::Error) -> Self { Self::Io(e) }
46}
47impl From<serde_json::Error> for PackageError {
48 fn from(e: serde_json::Error) -> Self { Self::Json(e) }
49}
50
51const RECEIPT_FILE: &str = "receipt.json";
53const MERKLE_FILE: &str = "merkle.json";
54const RENDER_FILE: &str = "render.json";
55const ARTIFACTS_DIR: &str = "artifacts";
56const PROOFS_DIR: &str = "proofs";
57const PREVIEW_FILE: &str = "preview.html";
58
59const APPROVALS_DIR: &str = "approvals";
72const APPROVALS_GRANTS: &str = "approvals/grants";
73const APPROVALS_USES: &str = "approvals/uses";
74const APPROVALS_CHECKPOINTS:&str = "approvals/checkpoints";
75const APPROVALS_INDEX_FILE: &str = "approvals/index.json";
76
77#[derive(Debug, Clone, Default)]
87pub struct ApprovalsBundle {
88 pub grants: Vec<(String, Vec<u8>)>,
93 pub uses: Vec<ApprovalUse>,
97 pub checkpoints: Vec<JournalCheckpoint>,
100 pub revocations: Vec<ApprovalRevocation>,
105
106 pub action_envelopes: Vec<(String, Vec<u8>)>,
114}
115
116#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct ApprovalsIndex {
121 #[serde(rename = "type")]
123 pub type_: String,
124 pub schema_version: u32,
125 pub grants: Vec<String>,
128 pub uses: Vec<String>,
130 pub checkpoints: Vec<String>,
131 pub revocations: Vec<String>,
132}
133
134impl ApprovalsIndex {
135 pub fn type_string() -> &'static str { "treeship/approvals-index/v1" }
136}
137
138pub struct PackageOutput {
140 pub path: PathBuf,
142 pub receipt_digest: String,
144 pub merkle_root: Option<String>,
146 pub file_count: usize,
148}
149
150pub fn build_package(
160 receipt: &SessionReceipt,
161 output_dir: &Path,
162) -> Result<PackageOutput, PackageError> {
163 build_package_with_approvals(receipt, output_dir, None)
164}
165
166pub fn build_package_with_approvals(
170 receipt: &SessionReceipt,
171 output_dir: &Path,
172 bundle: Option<&ApprovalsBundle>,
173) -> Result<PackageOutput, PackageError> {
174 let session_id = &receipt.session.id;
175 let pkg_dir = output_dir.join(format!("{session_id}.treeship"));
176
177 std::fs::create_dir_all(&pkg_dir)?;
178 std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
179 std::fs::create_dir_all(pkg_dir.join(PROOFS_DIR))?;
180
181 let mut file_count = 0usize;
182
183 let receipt_bytes = serde_json::to_vec_pretty(receipt)?;
185 std::fs::write(pkg_dir.join(RECEIPT_FILE), &receipt_bytes)?;
186 file_count += 1;
187
188 let receipt_hash = Sha256::digest(&receipt_bytes);
189 let receipt_digest = format!("sha256:{}", hex::encode(receipt_hash));
190
191 let merkle_bytes = serde_json::to_vec_pretty(&receipt.merkle)?;
193 std::fs::write(pkg_dir.join(MERKLE_FILE), &merkle_bytes)?;
194 file_count += 1;
195
196 let render_bytes = serde_json::to_vec_pretty(&receipt.render)?;
198 std::fs::write(pkg_dir.join(RENDER_FILE), &render_bytes)?;
199 file_count += 1;
200
201 for proof_entry in &receipt.merkle.inclusion_proofs {
203 let proof_bytes = serde_json::to_vec_pretty(proof_entry)?;
204 let filename = format!("{}.proof.json", proof_entry.artifact_id);
205 std::fs::write(pkg_dir.join(PROOFS_DIR).join(filename), &proof_bytes)?;
206 file_count += 1;
207 }
208
209 if receipt.render.generate_preview {
211 let preview = render_preview_html(receipt);
212 std::fs::write(pkg_dir.join(PREVIEW_FILE), preview.as_bytes())?;
213 file_count += 1;
214 }
215
216 if let Some(b) = bundle {
221 if !b.grants.is_empty() || !b.uses.is_empty() || !b.checkpoints.is_empty() || !b.revocations.is_empty() || !b.action_envelopes.is_empty() {
222 std::fs::create_dir_all(pkg_dir.join(APPROVALS_GRANTS))?;
223 std::fs::create_dir_all(pkg_dir.join(APPROVALS_USES))?;
224 std::fs::create_dir_all(pkg_dir.join(APPROVALS_CHECKPOINTS))?;
225 std::fs::create_dir_all(pkg_dir.join(ARTIFACTS_DIR))?;
231 for (artifact_id, envelope_bytes) in &b.action_envelopes {
232 let safe = sanitize_filename(artifact_id);
233 std::fs::write(
234 pkg_dir.join(ARTIFACTS_DIR).join(format!("{safe}.json")),
235 envelope_bytes,
236 )?;
237 file_count += 1;
238 }
239
240 let mut grant_ids = Vec::with_capacity(b.grants.len());
241 for (grant_id, envelope_bytes) in &b.grants {
242 let safe = sanitize_filename(grant_id);
243 std::fs::write(
244 pkg_dir.join(APPROVALS_GRANTS).join(format!("{safe}.json")),
245 envelope_bytes,
246 )?;
247 grant_ids.push(grant_id.clone());
248 file_count += 1;
249 }
250
251 let mut use_ids = Vec::with_capacity(b.uses.len());
252 for u in &b.uses {
253 let safe = sanitize_filename(&u.use_id);
254 let bytes = serde_json::to_vec_pretty(u)?;
255 std::fs::write(
256 pkg_dir.join(APPROVALS_USES).join(format!("{safe}.json")),
257 &bytes,
258 )?;
259 use_ids.push(u.use_id.clone());
260 file_count += 1;
261 }
262
263 let mut checkpoint_ids = Vec::with_capacity(b.checkpoints.len());
264 for cp in &b.checkpoints {
265 let safe = sanitize_filename(&cp.checkpoint_id);
266 let bytes = serde_json::to_vec_pretty(cp)?;
267 std::fs::write(
268 pkg_dir.join(APPROVALS_CHECKPOINTS).join(format!("{safe}.json")),
269 &bytes,
270 )?;
271 checkpoint_ids.push(cp.checkpoint_id.clone());
272 file_count += 1;
273 }
274
275 let mut revocation_ids = Vec::with_capacity(b.revocations.len());
276 for rev in &b.revocations {
277 let safe = sanitize_filename(&rev.revocation_id);
278 let bytes = serde_json::to_vec_pretty(rev)?;
279 std::fs::write(
280 pkg_dir.join(APPROVALS_DIR).join(format!("revocations-{safe}.json")),
281 &bytes,
282 )?;
283 revocation_ids.push(rev.revocation_id.clone());
284 file_count += 1;
285 }
286
287 let index = ApprovalsIndex {
288 type_: ApprovalsIndex::type_string().into(),
289 schema_version: 1,
290 grants: grant_ids,
291 uses: use_ids,
292 checkpoints: checkpoint_ids,
293 revocations: revocation_ids,
294 };
295 let index_bytes = serde_json::to_vec_pretty(&index)?;
296 std::fs::write(pkg_dir.join(APPROVALS_INDEX_FILE), &index_bytes)?;
297 file_count += 1;
298 }
299 }
300
301 Ok(PackageOutput {
302 path: pkg_dir,
303 receipt_digest,
304 merkle_root: receipt.merkle.root.clone(),
305 file_count,
306 })
307}
308
309fn sanitize_filename(s: &str) -> String {
313 s.chars()
314 .map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' { c } else { '_' })
315 .collect()
316}
317
318pub fn read_approvals_bundle(pkg_dir: &Path) -> Result<ApprovalsBundle, PackageError> {
328 let approvals_dir = pkg_dir.join(APPROVALS_DIR);
329 if !approvals_dir.is_dir() {
330 return Ok(ApprovalsBundle::default());
331 }
332
333 let mut bundle = ApprovalsBundle::default();
334
335 let grants_dir = pkg_dir.join(APPROVALS_GRANTS);
338 if grants_dir.is_dir() {
339 for entry in std::fs::read_dir(&grants_dir)? {
340 let entry = entry?;
341 let path = entry.path();
342 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
343 let id = path.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_string();
344 let bytes = std::fs::read(&path)?;
345 bundle.grants.push((id, bytes));
346 }
347 }
348
349 let uses_dir = pkg_dir.join(APPROVALS_USES);
350 if uses_dir.is_dir() {
351 for entry in std::fs::read_dir(&uses_dir)? {
352 let entry = entry?;
353 let path = entry.path();
354 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
355 let bytes = std::fs::read(&path)?;
356 let u: ApprovalUse = serde_json::from_slice(&bytes)?;
357 bundle.uses.push(u);
358 }
359 }
360
361 let cps_dir = pkg_dir.join(APPROVALS_CHECKPOINTS);
362 if cps_dir.is_dir() {
363 for entry in std::fs::read_dir(&cps_dir)? {
364 let entry = entry?;
365 let path = entry.path();
366 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
367 let bytes = std::fs::read(&path)?;
368 let cp: JournalCheckpoint = serde_json::from_slice(&bytes)?;
369 bundle.checkpoints.push(cp);
370 }
371 }
372
373 let arts_dir = pkg_dir.join(ARTIFACTS_DIR);
380 if arts_dir.is_dir() {
381 for entry in std::fs::read_dir(&arts_dir)? {
382 let entry = entry?;
383 let path = entry.path();
384 if path.extension().and_then(|s| s.to_str()) != Some("json") { continue; }
385 let id = path.file_stem().and_then(|s| s.to_str()).unwrap_or("").to_string();
386 let bytes = std::fs::read(&path)?;
387 bundle.action_envelopes.push((id, bytes));
388 }
389 }
390
391 Ok(bundle)
392}
393
394pub fn read_package(pkg_dir: &Path) -> Result<SessionReceipt, PackageError> {
396 let receipt_path = pkg_dir.join(RECEIPT_FILE);
397 if !receipt_path.exists() {
398 return Err(PackageError::InvalidPackage(
399 format!("missing {RECEIPT_FILE} in {}", pkg_dir.display()),
400 ));
401 }
402 let bytes = std::fs::read(&receipt_path)?;
403 let receipt: SessionReceipt = serde_json::from_slice(&bytes)?;
404
405 if receipt.type_ != RECEIPT_TYPE {
406 return Err(PackageError::InvalidPackage(
407 format!("unexpected type: {} (expected {RECEIPT_TYPE})", receipt.type_),
408 ));
409 }
410
411 Ok(receipt)
412}
413
414pub fn verify_package(pkg_dir: &Path) -> Result<Vec<VerifyCheck>, PackageError> {
432 let trust = match crate::trust::TrustRootStore::open_default_or_empty() {
433 Ok(t) => t,
434 Err(e) => {
435 return Ok(vec![VerifyCheck::fail(
439 "trust-root",
440 &format!("trust store unreadable: {e}"),
441 )]);
442 }
443 };
444 verify_package_with_trust(pkg_dir, &trust)
445}
446
447pub fn verify_package_with_trust(
451 pkg_dir: &Path,
452 trust: &crate::trust::TrustRootStore,
453) -> Result<Vec<VerifyCheck>, PackageError> {
454 let mut checks = Vec::new();
455
456 let receipt = match read_package(pkg_dir) {
458 Ok(r) => {
459 checks.push(VerifyCheck::pass("receipt.json", "Parses as valid Session Receipt"));
460 r
461 }
462 Err(e) => {
463 checks.push(VerifyCheck::fail("receipt.json", &format!("Failed to parse: {e}")));
464 return Ok(checks);
465 }
466 };
467
468 if receipt.type_ == RECEIPT_TYPE {
470 checks.push(VerifyCheck::pass("type", "Correct receipt type"));
471 } else {
472 checks.push(VerifyCheck::fail("type", &format!("Expected {RECEIPT_TYPE}, got {}", receipt.type_)));
473 }
474
475 let receipt_path = pkg_dir.join(RECEIPT_FILE);
477 let on_disk = std::fs::read(&receipt_path)?;
478 let re_serialized = serde_json::to_vec_pretty(&receipt)?;
479 if on_disk == re_serialized {
480 checks.push(VerifyCheck::pass("determinism", "receipt.json round-trips identically"));
481 } else {
482 checks.push(VerifyCheck::warn("determinism", "receipt.json does not byte-match after re-serialization"));
484 }
485
486 if !receipt.artifacts.is_empty() {
488 let version = receipt.merkle.merkle_version;
495 let mut tree = match crate::merkle::MerkleTree::with_version(version) {
496 Ok(t) => t,
497 Err(e) => {
498 checks.push(VerifyCheck::fail(
499 "merkle_root",
500 &format!("receipt declared unknown merkle_version: {e}"),
501 ));
502 return Ok(finish_package_checks(checks, &receipt));
505 }
506 };
507 for art in &receipt.artifacts {
508 tree.append(&art.artifact_id);
509 }
510 let root_bytes = tree.root();
511 let recomputed_root = root_bytes
512 .map(|r| format!("mroot_{}", hex::encode(r)));
513 let root_hex = root_bytes
514 .map(|r| hex::encode(r))
515 .unwrap_or_default();
516
517 if recomputed_root == receipt.merkle.root {
518 checks.push(VerifyCheck::pass("merkle_root", "Merkle root matches recomputed value"));
519 } else {
520 checks.push(VerifyCheck::fail(
521 "merkle_root",
522 &format!(
523 "Mismatch: on-disk {:?} vs recomputed {:?}",
524 receipt.merkle.root, recomputed_root
525 ),
526 ));
527 }
528
529 for proof_entry in &receipt.merkle.inclusion_proofs {
534 if proof_entry.proof.merkle_version != version {
535 checks.push(VerifyCheck::fail(
536 &format!("inclusion:{}", proof_entry.artifact_id),
537 &format!(
538 "proof merkle_version {} != receipt section v{}",
539 proof_entry.proof.merkle_version, version,
540 ),
541 ));
542 continue;
543 }
544 let verified = crate::merkle::MerkleTree::verify_proof(
545 version,
546 &root_hex,
547 &proof_entry.artifact_id,
548 &proof_entry.proof,
549 );
550 if verified {
551 checks.push(VerifyCheck::pass(
552 &format!("inclusion:{}", proof_entry.artifact_id),
553 "Inclusion proof valid",
554 ));
555 } else {
556 checks.push(VerifyCheck::fail(
557 &format!("inclusion:{}", proof_entry.artifact_id),
558 "Inclusion proof failed verification",
559 ));
560 }
561 }
562 } else {
563 checks.push(VerifyCheck::warn("merkle_root", "No artifacts to verify"));
564 }
565
566 if receipt.merkle.leaf_count == receipt.artifacts.len() {
568 checks.push(VerifyCheck::pass("leaf_count", "Leaf count matches artifact count"));
569 } else {
570 checks.push(VerifyCheck::fail(
571 "leaf_count",
572 &format!("leaf_count {} != artifact count {}", receipt.merkle.leaf_count, receipt.artifacts.len()),
573 ));
574 }
575
576 let ordered = receipt.timeline.windows(2).all(|w| {
578 (&w[0].timestamp, w[0].sequence_no, &w[0].event_id)
579 <= (&w[1].timestamp, w[1].sequence_no, &w[1].event_id)
580 });
581 if ordered {
582 checks.push(VerifyCheck::pass("timeline_order", "Timeline is correctly ordered"));
583 } else {
584 checks.push(VerifyCheck::fail("timeline_order", "Timeline entries are not in deterministic order"));
585 }
586
587 if receipt.proofs.event_log_skipped > 0 {
595 checks.push(VerifyCheck::warn(
596 "event_log_completeness",
597 &format!(
598 "{} event(s) skipped during close (malformed lines in events.jsonl). \
599 Receipt is cryptographically valid but does not represent the full event stream. \
600 Inspect close-time stderr or the events.jsonl directly to investigate.",
601 receipt.proofs.event_log_skipped,
602 ),
603 ));
604 }
605
606 if receipt.proofs.reconcile_untracked_truncated > 0 {
607 checks.push(VerifyCheck::warn(
608 "reconcile_completeness",
609 &format!(
610 "untracked git reconcile exceeded cap {} (saw at least {}). \
611 Per-file synthetic events were skipped and the receipt is bounded, not complete for untracked files.",
612 receipt.proofs.reconcile_untracked_cap,
613 receipt.proofs.reconcile_untracked_truncated,
614 ),
615 ));
616 }
617
618 let bundle = read_approvals_bundle(pkg_dir).unwrap_or_default();
630 add_approval_evidence_checks(&mut checks, &bundle, trust);
631
632 Ok(checks)
633}
634
635fn finish_package_checks(
640 mut checks: Vec<VerifyCheck>,
641 receipt: &SessionReceipt,
642) -> Vec<VerifyCheck> {
643 if receipt.merkle.leaf_count == receipt.artifacts.len() {
644 checks.push(VerifyCheck::pass("leaf_count", "Leaf count matches artifact count"));
645 } else {
646 checks.push(VerifyCheck::fail(
647 "leaf_count",
648 &format!(
649 "leaf_count {} != artifact count {}",
650 receipt.merkle.leaf_count, receipt.artifacts.len(),
651 ),
652 ));
653 }
654
655 let ordered = receipt.timeline.windows(2).all(|w| {
656 (&w[0].timestamp, w[0].sequence_no, &w[0].event_id)
657 <= (&w[1].timestamp, w[1].sequence_no, &w[1].event_id)
658 });
659 if ordered {
660 checks.push(VerifyCheck::pass("timeline_order", "Timeline is correctly ordered"));
661 } else {
662 checks.push(VerifyCheck::fail("timeline_order", "Timeline entries are not in deterministic order"));
663 }
664
665 checks
666}
667
668pub(crate) fn add_approval_evidence_checks(
679 checks: &mut Vec<VerifyCheck>,
680 bundle: &ApprovalsBundle,
681 trust: &crate::trust::TrustRootStore,
682) {
683 if bundle.uses.is_empty() && bundle.checkpoints.is_empty() {
684 return;
688 }
689
690 use std::collections::HashMap;
699 let mut by_nonce: HashMap<(String, String), Vec<&ApprovalUse>> = HashMap::new();
700 let mut by_use_id: HashMap<&str, Vec<&ApprovalUse>> = HashMap::new();
701 for u in &bundle.uses {
702 by_nonce
703 .entry((u.grant_id.clone(), u.nonce_digest.clone()))
704 .or_default()
705 .push(u);
706 by_use_id.entry(&u.use_id).or_default().push(u);
707 }
708 let over_max: Vec<((String, String), Vec<&ApprovalUse>, u32)> = by_nonce
709 .iter()
710 .filter_map(|(key, uses)| {
711 let max = uses.iter().filter_map(|u| u.max_uses).next()?;
712 if (uses.len() as u32) > max {
713 Some((key.clone(), uses.iter().map(|u| *u).collect(), max))
714 } else {
715 None
716 }
717 })
718 .collect();
719 let dup_use_ids: Vec<(&&str, &Vec<&ApprovalUse>)> =
720 by_use_id.iter().filter(|(_, v)| v.len() > 1).collect();
721
722 if over_max.is_empty() && dup_use_ids.is_empty() {
723 checks.push(VerifyCheck::pass(
724 "replay-package-local",
725 &format!("no duplicate approval use inside package ({} uses scanned)", bundle.uses.len()),
726 ));
727 } else {
728 let mut detail = String::from("package-local replay violation:");
729 for ((grant_id, _nd), uses, max) in &over_max {
730 detail.push_str(&format!(
731 " grant {grant_id} consumed {} times in this package (max_uses={max});",
732 uses.len(),
733 ));
734 }
735 for (uid, uses) in &dup_use_ids {
736 detail.push_str(&format!(" use_id {uid} appears {} times;", uses.len()));
737 }
738 checks.push(VerifyCheck::fail("replay-package-local", &detail));
739 }
740
741 if !bundle.checkpoints.is_empty() {
746 let mut tampered = Vec::new();
747 for cp in &bundle.checkpoints {
748 let recomputed = journal_checkpoint_record_digest(cp);
749 if recomputed != cp.record_digest {
750 tampered.push((cp.checkpoint_id.clone(), cp.record_digest.clone(), recomputed));
751 }
752 }
753 if tampered.is_empty() {
754 checks.push(VerifyCheck::pass(
755 "replay-included-checkpoint",
756 &format!("{} included journal checkpoint(s) verify offline", bundle.checkpoints.len()),
757 ));
758 } else {
759 let detail = tampered.iter()
760 .map(|(id, expected, actual)| {
761 format!("checkpoint {id} tampered (stored {expected}, recomputed {actual})")
762 })
763 .collect::<Vec<_>>()
764 .join("; ");
765 checks.push(VerifyCheck::fail("replay-included-checkpoint", &detail));
766 }
767 }
768
769 let mut tampered_uses = Vec::new();
779 for u in &bundle.uses {
780 let recomputed = approval_use_record_digest(u);
781 if recomputed != u.record_digest {
782 tampered_uses.push((u.use_id.clone(), u.record_digest.clone(), recomputed));
783 }
784 }
785 if !bundle.uses.is_empty() {
786 if tampered_uses.is_empty() {
787 checks.push(VerifyCheck::pass(
788 "approval-use-record-digest",
789 &format!("{} use record(s) recompute identically", bundle.uses.len()),
790 ));
791 } else {
792 let detail = tampered_uses.iter()
793 .map(|(id, expected, actual)| {
794 format!("use {id} tampered (stored {expected}, recomputed {actual})")
795 })
796 .collect::<Vec<_>>()
797 .join("; ");
798 checks.push(VerifyCheck::fail("approval-use-record-digest", &detail));
799 }
800 }
801
802 if !bundle.uses.is_empty() {
820 use crate::attestation::envelope::Envelope;
821 use crate::attestation::{pae, artifact_id_from_pae};
822 use crate::statements::{nonce_digest, ApprovalStatement};
823 let mut grant_nonce_digest: std::collections::HashMap<String, String> = std::collections::HashMap::new();
824 let mut tampered_grants: Vec<String> = Vec::new();
825 for (grant_id, env_bytes) in &bundle.grants {
826 let env = match Envelope::from_json(env_bytes) {
827 Ok(e) => e,
828 Err(_) => {
829 tampered_grants.push(format!("grant {grant_id} envelope unparseable"));
830 continue;
831 }
832 };
833 let derived = match env.payload_bytes() {
838 Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
839 Err(_) => {
840 tampered_grants.push(format!("grant {grant_id} envelope payload undecodable"));
841 continue;
842 }
843 };
844 if &derived != grant_id {
845 tampered_grants.push(format!(
846 "grant {grant_id} envelope content derives to {derived} -- envelope substituted or tampered",
847 ));
848 continue;
849 }
850 let approval: ApprovalStatement = match env.unmarshal_statement() {
851 Ok(a) => a,
852 Err(_) => {
853 tampered_grants.push(format!("grant {grant_id} payload not an ApprovalStatement"));
854 continue;
855 }
856 };
857 grant_nonce_digest.insert(grant_id.clone(), nonce_digest(&approval.nonce));
858 }
859 let mut mismatches: Vec<String> = Vec::new();
860 let mut missing_grants: Vec<String> = Vec::new();
861 for u in &bundle.uses {
862 match grant_nonce_digest.get(&u.grant_id) {
863 Some(expected) => {
864 if expected != &u.nonce_digest {
865 mismatches.push(format!(
866 "use {} claims nonce_digest {} but grant {} signed nonce hashes to {}",
867 u.use_id, u.nonce_digest, u.grant_id, expected,
868 ));
869 }
870 }
871 None => {
872 missing_grants.push(format!(
873 "use {} references grant {} but no usable grant envelope is in the package",
874 u.use_id, u.grant_id,
875 ));
876 }
877 }
878 }
879 if mismatches.is_empty() && missing_grants.is_empty() && tampered_grants.is_empty() {
880 checks.push(VerifyCheck::pass(
881 "approval-use-nonce-binding",
882 &format!(
883 "{} use record(s) bind to content-addressed grant signed nonces",
884 bundle.uses.len(),
885 ),
886 ));
887 } else {
888 let mut parts: Vec<String> = Vec::new();
889 if !tampered_grants.is_empty() { parts.push(tampered_grants.join("; ")); }
890 if !mismatches.is_empty() { parts.push(mismatches.join("; ")); }
891 if !missing_grants.is_empty() { parts.push(missing_grants.join("; ")); }
892 checks.push(VerifyCheck::fail("approval-use-nonce-binding", &parts.join("; ")));
893 }
894 }
895
896 if !bundle.uses.is_empty() {
911 use crate::attestation::envelope::Envelope;
912 use crate::attestation::{pae, artifact_id_from_pae};
913 use crate::statements::{nonce_digest, ActionStatement};
914 if bundle.action_envelopes.is_empty() {
915 checks.push(VerifyCheck::warn(
916 "approval-use-action-binding",
917 "no action envelopes embedded -- action↔use binding not asserted by package (pre-v0.9.10)",
918 ));
919 } else {
920 let use_ids: std::collections::HashSet<&str> = bundle.uses.iter().map(|u| u.use_id.as_str()).collect();
921 let mut violations: Vec<String> = Vec::new();
922 let mut bound_count = 0usize;
923 for (artifact_id, env_bytes) in &bundle.action_envelopes {
924 let env = match Envelope::from_json(env_bytes) {
925 Ok(e) => e,
926 Err(_) => {
927 violations.push(format!("action {artifact_id} envelope unparseable"));
928 continue;
929 }
930 };
931 let derived = match env.payload_bytes() {
939 Ok(p) => artifact_id_from_pae(&pae(&env.payload_type, &p)),
940 Err(_) => {
941 violations.push(format!("action {artifact_id} envelope payload undecodable"));
942 continue;
943 }
944 };
945 if &derived != artifact_id {
946 violations.push(format!(
947 "action {artifact_id} envelope content derives to {derived} -- envelope substituted or tampered",
948 ));
949 continue;
950 }
951 let action: ActionStatement = match env.unmarshal_statement() {
952 Ok(a) => a,
953 Err(_) => {
954 violations.push(format!("action {artifact_id} not an ActionStatement"));
955 continue;
956 }
957 };
958 let raw_nonce = match action.approval_nonce.as_deref() {
959 Some(n) => n,
960 None => continue,
961 };
962 let claimed_use_id = action
963 .meta
964 .as_ref()
965 .and_then(|m| m.get("approval_use_id"))
966 .and_then(|v| v.as_str());
967 let Some(claimed_use_id) = claimed_use_id else {
968 violations.push(format!(
969 "action {artifact_id} consumed an approval but its meta has no approval_use_id"
970 ));
971 continue;
972 };
973 if !use_ids.contains(claimed_use_id) {
974 violations.push(format!(
975 "action {artifact_id} claims approval_use_id={} but no such use is embedded",
976 claimed_use_id,
977 ));
978 continue;
979 }
980 let expected = nonce_digest(raw_nonce);
981 let matched_use = bundle.uses.iter().find(|u| u.use_id == claimed_use_id);
982 if let Some(u) = matched_use {
983 if u.nonce_digest != expected {
984 violations.push(format!(
985 "action {artifact_id} approval_nonce hashes to {} but use {} stores nonce_digest {}",
986 expected, claimed_use_id, u.nonce_digest,
987 ));
988 continue;
989 }
990 }
991 bound_count += 1;
992 }
993 if violations.is_empty() {
994 checks.push(VerifyCheck::pass(
995 "approval-use-action-binding",
996 &format!(
997 "{bound_count} consuming action(s) bind cleanly to content-addressed envelope(s)",
998 ),
999 ));
1000 } else {
1001 checks.push(VerifyCheck::fail(
1002 "approval-use-action-binding",
1003 &violations.join("; "),
1004 ));
1005 }
1006 }
1007 }
1008
1009 if !bundle.uses.is_empty() || !bundle.checkpoints.is_empty() {
1036 use std::collections::{HashMap, HashSet};
1037 struct Node<'a> { label: String, digest: &'a str, prev: &'a str }
1040 let mut nodes: Vec<Node> = Vec::new();
1041 for u in &bundle.uses {
1042 nodes.push(Node {
1043 label: format!("use {}", u.use_id),
1044 digest: u.record_digest.as_str(),
1045 prev: u.previous_record_digest.as_str(),
1046 });
1047 }
1048 for cp in &bundle.checkpoints {
1049 nodes.push(Node {
1050 label: format!("checkpoint {}", cp.checkpoint_id),
1051 digest: cp.record_digest.as_str(),
1052 prev: cp.previous_record_digest.as_str(),
1053 });
1054 }
1055
1056 let owned: HashSet<&str> = std::iter::once("")
1057 .chain(nodes.iter().map(|n| n.digest))
1058 .collect();
1059
1060 let mut violations: Vec<String> = Vec::new();
1061 for n in &nodes {
1063 if !owned.contains(n.prev) {
1064 violations.push(format!(
1065 "{} previous_record_digest {} not anchored in package",
1066 n.label, n.prev,
1067 ));
1068 }
1069 }
1070 let genesis: Vec<&Node> = nodes.iter().filter(|n| n.prev.is_empty()).collect();
1072 if genesis.len() > 1 {
1073 violations.push(format!(
1074 "{} records claim previous_record_digest='' (genesis): {}",
1075 genesis.len(),
1076 genesis.iter().map(|n| n.label.clone()).collect::<Vec<_>>().join(", "),
1077 ));
1078 }
1079 let mut by_prev: HashMap<&str, Vec<&Node>> = HashMap::new();
1081 for n in &nodes {
1082 by_prev.entry(n.prev).or_default().push(n);
1083 }
1084 for (prev, group) in &by_prev {
1085 if group.len() > 1 && !prev.is_empty() {
1086 violations.push(format!(
1087 "fork: {} records share previous_record_digest {}: {}",
1088 group.len(),
1089 prev,
1090 group.iter().map(|n| n.label.clone()).collect::<Vec<_>>().join(", "),
1091 ));
1092 }
1093 }
1094
1095 if violations.is_empty() {
1098 let by_digest: HashMap<&str, &Node> = nodes.iter().map(|n| (n.digest, n)).collect();
1099 let next_of: HashMap<&str, &Node> = nodes
1100 .iter()
1101 .filter(|n| !n.prev.is_empty())
1102 .map(|n| (n.prev, *(&n)))
1103 .collect();
1104 let start = match genesis.first() {
1105 Some(g) => Some(*g),
1106 None => None,
1107 };
1108 let mut visited: HashSet<&str> = HashSet::new();
1109 let mut current = start;
1110 while let Some(node) = current {
1111 if !visited.insert(node.digest) {
1112 violations.push(format!(
1113 "cycle detected at {} (record_digest {})",
1114 node.label, node.digest,
1115 ));
1116 break;
1117 }
1118 current = next_of.get(node.digest).copied();
1119 }
1120 if violations.is_empty() && visited.len() != nodes.len() {
1122 let unreached: Vec<String> = nodes.iter()
1123 .filter(|n| !visited.contains(n.digest))
1124 .map(|n| n.label.clone())
1125 .collect();
1126 if !unreached.is_empty() {
1127 violations.push(format!(
1128 "disconnected subchain: {} record(s) not reachable from genesis: {}",
1129 unreached.len(),
1130 unreached.join(", "),
1131 ));
1132 }
1133 }
1134 let _ = by_digest; }
1136
1137 if violations.is_empty() {
1138 checks.push(VerifyCheck::pass(
1139 "approval-use-chain-continuity",
1140 &format!(
1141 "{} record(s) form a single connected linked list from one genesis with no cycles or forks",
1142 nodes.len(),
1143 ),
1144 ));
1145 } else {
1146 checks.push(VerifyCheck::fail(
1147 "approval-use-chain-continuity",
1148 &violations.join("; "),
1149 ));
1150 }
1151 }
1152
1153 let hub_checkpoints: Vec<&JournalCheckpoint> = bundle
1167 .checkpoints
1168 .iter()
1169 .filter(|cp| cp.checkpoint_kind == crate::statements::CheckpointKind::HubOrg)
1170 .collect();
1171 if !hub_checkpoints.is_empty() {
1172 let mut all_ok = true;
1173 let mut details: Vec<String> = Vec::new();
1174 let mut have_valid_signature = false;
1175 let mut security_fatal = false;
1184
1185 for cp in &hub_checkpoints {
1186 match crate::statements::verify_hub_checkpoint_signature(cp, trust) {
1187 crate::statements::HubCheckpointVerification::Valid => {
1188 have_valid_signature = true;
1189 let covered: std::collections::HashSet<&String> =
1194 cp.covered_use_ids.iter().collect();
1195 let missing: Vec<String> = bundle
1196 .uses
1197 .iter()
1198 .filter(|u| !covered.contains(&u.use_id))
1199 .map(|u| u.use_id.clone())
1200 .collect();
1201 if missing.is_empty() {
1202 details.push(format!(
1203 "{} signed by {} verifies; covers {} use(s)",
1204 cp.checkpoint_id,
1205 cp.hub_id,
1206 cp.covered_use_ids.len(),
1207 ));
1208 } else {
1209 all_ok = false;
1210 details.push(format!(
1211 "{} verifies but does not cover {} use(s): {}",
1212 cp.checkpoint_id,
1213 missing.len(),
1214 missing.join(", "),
1215 ));
1216 }
1217 }
1218 crate::statements::HubCheckpointVerification::MissingFields(field) => {
1219 all_ok = false;
1220 details.push(format!(
1221 "{} declares kind=hub-org but field `{}` is missing",
1222 cp.checkpoint_id, field,
1223 ));
1224 }
1225 crate::statements::HubCheckpointVerification::Tampered => {
1226 all_ok = false;
1227 security_fatal = true;
1228 details.push(format!(
1229 "{} hub signature failed verification (tampered or wrong key)",
1230 cp.checkpoint_id,
1231 ));
1232 }
1233 crate::statements::HubCheckpointVerification::NotHubKind => {
1234 all_ok = false;
1238 security_fatal = true;
1239 details.push(format!(
1240 "{} kind toggled out of hub-org during verify",
1241 cp.checkpoint_id,
1242 ));
1243 }
1244 crate::statements::HubCheckpointVerification::UntrustedIssuer => {
1245 all_ok = false;
1246 security_fatal = true;
1247 details.push(format!(
1248 "{} hub_public_key is not a trusted root (configure via `treeship trust add`)",
1249 cp.checkpoint_id,
1250 ));
1251 }
1252 }
1253 }
1254 if all_ok && have_valid_signature {
1255 checks.push(VerifyCheck::pass(
1256 "replay-hub-org",
1257 &details.join("; "),
1258 ));
1259 } else if security_fatal {
1260 checks.push(VerifyCheck::fail(
1264 "replay-hub-org",
1265 &details.join("; "),
1266 ));
1267 } else {
1268 checks.push(VerifyCheck::warn(
1273 "replay-hub-org",
1274 &details.join("; "),
1275 ));
1276 }
1277 }
1278 let _ = ReplayCheckLevel::HubOrg;
1282 let _ = approval_revocation_record_digest as fn(&ApprovalRevocation) -> String;
1283 let _ = ReplayCheck::not_performed;
1284}
1285
1286#[derive(Debug, Clone)]
1288pub struct VerifyCheck {
1289 pub name: String,
1290 pub status: VerifyStatus,
1291 pub detail: String,
1292}
1293
1294#[derive(Debug, Clone, PartialEq, Eq)]
1296pub enum VerifyStatus {
1297 Pass,
1298 Fail,
1299 Warn,
1300}
1301
1302impl VerifyCheck {
1303 pub fn pass(name: &str, detail: &str) -> Self {
1304 Self { name: name.into(), status: VerifyStatus::Pass, detail: detail.into() }
1305 }
1306 pub fn fail(name: &str, detail: &str) -> Self {
1307 Self { name: name.into(), status: VerifyStatus::Fail, detail: detail.into() }
1308 }
1309 pub fn warn(name: &str, detail: &str) -> Self {
1310 Self { name: name.into(), status: VerifyStatus::Warn, detail: detail.into() }
1311 }
1312}
1313
1314impl VerifyCheck {
1315 pub fn passed(&self) -> bool {
1316 self.status == VerifyStatus::Pass
1317 }
1318}
1319
1320const PREVIEW_TEMPLATE: &str = include_str!("preview_template.html");
1323
1324pub fn render_preview_html(receipt: &SessionReceipt) -> String {
1331 let receipt_json = serde_json::to_string_pretty(receipt)
1332 .unwrap_or_else(|_| "{}".to_string());
1333 let safe_json = receipt_json.replace('<', r"\u003c");
1341
1342 PREVIEW_TEMPLATE
1349 .replacen("__RECEIPT_JSON__", &safe_json, 1)
1350}
1351
1352#[cfg(test)]
1353mod tests {
1354 use super::*;
1355 use crate::session::event::*;
1356 use crate::session::manifest::SessionManifest;
1357 use crate::session::receipt::{ArtifactEntry, ReceiptComposer};
1358
1359 fn make_receipt() -> SessionReceipt {
1360 let manifest = SessionManifest::new(
1361 "ssn_pkg_test".into(),
1362 "agent://test".into(),
1363 "2026-04-05T08:00:00Z".into(),
1364 1743843600000,
1365 );
1366
1367 let mk = |seq: u64, inst: &str, et: EventType| -> SessionEvent {
1368 SessionEvent {
1369 session_id: "ssn_pkg_test".into(),
1370 event_id: format!("evt_{:016x}", seq),
1371 timestamp: format!("2026-04-05T08:{:02}:00Z", seq),
1372 sequence_no: seq,
1373 trace_id: "trace_1".into(),
1374 span_id: format!("span_{seq}"),
1375 parent_span_id: None,
1376 agent_id: format!("agent://{inst}"),
1377 agent_instance_id: inst.into(),
1378 agent_name: inst.into(),
1379 agent_role: None,
1380 host_id: "host_1".into(),
1381 tool_runtime_id: None,
1382 event_type: et,
1383 artifact_ref: None,
1384 meta: None,
1385 }
1386 };
1387
1388 let events = vec![
1389 mk(0, "root", EventType::SessionStarted),
1390 mk(1, "root", EventType::AgentStarted { parent_agent_instance_id: None }),
1391 mk(2, "root", EventType::AgentCalledTool {
1392 tool_name: "read_file".into(),
1393 tool_input_digest: None,
1394 tool_output_digest: None,
1395 duration_ms: Some(10),
1396 }),
1397 mk(3, "root", EventType::AgentCompleted { termination_reason: None }),
1398 mk(4, "root", EventType::SessionClosed { summary: Some("Done".into()), duration_ms: Some(60000) }),
1399 ];
1400
1401 let artifacts = vec![
1402 ArtifactEntry { artifact_id: "art_001".into(), payload_type: "action".into(), digest: None, signed_at: None },
1403 ];
1404
1405 ReceiptComposer::compose(&manifest, &events, artifacts)
1406 }
1407
1408 #[test]
1409 fn build_and_read_package() {
1410 let receipt = make_receipt();
1411 let tmp = std::env::temp_dir().join(format!("treeship-pkg-test-{}", rand::random::<u32>()));
1412
1413 let output = build_package(&receipt, &tmp).unwrap();
1414 assert!(output.path.exists());
1415 assert!(output.path.join("receipt.json").exists());
1416 assert!(output.path.join("merkle.json").exists());
1417 assert!(output.path.join("render.json").exists());
1418 assert!(output.path.join("preview.html").exists());
1419 assert!(output.receipt_digest.starts_with("sha256:"));
1420 assert!(output.file_count >= 4);
1421
1422 let read_back = read_package(&output.path).unwrap();
1424 assert_eq!(read_back.session.id, "ssn_pkg_test");
1425 assert_eq!(read_back.type_, RECEIPT_TYPE);
1426
1427 let _ = std::fs::remove_dir_all(&tmp);
1428 }
1429
1430 #[test]
1431 fn verify_valid_package() {
1432 let receipt = make_receipt();
1433 let tmp = std::env::temp_dir().join(format!("treeship-pkg-verify-{}", rand::random::<u32>()));
1434
1435 let output = build_package(&receipt, &tmp).unwrap();
1436 let checks = verify_package(&output.path).unwrap();
1437
1438 let fails: Vec<_> = checks.iter().filter(|c| c.status == VerifyStatus::Fail).collect();
1439 assert!(fails.is_empty(), "unexpected failures: {fails:?}");
1440
1441 let passes: Vec<_> = checks.iter().filter(|c| c.status == VerifyStatus::Pass).collect();
1442 assert!(passes.len() >= 5, "expected at least 5 pass checks, got {}", passes.len());
1443
1444 let _ = std::fs::remove_dir_all(&tmp);
1445 }
1446
1447 #[test]
1448 fn verify_detects_missing_receipt() {
1449 let tmp = std::env::temp_dir().join(format!("treeship-pkg-empty-{}", rand::random::<u32>()));
1450 std::fs::create_dir_all(&tmp).unwrap();
1451
1452 let err = read_package(&tmp);
1453 assert!(err.is_err());
1454
1455 let _ = std::fs::remove_dir_all(&tmp);
1456 }
1457
1458 #[test]
1459 fn preview_html_contains_session_info() {
1460 let receipt = make_receipt();
1461 let html = render_preview_html(&receipt);
1462 assert!(html.contains("ssn_pkg_test"));
1463 assert!(html.contains("treeship.dev"));
1464 assert!(html.contains("Timeline"));
1465
1466 assert!(
1476 html.contains("'__RECEIPT'+'_JSON__'"),
1477 "JS placeholder check was clobbered by the receipt substitution",
1478 );
1479 assert!(
1480 !html.contains("application/json\">__RECEIPT_JSON__</script>"),
1481 "data slot was not substituted with the receipt JSON",
1482 );
1483 assert_eq!(
1487 html.matches("__RECEIPT_JSON__").count(),
1488 0,
1489 "no raw placeholder token should remain after substitution",
1490 );
1491 }
1492}