fix(flow): 草稿保存关严格XSD校验(bpmn-js默认命名空间输出)+字节级DOCTYPE守卫保XXE
阶段2 live 验收阻断 bug:UI 保存草稿恒 422。根因=T2.1 评审 I-1 我把 enableSafeBpmnXml 从
false 改 true 时,convertToBpmnModel(source, validateSchema=true, ...) 的 validateSchema=true
开启严格 XSD 校验;bpmn-js saveXML 用默认命名空间(无前缀 definitions/process)重序列化,Flowable
严格 Xerces XSD 校验拒之(cvc-complex-type.2.4.a: element {MODEL}process ... expected)——bpmn-js
面向 BPMN 模型非字节级 XSD 合规,这是已知固有不兼容;原始 source(带前缀)故能过。
架构:草稿是可视化工具编辑的半成品,不该做完整 XSD 校验(完整校验属部署门 T3.1 ProcessValidator,
在解析后模型上跑,非 XSD)。修法:
1. parseGuard 的 convertToBpmnModel(..., true, true) → (..., false, true)(validateSchema=false)。
仍解析(非良构 XML 抛→422),不做严格 XSD。
2. XXE 不只靠此:解析前加字节级 DOCTYPE 拒绝守卫(移植 platform-report ReportXmlGuard
hasDoctypeInBytes/hasDoctype 模式,含 BOM/UTF-16 编码识别,就地复刻不引 platform-* 依赖),
与 enableSafeBpmnXml=true 双保险防外部实体注入。
测试(TDD 先红后绿):ProcessModelControllerTest 16→19——①bpmn-js 默认命名空间输出(BPMNDiagram
先于 process,触发同族 cvc-complex-type.2.4.a)改前 422 改后 200 落库(回归锚点,改前实测必红);
②含 <!DOCTYPE 声明的 XML → 422(XXE 守卫);③非良构 XML(标签不匹配)→ 关 XSD 后仍 422。
flowable-service 全模块 92 测试绿,BUILD SUCCESS,既有 16 测试零改动。
附带排查(T3.1 deploy 路径同问题,不在本 task 范围):Flowable 6.8.1 BpmnParse 构造器
validateSchema 默认 iconst_1=true,BpmnDeployer 把它传给 convertToBpmnModel → createDeployment()
.addString(bpmnJsXml).deploy() 会被同一 cvc-complex-type.2.4.a 咬。escape hatch:DeploymentBuilder
有 disableSchemaValidation()(仅跳 XSD)。T3.1 应:parse(validateSchema=false)→跑 ProcessValidator
(语义校验,计划已设计)→ createDeployment().disableSchemaValidation().addString().deploy()。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -42,9 +42,11 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
|||||||
* <p><b>存量隔离</b>:本控制器建的草稿一律 {@code setCategory("eip4")},{@link #list()} 只查该分类,
|
* <p><b>存量隔离</b>:本控制器建的草稿一律 {@code setCategory("eip4")},{@link #list()} 只查该分类,
|
||||||
* 库里 143 条老 shape act_re_model(旧 metaInfo + 可能悬空 editorSource)不进 Modeler 列表、避免打不开。</p>
|
* 库里 143 条老 shape act_re_model(旧 metaInfo + 可能悬空 editorSource)不进 Modeler 列表、避免打不开。</p>
|
||||||
*
|
*
|
||||||
* <p>写入前 XML 一律过 {@link BpmnXMLConverter} 解析守卫(validateSchema=true + enableSafeBpmnXml=true,
|
* <p>写入前 XML 一律过解析守卫({@link #parseGuard}):①字节级拒 DOCTYPE(XXE 门);②{@link BpmnXMLConverter}
|
||||||
* 后者关外部 DTD/实体解析,防 XXE/SSRF):坏 XML 返 422 且绝不落库;422 只回通用文案,原始解析原因仅进日志。
|
* 以 {@code validateSchema=false}(草稿是半成品,不做严格 XSD——bpmn-js 默认命名空间输出与严格 XSD 已知固有不兼容,
|
||||||
* 守卫异常在本控制器就地 catch 转 422,不逸出到 {@code FlowExceptionHandler}(那里 FlowableException→400,语义不符)。
|
* 完整校验属部署门)+ {@code enableSafeBpmnXml=true}(加固解析器,双保险防外部实体)解析。坏 XML 返 422 且绝不落库;
|
||||||
|
* 422 只回通用文案,原始解析原因仅进日志。守卫异常就地 catch 转 422,不逸出到 {@code FlowExceptionHandler}
|
||||||
|
* (那里 FlowableException→400,语义不符)。
|
||||||
* {@code from-procdef} 复用「按 procDefId 读部署 BPMN」逻辑(本控制器自带 {@link #readProcessModel} 小方法,
|
* {@code from-procdef} 复用「按 procDefId 读部署 BPMN」逻辑(本控制器自带 {@link #readProcessModel} 小方法,
|
||||||
* 零改动 ProcessDiagramController)→ {@link BpmnIdSanitizer#sanitize} 净化中文 id → 建草稿,
|
* 零改动 ProcessDiagramController)→ {@link BpmnIdSanitizer#sanitize} 净化中文 id → 建草稿,
|
||||||
* 原→净化映射连同源 procDefId/原 key 写进 metaInfo,供发布逆向还原。</p>
|
* 原→净化映射连同源 procDefId/原 key 写进 metaInfo,供发布逆向还原。</p>
|
||||||
@@ -62,6 +64,9 @@ public class ProcessModelController {
|
|||||||
/** EIP4 草稿分类:建草稿时打此标,list() 只查此分类,隔离库里 143 条老 shape 存量 act_re_model。 */
|
/** EIP4 草稿分类:建草稿时打此标,list() 只查此分类,隔离库里 143 条老 shape 存量 act_re_model。 */
|
||||||
static final String EIP4_CATEGORY = "eip4";
|
static final String EIP4_CATEGORY = "eip4";
|
||||||
|
|
||||||
|
/** DOCTYPE 门 prolog 扫描字节上限(BOM/XML 声明/注释/DOCTYPE 都在最前)。 */
|
||||||
|
private static final int PROLOG_SCAN_BYTES = 8192;
|
||||||
|
|
||||||
private final RepositoryService repositoryService;
|
private final RepositoryService repositoryService;
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
@@ -229,16 +234,27 @@ public class ProcessModelController {
|
|||||||
* BPMN 解析守卫:过则返回 {@code null}(放行),坏 XML 返回 422 ResponseEntity({status:1,msg})。
|
* BPMN 解析守卫:过则返回 {@code null}(放行),坏 XML 返回 422 ResponseEntity({status:1,msg})。
|
||||||
* 就地 catch 一切异常,不让引擎 XMLException/FlowableException 逸出到全局 advice(那里映射成 400,语义不符)。
|
* 就地 catch 一切异常,不让引擎 XMLException/FlowableException 逸出到全局 advice(那里映射成 400,语义不符)。
|
||||||
*
|
*
|
||||||
* <p>{@code convertToBpmnModel(source, validateSchema, enableSafeBpmnXml)} 两布尔参:
|
* <p><b>两道关</b>:①解析前字节级 {@link #hasDoctypeInBytes} 拒 DOCTYPE 声明(XXE 兜底,不依赖 flowable
|
||||||
* {@code validateSchema=true} 走 BPMN 2.0 XSD 校验(拒非法结构);
|
* 内部 {@code supportDTD=false} 实现);②{@code convertToBpmnModel(source, validateSchema, enableSafeBpmnXml)}
|
||||||
* {@code enableSafeBpmnXml=true} 启用 flowable 加固解析器(禁外部 DTD/参数实体/DOCTYPE),
|
* 解析——{@code validateSchema=false}:草稿是可视化工具编辑的半成品,<b>不做</b>严格 BPMN 2.0 XSD 校验。
|
||||||
* 关掉 XXE/SSRF 面——校验路径也走加固解析器,不给外部实体注入留口。</p>
|
* bpmn-js {@code saveXML} 用默认命名空间(无前缀 definitions/process)重序列化,Flowable 严格 Xerces XSD 校验
|
||||||
|
* 会拒({@code cvc-complex-type.2.4.a})——这是 bpmn-js(面向 BPMN 模型,非字节级 XSD 合规)与严格 XSD 的
|
||||||
|
* <b>已知固有不兼容</b>;完整校验属部署门(T3.1 {@code ProcessValidator} 在解析后的模型上跑,非 XSD)。
|
||||||
|
* {@code validateSchema=false} 仍解析(非良构 XML 抛→422);{@code enableSafeBpmnXml=true} 保加固解析器
|
||||||
|
* (禁外部 DTD/参数实体/DOCTYPE),双保险关 XXE/SSRF。</p>
|
||||||
*
|
*
|
||||||
* <p>422 只回通用文案;原始解析原因(可能含内部路径/实体细节)仅进 {@code log.warn},不回显给调用方。</p>
|
* <p>422 只回通用文案;原始解析原因(可能含内部路径/实体细节)仅进 {@code log.warn},不回显给调用方。</p>
|
||||||
*/
|
*/
|
||||||
private ResponseEntity<Map<String, Object>> parseGuard(String xml) {
|
private ResponseEntity<Map<String, Object>> parseGuard(String xml) {
|
||||||
|
// ① XXE 兜底:解析前字节级拒 DOCTYPE。按 StringStreamSource 喂解析器的 UTF-8 字节视角扫描(含 BOM/UTF-16
|
||||||
|
// 识别),消除「扫描端 vs 解析端编码错位」绕过。null XML 同走 422(与解析必抛等价)。
|
||||||
|
if (xml == null || hasDoctypeInBytes(xml.getBytes(StandardCharsets.UTF_8))) {
|
||||||
|
log.warn("[流程草稿] BPMN 拒绝: {}", xml == null ? "空 XML" : "含 DOCTYPE 声明(XXE 防御)");
|
||||||
|
return status(HttpStatus.UNPROCESSABLE_ENTITY, "BPMN 解析失败");
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
new BpmnXMLConverter().convertToBpmnModel(new StringStreamSource(xml), true, true);
|
// ② validateSchema=false(不做严格 XSD)+ enableSafeBpmnXml=true(加固解析器);详见方法 javadoc。
|
||||||
|
new BpmnXMLConverter().convertToBpmnModel(new StringStreamSource(xml), false, true);
|
||||||
return null;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("[流程草稿] BPMN 解析守卫拒绝: {}", e.toString());
|
log.warn("[流程草稿] BPMN 解析守卫拒绝: {}", e.toString());
|
||||||
@@ -246,6 +262,84 @@ public class ProcessModelController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 字节级 DOCTYPE 检测(XXE 门):按 BOM / 无 BOM 的 UTF-16 空字节特征识别编码后解码 prolog,再走
|
||||||
|
* {@link #hasDoctype}。消除「扫描端与解析端编码视角不一致」的绕过(UTF-8 BOM / UTF-16 会让纯字节扫描漏判
|
||||||
|
* {@code <!DOCTYPE},而 xerces 仍按 BOM 正确解析触发 XXE)。移植 platform-report {@code ReportXmlGuard}
|
||||||
|
* (flowable-service 不依赖 platform-* 模块,故就地复刻,不引依赖)。
|
||||||
|
*/
|
||||||
|
static boolean hasDoctypeInBytes(byte[] raw) {
|
||||||
|
if (raw == null || raw.length == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
java.nio.charset.Charset cs;
|
||||||
|
int off = 0;
|
||||||
|
int u0 = raw[0] & 0xFF;
|
||||||
|
int u1 = raw.length > 1 ? raw[1] & 0xFF : -1;
|
||||||
|
int u2 = raw.length > 2 ? raw[2] & 0xFF : -1;
|
||||||
|
if (u0 == 0xEF && u1 == 0xBB && u2 == 0xBF) {
|
||||||
|
cs = StandardCharsets.UTF_8;
|
||||||
|
off = 3; // UTF-8 BOM
|
||||||
|
} else if (u0 == 0xFF && u1 == 0xFE) {
|
||||||
|
cs = StandardCharsets.UTF_16LE;
|
||||||
|
off = 2; // UTF-16LE BOM
|
||||||
|
} else if (u0 == 0xFE && u1 == 0xFF) {
|
||||||
|
cs = StandardCharsets.UTF_16BE;
|
||||||
|
off = 2; // UTF-16BE BOM
|
||||||
|
} else if (u0 == '<' && u1 == 0x00) {
|
||||||
|
cs = StandardCharsets.UTF_16LE; // 无 BOM UTF-16LE(ASCII 起始字符夹空字节)
|
||||||
|
} else if (u0 == 0x00 && u1 == '<') {
|
||||||
|
cs = StandardCharsets.UTF_16BE; // 无 BOM UTF-16BE
|
||||||
|
} else {
|
||||||
|
cs = StandardCharsets.ISO_8859_1; // UTF-8 / Latin1:prolog 为 ASCII,字节级保真扫描
|
||||||
|
}
|
||||||
|
int scanLen = Math.min(raw.length - off, PROLOG_SCAN_BYTES);
|
||||||
|
return hasDoctype(new String(raw, off, scanLen, cs));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文档 prolog(根元素之前)是否含 DOCTYPE 声明(大小写不敏感)。正确跳过 XML 声明 {@code <?xml?>} 与注释
|
||||||
|
* {@code <!-- -->},命中根元素起始标签即停(元素内容里的字面 {@code <!DOCTYPE} 文本不算)。
|
||||||
|
*/
|
||||||
|
static boolean hasDoctype(String xml) {
|
||||||
|
if (xml == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
int n = xml.length();
|
||||||
|
while (i < n) {
|
||||||
|
char c = xml.charAt(i);
|
||||||
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '') {
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (c != '<') {
|
||||||
|
return false; // prolog 里非标记的文本 → 视为无 DOCTYPE
|
||||||
|
}
|
||||||
|
if (xml.regionMatches(true, i, "<!DOCTYPE", 0, 9)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (xml.startsWith("<!--", i)) { // 注释:整段跳过
|
||||||
|
int end = xml.indexOf("-->", i + 4);
|
||||||
|
if (end < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i = end + 3;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (xml.startsWith("<?", i)) { // XML 声明 / 处理指令:整段跳过
|
||||||
|
int end = xml.indexOf("?>", i + 2);
|
||||||
|
if (end < 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
i = end + 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return false; // 到达根元素起始标签,prolog 结束,无 DOCTYPE
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** 读流程定义部署 BPMN XML(getProcessModel 返回资源流)。读失败/为空返回 null(前端降级)。 */
|
/** 读流程定义部署 BPMN XML(getProcessModel 返回资源流)。读失败/为空返回 null(前端降级)。 */
|
||||||
private String readProcessModel(String procDefId) {
|
private String readProcessModel(String procDefId) {
|
||||||
try (InputStream is = repositoryService.getProcessModel(procDefId)) {
|
try (InputStream is = repositoryService.getProcessModel(procDefId)) {
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ class ProcessModelControllerTest {
|
|||||||
|
|
||||||
MockMvc mvc;
|
MockMvc mvc;
|
||||||
|
|
||||||
// 最小 schema 合法 BPMN(default ns=BPMN20 MODEL),供 convertToBpmnModel(validateSchema=true) 过门。
|
// 最小 schema 合法 BPMN(default ns=BPMN20 MODEL)。
|
||||||
private static final String VALID_BPMN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
private static final String VALID_BPMN = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||||
+ "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" targetNamespace=\"http://eip4\">"
|
+ "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" targetNamespace=\"http://eip4\">"
|
||||||
+ "<process id=\"p1\" name=\"p1\">"
|
+ "<process id=\"p1\" name=\"p1\">"
|
||||||
@@ -58,6 +58,31 @@ class ProcessModelControllerTest {
|
|||||||
+ "<endEvent id=\"end\"/>"
|
+ "<endEvent id=\"end\"/>"
|
||||||
+ "</process></definitions>";
|
+ "</process></definitions>";
|
||||||
|
|
||||||
|
// bpmn-js saveXML 风格:默认命名空间(无前缀 definitions/process)+ 带前缀 BPMNDI 布局段。
|
||||||
|
// 此处 BPMNDiagram 排在 process 之前(触发严格 XSD 的 cvc-complex-type.2.4.a 序列违例——definitions 内容
|
||||||
|
// 模型要求 rootElement* 在 BPMNDiagram* 之前)——模拟 bpmn-js 输出被严格 XSD 拒的真实故障族。
|
||||||
|
// 草稿保存关 validateSchema 后应良构解析通过 → 200。
|
||||||
|
private static final String BPMN_JS_STYLE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||||
|
+ "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\""
|
||||||
|
+ " xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\""
|
||||||
|
+ " xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\""
|
||||||
|
+ " xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\""
|
||||||
|
+ " id=\"Definitions_1\" targetNamespace=\"http://bpmn.io/schema/bpmn\">"
|
||||||
|
+ "<bpmndi:BPMNDiagram id=\"BPMNDiagram_1\">"
|
||||||
|
+ "<bpmndi:BPMNPlane id=\"BPMNPlane_1\" bpmnElement=\"Process_1\">"
|
||||||
|
+ "<bpmndi:BPMNShape id=\"StartEvent_1_di\" bpmnElement=\"StartEvent_1\">"
|
||||||
|
+ "<dc:Bounds x=\"152\" y=\"82\" width=\"36\" height=\"36\"/>"
|
||||||
|
+ "</bpmndi:BPMNShape></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>"
|
||||||
|
+ "<process id=\"Process_1\" isExecutable=\"true\">"
|
||||||
|
+ "<startEvent id=\"StartEvent_1\" name=\"开始\"/>"
|
||||||
|
+ "</process></definitions>";
|
||||||
|
|
||||||
|
// 含 DOCTYPE 声明(XXE 面):正文良构、关严格 XSD 后本可解析,但字节级 DOCTYPE 守卫必须拒。
|
||||||
|
private static final String DOCTYPE_XML = "<?xml version=\"1.0\"?>"
|
||||||
|
+ "<!DOCTYPE definitions [<!ENTITY xxe \"payload\">]>"
|
||||||
|
+ "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\">"
|
||||||
|
+ "<process id=\"p1\"><startEvent id=\"s\"/></process></definitions>";
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
ProcessModelController controller = new ProcessModelController(repositoryService);
|
ProcessModelController controller = new ProcessModelController(repositoryService);
|
||||||
@@ -178,6 +203,47 @@ class ProcessModelControllerTest {
|
|||||||
verify(repositoryService, never()).addModelEditorSource(anyString(), any());
|
verify(repositoryService, never()).addModelEditorSource(anyString(), any());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 建草稿_bpmnJs默认命名空间输出_关严格XSD后200落库() throws Exception {
|
||||||
|
// 回归锚点:bpmn-js saveXML 默认命名空间输出被严格 XSD 拒(改前必红=422)。草稿保存不该做完整 XSD 校验
|
||||||
|
// (完整校验属部署时 ProcessValidator,非 XSD)→ 关 validateSchema 后良构解析通过 → 200 建草稿成功。
|
||||||
|
Model m = Mockito.mock(Model.class);
|
||||||
|
when(m.getId()).thenReturn("draft-js");
|
||||||
|
when(repositoryService.newModel()).thenReturn(m);
|
||||||
|
|
||||||
|
String body = "{\"name\":\"新流程\",\"key\":\"newproc\",\"bpmnXml\":" + jsonString(BPMN_JS_STYLE) + "}";
|
||||||
|
mvc.perform(post("/api/flow/models").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(jsonPath("$.modelId").value("draft-js"));
|
||||||
|
|
||||||
|
verify(repositoryService).saveModel(m);
|
||||||
|
verify(repositoryService).addModelEditorSource(eq("draft-js"), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 建草稿_含DOCTYPE声明_字节级XXE守卫返422不落库() throws Exception {
|
||||||
|
// 正文良构(关 XSD 后本可解析),但 DOCTYPE 声明是 XXE 面 → 解析前字节级守卫拒,不依赖 flowable 内部实现。
|
||||||
|
String body = "{\"name\":\"x\",\"key\":\"k\",\"bpmnXml\":" + jsonString(DOCTYPE_XML) + "}";
|
||||||
|
mvc.perform(post("/api/flow/models").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||||
|
.andExpect(status().isUnprocessableEntity())
|
||||||
|
.andExpect(jsonPath("$.status").value(1));
|
||||||
|
|
||||||
|
verify(repositoryService, never()).saveModel(any());
|
||||||
|
verify(repositoryService, never()).addModelEditorSource(anyString(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void 建草稿_非良构XML_关严格XSD后仍返422不落库() throws Exception {
|
||||||
|
// 关 validateSchema=false 仅关严格 XSD,仍解析:标签不匹配是非良构 XML,解析必抛 → 422。
|
||||||
|
String body = "{\"name\":\"x\",\"key\":\"k\",\"bpmnXml\":"
|
||||||
|
+ jsonString("<definitions><process></definitions>") + "}";
|
||||||
|
mvc.perform(post("/api/flow/models").contentType(MediaType.APPLICATION_JSON).content(body))
|
||||||
|
.andExpect(status().isUnprocessableEntity())
|
||||||
|
.andExpect(jsonPath("$.status").value(1));
|
||||||
|
|
||||||
|
verify(repositoryService, never()).saveModel(any());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void 更新_草稿不存在返404() throws Exception {
|
void 更新_草稿不存在返404() throws Exception {
|
||||||
when(repositoryService.getModel("nope")).thenReturn(null);
|
when(repositoryService.getModel("nope")).thenReturn(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user