Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(restore): validate the gzip trailer before replacing live data

Samir Hanna Verza2dd2f5e4cdd9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e65cba04..b516f032 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- Refused backup restores with a missing or invalid gzip trailer before
+ replacing live data, even when all tar entries were readable (#NNN).
- Refused empty and whitespace-only `--workspace` and `--project` flags at the
shared CLI scope-resolution choke point (`resolve_scope`,
`resolve_scope_noting_marker_project`, `resolve_scope_with_marker`, and
diff --git a/crates/ai-memory-cli/src/commands/restore.rs b/crates/ai-memory-cli/src/commands/restore.rs
index 9a6d39a9..46a14ac7 100644
--- a/crates/ai-memory-cli/src/commands/restore.rs
+++ b/crates/ai-memory-cli/src/commands/restore.rs
@@ -206,6 +206,11 @@ async fn stage_then_swap(
let mut archive = tar::Archive::new(decoder);
unpack_checked_archive(&mut archive, staging)
.with_context(|| format!("extracting {} into {}", from.display(), staging.display()))?;
+ // tar stops at its zero header, before the gzip decoder necessarily reads
+ // the checksum/size trailer. Finish the member before trusting its files.
+ // https://docs.rs/tar/latest/tar/struct.Archive.html#method.set_ignore_zeros
+ std::io::copy(&mut archive.into_inner(), &mut std::io::sink())
+ .with_context(|| format!("verifying gzip stream in {}", from.display()))?;
info!(from = %from.display(), into = %staging.display(), "tarball extracted into staging");
let placement = if !staging.join(CONFIG_FILE).is_file() {
@@ -880,6 +885,53 @@ mod tests {
assert_live_data_untouched(data.path());
}
+ #[tokio::test]
+ async fn restore_force_keeps_live_data_when_the_gzip_checksum_is_invalid() {
+ let data = tempfile::TempDir::new().unwrap();
+ seed_live_data(data.path());
+ let scratch = tempfile::TempDir::new().unwrap();
+ let mut bytes = good_archive();
+ // All tar entries are readable, but the gzip trailer's CRC32 is wrong.
+ let checksum_offset = bytes.len() - 8;
+ bytes[checksum_offset] ^= 0x80;
+ let from = write_tarball(scratch.path(), "bad-crc.tar.gz", &bytes);
+
+ let err = restore_data_dir(data.path(), &from, true, false)
+ .await
+ .unwrap_err();
+
+ assert!(
+ format!("{err:#}").contains("verifying gzip stream"),
+ "unexpected error: {err:#}"
+ );
+ assert_live_data_untouched(data.path());
+ }
+
+ #[tokio::test]
+ async fn restore_force_keeps_live_data_when_the_gzip_trailer_is_missing() {
+ let data = tempfile::TempDir::new().unwrap();
+ seed_live_data(data.path());
+ let scratch = tempfile::TempDir::new().unwrap();
+ let bytes = good_archive();
+ // Keep the compressed tar, including its end marker, but remove the
+ // gzip checksum and original-size trailer.
+ let from = write_tarball(
+ scratch.path(),
+ "no-trailer.tar.gz",
+ &bytes[..bytes.len() - 8],
+ );
+
+ let err = restore_data_dir(data.path(), &from, true, false)
+ .await
+ .unwrap_err();
+
+ assert!(
+ format!("{err:#}").contains("verifying gzip stream"),
+ "unexpected error: {err:#}"
+ );
+ assert_live_data_untouched(data.path());
+ }
+
#[tokio::test]
async fn restore_force_keeps_live_data_when_an_entry_is_outside_the_allowed_layout() {
let data = tempfile::TempDir::new().unwrap();
diff --git a/docs/lifecycle-ops.md b/docs/lifecycle-ops.md
index afb26dfa..8c0faca8 100644
--- a/docs/lifecycle-ops.md
+++ b/docs/lifecycle-ops.md
@@ -1221,7 +1221,9 @@ Order of operations:
1. Check the data dir is empty (or the user passed `--force`).
2. Extract the tarball into a staging directory beside the live data
- (`<data_dir>/.restore-staging-<stamp>/`), validating every entry.
+ (`<data_dir>/.restore-staging-<stamp>/`), validating every entry and
+ reading the gzip stream through its checksum and size trailer. A missing
+ or invalid trailer fails here even when every tar entry was readable.
3. Require an existing `db/memory.sqlite` snapshot with applied ai-memory
migration history, then open the staged store so pending migrations run
and the SQLite snapshot is checked, still without touching the live data.