1use std::collections::BTreeMap;
11
12use encoding_rs::Encoding;
13use roxmltree::{Document as XmlDocument, Node, ParsingOptions};
14
15use crate::{Error, ErrorCode, Limits};
16
17#[derive(Clone, Debug)]
22pub struct XmlSource {
23 text: String,
24 encoding: String,
25}
26
27impl XmlSource {
28 pub fn decode(bytes: &[u8], limits: &Limits) -> Result<Self, Error> {
31 if bytes.len() as u64 > limits.max_input_bytes {
32 return Err(Error::new(
33 ErrorCode::InputTooLarge,
34 format!("input exceeds {} bytes", limits.max_input_bytes),
35 ));
36 }
37 let (text, encoding) = decode_xml(bytes)?;
38 crate::scan::prescan(&text, limits)?;
41 Ok(Self { text, encoding })
42 }
43
44 pub fn text(&self) -> &str {
46 &self.text
47 }
48
49 pub fn encoding(&self) -> &str {
51 &self.encoding
52 }
53
54 pub fn parse_tree(&self, limits: &Limits) -> Result<XmlDocument<'_>, Error> {
56 let parsing = ParsingOptions {
57 allow_dtd: false,
58 nodes_limit: u32::try_from(limits.max_xml_nodes.saturating_mul(4)).unwrap_or(u32::MAX),
61 ..ParsingOptions::default()
62 };
63 XmlDocument::parse_with_options(&self.text, parsing).map_err(xml_error)
64 }
65}
66
67pub fn id_map<'a, 'input>(
73 root: Node<'a, 'input>,
74) -> Result<BTreeMap<&'a str, Node<'a, 'input>>, Error> {
75 let mut ids = BTreeMap::new();
76 for node in root.descendants().filter(Node::is_element) {
77 for attribute in node
78 .attributes()
79 .filter(|attribute| attribute.namespace().is_none())
80 {
81 if !matches!(attribute.name(), "Id" | "ID" | "id") {
82 continue;
83 }
84 if attribute.value().is_empty() || ids.insert(attribute.value(), node).is_some() {
85 return Err(Error::new(
86 ErrorCode::DuplicateId,
87 "XML IDs must be non-empty and unique",
88 ));
89 }
90 }
91 }
92 Ok(ids)
93}
94
95fn decode_xml(bytes: &[u8]) -> Result<(String, String), Error> {
96 let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]).unwrap_or(bytes);
97 let label = declared_encoding(bytes)
98 .map(|declaration| declaration.label)
99 .unwrap_or_else(|| "UTF-8".to_owned());
100 let normalized = label.to_ascii_lowercase().replace('_', "-");
101 if normalized == "utf-8" || normalized == "utf8" {
102 return String::from_utf8(bytes.to_vec())
103 .map(|text| (text, "UTF-8".to_owned()))
104 .map_err(|_| Error::new(ErrorCode::InvalidEncoding, "input is not valid UTF-8"));
105 }
106 if normalized != "iso-8859-2" && normalized != "iso8859-2" {
107 return Err(Error::new(
108 ErrorCode::UnsupportedEncoding,
109 "only UTF-8 and ISO-8859-2 XML encodings are supported",
110 ));
111 }
112 let encoding = Encoding::for_label(b"iso-8859-2").expect("encoding_rs has ISO-8859-2");
113 let (decoded, _, had_errors) = encoding.decode(bytes);
114 if had_errors {
115 return Err(Error::new(
116 ErrorCode::InvalidEncoding,
117 "input is not valid ISO-8859-2",
118 ));
119 }
120 Ok((decoded.into_owned(), "ISO-8859-2".to_owned()))
121}
122
123#[derive(Clone, Debug, Eq, PartialEq)]
129pub struct EncodingDeclaration {
130 pub label: String,
132 pub start: usize,
134 pub end: usize,
136}
137
138pub fn declared_encoding(bytes: &[u8]) -> Option<EncodingDeclaration> {
150 const MARK: &[u8] = &[0xEF, 0xBB, 0xBF];
151 let offset = if bytes.starts_with(MARK) {
152 MARK.len()
153 } else {
154 0
155 };
156 let bytes = &bytes[offset..];
157 let prefix = &bytes[..bytes.len().min(512)];
158 if !prefix.starts_with(b"<?xml") || !prefix.get(5..)?.first()?.is_ascii_whitespace() {
159 return None;
160 }
161 let end = prefix.windows(2).position(|window| window == b"?>")?;
162 let declaration = std::str::from_utf8(&prefix[5..end]).ok()?;
163 let base = offset + 5;
166 let mut cursor = 0;
167 while let Some(position) = declaration[cursor..].find("encoding") {
168 let name_at = cursor + position;
169 let preceded_by_space = declaration[..name_at]
170 .chars()
171 .next_back()
172 .is_some_and(char::is_whitespace);
173 cursor = name_at + "encoding".len();
174 if !preceded_by_space {
175 continue;
176 }
177 let after = &declaration[cursor..];
178 let trimmed = after.trim_start();
179 let Some(value) = trimmed.strip_prefix('=') else {
180 continue;
181 };
182 let mut at = cursor + (after.len() - trimmed.len()) + 1;
185 let trimmed = value.trim_start();
186 at += value.len() - trimmed.len();
187 let quote = trimmed.chars().next()?;
188 if quote != '\'' && quote != '"' {
189 return None;
190 }
191 at += quote.len_utf8();
192 let label = &trimmed[quote.len_utf8()..];
193 let close = label.find(quote)?;
194 return Some(EncodingDeclaration {
195 label: label[..close].to_owned(),
196 start: base + at,
197 end: base + at + close,
198 });
199 }
200 None
201}
202
203fn xml_error(error: roxmltree::Error) -> Error {
207 match error {
208 roxmltree::Error::DtdDetected => Error::new(
209 ErrorCode::UnsafeXml,
210 "DTD and entity declarations are not allowed",
211 ),
212 roxmltree::Error::NodesLimitReached
213 | roxmltree::Error::AttributesLimitReached
214 | roxmltree::Error::NamespacesLimitReached => {
215 Error::new(ErrorCode::UnsafeXml, "XML exceeds the node limit")
216 }
217 other => {
218 let position = other.pos();
219 Error::new(
220 ErrorCode::InvalidXml,
221 format!(
222 "XML parsing failed at line {} column {}",
223 position.row, position.col
224 ),
225 )
226 }
227 }
228}
229
230#[cfg(test)]
231mod tests {
232 use super::*;
233
234 #[test]
244 fn a_doctype_is_rejected_before_the_tree_parser_runs() {
245 let xml = b"<!DOCTYPE a><a/>";
246 let error = XmlSource::decode(xml, &Limits::default()).unwrap_err();
247 assert_eq!(error.code(), ErrorCode::UnsafeXml);
248 }
249
250 #[test]
255 fn a_dtd_free_document_still_parses_with_the_hardened_options() {
256 let source = XmlSource::decode(b"<a><b/></a>", &Limits::default()).unwrap();
257 let tree = source.parse_tree(&Limits::default()).unwrap();
258 assert_eq!(tree.root_element().tag_name().name(), "a");
259 }
260
261 #[test]
264 fn every_declaration_spelling_yields_the_label_and_its_range() {
265 for text in [
266 "<?xml version=\"1.0\" encoding=\"ISO-8859-2\"?><a/>",
267 "<?xml version='1.0' encoding='ISO-8859-2'?><a/>",
268 "<?xml version=\"1.0\" encoding = \"ISO-8859-2\"?><a/>",
269 "<?xml version=\"1.0\" encoding\t=\n'ISO-8859-2'?><a/>",
270 ] {
271 let found = declared_encoding(text.as_bytes())
272 .unwrap_or_else(|| panic!("{text} declares an encoding"));
273 assert_eq!(found.label, "ISO-8859-2", "{text}");
274 assert_eq!(&text[found.start..found.end], "ISO-8859-2", "{text}");
275 }
276 }
277
278 #[test]
280 fn the_offsets_are_into_the_bytes_as_given() {
281 let mut bytes = vec![0xEF, 0xBB, 0xBF];
282 bytes.extend_from_slice(b"<?xml version='1.0' encoding='utf8'?><a/>");
283 let found = declared_encoding(&bytes).expect("the declaration is read past the mark");
284 assert_eq!(found.label, "utf8");
285 assert_eq!(&bytes[found.start..found.end], b"utf8");
286 }
287
288 #[test]
291 fn only_the_declarations_own_encoding_is_read() {
292 for text in [
293 "<a/>",
294 "<?xml version=\"1.0\"?><a/>",
295 "<?xml version=\"1.0\"?><!-- encoding=\"ISO-8859-2\" --><a/>",
296 "<?xml version=\"1.0\" xencoding=\"ISO-8859-2\"?><a/>",
299 ] {
300 assert_eq!(declared_encoding(text.as_bytes()), None, "{text}");
301 }
302 }
303
304 #[test]
307 fn the_label_is_matched_without_regard_to_case() {
308 let source = XmlSource::decode(
309 "<?xml version='1.0' encoding='ISO-8859-2'?><a/>".as_bytes(),
310 &Limits::default(),
311 )
312 .expect("a single-quoted declaration decodes");
313 assert_eq!(source.encoding(), "ISO-8859-2");
314 let source = XmlSource::decode(
315 "<?xml version=\"1.0\" encoding = \"utf-8\"?><a/>".as_bytes(),
316 &Limits::default(),
317 )
318 .expect("spaces around the equals sign decode");
319 assert_eq!(source.encoding(), "UTF-8");
320 }
321}