Revalidate artifacts and guard complete flash streams
build-test-release / release (push) Successful in 16m22s

This commit is contained in:
2026-10-14 20:54:29 -06:00
parent 3846b12d8c
commit 694e2fc7e5
4 changed files with 99 additions and 8 deletions
+20 -4
View File
@@ -52,6 +52,11 @@ impl VerifiedArtifact {
pub fn reader(&mut self) -> &mut File {
&mut self.file
}
/// Recheck the same descriptor immediately before a guarded write.
pub fn revalidate(&mut self) -> Result<(), VerifyError> {
verify_open_file(&mut self.file, &self.record)
}
}
pub fn seed_manifest() -> Result<Vec<ArtifactRecord>, VerifyError> {
@@ -118,9 +123,18 @@ fn signature_matches(format: &str, first: &[u8]) -> Result<bool, VerifyError> {
pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtifact, VerifyError> {
let mut file = File::open(path)?;
verify_open_file(&mut file, record)?;
Ok(VerifiedArtifact {
file,
record: record.clone(),
})
}
fn verify_open_file(file: &mut File, record: &ArtifactRecord) -> Result<(), VerifyError> {
if file.metadata()?.len() != record.size_bytes {
return Err(VerifyError::Size);
}
file.seek(SeekFrom::Start(0))?;
let mut first = [0_u8; 16];
let length = file.read(&mut first)?;
if !signature_matches(&record.format, &first[..length])? {
@@ -147,10 +161,7 @@ pub fn verify_file(path: &Path, record: &ArtifactRecord) -> Result<VerifiedArtif
return Err(VerifyError::Digest);
}
file.seek(SeekFrom::Start(0))?;
Ok(VerifiedArtifact {
file,
record: record.clone(),
})
Ok(())
}
#[cfg(test)]
@@ -189,9 +200,14 @@ mod tests {
applicability: "test".to_owned(),
};
let mut verified = verify_file(&path, &record).unwrap();
verified.revalidate().unwrap();
let mut replay = Vec::new();
verified.reader().read_to_end(&mut replay).unwrap();
assert_eq!(replay, bytes);
std::fs::write(&path, b"PK\x03\x04changed").unwrap();
assert!(matches!(verified.revalidate(), Err(VerifyError::Digest)));
std::fs::write(&path, bytes).unwrap();
verified.revalidate().unwrap();
record.size_bytes += 1;
assert!(matches!(
verify_file(&path, &record),