Skip to content

Commit 037e64a

Browse files
authored
Merge pull request #15623 from erik-krogh/cs-url
C#: update the QHelp for `cs/web/unvalidated-url-redirection`
2 parents d461571 + a5eb2dd commit 037e64a

File tree

5 files changed

+80
-24
lines changed

5 files changed

+80
-24
lines changed

csharp/ql/src/Security Features/CWE-601/UrlRedirect.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

csharp/ql/src/Security Features/CWE-601/UrlRedirect.qhelp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,43 @@ controlled by the attacker.</p>
1616
<p>To guard against untrusted URL redirection, it is advisable to avoid putting user input
1717
directly into a redirect URL. Instead, maintain a list of authorized
1818
redirects on the server; then choose from that list based on the user input provided.</p>
19-
19+
<p>
20+
If this is not possible, then the user input should be validated in some other way,
21+
for example, by verifying that the target URL is on the same host as the current page.
22+
</p>
2023
</recommendation>
24+
25+
2126
<example>
27+
<p>
28+
The following example shows an HTTP request parameter being used directly in a URL redirect
29+
without validating the input, which facilitates phishing attacks:
30+
</p>
31+
32+
<sample src="examples/UrlRedirect.cs"/>
2233

23-
<p>The following example shows an HTTP request parameter being used directly in a URL redirect
24-
without validating the input, which facilitates phishing attacks.
25-
It also shows how to remedy the problem by validating the user input against a known fixed string.
34+
<p>
35+
One way to remedy the problem is to validate the user input against a known fixed string
36+
before doing the redirection:
2637
</p>
2738

28-
<sample src="UrlRedirect.cs" />
39+
<sample src="examples/UrlRedirectGood.cs"/>
40+
41+
<p>
42+
Alternatively, we can check that the target URL does not redirect to a different host
43+
by checking that the URL is either relative or on a known good host:
44+
</p>
45+
46+
<sample src="examples/UrlRedirectGoodDomain.cs"/>
47+
48+
<p>
49+
Note that as written, the above code will allow redirects to URLs on <code>example.com</code>,
50+
which is harmless but perhaps not intended. You can substitute your own domain (if known) for
51+
<code>example.com</code> to prevent this.
52+
</p>
2953

3054
</example>
55+
3156
<references>
3257

3358
<li>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Web;
3+
4+
public class UnvalidatedUrlHandler : IHttpHandler
5+
{
6+
public void ProcessRequest(HttpContext ctx)
7+
{
8+
// BAD: a request parameter is incorporated without validation into a URL redirect
9+
ctx.Response.Redirect(ctx.Request.QueryString["page"]);
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Web;
3+
using System.Collections.Generic;
4+
5+
public class UnvalidatedUrlHandler : IHttpHandler
6+
{
7+
private List<string> VALID_REDIRECTS = new List<string>{ "http://cwe.mitre.org/data/definitions/601.html", "http://cwe.mitre.org/data/definitions/79.html" };
8+
9+
public void ProcessRequest(HttpContext ctx)
10+
{
11+
if (VALID_REDIRECTS.Contains(ctx.Request.QueryString["page"]))
12+
{
13+
// GOOD: the request parameter is validated against a known list of strings
14+
ctx.Response.Redirect(ctx.Request.QueryString["page"]);
15+
}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Web;
3+
4+
public class UnvalidatedUrlHandler : IHttpHandler
5+
{
6+
public void ProcessRequest(HttpContext ctx)
7+
{
8+
var urlString = ctx.Request.QueryString["page"];
9+
var url = new Uri(urlString, UriKind.RelativeOrAbsolute);
10+
11+
var url = new Uri(redirectUrl, UriKind.RelativeOrAbsolute);
12+
if (!url.IsAbsoluteUri) {
13+
// GOOD: The redirect is to a relative URL
14+
ctx.Response.Redirect(url.ToString());
15+
}
16+
17+
if (url.Host == "example.org") {
18+
// GOOD: The redirect is to a known host
19+
ctx.Response.Redirect(url.ToString());
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)