我们有一个 spring-boot (2.4.2) 应用程序和一个 Aspect 类,它执行一些处理“Around”方法,这些方法使用我们定义的自定义注释进行注释,并使用 SpEL 处理。
SpEL 表达式由我们定义为注释中的字段。
在运行 Sonar 工具和 Findsecbugs 时,我们被告知代码中存在漏洞,并出现错误“This use of org/springframework/expression/ExpressionParser.parseExpression(Ljava/lang/String;)Lorg/springframework/表达式/表达式;可能容易受到代码注入(Spring 表达式)的攻击”。违规行是下面的第 4 行:
1. private final ExpressionParser elParser = new SpelExpressionParser();
...
2. @Around(value = "@annotation(myCustomAnnotation)")
3. public Object aroundAdviceHandler(ProceedingJoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) throws Throwable {
...
4. **Expression expression = elParser.parseExpression(myCustomAnnotation.entityId());**
使用此 Aspect 的注释代码如下所示:
@Transactional
@MyCustomAnnotation(entityId = "[0].id") // some other methods my have here only "[0]" or "[0].otherId"
public Long save(JustADto dto) {
最后,自定义注释看起来像:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) public
@interface MyCustomAnnotation {
String entityId() default "";
}
这段代码似乎没有任何漏洞,因为 spring 表达式的输入是我们提供的。这是 Findsecbugs 的误报吗?除了使用 <@SuppressFBWarnings(value = {"SPEL_INJECTION"}, justification = "false positive")> 注释之外,还有什么方法可以防止出现 Sonar & Findsecbugs 错误?