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 @@ -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.
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments