While hunting open-source bounties across the Stellar / Soroban Rust CLI (stellar/stellar-cli) today, I ran into three deceptively subtle CLI and contract parser bugs that pass normal unit tests yet fail quietly in production.
If you build Rust CLIs with clap, parse JSON into typed contract ABIs, or design tool-call sandboxes for AI agents, these three patterns are worth auditing in your own codebase.
1. Silent .zip() Truncation on JSON Tuple-Struct Arrays
When parsing untyped JSON arrays into typed Rust tuple structs (like Soroban contract spec types), it is tempting to pair the incoming serde_json::Value::Array elements with the struct's field definitions using .iter().zip():
Value::Array(array) => {
if array.len() != strukt.fields.len() {
return Err(Error::InvalidValue(Some(Type::Udt(Udt {
name: strukt.name.clone(),
}))));
}
let fields: Result<Vec<_>, _> = array
.iter()
.zip(strukt.fields.iter())
.map(|(val, field)| self.from_json(val, &field.value_type))
.collect();
// ...
}
The trap: Rust's Iterator::zip stops as soon as either iterator is exhausted. Without an explicit array.len() != strukt.fields.len() arity check before .zip():
- Passing extra elements (
[10, 20]for a 1-field tuple struct) silently drops the trailing values and succeeds. - Passing too few elements (
[]for a 1-field tuple struct) yields an empty iterator, bypassing field validation and failing downstream in the XDR encoder with a cryptic error.
The fix: Always guard .zip() over external input with an explicit arity check before mapping.
2. Dynamic clap Kebab-Case Alias Collisions
Many modern CLIs dynamically generate clap::Command subcommands at runtime from external schemas (e.g., smart contract methods, OpenAPI specs, or MCP tool definitions).
To make snake_case contract methods ergonomic on the command line, you might attach a kebab-case visible alias (hello_world -> --hello-world).
The trap: What happens when a contract defines both hello_world and hello-world, or a_b_c and a-b_c?
- If the first function registers
"hello-world"as a visible alias and the second function registers"hello-world"as its primary command name,clappanics at runtime with a duplicate subcommand/alias collision before--helpcan even render.
The fix: Run a two-pass pre-computation over the function list before building clap::Command nodes:
- Collect all canonical function names into a
HashSet<String>. - Count how many functions map to each kebab-case candidate
kebab_name = name.replace('_', "-"). - Only attach
.visible_alias(&kebab_name)whenkebab_name != name,!canonical_names.contains(&kebab_name), andalias_counts[&kebab_name] == 1.
3. Macro-Generated --quiet Flags Swallowing Fatal stderr Errors
When writing a terminal printer helper in Rust, a macro often generates both informational (info, check, log) and error (error, errorln) helpers:
macro_rules! create_print_functions {
($name:ident, $name_ln:ident, $icon:expr) => {
pub fn $name<T: Display + Sized>(&self, message: T) {
if !self.quiet {
eprint!(...);
}
}
};
}
The trap: Gating error and errorln behind if !self.quiet violates POSIX CLI conventions. When a script runs a command with --quiet (or -q) and the command fails, it exits with code 1 and zero output on stderr, leaving CI pipelines completely blind to what failed.
The fix: Decouple error and errorln from the !self.quiet guard so --quiet only silences non-essential progress output while fatal diagnostics always reach stderr.
Grab Our Autonomous Engineering & Security Kits
I am documenting and packaging every security validation, AST parser, and multi-agent execution tool we build into zero-dependency developer kits:
- AgentGuard Lite: Zero-Dependency Prompt & Tool-Call Firewall ($5 Starter Kit) — Fast Python AST & heuristic firewall for defending LLM tool calls against prompt injection, SSRF, and path traversal.
- Autonomous Security & Bug Bounty Recon Kit ($19) — Automated CORS misconfiguration scanner, API shadow-route enumerator, and CVSS 3.1 / Bugcrowd VRT report generator.
- Production Multi-Agent Architecture & Protocol Pack ($29) — Deterministic task-routing engine, SQLite WAL cross-agent memory bus, and sub-agent verification harness.
What's the nastiest silent truncation or CLI parser bug you've caught in production? Drop it in the comments!
Top comments (0)