feat(stability): perPage 封顶 + statement_timeout + 连接池 5→20
防 perPage=999999 一次物化全表致 OOM/连接耗尽:
- Pagination.clampPerPage(上限 5000,安全护栏非产品限),接入 FlowController/PickerController 两 REST 入口(page 不封)
- application.yml 加 hikari.connection-init-sql=SET statement_timeout='${EIP4_DB_STATEMENT_TIMEOUT:120s}'(dev/local/prod 继承 base,可覆盖/设0关闭)
- maximum-pool-size 5→20(prod 无 profile 继承 base 受益)
验证:68/68 绿(PaginationTest+经真实 executeGet 集成+无回归);statement_timeout 经真实 PG SHOW=2min。见总览 §10.2。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -300,7 +300,8 @@ public class FlowController {
|
||||
return;
|
||||
}
|
||||
int page = parsePositiveInt(map.remove("page"), 1);
|
||||
int perPage = parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE);
|
||||
// perPage 封顶(总览 §10.2):防 perPage=999999 一次物化全表致 OOM/连接耗尽;page 不封(深翻页合法)
|
||||
int perPage = Pagination.clampPerPage(parsePositiveInt(map.remove("perPage"), DEFAULT_PER_PAGE));
|
||||
map.put(POJOCRUDService.PAGE_START, (page - 1) * perPage);
|
||||
map.put(POJOCRUDService.PAGE_SIZE, perPage);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
/**
|
||||
* 分页参数安全护栏(总览 §10.2 稳定性)。
|
||||
*
|
||||
* <p>仅作「安全上限」用途——防止客户端传 {@code perPage=999999} 让引擎一次把全表物化进内存,
|
||||
* 与小连接池叠加即可<em>单请求</em>耗尽连接 / OOM。这<strong>不是</strong>产品级分页上限:
|
||||
* 交互式大数据集应走服务端搜索({@code q});超大导出应走流式通道(未来工作)。
|
||||
*/
|
||||
final class Pagination {
|
||||
|
||||
/** 每页条数硬上限:远超任何真实页大小(amis 默认 10 / picker 20),远低于 OOM 阈值。 */
|
||||
static final int MAX_PER_PAGE = 5000;
|
||||
|
||||
private Pagination() {
|
||||
}
|
||||
|
||||
/** 把每页条数夹到 {@code [1, MAX_PER_PAGE]};用于所有 REST 分页入口。 */
|
||||
static int clampPerPage(int perPage) {
|
||||
if (perPage > MAX_PER_PAGE) {
|
||||
return MAX_PER_PAGE;
|
||||
}
|
||||
return Math.max(perPage, 1);
|
||||
}
|
||||
}
|
||||
@@ -174,8 +174,10 @@ public class PickerController {
|
||||
: buildCtx(asUser, roles, asDepart, asOrg);
|
||||
|
||||
Map<String, Object> cond = new HashMap<>();
|
||||
cond.put(POJOCRUDService.PAGE_START, (page - 1) * perPage);
|
||||
cond.put(POJOCRUDService.PAGE_SIZE, perPage);
|
||||
// perPage 封顶(总览 §10.2):防客户端传超大 perPage 一次拉全表致 OOM/连接耗尽
|
||||
int safePerPage = Pagination.clampPerPage(perPage);
|
||||
cond.put(POJOCRUDService.PAGE_START, (page - 1) * safePerPage);
|
||||
cond.put(POJOCRUDService.PAGE_SIZE, safePerPage);
|
||||
// 模糊搜索:仅当有关键字且存在可读标识列时下推 LIKE。q 经转义后进引擎(引擎对 String 条件裸拼
|
||||
// col like '%值%',转义单引号与 LIKE 通配符,杜绝注入与通配符逃逸)。标识列名本身来自元数据,天然合法。
|
||||
if (q != null && !q.isBlank() && labelColumn != null) {
|
||||
|
||||
@@ -616,6 +616,26 @@ class FlowControllerTest {
|
||||
assertEquals("放假", sent.get("TITLE"), "其它 query 参数仍作过滤条件保留");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("14. perPage 超大封顶: 防一次拉全表致 OOM/连接耗尽(总览 §10.2)")
|
||||
void clampsExcessivePerPage() {
|
||||
String flowId = "PM_PROJECT_SEARCH";
|
||||
FlowExcuteService engine = mock(FlowExcuteService.class);
|
||||
when(engine.excute(any(), any(), any())).thenReturn(new HashMap<>());
|
||||
FlowController controller = new FlowController(engine, resolverReturning(flowId, synthesized(flowId)));
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("perPage", "999999"); // 恶意/失误的超大每页
|
||||
controller.executeGet(flowId, params);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, Object>> mapCap = ArgumentCaptor.forClass(Map.class);
|
||||
org.mockito.Mockito.verify(engine).excute(mapCap.capture(), any(Properties.class), eq(flowId));
|
||||
Map<String, Object> sent = mapCap.getValue();
|
||||
assertEquals(Pagination.MAX_PER_PAGE, sent.get(POJOCRUDService.PAGE_SIZE),
|
||||
"perPage 超上限应被夹到 MAX_PER_PAGE,不得原样下传引擎");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("13. 分页缺省: 只给 page,perPage 回落 amis 默认 10;page 缺省 1")
|
||||
void pagingDefaults() {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/** 分页护栏单测(总览 §10.2):封顶 perPage,挡住一次拉全表的 OOM/DoS 向量。 */
|
||||
class PaginationTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("超大 perPage 夹到上限")
|
||||
void clampsExcessive() {
|
||||
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(999999));
|
||||
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Integer.MAX_VALUE));
|
||||
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Pagination.MAX_PER_PAGE + 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("正常与边界值原样保留")
|
||||
void keepsNormal() {
|
||||
assertEquals(10, Pagination.clampPerPage(10));
|
||||
assertEquals(20, Pagination.clampPerPage(20));
|
||||
assertEquals(Pagination.MAX_PER_PAGE, Pagination.clampPerPage(Pagination.MAX_PER_PAGE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("非正数夹到 1,避免 PAGE_SIZE<=0 的退化语义")
|
||||
void clampsNonPositive() {
|
||||
assertEquals(1, Pagination.clampPerPage(0));
|
||||
assertEquals(1, Pagination.clampPerPage(-1));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user