java - java 中文件系统路径、文件名或 URI 操作的 Coverity 问题

标签 java

我正在处理源代码中的一些覆盖问题。我在这里使用 代码如下,

    filePath = properties.getProperty("DO.LIB.LOC");
    String fileName = (String) request.getParameter("read");
    filePath += "/" + fileName;
    downloadResultSet.flushFile(filePath, response, 
    fileName.substring(fileName.lastIndexOf(".") + 1));

In my coveiry scanned tool found an error like "CID 38762 (#1 of 2): Filesystem path, filename, or URI manipulation (PATH_MANIPULATION) 2. sink: Constructing a path or URI using the tainted value filePath. This may allow an attacker to access, modify, or test the existence of critical or sensitive files. The value is used unsafely in bytecode, which cannot be displayed."

外部属性文件中定义的文件路径和文件名取自 请求。

我在不同的java文件中使用相同的代码进行文件上传、删除 ,下载功能。我怎样才能避免这些漏洞 从我的代码中。任何人都可以帮我解决这个问题吗?

最佳答案

您应该使用 Path 类型而不是 String (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Path.html)

它看起来像这样:

 Path path = Paths.get(properties.getProperty("DO.LIB.LOC"));
 path = path.resolve(Paths.get(request.getParameter("read")));

关于java - java 中文件系统路径、文件名或 URI 操作的 Coverity 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44321371/

相关文章:

java - 在 Java 中为变量分配动态名称

java - jdbc 驱动程序仅在使用内存时有效

java - 你如何创建一个字符串对象

java - 按下 javafx 按钮时不执行

java - 什么 Web UI 框架最适合构建动态页面/表单?

java - 在没有 clear() 的情况下更新 ListView 项目

java - 如何绘制一组水平线?

java - 使用 MVC 将 ArrayList 转换为 JList

java - 无法在 java 中访问 getJSONArray

java - 是否有任何池解决方案允许将 key 传递给 acquire/take 方法