Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用于检测slip注入的checkSlip函数存在安全缺陷 #3140

Closed
weiweiwei9811 opened this issue Jun 9, 2023 · 1 comment
Closed

用于检测slip注入的checkSlip函数存在安全缺陷 #3140

weiweiwei9811 opened this issue Jun 9, 2023 · 1 comment
Labels

Comments

@weiweiwei9811
Copy link

描述:

public static File checkSlip(File parentFile, File file) throws IllegalArgumentException {
if (null != parentFile && null != file) {
String parentCanonicalPath;
String canonicalPath;
try {
parentCanonicalPath = parentFile.getCanonicalPath();
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
// issue#I4CWMO@Gitee
// getCanonicalPath有时会抛出奇怪的IO异常,此时忽略异常,使用AbsolutePath判断。
parentCanonicalPath = parentFile.getAbsolutePath();
canonicalPath = file.getAbsolutePath();
}
if (false == canonicalPath.startsWith(parentCanonicalPath)) {
throw new IllegalArgumentException("New file is outside of the parent dir: " + file.getName());
}
}
return file;
}

用于检测slip注入的checkSlip函数存在安全缺陷,在检测位于与父目录同级且目录名以父目录名开头(eg: /parentFile*/)的文件时不会抛出异常。

假设parentFile路径为/home/safe,file路径为/home/safe/../safe_bak/filename时(如解压zip文件时拼接解压路径和文件名会出现)

file路径在经过File.getCanonicalPath()处理后,会变成/home/safe_bak/filename,然后再通过String.startsWith()与parentFile对比。

但因为都是以/home/safe开头,函数会返回True也就没有抛出异常,导致安全目录外的文件被操作,存在安全隐患。

修复建议:

使用 java.nio.files.Path.startsWith() 来替代原来的比较,该函数将对路径进行比较,而不是子字符串.
如果继续使用String.startWith()来比较路径, 需要确保被比较的路径以File.separator结尾, 如 String.startsWith(parentCanonicalPath+ File.separator).

类似问题参考:

88250/symphony#76
lukashinsch/spring-boot-actuator-logview#33

@looly looly added the bug label Jun 9, 2023
@looly looly closed this as completed Jun 9, 2023
@looly
Copy link
Member

looly commented Jun 9, 2023

5.8.20和6.0.0-M4修复。邮件已回复。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants