Skip to content

Commit 0779eb0

Browse files
authored
Add random.randbytes to blacklist calls (#1096)
In Python 3.9, the random module added new function randbytes(n). This function shouldn't be used for any cryptographic operations. As the doc recommends, use secrets.token_bytes() instead. https://docs.python.org/3/library/random.html#random.randbytes Signed-off-by: Eric Brown <[email protected]>
1 parent 7129108 commit 0779eb0

File tree

3 files changed

+5
-2
lines changed

3 files changed

+5
-2
lines changed

bandit/blacklists/calls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
| | | - random.choices | |
199199
| | | - random.uniform | |
200200
| | | - random.triangular | |
201+
| | | - random.randbytes | |
201202
+------+---------------------+------------------------------------+-----------+
202203
203204
B312: telnetlib
@@ -523,6 +524,7 @@ def gen_blacklist():
523524
"random.choices",
524525
"random.uniform",
525526
"random.triangular",
527+
"random.randbytes",
526528
],
527529
"Standard pseudo-random generators are not suitable for "
528530
"security/cryptographic purposes.",

examples/random_module.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
bad = random.choices()
1111
bad = random.uniform()
1212
bad = random.triangular()
13+
bad = random.randbytes()
1314

1415
good = os.urandom()
1516
good = random.SystemRandom()

tests/functional/test_functional.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,8 +396,8 @@ def test_popen_wrappers(self):
396396
def test_random_module(self):
397397
"""Test for the `random` module."""
398398
expect = {
399-
"SEVERITY": {"UNDEFINED": 0, "LOW": 8, "MEDIUM": 0, "HIGH": 0},
400-
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 8},
399+
"SEVERITY": {"UNDEFINED": 0, "LOW": 9, "MEDIUM": 0, "HIGH": 0},
400+
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 0, "HIGH": 9},
401401
}
402402
self.check_example("random_module.py", expect)
403403

0 commit comments

Comments
 (0)