Skip to content

Commit 6e5fb05

Browse files
authored
Merge pull request #248 from CoreTrace/fix/unsafe-xml-risk
fix(unsafe-xml): report what each XML parser family risks
2 parents ca3b215 + e10434c commit 6e5fb05

7 files changed

Lines changed: 246 additions & 19 deletions

File tree

‎docs/plugins.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ class SensitiveLoggingPlugin(SymbolCallDetector):
8585
message_template: ClassVar[str] = "call to {symbol} may log sensitive data"
8686
```
8787

88-
The shipped `dangerous-eval` and `weak-crypto` plugins are of this kind.
88+
The shipped `dangerous-eval` and `weak-crypto` plugins are of this kind. `accepts(call, symbol, defs)`
89+
keeps only some calls; `describe(ctx, call, symbol, defs)` returns the `CallReport` (message and
90+
confidence) of one call, the rule's `message_template` and `confidence` by default, for a rule that
91+
tells its calls apart, as `unsafe-xml` does.
8992

9093
### A detector of taint flows: `TaintDetector`
9194

‎docs/usage.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ reads no template.
203203
| `debug-enabled` | `app.run(debug=True)` on a Flask application. |
204204
| `missing-timeout` | A Requests or httpx call without a `timeout`. |
205205
| `insecure-tls` | `verify=False` on a Requests or httpx call, or `ssl._create_unverified_context()`. |
206-
| `unsafe-xml` | A standard-library or lxml XML parser, which expands entities; `defusedxml` is the safe replacement. |
206+
| `unsafe-xml` | An XML parser of the standard library or of lxml, reported for what it risks; `defusedxml` is the safe replacement. The standard library's parsers run on Expat, which reads no local file but expands the entities a document declares: a crafted document can exhaust memory with Expat older than 2.7.2, which depends on the Python build at run time (`pyexpat.EXPAT_VERSION`). lxml resolves external entities by default before 5.0, and in `iterparse` and `ETCompatXMLParser` before 6.1 (CVE-2026-41066), which lets a crafted document read local files; `resolve_entities=True` does so whatever the version, and `'internal'` keeps external entities out only from lxml 5.0 on, an earlier lxml reading the string as true; a parser given `resolve_entities=False` is not reported. XML content the project wrote itself, a string constant or a module-level string bound once passed to a function that parses its argument as the document (`fromstring`, `parseString`, `XML`), is reported at low confidence; a constant path keeps its confidence, since whoever can write the file chooses the document. |
207207
| `unsafe-archive-extraction` | `tarfile` or `zipfile` extraction, or `shutil.unpack_archive`, without a member `filter`. |
208208
| `insecure-temp-file` | `tempfile.mktemp`, which names a file without creating it. |
209209
| `weak-random` | A `random` function whose result is assigned to a credential-like name, or produced by a function named like one. Medium confidence. |

‎src/coretrace_python/bundled/syntax/unsafe_xml/plugin.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "unsafe-xml"
22
version = "1.0.0"
33
plugin_api = ">=1,<2"
4-
requires = ["interprocedural.callgraph", "ir.ssa"]
4+
requires = ["interprocedural.callgraph", "ir.ssa", "abstract.module_strings"]
55
provides = ["vulnerability.unsafe-xml"]
66

77
[entrypoint]
Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,115 @@
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."""
314

415
from __future__ import annotations
516

17+
from collections.abc import Mapping
618
from typing import ClassVar
719

20+
from coretrace_python.abstract.strings import ModuleStringsAnalysis
821
from coretrace_python.analysis import AnyAnalysis
9-
from coretrace_python.findings import Severity
22+
from coretrace_python.findings import Confidence, Severity
1023
from coretrace_python.interprocedural import CallGraphAnalysis
24+
from coretrace_python.ir.model import Call, Constant, Global, Instruction, Value
1125
from coretrace_python.ir.ssa import SSAAnalysis
12-
from coretrace_python.plugins import SymbolCallDetector
26+
from coretrace_python.plugins import CallReport, PluginContext, SymbolCallDetector
1327
from coretrace_python.semantic.symbols import SymbolId
1428

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(
1635
"xml.etree.ElementTree.parse", "xml.etree.ElementTree.fromstring", "xml.etree.ElementTree.iterparse",
1736
"xml.etree.ElementTree.XMLParser", "xml.etree.ElementTree.fromstringlist",
1837
"xml.dom.minidom.parse", "xml.dom.minidom.parseString",
1938
"xml.dom.pulldom.parse", "xml.dom.pulldom.parseString",
2039
"xml.sax.parse", "xml.sax.parseString", "xml.sax.make_parser",
2140
"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",
2441
)
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
2563

2664

2765
class UnsafeXmlPlugin(SymbolCallDetector):
2866
name: ClassVar[str] = "unsafe-xml"
2967
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
3270
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)

‎src/coretrace_python/plugins/__init__.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
apply_refinement,
1212
run_plugins,
1313
)
14-
from coretrace_python.plugins.detectors import Assessment, SymbolCallDetector, TaintDetector
14+
from coretrace_python.plugins.detectors import (
15+
Assessment,
16+
CallReport,
17+
SymbolCallDetector,
18+
TaintDetector,
19+
)
1520
from coretrace_python.plugins.loader import (
1621
IncompatiblePluginError,
1722
LoadedPlugin,
@@ -36,6 +41,7 @@
3641
__all__ = [
3742
"PLUGIN_API_VERSION",
3843
"Assessment",
44+
"CallReport",
3945
"Entrypoint",
4046
"IncompatiblePluginError",
4147
"LoadedPlugin",

‎src/coretrace_python/plugins/detectors.py‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
detector is a few class attributes on top of it, and a rule that knows more about its
55
sinks refines the verdict (``judge``) or the severity too (``assess``). ``SymbolCallDetector`` reports calls
66
whose callee resolves to a canonical symbol, through imports and local aliases alike,
7-
by reading the SSA form.
7+
by reading the SSA form; a rule that tells its calls apart words each finding itself
8+
(``describe``).
89
"""
910

1011
from __future__ import annotations
@@ -37,6 +38,14 @@ class Assessment:
3738
metadata: Mapping[str, str] = field(default_factory=dict)
3839

3940

41+
@dataclass(frozen=True)
42+
class CallReport:
43+
"""What a ``SymbolCallDetector`` reports of one call: its message and confidence."""
44+
45+
message: str
46+
confidence: Confidence
47+
48+
4049
class TaintDetector(Plugin):
4150
"""Report every taint flow carrying ``kind`` into a sink."""
4251

@@ -128,6 +137,14 @@ class SymbolCallDetector(Plugin):
128137
def accepts(self, call: Call, symbol: SymbolId, defs: Mapping[Value, Instruction]) -> bool:
129138
return True
130139

140+
def describe(
141+
self, ctx: PluginContext, call: Call, symbol: SymbolId, defs: Mapping[Value, Instruction]
142+
) -> CallReport:
143+
"""The message and confidence of the finding for ``call``: the rule's own, unless
144+
the rule tells its calls apart."""
145+
146+
return CallReport(self.message_template.format(symbol=symbol), self.confidence)
147+
131148
def analyze(self, ctx: PluginContext) -> Sequence[Finding]:
132149
findings: list[Finding] = []
133150
graph = ctx.get(CallGraphAnalysis) if CallGraphAnalysis in self.requires else None
@@ -150,12 +167,13 @@ def analyze(self, ctx: PluginContext) -> Sequence[Finding]:
150167
continue
151168
if not self.accepts(instruction, symbol, defs):
152169
continue
170+
described = self.describe(ctx, instruction, symbol, defs)
153171
findings.append(
154172
Finding(
155173
rule_id=self.rule_id,
156-
message=self.message_template.format(symbol=symbol),
174+
message=described.message,
157175
severity=self.severity,
158-
confidence=self.confidence,
176+
confidence=described.confidence,
159177
span=instruction.location,
160178
function=function.name,
161179
metadata={"symbol": str(symbol)},

‎tests/test_unsafe_xml_risk.py‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
"""Acceptance tests for issue #242: ``unsafe-xml`` says what each parser family risks.
2+
3+
The standard library's parsers run on Expat, which never reads a local file; they expand
4+
the entities a document declares, which can exhaust memory with Expat older than 2.7.2.
5+
lxml resolves external entities by default before 5.0, and for ``iterparse`` and
6+
``ETCompatXMLParser`` before 6.1 (CVE-2026-41066); ``resolve_entities=True`` resolves them
7+
whatever the version, ``False`` never. ``'internal'`` keeps them out only from lxml 5.0 on:
8+
an earlier lxml reads the string as true. XML content the project wrote itself is no
9+
attacker's: it is reported at low confidence; a constant path is not the document it
10+
names, which anyone able to write the file chooses.
11+
"""
12+
13+
from __future__ import annotations
14+
15+
from pathlib import Path
16+
17+
import pytest
18+
19+
from coretrace_python import engine
20+
from coretrace_python.findings import Confidence, Finding, Severity
21+
from coretrace_python.source import SourceManager
22+
23+
PLUGINS = Path(__file__).resolve().parent.parent / "src" / "coretrace_python" / "bundled"
24+
25+
26+
def check(text: str) -> tuple[Finding, ...]:
27+
findings = engine.check(SourceManager().add_source("m.py", text), [PLUGINS])
28+
return tuple(f for f in findings if f.rule_id == "unsafe-xml")
29+
30+
31+
def only(text: str) -> Finding:
32+
(finding,) = check(text)
33+
assert finding.severity is Severity.MEDIUM
34+
return finding
35+
36+
37+
@pytest.mark.parametrize(
38+
"call",
39+
[
40+
"xml.etree.ElementTree.fromstring(data)",
41+
"xml.dom.minidom.parseString(data)",
42+
"xml.sax.parseString(data, None)",
43+
],
44+
)
45+
def test_a_standard_library_parser_is_reported_for_entity_amplification(call: str) -> None:
46+
finding = only(f"import xml.etree.ElementTree\nimport xml.dom.minidom\nimport xml.sax\n\ndef f(data):\n {call}\n")
47+
48+
assert "Expat older than 2.7.2" in finding.message
49+
assert "local file" not in finding.message
50+
assert finding.confidence is Confidence.HIGH
51+
52+
53+
@pytest.mark.parametrize("function", ["fromstring", "parse", "XML", "XMLParser"])
54+
def test_an_lxml_parser_with_its_defaults_is_reported_for_versions_before_5(function: str) -> None:
55+
finding = only(f"import lxml.etree\n\ndef f(data):\n lxml.etree.{function}(data)\n")
56+
57+
assert "before lxml 5.0" in finding.message
58+
assert "local files" in finding.message
59+
60+
61+
@pytest.mark.parametrize("function", ["iterparse", "ETCompatXMLParser"])
62+
def test_iterparse_and_the_compatibility_parser_are_reported_for_versions_before_6_1(function: str) -> None:
63+
finding = only(f"import lxml.etree\n\ndef f(data):\n lxml.etree.{function}(data)\n")
64+
65+
assert "before lxml 6.1" in finding.message
66+
assert "CVE-2026-41066" in finding.message
67+
68+
69+
def test_resolving_entities_explicitly_is_reported_whatever_the_version() -> None:
70+
finding = only("import lxml.etree\n\ndef f(data):\n lxml.etree.XMLParser(resolve_entities=True)\n")
71+
72+
assert "resolve_entities=True" in finding.message
73+
assert "before lxml" not in finding.message
74+
75+
76+
def test_a_parser_that_resolves_no_external_entity_is_not_reported() -> None:
77+
assert check("import lxml.etree\n\ndef f(data):\n lxml.etree.XMLParser(resolve_entities=False)\n") == ()
78+
assert check("import lxml.etree\n\ndef f(data):\n lxml.etree.iterparse(data, resolve_entities=False)\n") == ()
79+
80+
81+
@pytest.mark.parametrize("function", ["XMLParser", "iterparse"])
82+
def test_internal_entities_only_are_reported_for_versions_before_5(function: str) -> None:
83+
finding = only(f"import lxml.etree\n\ndef f(data):\n lxml.etree.{function}(data, resolve_entities='internal')\n")
84+
85+
assert "resolve_entities='internal'" in finding.message
86+
assert "before lxml 5.0" in finding.message
87+
88+
89+
@pytest.mark.parametrize(
90+
"text",
91+
[
92+
"from xml.dom import minidom\n\ndef f():\n minidom.parseString('<report/>')\n",
93+
"from xml.dom import minidom\nEXPECTED = '<report/>'\n\ndef f():\n minidom.parseString(EXPECTED)\n",
94+
"import lxml.etree\n\ndef f():\n lxml.etree.fromstring('<report/>')\n",
95+
],
96+
)
97+
def test_a_document_the_project_wrote_is_reported_at_low_confidence(text: str) -> None:
98+
assert only(text).confidence is Confidence.LOW
99+
100+
101+
@pytest.mark.parametrize(
102+
"call",
103+
[
104+
"lxml.etree.parse('/tmp/upload.xml')",
105+
"lxml.etree.iterparse('/tmp/upload.xml')",
106+
"xml.etree.ElementTree.parse('/tmp/upload.xml')",
107+
"xml.dom.minidom.parse('/tmp/upload.xml')",
108+
"xml.sax.parse('/tmp/upload.xml', None)",
109+
],
110+
)
111+
def test_a_constant_path_keeps_high_confidence(call: str) -> None:
112+
text = f"import lxml.etree\nimport xml.dom.minidom\nimport xml.etree.ElementTree\nimport xml.sax\n\ndef f():\n {call}\n"
113+
114+
assert only(text).confidence is Confidence.HIGH
115+
116+
117+
def test_a_document_of_unknown_origin_keeps_high_confidence() -> None:
118+
text = "from xml.dom import minidom\nEXPECTED = '<report/>'\nEXPECTED = load()\n\ndef f():\n minidom.parseString(EXPECTED)\n"
119+
120+
assert only(text).confidence is Confidence.HIGH

0 commit comments

Comments
 (0)