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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use std::collections::HashSet;
use std::fmt::{self, Debug, Display, Formatter};
use std::fs::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, NotUnique, Unique,
};
use crate::error::FeroxFuzzError;
use crate::input::Data;
use crate::std_ext::convert::AsInner;
use crate::std_ext::fmt::DisplayExt;
use crate::std_ext::ops::Len;

/// generic container representing a wordlist
///
/// # Examples
///
/// ## Normal wordlist
///
/// items may repeat in a normal wordlist
///
/// ```
/// # use feroxfuzz::corpora::Wordlist;
/// # use feroxfuzz::corpora::Corpus;
/// # use feroxfuzz::state::SharedState;
/// # use feroxfuzz::Len;
/// let wordlist = Wordlist::new().word("1").word("2").name("words").build();
///
/// assert_eq!(wordlist.len(), 2);
/// ```
///
/// ## Unique wordlist
///
/// ### Note
///
/// There are two primary considerations when choosing to use a unique wordlist:
/// - A hashset is used to store the unique items alongside a Vec, so the memory footprint will be doubled
/// - The original order of the items will be lost
///
/// ```
/// # use feroxfuzz::corpora::Wordlist;
/// # use feroxfuzz::corpora::Corpus;
/// # use feroxfuzz::state::SharedState;
/// # use feroxfuzz::Len;
/// let wordlist = Wordlist::new()
///    .words(["one", "two", "three", "one", "two", "three"])
///    .name("words")
///    .unique()
///    .build();
///
/// assert_eq!(wordlist.len(), 3);
/// ```
///
#[derive(Clone, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Wordlist {
    items: Vec<Data>,
    unique_items: Option<HashSet<Data>>,
    corpus_name: String,
}

/// non-consuming iterator over Wordlist
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::Wordlist;
/// # use feroxfuzz::state::SharedState;
/// let expected = vec!["1", "2", "3"];
/// let wordlist = Wordlist::with_words(expected.clone()).name("words").build();
///
/// let mut gathered = vec![];
///
/// for i in &wordlist {
///     gathered.push(i.clone());
/// }
///
/// assert_eq!(gathered, expected);
/// ```
impl<'i> IntoIterator for &'i Wordlist {
    /// the type of the elements being iterated over
    type Item = <&'i [Data] as IntoIterator>::Item;

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

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

/// non-consuming mutable iterator over Wordlist
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::{Wordlist, Corpus};
/// # use feroxfuzz::state::SharedState;
/// let mut wordlist = Wordlist::new().words(["1", "2", "3"]).name("words").build();
///
/// for i in &mut wordlist {
///     *i = "4".into();
/// }
///
/// assert_eq!(wordlist.items(), &["4", "4", "4"]);
/// ```
///
impl<'i> IntoIterator for &'i mut Wordlist {
    /// the type of the elements being iterated over
    type Item = <&'i mut [Data] as IntoIterator>::Item;

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

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

/// consuming iterator over Wordlist
///
/// # Examples
///
/// ```
/// # use feroxfuzz::corpora::Wordlist;
/// # use feroxfuzz::state::SharedState;
/// let expected = vec!["1", "2", "3"];
/// let wordlist = Wordlist::with_words(expected.clone()).name("words").build();
///
/// let mut gathered = vec![];
///
/// for i in wordlist {
///     gathered.push(i.clone());
/// }
///
/// assert_eq!(gathered, expected);
/// ```
///
impl IntoIterator for Wordlist {
    /// the type of the elements being iterated over
    type Item = <Vec<Data> as IntoIterator>::Item;

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

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

/// general `Wordlist` implementation
impl Wordlist {
    /// create a default (empty) `WordlistBuilder`
    ///
    /// # Note
    ///
    /// `WordlistBuilder::build` can only be called after `WordlistBuilder::name` and
    /// `WordlistBuilder::word` or `WordlistBuilder::words` have been called.
    ///
    /// There are other constructors to immediately provide the corpus items, if desired.
    ///
    /// - [`Wordlist::with_words`]
    /// - [`Wordlist::from_file`]
    ///
    /// # Examples
    ///
    /// ```
    /// # use feroxfuzz::corpora::Wordlist;
    /// let wordlist = Wordlist::new().word("1").name("smol").build();
    /// ```
    #[must_use]
    #[allow(clippy::new_ret_no_self)]
    pub const fn new() -> WordlistBuilder<NoItems, NoName, NotUnique> {
        WordlistBuilder {
            items: Vec::new(),
            corpus_name: None,
            unique_items: None,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        }
    }

    /// given a collection of items, create a new `WordlistBuilder`
    ///
    /// # Note
    ///
    /// `WordlistBuilder::build` can only be called after `WordlistBuilder::name` and
    /// `WordlistBuilder::word` or `WordlistBuilder::words` have been called.
    ///
    /// In addtion to this function, the [`Wordlist::from_file`] constructor can be used
    /// to immediately provide the corpus items, if desired.
    ///
    /// # Examples
    ///
    /// ```
    /// # use feroxfuzz::corpora::Wordlist;
    /// let wordlist = Wordlist::with_words(["1", "2", "3"]).name("words").build();
    /// ```
    #[inline]
    pub fn with_words<I, T>(words: I) -> WordlistBuilder<HasItems, NoName, NotUnique>
    where
        Data: From<T>,
        I: IntoIterator<Item = T>,
    {
        WordlistBuilder {
            items: words.into_iter().map(Data::from).collect(),
            corpus_name: None,
            unique_items: None,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        }
    }

    /// Populates `Wordlist` with test cases out of the file with the path given by `file_path`
    ///
    /// # Note
    ///
    /// `WordlistBuilder::build` can only be called after `WordlistBuilder::name` and
    /// `WordlistBuilder::word` or `WordlistBuilder::words` have been called.
    ///
    /// In addtion to this function, the [`Wordlist::with_words`] constructor can be used
    /// to immediately provide the corpus items, if desired.
    ///
    /// # Errors
    ///
    /// If this function encounters any form of I/O error, an error
    /// variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// # use std::fs;
    /// # use feroxfuzz::corpora::Wordlist;
    /// # use feroxfuzz::prelude::*;
    /// # use std::str::FromStr;
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let file_name = "smol-wordlist.txt";
    /// let words = "one\ntwo\n\n\n#three\nfour\n";
    /// fs::write(file_name, words);
    ///
    /// let wordlist = Wordlist::from_file(file_name)?.name("words").build();
    ///
    /// fs::remove_file(file_name);
    ///
    /// assert_eq!(wordlist.len(), 3);
    /// assert_eq!(wordlist.items(), &["one", "two", "four"]);
    /// # Ok(())
    /// # }
    /// ```
    #[instrument(skip_all, level = "trace")]
    pub fn from_file<P>(
        file_path: P,
    ) -> Result<WordlistBuilder<HasItems, NoName, NotUnique>, FeroxFuzzError>
    where
        P: AsRef<Path>,
        Self: Corpus,
    {
        let file = File::open(&file_path).map_err(|source| {
            error!(
                file = file_path.as_ref().to_string_lossy().to_string(),
                "could not open file while populating the corpus: {}", source
            );

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

        let reader = BufReader::new(file);

        let mut items = Vec::new();

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

            items.push(line.into());
        }

        Ok(WordlistBuilder {
            items,
            unique_items: None,
            corpus_name: None,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        })
    }

    /// get mutable reference to inner `words` collection
    #[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 AsInner for Wordlist {
    type Type = Vec<Data>;

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

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

/// `Corpus` implementation for `Wordlist` with an inner `Vec`
impl Corpus for Wordlist {
    #[inline]
    fn add(&mut self, value: Data) {
        if let Some(ref mut unique_items) = self.unique_items {
            // if unique_items is Some, then we are only adding unique items, meaning
            // we can return early if the item is already in the set
            if unique_items.contains(&value) {
                return;
            }

            // item is not in the set, so we can insert it
            unique_items.insert(value.clone());
        }

        // if we made it here, then we know that either we don't care about unique items
        // or the item is unique, so, either way, we can push it onto the vector
        self.items.push(value);
    }

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

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

impl Named for Wordlist {
    #[inline]
    fn name(&self) -> &str {
        &self.corpus_name
    }
}

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

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

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

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

pub struct WordlistBuilder<ItemState, NameState, UniqueNess>
where
    ItemState: CorpusBuildState,
    NameState: CorpusBuildState,
    UniqueNess: CorpusBuildState,
{
    items: Vec<Data>,
    unique_items: Option<HashSet<Data>>,
    corpus_name: Option<String>,
    _item_state: PhantomData<ItemState>,
    _name_state: PhantomData<NameState>,
    _unique_state: PhantomData<UniqueNess>,
}

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

impl<ItemState, NameState> WordlistBuilder<ItemState, NameState, NotUnique>
where
    ItemState: CorpusBuildState,
    NameState: CorpusBuildState,
{
    // false positive; clippy thinks this should be a const fn, but it can't be
    #[allow(clippy::missing_const_for_fn)]
    pub fn unique(self) -> WordlistBuilder<ItemState, NameState, Unique> {
        WordlistBuilder {
            items: self.items,
            unique_items: self.unique_items,
            corpus_name: self.corpus_name,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        }
    }
}

impl<ItemState, NameState, UniqueNess> WordlistBuilder<ItemState, NameState, UniqueNess>
where
    ItemState: CorpusBuildState,
    NameState: CorpusBuildState,
    UniqueNess: CorpusBuildState,
{
    pub fn word<T>(mut self, word: T) -> WordlistBuilder<HasItems, NameState, UniqueNess>
    where
        Data: From<T>,
    {
        self.items.push(word.into());

        WordlistBuilder {
            items: self.items,
            unique_items: self.unique_items,
            corpus_name: self.corpus_name,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        }
    }

    pub fn words<I, T>(mut self, words: I) -> WordlistBuilder<HasItems, NameState, UniqueNess>
    where
        Data: From<T>,
        I: IntoIterator<Item = T>,
    {
        self.items.extend(words.into_iter().map(Data::from));

        WordlistBuilder {
            items: self.items,
            unique_items: self.unique_items,
            corpus_name: self.corpus_name,
            _item_state: PhantomData,
            _name_state: PhantomData,
            _unique_state: PhantomData,
        }
    }
}

impl WordlistBuilder<HasItems, HasName, Unique> {
    pub fn build(mut self) -> CorpusType {
        // remove duplicates from the vector
        self.items.sort_unstable();
        self.items.dedup();

        let mut unique_items = HashSet::with_capacity(self.items.len());
        unique_items.extend(self.items.iter().cloned());

        CorpusType::Wordlist(Wordlist {
            items: self.items,
            unique_items: Some(unique_items),
            corpus_name: self.corpus_name.unwrap(),
        })
    }
}

impl WordlistBuilder<HasItems, HasName, NotUnique> {
    pub fn build(self) -> CorpusType {
        CorpusType::Wordlist(Wordlist {
            items: self.items,
            unique_items: None,
            corpus_name: self.corpus_name.unwrap(),
        })
    }
}

impl WordlistBuilder<NoItems, HasName, Unique> {
    pub fn build(self) -> CorpusType {
        CorpusType::Wordlist(Wordlist {
            items: Vec::new(),
            unique_items: Some(HashSet::new()),
            corpus_name: self.corpus_name.unwrap(),
        })
    }
}

impl WordlistBuilder<NoItems, HasName, NotUnique> {
    pub fn build(self) -> CorpusType {
        CorpusType::Wordlist(Wordlist {
            items: Vec::new(),
            unique_items: None,
            corpus_name: self.corpus_name.unwrap(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wordlist_builder_with_single_word() {
        let wordlist = Wordlist::new()
            .word("one")
            .word("two")
            .word("three")
            .name("words")
            .words(["four", "five", "six"])
            .build();

        assert_eq!(wordlist.len(), 6);
        assert_eq!(
            wordlist.items(),
            &["one", "two", "three", "four", "five", "six"]
        );
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_wordlist_builder_with_both_word_methods() {
        let wordlist = Wordlist::new()
            .words(["one", "two", "three"])
            .word("four")
            .name("words")
            .build();

        assert_eq!(wordlist.len(), 4);
        assert_eq!(wordlist.items(), &["one", "two", "three", "four"]);
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_wordlist_builder_with_name_first() {
        let wordlist = Wordlist::new()
            .name("words")
            .words(["one", "two", "three"])
            .word("four")
            .build();

        assert_eq!(wordlist.len(), 4);
        assert_eq!(wordlist.items(), &["one", "two", "three", "four"]);
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_wordlist_with_unique_items_first() {
        let wordlist = Wordlist::new()
            .words(["one", "two", "three", "one", "two", "three"])
            .name("words")
            .unique()
            .build();

        assert_eq!(wordlist.len(), 3);
        for item in wordlist.items() {
            assert!([Data::from("one"), Data::from("two"), Data::from("three")].contains(item));
        }
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_wordlist_with_unique_items_second() {
        let wordlist = Wordlist::new()
            .words(["one", "two", "three", "one", "two", "three"])
            .unique()
            .name("words")
            .build();

        assert_eq!(wordlist.len(), 3);
        for item in wordlist.items() {
            assert!([Data::from("one"), Data::from("two"), Data::from("three")].contains(item));
        }
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_wordlist_with_unique_items_last() {
        let wordlist = Wordlist::new()
            .words(["one", "two", "three", "one", "two", "three"])
            .name("words")
            .unique()
            .build();

        assert_eq!(wordlist.len(), 3);
        for item in wordlist.items() {
            assert!([Data::from("one"), Data::from("two"), Data::from("three")].contains(item));
        }
        assert_eq!(wordlist.name(), "words");
    }

    #[test]
    fn test_unique_wordlist_remains_unique_after_using_corpus_add() {
        let mut wordlist = Wordlist::new()
            .words(["one", "two"])
            .name("words")
            .unique()
            .build();

        wordlist.add("one".into());
        wordlist.add("two".into());
        wordlist.add("three".into());
        wordlist.add("three".into());

        assert_eq!(wordlist.len(), 3);
        for item in wordlist.items() {
            assert!([Data::from("one"), Data::from("two"), Data::from("three"),].contains(item));
        }
        assert_eq!(wordlist.name(), "words");
    }
}