File tree Expand file tree Collapse file tree
lib/semmle/code/csharp/security/dataflow
test/query-tests/Security Features/CWE-601/UrlRedirect Expand file tree Collapse file tree Original file line number Diff line number Diff 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.
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments