Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 1.72 KB

path_traversal.md

File metadata and controls

47 lines (37 loc) · 1.72 KB

漏洞代码

    private String uploadExcelFile(MultipartFile file)throws Exception{
        String fileName = System.currentTimeMillis() +"_"+file.getOriginalFilename();    // 获取当前系统时间戳与文件名拼接
        String basePath = "D:\\repos\\xxx\upload\\";
        File newFile = new File(basePath+fileName);

        // 父目录不存在,会创建
        if (!newFile.getParentFile().exists()){
            newFile.getParentFile().mkdirs();
        }
        
        // 文件不存在,会创建
        if (!newFile.exists()){
            newFile.createNewFile();
        }
        Files.write(file.getBytes(), newFile);    // 写入文件
        
        return basePath+fileName;
    }

修复代码

// 这里是参照CVE-2019-3398的修复方式来的。 File#getName方法仅取文件名的最后部分。参考:https://docs.oracle.com/javase/8/docs/api/java/io/File.html#getName--

    private String uploadExcelFile(MultipartFile file)throws Exception{
        String fileName = System.currentTimeMillis() +"_"+file.getOriginalFilename();
        String basePath = "D:\\repos\\xxx\\upload\\";
        
        // 这里是参照CVE-2019-3398的修复方式来的。File#getName方法仅取文件名的最后部分。参考:https://docs.oracle.com/javase/8/docs/api/java/io/File.html#getName--
        File newFile = new File(basePath+(new File(fileName)).getName());
        

        if (!newFile.getParentFile().exists()){
            newFile.getParentFile().mkdirs();
        }
        if (!newFile.exists()){
            newFile.createNewFile();
        }
        Files.write(file.getBytes(), newFile);
        
        return newFile.getName();
    }