Skip to content

Building 70 Projects ranging from beginner to advanced so anyone can β€” learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company πŸ‘‡

fix(lisdex): B22 - an interrupted clone was reused as a finished tree with a confident revision

CarterPerez-devb814c3487526
diff --git a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs
index 74605b25..89388905 100644
--- a/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs
+++ b/PROJECTS/advanced/zero-day-vulnerability-scanner/crates/lisdex-ingest/src/acquire.rs
@@ -24,6 +24,9 @@ const ARG_HEAD: &str = "HEAD";
const ARG_C: &str = "-C";
const ARG_ARCHIVE: &str = "-a";
const COPY_CONTENTS: &str = "/.";
+const UNKNOWN_REVISION: &str = "unknown";
+
+pub const MARKER_FILE: &str = ".lisdex-acquired";
#[derive(Debug, thiserror::Error)]
pub enum AcquireError {
@@ -38,6 +41,10 @@ pub enum AcquireError {
MissingSource(PathBuf),
#[error("destination {0} already holds files; pass reuse to build it again")]
DestinationNotEmpty(PathBuf),
+ #[error(
+ "destination {path} holds a partial acquisition: no {marker} completion marker. An interrupted clone or copy leaves a tree that looks populated and reports a revision but is missing files. Delete it and acquire again"
+ )]
+ Incomplete { path: PathBuf, marker: &'static str },
#[error("{command} failed ({code:?}): {output}")]
Command {
command: String,
@@ -142,6 +149,22 @@ fn is_populated(path: &Path) -> Result<bool, AcquireError> {
Ok(entries.next().is_some())
}
+#[must_use]
+pub fn marker_path(root: &Path) -> PathBuf {
+ root.join(MARKER_FILE)
+}
+
+#[must_use]
+pub fn is_complete(root: &Path) -> bool {
+ marker_path(root).is_file()
+}
+
+fn mark_complete(root: &Path, revision: Option<&str>) -> Result<(), AcquireError> {
+ let path = marker_path(root);
+ let contents = format!("{}\n", revision.unwrap_or(UNKNOWN_REVISION));
+ std::fs::write(&path, contents).map_err(|source| AcquireError::Io { path, source })
+}
+
fn checked(execution: Execution) -> Result<Execution, AcquireError> {
if execution.success {
return Ok(execution);
@@ -180,6 +203,12 @@ pub fn acquire(source: &Source, dest: &Path, reuse: bool) -> Result<Acquisition,
if !reuse {
return Err(AcquireError::DestinationNotEmpty(dest.to_path_buf()));
}
+ if !is_complete(dest) {
+ return Err(AcquireError::Incomplete {
+ path: dest.to_path_buf(),
+ marker: MARKER_FILE,
+ });
+ }
return Ok(Acquisition {
root: dest.to_path_buf(),
revision: revision(dest),
@@ -216,9 +245,11 @@ pub fn acquire(source: &Source, dest: &Path, reuse: bool) -> Result<Acquisition,
}
}
+ let resolved = revision(dest);
+ mark_complete(dest, resolved.as_deref())?;
Ok(Acquisition {
root: dest.to_path_buf(),
- revision: revision(dest),
+ revision: resolved,
reused: false,
})
}
@@ -354,15 +385,55 @@ mod tests {
let dir = scratch("reuse");
let src = dir.join("src");
std::fs::create_dir_all(&src).unwrap();
+ std::fs::write(src.join("kept"), "old").unwrap();
let dest = dir.join("work");
- std::fs::create_dir_all(&dest).unwrap();
- std::fs::write(dest.join("kept"), "old").unwrap();
+ assert!(
+ !acquire(&Source::Local(src.clone()), &dest, false)
+ .unwrap()
+ .reused
+ );
let acquired = acquire(&Source::Local(src), &dest, true).unwrap();
assert!(acquired.reused);
assert_eq!(std::fs::read_to_string(dest.join("kept")).unwrap(), "old");
}
+ #[test]
+ fn a_finished_acquisition_records_its_completion_marker() {
+ let dir = scratch("marked");
+ let src = dir.join("src");
+ std::fs::create_dir_all(&src).unwrap();
+ std::fs::write(src.join("configure.ac"), "AC_INIT").unwrap();
+ let dest = dir.join("work");
+
+ assert!(!is_complete(&dest));
+ acquire(&Source::Local(src), &dest, false).unwrap();
+ assert!(
+ is_complete(&dest),
+ "a finished acquire must leave {MARKER_FILE}"
+ );
+ assert_eq!(
+ std::fs::read_to_string(marker_path(&dest)).unwrap(),
+ format!("{UNKNOWN_REVISION}\n")
+ );
+ }
+
+ #[test]
+ fn an_interrupted_clone_is_refused_on_reuse_rather_than_built_half_checked_out() {
+ let dir = scratch("partial");
+ let src = dir.join("src");
+ std::fs::create_dir_all(&src).unwrap();
+ let dest = dir.join("work");
+ std::fs::create_dir_all(dest.join(GIT_DIR)).unwrap();
+ std::fs::write(dest.join("Makefile"), "all:").unwrap();
+
+ let refused = acquire(&Source::Local(src), &dest, true);
+ assert!(
+ matches!(refused, Err(AcquireError::Incomplete { .. })),
+ "a populated but unmarked tree must be refused, got {refused:?}"
+ );
+ }
+
#[test]
fn acquiring_a_missing_local_tree_is_an_error() {
let dir = scratch("missing");