File tree Expand file tree Collapse file tree 2 files changed +27
-0
lines changed
lib/semmle/code/csharp/security/dataflow
test/query-tests/Security Features/CWE-601/UrlRedirect Expand file tree Collapse file tree 2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,27 @@ class ContainsUrlSanitizer extends Sanitizer {
161
161
}
162
162
}
163
163
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
+
164
185
/**
165
186
* A call to the getter of the RawUrl property, whose value is considered to be safe for URL
166
187
* redirects.
Original file line number Diff line number Diff line change @@ -20,6 +20,12 @@ public void ProcessRequest(HttpContext ctx)
20
20
// GOOD: the request parameter is validated against set of known fixed strings
21
21
ctx . Response . Redirect ( redirectUrl ) ;
22
22
}
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
+ }
23
29
24
30
}
25
31
}
You can’t perform that action at this time.
0 commit comments