1
-
2
1
private import csharp
3
2
private import semmle.code.csharp.frameworks.Moq
4
3
private import semmle.code.csharp.frameworks.System
@@ -25,10 +24,10 @@ abstract class Sanitizer extends DataFlow::ExprNode { }
25
24
class NonEmptyStringLiteral extends Source {
26
25
NonEmptyStringLiteral ( ) {
27
26
exists ( StringLiteral literal |
28
- this .getExpr ( ) = literal
29
- and literal .getValue ( ) .length ( ) > 1
30
- and literal .fromSource ( )
31
- and not literal .getValue ( ) .matches ( "${%}" )
27
+ this .getExpr ( ) = literal and
28
+ literal .getValue ( ) .length ( ) > 1 and
29
+ literal .fromSource ( ) and
30
+ not literal .getValue ( ) .matches ( "${%}" )
32
31
)
33
32
}
34
33
}
@@ -42,8 +41,8 @@ class ByteArrayLiteral extends Source {
42
41
any ( ArrayCreation ac |
43
42
ac .getArrayType ( ) .getElementType ( ) instanceof ByteType and
44
43
ac .hasInitializer ( )
45
- )
46
- and this .getExpr ( ) .fromSource ( )
44
+ ) and
45
+ this .getExpr ( ) .fromSource ( )
47
46
}
48
47
}
49
48
@@ -56,71 +55,70 @@ class CharArrayLiteral extends Source {
56
55
any ( ArrayCreation ac |
57
56
ac .getArrayType ( ) .getElementType ( ) instanceof CharType and
58
57
ac .hasInitializer ( )
59
- )
60
- and this .getExpr ( ) .fromSource ( )
58
+ ) and
59
+ this .getExpr ( ) .fromSource ( )
61
60
}
62
61
}
63
62
64
63
// taint from a string literal to the constructor of a SymmetricSecurityKey
65
- class LiteralToSecurityKeyConfig extends TaintTracking :: Configuration {
66
- LiteralToSecurityKeyConfig ( ) { this = "LiteralToSecurityKeyConfig" }
64
+ module LiteralToSecurityKeyConfig implements DataFlow :: ConfigSig {
65
+ predicate isSource ( DataFlow :: Node source ) { source instanceof Source }
67
66
68
- override predicate isSource ( DataFlow:: Node source ) { source instanceof Source }
67
+ predicate isSink ( DataFlow:: Node sink ) { sink instanceof Sink }
69
68
70
- override predicate isSink ( DataFlow:: Node sink ) {
71
- sink instanceof Sink
72
- }
73
-
74
- override predicate isSanitizer ( DataFlow:: Node node ) { node instanceof Sanitizer }
69
+ predicate isBarrier ( DataFlow:: Node node ) { node instanceof Sanitizer }
75
70
}
76
71
72
+ module LiteralToSecurityKeyFlow = TaintTracking:: Global< LiteralToSecurityKeyConfig > ;
73
+
77
74
class SymmetricSecurityKey extends Sink {
78
75
SymmetricSecurityKey ( ) {
79
76
exists ( ObjectCreation securityKey |
80
77
securityKey .getAnArgument ( ) = this .getExpr ( ) and
81
- securityKey . getType ( ) . getQualifiedName ( ) in [
82
- "Microsoft.IdentityModel.Tokens.SymmetricSecurityKey" ,
83
- "System .IdentityModel.Tokens.SymmetricSecurityKey"
84
- ]
78
+ securityKey
79
+ . getType ( )
80
+ . hasFullyQualifiedName ( [ "Microsoft .IdentityModel.Tokens" , "System.IdentityModel.Tokens" ] ,
81
+ "SymmetricSecurityKey" )
85
82
)
86
83
}
87
84
}
88
85
89
-
90
- /**
86
+ /**
91
87
* A result from a mock interface
92
88
* Ignore values that are ultimately returned by mocks, as they don't represent "real" credentials.
93
- */
89
+ */
94
90
class MockSanitizer extends Sanitizer {
95
91
MockSanitizer ( ) {
96
- exists ( ReturnedByMockObject mock |
97
- mock .getAMemberInitializationValue ( ) = this .asExpr ( )
98
- or mock .getAnArgument ( ) = this .asExpr ( )
92
+ exists ( ReturnedByMockObject mock |
93
+ mock .getAMemberInitializationValue ( ) = this .asExpr ( ) or
94
+ mock .getAnArgument ( ) = this .asExpr ( )
99
95
)
100
96
}
101
97
}
102
98
103
- /**
99
+ /**
104
100
* A result from a mock interface
105
101
* The function is not itself marked as a mock, but all uses of it are in mocks.
106
- */
102
+ */
107
103
class MockSanitizer2 extends Sanitizer {
108
104
MockSanitizer2 ( ) {
109
105
exists ( Method method , ReturnedByMockObject mock |
110
106
exists ( Call call |
111
- call = method .getACall ( ) and method .getAChild * ( ) = this .asExpr ( ) and
107
+ call = method .getACall ( ) and
108
+ method .getAChild * ( ) = this .asExpr ( ) and
112
109
(
113
- mock .getAMemberInitializationValue ( ) .getAChild * ( ) = call
114
- or mock .getAnArgument ( ) .getAChild * ( ) = call
110
+ mock .getAMemberInitializationValue ( ) .getAChild * ( ) = call or
111
+ mock .getAnArgument ( ) .getAChild * ( ) = call
115
112
)
116
- )
117
- and not exists ( Call call |
118
- call = method .getACall ( ) and method .getAChild * ( ) = this .asExpr ( ) and
113
+ ) and
114
+ not exists ( Call call |
115
+ call = method .getACall ( ) and
116
+ method .getAChild * ( ) = this .asExpr ( ) and
119
117
not (
120
- mock .getAMemberInitializationValue ( ) .getAChild * ( ) = call
121
- or mock .getAnArgument ( ) .getAChild * ( ) = call
122
- )
118
+ mock .getAMemberInitializationValue ( ) .getAChild * ( ) = call or
119
+ mock .getAnArgument ( ) .getAChild * ( ) = call
123
120
)
121
+ )
124
122
)
125
123
}
126
124
}
@@ -161,11 +159,12 @@ class ToStringSanitizer extends Sanitizer {
161
159
class ConfigurationSanitizer extends Sanitizer {
162
160
ConfigurationSanitizer ( ) {
163
161
exists ( Access configuration , MethodCall call |
164
- configuration .getType ( ) .getQualifiedName ( ) in [
165
- "Microsoft.Extensions.Configuration.IConfiguration" , "Microsoft.Extensions.Configuration.ConfigurationManager"
166
- ]
167
- and call .getQualifier ( ) = configuration
168
- and call .getAnArgument ( ) = this .getExpr ( )
162
+ configuration
163
+ .getType ( )
164
+ .hasFullyQualifiedName ( "Microsoft.Extensions.Configuration" ,
165
+ [ "IConfiguration" , "ConfigurationManager" ] ) and
166
+ call .getQualifier ( ) = configuration and
167
+ call .getAnArgument ( ) = this .getExpr ( )
169
168
)
170
169
}
171
170
}
@@ -175,9 +174,14 @@ class ConfigurationSanitizer extends Sanitizer {
175
174
*/
176
175
class FileSanitizer extends Sanitizer {
177
176
FileSanitizer ( ) {
178
- exists ( Call c | c .getTarget ( ) .hasQualifiedName ( "System.IO.File" , [
179
- "ReadAllBytes" , "ReadAllText" , "Open" , "OpenText" , "OpenRead" , "OpenHandle" , "ReadAllTextAsync" , "ReadAllBytesAsync" , "ReadAllLines" , "ReadAllLinesAsync" , "ReadLines" , "ReadLinesAsync" , "OpenTextAsync"
180
- ] ) and
177
+ exists ( Call c |
178
+ c .getTarget ( )
179
+ .hasFullyQualifiedName ( "System.IO.File" ,
180
+ [
181
+ "ReadAllBytes" , "ReadAllText" , "Open" , "OpenText" , "OpenRead" , "OpenHandle" ,
182
+ "ReadAllTextAsync" , "ReadAllBytesAsync" , "ReadAllLines" , "ReadAllLinesAsync" ,
183
+ "ReadLines" , "ReadLinesAsync" , "OpenTextAsync"
184
+ ] ) and
181
185
c .getAnArgument ( ) = this .getExpr ( )
182
186
)
183
187
}
@@ -207,7 +211,8 @@ class TestClassSanitizer extends Sanitizer {
207
211
*/
208
212
class TestNamespaceSanitizer extends Sanitizer {
209
213
TestNamespaceSanitizer ( ) {
210
- exists ( Namespace n | n .getName ( ) .matches ( [ "Test%" , "%Test" , "%Tests" , "Mock%" , "%Mocks" , "%Mock" , "Fake%" ] ) and
214
+ exists ( Namespace n |
215
+ n .getName ( ) .matches ( [ "Test%" , "%Test" , "%Tests" , "Mock%" , "%Mocks" , "%Mock" , "Fake%" ] ) and
211
216
(
212
217
this .getExpr ( ) = n .getAClass ( ) .getAMethod ( ) .getAChild * ( ) or
213
218
this .getExpr ( ) = n .getAClass ( ) .getAField ( ) .getAChild * ( ) or
@@ -222,13 +227,17 @@ class TestNamespaceSanitizer extends Sanitizer {
222
227
*/
223
228
class DebugSanitizer extends Sanitizer {
224
229
DebugSanitizer ( ) {
225
- exists ( IfDirective if_d , EndifDirective endif_d , Location if_loc , Location endif_loc , Location loc |
230
+ exists (
231
+ IfDirective if_d , EndifDirective endif_d , Location if_loc , Location endif_loc , Location loc
232
+ |
226
233
loc = this .getLocation ( ) and
227
- if_d .getCondition ( ) .toString ( ) = "DEBUG" and if_d .getEndifDirective ( ) = endif_d
228
- and if_d .getLocation ( ) = if_loc and endif_d .getLocation ( ) = endif_loc
229
- and loc .getStartLine ( ) > if_loc .getEndLine ( )
230
- and loc .getEndLine ( ) < endif_loc .getStartLine ( )
231
- and loc .getFile ( ) = if_loc .getFile ( )
234
+ if_d .getCondition ( ) .toString ( ) = "DEBUG" and
235
+ if_d .getEndifDirective ( ) = endif_d and
236
+ if_d .getLocation ( ) = if_loc and
237
+ endif_d .getLocation ( ) = endif_loc and
238
+ loc .getStartLine ( ) > if_loc .getEndLine ( ) and
239
+ loc .getEndLine ( ) < endif_loc .getStartLine ( ) and
240
+ loc .getFile ( ) = if_loc .getFile ( )
232
241
)
233
242
}
234
- }
243
+ }
0 commit comments