
// serde_vuln.rs - Deserializes an enum from user JSON without validation
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Deserialize, Debug)]
enum AdminAction {
ReadLogs,
DeleteUser(String),
CreateUser(String),
}
#[derive(Deserialize, Debug)]
struct Command {
action: AdminAction,
}
fn main() {
let user_input = r#"{"action": {"DeleteUser": "admin"}}"#;
let cmd: Command = serde_json::from_str(user_input).unwrap();
println!("Executing: {:?}", cmd.action);
// Could delete admin if permissions not checked!
}
A Rust application using serde deserializes untrusted JSON into an enum without validating that the caller is authorized for the variant. An attacker can inject a different enum variant (e.g., DeleteUser) and trigger unintended actions.
Compile and run the Rust program:
cargo add serde serde_json
rustc serde_vuln.rs
./serde_vuln
It executes the DeleteUser variant without authorization.