1

我们有一个 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 错误?

4

1 回答 1

1

返回的值MyCustomAnnotation.entityId()是在您的代码中硬编码的表达式。因此,我认为来源是安全的。

您可以信任编写代码的开发人员。您不能信任远程用户。此处的值仅由开发人员控制。


旁注:我认为该工具将注释视为始终安全是合理的。为此创建了一张票。

于 2021-08-03T06:28:39.240 回答