Skip to content

Commit 9d9a7ab

Browse files
haby0smowton
authored andcommitted
Fix
1 parent 283376e commit 9d9a7ab

File tree

3 files changed

+3
-114
lines changed

3 files changed

+3
-114
lines changed

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

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,7 @@ public ModelAndView bad1(String url) {
1515
}
1616

1717
@GetMapping("/bad2")
18-
public ModelAndView bad2(String url) {
19-
ModelAndView modelAndView = new ModelAndView();
20-
modelAndView.setViewName(url);
21-
return modelAndView;
22-
}
23-
24-
@GetMapping("/bad3")
25-
public String bad3(String url) {
26-
return "forward:" + url + "/swagger-ui/index.html";
27-
}
28-
29-
@GetMapping("/bad4")
30-
public ModelAndView bad4(String url) {
31-
ModelAndView modelAndView = new ModelAndView("forward:" + url);
32-
return modelAndView;
33-
}
34-
35-
@GetMapping("/bad5")
36-
public void bad5(String url, HttpServletRequest request, HttpServletResponse response) {
37-
try {
38-
request.getRequestDispatcher(url).include(request, response);
39-
} catch (ServletException e) {
40-
e.printStackTrace();
41-
} catch (IOException e) {
42-
e.printStackTrace();
43-
}
44-
}
45-
46-
@GetMapping("/bad6")
47-
public void bad6(String url, HttpServletRequest request, HttpServletResponse response) {
18+
public void bad2(String url, HttpServletRequest request, HttpServletResponse response) {
4819
try {
4920
request.getRequestDispatcher("/WEB-INF/jsp/" + url + ".jsp").include(request, response);
5021
} catch (ServletException e) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
<example>
1818

1919
<p>The following examples show the bad case and the good case respectively.
20-
In <code>bad1</code> method and <code>bad2</code> method and <code>bad3</code> method and
21-
<code>bad4</code> method and <code>bad5</code> method and <code>bad6</code> method, shows an HTTP request parameter being used directly in a URL forward
20+
The <code>bad</code> methods show an HTTP request parameter being used directly in a URL forward
2221
without validating the input, which may cause file leakage. In <code>good1</code> method,
2322
ordinary forwarding requests are shown, which will not cause file leakage.
2423
</p>

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

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import DataFlow
33
import semmle.code.java.dataflow.FlowSources
44
import semmle.code.java.frameworks.Servlets
55
import semmle.code.java.frameworks.spring.SpringWeb
6+
import semmle.code.java.security.RequestForgery
67

78
/** A sanitizer for unsafe url forward vulnerabilities. */
89
abstract class UnsafeUrlForwardSanitizer extends DataFlow::Node { }
@@ -32,88 +33,6 @@ private Expr getAUnsafeUrlForwardSanitizingPrefix() {
3233
result.(AddExpr).getAnOperand() = getAUnsafeUrlForwardSanitizingPrefix()
3334
}
3435

35-
/** A call to `StringBuilder.append` method. */
36-
class StringBuilderAppend extends MethodAccess {
37-
StringBuilderAppend() {
38-
this.getMethod().getDeclaringType() instanceof StringBuildingType and
39-
this.getMethod().hasName("append")
40-
}
41-
}
42-
43-
private Expr getQualifier(Expr e) { result = e.(MethodAccess).getQualifier() }
44-
45-
/**
46-
* An extension of `StringBuilderVar` that also accounts for strings appended in StringBuilder/Buffer's constructor
47-
* and in `append` calls chained onto the constructor call.
48-
*
49-
* The original `StringBuilderVar` doesn't care about these because it is designed to model taint, and
50-
* in taint rules terms these are not needed, as the connection between construction, appends and the
51-
* eventual `toString` is more obvious.
52-
*/
53-
private class StringBuilderVarExt extends StringBuilderVar {
54-
/**
55-
* Returns a first assignment after this StringBuilderVar is first assigned.
56-
*
57-
* For example, for `StringBuilder sbv = new StringBuilder("1").append("2"); sbv.append("3").append("4");`
58-
* this returns the append of `"3"`.
59-
*/
60-
private StringBuilderAppend getAFirstAppendAfterAssignment() {
61-
result = this.getAnAppend() and not result = this.getNextAppend(_)
62-
}
63-
64-
/**
65-
* Gets the next `append` after `prev`, where `prev` is, perhaps after some more `append` or other
66-
* chained calls, assigned to this `StringBuilderVar`.
67-
*/
68-
private StringBuilderAppend getNextAssignmentChainedAppend(StringBuilderConstructorOrAppend prev) {
69-
getQualifier*(result) = this.getAnAssignedValue() and
70-
result.getQualifier() = prev
71-
}
72-
73-
/**
74-
* Get a constructor call or `append` call that contributes a string to this string builder.
75-
*/
76-
StringBuilderConstructorOrAppend getAConstructorOrAppend() {
77-
exists(this.getNextAssignmentChainedAppend(result)) or
78-
result = this.getAnAssignedValue() or
79-
result = this.getAnAppend()
80-
}
81-
82-
/**
83-
* Like `StringBuilderVar.getNextAppend`, except including appends and constructors directly
84-
* assigned to this `StringBuilderVar`.
85-
*/
86-
private StringBuilderAppend getNextAppendIncludingAssignmentChains(
87-
StringBuilderConstructorOrAppend prev
88-
) {
89-
result = getNextAssignmentChainedAppend(prev)
90-
or
91-
prev = this.getAnAssignedValue() and
92-
result = this.getAFirstAppendAfterAssignment()
93-
or
94-
result = this.getNextAppend(prev)
95-
}
96-
97-
/**
98-
* Implements `StringBuilderVarExt.getNextAppendIncludingAssignmentChains+(prev)`.
99-
*/
100-
pragma[nomagic]
101-
StringBuilderAppend getSubsequentAppendIncludingAssignmentChains(
102-
StringBuilderConstructorOrAppend prev
103-
) {
104-
result = this.getNextAppendIncludingAssignmentChains(prev) or
105-
result =
106-
this.getSubsequentAppendIncludingAssignmentChains(this.getNextAppendIncludingAssignmentChains(prev))
107-
}
108-
}
109-
110-
private class StringBuilderConstructorOrAppend extends Call {
111-
StringBuilderConstructorOrAppend() {
112-
this instanceof StringBuilderAppend or
113-
this.(ClassInstanceExpr).getConstructedType() instanceof StringBuildingType
114-
}
115-
}
116-
11736
private class UnsafeUrlForwardSanitizedExpr extends Expr {
11837
UnsafeUrlForwardSanitizedExpr() {
11938
// Sanitize expressions that come after a sanitizing prefix in a tree of string additions:

0 commit comments

Comments
 (0)