|
1 | | -"""XML parsers of the standard library and lxml expand entities: a crafted document reads |
2 | | -local files or exhausts memory. ``defusedxml`` offers the same functions safely.""" |
| 1 | +"""XML parsers of the standard library and of lxml, each reported for what it risks. |
| 2 | +
|
| 3 | +The standard library's parsers run on Expat, which never reads a local file or opens a |
| 4 | +connection, but expands the entities a document declares: a crafted document can exhaust |
| 5 | +memory with Expat older than 2.7.2, a fact of run time (``pyexpat.EXPAT_VERSION``). lxml |
| 6 | +resolves external entities by default before 5.0, and in ``iterparse`` and |
| 7 | +``ETCompatXMLParser`` before 6.1 (CVE-2026-41066), which lets a crafted document read |
| 8 | +local files; ``resolve_entities=True`` does so whatever the version, ``False`` never. |
| 9 | +``'internal'`` keeps external entities out only from lxml 5.0 on: an earlier lxml reads |
| 10 | +the string as true. ``defusedxml`` offers the same functions safely. XML content the |
| 11 | +project wrote itself, a string constant or a module-level string bound once given to a |
| 12 | +function that parses its argument as the document, is no attacker's: its finding is kept |
| 13 | +at low confidence. A path is not the document: whoever can write the file chooses it.""" |
3 | 14 |
|
4 | 15 | from __future__ import annotations |
5 | 16 |
|
| 17 | +from collections.abc import Mapping |
6 | 18 | from typing import ClassVar |
7 | 19 |
|
| 20 | +from coretrace_python.abstract.strings import ModuleStringsAnalysis |
8 | 21 | from coretrace_python.analysis import AnyAnalysis |
9 | | -from coretrace_python.findings import Severity |
| 22 | +from coretrace_python.findings import Confidence, Severity |
10 | 23 | from coretrace_python.interprocedural import CallGraphAnalysis |
| 24 | +from coretrace_python.ir.model import Call, Constant, Global, Instruction, Value |
11 | 25 | from coretrace_python.ir.ssa import SSAAnalysis |
12 | | -from coretrace_python.plugins import SymbolCallDetector |
| 26 | +from coretrace_python.plugins import CallReport, PluginContext, SymbolCallDetector |
13 | 27 | from coretrace_python.semantic.symbols import SymbolId |
14 | 28 |
|
15 | | -_PARSERS = ( |
| 29 | + |
| 30 | +def _symbols(*paths: str) -> frozenset[SymbolId]: |
| 31 | + return frozenset(SymbolId(f"python.{path}") for path in paths) |
| 32 | + |
| 33 | + |
| 34 | +_STANDARD_LIBRARY = _symbols( |
16 | 35 | "xml.etree.ElementTree.parse", "xml.etree.ElementTree.fromstring", "xml.etree.ElementTree.iterparse", |
17 | 36 | "xml.etree.ElementTree.XMLParser", "xml.etree.ElementTree.fromstringlist", |
18 | 37 | "xml.dom.minidom.parse", "xml.dom.minidom.parseString", |
19 | 38 | "xml.dom.pulldom.parse", "xml.dom.pulldom.parseString", |
20 | 39 | "xml.sax.parse", "xml.sax.parseString", "xml.sax.make_parser", |
21 | 40 | "xml.dom.expatbuilder.parse", "xml.dom.expatbuilder.parseString", |
22 | | - "lxml.etree.parse", "lxml.etree.fromstring", "lxml.etree.XMLParser", "lxml.etree.iterparse", |
23 | | - "lxml.etree.XML", |
24 | 41 | ) |
| 42 | +_LXML = _symbols("lxml.etree.parse", "lxml.etree.fromstring", "lxml.etree.XMLParser", "lxml.etree.XML") |
| 43 | +# Their default kept ``resolve_entities=True`` until lxml 6.1 (CVE-2026-41066). |
| 44 | +_LXML_UNTIL_6_1 = _symbols("lxml.etree.iterparse", "lxml.etree.ETCompatXMLParser") |
| 45 | +# The functions whose first argument is the XML itself, not a file, a URL or a stream. |
| 46 | +_CONTENT = _symbols( |
| 47 | + "xml.etree.ElementTree.fromstring", "xml.etree.ElementTree.fromstringlist", |
| 48 | + "xml.dom.minidom.parseString", "xml.dom.pulldom.parseString", "xml.sax.parseString", |
| 49 | + "xml.dom.expatbuilder.parseString", "lxml.etree.fromstring", "lxml.etree.XML", |
| 50 | +) |
| 51 | +# A keyword that is not a constant: its value is unknown. |
| 52 | +_UNKNOWN = object() |
| 53 | + |
| 54 | + |
| 55 | +def _resolve_entities(call: Call, defs: Mapping[Value, Instruction]) -> object: |
| 56 | + """The constant ``resolve_entities=`` of ``call``, ``None`` when it is not passed.""" |
| 57 | + |
| 58 | + for name, value in call.keywords: |
| 59 | + if name == "resolve_entities": |
| 60 | + made = defs.get(value) |
| 61 | + return made.value if isinstance(made, Constant) else _UNKNOWN |
| 62 | + return None |
25 | 63 |
|
26 | 64 |
|
27 | 65 | class UnsafeXmlPlugin(SymbolCallDetector): |
28 | 66 | name: ClassVar[str] = "unsafe-xml" |
29 | 67 | rule_id: ClassVar[str] = "unsafe-xml" |
30 | | - requires: ClassVar[frozenset[AnyAnalysis]] = frozenset({SSAAnalysis, CallGraphAnalysis}) |
31 | | - symbols: ClassVar[frozenset[SymbolId]] = frozenset(SymbolId(f"python.{p}") for p in _PARSERS) |
| 68 | + requires: ClassVar[frozenset[AnyAnalysis]] = frozenset({SSAAnalysis, CallGraphAnalysis, ModuleStringsAnalysis}) |
| 69 | + symbols: ClassVar[frozenset[SymbolId]] = _STANDARD_LIBRARY | _LXML | _LXML_UNTIL_6_1 |
32 | 70 | severity: ClassVar[Severity] = Severity.MEDIUM |
33 | | - message_template: ClassVar[str] = ( |
34 | | - "call to {symbol} parses XML with entity expansion enabled; use defusedxml" |
35 | | - ) |
| 71 | + message_template: ClassVar[str] = "call to {symbol} parses XML unsafely; use defusedxml" |
| 72 | + |
| 73 | + def accepts(self, call: Call, symbol: SymbolId, defs: Mapping[Value, Instruction]) -> bool: |
| 74 | + # ``resolve_entities`` is an lxml option; the standard library has none. |
| 75 | + return _resolve_entities(call, defs) is not False |
| 76 | + |
| 77 | + def describe( |
| 78 | + self, ctx: PluginContext, call: Call, symbol: SymbolId, defs: Mapping[Value, Instruction] |
| 79 | + ) -> CallReport: |
| 80 | + if symbol in _STANDARD_LIBRARY: |
| 81 | + risk = "expands the entities a document declares, which can exhaust memory with Expat older than 2.7.2" |
| 82 | + advice = "use defusedxml" |
| 83 | + elif _resolve_entities(call, defs) is True: |
| 84 | + risk = "resolves external entities (resolve_entities=True), which lets a crafted document read local files" |
| 85 | + advice = "use defusedxml" |
| 86 | + elif _resolve_entities(call, defs) == "internal": |
| 87 | + risk = ( |
| 88 | + "sets resolve_entities='internal', which keeps external entities out only from lxml 5.0 on: " |
| 89 | + "before lxml 5.0 the string reads as true and external entities are resolved, which lets a " |
| 90 | + "crafted document read local files" |
| 91 | + ) |
| 92 | + advice = "use defusedxml or resolve_entities=False" |
| 93 | + elif symbol in _LXML_UNTIL_6_1: |
| 94 | + risk = ( |
| 95 | + "resolves external entities by default before lxml 6.1 (CVE-2026-41066), " |
| 96 | + "which lets a crafted document read local files" |
| 97 | + ) |
| 98 | + advice = "use defusedxml or resolve_entities=False" |
| 99 | + else: |
| 100 | + risk = "resolves external entities by default before lxml 5.0, which lets a crafted document read local files" |
| 101 | + advice = "use defusedxml" |
| 102 | + written = symbol in _CONTENT and self.written_by_the_project(ctx, call, defs) |
| 103 | + confidence = Confidence.LOW if written else self.confidence |
| 104 | + return CallReport(f"call to {symbol} {risk}; {advice}", confidence) |
| 105 | + |
| 106 | + def written_by_the_project(self, ctx: PluginContext, call: Call, defs: Mapping[Value, Instruction]) -> bool: |
| 107 | + """Whether the first argument of ``call``, which a ``_CONTENT`` function parses as |
| 108 | + the document, is a string constant or a module-level name bound once to one.""" |
| 109 | + |
| 110 | + if not call.arguments: |
| 111 | + return False |
| 112 | + made = defs.get(call.arguments[0]) |
| 113 | + if isinstance(made, Constant): |
| 114 | + return isinstance(made.value, str | bytes) |
| 115 | + return isinstance(made, Global) and made.name in ctx.get(ModuleStringsAnalysis) |
0 commit comments