Skip to content

Commit 321e924

Browse files
committed
Fix directory traversal bug
1 parent 3244520 commit 321e924

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

Dockerfile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ LABEL description="I think therefore I am."
88

99
COPY target/JavaSecLab.jar /work/JavaSecLab.jar
1010

11-
RUN mkdir -p /tmp/upload && chmod -R 777 /tmp/upload \
11+
RUN mkdir -p /tmp/upload && mkdir -p /tmp/static \
12+
&& mkdir -p /tmp/static/api /tmp/static/css /tmp/static/images /tmp/static/js /tmp/static/lib /tmp/static/other /tmp/static/upload \
13+
&& chmod -R 777 /tmp/upload /tmp/static \
1214
&& echo "vul test.jsp" > /tmp/upload/test.jsp \
13-
&& echo "vul test.txt" > /tmp/upload/test.txt
15+
&& echo "vul test.txt" > /tmp/upload/test.txt \
16+
&& echo "test readme.md" > /tmp/static/api/readme.md \
17+
&& echo "test styles.css" > /tmp/static/css/styles.css \
18+
&& echo "test script.js" > /tmp/static/js/script.js \
19+
&& echo "test resource.txt" > /tmp/static/other/resource.txt \
20+
&& echo "test file.txt" > /tmp/static/upload/file.txt
1421

1522
EXPOSE 80
1623

src/main/java/top/whgojp/common/constant/SysConstant.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,36 @@ public class SysConstant {
3737
@Autowired
3838
private ResourceLoader resourceLoader;
3939

40-
@Value("${upload.folder:/tmp/upload}") // 容器内部固定路径,默认值为/tmp/upload
40+
@Value("${folder.upload:/tmp/upload}")
4141
private String uploadFolder;
4242

43+
@Value("${folder.static:/tmp/static}")
4344
private String staticFolder;
4445

4546
public SysConstant(ResourceLoader resourceLoader) {
4647
this.resourceLoader = resourceLoader;
4748
}
4849

49-
5050
@PostConstruct
5151
public void init() throws IOException {
52-
// 获取资源对象
53-
File uploadDir = new File(uploadFolder);
54-
if (!uploadDir.exists()) {
55-
if (!uploadDir.mkdirs()) {
56-
throw new IOException("Failed to create upload directory: " + uploadFolder);
57-
}
58-
}
52+
// 初始化上传目录
53+
initializeDirectory(uploadFolder, "upload");
54+
55+
// 初始化静态资源目录
56+
initializeDirectory(staticFolder, "static");
57+
}
5958

60-
// Resource uploadResource = resourceLoader.getResource("classpath:/static/upload/");
61-
Resource staticResource = resourceLoader.getResource("classpath:/static/");
62-
if (staticResource.exists()) {
63-
try {
64-
this.staticFolder = staticResource.getFile().getPath();
65-
} catch (IOException e) {
66-
this.staticFolder = staticResource.getURL().toString();
67-
}
68-
} else {
69-
throw new IOException("Resource not found!");
59+
/**
60+
* 初始化目录,如果不存在则尝试创建
61+
*
62+
* @param path 目录路径
63+
* @param directoryName 目录名称,用于错误提示
64+
* @throws IOException 如果目录创建失败
65+
*/
66+
private void initializeDirectory(String path, String directoryName) throws IOException {
67+
File dir = new File(path);
68+
if (!dir.exists() && !dir.mkdirs()) {
69+
throw new IOException("Failed to create " + directoryName + " directory: " + path);
7070
}
7171
}
7272

src/main/resources/application.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ mybatis-plus:
5656
# 关闭MP3.0自带的banner
5757
banner: false
5858

59-
upload:
60-
folder: /tmp/upload
59+
folder:
60+
upload: /tmp/upload
61+
static: /tmp/static
6162

6263
rsa:
6364
private:

0 commit comments

Comments
 (0)