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 14 apps in this directory
argilla
AI Development · Data Engineering
Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.
authentik
Authentication · Security
The self-hosted Identity Provider that replaces Okta, Auth0, and Entra ID with a unified SSO platform supporting SAML, OAuth2/OIDC, LDAP, RADIUS, and WebAuthn.
Khoj
AI Assistants · Knowledge Management · Productivity
A self-hostable AI second brain that chats with your documents, searches the web, builds custom agents, and runs entirely on your own LLM.
knowhere
AI Development · Developer Tools
Transform messy, unstructured documents into persistent, navigable memory that AI agents can actually use.
Label Studio
AI Development · Data Engineering
Label Studio is an open-source, multi-type data labeling platform that lets teams annotate images, text, audio, video, and time series data with a configurable XML-based UI and export annotations in formats ready for any ML framework.
Langflow
AI Agents · AI Development
Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.
LearnHouse
Learning Management · CMS
Open-source LMS with AI tutoring, real-time collaboration boards, live code execution, and built-in course monetization — self-hosted in minutes.
Memgraph
Databases · AI Development
High-performance in-memory graph database for AI context and real-time analytics
OpenBB
Databases · Analytics · Invoicing Finance
The AI Workspace for Finance: Connect Data, Run AI Agents, Build Analytics