Skip to content

Commit

Permalink
Add shelve to the pickle blacklists
Browse files Browse the repository at this point in the history
shelve is a stdlib module that wraps pickle in a dict-like interface.
  • Loading branch information
auscompgeek authored and ericwb committed Sep 30, 2019
1 parent d94a3fd commit 3f9618c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion bandit/blacklists/calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
| | | - dill.loads | |
| | | - dill.load | |
| | | - dill.Unpickler | |
| | | - shelve.open | |
| | | - shelve.DbfilenameShelf | |
+------+---------------------+------------------------------------+-----------+
B302: marshal
Expand Down Expand Up @@ -347,7 +349,9 @@ def gen_blacklist():
'cPickle.Unpickler',
'dill.loads',
'dill.load',
'dill.Unpickler'],
'dill.Unpickler',
'shelve.open',
'shelve.DbfilenameShelf'],
'Pickle and modules that wrap it can be unsafe when used to '
'deserialize untrusted data, possible security issue.'
))
Expand Down
3 changes: 2 additions & 1 deletion bandit/blacklists/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
| B403 | import_pickle | - pickle | low |
| | | - cPickle | |
| | | - dill | |
| | | - shelve | |
+------+---------------------+------------------------------------+-----------+
B404: import_subprocess
Expand Down Expand Up @@ -256,7 +257,7 @@ def gen_blacklist():
))

sets.append(utils.build_conf_dict(
'import_pickle', 'B403', ['pickle', 'cPickle', 'dill'],
'import_pickle', 'B403', ['pickle', 'cPickle', 'dill', 'shelve'],
'Consider possible security implications associated with '
'{name} module.', 'LOW'
))
Expand Down
12 changes: 12 additions & 0 deletions examples/shelve_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import shelve
import tempfile

with tempfile.TemporaryDirectory() as d:
filename = os.path.join(d, 'shelf')

with shelve.open(filename) as db:
db['spam'] = {'eggs': 'ham'}

with shelve.open(filename) as db:
print(db['spam'])
8 changes: 8 additions & 0 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ def test_dill(self):
}
self.check_example('dill.py', expect)

def test_shelve(self):
'''Test for the `shelve` module.'''
expect = {
'SEVERITY': {'UNDEFINED': 0, 'LOW': 1, 'MEDIUM': 2, 'HIGH': 0},
'CONFIDENCE': {'UNDEFINED': 0, 'LOW': 0, 'MEDIUM': 0, 'HIGH': 3}
}
self.check_example('shelve_open.py', expect)

def test_popen_wrappers(self):
'''Test the `popen2` and `commands` modules.'''
expect = {
Expand Down

0 comments on commit 3f9618c

Please sign in to comment.