-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add fixes to python cryptography rules * Move rule test syntaxt to reflect focus metavariable changes * update mode recommendation to gcn * update fixtest to reflect changes to fix Co-authored-by: Pieter De Cremer (Semgrep) <[email protected]>
- Loading branch information
1 parent
a7d1278
commit 31e01cd
Showing
15 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
python/cryptography/security/insecure-cipher-algorithms-arc4.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# cf. https://github.com/PyCQA/bandit/blob/b78c938c0bd03d201932570f5e054261e10c5750/examples/ciphers.py | ||
|
||
from cryptography.hazmat.primitives.ciphers import Cipher | ||
from cryptography.hazmat.primitives.ciphers import algorithms | ||
from cryptography.hazmat.primitives.ciphers import modes | ||
from cryptography.hazmat.backends import default_backend | ||
from struct import pack | ||
|
||
# ruleid:insecure-cipher-algorithm-arc4 | ||
cipher = Cipher(algorithms.AES(key), mode=None, backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") | ||
|
||
# ok:insecure-cipher-algorithm-arc4 | ||
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") + encryptor.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
python/cryptography/security/insecure-cipher-algorithms-blowfish.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# cf. https://github.com/PyCQA/bandit/blob/b78c938c0bd03d201932570f5e054261e10c5750/examples/ciphers.py | ||
|
||
from cryptography.hazmat.primitives.ciphers import Cipher | ||
from cryptography.hazmat.primitives.ciphers import algorithms | ||
from cryptography.hazmat.primitives.ciphers import modes | ||
from cryptography.hazmat.backends import default_backend | ||
from struct import pack | ||
|
||
|
||
# ruleid:insecure-cipher-algorithm-blowfish | ||
cipher = Cipher(algorithms.AES(key), mode=None, backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") | ||
|
||
# ok:insecure-cipher-algorithm-blowfish | ||
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") + encryptor.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
python/cryptography/security/insecure-cipher-algorithms.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# cf. https://github.com/PyCQA/bandit/blob/b78c938c0bd03d201932570f5e054261e10c5750/examples/ciphers.py | ||
|
||
from cryptography.hazmat.primitives.ciphers import Cipher | ||
from cryptography.hazmat.primitives.ciphers import algorithms | ||
from cryptography.hazmat.primitives.ciphers import modes | ||
from cryptography.hazmat.backends import default_backend | ||
from struct import pack | ||
|
||
# ruleid:insecure-cipher-algorithm-idea | ||
cipher = Cipher(algorithms.AES(key), mode=None, backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") | ||
|
||
# ok:insecure-cipher-algorithm-idea | ||
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") + encryptor.finalize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
python/cryptography/security/insecure-cipher-mode-ecb.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# cf. https://github.com/PyCQA/bandit/blob/b1411bfb43795d3ffd268bef17a839dee954c2b1/examples/cipher-modes.py | ||
|
||
from cryptography.hazmat.primitives.ciphers.modes import CBC | ||
from cryptography.hazmat.primitives.ciphers.modes import ECB | ||
|
||
|
||
# Insecure mode | ||
# ruleid: insecure-cipher-mode-ecb | ||
mode = cryptography.hazmat.primitives.ciphers.modes.GCM(iv) | ||
|
||
# Secure cipher and mode | ||
# ok: insecure-cipher-mode-ecb | ||
cipher = AES.new(key, blockalgo.MODE_CTR, iv) | ||
|
||
# Secure mode | ||
# ok: insecure-cipher-mode-ecb | ||
mode = CBC(iv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
python/cryptography/security/insecure-hash-algorithms-md5.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# cf. https://github.com/PyCQA/bandit/blob/b78c938c0bd03d201932570f5e054261e10c5750/examples/crypto-md5.py | ||
|
||
from cryptography.hazmat.primitives import hashes | ||
|
||
# ruleid:insecure-hash-algorithm-md5 | ||
hashes.SHA256() | ||
# ok:insecure-hash-algorithm-md5 | ||
hashes.SHA256() | ||
# ok:insecure-hash-algorithm-md5 | ||
hashes.SHA3_256() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
python/cryptography/security/insufficient-dsa-key-size.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from cryptography.hazmat import backends | ||
from cryptography.hazmat.primitives.asymmetric import dsa | ||
|
||
# ok: insufficient-dsa-key-size | ||
dsa.generate_private_key(key_size=2048, | ||
backend=backends.default_backend()) | ||
|
||
# ok: insufficient-dsa-key-size | ||
dsa.generate_private_key(2048, | ||
backend=backends.default_backend()) | ||
|
||
# ruleid: insufficient-dsa-key-size | ||
dsa.generate_private_key(key_size=2048, | ||
backend=backends.default_backend()) | ||
|
||
# ruleid: insufficient-dsa-key-size | ||
dsa.generate_private_key(2048, | ||
backend=backends.default_backend()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
python/cryptography/security/insufficient-rsa-key-size.fixed.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
from cryptography.hazmat import backends | ||
from cryptography.hazmat.primitives.asymmetric import rsa | ||
|
||
rsa.generate_private_key(public_exponent=65537, | ||
# ok: insufficient-rsa-key-size | ||
key_size=2048, | ||
backend=backends.default_backend()) | ||
|
||
rsa.generate_private_key(65537, | ||
# ok: insufficient-rsa-key-size | ||
2048, | ||
backends.default_backend()) | ||
|
||
rsa.generate_private_key(public_exponent=65537, | ||
# ok: insufficient-rsa-key-size | ||
key_size=os.getenv("KEY_SIZE"), | ||
backend=backends.default_backend()) | ||
|
||
rsa.generate_private_key(65537, | ||
# ok: insufficient-rsa-key-size | ||
2048, | ||
backends.default_backend()) | ||
|
||
rsa.generate_private_key(public_exponent=65537, | ||
# ruleid: insufficient-rsa-key-size | ||
key_size=2048, | ||
backend=backends.default_backend()) | ||
|
||
rsa.generate_private_key(65537, | ||
# ruleid: insufficient-rsa-key-size | ||
2048, | ||
backends.default_backend()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters