-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add further tests of cryptography (#177)
Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
- Loading branch information
Showing
10 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
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
5 changes: 5 additions & 0 deletions
5
tests/unit/rules/python/third_party/cryptography/examples/algorithms_arc4.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
5 changes: 5 additions & 0 deletions
5
tests/unit/rules/python/third_party/cryptography/examples/algorithms_blowfish.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
5 changes: 5 additions & 0 deletions
5
tests/unit/rules/python/third_party/cryptography/examples/algorithms_idea.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
6 changes: 5 additions & 1 deletion
6
tests/unit/rules/python/third_party/cryptography/examples/hashes_md5.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
5 changes: 5 additions & 0 deletions
5
tests/unit/rules/python/third_party/cryptography/examples/hashes_sha1.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
18 changes: 18 additions & 0 deletions
18
tests/unit/rules/python/third_party/cryptography/examples/modes_ecb.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 @@ | ||
# level: ERROR | ||
# start_line: 15 | ||
# end_line: 15 | ||
# start_column: 13 | ||
# end_column: 16 | ||
import os | ||
|
||
from cryptography.hazmat.primitives.ciphers import algorithms | ||
from cryptography.hazmat.primitives.ciphers import Cipher | ||
from cryptography.hazmat.primitives.ciphers import modes | ||
|
||
|
||
key = os.urandom(32) | ||
algorithm = algorithms.AES(key) | ||
mode = modes.ECB() | ||
cipher = Cipher(algorithm, mode=mode) | ||
encryptor = cipher.encryptor() | ||
ct = encryptor.update(b"a secret message") + encryptor.finalize() |
39 changes: 39 additions & 0 deletions
39
tests/unit/rules/python/third_party/cryptography/test_cryptography_weak_cipher.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,39 @@ | ||
# Copyright 2023 Secure Saurce LLC | ||
import os | ||
|
||
from parameterized import parameterized | ||
|
||
from precli.core.level import Level | ||
from precli.parsers import python | ||
from precli.rules import Rule | ||
from tests.unit.rules.python import test_case | ||
|
||
|
||
class CryptographyWeakCipherTests(test_case.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.rule_id = "PRE0501" | ||
self.parser = python.Python(enabled=[self.rule_id]) | ||
self.base_path = os.path.join( | ||
"tests", | ||
"unit", | ||
"rules", | ||
"python", | ||
"third_party", | ||
"cryptography", | ||
"examples", | ||
) | ||
|
||
def test_cryptography_weak_cipher_rule_meta(self): | ||
rule = Rule.get_by_id(self.rule_id) | ||
self.assertEqual(self.rule_id, rule.id) | ||
self.assertEqual( | ||
"use_of_a_broken_or_risky_cryptographic_algorithm", rule.name | ||
) | ||
self.assertEqual( | ||
f"https://docs.securesauce.dev/rules/{self.rule_id}", rule.help_url | ||
) | ||
self.assertEqual(True, rule.default_config.enabled) | ||
self.assertEqual(Level.WARNING, rule.default_config.level) | ||
self.assertEqual(-1.0, rule.default_config.rank) | ||
self.assertEqual("327", rule.cwe.cwe_id) |
45 changes: 45 additions & 0 deletions
45
tests/unit/rules/python/third_party/cryptography/test_cryptography_weak_cipher_mode.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,45 @@ | ||
# Copyright 2023 Secure Saurce LLC | ||
import os | ||
|
||
from parameterized import parameterized | ||
|
||
from precli.core.level import Level | ||
from precli.parsers import python | ||
from precli.rules import Rule | ||
from tests.unit.rules.python import test_case | ||
|
||
|
||
class CryptographyWeakCipherModeTests(test_case.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.rule_id = "PRE0502" | ||
self.parser = python.Python(enabled=[self.rule_id]) | ||
self.base_path = os.path.join( | ||
"tests", | ||
"unit", | ||
"rules", | ||
"python", | ||
"third_party", | ||
"cryptography", | ||
"examples", | ||
) | ||
|
||
def test_cryptography_weak_cipher_mode_rule_meta(self): | ||
rule = Rule.get_by_id(self.rule_id) | ||
self.assertEqual(self.rule_id, rule.id) | ||
self.assertEqual("use_of_risky_cryptographic_cipher_mode", rule.name) | ||
self.assertEqual( | ||
f"https://docs.securesauce.dev/rules/{self.rule_id}", rule.help_url | ||
) | ||
self.assertEqual(True, rule.default_config.enabled) | ||
self.assertEqual(Level.WARNING, rule.default_config.level) | ||
self.assertEqual(-1.0, rule.default_config.rank) | ||
self.assertEqual("327", rule.cwe.cwe_id) | ||
|
||
@parameterized.expand( | ||
[ | ||
"modes_ecb", | ||
] | ||
) | ||
def test(self, filename): | ||
self.check(filename) |
46 changes: 46 additions & 0 deletions
46
tests/unit/rules/python/third_party/cryptography/test_cryptography_weak_hash.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,46 @@ | ||
# Copyright 2023 Secure Saurce LLC | ||
import os | ||
|
||
from parameterized import parameterized | ||
|
||
from precli.core.level import Level | ||
from precli.parsers import python | ||
from precli.rules import Rule | ||
from tests.unit.rules.python import test_case | ||
|
||
|
||
class CryptographyWeakHashTests(test_case.TestCase): | ||
def setUp(self): | ||
super().setUp() | ||
self.rule_id = "PRE0503" | ||
self.parser = python.Python(enabled=[self.rule_id]) | ||
self.base_path = os.path.join( | ||
"tests", | ||
"unit", | ||
"rules", | ||
"python", | ||
"third_party", | ||
"cryptography", | ||
"examples", | ||
) | ||
|
||
def test_cryptography_weak_hash_rule_meta(self): | ||
rule = Rule.get_by_id(self.rule_id) | ||
self.assertEqual(self.rule_id, rule.id) | ||
self.assertEqual("reversible_one_way_hash", rule.name) | ||
self.assertEqual( | ||
f"https://docs.securesauce.dev/rules/{self.rule_id}", rule.help_url | ||
) | ||
self.assertEqual(True, rule.default_config.enabled) | ||
self.assertEqual(Level.WARNING, rule.default_config.level) | ||
self.assertEqual(-1.0, rule.default_config.rank) | ||
self.assertEqual("328", rule.cwe.cwe_id) | ||
|
||
@parameterized.expand( | ||
[ | ||
"hashes_md5", | ||
"hashes_sha1", | ||
] | ||
) | ||
def test(self, filename): | ||
self.check(filename) |