Skip to content

Commit a3da6c8

Browse files
authored
Merge pull request #15895 from erik-krogh/url-java-qhelp
Java: update the url-redirection in the same style as the C# qhelp
2 parents 44ab36f + ef8368c commit a3da6c8

File tree

6 files changed

+79
-21
lines changed

6 files changed

+79
-21
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ which is harmless but perhaps not intended. You can substitute your own domain (
5757

5858
<li>
5959
OWASP:
60-
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">XSS
60+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">
6161
Unvalidated Redirects and Forwards Cheat Sheet</a>.
6262
</li>
6363
<li>

java/ql/src/Security/CWE/CWE-601/UrlRedirect.java

-14
This file was deleted.

java/ql/src/Security/CWE/CWE-601/UrlRedirect.qhelp

+38-6
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,53 @@ 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.java"/>
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.java" />
39+
<sample src="examples/UrlRedirectGood.java"/>
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.java"/>
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>
31-
<references>
3255

56+
<references>
3357

58+
<li>
59+
OWASP:
60+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">
61+
Unvalidated Redirects and Forwards Cheat Sheet</a>.
62+
</li>
63+
<li>
64+
Microsoft Docs: <a href="https://docs.microsoft.com/en-us/aspnet/mvc/overview/security/preventing-open-redirection-attacks">Preventing Open Redirection Attacks (C#)</a>.
65+
</li>
3466

3567
</references>
3668
</qhelp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class UrlRedirect extends HttpServlet {
2+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
3+
// BAD: a request parameter is incorporated without validation into a URL redirect
4+
response.sendRedirect(request.getParameter("target"));
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class UrlRedirect extends HttpServlet {
2+
private static final List<String> VALID_REDIRECTS = Arrays.asList(
3+
"http://cwe.mitre.org/data/definitions/601.html",
4+
"http://cwe.mitre.org/data/definitions/79.html"
5+
);
6+
7+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
8+
// GOOD: the request parameter is validated against a known list of strings
9+
String target = request.getParameter("target");
10+
if (VALID_REDIRECTS.contains(target)) {
11+
response.sendRedirect(target);
12+
} else {
13+
response.sendRedirect("/error.html");
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class UrlRedirect extends HttpServlet {
2+
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
3+
try {
4+
String urlString = request.getParameter("page");
5+
URI url = new URI(urlString);
6+
7+
if (!url.isAbsolute()) {
8+
response.sendRedirect(url.toString()); // GOOD: The redirect is to a relative URL
9+
}
10+
11+
if ("example.org".equals(url.getHost())) {
12+
response.sendRedirect(url.toString()); // GOOD: The redirect is to a known host
13+
}
14+
} catch (URISyntaxException e) {
15+
// handle exception
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)