Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55fae2d

Browse files
committed
Added ESAPI sanitizer
1 parent 97d6e82 commit 55fae2d

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/** Classes and predicates for reasoning about the `owasp.easpi` package. */
2+
3+
import java
4+
5+
/**
6+
* The `org.owasp.esapi.Validator` interface.
7+
*/
8+
class EsapiValidator extends RefType {
9+
EsapiValidator() { this.hasQualifiedName("org.owasp.esapi", "Validator") }
10+
}
11+
12+
/**
13+
* The methods of `org.owasp.esapi.Validator` which validate data.
14+
*/
15+
class EsapiIsValidMethod extends Method {
16+
EsapiIsValidMethod() {
17+
this.getDeclaringType() instanceof EsapiValidator and
18+
this.hasName([
19+
"isValidCreditCard", "isValidDate", "isValidDirectoryPath", "isValidDouble",
20+
"isValidFileContent", "isValidFileName", "isValidInput", "isValidInteger",
21+
"isValidListItem", "isValidNumber", "isValidPrintable", "isValidRedirectLocation",
22+
"isValidSafeHTML", "isValidURI"
23+
])
24+
}
25+
}
26+
27+
/**
28+
* The methods of `org.owasp.esapi.Validator` which return validated data.
29+
*/
30+
class EsapiGetValidMethod extends Method {
31+
EsapiGetValidMethod() {
32+
this.getDeclaringType() instanceof EsapiValidator and
33+
this.hasName([
34+
"getValidCreditCard", "getValidDate", "getValidDirectoryPath", "getValidDouble",
35+
"getValidFileContent", "getValidFileName", "getValidInput", "getValidInteger",
36+
"getValidListItem", "getValidNumber", "getValidPrintable", "getValidRedirectLocation",
37+
"getValidSafeHTML", "getValidURI"
38+
])
39+
}
40+
}

java/ql/lib/semmle/code/java/security/TrustBoundaryViolationQuery.qll

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import java
44
private import semmle.code.java.dataflow.DataFlow
5+
private import semmle.code.java.controlflow.Guards
56
private import semmle.code.java.dataflow.ExternalFlow
67
private import semmle.code.java.dataflow.FlowSources
8+
private import semmle.code.java.frameworks.owasp.Esapi
79

810
/**
911
* A source of data that crosses a trust boundary.
@@ -26,6 +28,27 @@ class TrustBoundaryViolationSink extends DataFlow::Node {
2628

2729
abstract class TrustBoundaryValidationSanitizer extends DataFlow::Node { }
2830

31+
/**
32+
* A node validated by an OWASP ESAPI validation method.
33+
*/
34+
private class EsapiValidatedInputSanitizer extends TrustBoundaryValidationSanitizer {
35+
EsapiValidatedInputSanitizer() {
36+
this = DataFlow::BarrierGuard<esapiIsValidData/3>::getABarrierNode() or
37+
this.asExpr().(MethodAccess).getMethod() instanceof EsapiGetValidMethod
38+
}
39+
}
40+
41+
/**
42+
* Holds if `g` is a guard that checks that `e` is valid data according to an OWASP ESAPI validation method.
43+
*/
44+
private predicate esapiIsValidData(Guard g, Expr e, boolean branch) {
45+
branch = true and
46+
exists(MethodAccess ma | ma.getMethod() instanceof EsapiIsValidMethod |
47+
g = ma and
48+
e = ma.getArgument(1)
49+
)
50+
}
51+
2952
/**
3053
* Taint tracking for data that crosses a trust boundary.
3154
*/

java/ql/test/query-tests/security/CWE-501/TrustBoundaryViolations.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22
import javax.servlet.http.HttpServlet;
33
import javax.servlet.http.HttpServletRequest;
44
import javax.servlet.http.HttpServletResponse;
5+
import org.owasp.esapi.Validator;
56

67
public class TrustBoundaryViolations extends HttpServlet {
8+
Validator validator;
9+
710
public void doGet(HttpServletRequest request, HttpServletResponse response) {
811
String input = request.getParameter("input");
912

13+
// BAD: The input is written to the response without being sanitized.
1014
request.getSession().setAttribute("input", input); // $ hasTaintFlow
15+
16+
String input2 = request.getParameter("input2");
17+
18+
try {
19+
String sanitized = validator.getValidInput("HTTP parameter", input2, "HTTPParameterValue", 100, false);
20+
// GOOD: The input is sanitized before being written to the response.
21+
request.getSession().setAttribute("input2", sanitized);
22+
23+
} catch (Exception e) {
24+
}
25+
26+
try {
27+
String input3 = request.getParameter("input3");
28+
if (validator.isValidInput("HTTP parameter", input3, "HTTPParameterValue", 100, false)) {
29+
// GOOD: The input is sanitized before being written to the response.
30+
request.getSession().setAttribute("input3", input3);
31+
}
32+
} catch (Exception e) {
33+
}
1134
}
1235
}

0 commit comments

Comments
 (0)