Absolute Path Traversal 错误解决
一、使用专门的文件访问库,例如 Apache Commons IO 库中的 FileUtils 类
1. File文件修改
maven引入包
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</groupId>
<version>2.4</version>
</dependency>
File、FileInputStream 和 FileOutputStream 文件修改
import org.apache.commons.io.FileUtils;
File file = new File("xxx/xx/test.txt");
FileInputStream input = new FileInputStream(file);
FileOutputStream output = new FileOutputStream(file);
// TODO 改为
File file = FileUtils.getFile("xxx/xx/test.txt");
FileInputStream input = FileUtils.openInputStream(file);
FileOutputStream output = FileUtils.openOutputStream(file);
二、使用 Java 7 或更高版本中提供的 Path 和 Files 类,它们提供了更加安全的文件访问方法。
2. BufferReader 和 BufferWriter修改
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; BufferReader reader = new BufferReader(new File("path/to/text.txt"));
BufferWriter writer = new BufferWriter(new File("path/to/text.txt"));
// TODO 改为
Path readerPath = Paths.get("path/to/text.txt");
BufferReader = Files.newBufferedReader(readerPath);
Path writerPath = Paths.get("path/to/text.txt");
BufferWriter = Files.newBufferedWriter(writerPath);
3.FileReader 和 FileWriter修改
FileReader reader = new FileReader(new File("path/to/text.txt"));
Filewriter Writer= new FileWriter(new File("path/to/text.txt"));
// TODO 改为
Path readerPath = Paths.get("path/to/text.txt");
FileReader reader = new FileReader(readerPath.toFile());
Path writerPath = Paths.get("path/to/text.txt");
Filewriter Writer= new FileWriter(writerPath.toFile());
二、白名单法
以下是使用 Java 的文件输入流和输出流的代码示例,使用白名单检查文件路径是否合法,防止 APT 漏洞:
import java.io.*;
import java.util.ArrayList;
import java.util.List; public class APTExample {
private static final List<String> ALLOWED_PATHS = new ArrayList<>(); static {
// 添加允许访问的路径
ALLOWED_PATHS.add("/path/to/allowed/folder");
ALLOWED_PATHS.add("/path/to/another/allowed/folder");
} public static void main(String[] args) {
// 定义文件路径
String filePath = "/path/to/forbidden/folder/file.txt"; // 检查文件路径是否在允许的范围内
if (!isPathAllowed(filePath)) {
throw new SecurityException("文件路径不合法");
} try (FileInputStream fis = new FileInputStream(filePath);
FileOutputStream fos = new FileOutputStream("/path/to/destination/folder/newfile.txt")) { // 读取并写入文件
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
// 处理异常
e.printStackTrace();
}
} private static boolean isPathAllowed(String path) {
// 检查文件路径是否在允许的范围内
for (String allowedPath : ALLOWED_PATHS) {
if (path.startsWith(allowedPath)) {
return true;
}
}
return false;
}
}
在上面的示例中,ALLOWED_PATHS 列表包含允许访问的文件路径,isPathAllowed() 方法检查给定的文件路径是否在允许的范围内,如果不在范围内,则抛出一个 SecurityException。
在 main() 方法中,检查给定的文件路径是否允许访问,如果通过检查,则使用 FileInputStream 读取文件,并使用 FileOutputStream 写入文件。
例如使用安全的 API,如 Java 的 java.nio.file 包。
Absolute Path Traversal 错误解决的更多相关文章
- File already exists: filesystem '/path/file', transaction svn常见错误解决方法
前言 多人任务基本都会用到SVN,于是提交的时候如果不先更新在提交或者操作顺序不对,会经常出现错误,其中File already exists: filesystem这个就是个常见问题,上网找了半天没 ...
- Atitit.404错误解决标准流程and url汉字中文路径404错误resin4 resin chinese char path 404 err解决
Atitit.404错误解决标准流程and 错误resin4 resin chinese char path 404 err解决 1. #原因解析 1 2. #解决方式 2 3. 输出图片流... 2 ...
- php安装 出现Sorry, I cannot run apxs. ***错误解决方法
# tar zvxf php-5.1.2.tar.gz# cd php-5.1.2# ./configure --prefix=/usr/local/php --with-mysql=/usr/loc ...
- Hadoop - Mac OSX下配置和启动hadoop以及常见错误解决
0. 安装JDK 参考网上教程在OSX下安装jdk 1. 下载及安装hadoop a) 下载地址: http://hadoop.apache.org b) 配置ssh环境 在terminal里面输入: ...
- CAS SSO:汇集配置过程中的错误解决方法
本教程为gevin.me原创文章,转载请注明: CAS SSO:配置过程中的错误解决方法 | Gevin’s Blog 本文将收集在配置CAS SSO遇到的所有错误,希望对大家有帮助,也方便下次搭建的 ...
- ubuntu错误解决。
ubuntu中出现如下错误: W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/main/i ...
- go: GOPATH entry is relative; must be absolute path: "".
安装:vscode-go出现以下提示: go: GOPATH entry is relative; must be absolute path: "".Run 'go help g ...
- No plugin found for prefix 'tomcat' in the current project and in the plugin groups和java.net.BindException: Address already in use: JVM_Bind <null>:8080的错误解决
错误报告:No plugin found for prefix 'tomcat' in the current project and in the plugin groups [org.apache ...
- Windows Unity ARKit发布到IOS相关设置及错误解决
Windows 版Unity安装: 考虑到在虚拟机中运行Unity比较卡,所以采用在Windows Unity上将项目发布好然后再复制到Mac虚拟机中通过XCode进行编译的方式. Unity版本为 ...
- Maven项目错误解决小结
http://blog.csdn.net/typa01_kk/article/details/49185759 Maven项目错误解决小结 注:整理错误,不喜欢为了一个小问题,占篇幅,所以请Ctrl+ ...
随机推荐
- 【当年笔记】Collection集合部分
集合继承关系图 1)Vector 特点:线程安全,消耗偏大 2)ArrayList 特点:基于数组实现,随机访问某个元素效率高.集和头尾之间包括头插入删除操作效率较低,因为插入元素后,其他元素要后移. ...
- python随机数模拟
`#随机数 import random red = range(1,36,1) red_target_list=[] i=1 while i< 6: red_ran_index=random.r ...
- shell 每秒调用某个地址的方法
#!/bin/bash step=1 for (( i = 0; i < 60; i=(i+step) )); do curl http://fn.k6akg6f.bar/chat.php sl ...
- 给自己提个醒,渲染模版引擎handlebars已经足够好用了,不要再到处乱看浪费时间了。
<html><body onload="renderHello()"><div id="target">Loading... ...
- Java基础__02.数据类型
Java中的数据类型 Java是一种强类型的语言,所有的变量都必须要先定义才能使用. Java中的数据类型分为 基本数据类型和引用数据类型. 1.基本数据类型:(8种) 数值类型 整数类型 byte: ...
- 搭建Kubord管理k8s/EKS以及Harbor私有仓库教程
eks首先要去aws后台进行创建,这里不再讲解详细的过程,下面讲解如果通过命令行以及kuboard调度esk服务. 安装docker以及docker-compose yum install docke ...
- js中的this的指向问题
this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上this的最终指向的是那个调用它的对象 this永远指向的是最后调用它的对象,也就是看它执行的时候是谁调 ...
- PyTables文件格式、PyTables 文件支持的数据类型
翻译自 https://www.pytables.org/usersguide/file_format.html,http://www.pytables.org/usersguide/datatype ...
- 连接Oracle 19c出现ORA-28040:没有匹配的验证协议
错误信息:ORA-28040:没有匹配的验证协议处理方法 出现这个原因是因为你的Oracle连接客户端与服务端Oracle的版本不匹配造成的.一般是低版本客户端连接高版本服务端出现. 高版本连接低版本 ...
- redis获取图形验证码
1.在pom文件中先导入生成图像的依赖包 <dependency> <groupId>com.baomidou</groupId> <artifactId&g ...