【问题标题】:Spring boot ResourceUtils read file content is giving Path Traversal vulnerabilitySpring boot ResourceUtils 读取文件内容是给路径遍历漏洞
【发布时间】:2021-05-03 11:15:06
【问题描述】:

我正在构建一个 Spring Boot 应用程序,我需要在其中读取 json 文件以进行组件测试。我有一个实用方法,它采用文件名并使用 ResourceUtils 读取内容。代码如下:

public static String getContent(String path) throws IOException {
    File file = ResourceUtils.getFile(MyTest.class.getResource(path));
    String content = new String(Files.readAllBytes(file.toPath()));
    return content;
  }

checkmarx 将上述代码报告为“这可能会导致路径 遍历漏洞。” 如何解决这个问题?

谢谢

【问题讨论】:

    标签: spring-boot spring-mvc checkmarx


    【解决方案1】:

    路径遍历漏洞见此示例Path Traversal

    要解决此问题,请更改类似

    private static final String BASE_PATH ="/yourbasepath/somewherewherefileisstored";
    
    public static String getContent(String path) throws IOException {
       File file = new File(BASE_PATH, path);
       if (file.getCanonicalPath().startsWith(BASE_PATH)){
        String content = new String(Files.readAllBytes(file.toPath()));
        return content;
      }
      else{
        //throw some error
     }
     }
    

    【讨论】:

    • 谢谢。我通过谷歌搜索发现,但是我想知道有没有更好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-19
    • 2020-05-21
    相关资源
    最近更新 更多