| 1 |
XML Parser in Rust |
| 2 |
================== |
| 3 |
|
| 4 |
**Status as of 2025/May/02: not implemented** |
| 5 |
|
| 6 |
The purpose of this proposal is to replace libxml2 in librsvg with a |
| 7 |
Rust-based XML parser. |
| 8 |
|
| 9 |
Librsvg uses `libxml2 <https://gitlab.gnome.org/GNOME/libxml2>`_ to do |
| 10 |
the initial XML parsing of an SVG document. It does not let libxml2 |
| 11 |
build its own tree representation; instead, it uses the SAX2 |
| 12 |
"streaming" parser API and so librsvg builds a tree of its own with |
| 13 |
tag names and attributes. |
| 14 |
|
| 15 |
Pragmatically speaking, there is nothing wrong with using libxml2 in |
| 16 |
the way that librsvg uses it: |
| 17 |
|
| 18 |
* Libxml2 is fast. |
| 19 |
|
| 20 |
* It is well-maintained, is fuzz-tested at scale, and is such a |
| 21 |
critical piece of infrastructure that people actually pay attention |
| 22 |
to it. |
| 23 |
|
| 24 |
* It has built-in mitigations for common XML attacks like the |
| 25 |
"`billion laughs |
| 26 |
<https://en.wikipedia.org/wiki/Billion_laughs_attack>`". |
| 27 |
|
| 28 |
* Librsvg is careful to turn off features like network access and |
| 29 |
external XML entities, which are a well-known source of |
| 30 |
attacks. |
| 31 |
|
| 32 |
However, libxml2 has had many CVEs and security problems in the past. |
| 33 |
It is the sort of infrastructure that should be replaced with |
| 34 |
memory-safe code at some point. |
| 35 |
|
| 36 |
Steps |
| 37 |
----- |
| 38 |
|
| 39 |
1. Separate the XML tree from the SVG element tree. |
| 40 |
|
| 41 |
2. Change the XML tree to one that is produced by the new Rust-based |
| 42 |
XML parser. |
| 43 |
|
| 44 |
The sections below explore each of these steps. |
| 45 |
|
| 46 |
Separating the XML tree from the SVG element tree |
| 47 |
------------------------------------------------- |
| 48 |
|
| 49 |
Librsvg has a tree data structure, managed by the ``rctree`` crate, |
| 50 |
where each node is a combination of XML data (element name for the |
| 51 |
tag, and a list of attributes with their string values) and the parsed |
| 52 |
SVG data (individual structs for ``Group``, ``Path``, etc., plus |
| 53 |
parsed properties and element-specific attributes). |
| 54 |
|
| 55 |
From ``document.rs``: |
| 56 |
|
| 57 |
.. code-block:: rust |
| 58 |
|
| 59 |
pub struct Document { |
| 60 |
/// Tree of nodes; the root is guaranteed to be an `<svg>` element. |
| 61 |
tree: Node, |
| 62 |
|
| 63 |
// ... |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
From ``node.rs``: |
| 68 |
|
| 69 |
.. code-block:: rust |
| 70 |
|
| 71 |
pub type Node = rctree::Node<NodeData>; |
| 72 |
|
| 73 |
pub enum NodeData { |
| 74 |
Element(Box<Element>), |
| 75 |
Text(Box<Chars>), |
| 76 |
} |
| 77 |
|
| 78 |
From ``xml/attributes.rs``: |
| 79 |
|
| 80 |
.. code-block:: rust |
| 81 |
|
| 82 |
pub struct Attributes { |
| 83 |
attrs: Box<[(QualName, AttributeValue)]>, |
| 84 |
// ... |
| 85 |
} |
| 86 |
|
| 87 |
From ``element.rs``: |
| 88 |
|
| 89 |
.. code-block:: rust |
| 90 |
|
| 91 |
pub struct Element { |
| 92 |
element_name: QualName, |
| 93 |
attributes: Attributes, |
| 94 |
|
| 95 |
specified_values: SpecifiedValues, |
| 96 |
pub element_data: ElementData, |
| 97 |
// ... some fields omitted |
| 98 |
} |
| 99 |
|
| 100 |
pub enum ElementData { |
| 101 |
Circle(Box<Circle>), |
| 102 |
ClipPath(Box<ClipPath>), |
| 103 |
Ellipse(Box<Ellipse>), |
| 104 |
// ... |
| 105 |
} |
| 106 |
|
| 107 |
Here, ``struct Element`` is a combination of XML string data |
| 108 |
(``element_name``, ``attributes``), plus the result of parsing those |
| 109 |
strings into SVG and CSS-specific information (``specified_values``, |
| 110 |
``element_data``). |
| 111 |
|
| 112 |
**Goal:** Basically, have ``Element`` *not* contain XML string data. |
| 113 |
It may contain a pointer back to its corresponding XML node, and that |
| 114 |
may even depend on what the crate that represents that XML tree lets |
| 115 |
us do. |
| 116 |
|
| 117 |
Things to consider |
| 118 |
~~~~~~~~~~~~~~~~~~ |
| 119 |
|
| 120 |
* With the libxml2-based SAX2 parser, as soon as librsvg gets a "start |
| 121 |
element" event it will parse each value in the list of attributes. |
| 122 |
It will then use this information to construct an ``Element`` and |
| 123 |
then a ``Node``. We may have to change this "build from the inside |
| 124 |
out" process to instead assume that an XML tree is available and |
| 125 |
full of strings, and later an SVG tree can be constructed from it. |
| 126 |
|
| 127 |
|
| 128 |
Change the XML tree to one from a Rust-based parser |
| 129 |
--------------------------------------------------- |
| 130 |
|
| 131 |
FIXME |
| 132 |
|
| 133 |
* The code in ``css.rs`` which implements the ``selectors::Element`` |
| 134 |
trait for nodes in the tree, needs O(1) access to a node's parent and |
| 135 |
to its next sibling. |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
Notes |
| 140 |
----- |
| 141 |
|
| 142 |
This used to be https://gitlab.gnome.org/GNOME/librsvg/-/issues/224 |
| 143 |
but it was mostly a wishlist item, instead of a specification document |
| 144 |
like the present one. |