PyCQA / PyCQA/bandit

False positive / regression: [B314:blacklist] Using xml.etree.ElementTree.fromstring to parse untrusted XML - while defusedxml is used

Open
#995 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
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
  1. 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>"))
  1. Call bandit bandit_bug.py
    With bandit 1.7.5 you 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.