Skip to content

Commit e977791

Browse files
authored
Merge pull request github#3856 from geoffw0/qldoc5follow
C++: Make getSecureAlgorithmRegex() work as expected.
2 parents 286c091 + 8bdcc47 commit e977791

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

cpp/ql/src/semmle/code/cpp/security/Encryption.qll

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ string getASecureAlgorithmName() {
5959
* contain an algorithm that is known to be secure.
6060
*/
6161
string getSecureAlgorithmRegex() {
62-
// algorithms usually appear in names surrounded by characters that are not
63-
// alphabetical characters in the same case. This handles the upper and lower
64-
// case cases
65-
result = "(^|.*[^A-Z])" + getASecureAlgorithmName() + "([^A-Z].*|$)"
66-
or
67-
// for lowercase, we want to be careful to avoid being confused by camelCase
68-
// hence we require two preceding uppercase letters to be sure of a case
69-
// switch, or a preceding non-alphabetic character
70-
result = "(^|.*[A-Z]{2}|.*[^a-zA-Z])" + getASecureAlgorithmName().toLowerCase() + "([^a-z].*|$)"
62+
result =
63+
// algorithms usually appear in names surrounded by characters that are not
64+
// alphabetical characters in the same case. This handles the upper and lower
65+
// case cases
66+
"(^|.*[^A-Z])(" + strictconcat(getASecureAlgorithmName(), "|") + ")([^A-Z].*|$)" + "|" +
67+
// for lowercase, we want to be careful to avoid being confused by camelCase
68+
// hence we require two preceding uppercase letters to be sure of a case
69+
// switch, or a preceding non-alphabetic character
70+
"(^|.*[A-Z]{2}|.*[^a-zA-Z])(" + strictconcat(getASecureAlgorithmName().toLowerCase(), "|") +
71+
")([^a-z].*|$)"
7172
}
7273

7374
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
void des_function(); // insecure
3+
void function_using_des(); // insecure
4+
void EncryptWithDES(); // insecure
5+
6+
void aes_function(); // secure
7+
void function_using_aes(); // secure
8+
void EncryptionWithAES(); // secure
9+
10+
void abc_function();
11+
void function_using_abc();
12+
void EncryptionWithABC();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| test.cpp:2:6:2:17 | des_function | getInsecureAlgorithmRegex |
2+
| test.cpp:3:6:3:23 | function_using_des | getInsecureAlgorithmRegex |
3+
| test.cpp:4:6:4:19 | EncryptWithDES | getInsecureAlgorithmRegex |
4+
| test.cpp:6:6:6:17 | aes_function | getSecureAlgorithmRegex |
5+
| test.cpp:7:6:7:23 | function_using_aes | getSecureAlgorithmRegex |
6+
| test.cpp:8:6:8:22 | EncryptionWithAES | getSecureAlgorithmRegex |
7+
| test.cpp:10:6:10:17 | abc_function | |
8+
| test.cpp:11:6:11:23 | function_using_abc | |
9+
| test.cpp:12:6:12:22 | EncryptionWithABC | |
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import default
2+
import semmle.code.cpp.security.Encryption
3+
4+
string describe(Function f) {
5+
f.getName().regexpMatch(getSecureAlgorithmRegex()) and
6+
result = "getSecureAlgorithmRegex"
7+
or
8+
f.getName().regexpMatch(getInsecureAlgorithmRegex()) and
9+
result = "getInsecureAlgorithmRegex"
10+
}
11+
12+
from Function f
13+
where exists(f.getLocation().getFile())
14+
select f, concat(describe(f), ", ")

0 commit comments

Comments
 (0)