2025-06-16 16:46:22 +02:00
|
|
|
|
use crate::utils::project::get_project_info;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
use console::style;
|
2025-06-16 16:46:22 +02:00
|
|
|
|
use std::path::Path;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
|
|
|
|
|
|
use crate::database::index::{IssueRow, Indexer};
|
2025-06-17 11:35:23 +02:00
|
|
|
|
use crate::patterns::Severity;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
use crate::utils::config::Config;
|
2025-06-17 01:17:48 +02:00
|
|
|
|
use crate::utils::query_cache;
|
2025-06-16 23:47:50 +02:00
|
|
|
|
use crate::walk::spawn_senders;
|
2025-06-16 16:46:22 +02:00
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
use tree_sitter::{Language, Parser, QueryCursor, StreamingIterator};
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct Diag {
|
|
|
|
|
|
pub(crate) path: String,
|
|
|
|
|
|
pub(crate) line: usize,
|
|
|
|
|
|
pub(crate) col: usize,
|
|
|
|
|
|
pub(crate) severity: Severity,
|
|
|
|
|
|
pub(crate) id: String,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Entry point called by the CLI.
|
2025-06-16 16:46:22 +02:00
|
|
|
|
pub fn handle(
|
|
|
|
|
|
path: &str,
|
|
|
|
|
|
no_index: bool,
|
|
|
|
|
|
rebuild_index: bool,
|
2025-06-17 16:46:45 +02:00
|
|
|
|
format: String,
|
2025-06-16 16:46:22 +02:00
|
|
|
|
high_only: bool,
|
|
|
|
|
|
database_dir: &Path,
|
|
|
|
|
|
config: &Config,
|
|
|
|
|
|
) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
|
let scan_path = Path::new(path).canonicalize()?;
|
|
|
|
|
|
let (project_name, db_path) = get_project_info(&scan_path, database_dir)?;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let diags: Vec<Diag>;
|
|
|
|
|
|
|
2025-06-16 16:46:22 +02:00
|
|
|
|
if no_index {
|
2025-06-17 16:46:45 +02:00
|
|
|
|
diags = scan_filesystem(&scan_path, config)?;
|
2025-06-16 16:46:22 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
if rebuild_index || !db_path.exists() {
|
2025-06-17 17:42:41 +02:00
|
|
|
|
tracing::debug!("Scanning filesystem index filesystem");
|
|
|
|
|
|
crate::commands::index::build_index(&project_name,&scan_path, &db_path, config)?;
|
2025-06-16 16:46:22 +02:00
|
|
|
|
}
|
2025-06-17 17:52:22 +02:00
|
|
|
|
|
|
|
|
|
|
let mut indexer = Indexer::new(&project_name, &db_path)?;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
diags = scan_with_index(&project_name, &db_path, config, &mut indexer)?;
|
2025-06-16 16:46:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
if format == "console" || format == "" && config.output.default_format == "console" {
|
|
|
|
|
|
for d in &diags {
|
|
|
|
|
|
if high_only && d.severity != Severity::High {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
let sev_str = match d.severity {
|
|
|
|
|
|
Severity::High => style("HIGH").red().bold(),
|
|
|
|
|
|
Severity::Medium => style("MEDIUM").yellow().bold(),
|
|
|
|
|
|
Severity::Low => style("LOW").cyan().bold(),
|
|
|
|
|
|
};
|
|
|
|
|
|
println!(
|
|
|
|
|
|
"{}:{}:{} [{}] {}",
|
|
|
|
|
|
style(d.path.clone()).blue().underlined(),
|
|
|
|
|
|
d.line,
|
|
|
|
|
|
d.col,
|
|
|
|
|
|
sev_str,
|
|
|
|
|
|
style(&d.id).bold(),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-06-16 16:46:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
|
|
// Scanning helpers
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2025-06-16 23:47:50 +02:00
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
fn scan_filesystem(
|
|
|
|
|
|
root: &Path,
|
|
|
|
|
|
cfg: &Config,
|
|
|
|
|
|
) -> Result<Vec<Diag>, Box<dyn std::error::Error>> {
|
|
|
|
|
|
let rx = spawn_senders(root, cfg);
|
|
|
|
|
|
let mut issues: Vec<Diag> = Vec::new();
|
2025-06-16 23:47:50 +02:00
|
|
|
|
for batch in rx.iter().flatten() {
|
2025-06-17 16:46:45 +02:00
|
|
|
|
issues.append(&mut run_rules_on_file(&batch, cfg)?);
|
2025-06-16 23:47:50 +02:00
|
|
|
|
}
|
2025-06-17 16:46:45 +02:00
|
|
|
|
Ok(issues)
|
2025-06-16 23:47:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
fn scan_with_index(
|
|
|
|
|
|
project: &str,
|
|
|
|
|
|
_db_path: &Path,
|
|
|
|
|
|
cfg: &Config,
|
|
|
|
|
|
indexer: &mut Indexer,
|
|
|
|
|
|
) -> Result<Vec<Diag>, Box<dyn std::error::Error>> {
|
2025-06-17 17:42:41 +02:00
|
|
|
|
let paths = indexer.get_files(project).unwrap_or_default();
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let mut issues: Vec<Diag> = Vec::new();
|
2025-06-17 17:42:41 +02:00
|
|
|
|
for path in paths {
|
|
|
|
|
|
if indexer.should_scan(&path)? {
|
2025-06-17 17:52:22 +02:00
|
|
|
|
tracing::debug!("scanning files{}", path.display());
|
|
|
|
|
|
|
2025-06-17 17:42:41 +02:00
|
|
|
|
let mut diags = run_rules_on_file(&path, cfg)?;
|
|
|
|
|
|
let file_id = indexer.upsert_file(&path)?;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
|
|
|
|
|
|
let issue_rows: Vec<IssueRow> = diags
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.map(|d| IssueRow {
|
|
|
|
|
|
rule_id: d.id.as_ref(),
|
|
|
|
|
|
severity: match d.severity {
|
|
|
|
|
|
Severity::High => "HIGH",
|
|
|
|
|
|
Severity::Medium => "MEDIUM",
|
|
|
|
|
|
Severity::Low => "LOW",
|
|
|
|
|
|
},
|
|
|
|
|
|
line: d.line as i64,
|
|
|
|
|
|
col: d.col as i64,
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
|
|
indexer.replace_issues(file_id, issue_rows)?;
|
|
|
|
|
|
issues.append(&mut diags);
|
|
|
|
|
|
continue;
|
2025-06-16 23:47:50 +02:00
|
|
|
|
}
|
2025-06-17 17:42:41 +02:00
|
|
|
|
issues.append(&mut indexer.get_issues_from_file(&path)?);
|
2025-06-16 23:47:50 +02:00
|
|
|
|
}
|
2025-06-17 16:46:45 +02:00
|
|
|
|
Ok(issues)
|
2025-06-16 16:46:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
|
|
// Tree‑sitter‑based rule runner – returns a Vec<Diag>
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2025-06-17 17:42:41 +02:00
|
|
|
|
pub(crate) fn run_rules_on_file(
|
2025-06-16 23:47:50 +02:00
|
|
|
|
path: &Path,
|
2025-06-17 16:46:45 +02:00
|
|
|
|
cfg: &Config,
|
|
|
|
|
|
) -> Result<Vec<Diag>, Box<dyn std::error::Error>> {
|
2025-06-17 18:36:46 +02:00
|
|
|
|
let bytes = std::fs::read(path)?;
|
|
|
|
|
|
|
2025-06-16 23:47:50 +02:00
|
|
|
|
let mut parser = Parser::new();
|
|
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let lang_key = match path
|
|
|
|
|
|
.extension()
|
|
|
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
|
.to_ascii_lowercase()
|
|
|
|
|
|
.as_str()
|
|
|
|
|
|
{
|
2025-06-17 01:17:48 +02:00
|
|
|
|
"rs" => (Language::from(tree_sitter_rust::LANGUAGE), "rust"),
|
|
|
|
|
|
"c" => (Language::from(tree_sitter_c::LANGUAGE), "c"),
|
|
|
|
|
|
"cpp" | "c++" => (Language::from(tree_sitter_cpp::LANGUAGE), "cpp"),
|
|
|
|
|
|
"java" => (Language::from(tree_sitter_java::LANGUAGE), "java"),
|
|
|
|
|
|
"go" => (Language::from(tree_sitter_go::LANGUAGE), "go"),
|
|
|
|
|
|
"php" => (Language::from(tree_sitter_php::LANGUAGE_PHP), "php"),
|
|
|
|
|
|
"py" => (Language::from(tree_sitter_python::LANGUAGE), "python"),
|
|
|
|
|
|
"ts" | "tsx" => (Language::from(tree_sitter_typescript::LANGUAGE_TYPESCRIPT), "typescript"),
|
|
|
|
|
|
"js" => (Language::from(tree_sitter_javascript::LANGUAGE), "javascript"),
|
2025-06-17 16:46:45 +02:00
|
|
|
|
_ => return Ok(Vec::new()),
|
2025-06-17 01:17:48 +02:00
|
|
|
|
};
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let (ts_lang, lang_name) = lang_key;
|
2025-06-17 01:17:48 +02:00
|
|
|
|
|
|
|
|
|
|
parser.set_language(&ts_lang)?;
|
2025-06-17 18:36:46 +02:00
|
|
|
|
let tree = parser.parse(&*bytes, None).ok_or("tree‑sitter failed")?;
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let root = tree.root_node();
|
2025-06-17 01:17:48 +02:00
|
|
|
|
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let compiled = query_cache::for_lang(lang_name, ts_lang);
|
2025-06-17 01:17:48 +02:00
|
|
|
|
let mut cursor = QueryCursor::new();
|
2025-06-17 16:46:45 +02:00
|
|
|
|
let mut out = Vec::new();
|
2025-06-17 01:17:48 +02:00
|
|
|
|
|
|
|
|
|
|
for cq in &compiled {
|
|
|
|
|
|
if cfg.scanner.min_severity > cq.meta.severity {
|
2025-06-17 16:46:45 +02:00
|
|
|
|
continue;
|
2025-06-17 01:17:48 +02:00
|
|
|
|
}
|
2025-06-17 18:36:46 +02:00
|
|
|
|
let mut matches = cursor.matches(&cq.query, root, &*bytes);
|
2025-06-17 01:17:48 +02:00
|
|
|
|
while let Some(m) = matches.next() {
|
|
|
|
|
|
for cap in m.captures.iter().filter(|c| c.index == 0) {
|
|
|
|
|
|
let point = cap.node.start_position();
|
2025-06-17 16:46:45 +02:00
|
|
|
|
out.push(Diag {
|
|
|
|
|
|
path: path.to_string_lossy().to_string(),
|
|
|
|
|
|
line: point.row + 1,
|
|
|
|
|
|
col: point.column + 1,
|
|
|
|
|
|
severity: cq.meta.severity,
|
|
|
|
|
|
id: String::from(cq.meta.id),
|
|
|
|
|
|
});
|
2025-06-17 01:17:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-17 16:46:45 +02:00
|
|
|
|
Ok(out)
|
2025-06-16 16:46:22 +02:00
|
|
|
|
}
|