Open alwaysgoodtime opened 7 months ago
6f01709a28
)[!TIP] I can email you next time I complete a pull request if you set up your email here!
I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.
src/main/java/org/joychou/controller/SpEL.java
✓ https://github.com/alwaysgoodtime/java-sec-code/commit/726fcf5f32563de296c5b252815aee250145ac0f Edit
Modify src/main/java/org/joychou/controller/SpEL.java with contents:
• For the endpoint mapped to "/spel/vuln1", replace the direct evaluation of user input with a safer evaluation context. Specifically, change the method spel_vuln1 to use SimpleEvaluationContext similar to the "/spel/sec" endpoint. This prevents the evaluation of potentially dangerous expressions. - Replace the line in spel_vuln1 method with the following: ```java SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); Expression expression = parser.parseExpression(value); Object x = expression.getValue(context); return x.toString(); ```
• For the endpoint mapped to "/spel/vuln2", ensure that the evaluation context used is safe. Given that this endpoint uses TemplateParserContext, which might be necessary for its functionality, adjust the evaluation context to SimpleEvaluationContext to limit the capabilities of the evaluated expressions. - Modify the spel_vuln2 method to use SimpleEvaluationContext instead of StandardEvaluationContext: ```java SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); Expression expression = parser.parseExpression(value, new TemplateParserContext()); Object x = expression.getValue(context); return x.toString(); ```
• These changes ensure that both vulnerable endpoints now evaluate user input in a restricted and safer context, mitigating the risk of SpEL injection vulnerabilities.
--- +++ @@ -23,8 +23,11 @@ */ @RequestMapping("/spel/vuln1") public String spel_vuln1(String value) { - ExpressionParser parser = new SpelExpressionParser(); - return parser.parseExpression(value).getValue().toString(); + SpelExpressionParser parser = new SpelExpressionParser(); + SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); + Expression expression = parser.parseExpression(value); + Object x = expression.getValue(context); + return x.toString(); } /** @@ -34,11 +37,11 @@ */ @RequestMapping("spel/vuln2") public String spel_vuln2(String value) { - StandardEvaluationContext context = new StandardEvaluationContext(); + SimpleEvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build(); SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression(value, new TemplateParserContext()); - Object x = expression.getValue(context); // trigger vulnerability point - return x.toString(); // response + Object x = expression.getValue(context); + return x.toString(); } /**
src/main/java/org/joychou/controller/SpEL.java
✓ Edit
Check src/main/java/org/joychou/controller/SpEL.java with contents:
Ran GitHub Actions for 726fcf5f32563de296c5b252815aee250145ac0f:
I have finished reviewing the code for completeness. I did not find errors for sweep/speljava
.
💡 To recreate the pull request edit the issue title or description. Something wrong? Let us know.
This is an automated message generated by Sweep AI.
Checklist
- [X] Modify `src/main/java/org/joychou/controller/SpEL.java` ✓ https://github.com/alwaysgoodtime/java-sec-code/commit/726fcf5f32563de296c5b252815aee250145ac0f [Edit](https://github.com/alwaysgoodtime/java-sec-code/edit/sweep/speljava/src/main/java/org/joychou/controller/SpEL.java) - [X] Running GitHub Actions for `src/main/java/org/joychou/controller/SpEL.java` ✓ [Edit](https://github.com/alwaysgoodtime/java-sec-code/edit/sweep/speljava/src/main/java/org/joychou/controller/SpEL.java)