Skip to content

Commit a26c5d1

Browse files
committed
C#: Streamline the implementation for ASP.NET Core tainted members.
1 parent cb7763a commit a26c5d1

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/flowsources/Remote.qll

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,33 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa
115115
override string getSourceType() { result = "ASP.NET web service input" }
116116
}
117117

118+
private class CandidateMembersToTaint extends Member {
119+
CandidateMembersToTaint() {
120+
this.isPublic() and
121+
not this.isStatic() and
122+
(
123+
this =
124+
any(Property p |
125+
p.isAutoImplemented() and
126+
p.getGetter().isPublic() and
127+
p.getSetter().isPublic()
128+
)
129+
or
130+
this = any(Field f | f.isPublic())
131+
)
132+
}
133+
}
134+
118135
/**
119136
* Taint members (transitively) on types used in
120137
* 1. Action method parameters.
121138
* 2. WebMethod parameters.
122139
*
123140
* Note, that this also impacts uses of such types in other contexts.
124141
*/
125-
private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember {
142+
private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember,
143+
CandidateMembersToTaint
144+
{
126145
AspNetRemoteFlowSourceMember() {
127146
exists(Type t, Type t0 | t = this.getDeclaringType() |
128147
(t = t0 or t = t0.(ArrayType).getElementType()) and
@@ -133,18 +152,6 @@ private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember
133152
or
134153
t0 = any(AspNetServiceRemoteFlowSource source).getType()
135154
)
136-
) and
137-
this.isPublic() and
138-
not this.isStatic() and
139-
(
140-
this =
141-
any(Property p |
142-
p.isAutoImplemented() and
143-
p.getGetter().isPublic() and
144-
p.getSetter().isPublic()
145-
)
146-
or
147-
this = any(Field f | f.isPublic())
148155
)
149156
}
150157
}
@@ -253,14 +260,18 @@ class AspNetCoreRoutingMethodParameter extends AspNetCoreRemoteFlowSource, DataF
253260
* Flow is defined from any ASP.NET Core remote source object to any of its member
254261
* properties.
255262
*/
256-
private class AspNetCoreRemoteFlowSourceMember extends TaintTracking::TaintedMember, Property {
263+
private class AspNetCoreRemoteFlowSourceMember extends TaintTracking::TaintedMember,
264+
CandidateMembersToTaint
265+
{
257266
AspNetCoreRemoteFlowSourceMember() {
258-
this.getDeclaringType() = any(AspNetCoreRemoteFlowSource source).getType() and
259-
this.isPublic() and
260-
not this.isStatic() and
261-
this.isAutoImplemented() and
262-
this.getGetter().isPublic() and
263-
this.getSetter().isPublic()
267+
exists(Type t, Type t0 | t = this.getDeclaringType() |
268+
(t = t0 or t = t0.(ArrayType).getElementType()) and
269+
(
270+
t0 = any(AspNetCoreRemoteFlowSourceMember m).getType()
271+
or
272+
t0 = any(AspNetCoreRemoteFlowSource m).getType()
273+
)
274+
)
264275
}
265276
}
266277

csharp/ql/test/library-tests/dataflow/flowsources/aspremote/AspRemoteFlowSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Testing
88
public class ViewModel
99
{
1010
public string RequestId { get; set; } // Considered tainted.
11-
public object RequestIdField; // Not considered tainted as it is a field.
11+
public object RequestIdField; // Considered tainted.
1212
public string RequestIdOnlyGet { get; } // Not considered tainted as there is no setter.
1313
public string RequestIdPrivateSet { get; private set; } // Not considered tainted as it has a private setter.
1414
public static object RequestIdStatic { get; set; } // Not considered tainted as it is static.

csharp/ql/test/library-tests/dataflow/flowsources/aspremote/aspRemoteFlowSource.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
remoteFlowSourceMembers
22
| AspRemoteFlowSource.cs:10:23:10:31 | RequestId |
3+
| AspRemoteFlowSource.cs:11:23:11:36 | RequestIdField |
34
| AspRemoteFlowSource.cs:28:23:28:29 | Tainted |
45
remoteFlowSources
56
| AspRemoteFlowSource.cs:20:42:20:50 | viewModel |

0 commit comments

Comments
 (0)