Skip to content

Commit 952b34a

Browse files
haby0smowton
authored andcommitted
Eliminate FP
1 parent d0eec1e commit 952b34a

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Unsafe url forward from remote source
3-
* @description URL forward based on unvalidated user-input
3+
* @description URL forward based on unvalidated user-input
44
* may cause file information disclosure.
55
* @kind path-problem
66
* @problem.severity error
@@ -18,7 +18,16 @@ import DataFlow::PathGraph
1818
class UnsafeUrlForwardFlowConfig extends TaintTracking::Configuration {
1919
UnsafeUrlForwardFlowConfig() { this = "UnsafeUrlForwardFlowConfig" }
2020

21-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
21+
override predicate isSource(DataFlow::Node source) {
22+
source instanceof RemoteFlowSource and
23+
not exists(MethodAccess ma |
24+
ma.getMethod().getName() in ["getRequestURI", "getRequestURL", "getPathInfo"] and
25+
ma.getMethod()
26+
.getDeclaringType()
27+
.getASupertype*()
28+
.hasQualifiedName("javax.servlet.http", "HttpServletRequest")
29+
)
30+
}
2231

2332
override predicate isSink(DataFlow::Node sink) { sink instanceof UnsafeUrlForwardSink }
2433

@@ -30,11 +39,24 @@ class UnsafeUrlForwardFlowConfig extends TaintTracking::Configuration {
3039
exists(AddExpr ae |
3140
ae.getRightOperand() = node.asExpr() and
3241
(
33-
not ae.getLeftOperand().(CompileTimeConstantExpr).getStringValue().matches("/WEB-INF/%")
34-
and
42+
not ae.getLeftOperand().(CompileTimeConstantExpr).getStringValue().matches("/WEB-INF/%") and
3543
not ae.getLeftOperand().(CompileTimeConstantExpr).getStringValue() = "forward:"
3644
)
3745
)
46+
or
47+
exists(MethodAccess ma, int i |
48+
ma.getMethod().hasName("format") and
49+
ma.getMethod().getDeclaringType() instanceof TypeString and
50+
ma.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "redirect:" and
51+
ma.getArgument(i) = node.asExpr() and
52+
i != 0
53+
)
54+
or
55+
exists(StringBuilderAppendCall ma1, StringBuilderAppendCall ma2 |
56+
DataFlow2::localExprFlow(ma1.getQualifier(), ma2.getQualifier()) and
57+
ma1.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "redirect:" and
58+
ma2.getArgument(0) = node.asExpr()
59+
)
3860
}
3961
}
4062

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import DataFlow
33
import semmle.code.java.dataflow.FlowSources
44
import semmle.code.java.frameworks.Servlets
55

6+
/** A call to `StringBuilder.append` method. */
7+
class StringBuilderAppendCall extends MethodAccess {
8+
StringBuilderAppendCall() {
9+
this.getMethod().hasName("append") and
10+
this.getMethod().getDeclaringType() instanceof StringBuildingType
11+
}
12+
}
13+
614
/**
715
* A concatenate expression using the string `forward:` on the left.
816
*
@@ -19,10 +27,8 @@ class ForwardBuilderExpr extends AddExpr {
1927
*
2028
* E.g: `StringBuilder.append("forward:")`
2129
*/
22-
class ForwardAppendCall extends MethodAccess {
30+
class ForwardAppendCall extends StringBuilderAppendCall {
2331
ForwardAppendCall() {
24-
this.getMethod().hasName("append") and
25-
this.getMethod().getDeclaringType() instanceof StringBuildingType and
2632
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = "forward:"
2733
}
2834
}

0 commit comments

Comments
 (0)