Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

The archive inventory says when nested archives were left unopened (#534)

GGWA5cdcbb7503a7
diff --git a/crates/analysis/src/js_inventory.rs b/crates/analysis/src/js_inventory.rs
index a1ca0a8e..41189636 100644
--- a/crates/analysis/src/js_inventory.rs
+++ b/crates/analysis/src/js_inventory.rs
@@ -62,6 +62,7 @@ pub struct Inventory {
pub containers: Vec<Value>,
pub contradictions: Vec<Value>,
pub truncated: bool,
+ pub unopened_nested: usize,
pub hashed_bytes: u64,
}
@@ -280,8 +281,10 @@ impl<'c> Inventorier<'c> {
if archive {
if depth >= self.limits.max_depth {
self.note_last("nested archive not opened: depth limit");
+ self.inv.unopened_nested = self.inv.unopened_nested.saturating_add(1);
} else if bytes.len() as u64 > self.limits.max_nested_bytes {
self.note_last("nested archive not opened: too large");
+ self.inv.unopened_nested = self.inv.unopened_nested.saturating_add(1);
} else {
self.add_archive(&format!("{path}!/"), bytes, kind, depth + 1)?;
}
@@ -465,7 +468,24 @@ mod tests {
// Depth limit stops expansion.
let mut shallow = Inventorier::new("b", "zip", InvLimits { max_depth: 0, ..Default::default() }, &|| false);
shallow.add_file("outer.zip", &z, 0).unwrap();
- assert_eq!(shallow.finish().entries.len(), 1);
+ let shallow = shallow.finish();
+ assert_eq!(shallow.entries.len(), 1);
+ assert_eq!(serde_json::to_value(&shallow).unwrap()["unopened_nested"], 1);
+ assert!(!shallow.truncated);
+ assert_eq!(shallow.entries[0].note.as_deref(), Some("nested archive not opened: depth limit"));
+ }
+
+ #[test]
+ fn oversized_nested_archive_is_counted_without_truncating_the_listing() {
+ let z = fx::zip_stored(&[("leaf.txt", b"leaf")]);
+ let mut inv = Inventorier::new("bundle", "directory", InvLimits { max_nested_bytes: 0, ..Default::default() }, &|| false);
+ inv.add_file("inner.zip", &z, 0).unwrap();
+ inv.add_file("other.txt", b"other", 0).unwrap();
+ let inv = inv.finish();
+ assert_eq!(serde_json::to_value(&inv).unwrap()["unopened_nested"], 1);
+ assert!(!inv.truncated);
+ assert_eq!(inv.entries.len(), 2);
+ assert_eq!(inv.entries[0].note.as_deref(), Some("nested archive not opened: too large"));
}
#[test]
diff --git a/crates/engine/src/tools/artifact_tools.rs b/crates/engine/src/tools/artifact_tools.rs
index 661e160c..d3930f2b 100644
--- a/crates/engine/src/tools/artifact_tools.rs
+++ b/crates/engine/src/tools/artifact_tools.rs
@@ -567,10 +567,19 @@ fn core_archive_show(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Too
"A size, count or depth limit stopped the inventory early; raise max_entries, max_total_bytes or max_depth to see more.",
));
}
+ if inv.unopened_nested > 0 {
+ lims.push(Limitation::new(
+ "nested_not_opened",
+ format!(
+ "{} nested archive(s) were not opened (depth or size limit; each entry's note says which); raise max_depth to open deeper ones.",
+ inv.unopened_nested
+ ),
+ ));
+ }
lims.extend(unlisted_limitation(&unlisted));
lims.push(Limitation::new("no_disk_images", "Disk images (DMG), installers (PKG/MSI) and compressed tarballs are listed as single files, not opened."));
let mut coverage = Coverage::page(page.len() as u64, total as u64, p.offset as u64);
- if !unlisted.is_empty() {
+ if !unlisted.is_empty() || inv.unopened_nested > 0 {
coverage.status = craft_anything_evidence::CoverageStatus::Partial;
}
let ev = EvidenceBuilder::new("core_archive_show", subject, env(ctx))
@@ -580,6 +589,7 @@ fn core_archive_show(ctx: &mut Ctx<'_>, params: Value) -> Result<ToolOutput, Too
"root": inv.root, "root_kind": inv.root_kind, "content_id": inv.content_id(),
"files": files, "entries_total": total, "by_kind": inv.by_kind(), "hashed_bytes": inv.hashed_bytes,
"containers": inv.containers, "contradictions": inv.contradictions, "truncated": inv.truncated,
+ "unopened_nested": inv.unopened_nested,
"entries": page,
}))
.summary(summary)
@@ -817,6 +827,26 @@ mod tests {
e.call(id, p, CallOptions::new(Caller::Cli)).map(|o| o.to_json())
}
+ #[test]
+ fn unopened_nested_archives_are_disclosed_in_coverage() {
+ let dir = tempfile::tempdir().unwrap();
+ let e = Engine::new(crate::testing::session());
+ for depth in [2, 5] {
+ let mut bytes = fx::zip_stored(&[("leaf.txt", b"leaf")]);
+ for _ in 0..depth {
+ bytes = fx::zip_stored(&[("inner.zip", &bytes)]);
+ }
+ let path = dir.path().join(format!("nested-{depth}.zip"));
+ std::fs::write(&path, bytes).unwrap();
+ let inv = call(&e, "core_archive_show", json!({"path": path})).unwrap();
+ let unopened = depth == 5;
+ assert_eq!(inv["limitations"].as_array().unwrap().iter().any(|l| l["code"] == "nested_not_opened"), unopened);
+ assert_eq!(inv["coverage"]["status"], if unopened { "partial" } else { "complete" });
+ assert_eq!(inv["result"]["entries"].as_array().unwrap().iter().any(|entry| entry["note"] == "nested archive not opened: depth limit"), unopened);
+ assert_eq!(inv["result"]["truncated"], false);
+ }
+ }
+
#[test]
fn inventory_and_extract_asar() {
let dir = tempfile::tempdir().unwrap();