Repository navigation
Expand file tree
/
Copy pathsecurity_analysis.rs
More file actions
192 lines (169 loc) · 7.74 KB
/
Copy pathsecurity_analysis.rs
File metadata and controls
192 lines (169 loc) · 7.74 KB
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
use std::path::Path;
use syncable_cli::analyzer::{SecurityAnalysisConfig, SecurityAnalyzer, analyze_project};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
env_logger::init();
// Get project path from command line arguments or use current directory
let project_path = std::env::args().nth(1).unwrap_or_else(|| ".".to_string());
println!("🔍 Analyzing security for project: {}", project_path);
// First perform a general project analysis
let project_analysis = analyze_project(Path::new(&project_path))?;
println!("📊 Project Analysis Summary:");
println!(
" Languages: {:?}",
project_analysis
.languages
.iter()
.map(|l| &l.name)
.collect::<Vec<_>>()
);
println!(
" Technologies: {:?}",
project_analysis
.technologies
.iter()
.map(|t| &t.name)
.collect::<Vec<_>>()
);
println!(
" Environment Variables: {}",
project_analysis.environment_variables.len()
);
// Create security analyzer with default configuration
let security_config = SecurityAnalysisConfig {
include_low_severity: true, // Include low severity findings for demonstration
check_secrets: true,
check_code_patterns: true,
check_infrastructure: true,
check_compliance: true,
frameworks_to_check: vec!["SOC2".to_string(), "GDPR".to_string(), "OWASP".to_string()],
ignore_patterns: vec![
"node_modules".to_string(),
".git".to_string(),
"target".to_string(),
],
skip_gitignored_files: true,
downgrade_gitignored_severity: false,
};
let mut security_analyzer = SecurityAnalyzer::with_config(security_config)?;
// Perform security analysis
println!("\n🛡️ Running comprehensive security analysis...");
let security_report = security_analyzer.analyze_security(&project_analysis)?;
// Display results
println!("\n📋 Security Analysis Report");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
println!(
"🏆 Overall Security Score: {:.1}/100",
security_report.overall_score
);
println!("⚠️ Risk Level: {:?}", security_report.risk_level);
println!("🔍 Total Findings: {}", security_report.total_findings);
if !security_report.findings_by_severity.is_empty() {
println!("\n📊 Findings by Severity:");
for (severity, count) in &security_report.findings_by_severity {
let emoji = match severity {
syncable_cli::analyzer::SecuritySeverity::Critical => "🚨",
syncable_cli::analyzer::SecuritySeverity::High => "⚠️ ",
syncable_cli::analyzer::SecuritySeverity::Medium => "⚡",
syncable_cli::analyzer::SecuritySeverity::Low => "ℹ️ ",
syncable_cli::analyzer::SecuritySeverity::Info => "💡",
};
println!(" {} {:?}: {}", emoji, severity, count);
}
}
if !security_report.findings_by_category.is_empty() {
println!("\n🗂️ Findings by Category:");
for (category, count) in &security_report.findings_by_category {
let emoji = match category {
syncable_cli::analyzer::SecurityCategory::SecretsExposure => "🔐",
syncable_cli::analyzer::SecurityCategory::InsecureConfiguration => "⚙️ ",
syncable_cli::analyzer::SecurityCategory::CodeSecurityPattern => "💻",
syncable_cli::analyzer::SecurityCategory::InfrastructureSecurity => "🏗️ ",
syncable_cli::analyzer::SecurityCategory::AuthenticationSecurity => "🔑",
syncable_cli::analyzer::SecurityCategory::DataProtection => "🛡️ ",
syncable_cli::analyzer::SecurityCategory::NetworkSecurity => "🌐",
syncable_cli::analyzer::SecurityCategory::Compliance => "📜",
};
println!(" {} {:?}: {}", emoji, category, count);
}
}
// Display detailed findings
if !security_report.findings.is_empty() {
println!("\n🔍 Detailed Security Findings:");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
for (i, finding) in security_report.findings.iter().enumerate() {
let severity_emoji = match finding.severity {
syncable_cli::analyzer::SecuritySeverity::Critical => "🚨",
syncable_cli::analyzer::SecuritySeverity::High => "⚠️ ",
syncable_cli::analyzer::SecuritySeverity::Medium => "⚡",
syncable_cli::analyzer::SecuritySeverity::Low => "ℹ️ ",
syncable_cli::analyzer::SecuritySeverity::Info => "💡",
};
println!(
"\n{}. {} [{}] {}",
i + 1,
severity_emoji,
finding.id,
finding.title
);
println!(" 📝 {}", finding.description);
if let Some(file) = &finding.file_path {
print!(" 📁 File: {}", file.display());
if let Some(line) = finding.line_number {
print!(" (line {})", line);
}
println!();
}
if let Some(evidence) = &finding.evidence {
println!(" 🔍 Evidence: {}", evidence);
}
if !finding.remediation.is_empty() {
println!(" 🔧 Remediation:");
for remediation in &finding.remediation {
println!(" • {}", remediation);
}
}
if let Some(cwe) = &finding.cwe_id {
println!(" 🏷️ CWE: {}", cwe);
}
}
}
// Display recommendations
if !security_report.recommendations.is_empty() {
println!("\n💡 Security Recommendations:");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
for (i, recommendation) in security_report.recommendations.iter().enumerate() {
println!("{}. {}", i + 1, recommendation);
}
}
// Display compliance status
if !security_report.compliance_status.is_empty() {
println!("\n📜 Compliance Status:");
println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
for (framework, status) in &security_report.compliance_status {
println!("🏛️ {}: {:.1}% coverage", framework, status.coverage);
if !status.missing_controls.is_empty() {
println!(
" Missing controls: {}",
status.missing_controls.join(", ")
);
}
}
}
println!("\n✅ Security analysis completed!");
// Exit with appropriate code based on findings
if security_report
.findings_by_severity
.contains_key(&syncable_cli::analyzer::SecuritySeverity::Critical)
{
println!("❌ Critical security issues found. Please address immediately.");
std::process::exit(1);
} else if security_report
.findings_by_severity
.contains_key(&syncable_cli::analyzer::SecuritySeverity::High)
{
println!("⚠️ High severity security issues found. Review recommended.");
std::process::exit(2);
}
Ok(())
}