Skip to content

Commit 88b52ad

Browse files
committed
C#: Streamline the implementation for ASP.NET Core tainted members.
1 parent 8721d35 commit 88b52ad

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
@@ -116,6 +116,23 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa
116116
override string getSourceType() { result = "ASP.NET web service input" }
117117
}
118118

119+
private class CandidateMembersToTaint extends Member {
120+
CandidateMembersToTaint() {
121+
this.isPublic() and
122+
not this.isStatic() and
123+
(
124+
this =
125+
any(Property p |
126+
p.isAutoImplemented() and
127+
p.getGetter().isPublic() and
128+
p.getSetter().isPublic()
129+
)
130+
or
131+
this = any(Field f | f.isPublic())
132+
)
133+
}
134+
}
135+
119136
/**
120137
* Taint members (transitively) on types used in
121138
* 1. Action method parameters.
@@ -124,7 +141,9 @@ class AspNetServiceRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::Pa
124141
* Note, as this also impact uses of such types in other contexts, we only do this for
125142
* user-defined types.
126143
*/
127-
private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember {
144+
private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember,
145+
CandidateMembersToTaint
146+
{
128147
AspNetRemoteFlowSourceMember() {
129148
exists(Type t, Type t0 | t = this.getDeclaringType() and t.fromSource() |
130149
(t = t0 or t = t0.(ArrayType).getElementType()) and
@@ -135,18 +154,6 @@ private class AspNetRemoteFlowSourceMember extends TaintTracking::TaintedMember
135154
or
136155
t0 = any(AspNetServiceRemoteFlowSource source).getType()
137156
)
138-
) and
139-
this.isPublic() and
140-
not this.isStatic() and
141-
(
142-
this =
143-
any(Property p |
144-
p.isAutoImplemented() and
145-
p.getGetter().isPublic() and
146-
p.getSetter().isPublic()
147-
)
148-
or
149-
this = any(Field f | f.isPublic())
150157
)
151158
}
152159
}
@@ -255,14 +262,18 @@ class AspNetCoreRoutingMethodParameter extends AspNetCoreRemoteFlowSource, DataF
255262
* Flow is defined from any ASP.NET Core remote source object to any of its member
256263
* properties.
257264
*/
258-
private class AspNetCoreRemoteFlowSourceMember extends TaintTracking::TaintedMember, Property {
265+
private class AspNetCoreRemoteFlowSourceMember extends TaintTracking::TaintedMember,
266+
CandidateMembersToTaint
267+
{
259268
AspNetCoreRemoteFlowSourceMember() {
260-
this.getDeclaringType() = any(AspNetCoreRemoteFlowSource source).getType() and
261-
this.isPublic() and
262-
not this.isStatic() and
263-
this.isAutoImplemented() and
264-
this.getGetter().isPublic() and
265-
this.getSetter().isPublic()
269+
exists(Type t, Type t0 | t = this.getDeclaringType() and t.fromSource() |
270+
(t = t0 or t = t0.(ArrayType).getElementType()) and
271+
(
272+
t0 = any(AspNetCoreRemoteFlowSourceMember m).getType()
273+
or
274+
t0 = any(AspNetCoreRemoteFlowSource m).getType()
275+
)
276+
)
266277
}
267278
}
268279

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)