From 534cbe8fe189c1c9258d6d3fb18334f1fe99626e Mon Sep 17 00:00:00 2001 From: epi Date: Sun, 3 Oct 2021 14:17:28 -0500 Subject: [PATCH] added a few tests --- src/utils.rs | 42 +++++++++++++++++++++++++ tests/test_deny_list.rs | 70 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/src/utils.rs b/src/utils.rs index 0ecd315..82c2b17 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -712,4 +712,46 @@ mod tests { assert!(!should_deny_url(&tested_url, handles).unwrap()); } + + #[test] + /// provide a denier where the tested url is matched against a regular expression in the path + /// of the url + fn should_deny_url_blocks_urls_based_on_regex_in_path() { + let scan_url = "https://testdomain.com/"; + let deny_pattern = "/deni.*"; + let tested_url = Url::parse("https://testdomain.com/denied/").unwrap(); + + let scans = Arc::new(FeroxScans::default()); + scans.add_directory_scan(scan_url, ScanOrder::Initial); + + let mut config = Configuration::new().unwrap(); + config.regex_denylist = vec![Regex::new(deny_pattern).unwrap()]; + let config = Arc::new(config); + + let handles = Arc::new(Handles::for_testing(Some(scans), Some(config)).0); + + assert!(should_deny_url(&tested_url, handles).unwrap()); + } + + #[test] + /// provide a denier where the tested url is matched against a regular expression in the scheme + /// of the url + fn should_deny_url_blocks_urls_based_on_regex_in_scheme() { + let scan_url = "https://testdomain.com/"; + let deny_pattern = "http:"; + let tested_http_url = Url::parse("http://testdomain.com/denied/").unwrap(); + let tested_https_url = Url::parse("https://testdomain.com/denied/").unwrap(); + + let scans = Arc::new(FeroxScans::default()); + scans.add_directory_scan(scan_url, ScanOrder::Initial); + + let mut config = Configuration::new().unwrap(); + config.regex_denylist = vec![Regex::new(deny_pattern).unwrap()]; + let config = Arc::new(config); + + let handles = Arc::new(Handles::for_testing(Some(scans), Some(config)).0); + + assert!(!should_deny_url(&tested_https_url, handles.clone()).unwrap()); + assert!(should_deny_url(&tested_http_url, handles).unwrap()); + } } diff --git a/tests/test_deny_list.rs b/tests/test_deny_list.rs index 1803c69..7c23116 100644 --- a/tests/test_deny_list.rs +++ b/tests/test_deny_list.rs @@ -210,3 +210,73 @@ fn deny_list_works_during_recursion_with_inverted_parents() { teardown_tmp_directory(tmp_dir); } + +#[test] +/// test that a regex that prevents the base url from being scanned results in an early exit +fn deny_list_prevents_regex_that_denies_base_url() { + let srv = MockServer::start(); + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist").unwrap(); + + let mock = srv.mock(|when, then| { + when.method(GET).path("/LICENSE"); + then.status(200).body("this is a test"); + }); + + let cmd = Command::cargo_bin("feroxbuster") + .unwrap() + .arg("--url") + .arg(srv.url("/")) + .arg("--wordlist") + .arg(file.as_os_str()) + .arg("--dont-scan") + .arg("/") + .unwrap(); + + teardown_tmp_directory(tmp_dir); + + let err_msg = format!( + "Could not determine initial targets: The regex '/' matches {}/; the scan will never start", + srv.base_url() + ); + cmd.assert() + .success() + .stderr(predicate::str::contains(err_msg)); + + assert_eq!(mock.hits(), 0); +} + +#[test] +/// test that a url that prevents the base url from being scanned results in an early exit +fn deny_list_prevents_url_that_denies_base_url() { + let srv = MockServer::start(); + let (tmp_dir, file) = setup_tmp_directory(&["LICENSE".to_string()], "wordlist").unwrap(); + + let mock = srv.mock(|when, then| { + when.method(GET).path("/LICENSE"); + then.status(200).body("this is a test"); + }); + + let cmd = Command::cargo_bin("feroxbuster") + .unwrap() + .arg("--url") + .arg(srv.url("/")) + .arg("--wordlist") + .arg(file.as_os_str()) + .arg("--dont-scan") + .arg(srv.base_url()) + .unwrap(); + + teardown_tmp_directory(tmp_dir); + + let err_msg = format!( + "Could not determine initial targets: The url '{}/' matches {}/; the scan will never start", + srv.base_url(), + srv.base_url() + ); + + cmd.assert() + .success() + .stderr(predicate::str::contains(err_msg)); + + assert_eq!(mock.hits(), 0); +}