【问题标题】:SpringBoot and Sanitizing @PathVariableSpring Boot 和清理@PathVariable
【发布时间】:2021-04-12 11:55:39
【问题描述】:

在我们的项目中,我们目前正在使用 Fortify 扫描仪来扫描我们的代码,我们有一个有趣的问题。我们正在考虑类似

@PathVariable (required = true) String id

Spring 已经对它进行了清理,但在我们的例子中,它在以下上下文中使用时会引发问题

@RequestMapping(
        method = RequestMethod.DELETE,
        path = "/{id}",
        consumes = "application/json"
)
public ResponseEntity cancelTask(
        @PathVariable (required = true) String id,
        @RequestBody (required = false) String reason
) {
    String userId = authenticationContext.getUserId();
    if (!authorizationService.hasPermission(userId, id, TaskAccessRight.EDIT)) {            log.warn("User {} is not authorized to cancel task {}", userId, id);
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Not authorized to cancel task " + id);
    }
 ...
}

Fortify 将此标记为跨站点脚本:反映问题。

我的问题是真的可以通过说 id 来利用

<script>alert('Hello Jack')</script>

作为路径输入...如果是这样,我们应该如何在正文中对其进行消毒?

我们不确定这是否经过消毒,所以我们不确定这是误报,有人可以确认 Spring 会自动处理这个问题吗?

我们有几十个由 fortify 提出的类似问题

【问题讨论】:

  • 您是否尝试通过@PathVariable 实际发送带有 HTML+Javascript 的字符串?我得到 500 - The request was rejected because the URL contained a potentially malicious String \"//\" 或 400 - The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing)。在我看来,Spring 阻止了不属于路径层次结构的正斜杠(例如(转义/编码)。

标签: spring spring-boot sanitization fortify


【解决方案1】:

基于静态代码分析,这一强化发现是有意义的。
字符串 id 是您的方法的输入,您的代码没有验证或清理输入,并且在 UNAUTHORIZED 的情况下,您的代码正在返回它。因此,“坏”代码正在流经您的应用程序并进行强化,我不知道客户端对响应做了什么,因此存在某种风险。

Spring 不会自动处理这个问题!

您可以使用 Hibernate-Validator(请参阅 Maven 库 spring-boot-starter-validation)来验证输入并拒绝像您的脚本一样的无效输入。
在我的德语博客中,写过一篇关于 Spring 和验证的文章,见:
https://agile-coding.blogspot.com/2020/11/validation-with-spring.html

【讨论】:

  • 啊,所以它不这样做。事实上,在这种情况下,这是一个真正的问题。谢谢,我会寻找一些简单的方法来进行不太干扰的输出清理。
  • 在我看来这很简单。您添加一个 Maven 依赖项,在 Controller 类上添加 @Validated 注释,并在 id 属性前面添加 @Pattern(regexp = "[A-Z]{1,10}") (使用您想要的正则表达式)注释。就是这样。
  • 我尝试用谷歌翻译你的文章。谢谢
猜你喜欢
  • 2018-04-06
  • 2015-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 2018-03-14
相关资源
最近更新 更多