Repository navigation
zip: Reject entries whose compression method is unsupported - #282
Open
dhivadhiva wants to merge 1 commit into
Open
dhivadhiva wants to merge 1 commit into
dhivadhiva wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zip/rust/Cargo.tomlleaves 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:CompressionMethodreports them asUnsupported(kUnimplementedin C++).read.hsaysGetFileByIndex()and the stream readers returnkUnimplementedfor 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:GetFileByIndex(0)then a 1-byteReadInto()GetFileByIndex(0)returnskUnimplementedEvery such entry opened at the same time adds another 256 MiB. A Deflate64 entry is decoded too, and its errors surface as
kInternalrather thankUnimplemented.Why not
default-features = falseThat was the first thing I tried. The Rust tests pass with it, but the CMake build fails. Crubit's
cargo-cpp_api_from_rustgenerates arust-cc-apicrate that depends on every crates.io package asname = "version", herezip = "9.6.1", which turns zip's default features back on. The bindings, generated with them off, then fail their layout assertions:(
pixel_bridgeturns offimage's defaults without hitting this, presumably because noimagetypes reach its bindings.)Fix
Check the method in the bridge instead:
get_file_by_index()onBufferedZipArchiveandFsZipArchive, andread_next_file*()on both stream readers, now returnUnimplementedfor a method thatCompressionMethoddoesn't name, before any decompressor exists.read_next_file_impldocuments 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.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:BufferedZipArchiveandFsZipArchive, plus raw access, which still works.test_stream_reader_rejects_ppmd_entry:Unimplemented, thenFailedPrecondition.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
mainand 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.txthad no tests. This adds azip_rust_testctest entry that runscargo test, the same wayregex_rust_testdoes.Built from a clean checkout with the CI recipe (
cmake -B build -S zip, Crubit b715411, nightly-2026-11-11). The build passes,ctestrunszip_rust_test(36 passed), andzip_examplestill runs.