Skip to content

Commit

Permalink
feat: refactor reports module to reports_xss, add JSON output support…
Browse files Browse the repository at this point in the history
… and enhance error handling
  • Loading branch information
Kremilly committed Dec 10, 2024
1 parent da3f889 commit e517bed
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ dumpsync1.yml
# Reports files
*.csv
*.pdf
*.json
*.html
*.xlsx
2 changes: 1 addition & 1 deletion src/plugins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod reports;
pub mod reports_xss;
pub mod scan_xss;
29 changes: 0 additions & 29 deletions src/plugins/reports.rs

This file was deleted.

58 changes: 58 additions & 0 deletions src/plugins/reports_xss.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use csv::Writer;
use std::fs::File;
use serde::Serialize;
use std::error::Error;
use serde_json::to_writer_pretty;

use crate::ui::scan_alerts::ScanAlerts;

#[derive(Serialize)]
struct Detection {
table: String,
row_index: usize,
column: String,
value: String,
}

pub struct ReportsXSS;

impl ReportsXSS {

pub fn csv(&self, detections: Vec<(String, usize, String, String)>, output_path: &str) -> Result<(), Box<dyn Error>> {
let mut writer = Writer::from_path(output_path)?;
writer.write_record(&["Table", "Row Index", "Column", "Value"])?;

for (table, row_index, column, value) in detections {
writer.write_record(&[
table,
row_index.to_string(),
column,
value,
])?;
}

writer.flush()?;

ScanAlerts::reports_generated(output_path);
Ok(())
}

pub fn json(&self, detections: Vec<(String, usize, String, String)>, output_path: &str) -> Result<(), Box<dyn Error>> {
let detections: Vec<Detection> = detections
.into_iter()
.map(|(table, row_index, column, value)| Detection {
table,
row_index,
column,
value,
})
.collect();

let file = File::create(output_path)?;
to_writer_pretty(file, &detections)?;

ScanAlerts::reports_generated(output_path);
Ok(())
}

}
18 changes: 14 additions & 4 deletions src/plugins/scan_xss.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ use mysql::{

use crate::{
consts::global::Global,
plugins::reports::Reports,
ui::scan_alerts::ScanAlerts,
engine::connection::Connection,
plugins::reports_xss::ReportsXSS,
engine::connection::Connection,

ui::{
scan_alerts::ScanAlerts,
errors_alerts::ErrorsAlerts,
},

helpers::{
scan_handlers::ScanHandlers,
Expand Down Expand Up @@ -118,7 +122,13 @@ impl ScanXSS {
}

if let Some(file) = &self.file {
Reports.xss(detections, file)?;
if file.ends_with(".csv") {
ReportsXSS.csv(detections, file)?;
} else if file.ends_with(".json") {
ReportsXSS.json(detections, file)?;
} else {
ErrorsAlerts::report_format();
}
}

Ok(())
Expand Down
9 changes: 9 additions & 0 deletions src/ui/errors_alerts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ impl ErrorsAlerts {
);
}

pub fn report_format() {
let message = "Invalid file format, only CSV and JSON are supported.";

println!("{}", "-".repeat(50));
println!(
"{}", message.red().bold(),
);
}

}

0 comments on commit e517bed

Please sign in to comment.