Skip to content

Commit 3f8de82

Browse files
committed
add a sanitizer for List.Contains() in url-redirect
1 parent 5979280 commit 3f8de82

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,28 @@ class LocalUrlSanitizer extends Sanitizer {
139139
LocalUrlSanitizer() { this = DataFlow::BarrierGuard<isLocalUrlSanitizer/3>::getABarrierNode() }
140140
}
141141

142+
/**
143+
* A argument to a call to `List.Contains()` that is a sanitizer for URL redirects.
144+
*/
145+
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+
)
153+
}
154+
155+
/**
156+
* A URL argument to a call to `List.Contains()` that is a sanitizer for URL redirects.
157+
*/
158+
class ContainsUrlSanitizer extends Sanitizer {
159+
ContainsUrlSanitizer() {
160+
this = DataFlow::BarrierGuard<isContainsUrlSanitizer/3>::getABarrierNode()
161+
}
162+
}
163+
142164
/**
143165
* A call to the getter of the RawUrl property, whose value is considered to be safe for URL
144166
* redirects.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public void ProcessRequest(HttpContext ctx)
1414
ctx.Response.Redirect(ctx.Request.QueryString["page"]);
1515

1616
List<string> VALID_REDIRECTS = new List<string>{ "http://cwe.mitre.org/data/definitions/601.html", "http://cwe.mitre.org/data/definitions/79.html" };
17+
var redirectUrl = ctx.Request.QueryString["page"];
18+
if (VALID_REDIRECTS.Contains(redirectUrl))
19+
{
20+
// GOOD: the request parameter is validated against set of known fixed strings
21+
ctx.Response.Redirect(redirectUrl);
22+
}
1723

1824
}
1925
}

0 commit comments

Comments
 (0)