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
//! # Parse Options & Security Policy
//!
//! Provides configurable parsing options and security thresholds to mitigate DoS and XXE vulnerabilities.
use crate::alloc_prelude::*;
use crate::error::{Result, XmlError};
/// Security policy options and resource limits for the XML parser.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseOptions {
/// Maximum XML document size in bytes (default: 100 MB).
pub max_xml_size: usize,
/// Maximum depth for recursive entity reference expansion (default: 512).
pub max_entity_expansion_depth: usize,
/// Maximum element nesting depth (default: 1000).
pub max_nesting_depth: usize,
/// Maximum total element count in document (default: 1,000,000).
pub max_element_count: usize,
/// Maximum attribute count per element (default: 10,000).
pub max_attribute_count: usize,
/// Maximum total attribute count across document (default: 1,000,000).
pub max_total_attribute_count: usize,
/// Maximum text node character size (default: 1 MB).
pub max_text_node_size: usize,
/// Maximum total cumulative entity expansion size in bytes (default: 10 MB, Billion Laughs guard).
pub max_total_entity_expansion_size: usize,
/// Allow fetching external entity URIs (default: false for XXE protection).
pub allow_external_entities: bool,
/// Base directory for resolving relative external entity and DTD paths.
pub base_dir: Option<String>,
/// Whether the document was decoded from a UTF-16 byte stream.
pub is_utf16: bool,
/// Whether XML namespace validation rules are enforced (default: true).
pub namespace_aware: bool,
}
impl Default for ParseOptions {
fn default() -> Self {
Self {
max_xml_size: 100 * 1024 * 1024,
max_entity_expansion_depth: 512,
max_nesting_depth: 1000,
max_element_count: 1_000_000,
max_attribute_count: 10_000,
max_total_attribute_count: 1_000_000,
max_text_node_size: 1024 * 1024,
max_total_entity_expansion_size: 10 * 1024 * 1024,
allow_external_entities: false,
base_dir: None,
is_utf16: false,
namespace_aware: true,
}
}
}
impl ParseOptions {
/// Validates element nesting depth against `max_nesting_depth`.
pub fn check_nesting_depth(&self, depth: usize) -> Result<()> {
if depth > self.max_nesting_depth {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum nesting depth of {} exceeded",
self.max_nesting_depth
)))
} else {
Ok(())
}
}
/// Validates total element count against `max_element_count`.
pub fn check_element_count(&self, count: usize) -> Result<()> {
if count > self.max_element_count {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum element count of {} exceeded",
self.max_element_count
)))
} else {
Ok(())
}
}
/// Validates tag attribute count against `max_attribute_count`.
pub fn check_attribute_count(&self, count: usize) -> Result<()> {
if count > self.max_attribute_count {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum element attribute count of {} exceeded",
self.max_attribute_count
)))
} else {
Ok(())
}
}
/// Validates total attribute count across document against `max_total_attribute_count`.
pub fn check_total_attribute_count(&self, count: usize) -> Result<()> {
if count > self.max_total_attribute_count {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum total attribute count of {} exceeded",
self.max_total_attribute_count
)))
} else {
Ok(())
}
}
/// Validates overall XML document size against `max_xml_size`.
pub fn check_xml_size(&self, size: usize) -> Result<()> {
if size > self.max_xml_size {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum XML document size of {} bytes exceeded (found {})",
self.max_xml_size, size
)))
} else {
Ok(())
}
}
/// Validates character size of a text/CDATA node against `max_text_node_size`.
pub fn check_text_node_size(&self, size: usize) -> Result<()> {
if size > self.max_text_node_size {
Err(XmlError::SecurityLimitExceeded(format!(
"Maximum text node size of {} characters exceeded (found {})",
self.max_text_node_size, size
)))
} else {
Ok(())
}
}
}