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
use super::{Observer, ObserverHooks};
use crate::actions::Action;
use crate::requests::{Request, RequestId};
use crate::responses::{Response, Timed};
use crate::std_ext::tuple::Named;

use std::collections::HashMap;

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

const RESPONSE_OBSERVER_NAME: &str = "ResponseObserver";

/// observes the given implementor of implementor of [`Response`] and [`Timed`]
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ResponseObserver<R>
where
    R: Response,
{
    // satisfy Named trait
    name: &'static str,
    // satisfy ResponseExt trait
    response: R,
}

impl<R> Timed for ResponseObserver<R>
where
    R: Response + Timed,
{
    fn elapsed(&self) -> &std::time::Duration {
        self.response.elapsed()
    }
}

impl<R> ResponseObserver<R>
where
    R: Response + Default,
{
    /// create a new `ResponseObserver`
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// given an implementor of [`Response`], return a new `ResponseObserver`
    ///
    /// # Examples
    ///
    /// While the example below works, the normal use-case for this struct is to pass
    /// it, and any other [`Observers`] to the [`build_observers`] macro, and pass
    /// the result of that call to your chosen [`Fuzzer`] implementation.
    ///
    /// [`build_observers`]: crate::build_observers
    /// [`Fuzzer`]: crate::fuzzers::Fuzzer
    /// [`Observers`]: crate::observers::Observers
    ///
    /// ```
    /// # use http;
    /// # use feroxfuzz::responses::{Response, AsyncResponse};
    /// # use feroxfuzz::requests::Request;
    /// # use feroxfuzz::prelude::*;
    /// # use feroxfuzz::observers::ResponseObserver;
    /// # use tokio_test;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), FeroxFuzzError> {
    /// # tokio_test::block_on(async {
    /// // for testing, normal Response comes as a result of a sent request
    /// let reqwest_response = http::response::Builder::new().status(302).header("Location", "/somewhere").body("").unwrap();
    ///
    /// // should come from timing during the client's send function
    /// let elapsed = Duration::from_secs(1);  
    ///
    /// let response = AsyncResponse::try_from_reqwest_response(Request::default(), reqwest_response.into(), elapsed).await?;
    /// let observer = ResponseObserver::with_response(response);
    ///
    /// # Result::<(), FeroxFuzzError>::Ok(())
    /// # })
    /// # }
    /// ```
    pub const fn with_response(response: R) -> Self
    where
        R: Response,
    {
        Self {
            response,
            name: RESPONSE_OBSERVER_NAME,
        }
    }

    /// true if this `Response` is a well-formed HTTP redirect
    ///
    /// i.e. response code is 3XX and has a Location header
    ///
    /// # Examples
    ///
    /// ```
    /// # use http;
    /// # use feroxfuzz::responses::{Response, AsyncResponse};
    /// # use feroxfuzz::requests::Request;
    /// # use feroxfuzz::error::FeroxFuzzError;
    /// # use feroxfuzz::observers::ResponseObserver;
    /// # use tokio_test;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), FeroxFuzzError> {
    /// # tokio_test::block_on(async {
    /// // for testing, normal Response comes as a result of a sent request
    /// let reqwest_response = http::response::Builder::new().status(302).header("Location", "/somewhere").body("").unwrap();
    ///
    /// // should come from timing during the client's send function
    /// let elapsed = Duration::from_secs(1);  
    ///
    /// let response = AsyncResponse::try_from_reqwest_response(Request::default(), reqwest_response.into(), elapsed).await?;
    /// let observer: ResponseObserver<_> = response.into();
    ///
    /// assert_eq!(observer.is_redirect(), true);
    /// assert_eq!(observer.is_permanent_redirect(), false);
    /// # Result::<(), FeroxFuzzError>::Ok(())
    /// # })
    /// # }
    /// ```
    #[must_use]
    pub fn is_redirect(&self) -> bool {
        let is_redirect = (300..400).contains(&self.status_code());

        is_redirect
            && (self.headers().contains_key("Location") || self.headers().contains_key("location"))
    }

    /// true if this `Response` is one of the permanent versions of redirect
    ///
    /// i.e. response code is 301 (Moved Permanently) or 308 (Permanent Redirect)
    /// and has a Location header
    ///
    /// # Examples
    ///
    /// ```
    /// # use http;
    /// # use feroxfuzz::responses::{Response, AsyncResponse};
    /// # use feroxfuzz::observers::ResponseObserver;
    /// # use feroxfuzz::requests::Request;
    /// # use feroxfuzz::error::FeroxFuzzError;
    /// # use tokio_test;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), FeroxFuzzError> {
    /// # tokio_test::block_on(async {
    /// // for testing, normal Response comes as a result of a sent request
    /// let reqwest_response = http::response::Builder::new().status(308).header("Location", "/somewhere").body("").unwrap();
    ///
    /// // should come from timing during the client's send function
    /// let elapsed = Duration::from_secs(1);  
    ///
    /// let response = AsyncResponse::try_from_reqwest_response(Request::default(), reqwest_response.into(), elapsed).await?;
    /// let observer: ResponseObserver<_> = response.into();
    ///
    /// assert_eq!(observer.is_redirect(), true);
    /// assert_eq!(observer.is_permanent_redirect(), true);
    /// # Result::<(), FeroxFuzzError>::Ok(())
    /// # })
    /// # }
    /// ```
    #[must_use]
    pub fn is_permanent_redirect(&self) -> bool {
        let numeric_code = self.status_code();
        let is_permanent_redirect = numeric_code == 301 || numeric_code == 308;

        is_permanent_redirect
            && (self.headers().contains_key("Location") || self.headers().contains_key("location"))
    }

    /// determine whether the response is a directory
    ///
    /// handles 2xx and 3xx responses by either checking if the url ends with a / (2xx)
    /// or if the Location header is present and matches the base url + / (3xx)
    ///
    /// # Examples
    ///
    /// example code requires the **crate-feature** `reqwest`
    ///
    /// A 2xx status code, with a path ending in `/` is interpreted as a directory
    ///
    /// ```
    /// # use http;
    /// # use feroxfuzz::responses::{Response, AsyncResponse};
    /// # use feroxfuzz::requests::Request;
    /// # use feroxfuzz::error::FeroxFuzzError;
    /// # use feroxfuzz::observers::ResponseObserver;
    /// # use tokio_test;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), FeroxFuzzError> {
    /// # tokio_test::block_on(async {
    /// // for testing, normal Response comes as a result of a sent request
    /// let reqwest_response = http::response::Builder::new().status(200).body("derp").unwrap();
    ///
    /// // should come from timing during the client's send function
    /// let elapsed = Duration::from_secs(1);  
    ///
    /// let response = AsyncResponse::try_from_reqwest_response(Request::default(), reqwest_response.into(), elapsed).await?;
    /// let observer: ResponseObserver<_> = response.into();
    ///
    /// assert_eq!(observer.url().as_str(), "http://no.url.provided.local/");
    /// assert_eq!(observer.is_directory(), true);
    /// # Result::<(), FeroxFuzzError>::Ok(())
    /// # })
    /// # }
    /// ```
    ///
    /// A 3xx response with a location header that matches base url + /
    ///
    /// ```
    /// # use http;
    /// # use feroxfuzz::responses::{Response, AsyncResponse};
    /// # use feroxfuzz::observers::ResponseObserver;
    /// # use feroxfuzz::requests::Request;
    /// # use feroxfuzz::error::FeroxFuzzError;
    /// # use tokio_test;
    /// # use std::time::Duration;
    /// # fn main() -> Result<(), FeroxFuzzError> {
    /// # tokio_test::block_on(async {
    /// // for testing, normal Response comes as a result of a sent request
    /// let mut reqwest_response = http::response::Builder::new().status(301).header("Location", "/").body("").unwrap();
    ///
    /// // should come from timing during the client's send function
    /// let elapsed = Duration::from_secs(1);  
    ///
    /// let response = AsyncResponse::try_from_reqwest_response(Request::default(), reqwest_response.into(), elapsed).await?;
    /// let observer: ResponseObserver<_> = response.into();
    ///
    /// assert_eq!(observer.url().as_str(), "http://no.url.provided.local/");
    /// assert_eq!(observer.is_directory(), true);
    /// # Result::<(), FeroxFuzzError>::Ok(())
    /// # })
    /// # }
    /// ```
    pub fn is_directory(&self) -> bool {
        if self.status_code() >= 300 && self.status_code() < 400 {
            // status code is 3xx
            if let Some(location) = self.get_header_case_insensitive("location") {
                // and has a location header

                // get absolute redirect Url based on the already known base url
                if let Ok(abs_url) = self.url().join(&String::from_utf8_lossy(location)) {
                    let mut trailing_slash = self.url().as_str().to_string();

                    if !trailing_slash.ends_with('/') {
                        // only append a slash if not present already
                        trailing_slash.push('/');
                    }

                    if trailing_slash == abs_url.as_str() {
                        // if current response's Url + / == the absolute redirection
                        // location, we've found a directory
                        return true;
                    }
                }
            } else {
                // 3xx response without a Location header
                return false;
            }
        } else if self.status_code() >= 200 && self.status_code() < 300 {
            // status code is 2xx, need to check if it ends in /
            if self.url().as_str().ends_with('/') {
                return true;
            }
        }

        false
    }

    /// examine the response's url and grab the file's extension if one is available
    /// to be grabbed.
    #[allow(clippy::missing_panics_doc)]
    #[must_use]
    pub fn extension(&self) -> Option<&str> {
        // path_segments:
        //   Return None for cannot-be-a-base URLs.
        //   When Some is returned, the iterator always contains at least one string
        //     (which may be empty).
        //
        // meaning: the two unwraps here are fine, the worst outcome is an empty string
        let filename = self.url().path_segments().unwrap().next_back().unwrap();

        if !filename.is_empty() {
            // non-empty string, try to get extension
            let parts: Vec<_> = filename
                .split('.')
                // keep things like /.bash_history from becoming an extension
                .filter(|part| !part.is_empty())
                .collect();

            if parts.len() > 1 {
                // filename + at least one extension, i.e. whatever.js becomes ["whatever", "js"]
                return Some(parts.last().unwrap());
            }
        }

        None
    }

    // account for any case variations in header names
    fn get_header_case_insensitive(&self, header_name: &str) -> Option<&Vec<u8>> {
        let lowercase_name = header_name.to_lowercase();

        self.headers()
            .iter()
            .find(|(key, _)| key.to_lowercase() == lowercase_name)
            .map(|(_, value)| value)
    }
}

impl<R> Named for ResponseObserver<R>
where
    R: Response,
{
    fn name(&self) -> &str {
        self.name
    }
}

impl<R> Response for ResponseObserver<R>
where
    R: Response,
{
    /// get the `id` from the associated `Response`
    fn id(&self) -> RequestId {
        self.response.id()
    }

    /// get the `url` from the associated `Response`
    fn url(&self) -> &Url {
        self.response.url()
    }

    /// get the `status_code` from the associated `Response`
    fn status_code(&self) -> u16 {
        self.response.status_code()
    }
    /// get the `headers` from the associated `Response`
    fn headers(&self) -> &HashMap<String, Vec<u8>> {
        self.response.headers()
    }

    /// get the `body` from the associated `Response`
    fn body(&self) -> &[u8] {
        self.response.body()
    }

    /// get the `content_length` from the associated `Response`
    fn content_length(&self) -> usize {
        self.response.content_length()
    }

    /// get the `line_count` from the associated `Response`
    fn line_count(&self) -> usize {
        self.response.line_count()
    }

    /// get the `word_count` from the associated `Response`
    fn word_count(&self) -> usize {
        self.response.word_count()
    }

    /// get the original http request method used to generate the response
    fn method(&self) -> &str {
        self.response.method()
    }

    /// Get the [`Action`] to be taken as a result of this response
    fn action(&self) -> Option<&Action> {
        self.response.action()
    }

    fn request(&self) -> &Request {
        self.response.request()
    }
}

impl<R> Observer for ResponseObserver<R> where R: Response {}

impl<R> ObserverHooks<R> for ResponseObserver<R>
where
    R: Response,
{
    #[instrument(skip_all, fields(%self.name), level = "trace")]
    fn post_send_hook(&mut self, response: R) {
        self.response = response;
    }
}

impl<R> Default for ResponseObserver<R>
where
    R: Response + Default,
{
    fn default() -> Self {
        Self {
            name: RESPONSE_OBSERVER_NAME,
            response: R::default(),
        }
    }
}

cfg_if! {
    if #[cfg(feature = "async")] {
        use crate::responses::AsyncResponse;

        impl From<AsyncResponse> for ResponseObserver<AsyncResponse> {
            fn from(response: AsyncResponse) -> Self {
                Self::with_response(response)
            }
        }
    }
}

cfg_if! {
    if #[cfg(feature = "blocking")] {
        use crate::responses::BlockingResponse;

        impl From<BlockingResponse> for ResponseObserver<BlockingResponse> {
            fn from(response: BlockingResponse) -> Self {
                Self::with_response(response)
            }
        }
    }

}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error::FeroxFuzzError;
    use crate::responses::AsyncResponse;
    use http::response;
    use std::time::Duration;

    #[tokio::test]
    async fn test_get_header_case_insensitive() -> Result<(), FeroxFuzzError> {
        // Create a response with headers in different cases

        let reqwest_response = response::Builder::new()
            .status(200)
            .header("Content-Type", "text/html")
            .header("X-Custom-Header", "CustomValue")
            .header("accept-encoding", "gzip")
            .body("")
            .unwrap();

        let elapsed = Duration::from_secs(1);

        let response = AsyncResponse::try_from_reqwest_response(
            Request::default(),
            reqwest_response.into(),
            elapsed,
        )
        .await?;

        let observer = ResponseObserver::with_response(response);

        // Test exact case match
        assert_eq!(
            observer.get_header_case_insensitive("Content-Type"),
            Some(&b"text/html".to_vec())
        );

        // Test different case variations
        assert_eq!(
            observer.get_header_case_insensitive("content-type"),
            Some(&b"text/html".to_vec())
        );
        assert_eq!(
            observer.get_header_case_insensitive("CONTENT-TYPE"),
            Some(&b"text/html".to_vec())
        );
        assert_eq!(
            observer.get_header_case_insensitive("CoNtEnT-tYpE"),
            Some(&b"text/html".to_vec())
        );

        // Test another header with mixed case
        assert_eq!(
            observer.get_header_case_insensitive("x-custom-header"),
            Some(&b"CustomValue".to_vec())
        );
        assert_eq!(
            observer.get_header_case_insensitive("X-CUSTOM-HEADER"),
            Some(&b"CustomValue".to_vec())
        );

        // Test lowercase header
        assert_eq!(
            observer.get_header_case_insensitive("ACCEPT-ENCODING"),
            Some(&b"gzip".to_vec())
        );

        // Test header that doesn't exist
        assert_eq!(
            observer.get_header_case_insensitive("non-existent-header"),
            None
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_is_redirect_with_case_variations() -> Result<(), FeroxFuzzError> {
        // Create a response with Location header in lowercase

        let reqwest_response = response::Builder::new()
            .status(302)
            .header("location", "/redirect")
            .body("")
            .unwrap();

        let elapsed = Duration::from_secs(1);

        let response = AsyncResponse::try_from_reqwest_response(
            Request::default(),
            reqwest_response.into(),
            elapsed,
        )
        .await?;

        let observer_lowercase = ResponseObserver::with_response(response);

        assert!(observer_lowercase.is_redirect());

        // Create a response with Location header in uppercase
        let reqwest_response = response::Builder::new()
            .status(302)
            .header("LOCaTION", "/redirect")
            .body("")
            .unwrap();

        let response = AsyncResponse::try_from_reqwest_response(
            Request::default(),
            reqwest_response.into(),
            elapsed,
        )
        .await?;

        let observer_uppercase = ResponseObserver::with_response(response);

        // This should still pass with the current implementation, which only checks for "Location" and "location"
        // If we update is_redirect to use get_header_case_insensitive, this test would still pass
        assert!(observer_uppercase.is_redirect());

        Ok(())
    }
}