Skip to content

Commit f4dd3e9

Browse files
committed
treat relative URLs as safe for url-redirects
1 parent 3f8de82 commit f4dd3e9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ class ContainsUrlSanitizer extends Sanitizer {
161161
}
162162
}
163163

164+
/**
165+
* A check that the URL is relative, and therefore safe for URL redirects.
166+
*/
167+
private predicate isRelativeUrlSanitizer(Guard guard, Expr e, AbstractValue v) {
168+
exists(PropertyAccess access | access = guard |
169+
access.getProperty().getName() = "IsAbsoluteUri" and
170+
// TOOD: type = URL?
171+
e = access.getQualifier() and
172+
v.(AbstractValues::BooleanValue).getValue() = false
173+
)
174+
}
175+
176+
/**
177+
* A check that the URL is relative, and therefore safe for URL redirects.
178+
*/
179+
class RelativeUrlSanitizer extends Sanitizer {
180+
RelativeUrlSanitizer() {
181+
this = DataFlow::BarrierGuard<isRelativeUrlSanitizer/3>::getABarrierNode()
182+
}
183+
}
184+
164185
/**
165186
* A call to the getter of the RawUrl property, whose value is considered to be safe for URL
166187
* 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
@@ -20,6 +20,12 @@ public void ProcessRequest(HttpContext ctx)
2020
// GOOD: the request parameter is validated against set of known fixed strings
2121
ctx.Response.Redirect(redirectUrl);
2222
}
23+
24+
var url = new Uri(redirectUrl, UriKind.RelativeOrAbsolute);
25+
if (!url.IsAbsoluteUri) {
26+
// GOOD: The redirect is to a relative URL
27+
ctx.Response.Redirect(url.ToString());
28+
}
2329

2430
}
2531
}

0 commit comments

Comments
 (0)