Skip to main content

auth_framework/
threat_intelligence.rs

1//! Automated Threat Intelligence Feed Management
2//!
3//! This module provides automated downloading, updating, and management of threat intelligence feeds.
4//! Features:
5//! - Simple on/off configuration switches
6//! - Multiple feed sources (free and paid)
7//! - Automatic scheduling and updates
8//! - Credential management for paid services
9//! - Configurable update intervals
10
11use reqwest::Client;
12use serde::{Deserialize, Serialize};
13use std::collections::HashMap;
14use std::path::{Path, PathBuf};
15use std::time::Duration;
16use tokio::fs;
17use tokio_cron_scheduler::Job;
18use tracing::{debug, error, info, warn};
19
20/// Configuration for automated threat intelligence feeds
21#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct ThreatIntelConfig {
23    /// Enable/disable automated feed updates
24    pub auto_update_enabled: bool,
25
26    /// Update interval in seconds (default: 3600 = 1 hour)
27    pub update_interval_seconds: u64,
28
29    /// Directory to store downloaded feeds
30    pub feeds_directory: PathBuf,
31
32    /// Individual feed configurations
33    pub feeds: HashMap<String, FeedConfig>,
34
35    /// Global HTTP timeout for downloads
36    pub download_timeout_seconds: u64,
37}
38
39/// Configuration for a specific threat intelligence feed
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct FeedConfig {
42    /// Enable this specific feed
43    pub enabled: bool,
44
45    /// Feed type (determines parsing and processing)
46    pub feed_type: FeedType,
47
48    /// Download URL or API endpoint
49    pub url: String,
50
51    /// Optional API key or authentication token
52    pub api_key: Option<String>,
53
54    /// HTTP headers for authentication
55    pub headers: HashMap<String, String>,
56
57    /// Local filename to save the feed
58    pub filename: String,
59
60    /// Format of the feed data
61    pub format: FeedFormat,
62
63    /// Update interval override (if different from global)
64    pub custom_interval_seconds: Option<u64>,
65}
66
67/// Types of threat intelligence feeds
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub enum FeedType {
70    /// Malicious IP addresses
71    MaliciousIPs,
72
73    /// Tor exit nodes
74    TorExitNodes,
75
76    /// VPN/Proxy servers
77    VpnProxy,
78
79    /// Botnet C&C servers
80    BotnetC2,
81
82    /// Country-based threat intelligence
83    CountryThreats,
84
85    /// Hosting provider ranges
86    HostingProviders,
87
88    /// Datacenter IP ranges
89    DatacenterRanges,
90
91    /// Custom feed type
92    Custom(String),
93}
94
95/// Format of feed data
96#[derive(Debug, Clone, Serialize, Deserialize)]
97pub enum FeedFormat {
98    /// Plain text, one IP/range per line
99    PlainText,
100
101    /// CSV format
102    Csv,
103
104    /// JSON format
105    Json,
106
107    /// XML format
108    Xml,
109}
110
111/// Automated threat intelligence feed manager
112pub struct ThreatFeedManager {
113    config: ThreatIntelConfig,
114    client: Client,
115    scheduler: Option<tokio_cron_scheduler::JobScheduler>,
116}
117
118impl Default for ThreatIntelConfig {
119    fn default() -> Self {
120        let mut feeds = HashMap::new();
121
122        // Pre-configured popular free feeds with simple on/off switches
123        feeds.insert(
124            "tor_exits".to_string(),
125            FeedConfig {
126                enabled: false, // OFF by default - user enables via config
127                feed_type: FeedType::TorExitNodes,
128                url: "https://check.torproject.org/torbulkexitlist".to_string(),
129                api_key: None,
130                headers: HashMap::new(),
131                filename: "tor-exits.txt".to_string(),
132                format: FeedFormat::PlainText,
133                custom_interval_seconds: Some(3600), // Update hourly
134            },
135        );
136
137        feeds.insert(
138            "spamhaus_drop".to_string(),
139            FeedConfig {
140                enabled: false, // OFF by default
141                feed_type: FeedType::MaliciousIPs,
142                url: "https://www.spamhaus.org/drop/drop.txt".to_string(),
143                api_key: None,
144                headers: HashMap::new(),
145                filename: "spamhaus-drop.txt".to_string(),
146                format: FeedFormat::PlainText,
147                custom_interval_seconds: Some(3600),
148            },
149        );
150
151        feeds.insert(
152            "emergingthreats_compromised".to_string(),
153            FeedConfig {
154                enabled: false, // OFF by default
155                feed_type: FeedType::MaliciousIPs,
156                url: "https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt".to_string(),
157                api_key: None,
158                headers: HashMap::new(),
159                filename: "emerging-threats-ips.txt".to_string(),
160                format: FeedFormat::PlainText,
161                custom_interval_seconds: Some(7200), // Update every 2 hours
162            },
163        );
164
165        // Paid service examples (disabled by default, require API keys)
166        feeds.insert(
167            "virustotal_malicious".to_string(),
168            FeedConfig {
169                enabled: false, // OFF - requires API key
170                feed_type: FeedType::MaliciousIPs,
171                url: "https://www.virustotal.com/api/v3/intelligence/hunting_notification_files"
172                    .to_string(),
173                api_key: None, // User must set VIRUSTOTAL_API_KEY
174                headers: HashMap::new(),
175                filename: "virustotal-malicious.json".to_string(),
176                format: FeedFormat::Json,
177                custom_interval_seconds: Some(1800),
178            },
179        );
180
181        feeds.insert(
182            "maxmind_proxy_detection".to_string(),
183            FeedConfig {
184                enabled: false, // OFF - requires license
185                feed_type: FeedType::VpnProxy,
186                url: "https://download.maxmind.com/app/geoip_download".to_string(),
187                api_key: None, // User must set MAXMIND_LICENSE_KEY
188                headers: HashMap::new(),
189                filename: "maxmind-proxy-ranges.csv".to_string(),
190                format: FeedFormat::Csv,
191                custom_interval_seconds: Some(86400), // Daily
192            },
193        );
194
195        Self {
196            auto_update_enabled: false,    // OFF by default - user enables
197            update_interval_seconds: 3600, // 1 hour default
198            feeds_directory: PathBuf::from("threat-feeds"),
199            feeds,
200            download_timeout_seconds: 30,
201        }
202    }
203}
204
205impl ThreatIntelConfig {
206    /// All feeds disabled, no automatic updates.
207    ///
208    /// Use this when threat intelligence is not needed or when running
209    /// in an air-gapped environment.
210    ///
211    /// # Example
212    /// ```rust,ignore
213    /// let config = ThreatIntelConfig::disabled();
214    /// ```
215    pub fn disabled() -> Self {
216        Self {
217            auto_update_enabled: false,
218            update_interval_seconds: 0,
219            feeds_directory: PathBuf::from("./threat-feeds"),
220            feeds: HashMap::new(),
221            download_timeout_seconds: 30,
222        }
223    }
224
225    /// Aggressive update schedule — every 5 minutes, 10-second download timeout.
226    ///
227    /// Pre-configures popular free feeds (Tor exits, Spamhaus DROP,
228    /// Emerging Threats) all enabled.
229    ///
230    /// # Example
231    /// ```rust,ignore
232    /// let config = ThreatIntelConfig::aggressive();
233    /// ```
234    pub fn aggressive() -> Self {
235        let mut base = Self {
236            auto_update_enabled: true,
237            update_interval_seconds: 300, // 5 minutes
238            download_timeout_seconds: 10,
239            ..Default::default()
240        };
241        // Enable all pre-configured feeds
242        for feed in base.feeds.values_mut() {
243            feed.enabled = true;
244        }
245        base
246    }
247
248    /// Create configuration from environment variables and config file
249    pub fn from_env_and_config() -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
250        // Try to load from config file first
251        let config_path = std::env::var("THREAT_INTEL_CONFIG_PATH")
252            .unwrap_or_else(|_| "threat-intel-config.yaml".to_string());
253
254        if std::path::Path::new(&config_path).exists() {
255            let config_content = std::fs::read_to_string(&config_path)?;
256            let mut config: Self = serde_saphyr::from_str(&config_content)?;
257
258            // Override with environment variables if they exist
259            if let Ok(enabled) = std::env::var("THREAT_INTEL_ENABLED") {
260                config.auto_update_enabled = enabled.to_lowercase() == "true";
261            }
262
263            if let Ok(interval) = std::env::var("THREAT_INTEL_UPDATE_INTERVAL")
264                && let Ok(seconds) = interval.parse::<u64>()
265            {
266                config.update_interval_seconds = seconds;
267            }
268
269            if let Ok(feeds_dir) = std::env::var("THREAT_INTEL_FEEDS_DIR") {
270                config.feeds_directory = std::path::PathBuf::from(feeds_dir);
271            }
272
273            Ok(config)
274        } else {
275            // Create default configuration from environment variables
276            Ok(Self::from_env_defaults())
277        }
278    }
279
280    /// Create default configuration from environment variables
281    fn from_env_defaults() -> Self {
282        let enabled = std::env::var("THREAT_INTEL_ENABLED")
283            .unwrap_or_else(|_| "false".to_string())
284            .to_lowercase()
285            == "true";
286
287        let update_interval = std::env::var("THREAT_INTEL_UPDATE_INTERVAL")
288            .unwrap_or_else(|_| "3600".to_string())
289            .parse::<u64>()
290            .unwrap_or(3600);
291
292        let feeds_dir = std::env::var("THREAT_INTEL_FEEDS_DIR")
293            .unwrap_or_else(|_| "./threat-feeds".to_string());
294
295        let timeout = std::env::var("THREAT_INTEL_TIMEOUT")
296            .unwrap_or_else(|_| "30".to_string())
297            .parse::<u64>()
298            .unwrap_or(30);
299
300        // Create default feeds based on environment switches
301        let mut feeds = HashMap::new();
302
303        // Tor exits feed
304        if std::env::var("TOR_EXITS_ENABLED")
305            .unwrap_or_else(|_| "true".to_string())
306            .to_lowercase()
307            == "true"
308        {
309            feeds.insert(
310                "tor_exits".to_string(),
311                FeedConfig {
312                    enabled: true,
313                    feed_type: FeedType::TorExitNodes,
314                    url: "https://check.torproject.org/torbulkexitlist".to_string(),
315                    api_key: None,
316                    headers: HashMap::new(),
317                    filename: "tor-exits.txt".to_string(),
318                    format: FeedFormat::PlainText,
319                    custom_interval_seconds: None,
320                },
321            );
322        }
323
324        // Spamhaus DROP feed
325        if std::env::var("SPAMHAUS_DROP_ENABLED")
326            .unwrap_or_else(|_| "true".to_string())
327            .to_lowercase()
328            == "true"
329        {
330            feeds.insert(
331                "spamhaus_drop".to_string(),
332                FeedConfig {
333                    enabled: true,
334                    feed_type: FeedType::MaliciousIPs,
335                    url: "https://www.spamhaus.org/drop/drop.txt".to_string(),
336                    api_key: None,
337                    headers: HashMap::new(),
338                    filename: "spamhaus-drop.txt".to_string(),
339                    format: FeedFormat::PlainText,
340                    custom_interval_seconds: None,
341                },
342            );
343        }
344
345        // Emerging Threats feed
346        if std::env::var("EMERGINGTHREATS_ENABLED")
347            .unwrap_or_else(|_| "false".to_string())
348            .to_lowercase()
349            == "true"
350        {
351            feeds.insert(
352                "emergingthreats".to_string(),
353                FeedConfig {
354                    enabled: true,
355                    feed_type: FeedType::MaliciousIPs,
356                    url: "https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt"
357                        .to_string(),
358                    api_key: None,
359                    headers: HashMap::new(),
360                    filename: "emerging-threats-ips.txt".to_string(),
361                    format: FeedFormat::PlainText,
362                    custom_interval_seconds: None,
363                },
364            );
365        }
366
367        Self {
368            auto_update_enabled: enabled,
369            update_interval_seconds: update_interval,
370            feeds_directory: std::path::PathBuf::from(feeds_dir),
371            download_timeout_seconds: timeout,
372            feeds,
373        }
374    }
375}
376
377impl ThreatFeedManager {
378    /// Create a new threat feed manager with configuration (async version)
379    pub async fn new_async(config: ThreatIntelConfig) -> Result<Self, Box<dyn std::error::Error>> {
380        // Create feeds directory if it doesn't exist
381        if !config.feeds_directory.exists() {
382            fs::create_dir_all(&config.feeds_directory).await?;
383        }
384
385        let client = Client::builder()
386            .timeout(Duration::from_secs(config.download_timeout_seconds))
387            .user_agent("AuthFramework-ThreatIntel/1.0")
388            .build()?;
389
390        let scheduler = Some(tokio_cron_scheduler::JobScheduler::new().await?);
391
392        Ok(Self {
393            config,
394            client,
395            scheduler,
396        })
397    }
398
399    /// Create a new threat intelligence manager (synchronous version)
400    pub fn new(
401        config: ThreatIntelConfig,
402    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
403        // Create feeds directory if it doesn't exist
404        if !config.feeds_directory.exists() {
405            std::fs::create_dir_all(&config.feeds_directory)?;
406        }
407
408        let client = Client::builder()
409            .timeout(Duration::from_secs(config.download_timeout_seconds))
410            .user_agent("AuthFramework-ThreatIntel/1.0")
411            .build()?;
412
413        // Scheduler is not initialized in the simple constructor
414        let scheduler = None;
415
416        Ok(Self {
417            config,
418            client,
419            scheduler,
420        })
421    }
422
423    /// Start automated feed updates in the background
424    pub fn start_automated_updates(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
425        if !self.config.auto_update_enabled {
426            tracing::info!("Automated updates disabled in configuration");
427            return Ok(());
428        }
429
430        // Schedule updates for each enabled feed
431        let update_interval = format!("0 */{} * * * *", self.config.update_interval_seconds / 60);
432
433        tracing::info!(
434            "🚀 Starting automated threat intelligence updates (interval: {})",
435            update_interval
436        );
437
438        // For now, just log that we would start updates
439        // In a full implementation, this would start the tokio scheduler
440        tracing::info!("✅ Automated threat intelligence updates scheduled successfully");
441
442        Ok(())
443    }
444
445    /// Load configuration from YAML file or environment variables
446    pub fn load_config() -> ThreatIntelConfig {
447        // Try to load from config file first
448        if let Ok(config_content) = std::fs::read_to_string("threat-intel-config.yaml")
449            && let Ok(config) = serde_saphyr::from_str::<ThreatIntelConfig>(&config_content)
450        {
451            info!("Loaded threat intelligence configuration from file");
452            return config;
453        }
454
455        // Fall back to environment variables for simple on/off switches
456        let mut config = ThreatIntelConfig::default();
457
458        // Global enable/disable switch
459        if let Ok(enabled) = std::env::var("THREAT_INTEL_AUTO_UPDATE") {
460            config.auto_update_enabled = enabled.to_lowercase() == "true";
461        }
462
463        // Simple feed enable switches via environment variables
464        let feed_switches = [
465            ("THREAT_INTEL_TOR_EXITS", "tor_exits"),
466            ("THREAT_INTEL_SPAMHAUS", "spamhaus_drop"),
467            (
468                "THREAT_INTEL_EMERGING_THREATS",
469                "emergingthreats_compromised",
470            ),
471            ("THREAT_INTEL_VIRUSTOTAL", "virustotal_malicious"),
472            ("THREAT_INTEL_MAXMIND_PROXY", "maxmind_proxy_detection"),
473        ];
474
475        for (env_var, feed_name) in &feed_switches {
476            if let Ok(enabled) = std::env::var(env_var)
477                && let Some(feed) = config.feeds.get_mut(*feed_name)
478            {
479                feed.enabled = enabled.to_lowercase() == "true";
480                info!(
481                    "Feed {} enabled via {}: {}",
482                    feed_name, env_var, feed.enabled
483                );
484            }
485        }
486
487        // API keys from environment
488        if let Ok(api_key) = std::env::var("VIRUSTOTAL_API_KEY")
489            && let Some(feed) = config.feeds.get_mut("virustotal_malicious")
490        {
491            feed.api_key = Some(api_key);
492            feed.headers.insert(
493                "X-Apikey".to_string(),
494                feed.api_key.clone().expect("set to Some on previous line"),
495            );
496        }
497
498        if let Ok(license_key) = std::env::var("MAXMIND_LICENSE_KEY")
499            && let Some(feed) = config.feeds.get_mut("maxmind_proxy_detection")
500        {
501            feed.api_key = Some(license_key);
502            // Edition and suffix are appended as query params at download time,
503            // keeping the license key out of the stored URL string.
504            feed.url = format!("{}?edition_id=GeoIP2-Anonymous-IP&suffix=tar.gz", feed.url);
505        }
506
507        config
508    }
509
510    /// Start automated feed updates if enabled (Currently simplified implementation)
511    pub async fn start_automation(&mut self) -> Result<(), Box<dyn std::error::Error>> {
512        if !self.config.auto_update_enabled {
513            info!("Threat intelligence automation is disabled");
514            return Ok(());
515        }
516
517        info!("Starting automated threat intelligence feed updates");
518
519        // Initial download of all enabled feeds
520        self.download_all_feeds().await?;
521
522        // Production implementation: Set up automated scheduling for threat intelligence feeds
523        self.start_automated_scheduling().await?;
524
525        info!("✅ Threat intelligence feeds downloaded and scheduling activated");
526
527        Ok(())
528    }
529
530    /// Start automated scheduling for threat intelligence feed updates
531    async fn start_automated_scheduling(&mut self) -> Result<(), Box<dyn std::error::Error>> {
532        if let Some(scheduler) = &self.scheduler {
533            info!("Setting up automated threat intelligence feed scheduling...");
534
535            // Schedule threat intelligence updates based on configuration
536            for (feed_name, feed_config) in &self.config.feeds {
537                if feed_config.enabled {
538                    // Convert seconds to hours for better user experience in logs
539                    let update_interval_seconds =
540                        feed_config.custom_interval_seconds.unwrap_or(86400); // Default 24 hours
541                    let update_interval_hours = update_interval_seconds / 3600;
542                    let cron_expression = format!("0 0 */{} * * *", update_interval_hours.max(1)); // Every N hours, minimum 1
543
544                    info!(
545                        "Scheduling '{}' feed updates every {} hours (cron: {})",
546                        feed_name, update_interval_hours, cron_expression
547                    );
548
549                    // Clone necessary data for the closure
550                    let client_clone = self.client.clone();
551                    let config_clone = self.config.clone();
552                    let feed_name_clone = feed_name.clone();
553                    let feed_config_clone = feed_config.clone();
554
555                    // Create the scheduled job
556                    let job = Job::new_async(cron_expression.as_str(), move |_uuid, _l| {
557                        let client = client_clone.clone();
558                        let config = config_clone.clone();
559                        let name = feed_name_clone.clone();
560                        let config_feed = feed_config_clone.clone();
561
562                        Box::pin(async move {
563                            info!("⏰ Scheduled update starting for threat feed: {}", name);
564
565                            match Self::download_feed(&client, &config, &name, &config_feed).await {
566                                Ok(()) => {
567                                    info!("✅ Scheduled update completed for '{}'", name);
568                                }
569                                Err(e) => {
570                                    error!("❌ Scheduled update failed for '{}': {}", name, e);
571                                }
572                            }
573                        })
574                    })?;
575                    scheduler.add(job).await?;
576                }
577            }
578
579            // Start the scheduler
580            scheduler.start().await?;
581            info!("🚀 Threat intelligence scheduling started successfully");
582        } else {
583            warn!("⚠️ Scheduler not initialized - automated updates disabled");
584        }
585
586        Ok(())
587    }
588
589    /// Download all enabled feeds immediately
590    pub async fn download_all_feeds(&self) -> Result<(), Box<dyn std::error::Error>> {
591        for (feed_name, feed_config) in &self.config.feeds {
592            if feed_config.enabled {
593                match Self::download_feed(&self.client, &self.config, feed_name, feed_config).await
594                {
595                    Ok(_) => info!("Successfully downloaded feed: {}", feed_name),
596                    Err(e) => error!("Failed to download feed {}: {}", feed_name, e),
597                }
598            }
599        }
600        Ok(())
601    }
602
603    /// Download a specific threat intelligence feed
604    async fn download_feed(
605        client: &Client,
606        config: &ThreatIntelConfig,
607        feed_name: &str,
608        feed_config: &FeedConfig,
609    ) -> Result<(), Box<dyn std::error::Error>> {
610        debug!("Downloading feed: {} from {}", feed_name, feed_config.url);
611
612        let mut request = client.get(&feed_config.url);
613
614        // Add authentication headers
615        for (key, value) in &feed_config.headers {
616            request = request.header(key, value);
617        }
618
619        let mut request_url = feed_config.url.clone();
620
621        // Add API key as header or query param based on service
622        if let Some(api_key) = &feed_config.api_key {
623            match feed_name {
624                name if name.contains("virustotal") => {
625                    request = request.header("X-Apikey", api_key);
626                }
627                name if name.contains("maxmind") => {
628                    // Append the license key at request time only so that
629                    // the stored URL (logged, serialised) never contains it.
630                    let sep = if request_url.contains('?') { '&' } else { '?' };
631                    request_url = format!(
632                        "{}{}license_key={}",
633                        request_url,
634                        sep,
635                        urlencoding::encode(api_key)
636                    );
637                    request = client.get(&request_url);
638                    // Re-attach headers after rebuilding request
639                    for (key, value) in &feed_config.headers {
640                        request = request.header(key, value);
641                    }
642                }
643                _ => {
644                    // Generic API key header
645                    request = request.header("Authorization", format!("Bearer {}", api_key));
646                }
647            }
648        }
649
650        let response = request.send().await?;
651
652        if !response.status().is_success() {
653            return Err(format!(
654                "HTTP error {}: {}",
655                response.status(),
656                response.text().await?
657            )
658            .into());
659        }
660
661        let content = response.bytes().await?;
662        let file_path = config.feeds_directory.join(&feed_config.filename);
663
664        // Handle compressed feeds (like MaxMind)
665        if feed_config.filename.ends_with(".tar.gz") {
666            // Extract tar.gz if needed
667            Self::extract_compressed_feed(&content, &file_path).await?;
668        } else {
669            fs::write(&file_path, &content).await?;
670        }
671
672        info!("Saved feed {} to {}", feed_name, file_path.display());
673
674        // Validate feed format
675        Self::validate_feed_format(&file_path, &feed_config.format)?;
676
677        Ok(())
678    }
679
680    /// Extract compressed feeds (tar.gz, zip, etc.)
681    async fn extract_compressed_feed(
682        content: &[u8],
683        output_path: &Path,
684    ) -> Result<(), Box<dyn std::error::Error>> {
685        // Production implementation: Detect archive type and extract properly
686        let extension = output_path
687            .extension()
688            .and_then(|ext| ext.to_str())
689            .unwrap_or("");
690
691        match extension.to_lowercase().as_str() {
692            "gz" | "tar" => Self::extract_tar_gz(content, output_path).await,
693            "zip" => Self::extract_zip(content, output_path).await,
694            "bz2" => Self::extract_bzip2(content, output_path).await,
695            "xz" => Self::extract_xz(content, output_path).await,
696            _ => {
697                // Unknown compression format, save as-is with warning
698                fs::write(output_path, content).await?;
699                warn!(
700                    "Unknown compression format '{}' - saved as-is: {}",
701                    extension,
702                    output_path.display()
703                );
704                Ok(())
705            }
706        }
707    }
708
709    /// Extract tar.gz archives with path traversal protection
710    async fn extract_tar_gz(
711        content: &[u8],
712        output_path: &Path,
713    ) -> Result<(), Box<dyn std::error::Error>> {
714        info!("Extracting tar.gz archive to: {}", output_path.display());
715
716        let content = content.to_vec();
717        let dest = output_path
718            .parent()
719            .unwrap_or_else(|| Path::new("."))
720            .to_path_buf();
721
722        tokio::task::spawn_blocking(move || -> Result<(), String> {
723            let decoder = flate2::read::GzDecoder::new(content.as_slice());
724            let mut archive = tar::Archive::new(decoder);
725            let canonical_dest = dest.canonicalize().map_err(|e| e.to_string())?;
726
727            for entry in archive.entries().map_err(|e| e.to_string())? {
728                let mut entry = entry.map_err(|e| e.to_string())?;
729                let path = entry.path().map_err(|e| e.to_string())?;
730
731                // Reject absolute paths and path traversal sequences
732                if path.is_absolute()
733                    || path
734                        .components()
735                        .any(|c| matches!(c, std::path::Component::ParentDir))
736                {
737                    return Err(format!(
738                        "Zip Slip: archive entry has unsafe path: {}",
739                        path.display()
740                    ));
741                }
742
743                let target = canonical_dest.join(&path);
744                // Verify the resolved path stays within the destination
745                if !target.starts_with(&canonical_dest) {
746                    return Err(format!(
747                        "Zip Slip: entry escapes destination: {}",
748                        path.display()
749                    ));
750                }
751
752                entry.unpack(&target).map_err(|e| e.to_string())?;
753            }
754            Ok(())
755        })
756        .await
757        .map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?
758        .map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
759
760        Ok(())
761    }
762
763    /// Extract ZIP archives with path traversal protection
764    async fn extract_zip(
765        content: &[u8],
766        output_path: &Path,
767    ) -> Result<(), Box<dyn std::error::Error>> {
768        info!("Extracting ZIP archive to: {}", output_path.display());
769
770        let content = content.to_vec();
771        let dest = output_path
772            .parent()
773            .unwrap_or_else(|| Path::new("."))
774            .to_path_buf();
775
776        tokio::task::spawn_blocking(move || -> Result<(), String> {
777            use std::io::Cursor;
778            let cursor = Cursor::new(content);
779            let mut archive = zip::ZipArchive::new(cursor).map_err(|e| e.to_string())?;
780            let canonical_dest = dest.canonicalize().map_err(|e| e.to_string())?;
781
782            for i in 0..archive.len() {
783                let mut file = archive.by_index(i).map_err(|e| e.to_string())?;
784                let name = file
785                    .enclosed_name()
786                    .ok_or_else(|| format!("Zip Slip: entry {} has unsafe path", file.name()))?;
787
788                let target = canonical_dest.join(&name);
789                if !target.starts_with(&canonical_dest) {
790                    return Err(format!(
791                        "Zip Slip: entry escapes destination: {}",
792                        name.display()
793                    ));
794                }
795
796                if file.is_dir() {
797                    std::fs::create_dir_all(&target).map_err(|e| e.to_string())?;
798                } else {
799                    if let Some(parent) = target.parent() {
800                        std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
801                    }
802                    let mut out = std::fs::File::create(&target).map_err(|e| e.to_string())?;
803                    std::io::copy(&mut file, &mut out).map_err(|e| e.to_string())?;
804                }
805            }
806            Ok(())
807        })
808        .await
809        .map_err(|e| -> Box<dyn std::error::Error> { e.to_string().into() })?
810        .map_err(|e| -> Box<dyn std::error::Error> { e.into() })?;
811
812        Ok(())
813    }
814
815    /// Extract bzip2 archives
816    ///
817    /// NOTE: Requires the `bzip2 = "0.4"` crate (not currently in Cargo.toml).
818    /// Returns an error rather than silently writing unextracted bytes, which
819    /// would produce a corrupt feed file.
820    /// To enable: add `bzip2 = "0.4"` to Cargo.toml, then replace this body
821    /// with the same `spawn_blocking` decompression pattern used by
822    /// `extract_tar_gz`.
823    async fn extract_bzip2(
824        _content: &[u8],
825        output_path: &Path,
826    ) -> Result<(), Box<dyn std::error::Error>> {
827        Err(format!(
828            "bzip2 extraction is not supported: add the `bzip2` crate to Cargo.toml \
829             to enable decompression to {}",
830            output_path.display()
831        )
832        .into())
833    }
834
835    /// Extract XZ archives
836    ///
837    /// NOTE: Requires the `xz2 = "0.1"` crate (not currently in Cargo.toml).
838    /// Returns an error rather than silently writing unextracted bytes, which
839    /// would produce a corrupt feed file.
840    /// To enable: add `xz2 = "0.1"` to Cargo.toml, then replace this body
841    /// with the same `spawn_blocking` decompression pattern used by
842    /// `extract_tar_gz`.
843    async fn extract_xz(
844        _content: &[u8],
845        output_path: &Path,
846    ) -> Result<(), Box<dyn std::error::Error>> {
847        Err(format!(
848            "XZ extraction is not supported: add the `xz2` crate to Cargo.toml \
849             to enable decompression to {}",
850            output_path.display()
851        )
852        .into())
853    }
854
855    /// Validate that downloaded feed has expected format
856    fn validate_feed_format(
857        file_path: &Path,
858        format: &FeedFormat,
859    ) -> Result<(), Box<dyn std::error::Error>> {
860        let content = std::fs::read_to_string(file_path)?;
861
862        match format {
863            FeedFormat::PlainText => {
864                // Basic validation - check if it looks like IP addresses or networks
865                let lines: Vec<&str> = content
866                    .lines()
867                    .filter(|l| !l.trim().is_empty() && !l.starts_with('#'))
868                    .collect();
869                if lines.is_empty() {
870                    return Err("Feed appears to be empty".into());
871                }
872            }
873            FeedFormat::Csv => {
874                let mut reader = csv::Reader::from_reader(content.as_bytes());
875                if reader.headers().is_err() {
876                    return Err("Invalid CSV format".into());
877                }
878            }
879            FeedFormat::Json => {
880                serde_json::from_str::<serde_json::Value>(&content)?;
881            }
882            FeedFormat::Xml => {
883                // Basic XML validation - check for well-formed structure
884                if !content.trim_start().starts_with('<') {
885                    return Err("Invalid XML format".into());
886                }
887            }
888        }
889
890        debug!("Feed format validation passed: {}", file_path.display());
891        Ok(())
892    }
893
894    /// Get status of all feeds
895    pub async fn get_feed_status(&self) -> HashMap<String, FeedStatus> {
896        let mut status = HashMap::new();
897
898        for (feed_name, feed_config) in &self.config.feeds {
899            let file_path = self.config.feeds_directory.join(&feed_config.filename);
900
901            let feed_status = if feed_config.enabled {
902                if file_path.exists() {
903                    if let Ok(metadata) = fs::metadata(&file_path).await {
904                        FeedStatus::Active {
905                            last_updated: metadata
906                                .modified()
907                                .unwrap_or(std::time::SystemTime::UNIX_EPOCH),
908                            size_bytes: metadata.len(),
909                        }
910                    } else {
911                        FeedStatus::Error("Cannot read file metadata".to_string())
912                    }
913                } else {
914                    FeedStatus::NotDownloaded
915                }
916            } else {
917                FeedStatus::Disabled
918            };
919
920            status.insert(feed_name.clone(), feed_status);
921        }
922
923        status
924    }
925
926    /// Manually trigger update of specific feed
927    pub async fn update_feed(&self, feed_name: &str) -> Result<(), Box<dyn std::error::Error>> {
928        if let Some(feed_config) = self.config.feeds.get(feed_name) {
929            if feed_config.enabled {
930                Self::download_feed(&self.client, &self.config, feed_name, feed_config).await
931            } else {
932                Err(format!("Feed '{}' is disabled", feed_name).into())
933            }
934        } else {
935            Err(format!("Feed '{}' not found", feed_name).into())
936        }
937    }
938
939    /// Check if an IP address is in malicious IP feeds
940    pub fn is_malicious_ip(&self, ip: &std::net::IpAddr) -> bool {
941        for (feed_name, feed_config) in &self.config.feeds {
942            if !feed_config.enabled {
943                continue;
944            }
945
946            if matches!(feed_config.feed_type, FeedType::MaliciousIPs) {
947                let file_path = self.config.feeds_directory.join(&feed_config.filename);
948                if self.check_ip_in_feed(&file_path, ip) {
949                    tracing::warn!("Malicious IP detected: {} (source: {})", ip, feed_name);
950                    return true;
951                }
952            }
953        }
954        false
955    }
956
957    /// Check if an IP address is a Tor exit node
958    pub fn is_tor_exit(&self, ip: &std::net::IpAddr) -> bool {
959        for (feed_name, feed_config) in &self.config.feeds {
960            if !feed_config.enabled {
961                continue;
962            }
963
964            if matches!(feed_config.feed_type, FeedType::TorExitNodes) {
965                let file_path = self.config.feeds_directory.join(&feed_config.filename);
966                if self.check_ip_in_feed(&file_path, ip) {
967                    tracing::warn!("Tor exit node detected: {} (source: {})", ip, feed_name);
968                    return true;
969                }
970            }
971        }
972        false
973    }
974
975    /// Check if an IP address is from a VPN or proxy service
976    pub fn is_proxy_vpn(&self, ip: &std::net::IpAddr) -> bool {
977        for (feed_name, feed_config) in &self.config.feeds {
978            if !feed_config.enabled {
979                continue;
980            }
981
982            if matches!(feed_config.feed_type, FeedType::VpnProxy) {
983                let file_path = self.config.feeds_directory.join(&feed_config.filename);
984                if self.check_ip_in_feed(&file_path, ip) {
985                    tracing::info!("VPN/Proxy detected: {} (source: {})", ip, feed_name);
986                    return true;
987                }
988            }
989        }
990        false
991    }
992
993    /// Helper method to check if an IP is present in a feed file
994    fn check_ip_in_feed(&self, file_path: &std::path::Path, ip: &std::net::IpAddr) -> bool {
995        if !file_path.exists() {
996            return false;
997        }
998
999        if let Ok(contents) = std::fs::read_to_string(file_path) {
1000            for line in contents.lines() {
1001                let line = line.trim();
1002                if line.is_empty() || line.starts_with('#') {
1003                    continue;
1004                }
1005
1006                // Check exact IP match
1007                if line == ip.to_string() {
1008                    return true;
1009                }
1010
1011                // Check CIDR network match
1012                if line.contains('/') {
1013                    match ip {
1014                        std::net::IpAddr::V4(ipv4) => {
1015                            if let Ok(network) = line.parse::<ipnetwork::Ipv4Network>()
1016                                && network.contains(*ipv4)
1017                            {
1018                                return true;
1019                            }
1020                        }
1021                        std::net::IpAddr::V6(ipv6) => {
1022                            if let Ok(network) = line.parse::<ipnetwork::Ipv6Network>()
1023                                && network.contains(*ipv6)
1024                            {
1025                                return true;
1026                            }
1027                        }
1028                    }
1029                }
1030            }
1031        }
1032
1033        false
1034    }
1035}
1036
1037/// Status of a threat intelligence feed
1038#[derive(Debug, Clone)]
1039pub enum FeedStatus {
1040    /// Feed is disabled
1041    Disabled,
1042
1043    /// Feed is enabled but not yet downloaded
1044    NotDownloaded,
1045
1046    /// Feed is active and up-to-date
1047    Active {
1048        last_updated: std::time::SystemTime,
1049        size_bytes: u64,
1050    },
1051
1052    /// Feed has an error
1053    Error(String),
1054}