P2-LIKE: 保真层 GeneralPO 值位置单引号转义堵 LIKE 搜索注入
承 P4-4 §坑8.8 + 架构 §四安全第 2 层 LIKE 路径项。GeneralPO 对 String 搜索值裸拼 col like '%值%'(GeneralPO.java:1940)及同形态 5 处(Object 等值 1982/IN 1978/全表模糊 ilike 2075/like 2145/2173)无参数绑定 → 单引号闭合 字面量注入。 修复(保真层最小改动,严格口径): - StringUtil 新增 escapeSqlLiteral:只把 ' 双写为 ''(SQL 标准转义堵注入)、 绝不转义通配符 % _(旧系统通配语义保真)、不补外层引号(拼接形态零变化) - GeneralPO 6 处用户可控值位置各包一层 escapeSqlLiteral,SQL 结构一字未改 - m_IDs(主键)/get_TableName()(标识符,白名单守)非值位置,不在本次范围 - 真参数化(PreparedStatement ?)列 P2 后续热点项,本次只堵值转义注入面 验证:单测 7/7;mvn install 10 模块 + ArchUnit 绿;ParityCli --all 21 实体 全一致 + --privilege 9 正/3 负全过;REST 8091 实测注入负例 200 空结果 不注入不报错(表未被 DROP),a%b/纯 % 仍通配。详见 P2-LIKE注入修复报告.md + 迁移手册 §8.10。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -748,6 +748,38 @@ public class StringUtil {
|
||||
return out.toString();
|
||||
} // TO_STRING
|
||||
|
||||
/**
|
||||
* 值位置单引号转义(仅把 {@code '} 双写为 {@code ''},不加外层引号)。
|
||||
*
|
||||
* <p>用途:保真层 SQL 引擎里值已被 {@code '...'} 包裹、但通配符 {@code %}/{@code _}
|
||||
* 须留在引号外保持 LIKE 通配语义的拼接点(如 {@code col like '%值%'})。语义等价于
|
||||
* {@link #TO_STRING(String, int)} 的单引号转义部分,区别是<b>不再补外层引号</b>,
|
||||
* 因此调用方原有的 {@code '%} / {@code %'} 拼接形态零变化。
|
||||
*
|
||||
* <p><b>刻意不转义 LIKE 通配符 {@code %} 和 {@code _}</b>:旧系统把搜索值里的
|
||||
* {@code %}/{@code _} 当通配符是既有行为,转义它们会改变查询语义、破坏行为保真。
|
||||
* 本方法只堵单引号闭合字面量的注入面,对所有合法值(含中文、空格、含 {@code %} 的
|
||||
* 通配搜索)产物与旧系统逐字节一致。
|
||||
*
|
||||
* @param txt 原始值({@code null} 原样返回 {@code null},由调用方决定如何拼接)
|
||||
* @return 单引号已双写、通配符与其余字符原样保留的值
|
||||
*/
|
||||
public static String escapeSqlLiteral(String txt) {
|
||||
if (txt == null)
|
||||
return null;
|
||||
if (txt.indexOf(QUOTE) < 0)
|
||||
return txt;
|
||||
StringBuilder out = new StringBuilder(txt.length() + 4);
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
char c = txt.charAt(i);
|
||||
if (c == QUOTE)
|
||||
out.append("''");
|
||||
else
|
||||
out.append(c);
|
||||
}
|
||||
return out.toString();
|
||||
} // escapeSqlLiteral
|
||||
|
||||
public static String convertSplitStrToSql(String txt) {
|
||||
if (txt == null || !txt.contains(",")) {
|
||||
return TO_STRING(txt, 0);
|
||||
|
||||
@@ -1938,7 +1938,8 @@ public class GeneralPO implements GeneralPOJO, LogItem, Serializable {
|
||||
processListIn(sb, columnName, (List) obj, true);
|
||||
} else {
|
||||
sb.append(columnName + " like '%"
|
||||
+ obj.toString().trim() + "%'");
|
||||
+ StringUtil.escapeSqlLiteral(obj.toString()
|
||||
.trim()) + "%'");
|
||||
}
|
||||
} else if (clazz == Object.class) {
|
||||
if (obj instanceof Map && ((Map) obj).size() > 0) {
|
||||
@@ -1975,11 +1976,15 @@ public class GeneralPO implements GeneralPOJO, LogItem, Serializable {
|
||||
} else {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append("'").append(subId).append("'");
|
||||
sb.append("'")
|
||||
.append(StringUtil
|
||||
.escapeSqlLiteral(subId))
|
||||
.append("'");
|
||||
}
|
||||
sb.append(") ");
|
||||
} else {
|
||||
sb.append(columnName + " = '" + obj.toString()
|
||||
sb.append(columnName + " = '"
|
||||
+ StringUtil.escapeSqlLiteral(obj.toString())
|
||||
+ "'");
|
||||
}
|
||||
} else if (clazz == List.class && obj instanceof Map
|
||||
@@ -2072,7 +2077,8 @@ public class GeneralPO implements GeneralPOJO, LogItem, Serializable {
|
||||
whereBuffer.append(" OR ");
|
||||
}
|
||||
whereBuffer.append(pre).append(".").append(columnName)
|
||||
.append(" ilike '%").append(fuzzyCondition)
|
||||
.append(" ilike '%")
|
||||
.append(StringUtil.escapeSqlLiteral(fuzzyCondition))
|
||||
.append("%' ");
|
||||
} else if (clazz == Object.class && !p_info.isVirtualColumn(index)) {
|
||||
|
||||
@@ -2143,7 +2149,9 @@ public class GeneralPO implements GeneralPOJO, LogItem, Serializable {
|
||||
}
|
||||
whereBuffer.append(subPre).append(".")
|
||||
.append(combidentifierCol).append(" like '%")
|
||||
.append(fuzzyCondition).append("%' ");
|
||||
.append(StringUtil
|
||||
.escapeSqlLiteral(fuzzyCondition))
|
||||
.append("%' ");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2171,7 +2179,8 @@ public class GeneralPO implements GeneralPOJO, LogItem, Serializable {
|
||||
}
|
||||
whereBuffer.append(subPre).append(".")
|
||||
.append(displayColumnName).append(" like '%")
|
||||
.append(fuzzyCondition).append("%' ");
|
||||
.append(StringUtil.escapeSqlLiteral(fuzzyCondition))
|
||||
.append("%' ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user