Skip to content

Commit 4dae8d0

Browse files
committed
add host comparisons as a sanitizer for url-redirect
1 parent f4dd3e9 commit 4dae8d0

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,31 @@ class RelativeUrlSanitizer extends Sanitizer {
182182
}
183183
}
184184

185+
/**
186+
* A comparison on the `Host` property of a url, that is a sanitizer for URL redirects.
187+
* E.g. `url.Host == "example.org"`
188+
*/
189+
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+
e = access.getQualifier()
194+
) and
195+
if comparison instanceof EQExpr
196+
then v.(AbstractValues::BooleanValue).getValue() = true
197+
else v.(AbstractValues::BooleanValue).getValue() = false
198+
)
199+
}
200+
201+
/**
202+
* A comparison on the `Host` property of a url, that is a sanitizer for URL redirects.
203+
*/
204+
class HostComparisonSanitizer extends Sanitizer {
205+
HostComparisonSanitizer() {
206+
this = DataFlow::BarrierGuard<isHostComparisonSanitizer/3>::getABarrierNode()
207+
}
208+
}
209+
185210
/**
186211
* A call to the getter of the RawUrl property, whose value is considered to be safe for URL
187212
* redirects.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public void ProcessRequest(HttpContext ctx)
2626
// GOOD: The redirect is to a relative URL
2727
ctx.Response.Redirect(url.ToString());
2828
}
29-
29+
30+
if (url.Host == "example.org") {
31+
// GOOD: The redirect is to a known host
32+
ctx.Response.Redirect(url.ToString());
33+
}
3034
}
3135
}

0 commit comments

Comments
 (0)