Skip to content

Commit 91150af

Browse files
committed
C#: Re-factor HardcodedConnectionString to use the new API.
1 parent 3bda0b9 commit 91150af

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

csharp/ql/src/Security Features/CWE-798/HardcodedConnectionString.ql

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import csharp
1616
import semmle.code.csharp.frameworks.system.Data
1717
import semmle.code.csharp.security.dataflow.HardcodedCredentialsQuery
18-
import semmle.code.csharp.dataflow.DataFlow::DataFlow::PathGraph
18+
import ConnectionString::PathGraph
1919

2020
/**
2121
* A string literal containing a username or password field.
@@ -29,24 +29,24 @@ class ConnectionStringPasswordOrUsername extends NonEmptyStringLiteral {
2929
/**
3030
* A taint-tracking configuration for tracking string literals to a `ConnectionString` property.
3131
*/
32-
class ConnectionStringTaintTrackingConfiguration extends TaintTracking::Configuration {
33-
ConnectionStringTaintTrackingConfiguration() { this = "connectionstring" }
32+
module ConnectionStringConfig implements DataFlow::ConfigSig {
33+
predicate isSource(DataFlow::Node source) { source instanceof ConnectionStringPasswordOrUsername }
3434

35-
override predicate isSource(DataFlow::Node source) {
36-
source instanceof ConnectionStringPasswordOrUsername
37-
}
38-
39-
override predicate isSink(DataFlow::Node sink) {
35+
predicate isSink(DataFlow::Node sink) {
4036
sink.asExpr() =
4137
any(SystemDataConnectionClass connection).getConnectionStringProperty().getAnAssignedValue()
4238
}
4339

44-
override predicate isSanitizer(DataFlow::Node node) { node instanceof StringFormatSanitizer }
40+
predicate isBarrier(DataFlow::Node node) { node instanceof StringFormatSanitizer }
4541
}
4642

47-
from
48-
ConnectionStringTaintTrackingConfiguration c, DataFlow::PathNode source, DataFlow::PathNode sink
49-
where c.hasFlowPath(source, sink)
43+
/**
44+
* A taint-tracking module for tracking string literals to a `ConnectionString` property.
45+
*/
46+
module ConnectionString = TaintTracking::Global<ConnectionStringConfig>;
47+
48+
from ConnectionString::PathNode source, ConnectionString::PathNode sink
49+
where ConnectionString::flowPath(source, sink)
5050
select source.getNode(), source, sink,
5151
"'ConnectionString' property includes hard-coded credentials set in $@.",
5252
any(Call call | call.getAnArgument() = sink.getNode().asExpr()) as call, call.toString()

csharp/ql/test/query-tests/Security Features/CWE-798/HardcodedConnectionString.expected

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
edges
2-
| HardcodedCredentials.cs:47:30:47:60 | array creation of type Byte[] : Byte[] | HardcodedCredentials.cs:50:13:50:23 | access to local variable rawCertData |
32
nodes
4-
| HardcodedCredentials.cs:15:25:15:36 | "myPa55word" | semmle.label | "myPa55word" |
5-
| HardcodedCredentials.cs:31:19:31:28 | "username" | semmle.label | "username" |
6-
| HardcodedCredentials.cs:45:39:45:53 | "myNewPa55word" | semmle.label | "myNewPa55word" |
7-
| HardcodedCredentials.cs:47:30:47:60 | array creation of type Byte[] : Byte[] | semmle.label | array creation of type Byte[] : Byte[] |
8-
| HardcodedCredentials.cs:50:13:50:23 | access to local variable rawCertData | semmle.label | access to local variable rawCertData |
9-
| HardcodedCredentials.cs:51:13:51:24 | "myPa55word" | semmle.label | "myPa55word" |
103
| HardcodedCredentials.cs:54:48:54:63 | "Password=12345" | semmle.label | "Password=12345" |
114
| HardcodedCredentials.cs:56:49:56:63 | "User Id=12345" | semmle.label | "User Id=12345" |
12-
| HardcodedCredentials.cs:74:31:74:42 | "myusername" | semmle.label | "myusername" |
13-
| HardcodedCredentials.cs:74:45:74:56 | "mypassword" | semmle.label | "mypassword" |
14-
| TestHardcodedCredentials.cs:21:31:21:42 | "myusername" | semmle.label | "myusername" |
15-
| TestHardcodedCredentials.cs:21:45:21:56 | "mypassword" | semmle.label | "mypassword" |
16-
| TestHardcodedCredentials.cs:26:19:26:28 | "username" | semmle.label | "username" |
175
subpaths
186
#select
197
| HardcodedCredentials.cs:54:48:54:63 | "Password=12345" | HardcodedCredentials.cs:54:48:54:63 | "Password=12345" | HardcodedCredentials.cs:54:48:54:63 | "Password=12345" | 'ConnectionString' property includes hard-coded credentials set in $@. | HardcodedCredentials.cs:54:30:54:64 | object creation of type SqlConnection | object creation of type SqlConnection |

0 commit comments

Comments
 (0)