fix: contain asynchronous BPM route failures to prevent service crashes

This commit is contained in:
阿白叔 committed 2026-10-18 08:31:12 +08:00
1 parent 3363a88abf
commit e2bd8c390f
3 files changed
+33 -4

No files matched your search

+8
View File
@@ -107,3 +107,11 @@
- 首版界面参考 Open WebUI 的交互方式,自行实现,共用现有身份。390 px 无横向溢出,聊天刷新恢复、卡片明确点击才执行、停止回复与 HTML 原样显示已在实际浏览器验证。
- 未配置真实供应商认证,未发送业务邮件或操作生产审批。详细来源和控制边界见 docs/zcode-integration-2026-10-18.md。
- 官方 CLI 最终专项 1/1 通过(output/verification/zcode-official-final.log):真实模型请求无 tools;恶意 Write 响应确实发送一次,攻击轮产生两次模型请求后正常回复,canary 文件不存在。接口为本机测试模型,不是付费供应商认证。
## BPM 请求异常导致工作台退出(2026-10-18)
- 正式日志捕获 `route.fulfill: Fetch response has been disposed` 未处理拒绝,Node 24 退出,18765/18766 均 `ECONNREFUSED`。问题发生于 BPM 路由异步回调,不是助手页面路由。
- 路由在 `try` 中直接返回 `route.fulfill` / `route.abort` 的 Promise,异步拒绝绕过同层 `catch`。改为 `return await`,异常进入既有兜底终止当前请求,保留域名与附件拦截规则。
- 新增 `tests/bpm-route-errors.test.cjs` 先复现同样 disposed 错误,再验证修复。覆盖响应提交失败、附件/重定向/弹窗拦截失败、fetch 失败及后续有效请求恢复;没有使用全局吞错处理。
- 正式服务已重启;首页及 bootstrap 状态 HTTP 200,真实浏览器可见登录表单、无页面脚本错误。没有使用或更改工作台密码。
- 崩溃专项 1/1、既有 BPM 真实浏览器与服务重启回归 12/12 通过;后者日志 output/verification/bpm-route-crash-regression.log。语法与 Git diff 检查通过。
+6 -4
View File
@@ -223,7 +223,7 @@ class BpmSessionManager {
try {
// A popup is never a remote-control surface. Block its first navigation,
// before even an allowed host can redirect it elsewhere.
if (route.request().isNavigationRequest() && route.request().frame().page()!==lease.page) return route.abort('blockedbyclient');
if (route.request().isNavigationRequest() && route.request().frame().page()!==lease.page) return await route.abort('blockedbyclient');
if (this.allowed(route.request().url())) {
// route.continue follows redirects without re-entering the route handler.
// Inspect each redirect before Chromium receives it, including resource redirects.
@@ -231,10 +231,12 @@ class BpmSessionManager {
const location=response.headers().location;
if (location && response.status()>=300 && response.status()<400 && !this.allowed(new URL(location,route.request().url()).href)) {
if (route.request().isNavigationRequest() && !route.request().frame().parentFrame()) lease.blocked=true;
return route.abort('blockedbyclient');
return await route.abort('blockedbyclient');
}
if (/attachment/i.test(response.headers()['content-disposition']||'')) return route.abort('blockedbyclient');
return route.fulfill({response});
if (/attachment/i.test(response.headers()['content-disposition']||'')) return await route.abort('blockedbyclient');
// Await inside this try: closing a context can dispose the fetched response
// before fulfill finishes. Returning its promise bypasses this catch.
return await route.fulfill({response});
}
if (route.request().isNavigationRequest() && !route.request().frame().parentFrame()) lease.blocked = true;
await route.abort('blockedbyclient');
+19
View File
@@ -0,0 +1,19 @@
'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path');
const {BpmSessionManager}=require('../src/bpm-session-manager.cjs');
test('disposed BPM responses and rejected navigation aborts are contained in the route handler',async t=>{
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'bpm-route-errors-'));let handler;
const origin='http://127.0.0.1:18888',page={url:()=>origin+'/',goto:async()=>{},on(){}},network={on(){},send:async method=>method==='Page.getFrameTree'?{frameTree:{frame:{id:'main'}}}:{}};
const context={route:async(_pattern,callback)=>{handler=callback;},addInitScript:async()=>{},newPage:async()=>page,newCDPSession:async()=>network,on(){},close:async()=>{}};
const manager=new BpmSessionManager({stateDir:dir,urls:{main:origin+'/'},allowedOrigins:[origin],browserFactory:async()=>({newContext:async()=>context,close:async()=>{}})});
manager._vault={read:async()=>null,destroy(){}};t.after(async()=>{await manager.shutdown();await fs.rm(dir,{recursive:true,force:true});});
await manager.open({id:'route-test',owner:'owner',status:'disconnected'},{user:{username:'owner',role:'operator'},sessionId:'route-test'});
for(const scenario of ['fulfill','attachment','redirect','popup','fetch']){
const aborted=[],request={url:()=>origin+'/',isNavigationRequest:()=>scenario==='popup',frame:()=>({page:()=>null,parentFrame:()=>null})};
const route={request:()=>request,fetch:async()=>{if(scenario==='fetch')throw Error('context closed');return {headers:()=>scenario==='attachment'?{'content-disposition':'attachment'}:scenario==='redirect'?{location:'https://forbidden.example/'}:{},status:()=>scenario==='redirect'?302:200};},fulfill:async()=>{await Promise.resolve();throw Error('Fetch response has been disposed');},abort:async reason=>{aborted.push(reason);throw Error('Route already handled');}};
await assert.doesNotReject(()=>handler(route),scenario);
assert.equal(aborted.at(-1),'failed',scenario);
}
// A failed request must not prevent handling the next valid request.
let fulfilled=false;await handler({request:()=>({url:()=>origin+'/',isNavigationRequest:()=>false}),fetch:async()=>({headers:()=>({}),status:()=>200}),fulfill:async()=>{fulfilled=true;},abort:async()=>assert.fail('valid response was aborted')});assert.equal(fulfilled,true);
});