Skip to content

Commit

Permalink
修复FileUtil.createTempFile可能导致的漏洞
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed May 15, 2023
1 parent 6896fed commit c33550f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* 【http 】 修复HttpDownloader.downloadFile 方法缺少static问题(issue#I6Z8VU@Gitee)
* 【core 】 修复NumberUtil mul 传入null的string入参报错问题(issue#I70JB3@Gitee)
* 【core 】 修复ZipReader.get调用reset异常问题(issue#3099@Github)
* 【core 】 修复FileUtil.createTempFile可能导致的漏洞(issue#3103@Github)

-------------------------------------------------------------------------------------------------------------
# 5.8.18 (2023-04-27)
Expand Down
4 changes: 3 additions & 1 deletion hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,9 @@ public static File createTempFile(String prefix, String suffix, File dir, boolea
int exceptionsCount = 0;
while (true) {
try {
File file = File.createTempFile(prefix, suffix, mkdir(dir)).getCanonicalFile();
// https://github.com/dromara/hutool/issues/3103
//File file = File.createTempFile(prefix, suffix, mkdir(dir)).getCanonicalFile();
final File file = PathUtil.createTempFile(prefix, suffix, null == dir ? null : dir.toPath()).toFile().getCanonicalFile();
if (isReCreat) {
//noinspection ResultOfMethodCallIgnored
file.delete();
Expand Down
28 changes: 28 additions & 0 deletions hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,34 @@ public static String getName(Path path) {
return path.getFileName().toString();
}

/**
* 创建临时文件<br>
* 创建后的文件名为 prefix[Random].suffix From com.jodd.io.FileUtil
*
* @param prefix 前缀,至少3个字符
* @param suffix 后缀,如果null则使用默认.tmp
* @param dir 临时文件创建的所在目录
* @return 临时文件
* @throws IORuntimeException IO异常
* @since 6.0.0
*/
public static Path createTempFile(final String prefix, final String suffix, final Path dir) throws IORuntimeException {
int exceptionsCount = 0;
while (true) {
try {
if(null == dir){
return Files.createTempFile(prefix, suffix);
}else{
return Files.createTempFile(mkdir(dir), prefix, suffix);
}
} catch (final IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied
if (++exceptionsCount >= 50) {
throw new IORuntimeException(ioex);
}
}
}
}

/**
* 删除文件或空目录,不追踪软链
*
Expand Down

0 comments on commit c33550f

Please sign in to comment.