Skip to content

Commit 0d38a96

Browse files
Jami CogswellJami Cogswell
authored andcommitted
Java: copy files from experimental
1 parent 67b3670 commit 0d38a96

17 files changed

+1320
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/java-all
4+
extensible: sourceModel
5+
data:
6+
- ["jakarta.servlet.http", "HttpServletRequest", True, "getServletPath", "", "", "ReturnValue", "remote", "manual"]
7+
- ["javax.servlet.http", "HttpServletRequest", True, "getServletPath", "", "", "ReturnValue", "remote", "manual"]
8+
9+
# # ! below added by me when debugging CVEs:
10+
# - ["org.springframework.cloud.config.server.resource", "ResourceController", True, "retrieve", "(String,String,String,ServletWebRequest,boolean)", "", "Parameter[3]", "remote", "manual"]
11+
# - ["org.springframework.web.context.request", "ServletWebRequest", True, "getContextPath", "()", "", "ReturnValue", "remote", "manual"]
12+
13+
- addsTo:
14+
pack: codeql/java-all
15+
extensible: sinkModel
16+
data:
17+
- ["java.util.concurrent", "TimeUnit", True, "sleep", "", "", "Argument[0]", "thread-pause", "manual"] # ! this seems like a typo; doesn't look like it's used in the query at all
18+
19+
- ["org.springframework.core.io", "ClassPathResource", True, "getFilename", "", "", "Argument[this]", "get-resource", "manual"] # ! Note: `ClassPathResource` implements `Resource`, so it might make more sense to model some of these as `Resource` with subtype True.
20+
- ["org.springframework.core.io", "ClassPathResource", True, "getPath", "", "", "Argument[this]", "get-resource", "manual"]
21+
- ["org.springframework.core.io", "ClassPathResource", True, "getURL", "", "", "Argument[this]", "get-resource", "manual"]
22+
- ["org.springframework.core.io", "ClassPathResource", True, "resolveURL", "", "", "Argument[this]", "get-resource", "manual"]
23+
# # ! below added by me when debugging CVEs:
24+
# - ["org.springframework.cloud.config.server.resource", "ResourceController", True, "retrieve", "", "", "Argument[0]", "get-resource", "manual"] # don't need
25+
# # - ["org.springframework.cloud.config.server.resource", "ResourceController", True, "getFilePath", "", "", "Argument[0..3]", "get-resource", "manual"] # don't need
26+
# # - ["org.springframework.cloud.config.server.resource", "ResourceRepository", True, "findOne", "", "", "Argument[0..3]", "get-resource", "manual"] # convert to summary
27+
# # - ["org.springframework.core.io", "InputStreamSource", True, "getInputStream", "", "", "Argument[this]", "get-resource", "manual"] # convert to summary
28+
# - ["org.springframework.util", "StreamUtils", True, "copyToString", "", "", "Argument[0]", "get-resource", "manual"] # * public class with good docs
29+
# # - ["org.springframework.cloud.config.server.environment", "SearchPathLocator", True, "getLocations", "(String,String,String)", "", "Argument[0..2]", "get-resource", "manual"] # convert to summary
30+
# # - ["org.springframework.cloud.config.server.environment", "SearchPathLocator$Locations", True, "getLocations", "()", "", "Argument[this]", "get-resource", "manual"] # convert to summary
31+
# - ["org.springframework.core.io", "ResourceLoader", True, "getResource", "", "", "Argument[0]", "get-resource", "manual"] # * public interface with good docs, might be problematic for FPs based on fact that the ext contributor changed this to a taint step to avoid "exists" FPS (maybe there's another way to exclude those FPs though).
32+
# - ["javax.servlet", "ServletContext", True, "getResource", "", "", "Argument[0]", "get-resource", "manual"]
33+
# - ["javax.servlet", "ServletContext", True, "getResourceAsStream", "", "", "Argument[0]", "get-resource", "manual"]
34+
# - ["javax.servlet", "ServletContext", True, "getResourcePaths", "", "", "Argument[0]", "get-resource", "manual"]
35+
# - ["javax.servlet", "ServletContext", True, "getResource", "", "", "Argument[this]", "get-resource", "manual"]
36+
# - ["javax.servlet", "ServletContext", True, "getResourceAsStream", "", "", "Argument[this]", "get-resource", "manual"]
37+
# - ["javax.servlet", "ServletContext", True, "getResourcePaths", "", "", "Argument[this]", "get-resource", "manual"]
38+
# # - ["org.apache.tomcat.util.http", "RequestUtil", True, "normalize", "", "", "Argument[0]", "get-resource", "manual"]
39+
40+
- addsTo:
41+
pack: codeql/java-all
42+
extensible: summaryModel
43+
data:
44+
- ["io.undertow.server.handlers.resource", "Resource", True, "getFile", "", "", "Argument[this]", "ReturnValue", "taint", "manual"]
45+
- ["io.undertow.server.handlers.resource", "Resource", True, "getFilePath", "", "", "Argument[this]", "ReturnValue", "taint", "manual"]
46+
- ["io.undertow.server.handlers.resource", "Resource", True, "getPath", "", "", "Argument[this]", "ReturnValue", "taint", "manual"] # ! this as a taint step seems to contradict the fact that they did `ClassPathResource.getPath` as a sink for Spring...
47+
- ["java.nio.file", "Path", True, "normalize", "", "", "Argument[this]", "ReturnValue", "taint", "manual"] # ! shouldn't this be a sanitizer instead??? Or no because WEB-INF ones don't care about normalization?
48+
- ["java.nio.file", "Path", True, "resolve", "", "", "Argument[this]", "ReturnValue", "taint", "manual"] # ! check if this and the below are already in the default models
49+
- ["java.nio.file", "Path", True, "resolve", "", "", "Argument[0]", "ReturnValue", "taint", "manual"]
50+
- ["java.nio.file", "Path", True, "toString", "", "", "Argument[this]", "ReturnValue", "taint", "manual"]
51+
- ["java.nio.file", "Paths", True, "get", "", "", "Argument[0..1]", "ReturnValue", "taint", "manual"]
52+
53+
- ["org.springframework.core.io", "ClassPathResource", False, "ClassPathResource", "", "", "Argument[0]", "Argument[this]", "taint", "manual"]
54+
- ["org.springframework.core.io", "Resource", True, "createRelative", "", "", "Argument[0]", "ReturnValue", "taint", "manual"]
55+
# # ! below added/modified by me when debugging CVEs:
56+
- ["org.springframework.core.io", "ResourceLoader", True, "getResource", "", "", "Argument[0]", "ReturnValue", "taint", "manual"]
57+
# - ["org.springframework.cloud.config.server.resource", "ResourceRepository", True, "findOne", "", "", "Argument[0..3]", "ReturnValue", "taint", "manual"] # * public interface, but might be too specific, no easily findable docs...
58+
# - ["org.springframework.core.io", "InputStreamSource", True, "getInputStream", "", "", "Argument[this]", "ReturnValue", "taint", "manual"] # * public interface with good docs, Note: other `getInputStream`s are remote source and/or taint step, so this as taint step versus sink probably is more consistent
59+
# - ["org.springframework.cloud.config.server.environment", "SearchPathLocator", True, "getLocations", "(String,String,String)", "", "Argument[0..2]", "ReturnValue", "taint", "manual"] # * public interface with docs: https://www.javadoc.io/static/org.springframework.cloud/spring-cloud-config-server/2.1.0.RELEASE/org/springframework/cloud/config/server/environment/SearchPathLocator.html
60+
# - ["org.springframework.cloud.config.server.environment", "SearchPathLocator$Locations", True, "getLocations", "()", "", "Argument[this]", "ReturnValue", "taint", "manual"] # ! is the `Locations` class package-private? Or does it inherit public from it's enclosing interface?
61+
# - ["org.springframework.cloud.config.server.resource", "ResourceController", True, "getFilePath", "", "", "Argument[0..3]", "ReturnValue", "taint", "manual"] # don't need
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//BAD: no path validation in Spring resource loading
2+
@GetMapping("/file")
3+
public String getFileContent(@RequestParam(name="fileName") String fileName) {
4+
ClassPathResource clr = new ClassPathResource(fileName);
5+
6+
File file = ResourceUtils.getFile(fileName);
7+
8+
Resource resource = resourceLoader.getResource(fileName);
9+
}
10+
11+
//GOOD: check for a trusted prefix, ensuring path traversal is not used to erase that prefix in Spring resource loading:
12+
@GetMapping("/file")
13+
public String getFileContent(@RequestParam(name="fileName") String fileName) {
14+
if (!fileName.contains("..") && fileName.hasPrefix("/public-content")) {
15+
ClassPathResource clr = new ClassPathResource(fileName);
16+
17+
File file = ResourceUtils.getFile(fileName);
18+
19+
Resource resource = resourceLoader.getResource(fileName);
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// BAD: no URI validation
2+
URL url = request.getServletContext().getResource(requestUrl);
3+
url = getClass().getResource(requestUrl);
4+
InputStream in = url.openStream();
5+
6+
InputStream in = request.getServletContext().getResourceAsStream(requestPath);
7+
in = getClass().getClassLoader().getResourceAsStream(requestPath);
8+
9+
// GOOD: check for a trusted prefix, ensuring path traversal is not used to erase that prefix:
10+
// (alternatively use `Path.normalize` instead of checking for `..`)
11+
if (!requestPath.contains("..") && requestPath.startsWith("/trusted")) {
12+
InputStream in = request.getServletContext().getResourceAsStream(requestPath);
13+
}
14+
15+
Path path = Paths.get(requestUrl).normalize().toRealPath();
16+
if (path.startsWith("/trusted")) {
17+
URL url = request.getServletContext().getResource(path.toString());
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// BAD: no URI validation
2+
String returnURL = request.getParameter("returnURL");
3+
RequestDispatcher rd = sc.getRequestDispatcher(returnURL);
4+
rd.forward(request, response);
5+
6+
// GOOD: check for a trusted prefix, ensuring path traversal is not used to erase that prefix:
7+
// (alternatively use `Path.normalize` instead of checking for `..`)
8+
if (!returnURL.contains("..") && returnURL.hasPrefix("/pages")) { ... }
9+
// Also GOOD: check for a forbidden prefix, ensuring URL-encoding is not used to evade the check:
10+
// (alternatively use `URLDecoder.decode` before `hasPrefix`)
11+
if (returnURL.hasPrefix("/internal") && !returnURL.contains("%")) { ... }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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;
8+
9+
@Controller
10+
public class UnsafeUrlForward {
11+
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+
}
27+
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();
36+
}
37+
}
38+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<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>
10+
11+
</overview>
12+
<recommendation>
13+
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>
20+
21+
</recommendation>
22+
<example>
23+
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.
28+
</p>
29+
30+
<sample src="UnsafeUrlForward.java" />
31+
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" />
52+
</example>
53+
<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>
68+
</li>
69+
</references>
70+
</qhelp>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @name Unsafe URL forward or include from a remote source
3+
* @description URL forward or include based on unvalidated user-input
4+
* may cause file information disclosure.
5+
* @kind path-problem
6+
* @problem.severity error
7+
* @precision high
8+
* @id java/unsafe-url-forward-include
9+
* @tags security
10+
* external/cwe-552
11+
*/
12+
13+
import java
14+
import UnsafeUrlForward
15+
import semmle.code.java.dataflow.FlowSources
16+
import semmle.code.java.dataflow.TaintTracking
17+
import experimental.semmle.code.java.frameworks.Jsf
18+
import semmle.code.java.security.PathSanitizer
19+
import UnsafeUrlForwardFlow::PathGraph
20+
21+
module UnsafeUrlForwardFlowConfig implements DataFlow::ConfigSig {
22+
predicate isSource(DataFlow::Node source) {
23+
source instanceof ThreatModelFlowSource and
24+
not exists(MethodCall ma, Method m | ma.getMethod() = m |
25+
(
26+
m instanceof HttpServletRequestGetRequestUriMethod or
27+
m instanceof HttpServletRequestGetRequestUrlMethod or
28+
m instanceof HttpServletRequestGetPathMethod
29+
) and
30+
ma = source.asExpr()
31+
)
32+
}
33+
34+
predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeUrlForwardSink }
35+
36+
predicate isBarrier(DataFlow::Node node) {
37+
node instanceof UnsafeUrlForwardSanitizer or
38+
node instanceof PathInjectionSanitizer
39+
}
40+
41+
DataFlow::FlowFeature getAFeature() { result instanceof DataFlow::FeatureHasSourceCallContext }
42+
43+
predicate isAdditionalFlowStep(DataFlow::Node prev, DataFlow::Node succ) {
44+
exists(MethodCall ma |
45+
(
46+
ma.getMethod() instanceof GetServletResourceMethod or
47+
ma.getMethod() instanceof GetFacesResourceMethod or
48+
ma.getMethod() instanceof GetClassResourceMethod or
49+
ma.getMethod() instanceof GetClassLoaderResourceMethod or
50+
ma.getMethod() instanceof GetWildflyResourceMethod
51+
) and
52+
ma.getArgument(0) = prev.asExpr() and
53+
ma = succ.asExpr()
54+
)
55+
}
56+
}
57+
58+
module UnsafeUrlForwardFlow = TaintTracking::Global<UnsafeUrlForwardFlowConfig>;
59+
60+
from UnsafeUrlForwardFlow::PathNode source, UnsafeUrlForwardFlow::PathNode sink
61+
where UnsafeUrlForwardFlow::flowPath(source, sink)
62+
select sink.getNode(), source, sink, "Potentially untrusted URL forward due to $@.",
63+
source.getNode(), "user-provided value"

0 commit comments

Comments
 (0)