Skip to content

Commit 1c50187

Browse files
authored
feat(fuzz): add base64_fuzz target (#1143)
## Summary - Add fuzz target for the `base64` builtin covering encode/decode with arbitrary data - Tests invalid base64 sequences, wrong padding, truncated input, roundtrips, and wrap width options ## Test plan - [x] `cargo check` passes in fuzz directory - [ ] CI green Closes #1104
1 parent 8dc37b3 commit 1c50187

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

crates/bashkit/fuzz/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ path = "fuzz_targets/printf_fuzz.rs"
7373
test = false
7474
doc = false
7575
bench = false
76+
77+
[[bin]]
78+
name = "base64_fuzz"
79+
path = "fuzz_targets/base64_fuzz.rs"
80+
test = false
81+
doc = false
82+
bench = false
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//! Fuzz target for the base64 builtin
2+
//!
3+
//! Tests base64 encode/decode to find:
4+
//! - Panics on invalid base64 sequences or wrong padding
5+
//! - Encode/decode roundtrip mismatches
6+
//! - Truncated input handling
7+
//! - Edge cases with wrap width and -d flag
8+
//!
9+
//! Run with: cargo +nightly fuzz run base64_fuzz -- -max_total_time=300
10+
11+
#![no_main]
12+
13+
use libfuzzer_sys::fuzz_target;
14+
15+
fuzz_target!(|data: &[u8]| {
16+
// Only process valid UTF-8
17+
if let Ok(input) = std::str::from_utf8(data) {
18+
// Limit input size to prevent OOM
19+
if input.len() > 1024 {
20+
return;
21+
}
22+
23+
let rt = tokio::runtime::Builder::new_current_thread()
24+
.enable_all()
25+
.build()
26+
.unwrap();
27+
28+
rt.block_on(async {
29+
let mut bash = bashkit::Bash::builder()
30+
.limits(
31+
bashkit::ExecutionLimits::new()
32+
.max_commands(50)
33+
.max_subst_depth(3)
34+
.max_stdout_bytes(8192)
35+
.max_stderr_bytes(4096)
36+
.timeout(std::time::Duration::from_millis(200)),
37+
)
38+
.build();
39+
40+
// Test 1: encode arbitrary data
41+
let script = format!(
42+
"echo -n '{}' | base64 2>/dev/null; true",
43+
input.replace('\'', "'\\''"),
44+
);
45+
let _ = bash.exec(&script).await;
46+
47+
// Test 2: decode arbitrary data (may be invalid base64)
48+
let script2 = format!(
49+
"echo -n '{}' | base64 -d 2>/dev/null; true",
50+
input.replace('\'', "'\\''"),
51+
);
52+
let _ = bash.exec(&script2).await;
53+
54+
// Test 3: encode then decode roundtrip
55+
let script3 = format!(
56+
"echo -n '{}' | base64 | base64 -d 2>/dev/null; true",
57+
input.replace('\'', "'\\''"),
58+
);
59+
let _ = bash.exec(&script3).await;
60+
61+
// Test 4: decode with --wrap=0
62+
let script4 = format!(
63+
"echo -n '{}' | base64 --wrap=0 2>/dev/null; true",
64+
input.replace('\'', "'\\''"),
65+
);
66+
let _ = bash.exec(&script4).await;
67+
});
68+
}
69+
});

0 commit comments

Comments
 (0)