Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.3.1"
serde_json = "1.0"

[dependencies.jtd]
path = ".."
Expand All @@ -23,3 +24,7 @@ members = ["."]
[[bin]]
name = "serde_schema_try_into"
path = "fuzz_targets/serde_schema_try_into.rs"

[[bin]]
name = "validate"
path = "fuzz_targets/validate.rs"
20 changes: 20 additions & 0 deletions fuzz/fuzz_targets/validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use serde_json;

fuzz_target!(|schema_and_instance: (jtd::schema::Schema, Vec<u8>)| {
let validator = jtd::validator::Validator {
max_errors: None,
max_depth: None,
};

// We're only interested in fuzzing against valid schemas.
if schema_and_instance.0.validate().is_err() {
return;
}

if let Ok(instance) = serde_json::from_slice(&schema_and_instance.1) {
let _ = validator.validate(&schema_and_instance.0, &instance);
}
});
9 changes: 9 additions & 0 deletions src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashSet;
use std::str::FromStr;

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum Form {
Empty,
Ref(Ref),
Expand All @@ -22,18 +23,21 @@ impl Default for Form {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Ref {
pub nullable: bool,
pub definition: String,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Type {
pub nullable: bool,
pub type_value: TypeValue,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub enum TypeValue {
Boolean,
Float32,
Expand Down Expand Up @@ -70,18 +74,21 @@ impl FromStr for TypeValue {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Enum {
pub nullable: bool,
pub values: HashSet<String>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Elements {
pub nullable: bool,
pub schema: Box<Schema>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Properties {
pub nullable: bool,
pub required: HashMap<String, Schema>,
Expand All @@ -91,12 +98,14 @@ pub struct Properties {
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Values {
pub nullable: bool,
pub schema: Box<Schema>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "fuzz", derive(arbitrary::Arbitrary))]
pub struct Discriminator {
pub nullable: bool,
pub discriminator: String,
Expand Down
17 changes: 17 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ pub struct Schema {
pub metadata: HashMap<String, Value>,
}

#[cfg(feature = "fuzz")]
impl arbitrary::Arbitrary for Schema {
fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
Ok(Schema {
definitions: arbitrary::Arbitrary::arbitrary(u)?,
form: arbitrary::Arbitrary::arbitrary(u)?,

// serde_json::Value does not derive Arbitrary. That's ok, because
// for the fuzz tests we're doing, we don't really care about
// manipulating arbitrary JSON values.
//
// So we'll always have metadata be None.
metadata: HashMap::new(),
})
}
}

#[derive(Debug, PartialEq)]
pub enum SerdeConvertError {
InvalidForm,
Expand Down