Skip to content

Commit 73c4427

Browse files
committed
Rust: Fix near duplicate results.
1 parent e9ab160 commit 73c4427

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

‎rust/ql/lib/codeql/rust/security/HardcodedCryptographicValueExtensions.qll‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ module HardcodedCryptographicValue {
7171
or
7272
isConstant(e.(ArrayRepeatExpr).getRepeatOperand()) // e.g. `[0; 10]`
7373
or
74+
// a match expression with one or more constant arms; taint would reach here
75+
// anyway, but we make it a source to avoid reporting many similar results
76+
// on each match arm.
77+
isConstant(e.(MatchExpr).getMatchArmList().getAnArm().getExpr())
78+
or
7479
// e.g. `const MY_CONST: u64 = ...`
7580
// the constant initializer / body is the preferred source location for flow paths, when available.
7681
e = any(Const c).getBody()

‎rust/ql/test/query-tests/security/CWE-798/HardcodedCryptographicValue.expected‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
| test_cipher.rs:73:19:73:26 | [0u8; 32] | test_cipher.rs:73:19:73:26 | [0u8; 32] | test_cipher.rs:74:46:74:51 | const2 | This hard-coded value is used as $@. | test_cipher.rs:74:46:74:51 | const2 | a key |
99
| test_cookie.rs:17:28:17:34 | [0; 64] | test_cookie.rs:17:28:17:34 | [0; 64] | test_cookie.rs:18:26:18:32 | &array1 | This hard-coded value is used as $@. | test_cookie.rs:18:26:18:32 | &array1 | a key |
1010
| test_cookie.rs:21:28:21:34 | [0; 64] | test_cookie.rs:21:28:21:34 | [0; 64] | test_cookie.rs:22:26:22:32 | &array2 | This hard-coded value is used as $@. | test_cookie.rs:22:26:22:32 | &array2 | a key |
11-
| test_cookie.rs:26:14:26:18 | "one" | test_cookie.rs:26:14:26:18 | "one" | test_cookie.rs:30:26:30:40 | str3.as_bytes() | This hard-coded value is used as $@. | test_cookie.rs:30:26:30:40 | str3.as_bytes() | a key |
12-
| test_cookie.rs:27:14:27:18 | "two" | test_cookie.rs:27:14:27:18 | "two" | test_cookie.rs:30:26:30:40 | str3.as_bytes() | This hard-coded value is used as $@. | test_cookie.rs:30:26:30:40 | str3.as_bytes() | a key |
13-
| test_cookie.rs:28:14:28:19 | "many" | test_cookie.rs:28:14:28:19 | "many" | test_cookie.rs:30:26:30:40 | str3.as_bytes() | This hard-coded value is used as $@. | test_cookie.rs:30:26:30:40 | str3.as_bytes() | a key |
11+
| test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:30:26:30:40 | str3.as_bytes() | This hard-coded value is used as $@. | test_cookie.rs:30:26:30:40 | str3.as_bytes() | a key |
1412
| test_cookie.rs:46:28:46:36 | [0u8; 64] | test_cookie.rs:46:28:46:36 | [0u8; 64] | test_cookie.rs:50:34:50:39 | array2 | This hard-coded value is used as $@. | test_cookie.rs:50:34:50:39 | array2 | a key |
1513
| test_cookie.rs:57:23:57:25 | 0u8 | test_cookie.rs:57:23:57:25 | 0u8 | test_cookie.rs:61:34:61:39 | array3 | This hard-coded value is used as $@. | test_cookie.rs:61:34:61:39 | array3 | a key |
1614
| test_heuristic.rs:38:25:38:30 | 0xFFFF | test_heuristic.rs:38:25:38:30 | 0xFFFF | test_heuristic.rs:81:22:81:31 | MY_CONST_1 | This hard-coded value is used as $@. | test_heuristic.rs:81:22:81:31 | MY_CONST_1 | a salt |
@@ -69,9 +67,7 @@ edges
6967
| test_cookie.rs:22:26:22:32 | &array2 [&ref] | test_cookie.rs:22:26:22:32 | &array2 | provenance | Sink:MaD:5 |
7068
| test_cookie.rs:22:27:22:32 | array2 | test_cookie.rs:22:26:22:32 | &array2 [&ref] | provenance | |
7169
| test_cookie.rs:25:9:25:12 | str3 | test_cookie.rs:30:26:30:29 | str3 | provenance | |
72-
| test_cookie.rs:26:14:26:18 | "one" | test_cookie.rs:25:9:25:12 | str3 | provenance | |
73-
| test_cookie.rs:27:14:27:18 | "two" | test_cookie.rs:25:9:25:12 | str3 | provenance | |
74-
| test_cookie.rs:28:14:28:19 | "many" | test_cookie.rs:25:9:25:12 | str3 | provenance | |
70+
| test_cookie.rs:25:16:29:5 | match ... { ... } | test_cookie.rs:25:9:25:12 | str3 | provenance | |
7571
| test_cookie.rs:30:26:30:29 | str3 | test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | provenance | MaD:13 |
7672
| test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | test_cookie.rs:30:26:30:40 | str3.as_bytes() | provenance | Sink:MaD:5 |
7773
| test_cookie.rs:46:9:46:14 | array2 | test_cookie.rs:50:34:50:39 | array2 | provenance | Sink:MaD:1 |
@@ -163,9 +159,7 @@ nodes
163159
| test_cookie.rs:22:26:22:32 | &array2 [&ref] | semmle.label | &array2 [&ref] |
164160
| test_cookie.rs:22:27:22:32 | array2 | semmle.label | array2 |
165161
| test_cookie.rs:25:9:25:12 | str3 | semmle.label | str3 |
166-
| test_cookie.rs:26:14:26:18 | "one" | semmle.label | "one" |
167-
| test_cookie.rs:27:14:27:18 | "two" | semmle.label | "two" |
168-
| test_cookie.rs:28:14:28:19 | "many" | semmle.label | "many" |
162+
| test_cookie.rs:25:16:29:5 | match ... { ... } | semmle.label | match ... { ... } |
169163
| test_cookie.rs:30:26:30:29 | str3 | semmle.label | str3 |
170164
| test_cookie.rs:30:26:30:40 | str3.as_bytes() | semmle.label | str3.as_bytes() |
171165
| test_cookie.rs:30:26:30:40 | str3.as_bytes() [&ref] | semmle.label | str3.as_bytes() [&ref] |

‎rust/ql/test/query-tests/security/CWE-798/test_cookie.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ fn test_cookie_jar(array_var: &[u8], val: u64) {
2323
_ = jar.private_mut(&key2);
2424

2525
let str3 = match(val) {
26-
0 => "one", // $ Alert[rust/hard-coded-cryptographic-value]
27-
1 => "two", // $ Alert[rust/hard-coded-cryptographic-value]
28-
_ => "many" // $ Alert[rust/hard-coded-cryptographic-value]
29-
};
26+
0 => "one",
27+
1 => "two",
28+
_ => "many"
29+
}; // $ Alert[rust/hard-coded-cryptographic-value]
3030
let key3 = Key::from(str3.as_bytes()); // $ Sink
3131
_ = jar.signed_mut(&key3);
3232
}

0 commit comments

Comments
 (0)