fix: 发布前审计 Batch 2 — Debug遮蔽 + unwrap + 静默吞错 + MCP锁 + 索引 + Config验证

安全:
- LlmConfig 自定义 Debug impl,api_key 显示为 "***REDACTED***"
- tsconfig.json 移除 ErrorBoundary.tsx 排除项(安全关键组件)
- billing/handlers.rs Response builder unwrap → map_err 错误传播
- classroom_commands/mod.rs db_path.parent().unwrap() → ok_or_else

静默吞错:
- approvals.rs 3处 warn→error(审批状态丢失是严重事件)
- events.rs publish() 添加 Event dropped debug 日志
- mcp_transport.rs eprintln→tracing::warn (僵尸进程风险)
- zclaw-growth sqlite.rs 4处迁移:区分 duplicate column name 与真实错误

MCP Transport:
- 合并 stdin+stdout 为单一 Mutex<TransportHandles>
- send_request write-then-read 原子化,防止并发响应错配

数据库:
- 新迁移 20260418000001: idx_rle_created_at + idx_billing_sub_plan + idx_ki_created_by

配置验证:
- SaaSConfig::load() 添加 jwt_expiration_hours>=1, max_connections>0, min<=max
This commit is contained in:
iven
2026-04-18 14:09:36 +08:00
parent f3fb5340b5
commit e10549a1b9
10 changed files with 123 additions and 53 deletions

View File

@@ -84,12 +84,20 @@ impl McpServerConfig {
}
}
/// Combined transport handles (stdin + stdout) behind a single Mutex.
/// This ensures write-then-read is atomic, preventing concurrent requests
/// from receiving each other's responses.
struct TransportHandles {
stdin: BufWriter<ChildStdin>,
stdout: BufReader<ChildStdout>,
}
/// MCP Transport using stdio
pub struct McpTransport {
config: McpServerConfig,
child: Arc<Mutex<Option<Child>>>,
stdin: Arc<Mutex<Option<BufWriter<ChildStdin>>>>,
stdout: Arc<Mutex<Option<BufReader<ChildStdout>>>>,
/// Single Mutex protecting both stdin and stdout for atomic write-then-read
handles: Arc<Mutex<Option<TransportHandles>>>,
capabilities: Arc<Mutex<Option<ServerCapabilities>>>,
}
@@ -99,8 +107,7 @@ impl McpTransport {
Self {
config,
child: Arc::new(Mutex::new(None)),
stdin: Arc::new(Mutex::new(None)),
stdout: Arc::new(Mutex::new(None)),
handles: Arc::new(Mutex::new(None)),
capabilities: Arc::new(Mutex::new(None)),
}
}
@@ -162,9 +169,11 @@ impl McpTransport {
});
}
// Store handles in separate mutexes
*self.stdin.lock().await = Some(BufWriter::new(stdin));
*self.stdout.lock().await = Some(BufReader::new(stdout));
// Store handles in single mutex for atomic write-then-read
*self.handles.lock().await = Some(TransportHandles {
stdin: BufWriter::new(stdin),
stdout: BufReader::new(stdout),
});
*child_guard = Some(child);
Ok(())
@@ -201,21 +210,21 @@ impl McpTransport {
let line = serde_json::to_string(notification)
.map_err(|e| ZclawError::McpError(format!("Failed to serialize notification: {}", e)))?;
let mut stdin_guard = self.stdin.lock().await;
let stdin = stdin_guard.as_mut()
let mut handles_guard = self.handles.lock().await;
let handles = handles_guard.as_mut()
.ok_or_else(|| ZclawError::McpError("Transport not started".to_string()))?;
stdin.write_all(line.as_bytes())
handles.stdin.write_all(line.as_bytes())
.map_err(|e| ZclawError::McpError(format!("Failed to write notification: {}", e)))?;
stdin.write_all(b"\n")
handles.stdin.write_all(b"\n")
.map_err(|e| ZclawError::McpError(format!("Failed to write newline: {}", e)))?;
stdin.flush()
handles.stdin.flush()
.map_err(|e| ZclawError::McpError(format!("Failed to flush notification: {}", e)))?;
Ok(())
}
/// Send JSON-RPC request
/// Send JSON-RPC request (atomic write-then-read under single lock)
async fn send_request<T: DeserializeOwned>(
&self,
method: &str,
@@ -234,28 +243,23 @@ impl McpTransport {
let line = serde_json::to_string(&request)
.map_err(|e| ZclawError::McpError(format!("Failed to serialize request: {}", e)))?;
// Write to stdin
{
let mut stdin_guard = self.stdin.lock().await;
let stdin = stdin_guard.as_mut()
.ok_or_else(|| ZclawError::McpError("Transport not started".to_string()))?;
stdin.write_all(line.as_bytes())
.map_err(|e| ZclawError::McpError(format!("Failed to write request: {}", e)))?;
stdin.write_all(b"\n")
.map_err(|e| ZclawError::McpError(format!("Failed to write newline: {}", e)))?;
stdin.flush()
.map_err(|e| ZclawError::McpError(format!("Failed to flush request: {}", e)))?;
}
// Read from stdout
// Atomic write-then-read under single lock
let response_line = {
let mut stdout_guard = self.stdout.lock().await;
let stdout = stdout_guard.as_mut()
let mut handles_guard = self.handles.lock().await;
let handles = handles_guard.as_mut()
.ok_or_else(|| ZclawError::McpError("Transport not started".to_string()))?;
// Write to stdin
handles.stdin.write_all(line.as_bytes())
.map_err(|e| ZclawError::McpError(format!("Failed to write request: {}", e)))?;
handles.stdin.write_all(b"\n")
.map_err(|e| ZclawError::McpError(format!("Failed to write newline: {}", e)))?;
handles.stdin.flush()
.map_err(|e| ZclawError::McpError(format!("Failed to flush request: {}", e)))?;
// Read from stdout (still holding the lock — no interleaving possible)
let mut response_line = String::new();
stdout.read_line(&mut response_line)
handles.stdout.read_line(&mut response_line)
.map_err(|e| ZclawError::McpError(format!("Failed to read response: {}", e)))?;
response_line
};
@@ -429,7 +433,7 @@ impl Drop for McpTransport {
let _ = child.wait();
}
Err(e) => {
eprintln!("[McpTransport] Failed to kill child process: {}", e);
tracing::warn!("[McpTransport] Failed to kill child process (potential zombie): {}", e);
}
}
}