Skip to content

Commit ae96b34

Browse files
committed
C#: Address review comments.
1 parent 13f0a40 commit ae96b34

File tree

8 files changed

+122
-201
lines changed

8 files changed

+122
-201
lines changed

csharp/ql/src/semmle/code/csharp/dataflow/LibraryTypeDataFlow.qll

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,3 +1746,28 @@ class SystemNetWebUtilityFlow extends LibraryTypeDataFlow, SystemNetWebUtility {
17461746
preservesValue = false
17471747
}
17481748
}
1749+
1750+
/**
1751+
* The `StringValues` class used in many .NET Core libraries. Requires special `LibraryTypeDataFlow` flow.
1752+
*/
1753+
class StringValues extends Struct {
1754+
StringValues() { this.hasQualifiedName("Microsoft.Extensions.Primitives", "StringValues") }
1755+
}
1756+
1757+
/**
1758+
* Custom flow through StringValues.StringValues library class
1759+
*/
1760+
class StringValuesFlow extends LibraryTypeDataFlow, StringValues {
1761+
override predicate callableFlow(
1762+
CallableFlowSource source, CallableFlowSink sink, SourceDeclarationCallable c,
1763+
boolean preservesValue
1764+
) {
1765+
c = any(Callable ca | this = ca.getDeclaringType()) and
1766+
(
1767+
source = any(CallableFlowSourceArg a) or
1768+
source = any(CallableFlowSourceQualifier q)
1769+
) and
1770+
sink = any(CallableFlowSinkReturn r) and
1771+
preservesValue = false
1772+
}
1773+
}

csharp/ql/src/semmle/code/csharp/dataflow/flowsources/Remote.qll

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ private import semmle.code.csharp.frameworks.system.web.Services
1111
private import semmle.code.csharp.frameworks.system.web.ui.WebControls
1212
private import semmle.code.csharp.frameworks.WCF
1313
private import semmle.code.csharp.frameworks.microsoft.Owin
14-
private import semmle.code.csharp.frameworks.microsoft.Primitives
1514
private import semmle.code.csharp.frameworks.microsoft.AspNetCore
1615

17-
1816
/** A data flow source of remote user input. */
1917
abstract class RemoteFlowSource extends DataFlow::Node {
2018
/** Gets a string that describes the type of this remote flow source. */
@@ -31,9 +29,8 @@ class AspNetQueryStringMember extends Member {
3129
t instanceof SystemWebHttpRequestClass or
3230
t instanceof SystemNetHttpListenerRequestClass or
3331
t instanceof SystemWebHttpRequestBaseClass
34-
|
35-
this = t.getProperty(getHttpRequestFlowPropertyNames())
36-
or
32+
|
33+
this = t.getProperty(getHttpRequestFlowPropertyNames()) or
3734
this.(Field).getType() = t or
3835
this.(Property).getType() = t or
3936
this.(Callable).getReturnType() = t
@@ -64,47 +61,43 @@ class AspNetQueryStringRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow
6461
t instanceof SystemWebHttpRequestClass or
6562
t instanceof SystemNetHttpListenerRequestClass or
6663
t instanceof SystemWebHttpRequestBaseClass
67-
|
64+
|
6865
// A request object can be indexed, so taint the object as well
6966
this.getExpr().getType() = t
7067
)
7168
or
7269
this.getExpr() = any(AspNetQueryStringMember m).getAnAccess()
7370
}
7471

75-
override
76-
string getSourceType() { result = "ASP.NET query string" }
72+
override string getSourceType() { result = "ASP.NET query string" }
7773
}
7874

7975
/** A data flow source of remote user input (ASP.NET unvalidated request data). */
80-
class AspNetUnvalidatedQueryStringRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::ExprNode {
76+
class AspNetUnvalidatedQueryStringRemoteFlowSource extends AspNetRemoteFlowSource,
77+
DataFlow::ExprNode {
8178
AspNetUnvalidatedQueryStringRemoteFlowSource() {
8279
this.getExpr() = any(SystemWebUnvalidatedRequestValues c).getAProperty().getGetter().getACall() or
83-
this.getExpr() = any(SystemWebUnvalidatedRequestValuesBase c).getAProperty().getGetter().getACall()
80+
this.getExpr() = any(SystemWebUnvalidatedRequestValuesBase c)
81+
.getAProperty()
82+
.getGetter()
83+
.getACall()
8484
}
8585

86-
override
87-
string getSourceType() { result = "ASP.NET unvalidated request data" }
86+
override string getSourceType() { result = "ASP.NET unvalidated request data" }
8887
}
8988

9089
/** A data flow source of remote user input (ASP.NET user input). */
9190
class AspNetUserInputRemoteFlowSource extends AspNetRemoteFlowSource, DataFlow::ExprNode {
92-
AspNetUserInputRemoteFlowSource() {
93-
getType() instanceof SystemWebUIWebControlsTextBoxClass
94-
}
91+
AspNetUserInputRemoteFlowSource() { getType() instanceof SystemWebUIWebControlsTextBoxClass }
9592

96-
override
97-
string getSourceType() { result = "ASP.NET user input" }
93+
override string getSourceType() { result = "ASP.NET user input" }
9894
}
9995

10096
/** A data flow source of remote user input (WCF based web service). */
10197
class WcfRemoteFlowSource extends RemoteFlowSource, DataFlow::ParameterNode {
102-
WcfRemoteFlowSource() {
103-
exists(OperationMethod om | om.getAParameter() = this.getParameter())
104-
}
98+
WcfRemoteFlowSource() { exists(OperationMethod om | om.getAParameter() = this.getParameter()) }
10599

106-
override
107-
string getSourceType() { result = "web service input" }
100+
override string getSourceType() { result = "web service input" }
108101
}
109102

110103
/** A data flow source of remote user input (ASP.NET web service). */
@@ -116,8 +109,7 @@ class AspNetServiceRemoteFlowSource extends RemoteFlowSource, DataFlow::Paramete
116109
)
117110
}
118111

119-
override
120-
string getSourceType() { result = "ASP.NET web service input" }
112+
override string getSourceType() { result = "ASP.NET web service input" }
121113
}
122114

123115
/** A data flow source of remote user input (ASP.NET request message). */
@@ -126,8 +118,7 @@ class SystemNetHttpRequestMessageRemoteFlowSource extends RemoteFlowSource, Data
126118
getType() instanceof SystemWebHttpRequestMessageClass
127119
}
128120

129-
override
130-
string getSourceType() { result = "ASP.NET request message" }
121+
override string getSourceType() { result = "ASP.NET request message" }
131122
}
132123

133124
/**
@@ -139,15 +130,15 @@ class MicrosoftOwinStringFlowSource extends RemoteFlowSource, DataFlow::ExprNode
139130
this.getExpr() = any(MicrosoftOwinString owinString).getValueProperty().getGetter().getACall()
140131
}
141132

142-
override
143-
string getSourceType() { result = "Microsoft Owin request or query string" }
133+
override string getSourceType() { result = "Microsoft Owin request or query string" }
144134
}
145135

146136
/** A data flow source of remote user input (`Microsoft Owin IOwinRequest`). */
147137
class MicrosoftOwinRequestRemoteFlowSource extends RemoteFlowSource, DataFlow::ExprNode {
148138
MicrosoftOwinRequestRemoteFlowSource() {
149139
exists(Property p, MicrosoftOwinIOwinRequestClass owinRequest |
150-
this.getExpr() = p.getGetter().getACall() |
140+
this.getExpr() = p.getGetter().getACall()
141+
|
151142
p = owinRequest.getAcceptProperty() or
152143
p = owinRequest.getBodyProperty() or
153144
p = owinRequest.getCacheControlProperty() or
@@ -168,66 +159,62 @@ class MicrosoftOwinRequestRemoteFlowSource extends RemoteFlowSource, DataFlow::E
168159
)
169160
}
170161

171-
override
172-
string getSourceType() { result = "Microsoft Owin request" }
162+
override string getSourceType() { result = "Microsoft Owin request" }
173163
}
174164

175165
/** A parameter to an Mvc controller action method, viewed as a source of remote user input. */
176166
class ActionMethodParameter extends RemoteFlowSource, DataFlow::ParameterNode {
177167
ActionMethodParameter() {
178168
exists(Parameter p |
179169
p = this.getParameter() and
180-
p.fromSource() |
181-
p = any(Controller c).getAnActionMethod().getAParameter()
182-
or
170+
p.fromSource()
171+
|
172+
p = any(Controller c).getAnActionMethod().getAParameter() or
183173
p = any(ApiController c).getAnActionMethod().getAParameter()
184174
)
185175
}
186176

187-
override
188-
string getSourceType() { result = "ASP.NET MVC action method parameter" }
177+
override string getSourceType() { result = "ASP.NET MVC action method parameter" }
189178
}
190179

191180
/** A data flow source of remote user input (ASP.NET Core). */
192181
abstract class AspNetCoreRemoteFlowSource extends RemoteFlowSource { }
193182

194-
195183
/** A data flow source of remote user input (ASP.NET query collection). */
196184
class AspNetCoreQueryRemoteFlowSource extends AspNetCoreRemoteFlowSource, DataFlow::ExprNode {
197185
AspNetCoreQueryRemoteFlowSource() {
198186
exists(ValueOrRefType t |
199-
t instanceof MicrosoftAspNetCoreHttpHttpRequest
200-
or
201-
t instanceof MicrosoftAspNetCoreHttpQueryCollection
202-
or
187+
t instanceof MicrosoftAspNetCoreHttpHttpRequest or
188+
t instanceof MicrosoftAspNetCoreHttpQueryCollection or
203189
t instanceof MicrosoftAspNetCoreHttpQueryString
204-
|
205-
this.getExpr().(Call).getTarget().getDeclaringType() = t
206-
or
190+
|
191+
this.getExpr().(Call).getTarget().getDeclaringType() = t or
207192
this.asExpr().(Access).getTarget().getDeclaringType() = t
208193
)
209194
or
210195
exists(Call c |
211-
c.getTarget().getDeclaringType().hasQualifiedName("Microsoft.AspNetCore.Http", "IQueryCollection") and
196+
c
197+
.getTarget()
198+
.getDeclaringType()
199+
.hasQualifiedName("Microsoft.AspNetCore.Http", "IQueryCollection") and
212200
c.getTarget().getName() = "TryGetValue" and
213201
this.asExpr() = c.getArgumentForName("value")
214202
)
215203
}
216204

217-
override
218-
string getSourceType() { result = "ASP.NET Core query string" }
205+
override string getSourceType() { result = "ASP.NET Core query string" }
219206
}
220207

221208
/** A parameter to a `Mvc` controller action method, viewed as a source of remote user input. */
222209
class AspNetCoreActionMethodParameter extends RemoteFlowSource, DataFlow::ParameterNode {
223210
AspNetCoreActionMethodParameter() {
224211
exists(Parameter p |
225212
p = this.getParameter() and
226-
p.fromSource() |
213+
p.fromSource()
214+
|
227215
p = any(MicrosoftAspNetCoreMvcController c).getAnActionMethod().getAParameter()
228216
)
229217
}
230218

231-
override
232-
string getSourceType() { result = "ASP.NET Core MVC action method parameter" }
219+
override string getSourceType() { result = "ASP.NET Core MVC action method parameter" }
233220
}

0 commit comments

Comments
 (0)