Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shelve to the pickle blacklists #542

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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