Repository navigation
XML vulnerabilities in Python #61441
Description
Activity
Experimental fix for XML vulnerabilities against default. It's NOT ready and needs lots of polishing.
https://pypi.python.org/pypi/defusedxml contains explanations of all issues
https://pypi.python.org/pypi/defusedexpat is a standalone version of part of the patches for Python 2.6 to 3.3- addedextension-modulesC modules in the Modules dirC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-securityA security issueA security issue
on Feb 19, 2013 Since this has dragged on for quite a while, I'm probably just going to release 2.7.4 with a pointer to defusedxml in the release notes. (docs, though, perhaps)
Since this has dragged on for quite a while, I'm probably
just going to release 2.7.4 with a pointer to defusedxml
in the release notes. (docs, though, perhaps)+1
Since this has dragged on for quite a while, I'm probably just going to
release 2.7.4 with a pointer to defusedxml in the release notes. (docs,
though, perhaps)+1 too.
Not blocking 2.7.4 as discussed on mailing list.
I did a rough merge with current “default” (3.5 pre-release) branch so that I can have a closer look at this issue; see xmlbomb_20150518.patch for the result. There are some bits with Argument Clinit that need perfecting:
-
Unsure how to convert the ElementTree.XMLParser.__init__() signature (varied depending on XML_BOMB_PROTECTION compile-time flag) to Argument Clinic. So I just hard-coded it as if XML_BOMB_PROTECTION is always enabled. Why do we have to have a variable signature in the first place?
-
New pyexpat functions need porting to Argument Clinic.
-
I started looking at the lower Expat-level changes. Here are some thoughts, in the order that I thought them. :) But the end result is to investigate a different approach to disable entities in existing versions of Expat.
Currently, it looks like max_entity_indirections = 0 is a special value meaning no limit. I think it would be better to use some other value such as None for this, and then 0 could disable all entity expansion (other than pre-defined entities like & &#xNNNN; etc).
What is the benefit of having the indirection limit? I would have thought the entity expansion (character) limit on its own would already be effective at preventing nested expansion attacks like “billion laughs”. Even if the entity expanded to an empty string, all of the intermediate entity references are still included in the character count.
I wonder if it would make more sense to have a total character limit instead, which would include the characters from custom entity expansions as already counted by the patch, but also count characters directly from the XML body. Why would you want to avoid 8 million characters from entity expansion, but allow 8 million characters of plain XML (or gzipped XML)? (I am not an XML expert, so I could be missing something obvious here.)
Now I have discovered that it seems you can build Python to use an external Expat library, which won’t be affected by Christian’s fix (correct me if I am wrong). I think we should find a different solution that will also work with existing external Expat versions. Maybe setting EntityDeclHandler to raise an error would be good enough:
>>> from xml.parsers import expat >>> bomb = '<!DOCTYPE bomb [\n<!ENTITY a "" >\n<!ENTITY b "' + '&a;' * 1000 + '" >\n<!ENTITY c "' + '&b;' * 1000 + '" >\n]>\n<bomb a="' + '&c;' * 10 + '" />\n' >>> p = expat.ParserCreate() >>> p.Parse(bomb, True) # Noticeable delay (DOS) while parsing 1 >>> p = expat.ParserCreate() >>> def handler(*so_much_argh): ... raise ValueError("Entity handling disabled") ... >>> p.EntityDeclHandler = handler >>> p.Parse(bomb, True) # Instant failure (no DOS) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/build/python/src/Python-3.4.3/Modules/pyexpat.c", line 494, in EntityDecl File "<stdin>", line 2, in handler ValueError: Entity handling disabled
This solution has been suggested and implemented elsewhere:
- https://bugzilla.redhat.com/show_bug.cgi?id=1000109#c1
- http://mail-archives.apache.org/mod_mbox/apr-dev/200906.mbox/%3C20090602162934.GA28360@redhat.com%3E (though I suspect the SetDefaultHandler option there is not sufficient)
I have opened bpo-24238 with a patch for Element Tree that uses my EntityDeclHandler technique, instead of patching Expat. I would be interested in other people’s thoughts on the approach.
This issue didn't get much attention in 5 years. The XML documentation starts with a big red warning:
https://docs.python.org/dev/library/xml.htmlThe warning is present in 2.7 and 3.4 as well:
https://docs.python.org/2.7/library/xml.html
https://docs.python.org/3.4/library/xml.htmlIt seems like XML is getting less popular because of JSON becoming more popular (JSON obviously comes with its own set of security issues). It seems like less core developers care about XML.
I suggest to:
We just have to accept that core developers have limited availability and that documenting security issues is an acceptable tradeoff. I don't see any value of keeping these 3 issues open.
22 remaining items
- addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.7 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on Nov 4, 2021 - added3.8 (EOL)end of lifeend of life3.7 (EOL)end of lifeend of lifeand removed3.8 (EOL)end of lifeend of life3.7 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of life
on May 24, 2025
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
bpo-24238 is #68426 which remains open.