defusedxml

Drop-in replacements for Python's XML modules that block XML-bomb and entity-expansion attacks

Library
PyPI
v0.7.1
557 stars
Custom / Unknown

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
41 /100 Fair
Development Activity 0
Maintenance 20
Community 64
Maturity 60
Momentum 20

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
79 /100 Good
Architecture 75
Code Quality 82
Innovation 65
Learning Curve 92

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.ElementTree as a secure drop-in for xml.etree.ElementTree
  • defusedxml.minidom, defusedxml.sax, and defusedxml.pulldom secure equivalents of their stdlib counterparts
  • defusedxml.expatbuilder and defusedxml.expatreader for lower-level Expat-based parsing with entity protections
  • defusedxml.xmlrpc to monkey-patch Python’s xmlrpc client/server against XML attacks
  • defusedxml.lxml for adding the same protections when using the third-party lxml library
  • 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.minidom usage 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

Python
73%
AGPL 3.0

OpenViking

AI Development · AI Memory · Databases

40,705

An open-source context database that gives AI agents a unified filesystem for memory, resources, and skills with hierarchical tiered retrieval.

View details
85
Repo Health
75
Technical
64
Dependency
Built with
Python 73%
TypeScript 12%
Rust 11%
Updated 6 days ago
TypeScript
50%
Other

Phase Console

Devops · Security

931

End-to-end encrypted secrets management for engineering teams — from local dev to Kubernetes production.

View details
85
Repo Health
73
Technical
63
Dependency
Built with
TypeScript 50%
Python 48%
Updated 1 weeks ago
Python
55%
Other

PostHog

Ab Testing Experimentation · Analytics · Developer Tools

40,397

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.

View details
92
Repo Health
80
Technical
65
Dependency
Built with
Python 55%
TypeScript 36%
Updated 6 days ago
Python
64%
AGPL 3.0

Shadowbroker

Analytics · Monitoring · Security

11,358

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.

View details
85
Repo Health
83
Technical
71
Dependency
Built with
Python 64%
TypeScript 32%
Updated 1 weeks ago
Python
97%
MPL 2.0

Taiga Back

Developer Tools · Project Management

859

Self-hosted agile project management backend with Scrum, Kanban, issue tracking, and a full REST API — built on Django and PostgreSQL.

View details
72
Repo Health
74
Technical
64
Dependency
Built with
Python 97%
Updated 1 months ago
Python
68%
AGPL 3.0

Tracecat

AI Agents · Automation · Security

3,860

Open-source agentic security automation platform that runs AI agents and durable workflows at scale with sandboxed execution.

View details
89
Repo Health
88
Technical
66
Dependency
Built with
Python 68%
TypeScript 30%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers