ddex_parser/parser/
security.rs

1use std::time::Duration;
2
3/// Security configuration for XML parsing
4#[derive(Debug, Clone)]
5pub struct SecurityConfig {
6    // Entity expansion protection
7    pub disable_dtd: bool,
8    pub disable_external_entities: bool,
9    pub max_entity_expansions: usize,
10    pub max_entity_depth: usize,
11    
12    // Size limits
13    pub max_element_depth: usize,
14    pub max_attribute_size: usize,
15    pub max_text_size: usize,
16    pub max_file_size: usize,
17    
18    // Time limits
19    pub parse_timeout: Duration,
20    pub stream_timeout: Duration,
21    
22    // Network protection
23    pub allow_network: bool,
24    pub allowed_schemas: Vec<String>,
25}
26
27impl Default for SecurityConfig {
28    fn default() -> Self {
29        Self::strict()
30    }
31}
32
33impl SecurityConfig {
34    /// Strict security configuration (default)
35    pub fn strict() -> Self {
36        Self {
37            disable_dtd: true,
38            disable_external_entities: true,
39            max_entity_expansions: 1000,
40            max_entity_depth: 20,
41            max_element_depth: 100,
42            max_attribute_size: 100 * 1024,  // 100KB
43            max_text_size: 1024 * 1024,      // 1MB
44            max_file_size: 1024 * 1024 * 1024, // 1GB
45            parse_timeout: Duration::from_secs(30),
46            stream_timeout: Duration::from_secs(300),
47            allow_network: false,
48            allowed_schemas: vec!["file".to_string()],
49        }
50    }
51    
52    /// Relaxed configuration for trusted sources
53    pub fn relaxed() -> Self {
54        Self {
55            max_element_depth: 200,
56            max_file_size: if cfg!(target_arch = "wasm32") { 
57                100 * 1024 * 1024 // 100MB for WASM 
58            } else { 
59                5 * 1024 * 1024 * 1024 // 5GB for native
60            },
61            parse_timeout: Duration::from_secs(120),
62            stream_timeout: Duration::from_secs(600),
63            ..Self::strict()
64        }
65    }
66}