Absolute Path Traversal (APT) 是一种常见的安全漏洞,攻击者可以通过该漏洞访问应用程序的文件系统中的文件,
包括敏感信息,从而可能导致应用程序遭受攻击。

一、使用专门的文件访问库,例如 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 错误解决的更多相关文章

  1. File already exists: filesystem '/path/file', transaction svn常见错误解决方法

    前言 多人任务基本都会用到SVN,于是提交的时候如果不先更新在提交或者操作顺序不对,会经常出现错误,其中File already exists: filesystem这个就是个常见问题,上网找了半天没 ...

  2. 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 ...

  3. 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 ...

  4. Hadoop - Mac OSX下配置和启动hadoop以及常见错误解决

    0. 安装JDK 参考网上教程在OSX下安装jdk 1. 下载及安装hadoop a) 下载地址: http://hadoop.apache.org b) 配置ssh环境 在terminal里面输入: ...

  5. CAS SSO:汇集配置过程中的错误解决方法

    本教程为gevin.me原创文章,转载请注明: CAS SSO:配置过程中的错误解决方法 | Gevin’s Blog 本文将收集在配置CAS SSO遇到的所有错误,希望对大家有帮助,也方便下次搭建的 ...

  6. ubuntu错误解决。

    ubuntu中出现如下错误: W: Failed to fetch http://cn.archive.ubuntu.com/ubuntu/dists/precise-backports/main/i ...

  7. go: GOPATH entry is relative; must be absolute path: "".

    安装:vscode-go出现以下提示: go: GOPATH entry is relative; must be absolute path: "".Run 'go help g ...

  8. 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 ...

  9. Windows Unity ARKit发布到IOS相关设置及错误解决

    Windows 版Unity安装: 考虑到在虚拟机中运行Unity比较卡,所以采用在Windows Unity上将项目发布好然后再复制到Mac虚拟机中通过XCode进行编译的方式. Unity版本为 ...

  10. Maven项目错误解决小结

    http://blog.csdn.net/typa01_kk/article/details/49185759 Maven项目错误解决小结 注:整理错误,不喜欢为了一个小问题,占篇幅,所以请Ctrl+ ...

随机推荐

  1. 日志分析查看—— cat+grep+awk+uniq+sort+wc+join

    有个统计日志信息的需求,下面是使用到的命令 //按 \t 对文件每一行进行切割,正则匹配第二列为32896时,输出第一列:再进行排序并去重,最后统计行数 cat file.log|awk -F '\t ...

  2. Android版本历史

    版本 Code Name 代号 API 日期 NDK 1.0 Astro 铁臂阿童木  1     1.1 Bender, Petit Four 发条机器人 2     1.5 Cupcake  纸杯 ...

  3. linux下进程和线程的区别和联系

    进程用fork()或者vfork()生成(vfork是专门为了加载其他程序的子程序而优化的,随着fork()的优化,vfork已经被优化) fork()生成的子进程与父进程共享代码区内存,对于其他内存 ...

  4. Hadoop 设置静态IP、关闭防火墙

    设置静态IP [root@localhost ~]# cd /etc[root@localhost etc]# cd sysconfig[root@localhost sysconfig]# cd n ...

  5. Java Swing 禁止黏贴动作

    碰到一个需求,不让复制黏贴. 可考的方法有:1 重写JTextField paste 函数    2 删除组件ActionMap中与CTRL + V按键相关的操作 因为JTextField 已经被封装 ...

  6. Qt中跨进程Socket通信以及socket跨线程通信

    一 QTcpServer 创建流程 创建套接字服务器 QTcpServer 对象, 通过 QTcpServer 对象设置监听,即:QTcpServer::listen() 基于 QTcpServer: ...

  7. docker compose设置不同容器间通信

    docker compose新启动了一个容器,这个时候怎么去连接到其他容器呢,去容器里面ping发现不通.一般来说是因为和其他容器没有在一个网络环境里面.首先用命令查看一下当前存在哪些网络环境. 使用 ...

  8. vue使用websoket

    参考链接:https://www.cnblogs.com/qisi007/p/10213886.html export default { name: "realdetail", ...

  9. 使用Github或Gitlab的Webhooks实现代码自动更新部署(Ubuntu20.04)

    1.安装ssh服务root@Ubuntu:~# apt-get install openssh-server 2.部署phproot@Ubuntu:~# add-apt-repository ppa: ...

  10. 【Python】Python 技巧集锦(长期更新)

    [Basic] 『List Comprehensions』 『Python 中 map(), filter(), reduce() 和 zip() 函数的用法』 『Python 中关于下划线 '_'  ...