Skip to content

Commit c331393

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: update qhelp
1 parent 09bc21d commit c331393

File tree

6 files changed

+25
-132
lines changed

6 files changed

+25
-132
lines changed

java/ql/src/Security/CWE/CWE-552/UnsafeLoadSpringResource.java

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

java/ql/src/Security/CWE/CWE-552/UnsafeResourceGet.java

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

java/ql/src/Security/CWE/CWE-552/UnsafeServletRequestDispatch.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
1-
import java.io.IOException;
2-
import javax.servlet.ServletException;
3-
import javax.servlet.http.HttpServletRequest;
4-
import javax.servlet.http.HttpServletResponse;
5-
import org.springframework.stereotype.Controller;
6-
import org.springframework.web.bind.annotation.GetMapping;
7-
import org.springframework.web.servlet.ModelAndView;
1+
public class UrlForward extends HttpServlet {
2+
private static final String VALID_FORWARD = "https://cwe.mitre.org/data/definitions/552.html";
83

9-
@Controller
10-
public class UrlForward {
4+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
5+
throws ServletException, IOException {
6+
ServletConfig cfg = getServletConfig();
7+
ServletContext sc = cfg.getServletContext();
118

12-
@GetMapping("/bad1")
13-
public ModelAndView bad1(String url) {
14-
return new ModelAndView(url);
15-
}
16-
17-
@GetMapping("/bad2")
18-
public void bad2(String url, HttpServletRequest request, HttpServletResponse response) {
19-
try {
20-
request.getRequestDispatcher("/WEB-INF/jsp/" + url + ".jsp").include(request, response);
21-
} catch (ServletException e) {
22-
e.printStackTrace();
23-
} catch (IOException e) {
24-
e.printStackTrace();
25-
}
26-
}
9+
// BAD: a request parameter is incorporated without validation into a URL forward
10+
sc.getRequestDispatcher(request.getParameter("target")).forward(request, response);
2711

28-
@GetMapping("/good1")
29-
public void good1(String url, HttpServletRequest request, HttpServletResponse response) {
30-
try {
31-
request.getRequestDispatcher("/index.jsp?token=" + url).forward(request, response);
32-
} catch (ServletException e) {
33-
e.printStackTrace();
34-
} catch (IOException e) {
35-
e.printStackTrace();
12+
// GOOD: the request parameter is validated against a known fixed string
13+
if (VALID_FORWARD.equals(request.getParameter("target"))) {
14+
sc.getRequestDispatcher(VALID_FORWARD).forward(request, response);
3615
}
3716
}
3817
}

java/ql/src/Security/CWE/CWE-552/UrlForward.qhelp

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,32 @@
55

66

77
<overview>
8-
<p>Constructing a server-side redirect path with user input could allow an attacker to download application binaries
9-
(including application classes or jar files) or view arbitrary files within protected directories.</p>
8+
<p>Directly incorporating user input into a URL forward request without validating the input
9+
can cause file information disclosure by allowing an attacker to access unauthorized URLs.</p>
1010

1111
</overview>
1212
<recommendation>
1313

14-
<p>Unsanitized user provided data must not be used to construct the path for URL forwarding. In order to prevent
15-
untrusted URL forwarding, it is recommended to avoid concatenating user input directly into the forwarding URL.
16-
Instead, user input should be checked against allowed (e.g., must come within <code>user_content/</code>) or disallowed
17-
(e.g. must not come within <code>/internal</code>) paths, ensuring that neither path traversal using <code>../</code>
18-
or URL encoding are used to evade these checks.
19-
</p>
14+
<p>To guard against untrusted URL forwarding, it is advisable to avoid putting user input
15+
directly into a forwarded URL. Instead, maintain a list of authorized
16+
URLs on the server; then choose from that list based on the user input provided.</p>
2017

2118
</recommendation>
2219
<example>
2320

24-
<p>The following examples show the bad case and the good case respectively.
25-
The <code>bad</code> methods show an HTTP request parameter being used directly in a URL forward
26-
without validating the input, which may cause file leakage. In the <code>good1</code> method,
27-
ordinary forwarding requests are shown, which will not cause file leakage.
21+
<p>The following example shows an HTTP request parameter being used directly in a URL forward
22+
without validating the input, which may cause file information disclosure.
23+
It also shows how to remedy the problem by validating the user input against a known fixed string.
2824
</p>
2925

3026
<sample src="UrlForward.java" />
3127

32-
<p>The following examples show an HTTP request parameter or request path being used directly in a
33-
request dispatcher of Java EE without validating the input, which allows sensitive file exposure
34-
attacks. It also shows how to remedy the problem by validating the user input.
35-
</p>
36-
37-
<sample src="UnsafeServletRequestDispatch.java" />
38-
39-
<p>The following examples show an HTTP request parameter or request path being used directly to
40-
retrieve a resource of a Java EE application without validating the input, which allows sensitive
41-
file exposure attacks. It also shows how to remedy the problem by validating the user input.
42-
</p>
43-
44-
<sample src="UnsafeResourceGet.java" />
45-
46-
<p>The following examples show an HTTP request parameter being used directly to retrieve a resource
47-
of a Java Spring application without validating the input, which allows sensitive file exposure
48-
attacks. It also shows how to remedy the problem by validating the user input.
49-
</p>
50-
51-
<sample src="UnsafeLoadSpringResource.java" />
5228
</example>
5329
<references>
54-
<li>File Disclosure:
55-
<a href="https://vulncat.fortify.com/en/detail?id=desc.dataflow.java.file_disclosure_spring">Unsafe Url Forward</a>.
56-
</li>
57-
<li>Jakarta Javadoc:
58-
<a href="https://jakarta.ee/specifications/webprofile/9/apidocs/jakarta/servlet/servletrequest#getRequestDispatcher-java.lang.String-">Security vulnerability with unsafe usage of RequestDispatcher</a>.
59-
</li>
60-
<li>Micro Focus:
61-
<a href="https://vulncat.fortify.com/en/detail?id=desc.dataflow.java.file_disclosure_j2ee">File Disclosure: J2EE</a>
62-
</li>
63-
<li>CVE-2015-5174:
64-
<a href="https://vuldb.com/?id.81084">Apache Tomcat 6.0/7.0/8.0/9.0 Servletcontext getResource/getResourceAsStream/getResourcePaths Path Traversal</a>
65-
</li>
66-
<li>CVE-2019-3799:
67-
<a href="https://github.com/mpgn/CVE-2019-3799">CVE-2019-3799 - Spring-Cloud-Config-Server Directory Traversal &lt; 2.1.2, 2.0.4, 1.4.6</a>
30+
31+
<li>OWASP:
32+
<a href="https://cheatsheetseries.owasp.org/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.html">Unvalidated Redirects and Forwards Cheat Sheet</a>.
6833
</li>
34+
6935
</references>
7036
</qhelp>

java/ql/src/Security/CWE/CWE-552/UrlForward.ql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/**
22
* @name URL forward from a remote source
33
* @description URL forward based on unvalidated user-input
4-
* may cause file information disclosure or
5-
* redirection to malicious web sites.
4+
* may cause file information disclosure.
65
* @kind path-problem
76
* @problem.severity error
8-
* @security-severity 6.1
7+
* @security-severity 7.5
98
* @precision high
109
* @id java/unvalidated-url-forward
1110
* @tags security
1211
* external/cwe/cwe-552
13-
* external/cwe/cwe-601
1412
*/
1513

1614
import java

0 commit comments

Comments
 (0)