pub const MAX_DOCUMENT_DEPTH: usize = 1024;
pub(crate) const MAX_SCHEMA_DEPTH: usize = 512;
#[cfg(feature = "schematron")]
pub(crate) const MAX_RESOURCE_BYTES: u64 = 256 * 1024 * 1024;
#[cfg(feature = "schematron")]
pub(crate) const MAX_RESOURCES: usize = 1000;
#[cfg(feature = "schematron")]
pub(crate) const MAX_RESOURCES_BYTES: u64 = 1024 * 1024 * 1024;
pub(crate) fn xml_nesting_exceeds(text: &str, limit: usize) -> bool {
let bytes = text.as_bytes();
let skip_past = |from: usize, end: &[u8]| {
bytes[from..]
.windows(end.len())
.position(|window| window == end)
.map_or(bytes.len(), |offset| from + offset + end.len())
};
let mut depth = 0usize;
let mut i = 0;
while i < bytes.len() {
match text[i..].find('<') {
Some(offset) => i += offset,
None => break,
}
let rest = &bytes[i..];
if rest.starts_with(b"<!--") {
i = skip_past(i + 4, b"-->");
} else if rest.starts_with(b"<![CDATA[") {
i = skip_past(i + 9, b"]]>");
} else if rest.starts_with(b"<?") {
i = skip_past(i + 2, b"?>");
} else if rest.starts_with(b"<!") {
let mut brackets = 0usize;
let mut quote = None;
i += 2;
while i < bytes.len() {
let byte = bytes[i];
i += 1;
match (quote, byte) {
(Some(q), _) if byte == q => quote = None,
(Some(_), _) => {}
(None, b'"' | b'\'') => quote = Some(byte),
(None, b'[') => brackets += 1,
(None, b']') => brackets = brackets.saturating_sub(1),
(None, b'>') if brackets == 0 => break,
_ => {}
}
}
} else if rest.starts_with(b"</") {
depth = depth.saturating_sub(1);
i = skip_past(i + 2, b">");
} else {
let mut quote = None;
let mut self_closing = false;
i += 1;
while i < bytes.len() {
let byte = bytes[i];
i += 1;
match (quote, byte) {
(Some(q), _) if byte == q => quote = None,
(Some(_), _) => {}
(None, b'"' | b'\'') => quote = Some(byte),
(None, b'>') => {
self_closing = bytes[i - 2] == b'/';
break;
}
_ => {}
}
}
if !self_closing {
depth += 1;
if depth > limit {
return true;
}
}
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn counts_element_nesting_only() {
let text = r#"<?xml version="1.0"?><!DOCTYPE a [<!ENTITY e "<b>">]>
<a x="<c>"><!-- <d><d><d> --><![CDATA[<e><e>]]><f/><g><?pi <h>?></g></a>"#;
assert!(!xml_nesting_exceeds(text, 2));
assert!(xml_nesting_exceeds(text, 1));
}
#[test]
fn deep_documents_are_detected_without_parsing() {
let deep = "<a>".repeat(100_000) + &"</a>".repeat(100_000);
assert!(xml_nesting_exceeds(&deep, MAX_DOCUMENT_DEPTH));
let ok = "<a>".repeat(MAX_DOCUMENT_DEPTH) + &"</a>".repeat(MAX_DOCUMENT_DEPTH);
assert!(!xml_nesting_exceeds(&ok, MAX_DOCUMENT_DEPTH));
}
}