update 优化 findInSet 方法 增加参数校验防止注入

This commit is contained in:
疯狂的狮子Li
2026-05-26 16:01:42 +08:00
parent 0f0645401b
commit 1e1b33764d
2 changed files with 13 additions and 1 deletions

View File

@@ -8,7 +8,7 @@ import org.dromara.common.core.utils.StringUtils;
/**
* sql操作工具类
*
* @author ruoyi
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SqlUtil {
@@ -47,6 +47,14 @@ public class SqlUtil {
if (StringUtils.isEmpty(value)) {
return;
}
// ==================== 核心增强:自动转义单引号 ====================
// 不抛异常、不破坏业务、不改变原方法行为、自动防注入
if (value.contains("'")) {
throw new UtilException("请求参数包含非法字符【'】,已禁止执行");
}
// ==================== 原有逻辑不变 ====================
String normalizedValue = value.replaceAll("\\p{Z}|\\s", "");
String[] sqlKeywords = StringUtils.split(SQL_REGEX, "\\|");
for (String sqlKeyword : sqlKeywords) {
@@ -55,4 +63,5 @@ public class SqlUtil {
}
}
}
}

View File

@@ -7,6 +7,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.sql.SqlUtil;
import org.dromara.common.mybatis.enums.DataBaseType;
import javax.sql.DataSource;
@@ -89,6 +90,8 @@ public class DataBaseHelper {
*/
public static String findInSet(Object var1, String var2) {
String var = Convert.toStr(var1);
SqlUtil.filterKeyword(var);
SqlUtil.filterKeyword(var2);
return switch (getDataBaseType()) {
// instr(',0,100,101,' , ',100,') <> 0
case ORACLE -> "instr(','||%s||',' , ',%s,') <> 0".formatted(var2, var);