Skip to content

Commit a2bd45d

Browse files
committed
apply suggestions from code review
1 parent d31bfc0 commit a2bd45d

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

csharp/ql/lib/semmle/code/csharp/security/dataflow/UrlRedirectQuery.qll

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,24 @@ class LocalUrlSanitizer extends Sanitizer {
140140
}
141141

142142
/**
143-
* A argument to a call to `List.Contains()` that is a sanitizer for URL redirects.
143+
* An argument to a call to `List.Contains()` that is a sanitizer for URL redirects.
144144
*/
145145
private predicate isContainsUrlSanitizer(Guard guard, Expr e, AbstractValue v) {
146-
exists(MethodCall method | method = guard |
147-
exists(Method m | m = method.getTarget() |
148-
m.hasName("Contains") and
149-
e = method.getArgument(0)
150-
) and
151-
v.(AbstractValues::BooleanValue).getValue() = true
152-
)
146+
guard =
147+
any(MethodCall method |
148+
exists(Method m | m = method.getTarget() |
149+
m.hasName("Contains") and
150+
e = method.getArgument(0)
151+
) and
152+
v.(AbstractValues::BooleanValue).getValue() = true
153+
)
153154
}
154155

155156
/**
156-
* A URL argument to a call to `List.Contains()` that is a sanitizer for URL redirects.
157+
* An URL argument to a call to `.Contains()` that is a sanitizer for URL redirects.
158+
*
159+
* This `Contains` method is usually called on a list, but the sanitizer matches any call to a method
160+
* called `Contains`, so other methods with the same name will also be considered sanitizers.
157161
*/
158162
class ContainsUrlSanitizer extends Sanitizer {
159163
ContainsUrlSanitizer() {
@@ -165,12 +169,12 @@ class ContainsUrlSanitizer extends Sanitizer {
165169
* A check that the URL is relative, and therefore safe for URL redirects.
166170
*/
167171
private predicate isRelativeUrlSanitizer(Guard guard, Expr e, AbstractValue v) {
168-
exists(PropertyAccess access | access = guard |
169-
access.getProperty().getName() = "IsAbsoluteUri" and
170-
access.getQualifier().getType().getFullyQualifiedName() = "System.Uri" and
171-
e = access.getQualifier() and
172-
v.(AbstractValues::BooleanValue).getValue() = false
173-
)
172+
guard =
173+
any(PropertyAccess access |
174+
access.getProperty().hasFullyQualifiedName("System", "Uri", "IsAbsoluteUri") and
175+
e = access.getQualifier() and
176+
v.(AbstractValues::BooleanValue).getValue() = false
177+
)
174178
}
175179

176180
/**
@@ -187,16 +191,16 @@ class RelativeUrlSanitizer extends Sanitizer {
187191
* E.g. `url.Host == "example.org"`
188192
*/
189193
private predicate isHostComparisonSanitizer(Guard guard, Expr e, AbstractValue v) {
190-
exists(EqualityOperation comparison | comparison = guard |
191-
exists(PropertyAccess access | access = comparison.getAnOperand() |
192-
access.getProperty().getName() = "Host" and
193-
access.getQualifier().getType().getFullyQualifiedName() = "System.Uri" and
194-
e = access.getQualifier()
195-
) and
196-
if comparison instanceof EQExpr
197-
then v.(AbstractValues::BooleanValue).getValue() = true
198-
else v.(AbstractValues::BooleanValue).getValue() = false
199-
)
194+
guard =
195+
any(EqualityOperation comparison |
196+
exists(PropertyAccess access | access = comparison.getAnOperand() |
197+
access.getProperty().hasFullyQualifiedName("System", "Uri", "Host") and
198+
e = access.getQualifier()
199+
) and
200+
if comparison instanceof EQExpr
201+
then v.(AbstractValues::BooleanValue).getValue() = true
202+
else v.(AbstractValues::BooleanValue).getValue() = false
203+
)
200204
}
201205

202206
/**

csharp/ql/test/query-tests/Security Features/CWE-601/UrlRedirect/UrlRedirect2.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66

77
public class UrlRedirectHandler2 : IHttpHandler
88
{
9-
private const String VALID_REDIRECT = "http://cwe.mitre.org/data/definitions/601.html";
9+
private List<string> VALID_REDIRECTS = new List<string>{ "http://cwe.mitre.org/data/definitions/601.html", "http://cwe.mitre.org/data/definitions/79.html" };
1010

1111
public void ProcessRequest(HttpContext ctx)
1212
{
1313
// BAD: a request parameter is incorporated without validation into a URL redirect
1414
ctx.Response.Redirect(ctx.Request.QueryString["page"]);
1515

16-
List<string> VALID_REDIRECTS = new List<string>{ "http://cwe.mitre.org/data/definitions/601.html", "http://cwe.mitre.org/data/definitions/79.html" };
1716
var redirectUrl = ctx.Request.QueryString["page"];
1817
if (VALID_REDIRECTS.Contains(redirectUrl))
1918
{

0 commit comments

Comments
 (0)