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

Merge Develop into Release #3276

Merged
merged 3 commits into from
Jan 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/semgrep-rules-test-historical.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
python-version: 3.9.2
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v23.1
uses: tj-actions/changed-files@v41
with:
path: semgrep-rules
separator: ","
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]
jinja2 = "~=2.11.3"
jinja2 = "~=3.1.3"
pytest = "*"
semgrep = "*"
pyyaml = "*"
Expand Down
10 changes: 5 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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()
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
rules:
- id: insecure-cipher-algorithm-arc4
pattern: cryptography.hazmat.primitives.ciphers.algorithms.ARC4(...)
message: >-
ARC4 (Alleged RC4) is a stream cipher with serious weaknesses in its initial stream output.
Its use is strongly discouraged. ARC4 does not use mode constructions.
Use a strong symmetric cipher such as EAS instead.
With the `cryptography` package it is recommended to use the `Fernet` which is a secure implementation
of AES in CBC mode with a 128-bit key.
Alternatively, keep using the `Cipher` class from the hazmat primitives but use the AES algorithm instead.
metadata:
source-rule-url: https://github.com/PyCQA/bandit/blob/d5f8fa0d89d7b11442fc6ec80ca42953974354c8/bandit/blacklists/calls.py#L98
cwe:
Expand All @@ -28,3 +30,11 @@ rules:
severity: WARNING
languages:
- python
patterns:
- pattern: cryptography.hazmat.primitives.ciphers.algorithms.$ARC4($KEY)
- pattern-inside: cryptography.hazmat.primitives.ciphers.Cipher(...)
- metavariable-regex:
metavariable: $ARC4
regex: ^(ARC4)$
- focus-metavariable: $ARC4
fix: AES
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()
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
rules:
- id: insecure-cipher-algorithm-blowfish
pattern: cryptography.hazmat.primitives.ciphers.algorithms.Blowfish(...)
message: >-
Blowfish is a block cipher developed by Bruce Schneier. It is known to be susceptible to attacks when using weak keys.
The author has recommended that users of Blowfish move to newer algorithms such as AES.
With the `cryptography` package it is recommended to use `Fernet` which is a secure implementation
of AES in CBC mode with a 128-bit key.
Alternatively, keep using the `Cipher` class from the hazmat primitives but use the AES algorithm instead.
metadata:
source-rule-url: https://github.com/PyCQA/bandit/blob/d5f8fa0d89d7b11442fc6ec80ca42953974354c8/bandit/blacklists/calls.py#L98
cwe:
Expand All @@ -28,3 +30,10 @@ rules:
severity: WARNING
languages:
- python
patterns:
- pattern: cryptography.hazmat.primitives.ciphers.algorithms.$BLOWFISH($KEY)
- metavariable-regex:
metavariable: $BLOWFISH
regex: ^(Blowfish)$
- focus-metavariable: $BLOWFISH
fix: AES
17 changes: 17 additions & 0 deletions python/cryptography/security/insecure-cipher-algorithms.fixed.py
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()
11 changes: 10 additions & 1 deletion python/cryptography/security/insecure-cipher-algorithms.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
rules:
- id: insecure-cipher-algorithm-idea
pattern: cryptography.hazmat.primitives.ciphers.algorithms.IDEA(...)
message: >-
IDEA (International Data Encryption Algorithm) is a block cipher created in 1991.
It is an optional component of the OpenPGP standard. This cipher is susceptible to attacks when using weak keys.
It is recommended that you do not use this cipher for new applications.
Use a strong symmetric cipher such as EAS instead.
With the `cryptography` package it is recommended to use `Fernet` which is a secure implementation
of AES in CBC mode with a 128-bit key.
Alternatively, keep using the `Cipher` class from the hazmat primitives but use the AES algorithm instead.
metadata:
source-rule-url: https://github.com/PyCQA/bandit/blob/d5f8fa0d89d7b11442fc6ec80ca42953974354c8/bandit/blacklists/calls.py#L98
cwe:
Expand All @@ -30,3 +32,10 @@ rules:
severity: WARNING
languages:
- python
patterns:
- pattern: cryptography.hazmat.primitives.ciphers.algorithms.$IDEA($KEY)
- metavariable-regex:
metavariable: $IDEA
regex: ^(IDEA)$
- focus-metavariable: $IDEA
fix: AES
17 changes: 17 additions & 0 deletions python/cryptography/security/insecure-cipher-mode-ecb.fixed.py
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)
5 changes: 3 additions & 2 deletions python/cryptography/security/insecure-cipher-mode-ecb.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
rules:
- id: insecure-cipher-mode-ecb
pattern: cryptography.hazmat.primitives.ciphers.modes.ECB(...)
message: >-
ECB (Electronic Code Book) is the simplest mode of operation for block ciphers.
Each block of data is encrypted in the same way.
This means identical plaintext blocks will always result in identical ciphertext blocks, which can leave significant patterns in the output.
Use a different, more secure mode instead.
Use a different, cryptographically strong mode instead, such as GCM.
metadata:
source-rule-url: https://github.com/PyCQA/bandit/blob/d5f8fa0d89d7b11442fc6ec80ca42953974354c8/bandit/blacklists/calls.py#L101
cwe:
Expand All @@ -30,3 +29,5 @@ rules:
severity: WARNING
languages:
- python
pattern: cryptography.hazmat.primitives.ciphers.modes.ECB($IV)
fix: cryptography.hazmat.primitives.ciphers.modes.GCM($IV)
10 changes: 10 additions & 0 deletions python/cryptography/security/insecure-hash-algorithms-md5.fixed.py
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()
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
rules:
- id: insecure-hash-algorithm-md5
pattern: cryptography.hazmat.primitives.hashes.MD5(...)
message: >-
Detected MD5 hash algorithm which is considered insecure. MD5 is not
collision resistant and is therefore not suitable as a cryptographic
Expand Down Expand Up @@ -32,3 +31,10 @@ rules:
severity: WARNING
languages:
- python
patterns:
- pattern: cryptography.hazmat.primitives.hashes.$MD5()
- metavariable-regex:
metavariable: $MD5
regex: ^(MD5)$
- focus-metavariable: $MD5
fix: SHA256
18 changes: 18 additions & 0 deletions python/cryptography/security/insufficient-dsa-key-size.fixed.py
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())
3 changes: 3 additions & 0 deletions python/cryptography/security/insufficient-dsa-key-size.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ rules:
- metavariable-comparison:
metavariable: $SIZE
comparison: $SIZE < 2048
- focus-metavariable: $SIZE
fix: |
2048
message: >-
Detected an insufficient key size for DSA. NIST recommends
a key size of 2048 or higher.
Expand Down
33 changes: 33 additions & 0 deletions python/cryptography/security/insufficient-rsa-key-size.fixed.py
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())
12 changes: 6 additions & 6 deletions python/cryptography/security/insufficient-rsa-key-size.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@
from cryptography.hazmat import backends
from cryptography.hazmat.primitives.asymmetric import rsa

# ok: insufficient-rsa-key-size
rsa.generate_private_key(public_exponent=65537,
# ok: insufficient-rsa-key-size
key_size=2048,
backend=backends.default_backend())

# ok: insufficient-rsa-key-size
rsa.generate_private_key(65537,
# ok: insufficient-rsa-key-size
2048,
backends.default_backend())

# ok: insufficient-rsa-key-size
rsa.generate_private_key(public_exponent=65537,
# ok: insufficient-rsa-key-size
key_size=os.getenv("KEY_SIZE"),
backend=backends.default_backend())

# ok: insufficient-rsa-key-size
rsa.generate_private_key(65537,
# ok: insufficient-rsa-key-size
2048,
backends.default_backend())

# ruleid: insufficient-rsa-key-size
rsa.generate_private_key(public_exponent=65537,
# ruleid: insufficient-rsa-key-size
key_size=1024,
backend=backends.default_backend())

# ruleid: insufficient-rsa-key-size
rsa.generate_private_key(65537,
# ruleid: insufficient-rsa-key-size
1024,
backends.default_backend())
3 changes: 3 additions & 0 deletions python/cryptography/security/insufficient-rsa-key-size.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ rules:
- metavariable-comparison:
metavariable: $SIZE
comparison: $SIZE < 2048
- focus-metavariable: $SIZE
fix : |
2048
message: >-
Detected an insufficient key size for RSA. NIST recommends
a key size of 2048 or higher.
Expand Down
Loading