fix: 1020-1098-1099

This commit is contained in:
RB 2024-03-04 22:38:15 +08:00
parent 927e349e6e
commit 1396c8a0dc
5 changed files with 31 additions and 21 deletions

View file

@ -11,6 +11,7 @@ import com.rebuild.core.Application;
import com.rebuild.core.BootEnvironmentPostProcessor;
import com.rebuild.core.RebuildException;
import com.rebuild.core.service.PerHourJob;
import com.rebuild.utils.CommonsUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.BooleanUtils;
@ -40,9 +41,7 @@ public class RebuildConfiguration extends KVStorage {
* @return
*/
public static File getFileOfData(String filepath) {
if (filepath != null && filepath.contains("../")) {
throw new SecurityException("Attack path detected : " + filepath);
}
CommonsUtils.checkFilePathAttack(filepath);
String d = get(ConfigurationItem.DataDirectory);
File datadir = null;
@ -77,9 +76,7 @@ public class RebuildConfiguration extends KVStorage {
* @see PerHourJob#doCleanTempFiles()
*/
public static File getFileOfTemp(String filepath) {
if (filepath != null && filepath.contains("../")) {
throw new SecurityException("Attack path detected : " + filepath);
}
CommonsUtils.checkFilePathAttack(filepath);
File temp = getFileOfData("temp");
if (!temp.exists()) {

View file

@ -395,29 +395,29 @@ public class QiniuCloud {
/**
* 读取文件
*
* @param filePath
* @param filepath
* @return
* @throws IOException
* @throws RebuildException If cannot read/download
*/
public static File getStorageFile(String filePath) throws IOException, RebuildException {
File file;
if (filePath.startsWith("http://") || filePath.startsWith("https://")) {
String name = filePath.split("\\?")[0];
public static File getStorageFile(String filepath) throws IOException, RebuildException {
File file = null;
if (filepath.startsWith("http://") || filepath.startsWith("https://")) {
String name = filepath.split("\\?")[0];
name = name.substring(name.lastIndexOf("/") + 1);
file = RebuildConfiguration.getFileOfTemp("down" + System.nanoTime() + "." + name);
OkHttpUtils.readBinary(filePath, file, null);
OkHttpUtils.readBinary(filepath, file, null);
} else if (QiniuCloud.instance().available()) {
String name = parseFileName(filePath);
String name = parseFileName(filepath);
file = RebuildConfiguration.getFileOfTemp("down" + System.nanoTime() + "." + name);
instance().download(filePath, file);
instance().download(filepath, file);
} else {
file = RebuildConfiguration.getFileOfData(filePath);
} else if (filepath.startsWith("rb/") || filepath.startsWith("/rb/")) {
file = RebuildConfiguration.getFileOfData(filepath);
}
if (!file.exists()) throw new RebuildException("Cannot read file : " + filePath);
if (file == null || !file.exists()) throw new RebuildException("Cannot read file : " + filepath);
return file;
}
}

View file

@ -299,4 +299,16 @@ public class CommonsUtils {
int rnd = RandomUtils.nextInt(e);
return rnd < s ? rnd + s : rnd;
}
/**
* @param filepath
* @throws SecurityException
*/
public static void checkFilePathAttack(String filepath) throws SecurityException {
if (filepath == null) return;
if (filepath.contains(".rebuild") || filepath.contains("../")
|| filepath.contains("<") || filepath.contains(">")) {
throw new SecurityException("Attack path detected : " + filepath);
}
}
}

View file

@ -21,6 +21,7 @@ import com.rebuild.core.support.RebuildConfiguration;
import com.rebuild.core.support.i18n.Language;
import com.rebuild.core.support.integration.QiniuCloud;
import com.rebuild.utils.AppUtils;
import com.rebuild.utils.CommonsUtils;
import com.rebuild.utils.MarkdownUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
@ -203,10 +204,10 @@ public class RebuildWebConfigurer implements WebMvcConfigurer, ErrorViewResolver
if (StringUtils.isBlank(errorMsg)) errorMsg = Language.L("系统繁忙,请稍后重试");
error.getModel().put("error_code", errorCode);
error.getModel().put("error_msg", errorMsg);
error.getModel().put("error_msg", CommonsUtils.escapeHtml(errorMsg));
if (ex != null && Application.devMode()) {
error.getModel().put("error_stack", ThrowableUtils.extractStackTrace(ex));
error.getModel().put("error_stack", CommonsUtils.escapeHtml(ThrowableUtils.extractStackTrace(ex)));
}
return error;

View file

@ -268,8 +268,8 @@ public class FileDownloader extends BaseController {
filepath = CodecUtils.urlDecode(filepath);
filepath = filepath.replace("\\", "/");
if (filepath.contains("../")
|| filepath.startsWith("_log/") || filepath.contains("/_log/")
CommonsUtils.checkFilePathAttack(filepath);
if (filepath.startsWith("_log/") || filepath.contains("/_log/")
|| filepath.startsWith("_backups/") || filepath.contains("/_backups/")) {
throw new SecurityException("Attack path detected : " + filepath);
}