False positive / regression: [B314:blacklist] Using xml.etree.ElementTree.fromstring to parse untrusted XML - while defusedxml is used
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 8.3k
- Forks
- 835
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 1
Description
Describe the bug
Since the latest 1.7.5 release,
code that used to not raise warnings is now detected as invalid by bandit
Reproduction steps
- Put the following code in a file named
bandit_bug.py
try:
from defusedxml.ElementTree import fromstring
except ImportError:
from xml.etree.ElementTree import fromstring # nosec
print(fromstring("<svg></svg>"))
- Call
bandit bandit_bug.py
Withbandit 1.7.5you will get the following output:
>> Issue: [B314:blacklist] Using xml.etree.ElementTree.fromstring to parse untrusted XML data is known to be vulnerable to XML attacks. Replace xml.etree.ElementTree.fromstring with its defusedxml equivalent function or make sure defusedxml.defuse_stdlib() is called
Severity: Medium Confidence: High
CWE: CWE-20 (https://cwe.mitre.org/data/definitions/20.html)
More Info: https://bandit.readthedocs.io/en/1.7.5/blacklists/blacklist_calls.html#b313-b320-xml-bad-elementtree
Location: bandit_bug.py:7:6
Expected behavior
With bandit==1.7.4 no error was raised, and I was expecting this behaviour to stay consistent with new bandit releases
Bandit version
1.7.5 (Default)
Python version
3.8
Additional context
Tested with Python 3.8 & Python 3.10
This is not urgent matter for me, I simply disabled this check locally for now:
https://github.com/PyFPDF/fpdf2/pull/718/files#diff-ff268f4fc21f36f22d234ae39a8335470764ff490e1fbd27440d4695c1de0864
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with Bandit's B314 blacklist check and reproduce the report using the shown bandit_bug.py example under Bandit 1.7.5. Compare the result with 1.7.4 and add or update regression coverage so the defusedxml import path is handled as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- security
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100