perf(WS-D I1): /api/file/list 去函数包列 + sys_doc 函数式部分索引(全表扫→Index Scan)
I1 性能(必修):
- 根因: GET /api/file/list 原查询 WHERE UPPER(tablename)=? AND TRIM(businesskey)=?
AND isactive=1 —— 列被函数包裹(非 sargable),sys_doc 33.8 万行仅 PK 索引
→ 每次附件页渲染 Parallel Seq Scan 全表(amis service 容器每渲必打本端点;
EXPLAIN cost ~18572)。
- 修(FileController.list 查询):
· tablename 列本就全大写(DB 实测 tablename<>upper(tablename) 计数 0),app 侧已
tableName.trim().toUpperCase() → 去 UPPER 改 tablename = ?(sargable)。
· businesskey 不能简单 = trimmed: DB 实测 13000 行存「32 位 UUID + 4 空格 padding
至 36」CHAR 补白遗留(业务 PK 为 character(36)),naive businesskey=trimmed 会
漏列这 13000 行附件 → 保留 trim 语义改 rtrim(businesskey) = ?,配函数式索引使其
sargable。全程 ? 绑定(防注入,无拼接)。
- 索引 DDL: docs/sql/SYS_DOC_INDEX.sql
CREATE INDEX IF NOT EXISTS ix_sys_doc_biz
ON eip3.sys_doc (tablename, rtrim(businesskey)) WHERE isactive = 1;
已应用到 eip4dev(含 COMMENT,幂等)。客户库排切流窗口执行(与现有 docs/sql/* 同放
同风格;P5 Flyway 待办同 SYS_PAGE_SCHEMA.sql)。
- 验证(eip4dev): EXPLAIN Seq Scan(cost 18572) → Index Scan using ix_sys_doc_biz
(cost 8.44, exec 0.038ms); 补白键 OA_NOTICEMANAGE + 横线键 OA_NEWSMANAGE 均命中。
8093 重启含新 jar 后 /api/file/list 两种键风格均正常返附件(含 13000 行补白键场景)。
补测试:
- FileControllerTest #5 注入安全(priority 契约): 恶意 tableName/businessKey 不抛、返空,
ArgumentCaptor 断言走 ? 绑定(SQL 文本为静态参数化语句不含用户串,用户原串作绑定值)。
- TemplateToAmisConverterTest 新增 skip-guard: 表名 ORPHAN_*(SYS_ENTITY 解析不到)→
enrichAttachmentFields 降级守卫生效,附件字段保留原 input-file 不包 service。
- 测试全绿: FileControllerTest 5/5, TemplateToAmisConverterTest 93/93(JDK21)。
M4: buildAttachmentField 加注释说明 service.name 与内层 input-file 同名(DOCUMENT)
仅作布局归位 key,表单值由内层 input-file 绑定,amis 容忍(已浏览器验证)。
I2(已知系统级后续项,本次不修): /api/file/list 及 download 无行级数据权限——任意已认证
用户可枚举任意业务记录附件,与现有 download/flow 同 posture(生产仅认证可访问)。
TODO 注释标于 FileController.list javadoc。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -214,6 +214,10 @@ public class FileController {
|
||||
* 附件按 {@code sys_doc.tablename + businesskey} 关联业务记录(非物理列),查看态须列出已有附件供下载,
|
||||
* 而非空上传按钮。amis {@code service} 组件按本端点拉取,{@code each} 渲染「文件名 (大小KB) + 下载」列表。
|
||||
*
|
||||
* <p>TODO(I2 已知系统级后续项,本任务不修):本端点(及 {@code download} / 流式下载)
|
||||
* 无行级数据权限——任意已认证用户可凭 tableName+businessKey 枚举任意业务记录附件。
|
||||
* 与现有 download/flow 同 posture(生产仅认证可访问)。行级数据权限属系统级后续项。
|
||||
*
|
||||
* @param tableName 业务表名(大小写不敏感,入库前已大写存储)
|
||||
* @param businessKey 业务记录主键(空 → 返回空列表,防全表扫)
|
||||
* @return amis 约定 {@code {status:0, data:{items:[{id,name,size,sizeKb,url}]}}}
|
||||
@@ -228,9 +232,18 @@ public class FileController {
|
||||
List<Map<String, Object>> items = new java.util.ArrayList<>();
|
||||
// 业务键缺失 → 空列表(不查库,防 businesskey 为空时全表附件扫出)
|
||||
if (tableName != null && !tableName.isBlank() && businessKey != null && !businessKey.isBlank()) {
|
||||
// WS-D I1 性能:sys_doc 33.8 万行仅 PK 索引,原 UPPER()/TRIM() 包列 → 非 sargable
|
||||
// → 每次附件页渲染 Parallel Seq Scan 全表(amis service 容器每渲必打本端点)。
|
||||
// 修:tablename 列本就全大写(DB 实测 tablename<>upper(tablename) 计数 0),故 app 侧
|
||||
// tableName.trim().toUpperCase() 后即可直接 tablename = ? 等值匹配(去 UPPER,sargable)。
|
||||
// businesskey 列存 CHAR(36) 补白遗留(DB 实测 13000 行 32 位 UUID + 4 空格 padding 至 36):
|
||||
// 不能简单 businesskey = trimmed(会漏这 13000 行)→ 保留 trim 语义,用 rtrim(businesskey)
|
||||
// 配套函数式部分索引 ix_sys_doc_biz (tablename, rtrim(businesskey)) WHERE isactive=1,
|
||||
// 既 sargable(Index Scan,EXPLAIN cost 18572→8.44)又正确(同时命中补白/不补白键)。
|
||||
// 全程 ? 绑定(防注入,无拼接)。索引 DDL 见 docs/sql/SYS_DOC_INDEX.sql。
|
||||
List<Map<String, Object>> rows = jdbcTemplate.queryForList(
|
||||
"SELECT sys_doc_id, filename, filesize FROM sys_doc "
|
||||
+ "WHERE UPPER(tablename) = ? AND TRIM(businesskey) = ? AND isactive = 1 "
|
||||
+ "WHERE tablename = ? AND rtrim(businesskey) = ? AND isactive = 1 "
|
||||
+ "ORDER BY createtime",
|
||||
tableName.trim().toUpperCase(), businessKey.trim());
|
||||
for (Map<String, Object> r : rows) {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -15,6 +17,7 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.jdbc.core.JdbcOperations;
|
||||
|
||||
@@ -101,4 +104,42 @@ class FileControllerTest {
|
||||
List<Map<String, Object>> items = (List<Map<String, Object>>) data.get("items");
|
||||
assertEquals(1L, items.get(0).get("sizeKb"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("5. 注入安全(priority 契约):恶意 tableName 不抛、不破坏,走 ? 绑定(用户串只作绑定值,不进 SQL 文本)")
|
||||
void listSqlInjectionGoesThroughBindParameters() {
|
||||
String evilTable = "x'; DROP TABLE sys_doc; --";
|
||||
String evilKey = "y' OR '1'='1";
|
||||
// mock 返回空——证明不抛、返空集(anyString 消歧到变长参重载,与生产 String 绑定参一致)
|
||||
when(jdbc.queryForList(anyString(), anyString(), anyString())).thenReturn(List.of());
|
||||
|
||||
// 不抛异常
|
||||
ResponseEntity<Map<String, Object>> resp = controller.list(evilTable, evilKey);
|
||||
|
||||
// 返空 items(status=0,不破坏)
|
||||
Map<String, Object> body = resp.getBody();
|
||||
assertNotNull(body);
|
||||
assertEquals(0, body.get("status"));
|
||||
Map<String, Object> data = (Map<String, Object>) body.get("data");
|
||||
assertTrue(((List<?>) data.get("items")).isEmpty());
|
||||
|
||||
// 捕获实际传给 jdbc 的 SQL 文本 + 绑定参数,证明走 ? 绑定(非字符串拼接)。
|
||||
// 参数捕获用 String 类型(消歧到 queryForList(String, Object...) 变长参重载,与生产调用一致)。
|
||||
ArgumentCaptor<String> sqlCaptor = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> argCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(jdbc).queryForList(sqlCaptor.capture(), argCaptor.capture(), argCaptor.capture());
|
||||
|
||||
String sql = sqlCaptor.getValue();
|
||||
// SQL 文本是静态参数化语句:含 ? 占位、不含任何用户串片段
|
||||
assertTrue(sql.contains("WHERE tablename = ? AND rtrim(businesskey) = ?"),
|
||||
"SQL 应为静态参数化语句: " + sql);
|
||||
assertFalse(sql.contains("DROP TABLE"), "SQL 文本不得含用户注入串: " + sql);
|
||||
assertFalse(sql.contains("OR '1'='1"), "SQL 文本不得含用户注入串: " + sql);
|
||||
assertFalse(sql.contains(evilTable), "SQL 文本不得含原始 tableName: " + sql);
|
||||
|
||||
// 用户原串作为绑定参数传入(tableName 经 trim+大写归一,businessKey 经 trim)
|
||||
List<String> boundArgs = argCaptor.getAllValues();
|
||||
assertEquals(evilTable.trim().toUpperCase(), boundArgs.get(0));
|
||||
assertEquals(evilKey.trim(), boundArgs.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user