Skip to content

zip: Reject entries whose compression method is unsupported - #282

Open
dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:zip-default-features
Open

dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:zip-default-features

Conversation

@dhivadhiva

Copy link
Copy Markdown
Contributor

zip/rust/Cargo.toml leaves zip's default features on, so zip-rs also decodes PPMd and Deflate64 and has AES support built in. The bridge doesn't name these methods: CompressionMethod reports them as Unsupported (kUnimplemented in C++). read.h says GetFileByIndex() and the stream readers return kUnimplemented for an entry that uses an unsupported compression method, but they don't: zip-rs only builds the decompressor on the first read, and then decodes the entry anyway.

Memory from a tiny archive

A PPMd entry's 2-byte header picks the model size, up to 256 MiB, and the decoder allocates and zeroes that much before it decodes anything. On main:

134-byte archive, one PPMd entry Peak RSS
GetFileByIndex(0) then a 1-byte ReadInto() 267 MB (4.5 MB baseline)
The same with this change 4.8 MB, and GetFileByIndex(0) returns kUnimplemented

Every such entry opened at the same time adds another 256 MiB. A Deflate64 entry is decoded too, and its errors surface as kInternal rather than kUnimplemented.

Why not default-features = false

That was the first thing I tried. The Rust tests pass with it, but the CMake build fails. Crubit's cargo-cpp_api_from_rust generates a rust-cc-api crate that depends on every crates.io package as name = "version", here zip = "9.6.1", which turns zip's default features back on. The bindings, generated with them off, then fail their layout assertions:

error[E0080]: evaluation panicked: assertion failed: ::core::mem::size_of::<::zip::PreparedZipFile>() == 184
error[E0080]: evaluation panicked: assertion failed: ::core::mem::size_of::<::zip::write::ZipFileBuilder>() == 4496

(pixel_bridge turns off image's defaults without hitting this, presumably because no image types reach its bindings.)

Fix

Check the method in the bridge instead:

  • get_file_by_index() on BufferedZipArchive and FsZipArchive, and read_next_file*() on both stream readers, now return Unimplemented for a method that CompressionMethod doesn't name, before any decompressor exists.
  • For the stream readers this counts as a failed read and poisons the reader, as read_next_file_impl documents for every failed read. Dropping the rejected entry drains its raw bytes without decompressing.
  • get_file_by_index_raw() doesn't decompress and still works.
  • Encrypted entries, AES included, still fail earlier with kFailedPrecondition.

The C++ headers already document this behavior, so they don't change.

Testing

Four new Rust tests in read.rs:

  • test_ppmd_entry_is_unimplemented: BufferedZipArchive and FsZipArchive, plus raw access, which still works.
  • test_stream_reader_rejects_ppmd_entry: Unimplemented, then FailedPrecondition.
  • test_deflate64_entry_is_unimplemented.
  • test_aes_entry_is_failed_precondition: pins that the new check doesn't change the error for AES entries.

The first three fail on main and the AES one passes there. All 36 tests in the crate pass with this change.

The crate's Rust tests didn't run in CI before, because zip/CMakeLists.txt had no tests. This adds a zip_rust_test ctest entry that runs cargo test, the same way regex_rust_test does.

Built from a clean checkout with the CI recipe (cmake -B build -S zip, Crubit b715411, nightly-2026-11-11). The build passes, ctest runs zip_rust_test (36 passed), and zip_example still runs.

zip/rust/Cargo.toml leaves zip's default features on, so zip-rs also
decodes PPMd and Deflate64, and has AES support built in. The bridge
does not name these methods: CompressionMethod reports them as
Unsupported (kUnimplemented in C++), and read.h says GetFileByIndex()
and the stream readers return kUnimplemented for an entry that uses an
unsupported compression method. They don't. zip-rs only builds the
decompressor on the first read, and then decodes the entry anyway.

A PPMd entry's 2-byte header picks the model size, up to 256 MiB, which
the decoder allocates and zeroes before it decodes anything. So a
126-byte archive makes a 1-byte ReadInto() peak at 267 MB, and every
such entry opened at the same time adds another 256 MiB.

Turning the default features off would be the obvious fix, but Crubit's
cargo-cpp_api_from_rust generates a crate that depends on zip as
`zip = "<version>"`, which re-enables them. The bindings generated with
the features off then fail its layout assertions (PreparedZipFile,
ZipFileBuilder).

So check the method instead: get_file_by_index() on both archives and
read_next_file*() on both stream readers now return Unimplemented for a
method that CompressionMethod does not name, before any decompressor
exists. For the stream readers this counts as a failed read and poisons
the reader, as documented. get_file_by_index_raw() does not decompress
and still works. Encrypted entries, AES included, still fail earlier
with FailedPrecondition.

Add tests for PPMd and Deflate64 entries through both archives and the
stream reader, and one that pins the AES behaviour, and run the crate's
Rust tests from ctest, as regex does, so CI runs them.
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.

1 participant