File tree Expand file tree Collapse file tree 3 files changed +29
-10
lines changed
java/ql/src/Security/CWE/CWE-094 Expand file tree Collapse file tree 3 files changed +29
-10
lines changed Original file line number Diff line number Diff line change 1+ public void evaluate (Socket socket ) throws IOException {
2+ try (BufferedReader reader = new BufferedReader (
3+ new InputStreamReader (socket .getInputStream ()))) {
4+
5+ String expression = reader .readLine ();
6+ // BAD: the user-provided expression is directly evaluated
7+ MVEL .eval (expression );
8+ }
9+ }
10+
11+ public void safeEvaluate (Socket socket ) throws IOException {
12+ try (BufferedReader reader = new BufferedReader (
13+ new InputStreamReader (socket .getInputStream ()))) {
14+
15+ String expression = reader .readLine ();
16+ // GOOD: the user-provided expression is validated before evaluation
17+ validateExpression (expression );
18+ MVEL .eval (expression );
19+ }
20+ }
21+
22+ private void validateExpression (String expression ) {
23+ // Validate that the expression does not contain unexpected code.
24+ // For instance, this can be done with allow-lists or deny-lists of code patterns.
25+ }
Original file line number Diff line number Diff line change @@ -19,8 +19,10 @@ Including user input in a MVEL expression should be avoided.
1919
2020<example >
2121<p >
22- The following example uses untrusted data to build a MVEL expression
23- and then runs it in the default powerfull context.
22+ In the following sample, the first example uses untrusted data to build a MVEL expression
23+ and then runs it in the default context. In the second example, the untrusted data is
24+ validated with a custom method that checks that the expression does not contain unexpected code
25+ before evaluating it.
2426</p >
2527<sample src =" UnsafeMvelExpressionEvaluation.java" />
2628</example >
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments