PyCQA / PyCQA/bandit

B411 error can't be resolved by the suggested change

Open
#1,082 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
8.3k
Forks
836
Avg merge
5d 3h
Merged PRs (30d)
1

Description

Describe the bug

We recently faced the following bandit error in oslo.serialization project. It seems bandit 1.7.6 contains the commit 3260f137873798b4b0c0c289373cc5e8fa2d93ed and now it reject importing xmlrpc.

>> Issue: [B411:blacklist] Using client to parse untrusted XML data is known to be vulnerable to XML attacks. Use defused.xmlrpc.monkey_patch() function to monkey-patch xmlrpclib and mitigate XML vulnerabilities.
   Severity: High   Confidence: High
   CWE: CWE-20 (https://cwe.mitre.org/data/definitions/20.html)
   More Info: https://bandit.readthedocs.io/en/1.7.6/blacklists/blacklist_imports.html#b411-import-xmlrpclib
   Location: oslo_serialization/jsonutils.py:40:0
38	import json
39	import uuid
40	from xmlrpc import client as xmlrpclib
41
42	from oslo_utils import encodeutils

However bandit still detects the error even if we add the monkey_patch as suggested by the message.

Reproduction steps
  1. Run bandit with the code
from xmlrpc import client as xmlrpclib
  1. ZB411 error is detected
  2. Update the code to include the suggested change
import defusedxml.xmlrpc
frm xmlrpc import client as xmlrpclib

defusedxml.xmlrpc.monkey_patch()
  1. Run bandit again and it still detects the same error
Expected behavior

The execution at step 4 does not detect the error

Bandit version

1.7.6 (Default)

Python version

3.11

Additional context

No response

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 at the B411 blacklist rule and run Bandit against the two Python snippets in the issue, including defusedxml.xmlrpc.monkey_patch(). Done means the documented mitigation no longer produces B411, with regression coverage for the import and monkey-patch sequence.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.