【问题标题】:Path Manipulation error fix for filename文件名的路径操作错误修复
【发布时间】:2015-05-07 11:39:44
【问题描述】:

我有一段代码,我必须在其中读取文件以获取其可能的内容。

我遇到了同样的路径操作错误。

PFB 代码:

while ((ze = zis.getNextEntry()) != null) {
    String fileName = ze.getName();
    String esapiFileName = ESAPI.encoder().canonicalize(fileName);
    boolean esapiValidFileName = ESAPI.validator().isValidFileName("upload", esapiFileName, false);
    String _completefileNamePath = null;
    if (esapiValidFileName) {
      _completefileNamePath = _destination + esapiFileName;
      // Below line having Path Manipulation error
      FileOutputStream fos = new FileOutputStream(new File(_completefileNamePath).getCanonicalFile());
      // Path Manipulation error ends
      while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
        fos.write(buffer, 0, size);
      }// while
      fos.flush();
      fos.close();
      zis.closeEntry();
    }
}

【问题讨论】:

  • 收到此错误时 _completefileNamePath 的值是多少?
  • _completefileNamePath 是 /home/batch/app/ackfile/filename.txt

标签: java owasp fortify fortify-source


【解决方案1】:

你的路径是相对的还是绝对的?

顺便说一句,您实际上不需要在打开 FileOutputStream 之前获取规范文件:

FileOutputStream fos = new FileOutputStream(_completefileNamePath);

FileOutputStream fos = new FileOutputStream(new File(_completefileNamePath));

import java.nio.file.Files;
import java.nio.file.Paths;

while ((ze = zis.getNextEntry()) != null) {
    String fileName = ze.getName();
    String esapiFileName = ESAPI.encoder().canonicalize(fileName);
    boolean esapiValidFileName = ESAPI.validator().isValidFileName("upload", esapiFileName, false);
    String _completefileNamePath = null;
    if (esapiValidFileName) {
        _completefileNamePath = _destination + esapiFileName;
        // optional: Files.createDirectories(Paths.get(_completefileNamePath).getParent());
        Files.copy(zis, Paths.get(_completefileNamePath));
        zis.closeEntry();
    }
}

【讨论】:

  • 所以只删除规范文件信息就足够了吗?
  • 它可能,因为无论如何都没有必要。并且只要文件名确实有效。
  • 好的,让我尝试删除规范文件。
  • 完整文件名路径是/home/batch/app/ackfile/filename.txt
  • 现在工作了吗?该目录是否存在“/home/batch/app/ackfile/”?
猜你喜欢
  • 2021-12-30
  • 2022-12-09
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多