Skip to content

Commit 8051cfc

Browse files
committed
Fix tests and fix getStringValue method
1 parent 6455e18 commit 8051cfc

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import InsecureCryptoFlow::PathGraph
2727
string getStringValue(DataFlow::Node algo) {
2828
result = algo.asExpr().(StringLiteral).getValue()
2929
or
30-
result = algo.asExpr().(PropertiesGetPropertyMethodCall).getPropertyValue()
30+
exists(string value | value = algo.asExpr().(PropertiesGetPropertyMethodCall).getPropertyValue() |
31+
result = value and not value.regexpMatch(getSecureAlgorithmRegex())
32+
)
3133
}
3234

3335
from InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
edges
2+
| WeakHashing.java:21:86:21:90 | "MD5" : String | WeakHashing.java:21:56:21:91 | getProperty(...) |
23
nodes
34
| Test.java:19:45:19:49 | "DES" | semmle.label | "DES" |
45
| Test.java:42:33:42:37 | "RC2" | semmle.label | "RC2" |
6+
| WeakHashing.java:21:56:21:91 | getProperty(...) | semmle.label | getProperty(...) |
7+
| WeakHashing.java:21:86:21:90 | "MD5" : String | semmle.label | "MD5" : String |
58
subpaths
69
#select
710
| Test.java:19:20:19:50 | getInstance(...) | Test.java:19:45:19:49 | "DES" | Test.java:19:45:19:49 | "DES" | Cryptographic algorithm $@ is weak and should not be used. | Test.java:19:45:19:49 | "DES" | DES |
811
| Test.java:42:14:42:38 | getInstance(...) | Test.java:42:33:42:37 | "RC2" | Test.java:42:33:42:37 | "RC2" | Cryptographic algorithm $@ is weak and should not be used. | Test.java:42:33:42:37 | "RC2" | RC2 |
12+
| WeakHashing.java:21:30:21:92 | getInstance(...) | WeakHashing.java:21:86:21:90 | "MD5" : String | WeakHashing.java:21:56:21:91 | getProperty(...) | Cryptographic algorithm $@ is weak and should not be used. | WeakHashing.java:21:86:21:90 | "MD5" | MD5 |

java/ql/test/query-tests/security/CWE-327/semmle/tests/MaybeBrokenCryptoAlgorithm.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ edges
22
nodes
33
| Test.java:34:48:34:52 | "foo" | semmle.label | "foo" |
44
| WeakHashing.java:15:55:15:83 | getProperty(...) | semmle.label | getProperty(...) |
5+
| WeakHashing.java:18:56:18:95 | getProperty(...) | semmle.label | getProperty(...) |
6+
| WeakHashing.java:21:56:21:91 | getProperty(...) | semmle.label | getProperty(...) |
57
subpaths
68
#select
79
| Test.java:34:21:34:53 | new SecretKeySpec(...) | Test.java:34:48:34:52 | "foo" | Test.java:34:48:34:52 | "foo" | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | Test.java:34:48:34:52 | "foo" | foo |
810
| WeakHashing.java:15:29:15:84 | getInstance(...) | WeakHashing.java:15:55:15:83 | getProperty(...) | WeakHashing.java:15:55:15:83 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:15:55:15:83 | getProperty(...) | MD5 |
11+
| WeakHashing.java:18:30:18:96 | getInstance(...) | WeakHashing.java:18:56:18:95 | getProperty(...) | WeakHashing.java:18:56:18:95 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:18:56:18:95 | getProperty(...) | MD5 |
12+
| WeakHashing.java:21:30:21:92 | getInstance(...) | WeakHashing.java:21:56:21:91 | getProperty(...) | WeakHashing.java:21:56:21:91 | getProperty(...) | Cryptographic algorithm $@ may not be secure, consider using a different algorithm. | WeakHashing.java:21:56:21:91 | getProperty(...) | MD5 |

java/ql/test/query-tests/security/CWE-327/semmle/tests/WeakHashing.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ void hashing() throws NoSuchAlgorithmException, IOException {
1616

1717
// BAD: Using a weak hashing algorithm even with a secure default
1818
MessageDigest bad2 = MessageDigest.getInstance(props.getProperty("hashAlg1", "SHA-256"));
19+
20+
// BAD: Using a strong hashing algorithm but with a weak default
21+
MessageDigest bad3 = MessageDigest.getInstance(props.getProperty("hashAlg2", "MD5"));
1922

2023
// GOOD: Using a strong hashing algorithm
2124
MessageDigest ok = MessageDigest.getInstance(props.getProperty("hashAlg2"));
2225

23-
// OK: Using a strong hashing algorithm even with a weak default
24-
MessageDigest ok2 = MessageDigest.getInstance(props.getProperty("hashAlg2", "MD5"));
25-
2626
// OK: Property does not exist and default is secure
27-
MessageDigest ok3 = MessageDigest.getInstance(props.getProperty("hashAlg3", "SHA-256"));
27+
MessageDigest ok2 = MessageDigest.getInstance(props.getProperty("hashAlg3", "SHA-256"));
2828
}
2929
}

0 commit comments

Comments
 (0)