Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuzz: add harness for filters and escape #1069

Merged
merged 2 commits into from
Jun 18, 2024
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ members = [
"askama_warp",
"testing",
]

exclude = ["fuzz"]

resolver = "2"

default-members = [
Expand Down
27 changes: 0 additions & 27 deletions askama_parser/fuzz/Cargo.toml

This file was deleted.

File renamed without changes.
30 changes: 30 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "askama-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
arbitrary = "1.3.2"
askama = { path = "../askama" }
askama_parser = { path = "../askama_parser" }
askama_escape = { path = "../askama_escape" }

[profile.release]
debug = 1

[[bin]]
name = "askama_parser-fuzz_parser"
path = "fuzz_targets/fuzz_parser.rs"
test = false
doc = false

[[bin]]
name = "fuzz_filters"
path = "fuzz_targets/fuzz_filters.rs"
test = false
doc = false
File renamed without changes.
74 changes: 74 additions & 0 deletions fuzz/fuzz_targets/fuzz_filters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![no_main]
use arbitrary::{Arbitrary, Unstructured};
use askama::filters::*;
use askama_escape::Html;
use libfuzzer_sys::fuzz_target;
use std::str::{self};

macro_rules! fuzz {
($name: ident, $func: ident) => {
fn $name(data: &[u8]) {
if let Ok(data) = str::from_utf8(data) {
if let Ok(d) = $func(data) {
let _ = d.to_string();
}
}
}
};
($name: ident, $func: ident, $arg_type: ty) => {
fn $name(data: &[u8]) {
if let Some(adata) = get_arbitrary_data::<$arg_type>(data) {
if let Ok(sdata) = str::from_utf8(data) {
if let Ok(d) = $func(sdata, adata) {
let _ = d.to_string();
}
}
}
}
};
}

fn get_arbitrary_data<'a, T>(data: &'a [u8]) -> Option<T>
where
T: Arbitrary<'a>,
{
T::arbitrary(&mut Unstructured::new(data)).ok()
}

fuzz!(fuzz_urlencode, urlencode);
fuzz!(fuzz_urlencode_strict, urlencode_strict);
fuzz!(fuzz_linebreaks, linebreaks);
fuzz!(fuzz_paragraphbreaks, paragraphbreaks);
fuzz!(fuzz_trim, trim);
fuzz!(fuzz_title, title);
fuzz!(fuzz_capitalize, capitalize);
fuzz!(fuzz_truncate, truncate, usize);
fuzz!(fuzz_indent, indent, usize);
fuzz!(fuzz_center, center, usize);

fn fuzz_escape(data: &[u8]) {
if let Ok(data) = str::from_utf8(data) {
let _ = askama_escape::escape(data, Html);
}
}

fuzz_target!(|data: &[u8]| {
if data.is_empty() {
return;
}
let idx = data[0] % 11;
let data = &data[1..];
match idx {
0 => fuzz_urlencode(data),
1 => fuzz_urlencode_strict(data),
2 => fuzz_linebreaks(data),
3 => fuzz_paragraphbreaks(data),
4 => fuzz_trim(data),
5 => fuzz_title(data),
6 => fuzz_capitalize(data),
7 => fuzz_truncate(data),
8 => fuzz_indent(data),
9 => fuzz_escape(data),
_ => fuzz_center(data),
}
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![no_main]
use askama_parser::*;
use libfuzzer_sys::fuzz_target;
use std::str;

fuzz_target!(|data: &[u8]| {
// fuzzed code goes here
if let Ok(data) = str::from_utf8(data) {
if let Ok(_) = Ast::from_str(data, None, &Syntax::default()) {}
if let Ok(data) = std::str::from_utf8(data) {
let _ = Ast::from_str(data, None, &Syntax::default()).is_ok();
}
});