Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit b7966a8

Browse files
committed
Add read fuzzing module
As someone who has personal projects that take untrusted zips as input, it is important to me to be able to fuzz the zip project to simulate possible inputs and to ensure the projects are not vulnerable. This commit adds a cargo fuzz module for reading and extracting input. The `fuzz` directory was scaffolded with a `cargo fuzz init` I added a CI step to guard against the fuzz module decaying over time.
1 parent 7f424a7 commit b7966a8

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

.github/workflows/ci.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,19 @@ jobs:
7474

7575
- name: Docs
7676
run: cargo doc
77+
78+
fuzz:
79+
runs-on: ubuntu-latest
80+
81+
steps:
82+
- uses: actions/checkout@v2
83+
- uses: actions-rs/toolchain@v1
84+
with:
85+
profile: minimal
86+
toolchain: nightly
87+
override: true
88+
89+
- run: cargo install cargo-fuzz
90+
- name: compile fuzz
91+
run: |
92+
cargo fuzz build fuzz_read

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,24 @@ See the [examples directory](examples) for:
7575
* How to extract a zip file.
7676
* How to extract a single file from a zip.
7777
* How to read a zip from the standard input.
78+
79+
Fuzzing
80+
-------
81+
82+
Fuzzing support is through [cargo fuzz](https://github.com/rust-fuzz/cargo-fuzz). To install cargo fuzz:
83+
84+
```bash
85+
cargo install cargo-fuzz
86+
```
87+
88+
To list fuzz targets:
89+
90+
```bash
91+
cargo +nightly fuzz list
92+
```
93+
94+
To start fuzzing zip extraction:
95+
96+
```bash
97+
cargo +nightly fuzz run fuzz_read
98+
```

fuzz/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target
2+
corpus
3+
artifacts

fuzz/Cargo.toml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "zip-fuzz"
3+
version = "0.0.0"
4+
authors = ["Automatically generated"]
5+
publish = false
6+
edition = "2018"
7+
8+
[package.metadata]
9+
cargo-fuzz = true
10+
11+
[dependencies]
12+
libfuzzer-sys = "0.4"
13+
14+
[dependencies.zip]
15+
path = ".."
16+
17+
# Prevent this from interfering with workspaces
18+
[workspace]
19+
members = ["."]
20+
21+
[[bin]]
22+
name = "fuzz_read"
23+
path = "fuzz_targets/fuzz_read.rs"
24+
test = false
25+
doc = false

fuzz/fuzz_targets/fuzz_read.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![no_main]
2+
use libfuzzer_sys::fuzz_target;
3+
4+
fn decompress_all(data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
5+
let reader = std::io::Cursor::new(data);
6+
let mut zip = zip::ZipArchive::new(reader)?;
7+
8+
for i in 0..zip.len() {
9+
let mut file = zip.by_index(i)?;
10+
if file.size() < 1 << 20 {
11+
let _ = std::io::copy(&mut file, &mut std::io::sink());
12+
}
13+
}
14+
15+
Ok(())
16+
}
17+
18+
fuzz_target!(|data: &[u8]| {
19+
let _ = decompress_all(data);
20+
});

0 commit comments

Comments
 (0)