Skip to content

Commit 72d31c8

Browse files
committed
check if using version > 4.0, where encrypt true by default
1 parent 9401ab2 commit 72d31c8

File tree

1 file changed

+68
-42
lines changed

1 file changed

+68
-42
lines changed

csharp/ql/src/Security Features/CWE-327/InsecureSQLConnection.ql

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,71 @@
1010
* external/cwe/cwe-327
1111
*/
1212

13-
import csharp
14-
import InsecureSqlConnection::PathGraph
15-
16-
/**
17-
* A data flow configuration for tracking strings passed to `SqlConnection[StringBuilder]` instances.
18-
*/
19-
module InsecureSqlConnectionConfig implements DataFlow::ConfigSig {
20-
predicate isSource(DataFlow::Node source) {
21-
exists(string s | s = source.asExpr().(StringLiteral).getValue().toLowerCase() |
22-
s.matches("%encrypt=false%")
23-
or
24-
not s.matches("%encrypt=%")
25-
)
26-
}
27-
28-
predicate isSink(DataFlow::Node sink) {
29-
exists(ObjectCreation oc |
30-
oc.getRuntimeArgument(0) = sink.asExpr() and
31-
(
32-
oc.getType().getName() = "SqlConnectionStringBuilder"
33-
or
34-
oc.getType().getName() = "SqlConnection"
35-
) and
36-
not exists(MemberInitializer mi |
37-
mi = oc.getInitializer().(ObjectInitializer).getAMemberInitializer() and
38-
mi.getLValue().(PropertyAccess).getTarget().getName() = "Encrypt" and
39-
mi.getRValue().(BoolLiteral).getValue() = "true"
40-
)
41-
)
42-
}
43-
}
44-
45-
/**
46-
* A data flow configuration for tracking strings passed to `SqlConnection[StringBuilder]` instances.
47-
*/
48-
module InsecureSqlConnection = DataFlow::Global<InsecureSqlConnectionConfig>;
49-
50-
from InsecureSqlConnection::PathNode source, InsecureSqlConnection::PathNode sink
51-
where InsecureSqlConnection::flowPath(source, sink)
52-
select sink.getNode(), source, sink,
53-
"$@ flows to this SQL connection and does not specify `Encrypt=True`.", source.getNode(),
54-
"Connection string"
13+
import csharp
14+
import InsecureSqlConnection::PathGraph
15+
16+
class Source extends DataFlow::Node{
17+
string sourcestring;
18+
Source(){
19+
sourcestring = this.asExpr().(StringLiteral).getValue().toLowerCase() and
20+
(
21+
not sourcestring.matches("%encrypt=%") or
22+
sourcestring.matches("%encrypt=false%")
23+
)
24+
}
25+
predicate setsEncryptFalse(){
26+
sourcestring.matches("%encrypt=false%")
27+
}
28+
}
29+
30+
class Sink extends DataFlow::Node {
31+
Version version;
32+
Sink(){
33+
exists(ObjectCreation oc |
34+
oc.getRuntimeArgument(0) = this.asExpr() and
35+
(
36+
oc.getType().getName() = "SqlConnectionStringBuilder"
37+
or
38+
oc.getType().getName() = "SqlConnection"
39+
) and
40+
version = oc.getType().getALocation().(Assembly).getVersion()
41+
)
42+
}
43+
predicate isEncryptedByDefault(){
44+
version.compareTo("4.0") >= 0
45+
}
46+
Version getVersion(){
47+
result = version
48+
}
49+
}
50+
51+
predicate isEncryptTrue(Source source, Sink sink){
52+
sink.isEncryptedByDefault() and
53+
not source.setsEncryptFalse()
54+
}
55+
56+
/**
57+
* A data flow configuration for tracking strings passed to `SqlConnection[StringBuilder]` instances.
58+
*/
59+
module InsecureSqlConnectionConfig implements DataFlow::ConfigSig {
60+
predicate isSource(DataFlow::Node source) {
61+
source instanceof Source
62+
}
63+
64+
predicate isSink(DataFlow::Node sink) {
65+
sink instanceof Sink
66+
}
67+
}
68+
69+
/**
70+
* A data flow configuration for tracking strings passed to `SqlConnection[StringBuilder]` instances.
71+
*/
72+
module InsecureSqlConnection = DataFlow::Global<InsecureSqlConnectionConfig>;
73+
74+
from InsecureSqlConnection::PathNode source, InsecureSqlConnection::PathNode sink
75+
where InsecureSqlConnection::flowPath(source, sink) and
76+
not isEncryptTrue(source.getNode().(Source), sink.getNode().(Sink))
77+
select sink.getNode(), source, sink,
78+
"$@ flows to this SQL connection and does not specify `Encrypt=True`.", source.getNode(),
79+
"Connection string"
80+

0 commit comments

Comments
 (0)