Skip to content

Commit f0c4b19

Browse files
committed
Change getResource() to be a taint step
1 parent 7029802 commit f0c4b19

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

java/ql/lib/semmle/code/java/frameworks/Servlets.qll

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,18 @@ library class ServletContext extends RefType {
385385
ServletContext() { this.hasQualifiedName("javax.servlet", "ServletContext") }
386386
}
387387

388-
/** The `getResource` and `getResourceAsStream` methods of `ServletContext`. */
388+
/** The `getResource` method of `ServletContext`. */
389389
class GetServletResourceMethod extends Method {
390390
GetServletResourceMethod() {
391391
this.getDeclaringType() instanceof ServletContext and
392-
this.hasName(["getResource", "getResourceAsStream"])
392+
this.hasName("getResource")
393+
}
394+
}
395+
396+
/** The `getResourceAsStream` method of `ServletContext`. */
397+
class GetServletResourceAsStreamMethod extends Method {
398+
GetServletResourceAsStreamMethod() {
399+
this.getDeclaringType() instanceof ServletContext and
400+
this.hasName("getResourceAsStream")
393401
}
394402
}

java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.ql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ class UnsafeUrlForwardFlowConfig extends TaintTracking::Configuration {
4141
override DataFlow::FlowFeature getAFeature() {
4242
result instanceof DataFlow::FeatureHasSourceCallContext
4343
}
44+
45+
override predicate isAdditionalTaintStep(DataFlow::Node prev, DataFlow::Node succ) {
46+
exists(MethodAccess ma |
47+
(
48+
ma.getMethod() instanceof GetServletResourceMethod or
49+
ma.getMethod() instanceof GetFacesResourceMethod or
50+
ma.getMethod() instanceof GetClassResourceMethod or
51+
ma.getMethod() instanceof GetClassLoaderResourceMethod or
52+
ma.getMethod() instanceof GetWildflyResourceMethod
53+
) and
54+
ma.getArgument(0) = prev.asExpr() and
55+
ma = succ.asExpr()
56+
)
57+
}
4458
}
4559

4660
from DataFlow::PathNode source, DataFlow::PathNode sink, UnsafeUrlForwardFlowConfig conf

java/ql/src/experimental/Security/CWE/CWE-552/UnsafeUrlForward.qll

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,35 @@ private class RequestDispatcherSink extends UnsafeUrlForwardSink {
1919
}
2020
}
2121

22-
/** The `getResource` and `getResourceAsStream` methods of `Class`. */
22+
/** The `getResource` method of `Class`. */
2323
class GetClassResourceMethod extends Method {
2424
GetClassResourceMethod() {
2525
this.getSourceDeclaration().getDeclaringType().hasQualifiedName("java.lang", "Class") and
26-
this.hasName(["getResource", "getResourceAsStream"])
26+
this.hasName("getResource")
27+
}
28+
}
29+
30+
/** The `getResourceAsStream` method of `Class`. */
31+
class GetClassResourceAsStreamMethod extends Method {
32+
GetClassResourceAsStreamMethod() {
33+
this.getSourceDeclaration().getDeclaringType().hasQualifiedName("java.lang", "Class") and
34+
this.hasName("getResourceAsStream")
2735
}
2836
}
2937

30-
/** The `getResource` and `getResourceAsStream` methods of `ClassLoader`. */
38+
/** The `getResource` method of `ClassLoader`. */
3139
class GetClassLoaderResourceMethod extends Method {
3240
GetClassLoaderResourceMethod() {
3341
this.getDeclaringType().hasQualifiedName("java.lang", "ClassLoader") and
34-
this.hasName(["getResource", "getResourceAsStream"])
42+
this.hasName("getResource")
43+
}
44+
}
45+
46+
/** The `getResourceAsStream` method of `ClassLoader`. */
47+
class GetClassLoaderResourceAsStreamMethod extends Method {
48+
GetClassLoaderResourceAsStreamMethod() {
49+
this.getDeclaringType().hasQualifiedName("java.lang", "ClassLoader") and
50+
this.hasName("getResourceAsStream")
3551
}
3652
}
3753

@@ -66,13 +82,14 @@ class GetVirtualFileMethod extends Method {
6682
/** An argument to `getResource()` or `getResourceAsStream()`. */
6783
private class GetResourceSink extends UnsafeUrlForwardSink {
6884
GetResourceSink() {
85+
sinkNode(this, "open-url")
86+
or
6987
exists(MethodAccess ma |
7088
(
71-
ma.getMethod() instanceof GetServletResourceMethod or
72-
ma.getMethod() instanceof GetFacesResourceMethod or
73-
ma.getMethod() instanceof GetClassResourceMethod or
74-
ma.getMethod() instanceof GetClassLoaderResourceMethod or
75-
ma.getMethod() instanceof GetWildflyResourceMethod or
89+
ma.getMethod() instanceof GetServletResourceAsStreamMethod or
90+
ma.getMethod() instanceof GetFacesResourceAsStreamMethod or
91+
ma.getMethod() instanceof GetClassResourceAsStreamMethod or
92+
ma.getMethod() instanceof GetClassLoaderResourceAsStreamMethod or
7693
ma.getMethod() instanceof GetVirtualFileMethod
7794
) and
7895
ma.getArgument(0) = this.asExpr()

java/ql/src/experimental/semmle/code/java/frameworks/Jsf.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,21 @@ class ExternalContext extends RefType {
1414
}
1515

1616
/**
17-
* The methods `getResource()` and `getResourceAsStream()` declared in JSF `ExternalContext`.
17+
* The method `getResource()` declared in JSF `ExternalContext`.
1818
*/
1919
class GetFacesResourceMethod extends Method {
2020
GetFacesResourceMethod() {
2121
this.getDeclaringType().getASupertype*() instanceof ExternalContext and
22-
this.hasName(["getResource", "getResourceAsStream"])
22+
this.hasName("getResource")
23+
}
24+
}
25+
26+
/**
27+
* The method `getResourceAsStream()` declared in JSF `ExternalContext`.
28+
*/
29+
class GetFacesResourceAsStreamMethod extends Method {
30+
GetFacesResourceAsStreamMethod() {
31+
this.getDeclaringType().getASupertype*() instanceof ExternalContext and
32+
this.hasName("getResourceAsStream")
2333
}
2434
}

0 commit comments

Comments
 (0)