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