feroxfuzz 1.0.0-rc.13

Structure-aware, black box HTTP fuzzing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
use std::fmt::{self, Debug, Display, Formatter};
use std::fs::{self, File};
use std::io::{BufRead, BufReader};
use std::marker::PhantomData;
use std::ops::{Index, IndexMut};
use std::path::Path;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tracing::{error, instrument};

use super::{Corpus, CorpusType, Named};
use crate::corpora::typestate::{CorpusBuildState, HasItems, HasName, NoItems, NoName};
use crate::error::FeroxFuzzError;
use crate::input::Data;
use crate::std_ext::fmt::DisplayExt;
use crate::std_ext::ops::Len;
use crate::AsInner;

/// corpus created from the contents of an entire directory (non-recursive)
///
/// each file is read line-by-line. each line becomes a single entry in
/// the corpus
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::DirCorpus;
/// # use feroxfuzz::corpora::Corpus;
/// # use feroxfuzz::state::SharedState;
/// # use feroxfuzz::Len;
/// # use tempdir::TempDir;
/// # use std::fs::File;
/// # use std::io::Write;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // test setup:
/// // - temporary directory that contains 2 files
/// // - each file has 2 lines
/// let tmp_dir = TempDir::new("test-corpus")?;
///
/// let file_one = tmp_dir.path().join("test-file-one");
/// let mut tmp_file = File::create(file_one)?;
/// writeln!(tmp_file, "one")?;
/// writeln!(tmp_file, "two")?;
///
/// let file_two = tmp_dir.path().join("test-file-two");
/// tmp_file = File::create(file_two)?;
/// writeln!(tmp_file, "three")?;
/// writeln!(tmp_file, "four")?;
///
/// // create a Corpus from the given directory
/// let corpus = DirCorpus::from_directory(tmp_dir.path())?.name("corpus").build();
///
/// // resulting DirCorpus has 4 entries, one for each line found in the files above
/// assert_eq!(corpus.len(), 4);
/// # Ok(())
/// # }
/// ```
///
#[derive(Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct DirCorpus {
    items: Vec<Data>,
    corpus_name: String,
}

impl Corpus for DirCorpus {
    fn add(&mut self, value: Data) {
        self.items.push(value);
    }

    fn get(&self, index: usize) -> Option<&Data> {
        self.items.get(index)
    }

    #[inline]
    fn items(&self) -> &[Data] {
        &self.items
    }
}

/// internal helper to support multiple corpus directories
fn from_directory<P>(directory: P) -> Result<Vec<Data>, FeroxFuzzError>
where
    P: AsRef<Path>,
{
    let mut items = Vec::new();

    for entry in fs::read_dir(directory)? {
        let entry = entry?; // directory entry
        let path = entry.path(); // converted to a PathBuf

        let metadata = fs::metadata(&path)?;

        if metadata.is_file() {
            let file = File::open(&path).map_err(|source| {
                error!(
                    ?path,
                    "could not open file while populating the corpus: {}", source
                );

                FeroxFuzzError::CorpusFileOpenError {
                    source,
                    path: path.to_string_lossy().to_string(),
                }
            })?;

            let reader = BufReader::new(file);

            for line in reader.lines().map_while(Result::ok) {
                if line.is_empty() || line.starts_with('#') {
                    // skip empty lines and comments
                    continue;
                }

                // since the associated type `Item` must implement FromStr
                // we can call .parse() to convert it into the expected
                // type before pushing it onto the container. unwrap is safe here
                let associated_type = line.parse().unwrap();
                items.push(associated_type);
            }
        }
    }

    Ok(items)
}

impl DirCorpus {
    /// create a new/empty `DirCorpusBuilder`
    ///
    /// # Note
    ///
    /// `DirCorpusBuilder::build` can only be called after `DirCorpusBuilder::name` and
    /// `DirCorpusBuilder::directory` have been called.
    ///
    /// The [`DirCorpus::from_directory`] constructor can be used to immediately provide
    /// the corpus items, if desired.
    ///
    /// # Examples
    ///
    /// ```
    /// # use feroxfuzz::corpora::DirCorpus;
    /// # use feroxfuzz::prelude::*;
    /// # use tempdir::TempDir;
    /// # use std::fs::File;
    /// # use std::io::Write;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // test setup:
    /// // - 2 temporary directories that contain 1 file each
    /// // - each file has 2 lines
    /// let tmp_dir = TempDir::new("test-corpus-1").unwrap();
    /// let tmp_dir2 = TempDir::new("test-corpus-2").unwrap();
    ///
    /// let file_one = tmp_dir.path().join("test-file-one");
    /// let mut tmp_file = File::create(file_one).unwrap();
    /// writeln!(tmp_file, "one").unwrap();
    /// writeln!(tmp_file, "two").unwrap();
    ///
    /// let file_two = tmp_dir2.path().join("test-file-two");
    /// tmp_file = File::create(file_two).unwrap();
    /// writeln!(tmp_file, "three").unwrap();
    /// writeln!(tmp_file, "four").unwrap();
    ///
    /// let expected = vec!["one", "two", "three", "four"];
    ///
    /// // create a Corpus of Strings from the given directory
    /// let corpus = DirCorpus::new()
    ///     .directory(tmp_dir.path())?
    ///     .directory(tmp_dir2.path())?
    ///     .name("corpus")
    ///     .build();
    ///
    /// assert_eq!(corpus.items(), expected);
    /// # Ok(())
    /// # }
    /// ```
    #[must_use]
    #[inline]
    #[allow(clippy::new_ret_no_self)]
    pub const fn new() -> DirCorpusBuilder<NoItems, NoName> {
        DirCorpusBuilder {
            items: Vec::new(),
            corpus_name: None,
            _item_state: PhantomData,
            _name_state: PhantomData,
        }
    }

    /// create a new `DirCorpusBuilder` from the contents of the given `directory` (non-recursive)
    ///
    /// # Errors
    ///
    /// If this function encounters any form of I/O error, an error
    /// variant will be returned.
    ///
    /// # Note
    ///
    /// `DirCorpusBuilder::build` can only be called after `DirCorpusBuilder::name` and
    /// `DirCorpusBuilder::directory` have been called.
    ///
    /// This constructor can be used to immediately provide the items, if desired.
    ///
    /// # Examples
    ///
    /// ```
    /// # use feroxfuzz::corpora::DirCorpus;
    /// # use feroxfuzz::prelude::*;
    /// # use tempdir::TempDir;
    /// # use std::fs::File;
    /// # use std::io::Write;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// // test setup:
    /// // - temporary directory that contains 2 files
    /// // - each file has 2 lines
    /// let tmp_dir = TempDir::new("test-corpus")?;
    ///
    /// let file_one = tmp_dir.path().join("test-file-one");
    /// let mut tmp_file = File::create(file_one)?;
    /// writeln!(tmp_file, "one")?;
    /// writeln!(tmp_file, "two")?;
    ///
    /// let file_two = tmp_dir.path().join("test-file-two");
    /// tmp_file = File::create(file_two)?;
    /// writeln!(tmp_file, "three")?;
    /// writeln!(tmp_file, "four")?;
    ///
    /// // create a Corpus from the given directory
    /// let corpus = DirCorpus::from_directory(tmp_dir.path())?.name("corpus").build();
    ///
    /// // resulting DirCorpus has 4 entries, one for each line found in the files above
    /// assert_eq!(corpus.len(), 4);
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, level = "trace")]
    pub fn from_directory<P>(
        directory: P,
    ) -> Result<DirCorpusBuilder<HasItems, NoName>, FeroxFuzzError>
    where
        P: AsRef<Path>,
    {
        Ok(DirCorpusBuilder {
            items: from_directory(directory)?,
            corpus_name: None,
            _item_state: PhantomData,
            _name_state: PhantomData,
        })
    }

    /// get a reference to the inner collection of corpus items
    #[must_use]
    #[inline]
    pub fn items(&self) -> &[Data] {
        &self.items
    }

    /// get a mutable reference to the inner collection of corpus items
    #[must_use]
    #[inline]
    pub fn items_mut(&mut self) -> &mut [Data] {
        &mut self.items
    }

    /// Returns a mutable iterator over the items in the corpus.
    #[must_use]
    pub fn iter_mut(&mut self) -> <&mut [Data] as IntoIterator>::IntoIter {
        <&mut Self as IntoIterator>::into_iter(self)
    }

    /// Returns an iterator over the items in the corpus.
    #[must_use]
    pub fn iter(&self) -> <&[Data] as IntoIterator>::IntoIter {
        <&Self as IntoIterator>::into_iter(self)
    }
}

impl Named for DirCorpus {
    fn name(&self) -> &str {
        &self.corpus_name
    }
}

impl Len for DirCorpus {
    #[inline]
    fn len(&self) -> usize {
        self.items.len()
    }
}

impl AsInner for DirCorpus {
    type Type = Vec<Data>;

    fn inner(&self) -> &Self::Type {
        &self.items
    }
}

impl Display for DirCorpus {
    #[inline]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.display_top(3))
    }
}

impl Index<usize> for DirCorpus {
    type Output = Data;

    fn index(&self, index: usize) -> &Self::Output {
        &self.items()[index]
    }
}

impl IndexMut<usize> for DirCorpus {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.items_mut()[index]
    }
}

/// non-consuming mutable iterator over `DirCorpus`
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::DirCorpus;
/// # use feroxfuzz::prelude::*;
/// # use tempdir::TempDir;
/// # use std::fs::File;
/// # use std::io::Write;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // test setup:
/// // - temporary directory that contains 2 files
/// // - each file has 2 lines
/// let tmp_dir = TempDir::new("test-corpus")?;
///
/// let file_one = tmp_dir.path().join("test-file-one");
/// let mut tmp_file = File::create(file_one)?;
/// writeln!(tmp_file, "one")?;
/// writeln!(tmp_file, "two")?;
///
/// let file_two = tmp_dir.path().join("test-file-two");
/// tmp_file = File::create(file_two)?;
/// writeln!(tmp_file, "three")?;
/// writeln!(tmp_file, "four")?;
///
/// // the values we expect to procure during iteration
/// let expected = vec!["a", "a", "a", "a"];
///
/// // create a Corpus of Strings from the given directory
/// let mut corpus = DirCorpus::from_directory(tmp_dir.path())?.name("corpus").build();
///
/// for item in &mut corpus {
///     *item = "a".into();
/// }
///
/// assert_eq!(corpus.items(), expected);
/// # Ok(())
/// # }
/// ```
///
impl<'i> IntoIterator for &'i mut DirCorpus {
    /// the type of the elements being iterated over
    type Item = &'i mut Data;

    /// the kind of iterator we're turning `DirCorpus` into
    type IntoIter = <&'i mut [Data] as IntoIterator>::IntoIter;

    /// creates an iterator from `DirCorpus.items`
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.items.iter_mut()
    }
}

/// consuming iterator over `DirCorpus`
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::DirCorpus;
/// # use feroxfuzz::prelude::*;
/// # use tempdir::TempDir;
/// # use std::fs::File;
/// # use std::io::Write;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // test setup:
/// // - temporary directory that contains 2 files
/// // - each file has 2 lines
/// let tmp_dir = TempDir::new("test-corpus")?;
///
/// let file_one = tmp_dir.path().join("test-file-one");
/// let mut tmp_file = File::create(file_one)?;
/// writeln!(tmp_file, "one")?;
/// writeln!(tmp_file, "two")?;
///
/// let file_two = tmp_dir.path().join("test-file-two");
/// tmp_file = File::create(file_two)?;
/// writeln!(tmp_file, "three")?;
/// writeln!(tmp_file, "four")?;
///
/// // the values we expect to procure during iteration
/// let expected = vec!["one", "two", "three", "four"];
///
/// // create a Corpus of Strings from the given directory
/// let mut corpus = DirCorpus::from_directory(tmp_dir.path())?.name("corpus").build();
///
/// let mut gathered = vec![];
///
/// for item in corpus {
///     gathered.push(item);
/// }
///
/// for item in expected {
///    let data: Data = item.into();
///    assert!(gathered.contains(&data));
/// }
///
/// # Ok(())
/// # }
/// ```
///
impl IntoIterator for DirCorpus {
    /// the type of the elements being iterated over
    type Item = Data;

    /// the kind of iterator we're turning `DirCorpus` into
    type IntoIter = <Vec<Data> as IntoIterator>::IntoIter;

    /// creates an iterator from `DirCorpus.items`
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.items.into_iter()
    }
}

/// non-consuming iterator over `DirCorpus`
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::DirCorpus;
/// # use feroxfuzz::prelude::*;
/// # use tempdir::TempDir;
/// # use std::fs::File;
/// # use std::io::Write;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // test setup:
/// // - temporary directory that contains 2 files
/// // - each file has 2 lines
/// let tmp_dir = TempDir::new("test-corpus")?;
///
/// let file_one = tmp_dir.path().join("test-file-one");
/// let mut tmp_file = File::create(file_one)?;
/// writeln!(tmp_file, "one")?;
/// writeln!(tmp_file, "two")?;
///
/// let file_two = tmp_dir.path().join("test-file-two");
/// tmp_file = File::create(file_two)?;
/// writeln!(tmp_file, "three")?;
/// writeln!(tmp_file, "four")?;
///
/// // the values we expect to procure during iteration
/// let expected = vec!["one", "two", "three", "four"];
///
/// // create a Corpus of Strings from the given directory
/// let mut corpus = DirCorpus::from_directory(tmp_dir.path())?.name("corpus").build();
///
/// let mut gathered = vec![];
///
/// for item in &corpus {
///     gathered.push(item);
/// }
///
/// for item in expected {
///    let data: Data = item.into();
///    assert!(gathered.contains(&&data));
/// }
///
/// # Ok(())
/// # }
/// ```
impl<'i> IntoIterator for &'i DirCorpus {
    /// the type of the elements being iterated over
    type Item = &'i Data;

    /// the kind of iterator we're turning `DirCorpus` into
    type IntoIter = <&'i [Data] as IntoIterator>::IntoIter;

    /// creates an iterator from `DirCorpus.items`
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.items.iter()
    }
}

pub struct DirCorpusBuilder<IS, NS>
where
    IS: CorpusBuildState,
    NS: CorpusBuildState,
{
    items: Vec<Data>,
    corpus_name: Option<String>,
    _item_state: PhantomData<IS>,
    _name_state: PhantomData<NS>,
}

impl<IS> DirCorpusBuilder<IS, NoName>
where
    IS: CorpusBuildState,
{
    pub fn name(self, corpus_name: &str) -> DirCorpusBuilder<IS, HasName> {
        DirCorpusBuilder {
            items: self.items,
            corpus_name: Some(corpus_name.to_string()),
            _item_state: PhantomData,
            _name_state: PhantomData,
        }
    }
}

impl<IS, NS> DirCorpusBuilder<IS, NS>
where
    IS: CorpusBuildState,
    NS: CorpusBuildState,
{
    pub fn directory<P>(
        mut self,
        directory: P,
    ) -> Result<DirCorpusBuilder<HasItems, NS>, FeroxFuzzError>
    where
        P: AsRef<Path>,
    {
        let new_items = from_directory(directory)?;

        self.items.extend(new_items);

        Ok(DirCorpusBuilder {
            items: self.items,
            corpus_name: self.corpus_name,
            _item_state: PhantomData,
            _name_state: PhantomData,
        })
    }
}

impl DirCorpusBuilder<HasItems, HasName> {
    pub fn build(self) -> CorpusType {
        CorpusType::Dir(DirCorpus {
            items: self.items,
            corpus_name: self.corpus_name.unwrap(),
        })
    }
}