Skip to content

fix(write): raw_copy_file corrupted ZipCrypto metadata - #1288

Merged
Pr0methean merged 1 commit into
zip-rs:masterfrom
vdavid:fix/raw-copy-encrypted
Nov 10, 2026
Merged

Pr0methean merged 1 commit into
zip-rs:masterfrom
vdavid:fix/raw-copy-encrypted

Conversation

@vdavid

@vdavid vdavid commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Hi again!

(AI use disclosure: Claude-generated PR description edited by me. The code changes are also Claude-created, I've reviewed them and it looks good.)

Here is another one from Cmdr's in-place zip editing: deleting _one_ file from a password-protected zip left every other ZipCrypto entry in it undecryptable.

The problem: raw_copy_file writes a ZipCrypto entry with a clear encrypted flag (bit 0), so readers serve the ciphertext as plaintext. raw_copy_options builds on ZipFile::options(), which carries AES settings but no ZipCrypto state (it can't, there's no password), so start_entry never sets the flag. It drops the data descriptor flag (bit 3) too, and for ZipCrypto that one matters on its own: under bit 3, the password check byte is the DOS time's high byte (that's how Info-ZIP's zip -e and this crate's writer do it), otherwise it's the CRC's. So even with bit 0 back, the right password looks wrong.

(AES entries are fine since #1078: options() carries their mode, and the check lives in the copied bytes.)

A second bug with the same fix: a raw copy into ZipWriter::new_stream sets bit 3 (because !seek_possible) and zeroes the sizes in the local header, but finish_file writes no data descriptor for a raw entry. So a streaming reader can't find where the entry ends: cat out.zip | bsdtar -xOf - fails with "ZIP bad CRC". add_prepared_file already works around this by writing the descriptor itself.

What the PR changes:

  • A raw copy of an encrypted entry keeps its bit 0 and bit 3. A new crate-private ZipRawValues::encrypted_with_descriptor carries them into start_entry, which applies them after its own data descriptor default.
  • raw_copy_file_rename_internal writes a data descriptor after the copied bytes when bit 3 is set, same as add_prepared_file.
  • raw_copy_file_touch's docs mention the one case no copy can fix: a new time with a different high byte breaks a bit-3 ZipCrypto entry's password check, since the check byte sits encrypted in the copied bytes.

This PR makes no public API change.

Tests:

  • raw_copy_file_keeps_a_zipcrypto_entry_decryptable (end of write.rs's tests module): raw_copy_file and raw_copy_file_rename of a ZipCrypto entry keep bits 0 and 3, and the copy decrypts with the password.
  • raw_copy_to_a_stream_writes_a_data_descriptor (tests/data_descriptor.rs).

Both fail on master without the fix.

cargo test (default, all, no-default, deflate-zopfli, and deflate-flate2-zlib-rs features), clippy, doc, and fmt pass, apart from clippy's field_reassign_with_default in extra_field.rs with --no-default-features, which fails on master too.

Thanks again!

  • The PR title conforms to Conventional Commits and starts with an Angular type.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Comment thread src/write.rs Dismissed
Comment thread src/write.rs Dismissed
@Pr0methean
Pr0methean added this pull request to the merge queue Nov 10, 2026
Merged via the queue into zip-rs:master with commit d4976d1 Nov 10, 2026
129 checks passed
@Pr0methean Pr0methean changed the title fix(write): raw_copy_file keeps an encrypted entry decryptable fix(write): raw_copy_file corrupted ZipCrypto entry metadata Nov 10, 2026
@Pr0methean Pr0methean changed the title fix(write): raw_copy_file corrupted ZipCrypto entry metadata fix(write): raw_copy_file corrupted ZipCrypto metadata Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants