#[cfg(feature = "import-json")]
pub mod json;
#[cfg(feature = "import-nmap")]
pub mod nmap;
use std::io::BufRead;
use std::path::Path;
use crate::import::{ImportError, ImportLimits};
use crate::scanner::report::ScanReport;
pub trait ReportReader {
fn read(&self, input: &mut dyn BufRead) -> Result<ScanReport, ImportError>;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct ReportOptions {
pub limits: ImportLimits,
}
impl ReportOptions {
pub fn new() -> Self {
Self::default()
}
pub fn with_limits(mut self, limits: ImportLimits) -> Self {
self.limits = limits;
self
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ReportFormat {
#[cfg(feature = "import-json")]
Json,
#[cfg(feature = "import-nmap")]
Nmap,
}
impl ReportFormat {
pub fn from_extension(extension: &str) -> Option<Self> {
match extension
.trim_start_matches('.')
.to_ascii_lowercase()
.as_str()
{
#[cfg(feature = "import-json")]
"json" => Some(ReportFormat::Json),
#[cfg(feature = "import-nmap")]
"xml" => Some(ReportFormat::Nmap),
_ => None,
}
}
pub fn from_path(path: &Path) -> Option<Self> {
path.extension()
.and_then(|extension| extension.to_str())
.and_then(Self::from_extension)
}
pub fn sniff(input: &mut dyn BufRead) -> Result<Self, ImportError> {
let available = input.fill_buf()?;
let first = available.iter().find(|byte| !byte.is_ascii_whitespace());
match first {
#[cfg(feature = "import-json")]
Some(b'{') => Ok(ReportFormat::Json),
#[cfg(feature = "import-nmap")]
Some(b'<') => Ok(ReportFormat::Nmap),
_ => Err(ImportError::Malformed {
format: "report",
origin: crate::import::Origin::unknown(),
message: "the input begins as neither a JSON document nor an XML one".to_string(),
}),
}
}
pub fn resolve(path: Option<&Path>, input: &mut dyn BufRead) -> Result<Self, ImportError> {
match path.and_then(Self::from_path) {
Some(format) => Ok(format),
None => Self::sniff(input),
}
}
pub fn read(
self,
input: &mut dyn BufRead,
options: ReportOptions,
) -> Result<ScanReport, ImportError> {
match self {
#[cfg(feature = "import-json")]
ReportFormat::Json => json::JsonReportReader::new(options).read(input),
#[cfg(feature = "import-nmap")]
ReportFormat::Nmap => nmap::NmapXmlReportReader::new(options).read(input),
}
}
}
#[cfg(all(test, feature = "import-json", feature = "import-nmap"))]
mod tests {
use std::io::Cursor;
use std::path::Path;
use super::*;
#[test]
fn an_extension_names_the_format() {
assert_eq!(
ReportFormat::from_path(Path::new("engagement/scan.xml")),
Some(ReportFormat::Nmap)
);
assert_eq!(
ReportFormat::from_path(Path::new("scan.JSON")),
Some(ReportFormat::Json)
);
assert_eq!(ReportFormat::from_path(Path::new("scan.txt")), None);
}
#[test]
fn the_first_byte_names_the_format_when_nothing_else_does() {
let mut json = Cursor::new(b" {\"schema_version\":1}".as_slice());
assert_eq!(ReportFormat::sniff(&mut json).unwrap(), ReportFormat::Json);
let mut xml = Cursor::new(b"<?xml version=\"1.0\"?><nmaprun/>".as_slice());
assert_eq!(ReportFormat::sniff(&mut xml).unwrap(), ReportFormat::Nmap);
let mut neither = Cursor::new(b"192.168.0.1\n".as_slice());
assert!(ReportFormat::sniff(&mut neither).is_err());
}
#[test]
fn sniffing_leaves_the_input_where_it_found_it() {
let document = b"<?xml version=\"1.0\"?><nmaprun/>";
let mut input = Cursor::new(document.as_slice());
let format = ReportFormat::sniff(&mut input).unwrap();
let report = format.read(&mut input, ReportOptions::new());
assert!(
report.is_ok(),
"a sniff must not consume the bytes the reader needs: {report:?}"
);
}
#[test]
fn an_extension_outranks_what_the_bytes_look_like() {
let mut input = Cursor::new(b"{}".as_slice());
assert_eq!(
ReportFormat::resolve(Some(Path::new("scan.xml")), &mut input).unwrap(),
ReportFormat::Nmap
);
}
}