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. mariadb 集群 重启的几种情形

    最近项目中遇到过一次mariadb集群中三台节点有两台挂掉的情况.总结了一下mariadb集群可能遇到的几种情况需要重启. 环境说明: mariadb集群ip: 10.0.101.51 10.0.10 ...

  2. idea主题插件 ->Atom Material File Icons

    Atom Material File Icons    插件名

  3. CNN模型踩坑记录

    刚刚在跑textCNN的模型,加载了字典后,在同样的输入下模型的输出一直在变化,先发现损失函数一直在变化,不停debug之后发现是模型的输出一直在变化,在模型输入一直不变下模型输出不同,最后发现是模型 ...

  4. python33

    Python逻辑运算符 Python语言支持逻辑运算符,以下假设变量 a 为 10, b为 20: 运算符 逻辑表达式 描述 实例 and x and y 布尔"与" - 如果 x ...

  5. ffmpeg 截取视频

    ffmpeg -ss 00:00:03 -t 22 -i output20230301.mp4 output20230301_0.mp4 -ss:开始时间 -t:持续时间 -i:输入 接着是输出

  6. Winform 在高分变率显示器中窗体变模糊配置方式

    我们知道  Winform 前身与 XP 系统 同一时代出生 , 那时候显示器还是LCD 和 大头机 ,显示器普遍  96 DPI  . 随着显示器质量改善,2K 屏, 4K屏普及,DPI 达  19 ...

  7. 已知内存BUF单元开始的区域中存放有一组无符号字节数据,要求将这些数据按从小到大的顺序排列,排序后的数据依然放在原来的存储区中。

    设计要求: 已知内存BUF单元开始的区域中存放有一组无符号字节数据,要求将这些数据按从小到大的顺序排列,排序后的数据依然放在原来的存储区中.(10分) #make_BIN# BUF DB 22,21, ...

  8. Vue中使用model属性

    model属性接收两个参数 类型:{ prop?: string, event?: string } prop 也就是调用该组件的父组件中使用v-model指令绑定的属性 event 对应的是修改pr ...

  9. SSH远程服务器时报错 /bin/bash : Permission denied

    SSH远程服务器时报错 /bin/bash : Permission denied 今日接到用户那边的报障,/bin/bash : Permission denied报错,用户使用的是具有sudo权限 ...

  10. 《Python深度学习》《卷积神经网络的可视化》精读

    对于大多数深度学习模型,模型学到的表示都难以用人类可以理解的方式提取和呈现.但对于卷积神经网络来说,我们可以很容易第提取模型学习到的表示形式,并以此加深对卷积神经网络模型运作原理的理解. 这篇文章的内 ...