Skip to content

Commit 1c716be

Browse files
warthog9ericwb
authored andcommitted
Add PyCryptodome to import blacklists
PyCryptodome is a direct fork of PyCrypto, and is generally considered to not be the replacement for it. It is recommended that projects move to pyca/cryptography instead, as this may be exposing folks to the same inherent issues that PyCrypto was deprecated because of. Signed-off-by: John 'Warthog9' Hawley <warthog9@eaglescrag.net> Signed-off-by: John 'Warthog9' Hawley <jhawley@vmware.com> Signed-off-by: Terri Oda <terri.oda@intel.com>
1 parent 69a209b commit 1c716be

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ Usage::
224224
B411 import_xmlrpclib
225225
B412 import_httpoxy
226226
B413 import_pycrypto
227+
B414 import_pycryptodome
227228
B501 request_with_no_cert_validation
228229
B502 ssl_with_bad_version
229230
B503 ssl_with_bad_defaults

bandit/blacklists/imports.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@
199199
| | | - Crypto.Util | |
200200
+------+---------------------+------------------------------------+-----------+
201201
202+
B414: import_pycryptodome
203+
-------------------------
204+
pycryptodome is a direct fork of pycrypto that has not fully addressed
205+
the issues inherent in PyCrypto. It seems to exist, mainly, as an API
206+
compatible continuation of pycrypto and should be deprecated in favor
207+
of pyca/cryptography which has more support among the Python community.
208+
209+
+------+---------------------+------------------------------------+-----------+
210+
| ID | Name | Imports | Severity |
211+
+======+=====================+====================================+===========+
212+
| B414 | import_pycryptodome | - Cryptodome.Cipher | high |
213+
| | | - Cryptodome.Hash | |
214+
| | | - Cryptodome.IO | |
215+
| | | - Cryptodome.Protocol | |
216+
| | | - Cryptodome.PublicKey | |
217+
| | | - Cryptodome.Random | |
218+
| | | - Cryptodome.Signature | |
219+
| | | - Cryptodome.Util | |
220+
+------+---------------------+------------------------------------+-----------+
221+
202222
"""
203223

204224
from bandit.blacklists import utils
@@ -302,4 +322,18 @@ def gen_blacklist():
302322
'maintained and have been deprecated. '
303323
'Consider using pyca/cryptography library.', 'HIGH'))
304324

325+
sets.append(utils.build_conf_dict(
326+
'import_pycryptodome', 'B414',
327+
['Cryptodome.Cipher',
328+
'Cryptodome.Hash',
329+
'Cryptodome.IO',
330+
'Cryptodome.Protocol',
331+
'Cryptodome.PublicKey',
332+
'Cryptodome.Random',
333+
'Cryptodome.Signature',
334+
'Cryptodome.Util'],
335+
'The pycryptodome library is not considered a secure alternative '
336+
'to pycrypto.'
337+
'Consider using pyca/cryptography library.', 'HIGH'))
338+
305339
return {'Import': sets, 'ImportFrom': sets, 'Call': sets}

examples/pycryptodome.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from Cryptodome.Cipher import AES
2+
from Cryptodome import Random
3+
4+
from . import CryptoMaterialsCacheEntry
5+
6+
7+
def test_pycrypto():
8+
key = b'Sixteen byte key'
9+
iv = Random.new().read(AES.block_size)
10+
cipher = pycrypto_arc2.new(key, AES.MODE_CFB, iv)
11+
factory = CryptoMaterialsCacheEntry()

tests/functional/test_functional.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,11 @@ def test_blacklist_pycrypto(self):
729729
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 2}
730730
}
731731
self.check_example('pycrypto.py', expect)
732+
733+
def test_blacklist_pycryptodome(self):
734+
'''Test importing pycryptodome module'''
735+
expect = {
736+
'SEVERITY': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 2},
737+
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 2}
738+
}
739+
self.check_example('pycryptodome.py', expect)

0 commit comments

Comments
 (0)