defusedxml
Drop-in replacements for Python's XML modules that block XML-bomb and entity-expansion attacks
Repository Health
Technical Analysis
defusedxml provides hardened drop-in replacements for Python’s standard-library XML parsing modules — xml.etree.ElementTree, xml.dom.minidom, xml.sax, xmlrpc, and lxml.etree — that disable or guard against the entity-expansion and external-entity vulnerabilities present in the defaults. Python’s built-in XML parsers are, by design, vulnerable to attacks like billion-laughs entity expansion, quadratic blowup, external entity (XXE) file disclosure, and DTD retrieval, none of which most application code is prepared to defend against.
Rather than requiring developers to hand-configure every parser’s security options correctly, defusedxml ships pre-configured, secure-by-default parser wrappers with the same API as the modules they replace, so switching a vulnerable import xml.etree.ElementTree as ET to import defusedxml.ElementTree as ET closes the relevant attack classes with a one-line change.
What You Get
defusedxml.ElementTreeas a secure drop-in forxml.etree.ElementTreedefusedxml.minidom,defusedxml.sax, anddefusedxml.pulldomsecure equivalents of their stdlib counterpartsdefusedxml.expatbuilderanddefusedxml.expatreaderfor lower-level Expat-based parsing with entity protectionsdefusedxml.xmlrpcto monkey-patch Python’sxmlrpcclient/server against XML attacksdefusedxml.lxmlfor adding the same protections when using the third-partylxmllibrary- Clear, documented exceptions (
EntitiesForbidden,ExternalReferenceForbidden, etc.) raised when an attack pattern is detected
Common Use Cases
- Replacing unsafe stdlib XML parsing in any Python service that accepts XML from untrusted sources (uploads, APIs, SOAP)
- Hardening SAML, RSS/Atom feed, or SOAP/XML-RPC processing pipelines against XXE and billion-laughs attacks
- Passing security audits or compliance checks that flag raw
xml.etree/xml.dom.minidomusage as a known vulnerability class - Safely parsing XML configuration or data files uploaded by end users in web applications
Under The Hood
Architecture The package is a set of thin, focused wrapper modules — common.py (85 lines) defines the shared DTDForbidden, EntitiesForbidden, and ExternalReferenceForbidden exception types plus the core _generate_etree_functions-style guarding logic, while each of ElementTree.py (188 lines), minidom.py, sax.py, pulldom.py, expatbuilder.py, expatreader.py, cElementTree.py, lxml.py, and xmlrpc.py reimplements just enough of its corresponding stdlib module’s public API to intercept parser construction and inject a secure Expat/lxml resolver that raises rather than expands dangerous constructs; __init__.py centralizes the package’s parse-and-raise convenience helpers. The design deliberately keeps each module’s surface area minimal and mirrors the stdlib API 1:1 so existing code needs only an import-path change to adopt it. Tech Stack Pure Python, zero required runtime dependencies (the lxml module is only exercised if the optional lxml package is already installed by the consumer), packaged with a standard setup.py/pyproject.toml. Code Quality tests.py at the repository root exercises each of the guarded parsers against a xmltestdata/ corpus of known attack payloads (entity bombs, external entity references, quadratic blowup), with CI historically run via Travis and coverage tracked via Codecov (badges in the README); the project also maintains a SECURITY.md documenting its threat model and disclosure process. API Design Because every module mirrors its stdlib counterpart’s function names and signatures exactly, the learning curve is close to zero — the entire integration is typically a single changed import line, with the tradeoff that some rarely-needed stdlib XML features are deliberately unavailable if they can’t be made safe.
Used by 15 apps in this directory
OpenViking
AI Development · AI Memory · Databases
An open-source context database that gives AI agents a unified filesystem for memory, resources, and skills with hierarchical tiered retrieval.
Phase Console
Devops · Security
End-to-end encrypted secrets management for engineering teams — from local dev to Kubernetes production.
PostHog
Ab Testing Experimentation · Analytics · Developer Tools
The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.
Shadowbroker
Analytics · Monitoring · Security
Self-hosted OSINT dashboard that fuses 60+ live intelligence feeds — flight tracking, ship AIS, satellites, CCTV, seismic and radio networks — into one real-time map, with an agent-ready command channel for AI co-analysts.
Taiga Back
Developer Tools · Project Management
Self-hosted agile project management backend with Scrum, Kanban, issue tracking, and a full REST API — built on Django and PostgreSQL.
Tracecat
AI Agents · Automation · Security
Open-source agentic security automation platform that runs AI agents and durable workflows at scale with sandboxed execution.