Skip to main content

openszigno_core/
xml.rs

1//! Bounded XML access shared by structural parsing and signature verification.
2//!
3//! Both `openszigno_core::parse` and `openszigno-verify` must see *exactly* the
4//! same tree: a verifier that resolved references in a second, differently
5//! parsed tree would be open to the signature-wrapping class of attack this
6//! project cares most about. This module is therefore the only place that turns
7//! dossier bytes into text and text into a `roxmltree` tree, and the only place
8//! that defines the XML ID space.
9
10use std::collections::BTreeMap;
11
12use encoding_rs::Encoding;
13use roxmltree::{Document as XmlDocument, Node, ParsingOptions};
14
15use crate::{Error, ErrorCode, Limits};
16
17/// Decoded, pre-scanned dossier text, ready to be parsed into a tree.
18///
19/// Constructing one applies the input-size limit, the encoding policy, and the
20/// pre-parse scan; it never allocates more than a copy of the input text.
21#[derive(Clone, Debug)]
22pub struct XmlSource {
23    text: String,
24    encoding: String,
25}
26
27impl XmlSource {
28    /// Apply the size limit, decode the declared encoding, and run the
29    /// pre-parse scan that rejects DTDs, deep nesting, and huge node counts.
30    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        // Reject DTDs, deep nesting, and huge node counts before the recursive
39        // tree parser runs; see `scan.rs` for why this cannot be delegated.
40        crate::scan::prescan(&text, limits)?;
41        Ok(Self { text, encoding })
42    }
43
44    /// The decoded document text.
45    pub fn text(&self) -> &str {
46        &self.text
47    }
48
49    /// The XML encoding that was applied: `UTF-8` or `ISO-8859-2`.
50    pub fn encoding(&self) -> &str {
51        &self.encoding
52    }
53
54    /// Parse the pre-scanned text with DTDs disabled and a bounded node count.
55    pub fn parse_tree(&self, limits: &Limits) -> Result<XmlDocument<'_>, Error> {
56        let parsing = ParsingOptions {
57            allow_dtd: false,
58            // The pre-scan bounds elements; the parser also counts text and
59            // comment nodes, so its backstop limit is proportionally larger.
60            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
67/// The XML ID space of a dossier, keyed by ID value.
68///
69/// Only unprefixed `Id`/`ID`/`id` attributes take part, matching the structural
70/// parser's `OBJREF` resolution. An empty or repeated ID is the hard error
71/// `duplicate_id`, so a caller can never resolve a reference ambiguously.
72pub 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/// The `encoding` pseudo-attribute of an XML declaration, and where its value
124/// sits in the bytes it was read from.
125///
126/// The range is the label alone, quotes excluded, so a writer that has to
127/// restate the encoding can rewrite exactly those bytes and nothing else.
128#[derive(Clone, Debug, Eq, PartialEq)]
129pub struct EncodingDeclaration {
130    /// The label exactly as written, neither trimmed nor case-folded.
131    pub label: String,
132    /// The byte offset of the first byte of the label.
133    pub start: usize,
134    /// The byte offset one past the last byte of the label.
135    pub end: usize,
136}
137
138/// Read the `encoding` pseudo-attribute of the XML declaration only.
139///
140/// The search is bounded to the declaration itself so that comments or
141/// content later in the prolog cannot choose how the document is decoded, and
142/// it accepts every spelling XML allows there: either quote character, and
143/// any whitespace around the `=`. It is the one reading of that declaration
144/// this project has: `XmlSource::decode` decides the input encoding with it,
145/// and a writer restating the declaration must agree with it byte for byte.
146///
147/// A leading byte order mark is skipped, and the offsets returned are into
148/// `bytes` as given, mark included.
149pub 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    // Every offset below is relative to `declaration`; `base` turns one into
164    // an offset into `bytes` as the caller handed them over.
165    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        // The label starts after the name, the whitespace, the `=`, more
183        // whitespace, and the opening quote.
184        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
203/// Map a parser error to a stable code without echoing document content.
204/// `roxmltree` error messages include element, attribute, and entity names,
205/// which may be confidential; only the position is kept.
206fn 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    /// A document with a `DOCTYPE` is rejected through the public
235    /// `XmlSource::decode` path, which is what `crate::parse` and every
236    /// caller actually go through.
237    ///
238    /// This is caught by `scan::prescan`'s unconditional rejection of any
239    /// `<!...>` markup (see `scan.rs`), which runs before `parse_tree` (and
240    /// so before `ParsingOptions.allow_dtd`) ever sees the text. The
241    /// `allow_dtd: false` wiring in `parse_tree` is deliberate defence in
242    /// depth for that same rule, not the only thing enforcing it here.
243    #[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    /// Once text has passed the pre-scan (so contains no `<!` markup at
251    /// all), `parse_tree` builds a tree from it with `allow_dtd: false`. This
252    /// documents that a normal, DTD-free document still parses successfully
253    /// through the same options value the DOCTYPE case above never reaches.
254    #[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    /// The label, and the range that names it, for every spelling the
262    /// declaration allows.
263    #[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    /// A byte order mark shifts the offsets and nothing else.
279    #[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    /// Nothing outside the declaration is read, and a declaration that names
289    /// no encoding names none.
290    #[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            // `encoding` has to be a pseudo-attribute of its own, not the
297            // tail of another name.
298            "<?xml version=\"1.0\" xencoding=\"ISO-8859-2\"?><a/>",
299        ] {
300            assert_eq!(declared_encoding(text.as_bytes()), None, "{text}");
301        }
302    }
303
304    /// An upper-case label decodes as the same encoding a lower-case one
305    /// does, and both reach the caller as written.
306    #[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}