Writing

Dart archive: ZipDecoder accepts a corrupt zip unless you pass verify, and checks it late

By Mohamed Gado · · 3 min read

Hadeed, my gym log, backs up to a zip: one JSON file plus the user's progress photos. Restoring replaces everything on the phone. While upgrading the Dart archive package to 4.7, I read its changelog and found that my restore could delete somebody's photos and only then discover the backup was damaged.

Nobody had reported it. The fix is two parts, and you need both:

  1. Decode with verify: true.
  2. Read every entry before you delete anything.

What ZipDecoder does with damaged data#

The documentation in archive 4.7.1 is clear once you find it:

Without throwOnError, returned archive holds entries read before damage, and damaged entry may read as partial or empty data.

And about verify:

verify also checks checksums at a tiny cost of ~5-10% speed decrease depending on archive file, and a mismatch throws ArchiveChecksumException. throwOnError defaults to verify, so verify alone throws too.

So a plain ZipDecoder().decodeBytes(bytes) can return an archive that looks normal and holds half a photo. If your restore writes those bytes to disk, the user now has a broken picture and you reported success.

Verify is not enough on its own#

This is the part that surprised me. With verify: true, opening the zip still does not check the contents of every file. In the package's zip_file.dart, the checksum is compared inside decompress:

final crc = _Crc32Tee(output);
final tee = SinkOutputStream(crc);
_decompress(tee);
tee.flush();
_checkSize(tee.length);
_checkCrc32(crc.value);

An entry is decompressed when you first read it. So decodeBytes reads the list of files and returns, and each checksum is compared later, one at a time, as you ask for the data.

Look at what that does to a typical restore:

final archive = ZipDecoder().decodeBytes(zipBytes, verify: true); // passes
final data = readJson(archive);                                   // passes
await clearAllPhotos();                                           // gone
for (final f in archive.files) {
  await save(f.name, f.readBytes());                              // throws here
}

Every validation step succeeds because each one touched only the JSON. The damaged photo throws after the originals were deleted.

The safe order#

Here is the restore now, trimmed:

final Archive archive;
try {
  archive = ZipDecoder().decodeBytes(zipBytes, verify: true);
} catch (_) {
  return false;
}
 
final jsonFile = _findJson(archive);
if (jsonFile == null) return false;
// ...parse it, and check it is one of our backups...
 
// Read every entry now, while nothing is deleted yet.
try {
  for (final f in archive.files) {
    if (f.isFile) f.readBytes();
  }
} catch (_) {
  return false;
}
 
await MediaStore.clearAll();

The loop throws away what it reads. Its only job is to make every checksum run while the user's data is still there.

The check that the JSON is ours matters too. Any zip containing any JSON file gets that far, and the next step cannot be undone.

What it costs#

Reading everything up front means the photos are read before the restore starts. My backup stores them uncompressed, since a JPEG barely shrinks a second time, so this is mostly one pass over the bytes. I did not measure it. For very large archives, or ones from an untrusted source, look at the package's decode limits as well, and consider extracting to a temporary folder and swapping it in at the end. That is the stronger design: the old data survives a crash or a full disk during the restore, which my version does not protect against.

Test it with one flipped byte#

A test with a truncated file is not enough, because a cut zip often fails at decode. Damage the contents of one photo and leave the zip's structure intact:

  1. Build a backup with a photo in it, using your real export code.
  2. Find that photo's data inside the bytes and change one byte.
  3. Restore it.
  4. Expect false, and expect the existing data to be untouched.

Without verify, or without the read loop, that test fails. Step 4's second half is the important one: a restore that returns false after deleting everything is the bug.

The photo should be stored uncompressed for this test. A flipped byte in stored data leaves the size right and the checksum wrong, so only the CRC check can catch it.

Check your own code#

Search your project for ZipDecoder( and look at each call:

  • Is verify: true there?
  • What runs between the decode and the last readBytes()? Anything that deletes or overwrites should move after it.
  • Does any caller catch the exception and carry on with what it has?

Versions: archive 4.7.1. The 4.x changelog marks several changes to verify and error handling as breaking, so read it before you upgrade even if your code compiles.