Skip to content

Commit

Permalink
feat(python): add cwe-326 inadequate encryption strength rules
Browse files Browse the repository at this point in the history
  • Loading branch information
didroe authored and elsapet committed May 28, 2024
1 parent be64956 commit 5c62396
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
38 changes: 38 additions & 0 deletions rules/python/django/weak_secret_key.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
patterns:
- pattern: $<NAME> = $<KEY>
filters:
- variable: NAME
values:
- SECRET_KEY
- CRYPTOGRAPHY_KEY
- variable: KEY
length_less_than: 12
languages:
- python
metadata:
description: Usage of weak secret key
remediation_message: |-
## Description
Weak secret keys can compromise data security. To ensure effective encryption, secret keys should be 12 bytes or greater.
## Remediations
- **Do not** use secret keys shorter than 12 bytes. Short keys are easier to crack, putting your data at risk.
```python
SECRET_KEY = "weak" # unsafe
```
- **Do** ensure your secret keys are 12 bytes or longer to maintain strong encryption and protect sensitive data.
```python
SECRET_KEY = "correct-horse-battery-staple"
```
## References
- [Django secret key setting and recommended best practice](https://docs.djangoproject.com/en/5.0/ref/settings/#std:setting-SECRET_KEY)
- [django-cryptography documentation](https://django-cryptography.readthedocs.io/en/latest/)
cwe_id:
- 326
id: python_django_weak_secret_key
documentation_url: https://docs.bearer.com/reference/rules/python_django_weak_secret_key
severity: high
1 change: 0 additions & 1 deletion rules/python/lang/weak_hash_dss.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ auxiliary:
languages:
- python
skip_data_types:
- "Unique Identifier"
- "Passwords" # see python_lang_weak_password_hash_dss
metadata:
description: "Usage of weak hashing library (DSS)"
Expand Down
1 change: 0 additions & 1 deletion rules/python/lang/weak_hash_md5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ auxiliary:
languages:
- python
skip_data_types:
- "Unique Identifier"
- "Passwords" # see python_lang_weak_password_encryption_md5
metadata:
description: "Usage of weak hashing library (MDx)"
Expand Down
1 change: 0 additions & 1 deletion rules/python/lang/weak_hash_sha1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ auxiliary:
languages:
- python
skip_data_types:
- "Unique Identifier"
- "Passwords" # see python_lang_weak_password_encryption_sha1
metadata:
description: "Usage of weak hashing library (SHA-1)"
Expand Down
20 changes: 20 additions & 0 deletions tests/python/django/weak_secret_key/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const {
createNewInvoker,
getEnvironment,
} = require("../../../helper.js")
const { ruleId, ruleFile, testBase } = getEnvironment(__dirname)

describe(ruleId, () => {
const invoke = createNewInvoker(ruleId, ruleFile, testBase)

test("weak_secret_key", () => {
const testCase = "main.py"

const results = invoke(testCase)

expect(results).toEqual({
Missing: [],
Extra: []
})
})
})
7 changes: 7 additions & 0 deletions tests/python/django/weak_secret_key/testdata/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SECRET_KEY = "correct-horse-battery-staple"
# bearer:expected python_django_weak_secret_key
SECRET_KEY = "weak"

CRYPTOGRAPHY_KEY = "correct-horse-battery-staple"
# bearer:expected python_django_weak_secret_key
CRYPTOGRAPHY_KEY = "weak"

0 comments on commit 5c62396

Please sign in to comment.