1+ //! Example: Enhanced Security Analysis
2+ //!
3+ //! This example demonstrates the enhanced security analysis capabilities
4+ //! including the new modular JavaScript/TypeScript security analyzer.
5+
6+ use std:: path:: Path ;
7+ use syncable_cli:: analyzer:: { analyze_project, SecurityAnalyzer } ;
8+
9+ fn main ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
10+ env_logger:: init ( ) ;
11+
12+ // For this example, analyze the current directory or a provided path
13+ let project_path = std:: env:: args ( )
14+ . nth ( 1 )
15+ . map ( |p| Path :: new ( & p) . to_path_buf ( ) )
16+ . unwrap_or_else ( || std:: env:: current_dir ( ) . unwrap ( ) ) ;
17+
18+ println ! ( "🔍 Analyzing project security for: {}" , project_path. display( ) ) ;
19+
20+ // First, perform regular project analysis to detect languages
21+ let analysis = analyze_project ( & project_path) ?;
22+
23+ println ! ( "\n 📋 Detected Languages:" ) ;
24+ for lang in & analysis. languages {
25+ println ! ( " • {} (confidence: {:.1}%)" , lang. name, lang. confidence * 100.0 ) ;
26+ }
27+
28+ println ! ( "\n 🔧 Detected Technologies:" ) ;
29+ for tech in & analysis. technologies {
30+ println ! ( " • {} v{} ({:?})" ,
31+ tech. name,
32+ tech. version. as_deref( ) . unwrap_or( "unknown" ) ,
33+ tech. category
34+ ) ;
35+ }
36+
37+ // Check if this is a JavaScript/TypeScript project
38+ let has_js = analysis. languages . iter ( )
39+ . any ( |lang| matches ! ( lang. name. as_str( ) , "JavaScript" | "TypeScript" | "JSX" | "TSX" ) ) ;
40+
41+ if has_js {
42+ println ! ( "\n ✅ JavaScript/TypeScript project detected! Using enhanced security analysis..." ) ;
43+ } else {
44+ println ! ( "\n 📄 Using general security analysis..." ) ;
45+ }
46+
47+ // Run enhanced security analysis
48+ println ! ( "\n 🛡️ Starting enhanced security analysis..." ) ;
49+
50+ let mut security_analyzer = SecurityAnalyzer :: new ( ) ?;
51+ let security_report = security_analyzer. analyze_security_enhanced ( & analysis) ?;
52+
53+ // Display results
54+ println ! ( "\n 📊 Security Analysis Results:" ) ;
55+ println ! ( " Overall Score: {:.1}/100" , security_report. overall_score) ;
56+ println ! ( " Risk Level: {:?}" , security_report. risk_level) ;
57+ println ! ( " Total Findings: {}" , security_report. total_findings) ;
58+
59+ if security_report. total_findings > 0 {
60+ println ! ( "\n 🚨 Security Findings:" ) ;
61+
62+ // Group findings by severity
63+ for severity in [
64+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Critical ,
65+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: High ,
66+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Medium ,
67+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Low ,
68+ ] {
69+ let findings: Vec < _ > = security_report. findings . iter ( )
70+ . filter ( |f| f. severity == severity)
71+ . collect ( ) ;
72+
73+ if !findings. is_empty ( ) {
74+ let severity_icon = match severity {
75+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Critical => "🔴" ,
76+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: High => "🟠" ,
77+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Medium => "🟡" ,
78+ syncable_cli:: analyzer:: security:: core:: SecuritySeverity :: Low => "🔵" ,
79+ _ => "⚪" ,
80+ } ;
81+
82+ println ! ( "\n {} {:?} Severity ({} findings):" , severity_icon, severity, findings. len( ) ) ;
83+
84+ for finding in findings. iter ( ) . take ( 3 ) { // Show first 3 of each severity
85+ println ! ( " 📍 {}" , finding. title) ;
86+ if let Some ( ref file_path) = finding. file_path {
87+ let relative_path = file_path. strip_prefix ( & project_path)
88+ . unwrap_or ( file_path) ;
89+ print ! ( " 📄 {}" , relative_path. display( ) ) ;
90+ if let Some ( line) = finding. line_number {
91+ print ! ( ":{}" , line) ;
92+ }
93+ println ! ( ) ;
94+ }
95+ println ! ( " 💡 {}" , finding. description) ;
96+
97+ if !finding. remediation . is_empty ( ) {
98+ println ! ( " 🔧 Remediation: {}" , finding. remediation[ 0 ] ) ;
99+ }
100+ println ! ( ) ;
101+ }
102+
103+ if findings. len ( ) > 3 {
104+ println ! ( " ... and {} more findings" , findings. len( ) - 3 ) ;
105+ }
106+ }
107+ }
108+
109+ // Show recommendations
110+ if !security_report. recommendations . is_empty ( ) {
111+ println ! ( "\n 💡 Recommendations:" ) ;
112+ for ( i, recommendation) in security_report. recommendations . iter ( ) . enumerate ( ) {
113+ println ! ( " {}. {}" , i + 1 , recommendation) ;
114+ }
115+ }
116+ } else {
117+ println ! ( "✅ No security issues detected!" ) ;
118+ }
119+
120+ println ! ( "\n ✨ Enhanced security analysis complete!" ) ;
121+
122+ Ok ( ( ) )
123+ }
0 commit comments