Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/io/shiftleft/controller/SearchController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public String doGetSearch(@RequestParam String foo, HttpServletResponse response
try {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(foo);
message = (Object) exp.getValue();
SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
message = (Object) exp.getValue(context);

Check failure

Code scanning / CodeQL

Expression language injection (Spring)

SpEL expression depends on a [user-provided value](1).

Copilot Autofix

AI over 1 year ago

To fix the problem, we need to ensure that the user input is not directly used in a SpEL expression. Instead, we should sanitize the input or avoid using it in a SpEL expression altogether. If using the input in a SpEL expression is necessary, we should use a more restricted evaluation context.

In this case, we will sanitize the input to ensure it does not contain any potentially dangerous characters or expressions. We will also configure the SimpleEvaluationContext to be more restrictive.

Suggested changeset 1
src/main/java/io/shiftleft/controller/SearchController.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/main/java/io/shiftleft/controller/SearchController.java b/src/main/java/io/shiftleft/controller/SearchController.java
--- a/src/main/java/io/shiftleft/controller/SearchController.java
+++ b/src/main/java/io/shiftleft/controller/SearchController.java
@@ -23,5 +23,7 @@
     try {
+      // Sanitize user input
+      String sanitizedFoo = foo.replaceAll("[^a-zA-Z0-9]", "");
       ExpressionParser parser = new SpelExpressionParser();
-      Expression exp = parser.parseExpression(foo);
-      SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
+      Expression exp = parser.parseExpression(sanitizedFoo);
+      SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withInstanceMethods().build();
       message = (Object) exp.getValue(context);
EOF
@@ -23,5 +23,7 @@
try {
// Sanitize user input
String sanitizedFoo = foo.replaceAll("[^a-zA-Z0-9]", "");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(foo);
SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
Expression exp = parser.parseExpression(sanitizedFoo);
SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withInstanceMethods().build();
message = (Object) exp.getValue(context);
Copilot is powered by AI and may make mistakes. Always verify output.
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Expand Down