diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DictSqlConditionCheckUtil.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DictSqlConditionCheckUtil.java index 47374cbbf..18e45bd71 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DictSqlConditionCheckUtil.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/DictSqlConditionCheckUtil.java @@ -27,6 +27,7 @@ import net.sf.jsqlparser.schema.Column; import java.util.LinkedHashSet; import java.util.Set; +import java.util.regex.Pattern; /** * 前端字典条件 SQL 结构校验工具。 @@ -36,6 +37,12 @@ import java.util.Set; */ public final class DictSqlConditionCheckUtil { + /** + * 条件字段名:字母/数字/下划线组成的标识符,可带反引号或双引号,可带表别名前缀 + */ + private static final Pattern COLUMN_NAME_PATTERN = Pattern.compile( + "[`\"]?[\\p{L}_][\\p{L}\\p{N}_]*[`\"]?(\\.[`\"]?[\\p{L}_][\\p{L}\\p{N}_]*[`\"]?)*"); + private DictSqlConditionCheckUtil() { } @@ -53,6 +60,11 @@ public final class DictSqlConditionCheckUtil { String trimmed = value.trim(); Set conditionFields = new LinkedHashSet<>(); try { + //update-begin---author:scott ---date:20261027 for:【GHSA-jgwm-2xxm-mmm4/GHSA-xmw4-rx44-4jcp】禁止反斜杠,JSqlParser不把\当转义符而MySQL会,两者对字符串边界理解不一致可绕过校验----------- + if (trimmed.indexOf('\\') >= 0) { + throw new IllegalArgumentException("字典过滤条件不允许包含反斜杠"); + } + //update-end---author:scott ---date:20261027 for:【GHSA-jgwm-2xxm-mmm4/GHSA-xmw4-rx44-4jcp】禁止反斜杠,JSqlParser不把\当转义符而MySQL会,两者对字符串边界理解不一致可绕过校验----------- if (containsSqlComment(trimmed)) { throw new IllegalArgumentException("字典过滤条件不允许包含SQL注释"); } @@ -172,6 +184,11 @@ public final class DictSqlConditionCheckUtil { quote = current; continue; } + //update-begin---author:scott ---date:20261027 for:【GHSA-xmw4-rx44-4jcp】MySQL的#也是行注释----------- + if (current == '#') { + return true; + } + //update-end---author:scott ---date:20261027 for:【GHSA-xmw4-rx44-4jcp】MySQL的#也是行注释----------- if (i + 1 < value.length() && ((current == '-' && value.charAt(i + 1) == '-') || (current == '/' && value.charAt(i + 1) == '*'))) { @@ -228,6 +245,11 @@ public final class DictSqlConditionCheckUtil { if (!(expression instanceof Column column)) { return false; } + //update-begin---author:scott ---date:20261027 for:【GHSA-xmw4-rx44-4jcp】字段名只允许普通标识符,防止password#之类绕过敏感字段检查----------- + if (!COLUMN_NAME_PATTERN.matcher(column.getFullyQualifiedName()).matches()) { + return false; + } + //update-end---author:scott ---date:20261027 for:【GHSA-xmw4-rx44-4jcp】字段名只允许普通标识符,防止password#之类绕过敏感字段检查----------- conditionFields.add(column.getFullyQualifiedName()); return true; } diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SensitiveTableCheckUtil.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SensitiveTableCheckUtil.java index 867fdbc67..d6327b070 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SensitiveTableCheckUtil.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/SensitiveTableCheckUtil.java @@ -34,6 +34,12 @@ public final class SensitiveTableCheckUtil { * @param fields 查询字段 */ public static void checkForbiddenFields(String table, String... fields) { + //update-begin---author:scott ---date:20261027 for:【GHSA-g87f-mgx5-wj9c】isEmpty会把字面量null当空直接放行,改为拒绝----------- + if (table != null && "null".equalsIgnoreCase(table.trim())) { + log.error("字典查询表名非法:{}", table); + throw new JeecgSqlInjectionException(SQL_INJECTION_TIP + table); + } + //update-end---author:scott ---date:20261027 for:【GHSA-g87f-mgx5-wj9c】isEmpty会把字面量null当空直接放行,改为拒绝----------- if (oConvertUtils.isEmpty(table) || fields == null || fields.length == 0) { return; } diff --git a/jeecg-boot/jeecg-boot-base-core/src/test/java/org/jeecg/test/security/DictSqlConditionBypassTest.java b/jeecg-boot/jeecg-boot-base-core/src/test/java/org/jeecg/test/security/DictSqlConditionBypassTest.java new file mode 100644 index 000000000..299691623 --- /dev/null +++ b/jeecg-boot/jeecg-boot-base-core/src/test/java/org/jeecg/test/security/DictSqlConditionBypassTest.java @@ -0,0 +1,73 @@ +package org.jeecg.test.security; + +import org.jeecg.common.exception.JeecgSqlInjectionException; +import org.jeecg.common.util.SensitiveTableCheckUtil; +import org.jeecg.common.util.SqlInjectionUtil; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * 字典过滤条件 SQL 注入绕过 — 单元测试 + * + * GHSA-jgwm-2xxm-mmm4:反斜杠导致 JSqlParser 与 MySQL 对字符串边界理解不一致 + * GHSA-xmw4-rx44-4jcp:MySQL 的 # 注释、password# 字段名绕过敏感字段检查 + * GHSA-g87f-mgx5-wj9c:字面量 null 表名被当成空表名放行 + * + * @author scott + * @since 2026-10-27 GHSA-jgwm/GHSA-xmw4/GHSA-g87f 字典条件注入加固 + */ +@ExtendWith(PrintTestResultExtension.class) +public class DictSqlConditionBypassTest { + + private static void check(String filterSql) { + SqlInjectionUtil.filterDictConditionSqlFromRequest("sys_user", filterSql); + } + + @Test + @DisplayName("jgwm:反斜杠闭合引号的 IN 列表被拦截") + void backslashInListBlocked() { + assertThrows(JeecgSqlInjectionException.class, () -> check("id IN ('a\\',') or 1=1#')")); + assertThrows(JeecgSqlInjectionException.class, + () -> check("id IN ('a\\',') or (select ascii(substring(password from 1 for 1)) from sys_user where username=0x61646d696e)>0#')")); + } + + @Test + @DisplayName("jgwm:不带 # 的反斜杠变体同样被拦截") + void backslashWithoutCommentBlocked() { + assertThrows(JeecgSqlInjectionException.class, () -> check("username = 'a\\'' or username = ''")); + } + + @Test + @DisplayName("xmw4:password# 字段名与 # 注释被拦截") + void hashCommentBlocked() { + assertThrows(JeecgSqlInjectionException.class, () -> check("username='admin' and password# like '\nlike \"c%\"#'")); + assertThrows(JeecgSqlInjectionException.class, () -> check("username='admin' # comment")); + } + + @Test + @DisplayName("回归:引号内的 # 与正常条件放行") + void normalConditionAllowed() { + assertDoesNotThrow(() -> check("username = 'a#b'")); + assertDoesNotThrow(() -> check("status = '1' and sex in ('1','2') order by create_time desc")); + assertDoesNotThrow(() -> check("u.username like 'adm%' and `realname` is not null")); + } + + @Test + @DisplayName("回归:直接使用敏感字段仍被拦截") + void sensitiveFieldBlocked() { + assertThrows(JeecgSqlInjectionException.class, () -> check("username='admin' and password like 'c%'")); + } + + @Test + @DisplayName("g87f:字面量 null 表名不再放行") + void literalNullTableBlocked() { + assertThrows(JeecgSqlInjectionException.class, () -> SensitiveTableCheckUtil.checkForbiddenFields("null", "password")); + assertThrows(JeecgSqlInjectionException.class, () -> SensitiveTableCheckUtil.checkForbiddenFields(" NULL ", "password")); + assertDoesNotThrow(() -> SensitiveTableCheckUtil.checkForbiddenFields("", "password")); + assertDoesNotThrow(() -> SensitiveTableCheckUtil.checkForbiddenFields("sys_user", "username")); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/config/firewall/SqlInjection/impl/DictTableWhiteListHandlerImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/config/firewall/SqlInjection/impl/DictTableWhiteListHandlerImpl.java index 451996075..ae8a4cc92 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/config/firewall/SqlInjection/impl/DictTableWhiteListHandlerImpl.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/config/firewall/SqlInjection/impl/DictTableWhiteListHandlerImpl.java @@ -118,6 +118,11 @@ public class DictTableWhiteListHandlerImpl implements IDictTableWhiteListHandler @Override public boolean isPassByDict(String tableName, String... fields) { + //update-begin---author:scott ---date:20261027 for:【GHSA-g87f-mgx5-wj9c】字面量null表名不能当空表名放行----------- + if (tableName != null && "null".equalsIgnoreCase(tableName.trim())) { + return false; + } + //update-end---author:scott ---date:20261027 for:【GHSA-g87f-mgx5-wj9c】字面量null表名不能当空表名放行----------- if (oConvertUtils.isEmpty(tableName)) { return true; } diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java index f076bcfc8..781782cad 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/SysDictMapper.java @@ -200,20 +200,33 @@ public interface SysDictMapper extends BaseMapper { * @param page 分页参数 * @param table 表名(已转义) * @param fields SELECT 字段列表(已转义,逗号分隔) - * @param filterSql WHERE 子句内容(不含 WHERE 关键字,已注入校验) + * @param valueField 回显字段(已转义) + * @param keyValues 回显值,参数绑定 + * @param likeFields 模糊搜索字段(已转义) + * @param likeKeyword 模糊搜索关键词(含%),参数绑定 */ IPage> queryPageTableWithFields(Page> page, @Param("table") String table, @Param("fields") String fields, - @Param("filterSql") String filterSql); + @Param("valueField") String valueField, + @Param("keyValues") List keyValues, + @Param("likeFields") List likeFields, + @Param("likeKeyword") String likeKeyword); /** * 关联记录专用:统计满足条件的记录总数 * * @param table 表名(已转义) - * @param filterSql WHERE 子句内容(不含 WHERE 关键字,已注入校验) + * @param valueField 回显字段(已转义) + * @param keyValues 回显值,参数绑定 + * @param likeFields 模糊搜索字段(已转义) + * @param likeKeyword 模糊搜索关键词(含%),参数绑定 */ - Long countTableWithFields(@Param("table") String table, @Param("filterSql") String filterSql); + Long countTableWithFields(@Param("table") String table, + @Param("valueField") String valueField, + @Param("keyValues") List keyValues, + @Param("likeFields") List likeFields, + @Param("likeKeyword") String likeKeyword); /** * 根据应用id获取字典列表和详情 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml index e68cfedca..58cab195a 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/mapper/xml/SysDictMapper.xml @@ -209,21 +209,37 @@ - + + + diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java index 05c6535b1..9d13540f2 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/service/impl/SysDictServiceImpl.java @@ -939,38 +939,22 @@ public class SysDictServiceImpl extends ServiceImpl impl } String selectFields = String.join(", ", fieldSet); - // ---------- 3. 构造 WHERE 条件 ---------- - List conditionParts = new ArrayList<>(); + // ---------- 3. 构造 WHERE 条件(参数绑定) ---------- + //update-begin---author:scott ---date:20261027 for:【GHSA-jgwm-2xxm-mmm4】keyValues/keyword改为参数绑定,不再拼接SQL字符串(原写法只转义单引号,MySQL下可用反斜杠闭合引号注入)----------- + // 3.1 按 keyValues 精确查询(回显用),-1 保留空项,避免 keyValues="," 时列表为空导致不加条件查出全表 + List keyValueList = oConvertUtils.isNotEmpty(keyValues) ? Arrays.asList(keyValues.split(",", -1)) : null; - //update-begin---author:liusq ---date:2026-05-27 for:【优化】去掉无用参数searchFields、condition----------- - // 3.1 按 keyValues 精确查询(回显用) - if (oConvertUtils.isNotEmpty(keyValues)) { - keyValues = keyValues.replace("'", "''"); - String inValues = "'" + String.join("','", keyValues.split(",")) + "'"; - conditionParts.add(safeValueField + " IN (" + inValues + ")"); - } - //update-end---author:liusq ---date:2026-05-27 for:【优化】去掉无用参数searchFields、condition----------- - - // 3.2 关键词模糊搜索 + // 3.2 关键词模糊搜索字段 + List likeFields = new ArrayList<>(); + String likeKeyword = null; if (oConvertUtils.isNotEmpty(keyword)) { - keyword = keyword.replace("'", "''"); - String[] searchArr = showFields.split(","); - List likeParts = new ArrayList<>(); - for (String sf : searchArr) { + for (String sf : showFields.split(",")) { sf = sf.trim(); if (oConvertUtils.isNotEmpty(sf)) { - String safeSf = SqlInjectionUtil.getSqlInjectField(sf); - likeParts.add(safeSf + " like '%" + keyword + "%'"); + likeFields.add(SqlInjectionUtil.getSqlInjectField(sf)); } } - if (!likeParts.isEmpty()) { - conditionParts.add("(" + String.join(" or ", likeParts) + ")"); - } - } - - String filterSql = conditionParts.isEmpty() ? "" : String.join(" and ", conditionParts); - if (oConvertUtils.isNotEmpty(filterSql)) { - SqlInjectionUtil.filterDictConditionSqlFromRequest(tableName, filterSql); + likeKeyword = likeFields.isEmpty() ? null : "%" + keyword + "%"; } // ---------- 4. 分页查询 ---------- @@ -979,8 +963,10 @@ public class SysDictServiceImpl extends ServiceImpl impl Page> page = new Page<>(current, size); page.setSearchCount(false); // 手动 count,避免 MyBatis Plus 自动 count 与 ${} 语法冲突 - IPage> pageResult = baseMapper.queryPageTableWithFields(page, safeTable, selectFields, filterSql); - long total = baseMapper.countTableWithFields(safeTable, filterSql); + IPage> pageResult = baseMapper.queryPageTableWithFields(page, safeTable, selectFields, + safeValueField, keyValueList, likeFields, likeKeyword); + long total = baseMapper.countTableWithFields(safeTable, safeValueField, keyValueList, likeFields, likeKeyword); + //update-end---author:scott ---date:20261027 for:【GHSA-jgwm-2xxm-mmm4】keyValues/keyword改为参数绑定,不再拼接SQL字符串(原写法只转义单引号,MySQL下可用反斜杠闭合引号注入)----------- // ---------- 5. 组装返回值 ---------- Map result = new HashMap<>(4); diff --git a/jeecgboot-vue3/src/views/demo/jeecg/DictConditionDemo.vue b/jeecgboot-vue3/src/views/demo/jeecg/DictConditionDemo.vue new file mode 100644 index 000000000..72c5dba27 --- /dev/null +++ b/jeecgboot-vue3/src/views/demo/jeecg/DictConditionDemo.vue @@ -0,0 +1,261 @@ + + diff --git a/jeecgboot-vue3/src/views/demo/jeecg/index.vue b/jeecgboot-vue3/src/views/demo/jeecg/index.vue index 4f10c7fef..2dd900747 100644 --- a/jeecgboot-vue3/src/views/demo/jeecg/index.vue +++ b/jeecgboot-vue3/src/views/demo/jeecg/index.vue @@ -9,6 +9,7 @@ + @@ -23,6 +24,7 @@ import ImgTurnPage from './ImgTurnPage.vue'; import JeecgPdfView from './JeecgPdfView.vue'; import JUploadDemo from './JUploadDemo.vue'; + import DictConditionDemo from './DictConditionDemo.vue'; export default defineComponent({ name: 'comp-jeecg-basic', setup() { @@ -36,6 +38,7 @@ ImgTurnPage: ImgTurnPage, JeecgPdfView: JeecgPdfView, JUploadDemo: JUploadDemo, + DictConditionDemo: DictConditionDemo, }; return componentType[activeKey.value]; });