-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15596 from erik-krogh/url-san
C#: Add a few more sanitizers to `cs/web/unvalidated-url-redirection`
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
category: minorAnalysis | ||
--- | ||
* Added sanitizers for relative URLs, `List.Contains()`, and checking the `.Host` property on an URI to the `cs/web/unvalidated-url-redirection` query. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
csharp/ql/test/query-tests/Security Features/CWE-601/UrlRedirect/UrlRedirect2.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using System; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using System.Web.WebPages; | ||
using System.Collections.Generic; | ||
|
||
public class UrlRedirectHandler2 : IHttpHandler | ||
{ | ||
private List<string> VALID_REDIRECTS = new List<string>{ "http://cwe.mitre.org/data/definitions/601.html", "http://cwe.mitre.org/data/definitions/79.html" }; | ||
|
||
public void ProcessRequest(HttpContext ctx) | ||
{ | ||
// BAD: a request parameter is incorporated without validation into a URL redirect | ||
ctx.Response.Redirect(ctx.Request.QueryString["page"]); | ||
|
||
var redirectUrl = ctx.Request.QueryString["page"]; | ||
if (VALID_REDIRECTS.Contains(redirectUrl)) | ||
{ | ||
// GOOD: the request parameter is validated against set of known fixed strings | ||
ctx.Response.Redirect(redirectUrl); | ||
} | ||
|
||
var url = new Uri(redirectUrl, UriKind.RelativeOrAbsolute); | ||
if (!url.IsAbsoluteUri) { | ||
// GOOD: The redirect is to a relative URL | ||
ctx.Response.Redirect(url.ToString()); | ||
} | ||
|
||
if (url.Host == "example.org") { | ||
// GOOD: The redirect is to a known host | ||
ctx.Response.Redirect(url.ToString()); | ||
} | ||
} | ||
} |